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

Side by Side Diff: content/child/webcrypto/structured_clone.cc

Issue 379383002: Refactor WebCrypto code (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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 "content/child/webcrypto/structured_clone.h"
6
7 #include "base/logging.h"
8 #include "content/child/webcrypto/algorithm_dispatch.h"
9 #include "content/child/webcrypto/status.h"
10 #include "content/child/webcrypto/webcrypto_util.h"
11 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
12 #if defined(USE_OPENSSL)
13 // TODO(eroman):
14 #else
15 #include "content/child/webcrypto/nss/key_nss.h"
16 #endif
17
18 namespace content {
19
20 namespace webcrypto {
21
22 namespace {
23
24 // Returns the key format to use for structured cloning.
25 blink::WebCryptoKeyFormat GetCloneFormatForKeyType(
26 blink::WebCryptoKeyType type) {
27 switch (type) {
28 case blink::WebCryptoKeyTypeSecret:
29 return blink::WebCryptoKeyFormatRaw;
30 case blink::WebCryptoKeyTypePublic:
31 return blink::WebCryptoKeyFormatSpki;
32 case blink::WebCryptoKeyTypePrivate:
33 return blink::WebCryptoKeyFormatPkcs8;
34 }
35
36 NOTREACHED();
37 return blink::WebCryptoKeyFormatRaw;
38 }
39
40 // Converts a KeyAlgorithm into an equivalent Algorithm for import.
41 blink::WebCryptoAlgorithm KeyAlgorithmToImportAlgorithm(
42 const blink::WebCryptoKeyAlgorithm& algorithm) {
43 switch (algorithm.paramsType()) {
44 case blink::WebCryptoKeyAlgorithmParamsTypeAes:
45 return CreateAlgorithm(algorithm.id());
46 case blink::WebCryptoKeyAlgorithmParamsTypeHmac:
47 return CreateHmacImportAlgorithm(algorithm.hmacParams()->hash().id());
48 case blink::WebCryptoKeyAlgorithmParamsTypeRsaHashed:
49 return CreateRsaHashedImportAlgorithm(
50 algorithm.id(), algorithm.rsaHashedParams()->hash().id());
51 case blink::WebCryptoKeyAlgorithmParamsTypeNone:
52 break;
53 default:
54 break;
55 }
56 return blink::WebCryptoAlgorithm::createNull();
57 }
58
59 // There is some duplicated information in the serialized format used by
60 // structured clone (since the KeyAlgorithm is serialized separately from the
61 // key data). Use this extra information to further validate what was
62 // deserialized from the key data.
63 //
64 // A failure here implies either a bug in the code, or that the serialized data
65 // was corrupted.
66 bool ValidateDeserializedKey(const blink::WebCryptoKey& key,
67 const blink::WebCryptoKeyAlgorithm& algorithm,
68 blink::WebCryptoKeyType type) {
69 if (algorithm.id() != key.algorithm().id())
70 return false;
71
72 if (key.type() != type)
73 return false;
74
75 switch (algorithm.paramsType()) {
76 case blink::WebCryptoKeyAlgorithmParamsTypeAes:
77 if (algorithm.aesParams()->lengthBits() !=
78 key.algorithm().aesParams()->lengthBits())
79 return false;
80 break;
81 case blink::WebCryptoKeyAlgorithmParamsTypeRsaHashed:
82 if (algorithm.rsaHashedParams()->modulusLengthBits() !=
83 key.algorithm().rsaHashedParams()->modulusLengthBits())
84 return false;
85 if (algorithm.rsaHashedParams()->publicExponent().size() !=
86 key.algorithm().rsaHashedParams()->publicExponent().size())
87 return false;
88 if (memcmp(algorithm.rsaHashedParams()->publicExponent().data(),
89 key.algorithm().rsaHashedParams()->publicExponent().data(),
90 key.algorithm().rsaHashedParams()->publicExponent().size()) !=
91 0)
92 return false;
93 break;
94 case blink::WebCryptoKeyAlgorithmParamsTypeNone:
95 case blink::WebCryptoKeyAlgorithmParamsTypeHmac:
96 break;
97 default:
98 return false;
99 }
100
101 return true;
102 }
103
104 } // namespace
105
106 // Note that this function is called from the target Blink thread.
107 bool SerializeKeyForClone(const blink::WebCryptoKey& key,
108 blink::WebVector<uint8>* key_data) {
109 #if defined(USE_OPENSSL)
110 // TODO(eroman):
111 return false;
112 #else
113 const KeyNss* nss_key = static_cast<KeyNss*>(key.handle());
114 *key_data = nss_key->serialized_key_data();
115 return true;
116 #endif
117 }
118
119 // Note that this function is called from the target Blink thread.
120 bool DeserializeKeyForClone(const blink::WebCryptoKeyAlgorithm& algorithm,
121 blink::WebCryptoKeyType type,
122 bool extractable,
123 blink::WebCryptoKeyUsageMask usage_mask,
124 const CryptoData& key_data,
125 blink::WebCryptoKey* key) {
126 // TODO(eroman): This should not call into the platform crypto layer.
127 // Otherwise it runs the risk of stalling while the NSS/OpenSSL global locks
128 // are held.
129 //
130 // An alternate approach is to defer the key import until the key is used.
131 // However this means that any deserialization errors would have to be
132 // surfaced as WebCrypto errors, leading to slightly different behaviors. For
133 // instance you could clone a key which fails to be deserialized.
134 Status status = ImportKey(GetCloneFormatForKeyType(type),
135 key_data,
136 KeyAlgorithmToImportAlgorithm(algorithm),
137 extractable,
138 usage_mask,
139 key);
140 if (status.IsError())
141 return false;
142 return ValidateDeserializedKey(*key, algorithm, type);
143 }
144
145 } // namespace webcrypto
146
147 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698