<div dir="ltr">I am implementing a JWS based specification using openSSL. My code is below, in pascal. I'm trying to reproduce this test case here: <a href="https://datatracker.ietf.org/doc/html/rfc7515#appendix-A.3.1">https://datatracker.ietf.org/doc/html/rfc7515#appendix-A.3.1</a><div><br></div><div>I get a different outcome from EVP_DigestSignInit / EVP_DigestUpdate / EVP_DigestSignFinal from that specified (MEYCIQCf4hUhJvEFLZeOE4OPWrKT_LnyyNeU_1vdXgO5gqUK2AIhAILiDUd7i-FhbspRtlM90E6oSQD6eOBgiIylORcLhQbi instead of DtEhU3ljbEg8L38VWAfUAqOyKAM6-Xx-F4GawxaepmXFCgfTjDxw5djxLa8ISlSApmWQxfKTUJqPP3-Kg6NU1Q</div><div><br></div><div>I presume that this is because of this text in the JWS spec: "The result of the digital signature is the Elliptic Curve (EC) point (R, S), where R and S are unsigned integers.  The JWS Signature is the value R || S" - is that what EVP_DigestSignFinal should return? if not, how should I get R and S? </div><div><br></div><div>Or am I thinking about this wrong somehow?</div><div><br></div><div>thanks</div><div>Grahame</div><div><br></div><div><div>Code:</div><div><br></div><div>class function TJWTUtils.Sign_ES256(input: TBytes; key: TJWK): TBytes;<br>var<br>  ctx : PEVP_MD_CTX;<br>  keysize : integer;<br>  len : Cardinal;<br>  pkey: PEVP_PKEY;<br>  rkey: PEC_KEY;<br>  keys : TJWKList;<br>begin<br>  check(key <> nil, 'A key must be provided for ES256');<br><br>  // 1. Load the RSA private Key from FKey<br>  rkey := key.LoadEC(true);<br>  try<br>    pkey := EVP_PKEY_new;<br>    try<br>      check(EVP_PKEY_set1_EC_KEY(pkey, rkey) = 1, 'openSSL EVP_PKEY_set1_RSA failed');<br><br>      // 2. do the signing<br>      keysize := EVP_PKEY_size(pkey);<br>      SetLength(result, keysize);<br>      ctx := EVP_MD_CTX_new;<br>      try<br>        check(EVP_DigestSignInit(ctx, nil, EVP_sha256, nil, pKey) = 1, 'openSSL EVP_DigestInit_ex failed');<br>        check(EVP_DigestUpdate(ctx, @input[0], Length(input)) = 1, 'openSSL EVP_SignUpdate failed');<br>        check(EVP_DigestSignFinal(ctx, @result[0], @len) = 1, 'openSSL EVP_SignFinal failed');<br>        SetLength(result, len);<br>      finally<br>        EVP_MD_CTX_free(ctx);<br>      end;<br>    finally<br>      EVP_PKEY_free(pKey);<br>    end;<br>  finally<br>    EC_KEY_free(rkey);<br>  end;<br></div><div><br></div><div>Signing the content:</div><div><br></div><div>Loading the key:</div><div><br></div><div>function TJWK.LoadEC(privkey: boolean): PEC_KEY;<br>var<br>  pd, px, py : PBIGNUM;<br>  pub : PEC_POINT;<br>  grp : PEC_GROUP;<br>begin<br>  check(keyType = 'EC', 'EC Key expected in JWK, but found '+KeyType);<br>  check(hasX, 'EC Key needs an X');<br>  check(hasY, 'EC Key needs an Y');<br><br>  px := bn_decode_bin(x);<br>  py := bn_decode_bin(y);<br>  pd := bn_decode_bin(privateKey);<br><br>  result := EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);<br>  check(result <> nil, 'EC_KEY_new_by_curve_name = nil');<br>  grp := EC_KEY_get0_group(result);<br>  pub := EC_POINT_new(grp);<br>  check(EC_POINT_set_affine_coordinates_GFp(grp, pub, px, py, nil) = 1, 'EC_POINT_set_affine_coordinates_GFp failed');<br>  EC_KEY_set_public_key(result, pub);<br><br>  if (privkey) then<br>  begin<br>    check(hasPrivateKey, 'EC Key needs an private key');<br>    EC_KEY_set_private_key(result, pd)<br>  end;<br>end;</div><div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">-----<br><a href="http://www.healthintersections.com.au" target="_blank">http://www.healthintersections.com.au</a> / <a href="mailto:grahame@healthintersections.com.au" target="_blank">grahame@healthintersections.com.au</a> / +61 411 867 065<div><font face="arial, sans-serif"><span style="color:rgb(34,34,34)">Benson & Grieve:  </span><font color="#222222"> Principles of Health Interoperability (Health Information Technology Standards), 4th ed</font></font><span style="color:rgb(34,34,34);font-family:Calibri,sans-serif;font-size:14.6667px"> </span><font face="arial, sans-serif"><span style="color:rgb(34,34,34)">-  </span><a href="http://www.springer.com/978-3-030-56882-5" style="color:rgb(17,85,204)" target="_blank">http://www.springer.com/978-3-030-56882-5</a></font></div></div></div></div></div></div>