@@ -117,6 +117,21 @@ unsigned int GetBytesOfRS(const ManagedEVPPKey& pkey) {
117117 return (bits + 7 ) / 8 ;
118118}
119119
120+ bool ExtractP1363 (
121+ const unsigned char * sig_data,
122+ unsigned char * out,
123+ size_t len,
124+ size_t n) {
125+ ECDSASigPointer asn1_sig (d2i_ECDSA_SIG (nullptr , &sig_data, len));
126+ if (!asn1_sig)
127+ return false ;
128+
129+ const BIGNUM* pr = ECDSA_SIG_get0_r (asn1_sig.get ());
130+ const BIGNUM* ps = ECDSA_SIG_get0_s (asn1_sig.get ());
131+
132+ return BN_bn2binpad (pr, out, n) > 0 && BN_bn2binpad (ps, out + n, n) > 0 ;
133+ }
134+
120135// Returns the maximum size of each of the integers (r, s) of the DSA signature.
121136AllocatedBuffer ConvertSignatureToP1363 (Environment* env,
122137 const ManagedEVPPKey& pkey,
@@ -128,33 +143,49 @@ AllocatedBuffer ConvertSignatureToP1363(Environment* env,
128143 const unsigned char * sig_data =
129144 reinterpret_cast <unsigned char *>(signature.data ());
130145
131- ECDSASigPointer asn1_sig (d2i_ECDSA_SIG (nullptr , &sig_data, signature.size ()));
132- if (!asn1_sig)
133- return AllocatedBuffer ();
134-
135146 AllocatedBuffer buf = AllocatedBuffer::AllocateManaged (env, 2 * n);
136147 unsigned char * data = reinterpret_cast <unsigned char *>(buf.data ());
137148
138- const BIGNUM* r = ECDSA_SIG_get0_r (asn1_sig.get ());
139- const BIGNUM* s = ECDSA_SIG_get0_s (asn1_sig.get ());
140- CHECK_EQ (n, static_cast <unsigned int >(BN_bn2binpad (r, data, n)));
141- CHECK_EQ (n, static_cast <unsigned int >(BN_bn2binpad (s, data + n, n)));
149+ if (!ExtractP1363 (sig_data, data, signature.size (), n))
150+ return std::move (signature);
142151
143152 return buf;
144153}
145154
155+ // Returns the maximum size of each of the integers (r, s) of the DSA signature.
156+ ByteSource ConvertSignatureToP1363 (
157+ Environment* env,
158+ const ManagedEVPPKey& pkey,
159+ const ByteSource& signature) {
160+ unsigned int n = GetBytesOfRS (pkey);
161+ if (n == kNoDsaSignature )
162+ return ByteSource ();
163+
164+ const unsigned char * sig_data =
165+ reinterpret_cast <const unsigned char *>(signature.get ());
166+
167+ char * outdata = MallocOpenSSL<char >(n * 2 );
168+ memset (outdata, 0 , n * 2 );
169+ ByteSource out = ByteSource::Allocated (outdata, n * 2 );
170+ unsigned char * ptr = reinterpret_cast <unsigned char *>(outdata);
171+
172+ if (!ExtractP1363 (sig_data, ptr, signature.size (), n))
173+ return ByteSource ();
174+
175+ return out;
176+ }
146177
147178ByteSource ConvertSignatureToDER (
148179 const ManagedEVPPKey& pkey,
149- const ArrayBufferOrViewContents< char >& signature ) {
180+ ByteSource&& out ) {
150181 unsigned int n = GetBytesOfRS (pkey);
151182 if (n == kNoDsaSignature )
152- return signature. ToByteSource ( );
183+ return std::move (out );
153184
154185 const unsigned char * sig_data =
155- reinterpret_cast <const unsigned char *>(signature. data ());
186+ reinterpret_cast <const unsigned char *>(out. get ());
156187
157- if (signature .size () != 2 * n)
188+ if (out .size () != 2 * n)
158189 return ByteSource ();
159190
160191 ECDSASigPointer asn1_sig (ECDSA_SIG_new ());
@@ -511,7 +542,7 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
511542
512543 ByteSource signature = hbuf.ToByteSource ();
513544 if (dsa_sig_enc == kSigEncP1363 ) {
514- signature = ConvertSignatureToDER (pkey, hbuf);
545+ signature = ConvertSignatureToDER (pkey, hbuf. ToByteSource () );
515546 if (signature.get () == nullptr )
516547 return crypto::CheckThrow (env, Error::kSignMalformedSignature );
517548 }
@@ -657,7 +688,7 @@ void Verify::VerifySync(const FunctionCallbackInfo<Value>& args) {
657688
658689 ByteSource sig_bytes = ByteSource::Foreign (sig.data (), sig.size ());
659690 if (dsa_sig_enc == kSigEncP1363 ) {
660- sig_bytes = ConvertSignatureToDER (key, sig);
691+ sig_bytes = ConvertSignatureToDER (key, sig. ToByteSource () );
661692 if (!sig_bytes)
662693 return crypto::CheckThrow (env, SignBase::Error::kSignMalformedSignature );
663694 }
@@ -778,7 +809,7 @@ Maybe<bool> SignTraits::AdditionalConfig(
778809 Mutex::ScopedLock lock (*m_pkey.mutex ());
779810 if (UseP1363Encoding (m_pkey, params->dsa_encoding )) {
780811 params->signature =
781- ConvertFromWebCryptoSignature (m_pkey, signature.ToByteSource ());
812+ ConvertSignatureToDER (m_pkey, signature.ToByteSource ());
782813 } else {
783814 params->signature = mode == kCryptoJobAsync
784815 ? signature.ToCopy ()
@@ -874,8 +905,10 @@ bool SignTraits::DeriveBits(
874905 return false ;
875906
876907 if (UseP1363Encoding (m_pkey, params.dsa_encoding )) {
877- *out = ConvertToWebCryptoSignature (
878- params.key ->GetAsymmetricKey (), buf);
908+ *out = ConvertSignatureToP1363 (
909+ env,
910+ params.key ->GetAsymmetricKey (),
911+ buf);
879912 } else {
880913 buf.Resize (len);
881914 *out = std::move (buf);
0 commit comments