Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Side by Side Diff: content/child/webcrypto/nss/aes_kw_nss.cc

Issue 379383002: Refactor WebCrypto code (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase onto master Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 <secerr.h>
6
7 #include "content/child/webcrypto/crypto_data.h"
8 #include "content/child/webcrypto/nss/aes_key_nss.h"
9 #include "content/child/webcrypto/nss/key_nss.h"
10 #include "content/child/webcrypto/nss/sym_key_nss.h"
11 #include "content/child/webcrypto/nss/util_nss.h"
12 #include "content/child/webcrypto/status.h"
13 #include "content/child/webcrypto/webcrypto_util.h"
14 #include "crypto/scoped_nss_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 // The Default IV for AES-KW. See http://www.ietf.org/rfc/rfc3394.txt
25 // Section 2.2.3.1.
26 const unsigned char kAesIv[] = {0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6};
27
28 Status DoUnwrapSymKeyAesKw(const CryptoData& wrapped_key_data,
Ryan Sleevi 2014/07/17 00:06:54 Document why this exists as working on a SymKey (1
eroman 2014/07/17 20:37:25 Done. Documented as: // The result of unwrapping
29 PK11SymKey* wrapping_key,
30 CK_MECHANISM_TYPE mechanism,
31 CK_FLAGS flags,
32 crypto::ScopedPK11SymKey* unwrapped_key) {
33 DCHECK_GE(wrapped_key_data.byte_length(), 24u);
34 DCHECK_EQ(wrapped_key_data.byte_length() % 8, 0u);
35
36 SECItem iv_item = MakeSECItemForBuffer(CryptoData(kAesIv, sizeof(kAesIv)));
37 crypto::ScopedSECItem param_item(
38 PK11_ParamFromIV(CKM_NSS_AES_KEY_WRAP, &iv_item));
39 if (!param_item)
40 return Status::ErrorUnexpected();
41
42 SECItem cipher_text = MakeSECItemForBuffer(wrapped_key_data);
43
44 // The plaintext length is always 64 bits less than the data size.
45 const unsigned int plaintext_length = wrapped_key_data.byte_length() - 8;
46
47 #if defined(USE_NSS)
48 // Part of workaround for
49 // https://bugzilla.mozilla.org/show_bug.cgi?id=981170. See the explanation
50 // later in this function.
51 PORT_SetError(0);
52 #endif
53
54 crypto::ScopedPK11SymKey new_key(
55 PK11_UnwrapSymKeyWithFlags(wrapping_key,
56 CKM_NSS_AES_KEY_WRAP,
57 param_item.get(),
58 &cipher_text,
59 mechanism,
60 CKA_FLAGS_ONLY,
61 plaintext_length,
62 flags));
63
64 // TODO(padolph): Use NSS PORT_GetError() and friends to report a more
65 // accurate error, providing if doesn't leak any information to web pages
66 // about other web crypto users, key details, etc.
67 if (!new_key)
68 return Status::OperationError();
69
70 #if defined(USE_NSS)
71 // Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=981170
72 // which was fixed in NSS 3.16.0.
73 // If unwrap fails, NSS nevertheless returns a valid-looking PK11SymKey,
74 // with a reasonable length but with key data pointing to uninitialized
75 // memory.
76 // To understand this workaround see the fix for 981170:
77 // https://hg.mozilla.org/projects/nss/rev/753bb69e543c
78 if (!NSS_VersionCheck("3.16") && PORT_GetError() == SEC_ERROR_BAD_DATA)
79 return Status::OperationError();
80 #endif
81
82 *unwrapped_key = new_key.Pass();
83 return Status::Success();
84 }
85
86 Status WrapSymKeyAesKw(PK11SymKey* key,
87 PK11SymKey* wrapping_key,
88 std::vector<uint8>* buffer) {
89 // The data size must be at least 16 bytes and a multiple of 8 bytes.
90 // RFC 3394 does not specify a maximum allowed data length, but since only
91 // keys are being wrapped in this application (which are small), a reasonable
92 // max limit is whatever will fit into an unsigned. For the max size test,
93 // note that AES Key Wrap always adds 8 bytes to the input data size.
94 const unsigned int input_length = PK11_GetKeyLength(key);
95 DCHECK_GE(input_length, 16u);
96 DCHECK((input_length % 8) == 0);
97 if (input_length > UINT_MAX - 8)
98 return Status::ErrorDataTooLarge();
99
100 SECItem iv_item = MakeSECItemForBuffer(CryptoData(kAesIv, sizeof(kAesIv)));
101 crypto::ScopedSECItem param_item(
102 PK11_ParamFromIV(CKM_NSS_AES_KEY_WRAP, &iv_item));
103 if (!param_item)
104 return Status::ErrorUnexpected();
105
106 const unsigned int output_length = input_length + 8;
107 buffer->resize(output_length);
108 SECItem wrapped_key_item = MakeSECItemForBuffer(CryptoData(*buffer));
109
110 if (SECSuccess != PK11_WrapSymKey(CKM_NSS_AES_KEY_WRAP,
111 param_item.get(),
112 wrapping_key,
113 key,
114 &wrapped_key_item)) {
115 return Status::OperationError();
116 }
117 if (output_length != wrapped_key_item.len)
118 return Status::ErrorUnexpected();
119
120 return Status::Success();
121 }
122
123 class AesKwCryptoAlgorithmNss : public AesAlgorithm {
124 public:
125 AesKwCryptoAlgorithmNss()
126 : AesAlgorithm(
127 CKM_NSS_AES_KEY_WRAP,
128 CKF_WRAP | CKF_WRAP,
129 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey,
130 "KW") {}
131
132 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
133 const blink::WebCryptoKey& wrapping_key,
134 const CryptoData& data,
135 std::vector<uint8>* buffer) const OVERRIDE {
136 if (data.byte_length() < 16)
137 return Status::ErrorDataTooSmall();
138 if (data.byte_length() % 8)
139 return Status::ErrorInvalidAesKwDataLength();
140
141 // Due to limitations in the NSS API for the AES-KW algorithm, |data| must
142 // be temporarily viewed as a symmetric key to be wrapped (encrypted).
143 SECItem data_item = MakeSECItemForBuffer(data);
144 crypto::ScopedPK11Slot slot(PK11_GetInternalSlot());
145 crypto::ScopedPK11SymKey data_as_sym_key(
146 PK11_ImportSymKey(slot.get(),
147 CKK_GENERIC_SECRET,
148 PK11_OriginUnwrap,
149 CKA_SIGN,
150 &data_item,
151 NULL));
152 if (!data_as_sym_key)
153 return Status::OperationError();
154
155 return WrapSymKeyAesKw(
156 data_as_sym_key.get(), SymKeyNss::Cast(wrapping_key)->key(), buffer);
157 }
158
159 virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
160 const blink::WebCryptoKey& wrapping_key,
161 const CryptoData& data,
162 std::vector<uint8>* buffer) const OVERRIDE {
163 if (data.byte_length() < 24)
164 return Status::ErrorDataTooSmall();
165 if (data.byte_length() % 8)
166 return Status::ErrorInvalidAesKwDataLength();
167
168 // Due to limitations in the NSS API for the AES-KW algorithm, |data| must
169 // be
170 // temporarily viewed as a symmetric key to be unwrapped (decrypted).
171 crypto::ScopedPK11SymKey decrypted;
172 Status status = DoUnwrapSymKeyAesKw(data,
173 SymKeyNss::Cast(wrapping_key)->key(),
174 CKK_GENERIC_SECRET,
175 0,
176 &decrypted);
177 if (status.IsError())
178 return status;
179
180 // Once the decrypt is complete, extract the resultant raw bytes from NSS
181 // and return them to the caller.
182 if (PK11_ExtractKeyValue(decrypted.get()) != SECSuccess)
183 return Status::OperationError();
184 const SECItem* const key_data = PK11_GetKeyData(decrypted.get());
185 if (!key_data)
186 return Status::OperationError();
187 buffer->assign(key_data->data, key_data->data + key_data->len);
188
189 return Status::Success();
190 }
191 };
192
193 } // namespace
194
195 AlgorithmImplementation* CreatePlatformAesKwImplementation() {
196 return new AesKwCryptoAlgorithmNss;
197 }
198
199 } // namespace webcrypto
200
201 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698