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

Side by Side Diff: content/renderer/webcrypto/webcrypto_impl.cc

Issue 34583010: [webcrypto] Add RSA key generation using NSS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: re-upload after 500 server failure Created 7 years, 1 month 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
OLDNEW
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"
11 #include "third_party/WebKit/public/platform/WebCryptoKey.h" 11 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
12 12
13 namespace content { 13 namespace content {
14 14
15 namespace {
16
17 bool IsAlgorithmAsymmetric(const WebKit::WebCryptoAlgorithm& algorithm) {
18 const WebKit::WebCryptoAlgorithmId algorithm_id = algorithm.id();
19 // TODO: include all other asymmetric algorithms once they are defined,
eroman 2013/10/23 20:02:46 nit: use the format TODO(padolph): This doesn't m
padolph 2013/10/23 23:21:47 Done.
20 // e.g. EC and DH.
21 return (algorithm_id == WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 ||
22 algorithm_id == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
23 algorithm_id == WebKit::WebCryptoAlgorithmIdRsaOaep);
24 }
25
26 } // namespace
27
15 WebCryptoImpl::WebCryptoImpl() { 28 WebCryptoImpl::WebCryptoImpl() {
16 Init(); 29 Init();
17 } 30 }
18 31
19 // static 32 // static
20 // TODO(eroman): This works by re-allocating a new buffer. It would be better if 33 // TODO(eroman): This works by re-allocating a new buffer. It would be better if
21 // the WebArrayBuffer could just be truncated instead. 34 // the WebArrayBuffer could just be truncated instead.
22 void WebCryptoImpl::ShrinkBuffer( 35 void WebCryptoImpl::ShrinkBuffer(
23 WebKit::WebArrayBuffer* buffer, 36 WebKit::WebArrayBuffer* buffer,
24 unsigned new_size) { 37 unsigned new_size) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 } else { 86 } else {
74 result.completeWithBuffer(buffer); 87 result.completeWithBuffer(buffer);
75 } 88 }
76 } 89 }
77 90
78 void WebCryptoImpl::generateKey( 91 void WebCryptoImpl::generateKey(
79 const WebKit::WebCryptoAlgorithm& algorithm, 92 const WebKit::WebCryptoAlgorithm& algorithm,
80 bool exportable, 93 bool exportable,
81 WebKit::WebCryptoKeyUsageMask usage, 94 WebKit::WebCryptoKeyUsageMask usage,
82 WebKit::WebCryptoResult result) { 95 WebKit::WebCryptoResult result) {
83 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; 96 if (IsAlgorithmAsymmetric(algorithm)) {
84 WebKit::WebCryptoKeyType type; 97 scoped_ptr<WebKit::WebCryptoKeyHandle> public_key_handle;
85 if (!GenerateKeyInternal(algorithm, &handle, &type)) { 98 scoped_ptr<WebKit::WebCryptoKeyHandle> private_key_handle;
86 result.completeWithError(); 99 if (!GenerateKeyPairInternal(algorithm,
87 } else { 100 &public_key_handle,
88 WebKit::WebCryptoKey key( 101 &private_key_handle)) {
89 WebKit::WebCryptoKey::create(handle.release(), type, exportable, 102 result.completeWithError();
eroman 2013/10/23 20:02:46 I think the structure of this function would be mo
padolph 2013/10/23 23:21:47 Agreed for the error cases. But do you also want t
90 algorithm, usage)); 103 } else {
91 result.completeWithKey(key); 104 WebKit::WebCryptoKey public_key(
105 WebKit::WebCryptoKey::create(public_key_handle.release(),
106 WebKit::WebCryptoKeyTypePublic,
107 exportable,
108 algorithm,
109 usage));
110 WebKit::WebCryptoKey private_key(
111 WebKit::WebCryptoKey::create(private_key_handle.release(),
112 WebKit::WebCryptoKeyTypePrivate,
113 exportable,
114 algorithm,
115 usage));
116 result.completeWithKeyPair(public_key, private_key);
117 }
118 }
119 else {
120 scoped_ptr<WebKit::WebCryptoKeyHandle> handle;
121 WebKit::WebCryptoKeyType type;
122 if (!GenerateKeyInternal(algorithm, &handle, &type)) {
123 result.completeWithError();
124 } else {
125 WebKit::WebCryptoKey key(
126 WebKit::WebCryptoKey::create(handle.release(), type, exportable,
127 algorithm, usage));
128 result.completeWithKey(key);
129 }
92 } 130 }
93 } 131 }
94 132
95 void WebCryptoImpl::importKey( 133 void WebCryptoImpl::importKey(
96 WebKit::WebCryptoKeyFormat format, 134 WebKit::WebCryptoKeyFormat format,
97 const unsigned char* key_data, 135 const unsigned char* key_data,
98 unsigned key_data_size, 136 unsigned key_data_size,
99 const WebKit::WebCryptoAlgorithm& algorithm, 137 const WebKit::WebCryptoAlgorithm& algorithm,
100 bool extractable, 138 bool extractable,
101 WebKit::WebCryptoKeyUsageMask usage_mask, 139 WebKit::WebCryptoKeyUsageMask usage_mask,
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 data, 189 data,
152 data_size, 190 data_size,
153 &signature_match)) { 191 &signature_match)) {
154 result.completeWithError(); 192 result.completeWithError();
155 } else { 193 } else {
156 result.completeWithBoolean(signature_match); 194 result.completeWithBoolean(signature_match);
157 } 195 }
158 } 196 }
159 197
160 } // namespace content 198 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698