OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <openssl/evp.h> | 5 #include <openssl/evp.h> |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "content/child/webcrypto/crypto_data.h" | 8 #include "content/child/webcrypto/crypto_data.h" |
9 #include "content/child/webcrypto/openssl/key_openssl.h" | 9 #include "content/child/webcrypto/openssl/key_openssl.h" |
10 #include "content/child/webcrypto/openssl/rsa_key_openssl.h" | 10 #include "content/child/webcrypto/openssl/rsa_key_openssl.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 44 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
45 | 45 |
46 EVP_PKEY* pkey = AsymKeyOpenSsl::Cast(key)->key(); | 46 EVP_PKEY* pkey = AsymKeyOpenSsl::Cast(key)->key(); |
47 const EVP_MD* digest = | 47 const EVP_MD* digest = |
48 GetDigest(key.algorithm().rsaHashedParams()->hash().id()); | 48 GetDigest(key.algorithm().rsaHashedParams()->hash().id()); |
49 if (!digest) | 49 if (!digest) |
50 return Status::ErrorUnsupported(); | 50 return Status::ErrorUnsupported(); |
51 | 51 |
52 crypto::ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(pkey, NULL)); | 52 crypto::ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(pkey, NULL)); |
53 | 53 |
54 if (1 != init_func(ctx.get()) || | 54 if (!init_func(ctx.get()) || |
55 1 != EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_PKCS1_OAEP_PADDING) || | 55 1 != EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_PKCS1_OAEP_PADDING) || |
56 1 != EVP_PKEY_CTX_set_rsa_oaep_md(ctx.get(), digest) || | 56 1 != EVP_PKEY_CTX_set_rsa_oaep_md(ctx.get(), digest) || |
57 1 != EVP_PKEY_CTX_set_rsa_mgf1_md(ctx.get(), digest)) { | 57 1 != EVP_PKEY_CTX_set_rsa_mgf1_md(ctx.get(), digest)) { |
58 return Status::OperationError(); | 58 return Status::OperationError(); |
59 } | 59 } |
60 | 60 |
61 const blink::WebVector<uint8_t>& label = | 61 const blink::WebVector<uint8_t>& label = |
62 algorithm.rsaOaepParams()->optionalLabel(); | 62 algorithm.rsaOaepParams()->optionalLabel(); |
63 | 63 |
64 if (label.size()) { | 64 if (label.size()) { |
65 // Make a copy of the label, since the ctx takes ownership of it when | 65 // Make a copy of the label, since the ctx takes ownership of it when |
66 // calling set0_rsa_oaep_label(). | 66 // calling set0_rsa_oaep_label(). |
67 crypto::ScopedOpenSSLBytes label_copy; | 67 crypto::ScopedOpenSSLBytes label_copy; |
68 label_copy.reset(static_cast<uint8_t*>(OPENSSL_malloc(label.size()))); | 68 label_copy.reset(static_cast<uint8_t*>(OPENSSL_malloc(label.size()))); |
69 memcpy(label_copy.get(), label.data(), label.size()); | 69 memcpy(label_copy.get(), label.data(), label.size()); |
70 | 70 |
71 if (1 != EVP_PKEY_CTX_set0_rsa_oaep_label( | 71 if (1 != EVP_PKEY_CTX_set0_rsa_oaep_label( |
72 ctx.get(), label_copy.release(), label.size())) { | 72 ctx.get(), label_copy.release(), label.size())) { |
73 return Status::OperationError(); | 73 return Status::OperationError(); |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 // Determine the maximum length of the output. | 77 // Determine the maximum length of the output. |
78 size_t outlen = 0; | 78 size_t outlen = 0; |
79 if (1 != encrypt_decrypt_func( | 79 if (!encrypt_decrypt_func( |
80 ctx.get(), NULL, &outlen, data.bytes(), data.byte_length())) { | 80 ctx.get(), NULL, &outlen, data.bytes(), data.byte_length())) { |
81 return Status::OperationError(); | 81 return Status::OperationError(); |
82 } | 82 } |
83 buffer->resize(outlen); | 83 buffer->resize(outlen); |
84 | 84 |
85 // Do the actual encryption/decryption. | 85 // Do the actual encryption/decryption. |
86 if (1 != encrypt_decrypt_func(ctx.get(), | 86 if (!encrypt_decrypt_func(ctx.get(), |
87 vector_as_array(buffer), | 87 vector_as_array(buffer), |
88 &outlen, | 88 &outlen, |
89 data.bytes(), | 89 data.bytes(), |
90 data.byte_length())) { | 90 data.byte_length())) { |
91 return Status::OperationError(); | 91 return Status::OperationError(); |
92 } | 92 } |
93 buffer->resize(outlen); | 93 buffer->resize(outlen); |
94 | 94 |
95 return Status::Success(); | 95 return Status::Success(); |
96 } | 96 } |
97 | 97 |
98 class RsaOaepImplementation : public RsaHashedAlgorithm { | 98 class RsaOaepImplementation : public RsaHashedAlgorithm { |
99 public: | 99 public: |
100 RsaOaepImplementation() | 100 RsaOaepImplementation() |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 | 144 |
145 } // namespace | 145 } // namespace |
146 | 146 |
147 AlgorithmImplementation* CreatePlatformRsaOaepImplementation() { | 147 AlgorithmImplementation* CreatePlatformRsaOaepImplementation() { |
148 return new RsaOaepImplementation; | 148 return new RsaOaepImplementation; |
149 } | 149 } |
150 | 150 |
151 } // namespace webcrypto | 151 } // namespace webcrypto |
152 | 152 |
153 } // namespace content | 153 } // namespace content |
OLD | NEW |