@@ -117,6 +117,21 @@ unsigned int GetBytesOfRS(const ManagedEVPPKey& pkey) { |
117 | 117 | return (bits + 7) / 8; |
118 | 118 | } |
119 | 119 |
|
| 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 | + |
120 | 135 | // Returns the maximum size of each of the integers (r, s) of the DSA signature. |
121 | 136 | AllocatedBuffer ConvertSignatureToP1363(Environment* env, |
122 | 137 | const ManagedEVPPKey& pkey, |
@@ -128,33 +143,49 @@ AllocatedBuffer ConvertSignatureToP1363(Environment* env, |
128 | 143 | const unsigned char* sig_data = |
129 | 144 | reinterpret_cast<unsigned char*>(signature.data()); |
130 | 145 |
|
131 | | - ECDSASigPointer asn1_sig(d2i_ECDSA_SIG(nullptr, &sig_data, signature.size())); |
132 | | - if (!asn1_sig) |
133 | | - return AllocatedBuffer(); |
134 | | - |
135 | 146 | AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, 2 * n); |
136 | 147 | unsigned char* data = reinterpret_cast<unsigned char*>(buf.data()); |
137 | 148 |
|
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); |
142 | 151 |
|
143 | 152 | return buf; |
144 | 153 | } |
145 | 154 |
|
| 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 | +} |
146 | 177 |
|
147 | 178 | ByteSource ConvertSignatureToDER( |
148 | 179 | const ManagedEVPPKey& pkey, |
149 | | - const ArrayBufferOrViewContents<char>& signature) { |
| 180 | + ByteSource&& out) { |
150 | 181 | unsigned int n = GetBytesOfRS(pkey); |
151 | 182 | if (n == kNoDsaSignature) |
152 | | - return signature.ToByteSource(); |
| 183 | + return std::move(out); |
153 | 184 |
|
154 | 185 | const unsigned char* sig_data = |
155 | | - reinterpret_cast<const unsigned char*>(signature.data()); |
| 186 | + reinterpret_cast<const unsigned char*>(out.get()); |
156 | 187 |
|
157 | | - if (signature.size() != 2 * n) |
| 188 | + if (out.size() != 2 * n) |
158 | 189 | return ByteSource(); |
159 | 190 |
|
160 | 191 | ECDSASigPointer asn1_sig(ECDSA_SIG_new()); |
@@ -511,7 +542,7 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) { |
511 | 542 |
|
512 | 543 | ByteSource signature = hbuf.ToByteSource(); |
513 | 544 | if (dsa_sig_enc == kSigEncP1363) { |
514 | | - signature = ConvertSignatureToDER(pkey, hbuf); |
| 545 | + signature = ConvertSignatureToDER(pkey, hbuf.ToByteSource()); |
515 | 546 | if (signature.get() == nullptr) |
516 | 547 | return crypto::CheckThrow(env, Error::kSignMalformedSignature); |
517 | 548 | } |
@@ -657,7 +688,7 @@ void Verify::VerifySync(const FunctionCallbackInfo<Value>& args) { |
657 | 688 |
|
658 | 689 | ByteSource sig_bytes = ByteSource::Foreign(sig.data(), sig.size()); |
659 | 690 | if (dsa_sig_enc == kSigEncP1363) { |
660 | | - sig_bytes = ConvertSignatureToDER(key, sig); |
| 691 | + sig_bytes = ConvertSignatureToDER(key, sig.ToByteSource()); |
661 | 692 | if (!sig_bytes) |
662 | 693 | return crypto::CheckThrow(env, SignBase::Error::kSignMalformedSignature); |
663 | 694 | } |
@@ -778,7 +809,7 @@ Maybe<bool> SignTraits::AdditionalConfig( |
778 | 809 | Mutex::ScopedLock lock(*m_pkey.mutex()); |
779 | 810 | if (UseP1363Encoding(m_pkey, params->dsa_encoding)) { |
780 | 811 | params->signature = |
781 | | - ConvertFromWebCryptoSignature(m_pkey, signature.ToByteSource()); |
| 812 | + ConvertSignatureToDER(m_pkey, signature.ToByteSource()); |
782 | 813 | } else { |
783 | 814 | params->signature = mode == kCryptoJobAsync |
784 | 815 | ? signature.ToCopy() |
@@ -874,8 +905,10 @@ bool SignTraits::DeriveBits( |
874 | 905 | return false; |
875 | 906 |
|
876 | 907 | 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); |
879 | 912 | } else { |
880 | 913 | buf.Resize(len); |
881 | 914 | *out = std::move(buf); |
|