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/evp.h> | |
6 | |
7 #include "base/stl_util.h" | |
8 #include "content/child/webcrypto/crypto_data.h" | |
9 #include "content/child/webcrypto/openssl/key_openssl.h" | |
10 #include "content/child/webcrypto/openssl/rsa_hashed_algorithm_openssl.h" | |
11 #include "content/child/webcrypto/openssl/util_openssl.h" | |
12 #include "content/child/webcrypto/status.h" | |
13 #include "crypto/openssl_util.h" | |
14 #include "crypto/scoped_openssl_types.h" | |
15 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
16 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | |
17 | |
18 namespace content { | |
19 | |
20 namespace webcrypto { | |
21 | |
22 namespace { | |
23 | |
24 typedef int (*InitFunc)(EVP_PKEY_CTX* ctx); | |
25 typedef int (*EncryptDecryptFunc)(EVP_PKEY_CTX* ctx, | |
26 unsigned char* out, | |
27 size_t* outlen, | |
28 const unsigned char* in, | |
29 size_t inlen); | |
30 | |
31 // Helper for doing either RSA-OAEP encryption or decryption. | |
32 // | |
33 // To encrypt call with: | |
34 // init_func=EVP_PKEY_encrypt_init, encrypt_decrypt_func=EVP_PKEY_encrypt | |
35 // | |
36 // To decrypt call with: | |
37 // init_func=EVP_PKEY_decrypt_init, encrypt_decrypt_func=EVP_PKEY_decrypt | |
38 Status CommonEncryptDecrypt(InitFunc init_func, | |
39 EncryptDecryptFunc encrypt_decrypt_func, | |
40 const blink::WebCryptoAlgorithm& algorithm, | |
41 const blink::WebCryptoKey& key, | |
42 const CryptoData& data, | |
43 std::vector<uint8_t>* buffer) { | |
44 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
45 | |
46 EVP_PKEY* pkey = AsymKeyOpenSsl::Cast(key)->key(); | |
47 const EVP_MD* digest = | |
48 GetDigest(key.algorithm().rsaHashedParams()->hash().id()); | |
49 if (!digest) | |
50 return Status::ErrorUnsupported(); | |
51 | |
52 crypto::ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(pkey, NULL)); | |
53 | |
54 if (!init_func(ctx.get()) || | |
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) || | |
57 1 != EVP_PKEY_CTX_set_rsa_mgf1_md(ctx.get(), digest)) { | |
58 return Status::OperationError(); | |
59 } | |
60 | |
61 const blink::WebVector<uint8_t>& label = | |
62 algorithm.rsaOaepParams()->optionalLabel(); | |
63 | |
64 if (label.size()) { | |
65 // Make a copy of the label, since the ctx takes ownership of it when | |
66 // calling set0_rsa_oaep_label(). | |
67 crypto::ScopedOpenSSLBytes label_copy; | |
68 label_copy.reset(static_cast<uint8_t*>(OPENSSL_malloc(label.size()))); | |
69 memcpy(label_copy.get(), label.data(), label.size()); | |
70 | |
71 if (1 != EVP_PKEY_CTX_set0_rsa_oaep_label(ctx.get(), label_copy.release(), | |
72 label.size())) { | |
73 return Status::OperationError(); | |
74 } | |
75 } | |
76 | |
77 // Determine the maximum length of the output. | |
78 size_t outlen = 0; | |
79 if (!encrypt_decrypt_func(ctx.get(), NULL, &outlen, data.bytes(), | |
80 data.byte_length())) { | |
81 return Status::OperationError(); | |
82 } | |
83 buffer->resize(outlen); | |
84 | |
85 // Do the actual encryption/decryption. | |
86 if (!encrypt_decrypt_func(ctx.get(), vector_as_array(buffer), &outlen, | |
87 data.bytes(), data.byte_length())) { | |
88 return Status::OperationError(); | |
89 } | |
90 buffer->resize(outlen); | |
91 | |
92 return Status::Success(); | |
93 } | |
94 | |
95 class RsaOaepImplementation : public RsaHashedAlgorithm { | |
96 public: | |
97 RsaOaepImplementation() | |
98 : RsaHashedAlgorithm( | |
99 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageWrapKey, | |
100 blink::WebCryptoKeyUsageDecrypt | | |
101 blink::WebCryptoKeyUsageUnwrapKey) {} | |
102 | |
103 const char* GetJwkAlgorithm( | |
104 const blink::WebCryptoAlgorithmId hash) const override { | |
105 switch (hash) { | |
106 case blink::WebCryptoAlgorithmIdSha1: | |
107 return "RSA-OAEP"; | |
108 case blink::WebCryptoAlgorithmIdSha256: | |
109 return "RSA-OAEP-256"; | |
110 case blink::WebCryptoAlgorithmIdSha384: | |
111 return "RSA-OAEP-384"; | |
112 case blink::WebCryptoAlgorithmIdSha512: | |
113 return "RSA-OAEP-512"; | |
114 default: | |
115 return NULL; | |
116 } | |
117 } | |
118 | |
119 Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, | |
120 const blink::WebCryptoKey& key, | |
121 const CryptoData& data, | |
122 std::vector<uint8_t>* buffer) const override { | |
123 if (key.type() != blink::WebCryptoKeyTypePublic) | |
124 return Status::ErrorUnexpectedKeyType(); | |
125 | |
126 return CommonEncryptDecrypt(EVP_PKEY_encrypt_init, EVP_PKEY_encrypt, | |
127 algorithm, key, data, buffer); | |
128 } | |
129 | |
130 Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, | |
131 const blink::WebCryptoKey& key, | |
132 const CryptoData& data, | |
133 std::vector<uint8_t>* buffer) const override { | |
134 if (key.type() != blink::WebCryptoKeyTypePrivate) | |
135 return Status::ErrorUnexpectedKeyType(); | |
136 | |
137 return CommonEncryptDecrypt(EVP_PKEY_decrypt_init, EVP_PKEY_decrypt, | |
138 algorithm, key, data, buffer); | |
139 } | |
140 }; | |
141 | |
142 } // namespace | |
143 | |
144 AlgorithmImplementation* CreatePlatformRsaOaepImplementation() { | |
145 return new RsaOaepImplementation; | |
146 } | |
147 | |
148 } // namespace webcrypto | |
149 | |
150 } // namespace content | |
OLD | NEW |