From 524761e4e4796ac40308e3aaad62bb497ea679e5 Mon Sep 17 00:00:00 2001 From: Till Zimmermann Date: Thu, 29 Sep 2016 14:09:43 +0200 Subject: [PATCH] Fixes #94 --- src/Namshi/JOSE/Signer/OpenSSL/PublicKey.php | 35 ++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Namshi/JOSE/Signer/OpenSSL/PublicKey.php b/src/Namshi/JOSE/Signer/OpenSSL/PublicKey.php index f6556ea..3c5c6b6 100644 --- a/src/Namshi/JOSE/Signer/OpenSSL/PublicKey.php +++ b/src/Namshi/JOSE/Signer/OpenSSL/PublicKey.php @@ -1,7 +1,9 @@ ASN1::TYPE_SEQUENCE, + 'children' => [ + 'r' => [ + 'type' => ASN1::TYPE_INTEGER, + ], + 's' => [ + 'type' => ASN1::TYPE_INTEGER, + ], + ], + ]; public function sign($input, $key, $password = null) { $keyResource = $this->getKeyResource($key, $password); @@ -23,7 +37,19 @@ public function sign($input, $key, $password = null) $signature = null; openssl_sign($input, $signature, $keyResource, $this->getHashingAlgorithm()); + + $asn1Decoder = new ASN1(); + $asn1Decoded = $asn1Decoder->decodeBER($signature); + $asn1Decoded = $asn1Decoder->asn1map($asn1Decoded[0], self::$asn1Schema); + if( isset($asn1Decoded['r']) && isset($asn1Decoded['s']) && + $asn1Decoded['r'] instanceof BigInteger && + $asn1Decoded['s'] instanceof BigInteger ) { + + $signature = $asn1Decoded['r']->toBytes().$asn1Decoded['s']->toBytes(); + }else{ + throw new RuntimeException('No Signature generated'); + } return $signature; } @@ -36,9 +62,12 @@ public function verify($key, $signature, $input) if (!$this->supportsKey($keyResource)) { throw new InvalidArgumentException('Invalid key supplied.'); } - - $result = openssl_verify($input, $signature, $keyResource, $this->getHashingAlgorithm()); - + $asn1Encoder = new ASN1(); + $asn1Encoded = $asn1Encoder->encodeDER( [ + 'r'=>new BigInteger(substr($signature,0,32), 256), + 's'=>new BigInteger(substr($signature,32,32), 256) + ], self::$asn1Schema); + $result = openssl_verify($input, $asn1Encoded, $keyResource, $this->getHashingAlgorithm()); if ($result === -1) { throw new RuntimeException('Unknown error during verification.'); }