Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / child / webcrypto / openssl / aes_key_openssl.cc
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/openssl/aes_key_openssl.h"
6
7 #include "base/logging.h"
8 #include "content/child/webcrypto/crypto_data.h"
9 #include "content/child/webcrypto/jwk.h"
10 #include "content/child/webcrypto/openssl/key_openssl.h"
11 #include "content/child/webcrypto/openssl/sym_key_openssl.h"
12 #include "content/child/webcrypto/status.h"
13 #include "content/child/webcrypto/webcrypto_util.h"
14 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
15
16 namespace content {
17
18 namespace webcrypto {
19
20 AesAlgorithm::AesAlgorithm(blink::WebCryptoKeyUsageMask all_key_usages,
21                            const std::string& jwk_suffix)
22     : all_key_usages_(all_key_usages), jwk_suffix_(jwk_suffix) {
23 }
24
25 AesAlgorithm::AesAlgorithm(const std::string& jwk_suffix)
26     : all_key_usages_(blink::WebCryptoKeyUsageEncrypt |
27                       blink::WebCryptoKeyUsageDecrypt |
28                       blink::WebCryptoKeyUsageWrapKey |
29                       blink::WebCryptoKeyUsageUnwrapKey),
30       jwk_suffix_(jwk_suffix) {
31 }
32
33 Status AesAlgorithm::GenerateKey(const blink::WebCryptoAlgorithm& algorithm,
34                                  bool extractable,
35                                  blink::WebCryptoKeyUsageMask usages,
36                                  GenerateKeyResult* result) const {
37   Status status = CheckKeyCreationUsages(all_key_usages_, usages);
38   if (status.IsError())
39     return status;
40
41   unsigned int keylen_bits;
42   status = GetAesKeyGenLengthInBits(algorithm.aesKeyGenParams(), &keylen_bits);
43   if (status.IsError())
44     return status;
45
46   return GenerateSecretKeyOpenSsl(
47       blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
48       extractable,
49       usages,
50       keylen_bits / 8,
51       result);
52 }
53
54 Status AesAlgorithm::VerifyKeyUsagesBeforeImportKey(
55     blink::WebCryptoKeyFormat format,
56     blink::WebCryptoKeyUsageMask usages) const {
57   switch (format) {
58     case blink::WebCryptoKeyFormatRaw:
59     case blink::WebCryptoKeyFormatJwk:
60       return CheckKeyCreationUsages(all_key_usages_, usages);
61     default:
62       return Status::ErrorUnsupportedImportKeyFormat();
63   }
64 }
65
66 Status AesAlgorithm::ImportKeyRaw(const CryptoData& key_data,
67                                   const blink::WebCryptoAlgorithm& algorithm,
68                                   bool extractable,
69                                   blink::WebCryptoKeyUsageMask usages,
70                                   blink::WebCryptoKey* key) const {
71   const unsigned int keylen_bytes = key_data.byte_length();
72   Status status = VerifyAesKeyLengthForImport(keylen_bytes);
73   if (status.IsError())
74     return status;
75
76   // No possibility of overflow.
77   unsigned int keylen_bits = keylen_bytes * 8;
78
79   return ImportKeyRawOpenSsl(
80       key_data,
81       blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits),
82       extractable,
83       usages,
84       key);
85 }
86
87 Status AesAlgorithm::ImportKeyJwk(const CryptoData& key_data,
88                                   const blink::WebCryptoAlgorithm& algorithm,
89                                   bool extractable,
90                                   blink::WebCryptoKeyUsageMask usages,
91                                   blink::WebCryptoKey* key) const {
92   std::vector<uint8_t> raw_data;
93   Status status = ReadAesSecretKeyJwk(
94       key_data, jwk_suffix_, extractable, usages, &raw_data);
95   if (status.IsError())
96     return status;
97
98   return ImportKeyRaw(
99       CryptoData(raw_data), algorithm, extractable, usages, key);
100 }
101
102 Status AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey& key,
103                                   std::vector<uint8_t>* buffer) const {
104   *buffer = SymKeyOpenSsl::Cast(key)->raw_key_data();
105   return Status::Success();
106 }
107
108 Status AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey& key,
109                                   std::vector<uint8_t>* buffer) const {
110   const std::vector<uint8_t>& raw_data =
111       SymKeyOpenSsl::Cast(key)->raw_key_data();
112
113   WriteSecretKeyJwk(CryptoData(raw_data),
114                     MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()),
115                     key.extractable(),
116                     key.usages(),
117                     buffer);
118
119   return Status::Success();
120 }
121
122 }  // namespace webcrypto
123
124 }  // namespace content