OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/webcrypto_sha_digest.h" | |
6 | |
7 #include <pk11pub.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "crypto/nss_util.h" | |
11 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | |
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | |
13 | |
14 namespace content { | |
15 | |
16 WebCryptoSHADigest::WebCryptoSHADigest( | |
17 const WebKit::WebCryptoAlgorithmId algorithm_id) | |
18 : context_(NULL), | |
19 hash_algorithm_(SEC_OID_UNKNOWN) { | |
20 crypto::EnsureNSSInit(); | |
eroman
2013/07/19 01:11:13
Seems this could go in InitializeContext() instead
Bryan Eyler
2013/07/24 00:33:53
Done.
| |
21 | |
22 switch (algorithm_id) { | |
23 case WebKit::WebCryptoAlgorithmIdSha1: | |
24 hash_algorithm_ = SEC_OID_SHA1; | |
25 break; | |
26 case WebKit::WebCryptoAlgorithmIdSha224: | |
27 hash_algorithm_ = SEC_OID_SHA224; | |
28 break; | |
29 case WebKit::WebCryptoAlgorithmIdSha256: | |
30 hash_algorithm_ = SEC_OID_SHA256; | |
31 break; | |
32 case WebKit::WebCryptoAlgorithmIdSha384: | |
33 hash_algorithm_ = SEC_OID_SHA384; | |
34 break; | |
35 case WebKit::WebCryptoAlgorithmIdSha512: | |
36 hash_algorithm_ = SEC_OID_SHA512; | |
37 break; | |
38 default: | |
39 hash_algorithm_ = SEC_OID_UNKNOWN; | |
40 } | |
41 } | |
42 | |
43 WebCryptoSHADigest::~WebCryptoSHADigest() { | |
44 if (context_) { | |
45 PK11_DestroyContext(context_, PR_TRUE); | |
46 } | |
47 } | |
48 | |
49 bool WebCryptoSHADigest::InitializeContext() { | |
50 context_ = PK11_CreateDigestContext(hash_algorithm_); | |
51 if (!context_) { | |
52 LOG(ERROR) << "Could not create digest context for hash algorithm: " | |
53 << hash_algorithm_; | |
54 // TODO(bryaneyler): Error out. | |
55 return false; | |
56 } | |
57 | |
58 if (PK11_DigestBegin(context_) != SECSuccess) { | |
59 LOG(ERROR) << "Could not initialize digest context."; | |
60 // TODO(bryaneyler): Error out. | |
61 return false; | |
62 } | |
63 | |
64 return true; | |
65 } | |
66 | |
67 void WebCryptoSHADigest::process(const unsigned char* bytes, size_t size) { | |
68 // If this is the first process request, need to setup the context. | |
69 if (!context_ && !InitializeContext()) { | |
eroman
2013/07/19 01:11:13
[after my API change]
I suggest doing the Initial
Bryan Eyler
2013/07/24 00:33:53
Done.
| |
70 LOG(ERROR) << "Could not initialize context."; | |
71 // TODO(bryaneyler): Error out. | |
72 return; | |
73 } | |
74 | |
75 if (PK11_DigestOp(context_, bytes, size) != SECSuccess) { | |
76 LOG(ERROR) << "Could not process digest contents of size: " << size; | |
77 // TODO(bryaneyler): Error out. | |
78 } | |
79 } | |
80 | |
81 void WebCryptoSHADigest::abort() { | |
82 delete this; | |
83 } | |
84 | |
85 void WebCryptoSHADigest::finish(WebKit::WebCryptoOperationResult* result) { | |
86 // If no context yet created; create an empty one. | |
87 if (!context_ && !InitializeContext()) { | |
88 LOG(ERROR) << "Could not initialize context."; | |
89 // TODO(bryaneyler): Error out. | |
90 return; | |
91 } | |
92 | |
93 unsigned char* digest = NULL; | |
94 unsigned int length = 0; | |
95 | |
96 switch (hash_algorithm_) { | |
97 case SEC_OID_SHA1: | |
98 length = 20; | |
99 break; | |
100 case SEC_OID_SHA224: | |
101 length = 28; | |
102 break; | |
103 case SEC_OID_SHA256: | |
104 length = 32; | |
105 break; | |
106 case SEC_OID_SHA384: | |
107 length = 48; | |
108 break; | |
109 case SEC_OID_SHA512: | |
110 length = 64; | |
111 break; | |
112 default: | |
113 length = 0; | |
eroman
2013/07/19 01:11:13
[optional] Rather than running the risk of the two
Bryan Eyler
2013/07/24 00:33:53
Done.
| |
114 } | |
115 | |
116 WebKit::WebArrayBuffer buffer( | |
117 WebKit::WebArrayBuffer::create(length, 1)); | |
118 | |
119 digest = reinterpret_cast<unsigned char*>(buffer.data()); | |
120 if (!digest) { | |
121 LOG(ERROR) << "Could not allocate digest data."; | |
122 // TODO(bryaneyler): Error out. | |
123 return; | |
124 } | |
125 | |
126 unsigned int result_length = 0; | |
127 if (PK11_DigestFinal(context_, digest, &result_length, length) | |
128 != SECSuccess || result_length != length) { | |
129 LOG(ERROR) << "Could not finalize digest data."; | |
130 // TODO(bryaneyler): Error out. | |
131 return; | |
132 } | |
133 | |
134 result->setArrayBuffer(buffer); | |
135 | |
136 delete this; | |
137 } | |
138 | |
139 } // namespace content | |
OLD | NEW |