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.
5 #include <openssl/evp.h>
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"
24 typedef int (*InitFunc
)(EVP_PKEY_CTX
* ctx
);
25 typedef int (*EncryptDecryptFunc
)(EVP_PKEY_CTX
* ctx
,
28 const unsigned char* in
,
31 // Helper for doing either RSA-OAEP encryption or decryption.
33 // To encrypt call with:
34 // init_func=EVP_PKEY_encrypt_init, encrypt_decrypt_func=EVP_PKEY_encrypt
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
);
46 EVP_PKEY
* pkey
= AsymKeyOpenSsl::Cast(key
)->key();
47 const EVP_MD
* digest
=
48 GetDigest(key
.algorithm().rsaHashedParams()->hash().id());
50 return Status::ErrorUnsupported();
52 crypto::ScopedEVP_PKEY_CTX
ctx(EVP_PKEY_CTX_new(pkey
, NULL
));
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();
61 const blink::WebVector
<uint8_t>& label
=
62 algorithm
.rsaOaepParams()->optionalLabel();
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());
71 if (1 != EVP_PKEY_CTX_set0_rsa_oaep_label(ctx
.get(), label_copy
.release(),
73 return Status::OperationError();
77 // Determine the maximum length of the output.
79 if (!encrypt_decrypt_func(ctx
.get(), NULL
, &outlen
, data
.bytes(),
80 data
.byte_length())) {
81 return Status::OperationError();
83 buffer
->resize(outlen
);
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();
90 buffer
->resize(outlen
);
92 return Status::Success();
95 class RsaOaepImplementation
: public RsaHashedAlgorithm
{
97 RsaOaepImplementation()
99 blink::WebCryptoKeyUsageEncrypt
| blink::WebCryptoKeyUsageWrapKey
,
100 blink::WebCryptoKeyUsageDecrypt
|
101 blink::WebCryptoKeyUsageUnwrapKey
) {}
103 const char* GetJwkAlgorithm(
104 const blink::WebCryptoAlgorithmId hash
) const override
{
106 case blink::WebCryptoAlgorithmIdSha1
:
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";
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();
126 return CommonEncryptDecrypt(EVP_PKEY_encrypt_init
, EVP_PKEY_encrypt
,
127 algorithm
, key
, data
, buffer
);
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();
137 return CommonEncryptDecrypt(EVP_PKEY_decrypt_init
, EVP_PKEY_decrypt
,
138 algorithm
, key
, data
, buffer
);
144 AlgorithmImplementation
* CreatePlatformRsaOaepImplementation() {
145 return new RsaOaepImplementation
;
148 } // namespace webcrypto
150 } // namespace content