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 "content/child/webcrypto/crypto_data.h" |
| 6 #include "content/child/webcrypto/openssl/key_openssl.h" |
| 7 #include "content/child/webcrypto/openssl/rsa_key_openssl.h" |
| 8 #include "content/child/webcrypto/openssl/rsa_sign_openssl.h" |
| 9 #include "content/child/webcrypto/openssl/util_openssl.h" |
| 10 #include "content/child/webcrypto/status.h" |
| 11 #include "crypto/openssl_util.h" |
| 12 #include "crypto/scoped_openssl_types.h" |
| 13 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 namespace webcrypto { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Extracts the OpenSSL key and digest from a WebCrypto key. The returned |
| 22 // pointers will remain valid as long as |key| is alive. |
| 23 Status GetPKeyAndDigest(const blink::WebCryptoKey& key, |
| 24 EVP_PKEY** pkey, |
| 25 const EVP_MD** digest) { |
| 26 *pkey = AsymKeyOpenSsl::Cast(key)->key(); |
| 27 |
| 28 *digest = GetDigest(key.algorithm().rsaHashedParams()->hash().id()); |
| 29 if (!*digest) |
| 30 return Status::ErrorUnsupported(); |
| 31 |
| 32 return Status::Success(); |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 Status RsaSign(const blink::WebCryptoKey& key, |
| 38 const CryptoData& data, |
| 39 std::vector<uint8_t>* buffer) { |
| 40 if (key.type() != blink::WebCryptoKeyTypePrivate) |
| 41 return Status::ErrorUnexpectedKeyType(); |
| 42 |
| 43 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 44 crypto::ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); |
| 45 |
| 46 EVP_PKEY* private_key = NULL; |
| 47 const EVP_MD* digest = NULL; |
| 48 Status status = GetPKeyAndDigest(key, &private_key, &digest); |
| 49 if (status.IsError()) |
| 50 return status; |
| 51 |
| 52 // NOTE: A call to EVP_DigestSignFinal() with a NULL second parameter |
| 53 // returns a maximum allocation size, while the call without a NULL returns |
| 54 // the real one, which may be smaller. |
| 55 size_t sig_len = 0; |
| 56 if (!ctx.get() || |
| 57 !EVP_DigestSignInit(ctx.get(), NULL, digest, NULL, private_key) || |
| 58 !EVP_DigestSignUpdate(ctx.get(), data.bytes(), data.byte_length()) || |
| 59 !EVP_DigestSignFinal(ctx.get(), NULL, &sig_len)) { |
| 60 return Status::OperationError(); |
| 61 } |
| 62 |
| 63 buffer->resize(sig_len); |
| 64 if (!EVP_DigestSignFinal(ctx.get(), &buffer->front(), &sig_len)) |
| 65 return Status::OperationError(); |
| 66 |
| 67 buffer->resize(sig_len); |
| 68 return Status::Success(); |
| 69 } |
| 70 |
| 71 Status RsaVerify(const blink::WebCryptoKey& key, |
| 72 const CryptoData& signature, |
| 73 const CryptoData& data, |
| 74 bool* signature_match) { |
| 75 if (key.type() != blink::WebCryptoKeyTypePublic) |
| 76 return Status::ErrorUnexpectedKeyType(); |
| 77 |
| 78 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 79 crypto::ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); |
| 80 |
| 81 EVP_PKEY* public_key = NULL; |
| 82 const EVP_MD* digest = NULL; |
| 83 Status status = GetPKeyAndDigest(key, &public_key, &digest); |
| 84 if (status.IsError()) |
| 85 return status; |
| 86 |
| 87 if (!EVP_DigestVerifyInit(ctx.get(), NULL, digest, NULL, public_key)) |
| 88 return Status::OperationError(); |
| 89 |
| 90 if (!EVP_DigestVerifyUpdate(ctx.get(), data.bytes(), data.byte_length())) |
| 91 return Status::OperationError(); |
| 92 |
| 93 // Note that the return value can be: |
| 94 // 1 --> Success |
| 95 // 0 --> Verification failed |
| 96 // <0 --> Operation error |
| 97 int rv = EVP_DigestVerifyFinal( |
| 98 ctx.get(), signature.bytes(), signature.byte_length()); |
| 99 *signature_match = rv == 1; |
| 100 return rv >= 0 ? Status::Success() : Status::OperationError(); |
| 101 } |
| 102 |
| 103 } // namespace webcrypto |
| 104 |
| 105 } // namespace content |
OLD | NEW |