Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <openssl/ec.h> | |
| 6 #include <openssl/ec_key.h> | |
| 7 #include <openssl/evp.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "content/child/webcrypto/algorithm_implementation.h" | |
| 11 #include "content/child/webcrypto/crypto_data.h" | |
| 12 #include "content/child/webcrypto/generate_key_result.h" | |
| 13 #include "content/child/webcrypto/openssl/ec_key_openssl.h" | |
| 14 #include "content/child/webcrypto/openssl/key_openssl.h" | |
| 15 #include "content/child/webcrypto/openssl/util_openssl.h" | |
| 16 #include "content/child/webcrypto/status.h" | |
| 17 #include "content/child/webcrypto/webcrypto_util.h" | |
| 18 #include "crypto/openssl_util.h" | |
| 19 #include "crypto/scoped_openssl_types.h" | |
| 20 #include "crypto/secure_util.h" | |
| 21 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
| 22 #include "third_party/WebKit/public/platform/WebCryptoKey.h" | |
| 23 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | |
| 24 | |
| 25 namespace content { | |
| 26 | |
| 27 namespace webcrypto { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 // Extracts the OpenSSL key and digest from a WebCrypto key + algorithm. The | |
| 32 // returned pkey pointer will remain valid as long as |key| is alive. | |
| 33 Status GetPKeyAndDigest(const blink::WebCryptoAlgorithm& algorithm, | |
| 34 const blink::WebCryptoKey& key, | |
| 35 EVP_PKEY** pkey, | |
| 36 const EVP_MD** digest) { | |
| 37 *pkey = AsymKeyOpenSsl::Cast(key)->key(); | |
| 38 *digest = GetDigest(algorithm.ecdsaParams()->hash().id()); | |
| 39 if (!*digest) | |
| 40 return Status::ErrorUnsupported(); | |
| 41 return Status::Success(); | |
| 42 } | |
| 43 | |
| 44 // Gets the EC key's order size in bytes. | |
| 45 // | |
| 46 // TODO(eroman): Is there a more direct way of doing this? In BoringSSL's | |
| 47 // ECDSA_size() implementation it calls ec->ecdsa_meth->group_order_size(ec). | |
|
davidben
2014/11/07 00:17:21
We could probably expose something of the sort. (A
eroman
2014/11/08 01:52:57
I have replaced this with a call to EC_GROUP_get_d
| |
| 48 Status GetEcGroupOrderSize(EVP_PKEY* pkey, size_t* order_size_bytes) { | |
| 49 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
| 50 | |
| 51 crypto::ScopedEC_KEY ec(EVP_PKEY_get1_EC_KEY(pkey)); | |
| 52 if (!ec.get()) | |
| 53 return Status::ErrorUnexpected(); | |
| 54 | |
| 55 const EC_GROUP* group = EC_KEY_get0_group(ec.get()); | |
| 56 | |
| 57 crypto::ScopedBIGNUM order(BN_new()); | |
| 58 if (!EC_GROUP_get_order(group, order.get(), NULL)) | |
| 59 return Status::OperationError(); | |
| 60 | |
| 61 *order_size_bytes = BN_num_bytes(order.get()); | |
| 62 return Status::Success(); | |
| 63 } | |
| 64 | |
| 65 // Formats a DER-encoded signature (ECDSA-Sig-Value as specified in RFC 3279) to | |
| 66 // the signature format expected by WebCrypto (raw concatenated "r" and "s"). | |
| 67 // | |
| 68 // TODO(eroman): Where is the specification for WebCrypto's signature format? | |
| 69 Status ConvertDerSignatureToWebCryptoSignature( | |
| 70 EVP_PKEY* key, | |
| 71 std::vector<uint8_t>* signature) { | |
| 72 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
| 73 | |
| 74 const unsigned char* der_data = &signature->front(); | |
| 75 crypto::ScopedECDSA_SIG ecdsa_sig( | |
| 76 d2i_ECDSA_SIG(NULL, &der_data, static_cast<long>(signature->size()))); | |
| 77 if (!ecdsa_sig.get()) | |
|
davidben
2014/11/07 00:17:21
|| der_data != &signature->front() + signature->si
eroman
2014/11/08 01:52:57
Actually, see the comment below.
I have been find
davidben
2014/11/10 20:23:59
BoringSSL really shouldn't be padding that. At a g
eroman
2014/11/10 20:55:52
I will debug this further and get back to you.
Not
eroman
2014/11/10 22:02:59
Added the length check.
(I can no longer reproduc
| |
| 78 return Status::OperationError(); | |
| 79 | |
| 80 // Note that |der_data| is updated to point to past the end of the DER | |
| 81 // structure. It is possible there are unconsumed bytes at the end, and | |
| 82 // this is OK. (Since the ECDSA signature appears to be padded up to | |
| 83 // ECDSA_size()). | |
| 84 | |
| 85 // Determine the maximum length of r and s. | |
| 86 size_t order_size_bytes; | |
| 87 Status status = GetEcGroupOrderSize(key, &order_size_bytes); | |
| 88 if (status.IsError()) | |
| 89 return status; | |
| 90 | |
|
davidben
2014/11/07 00:17:21
This dance can just be two calls to BN_bn2bin_padd
eroman
2014/11/08 01:52:57
Done.
| |
| 91 const BIGNUM* r = ecdsa_sig.get()->r; | |
| 92 const BIGNUM* s = ecdsa_sig.get()->s; | |
| 93 unsigned int r_size_bytes = BN_num_bytes(r); | |
| 94 unsigned int s_size_bytes = BN_num_bytes(s); | |
| 95 | |
| 96 // This shouldn't happen since the values of "r" and "s" were computed by | |
| 97 // BoringSSL. | |
| 98 if (r_size_bytes > order_size_bytes || s_size_bytes > order_size_bytes) | |
| 99 return Status::ErrorUnexpected(); | |
| 100 | |
| 101 // Concatenate "r" and "s", zero-padding each to length |order_size_bytes|. | |
| 102 signature->clear(); | |
| 103 signature->resize(order_size_bytes * 2, 0); | |
| 104 BN_bn2bin(ecdsa_sig.get()->r, | |
| 105 &((*signature)[order_size_bytes - r_size_bytes])); | |
| 106 BN_bn2bin(ecdsa_sig.get()->s, | |
| 107 &((*signature)[signature->size() - s_size_bytes])); | |
| 108 return Status::Success(); | |
| 109 } | |
| 110 | |
| 111 // Formats a WebCrypto ECDSA signature to a DER-encoded signature | |
| 112 // (ECDSA-Sig-Value as specified in RFC 3279). | |
| 113 // | |
| 114 // TODO(eroman): What is the specification for WebCrypto's signature format? | |
| 115 // | |
| 116 // If the signature length is incorrect (not 2 * order_size), then | |
| 117 // Status::Success() is returned and |*incorrect_length| is set to true; | |
| 118 // | |
| 119 // Otherwise on success, der_signature is filled with a ASN.1 encoded | |
| 120 // ECDSA-Sig-Value. | |
| 121 Status ConvertWebCryptoSignatureToDerSignature( | |
| 122 EVP_PKEY* key, | |
| 123 const CryptoData& signature, | |
| 124 std::vector<uint8_t>* der_signature, | |
| 125 bool* incorrect_length) { | |
| 126 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
| 127 | |
| 128 // Determine the length of r and s. | |
| 129 size_t order_size_bytes; | |
| 130 Status status = GetEcGroupOrderSize(key, &order_size_bytes); | |
| 131 if (status.IsError()) | |
| 132 return status; | |
| 133 | |
| 134 // If the size of the signature is incorrect, verification must fail. Success | |
| 135 // is returned here rather than an error, so that the caller can fail | |
| 136 // verification with a boolean, rather than reject the promise with an | |
| 137 // exception. | |
| 138 if (signature.byte_length() != 2 * order_size_bytes) { | |
| 139 *incorrect_length = true; | |
| 140 return Status::Success(); | |
| 141 } | |
| 142 | |
| 143 *incorrect_length = false; | |
| 144 | |
| 145 // Construct an ECDSA_SIG from |signature|. | |
| 146 crypto::ScopedECDSA_SIG ecdsa_sig(ECDSA_SIG_new()); | |
| 147 if (!ecdsa_sig) | |
| 148 return Status::OperationError(); | |
| 149 | |
| 150 ecdsa_sig->r = BN_bin2bn(signature.bytes(), order_size_bytes, NULL); | |
| 151 ecdsa_sig->s = | |
| 152 BN_bin2bn(signature.bytes() + order_size_bytes, order_size_bytes, NULL); | |
|
davidben
2014/11/07 00:17:21
BN_bin2bn can return NULL. Dunno if you want to ch
eroman
2014/11/08 01:52:57
Done.
| |
| 153 | |
| 154 // Determine the size of the DER-encoded signature. | |
| 155 int der_encoding_size = i2d_ECDSA_SIG(ecdsa_sig.get(), NULL); | |
| 156 if (der_encoding_size < 0) | |
| 157 return Status::OperationError(); | |
| 158 | |
| 159 // DER-encode the signature. | |
| 160 der_signature->resize(der_encoding_size); | |
| 161 uint8_t* result = &der_signature->front(); | |
| 162 if (0 > i2d_ECDSA_SIG(ecdsa_sig.get(), &result)) | |
| 163 return Status::OperationError(); | |
| 164 | |
| 165 return Status::Success(); | |
| 166 } | |
| 167 | |
| 168 class EcdsaImplementation : public EcAlgorithm { | |
| 169 public: | |
| 170 EcdsaImplementation() | |
| 171 : EcAlgorithm(blink::WebCryptoKeyUsageVerify, | |
| 172 blink::WebCryptoKeyUsageSign) {} | |
| 173 | |
| 174 const char* GetJwkAlgorithm( | |
| 175 const blink::WebCryptoNamedCurve curve) const override { | |
| 176 switch (curve) { | |
| 177 case blink::WebCryptoNamedCurveP256: | |
| 178 return "ES256"; | |
| 179 case blink::WebCryptoNamedCurveP384: | |
| 180 return "ES384"; | |
| 181 case blink::WebCryptoNamedCurveP521: | |
| 182 // This is not a typo! ES512 means P-521 with SHA-512. | |
| 183 return "ES512"; | |
| 184 default: | |
| 185 return NULL; | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 Status Sign(const blink::WebCryptoAlgorithm& algorithm, | |
| 190 const blink::WebCryptoKey& key, | |
| 191 const CryptoData& data, | |
| 192 std::vector<uint8_t>* buffer) const override { | |
| 193 if (key.type() != blink::WebCryptoKeyTypePrivate) | |
| 194 return Status::ErrorUnexpectedKeyType(); | |
| 195 | |
| 196 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
| 197 crypto::ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); | |
| 198 | |
| 199 EVP_PKEY* private_key = NULL; | |
| 200 const EVP_MD* digest = NULL; | |
| 201 Status status = GetPKeyAndDigest(algorithm, key, &private_key, &digest); | |
| 202 if (status.IsError()) | |
| 203 return status; | |
| 204 | |
| 205 // NOTE: A call to EVP_DigestSignFinal() with a NULL second parameter | |
| 206 // returns a maximum allocation size, while the call without a NULL returns | |
| 207 // the real one, which may be smaller. | |
| 208 size_t sig_len = 0; | |
| 209 if (!ctx.get() || | |
| 210 !EVP_DigestSignInit(ctx.get(), NULL, digest, NULL, private_key) || | |
| 211 !EVP_DigestSignUpdate(ctx.get(), data.bytes(), data.byte_length()) || | |
| 212 !EVP_DigestSignFinal(ctx.get(), NULL, &sig_len)) { | |
| 213 return Status::OperationError(); | |
| 214 } | |
| 215 | |
| 216 buffer->resize(sig_len); | |
| 217 if (!EVP_DigestSignFinal(ctx.get(), &buffer->front(), &sig_len)) | |
| 218 return Status::OperationError(); | |
| 219 buffer->resize(sig_len); | |
| 220 | |
| 221 // ECDSA signing in BoringSSL outputs a DER-encoded (r,s). WebCrypto however | |
| 222 // expects a padded bitstring that is r concatenated to s. Convert to the | |
| 223 // expected format. | |
| 224 return ConvertDerSignatureToWebCryptoSignature(private_key, buffer); | |
| 225 } | |
| 226 | |
| 227 Status Verify(const blink::WebCryptoAlgorithm& algorithm, | |
| 228 const blink::WebCryptoKey& key, | |
| 229 const CryptoData& signature, | |
| 230 const CryptoData& data, | |
| 231 bool* signature_match) const override { | |
| 232 if (key.type() != blink::WebCryptoKeyTypePublic) | |
| 233 return Status::ErrorUnexpectedKeyType(); | |
| 234 | |
| 235 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
| 236 crypto::ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); | |
| 237 | |
| 238 EVP_PKEY* public_key = NULL; | |
| 239 const EVP_MD* digest = NULL; | |
| 240 Status status = GetPKeyAndDigest(algorithm, key, &public_key, &digest); | |
| 241 if (status.IsError()) | |
| 242 return status; | |
| 243 | |
| 244 std::vector<uint8_t> der_signature; | |
| 245 bool incorrect_length_signature; | |
| 246 status = ConvertWebCryptoSignatureToDerSignature( | |
| 247 public_key, signature, &der_signature, &incorrect_length_signature); | |
| 248 if (status.IsError()) | |
| 249 return status; | |
| 250 | |
| 251 if (incorrect_length_signature) { | |
| 252 *signature_match = false; | |
| 253 return Status::Success(); | |
| 254 } | |
| 255 | |
| 256 if (!EVP_DigestVerifyInit(ctx.get(), NULL, digest, NULL, public_key) || | |
| 257 !EVP_DigestVerifyUpdate(ctx.get(), data.bytes(), data.byte_length())) { | |
| 258 return Status::OperationError(); | |
| 259 } | |
| 260 | |
| 261 // Note that the return value can be: | |
| 262 // 1 --> Success | |
| 263 // 0 --> Verification failed | |
| 264 // <0 --> Operation error (including if DER-encoded ECDSA signature cannot | |
| 265 // be decoded). However this error should not be possible since the | |
| 266 // DER was created by BoringSSL. | |
| 267 int rv = EVP_DigestVerifyFinal(ctx.get(), &der_signature.front(), | |
| 268 der_signature.size()); | |
| 269 *signature_match = rv == 1; | |
| 270 return rv >= 0 ? Status::Success() : Status::OperationError(); | |
|
davidben
2014/11/07 00:17:21
Actually this just returns 0 or 1 now, as of
https
eroman
2014/11/08 01:52:57
Done.
| |
| 271 } | |
| 272 }; | |
| 273 | |
| 274 } // namespace | |
| 275 | |
| 276 AlgorithmImplementation* CreatePlatformEcdsaImplementation() { | |
| 277 return new EcdsaImplementation; | |
| 278 } | |
| 279 | |
| 280 } // namespace webcrypto | |
| 281 | |
| 282 } // namespace content | |
| OLD | NEW |