OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/webcrypto/webcrypto_impl.h" | 5 #include "content/renderer/webcrypto/webcrypto_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 9 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 memcpy(new_buffer.data(), buffer->data(), new_size); | 33 memcpy(new_buffer.data(), buffer->data(), new_size); |
34 *buffer = new_buffer; | 34 *buffer = new_buffer; |
35 } | 35 } |
36 | 36 |
37 void WebCryptoImpl::encrypt( | 37 void WebCryptoImpl::encrypt( |
38 const WebKit::WebCryptoAlgorithm& algorithm, | 38 const WebKit::WebCryptoAlgorithm& algorithm, |
39 const WebKit::WebCryptoKey& key, | 39 const WebKit::WebCryptoKey& key, |
40 const unsigned char* data, | 40 const unsigned char* data, |
41 unsigned data_size, | 41 unsigned data_size, |
42 WebKit::WebCryptoResult result) { | 42 WebKit::WebCryptoResult result) { |
43 DCHECK(!algorithm.isNull()); | |
43 WebKit::WebArrayBuffer buffer; | 44 WebKit::WebArrayBuffer buffer; |
44 if (!EncryptInternal(algorithm, key, data, data_size, &buffer)) { | 45 if (!EncryptInternal(algorithm, key, data, data_size, &buffer)) { |
45 result.completeWithError(); | 46 result.completeWithError(); |
46 } else { | 47 } else { |
47 result.completeWithBuffer(buffer); | 48 result.completeWithBuffer(buffer); |
48 } | 49 } |
49 } | 50 } |
50 | 51 |
51 void WebCryptoImpl::decrypt( | 52 void WebCryptoImpl::decrypt( |
52 const WebKit::WebCryptoAlgorithm& algorithm, | 53 const WebKit::WebCryptoAlgorithm& algorithm, |
53 const WebKit::WebCryptoKey& key, | 54 const WebKit::WebCryptoKey& key, |
54 const unsigned char* data, | 55 const unsigned char* data, |
55 unsigned data_size, | 56 unsigned data_size, |
56 WebKit::WebCryptoResult result) { | 57 WebKit::WebCryptoResult result) { |
58 DCHECK(!algorithm.isNull()); | |
57 WebKit::WebArrayBuffer buffer; | 59 WebKit::WebArrayBuffer buffer; |
58 if (!DecryptInternal(algorithm, key, data, data_size, &buffer)) { | 60 if (!DecryptInternal(algorithm, key, data, data_size, &buffer)) { |
59 result.completeWithError(); | 61 result.completeWithError(); |
60 } else { | 62 } else { |
61 result.completeWithBuffer(buffer); | 63 result.completeWithBuffer(buffer); |
62 } | 64 } |
63 } | 65 } |
64 | 66 |
65 void WebCryptoImpl::digest( | 67 void WebCryptoImpl::digest( |
66 const WebKit::WebCryptoAlgorithm& algorithm, | 68 const WebKit::WebCryptoAlgorithm& algorithm, |
67 const unsigned char* data, | 69 const unsigned char* data, |
68 unsigned data_size, | 70 unsigned data_size, |
69 WebKit::WebCryptoResult result) { | 71 WebKit::WebCryptoResult result) { |
72 DCHECK(!algorithm.isNull()); | |
70 WebKit::WebArrayBuffer buffer; | 73 WebKit::WebArrayBuffer buffer; |
71 if (!DigestInternal(algorithm, data, data_size, &buffer)) { | 74 if (!DigestInternal(algorithm, data, data_size, &buffer)) { |
72 result.completeWithError(); | 75 result.completeWithError(); |
73 } else { | 76 } else { |
74 result.completeWithBuffer(buffer); | 77 result.completeWithBuffer(buffer); |
75 } | 78 } |
76 } | 79 } |
77 | 80 |
81 WebKit::WebCryptoKey NullKey() { | |
82 // TODO(eroman): Expose this in Blink instead. | |
83 return WebKit::WebCryptoKey::create(NULL, WebKit::WebCryptoKeyTypeSecret, | |
84 false, | |
85 WebKit::WebCryptoAlgorithm::createNull(), | |
86 0); | |
87 } | |
88 | |
78 void WebCryptoImpl::generateKey( | 89 void WebCryptoImpl::generateKey( |
79 const WebKit::WebCryptoAlgorithm& algorithm, | 90 const WebKit::WebCryptoAlgorithm& algorithm, |
80 bool exportable, | 91 bool extractable, |
81 WebKit::WebCryptoKeyUsageMask usage, | 92 WebKit::WebCryptoKeyUsageMask usage_mask, |
82 WebKit::WebCryptoResult result) { | 93 WebKit::WebCryptoResult result) { |
83 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; | 94 DCHECK(!algorithm.isNull()); |
84 WebKit::WebCryptoKeyType type; | 95 WebKit::WebCryptoKey key = NullKey(); |
85 if (!GenerateKeyInternal(algorithm, &handle, &type)) { | 96 if (GenerateKeyInternal(algorithm, extractable, usage_mask, &key)) { |
86 result.completeWithError(); | 97 DCHECK(key.handle()); |
87 } else { | 98 DCHECK_EQ(algorithm.id(), key.algorithm().id()); |
88 WebKit::WebCryptoKey key( | 99 DCHECK_EQ(extractable, key.extractable()); |
89 WebKit::WebCryptoKey::create(handle.release(), type, exportable, | 100 DCHECK_EQ(usage_mask, key.usages()); |
90 algorithm, usage)); | |
91 result.completeWithKey(key); | 101 result.completeWithKey(key); |
102 return; | |
92 } | 103 } |
104 result.completeWithError(); | |
Ryan Sleevi
2013/10/30 20:55:53
Should you follow the normal error form here?
if
eroman
2013/10/30 21:26:21
Done.
| |
93 } | 105 } |
94 | 106 |
95 void WebCryptoImpl::importKey( | 107 void WebCryptoImpl::importKey( |
96 WebKit::WebCryptoKeyFormat format, | 108 WebKit::WebCryptoKeyFormat format, |
97 const unsigned char* key_data, | 109 const unsigned char* key_data, |
98 unsigned key_data_size, | 110 unsigned key_data_size, |
99 const WebKit::WebCryptoAlgorithm& algorithm, | 111 const WebKit::WebCryptoAlgorithm& algorithm_or_null, |
100 bool extractable, | 112 bool extractable, |
101 WebKit::WebCryptoKeyUsageMask usage_mask, | 113 WebKit::WebCryptoKeyUsageMask usage_mask, |
102 WebKit::WebCryptoResult result) { | 114 WebKit::WebCryptoResult result) { |
103 WebKit::WebCryptoKeyType type; | 115 WebKit::WebCryptoKey key = NullKey(); |
104 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; | 116 if (ImportKeyInternal(format, |
105 | 117 key_data, |
106 if (!ImportKeyInternal(format, | 118 key_data_size, |
107 key_data, | 119 algorithm_or_null, |
108 key_data_size, | 120 extractable, |
109 algorithm, | 121 usage_mask, |
110 usage_mask, | 122 &key)) { |
111 &handle, | 123 DCHECK(key.handle()); |
112 &type)) { | 124 DCHECK(!key.algorithm().isNull()); |
113 result.completeWithError(); | 125 DCHECK_EQ(extractable, key.extractable()); |
126 result.completeWithKey(key); | |
114 return; | 127 return; |
115 } | 128 } |
116 | 129 result.completeWithError(); |
Ryan Sleevi
2013/10/30 20:55:53
ditto. Why change from the original form?
eroman
2013/10/30 21:26:21
Done.
| |
117 WebKit::WebCryptoKey key( | |
118 WebKit::WebCryptoKey::create( | |
119 handle.release(), type, extractable, algorithm, usage_mask)); | |
120 | |
121 result.completeWithKey(key); | |
122 } | 130 } |
123 | 131 |
124 void WebCryptoImpl::sign( | 132 void WebCryptoImpl::sign( |
125 const WebKit::WebCryptoAlgorithm& algorithm, | 133 const WebKit::WebCryptoAlgorithm& algorithm, |
126 const WebKit::WebCryptoKey& key, | 134 const WebKit::WebCryptoKey& key, |
127 const unsigned char* data, | 135 const unsigned char* data, |
128 unsigned data_size, | 136 unsigned data_size, |
129 WebKit::WebCryptoResult result) { | 137 WebKit::WebCryptoResult result) { |
138 DCHECK(!algorithm.isNull()); | |
130 WebKit::WebArrayBuffer buffer; | 139 WebKit::WebArrayBuffer buffer; |
131 if (!SignInternal(algorithm, key, data, data_size, &buffer)) { | 140 if (!SignInternal(algorithm, key, data, data_size, &buffer)) { |
132 result.completeWithError(); | 141 result.completeWithError(); |
133 } else { | 142 } else { |
134 result.completeWithBuffer(buffer); | 143 result.completeWithBuffer(buffer); |
135 } | 144 } |
136 } | 145 } |
137 | 146 |
138 void WebCryptoImpl::verifySignature( | 147 void WebCryptoImpl::verifySignature( |
139 const WebKit::WebCryptoAlgorithm& algorithm, | 148 const WebKit::WebCryptoAlgorithm& algorithm, |
140 const WebKit::WebCryptoKey& key, | 149 const WebKit::WebCryptoKey& key, |
141 const unsigned char* signature, | 150 const unsigned char* signature, |
142 unsigned signature_size, | 151 unsigned signature_size, |
143 const unsigned char* data, | 152 const unsigned char* data, |
144 unsigned data_size, | 153 unsigned data_size, |
145 WebKit::WebCryptoResult result) { | 154 WebKit::WebCryptoResult result) { |
155 DCHECK(!algorithm.isNull()); | |
146 bool signature_match = false; | 156 bool signature_match = false; |
147 if (!VerifySignatureInternal(algorithm, | 157 if (!VerifySignatureInternal(algorithm, |
148 key, | 158 key, |
149 signature, | 159 signature, |
150 signature_size, | 160 signature_size, |
151 data, | 161 data, |
152 data_size, | 162 data_size, |
153 &signature_match)) { | 163 &signature_match)) { |
154 result.completeWithError(); | 164 result.completeWithError(); |
155 } else { | 165 } else { |
156 result.completeWithBoolean(signature_match); | 166 result.completeWithBoolean(signature_match); |
157 } | 167 } |
158 } | 168 } |
159 | 169 |
160 } // namespace content | 170 } // namespace content |
OLD | NEW |