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

Side by Side Diff: Source/testing/runner/MockWebCrypto.cpp

Issue 23164012: WebCrypto: Remove support for multi-part operations. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Sync to tot Created 7 years, 4 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
« no previous file with comments | « Source/testing/runner/MockWebCrypto.h ('k') | public/platform/WebCrypto.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 12 matching lines...) Expand all
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "MockWebCrypto.h" 31 #include "MockWebCrypto.h"
32 32
33 #include "public/platform/WebArrayBuffer.h"
34 #include "public/platform/WebCryptoAlgorithm.h" 33 #include "public/platform/WebCryptoAlgorithm.h"
35 #include <string> 34 #include <string>
36 #include <string.h>
37 35
38 using namespace WebKit; 36 using namespace WebKit;
39 37
40 namespace WebTestRunner { 38 namespace WebTestRunner {
41 39
42 namespace { 40 namespace {
43 41
44 enum Operation { 42 std::string mockSign(const unsigned char* bytes, size_t size)
45 Encrypt, 43 {
46 Decrypt, 44 return "signed HMAC:" + std::string(reinterpret_cast<const char*>(bytes), si ze);
47 Sign, 45 }
48 Verify,
49 Digest,
50 };
51
52 class MockCryptoOperation : public WebKit::WebCryptoOperation {
53 public:
54 MockCryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, Operation o p, const WebKit::WebCryptoOperationResult& result)
55 : m_algorithm(algorithm)
56 , m_operation(op)
57 , m_result(result) { }
58
59 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE
60 {
61 // Don't buffer too much data.
62 if (m_data.size() + size > 6) {
63 m_result.completeWithError();
64 delete this;
65 return;
66 }
67
68 if (size)
69 m_data.append(reinterpret_cast<const char*>(bytes), size);
70 }
71
72 virtual void abort() OVERRIDE
73 {
74 delete this;
75 }
76
77 virtual void finish() OVERRIDE
78 {
79 if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdSha1 && m_operation == Digest) {
80 if (m_data.empty()) {
81 const unsigned char result[] = {0xda, 0x39, 0xa3, 0xee, 0x5e, 0x 6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07 , 0x09};
82 completeWithArrayBuffer(result, sizeof(result));
83 } else if (m_data == std::string("\x00", 1)) {
84 const unsigned char result[] = {0x5b, 0xa9, 0x3c, 0x9d, 0xb0, 0x cf, 0xf9, 0x3f, 0x52, 0xb5, 0x21, 0xd7, 0x42, 0x0e, 0x43, 0xf6, 0xed, 0xa2, 0x78 , 0x4f};
85 completeWithArrayBuffer(result, sizeof(result));
86 } else if (m_data == std::string("\x00\x01\x02\x03\x04\x05", 6)) {
87 const unsigned char result[] = {0x86, 0x84, 0x60, 0xd9, 0x8d, 0x 09, 0xd8, 0xbb, 0xb9, 0x3d, 0x7b, 0x6c, 0xdd, 0x15, 0xcc, 0x7f, 0xbe, 0xc6, 0x76 , 0xb9};
88 completeWithArrayBuffer(result, sizeof(result));
89 } else {
90 m_result.completeWithError();
91 }
92 } else if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdHmac && m_ope ration == Sign) {
93 std::string result = "signed HMAC:" + m_data;
94 completeWithArrayBuffer(result.data(), result.size());
95 } else if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdHmac && m_ope ration == Verify) {
96 std::string expectedSignature = "signed HMAC:" + m_data;
97 m_result.completeWithBoolean(expectedSignature == m_signature);
98 } else {
99 m_result.completeWithError();
100 }
101 delete this;
102 }
103
104 void setSignature(const unsigned char* signatureBytes, size_t signatureLengt h)
105 {
106 m_signature.assign(reinterpret_cast<const char*>(signatureBytes), signat ureLength);
107 }
108
109 private:
110 void completeWithArrayBuffer(const void* data, size_t bytes)
111 {
112 WebKit::WebArrayBuffer buffer = WebKit::WebArrayBuffer::create(bytes, 1) ;
113 memcpy(buffer.data(), data, bytes);
114 m_result.completeWithArrayBuffer(buffer);
115 }
116
117 WebKit::WebCryptoAlgorithm m_algorithm;
118 Operation m_operation;
119 WebKit::WebCryptoOperationResult m_result;
120 std::string m_data;
121 std::string m_signature;
122 };
123 46
124 } // namespace 47 } // namespace
125 48
126 MockWebCrypto* MockWebCrypto::get() 49 MockWebCrypto* MockWebCrypto::get()
127 { 50 {
128 static MockWebCrypto crypto; 51 static MockWebCrypto crypto;
129 return &crypto; 52 return &crypto;
130 } 53 }
131 54
132 void MockWebCrypto::encrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W ebKit::WebCryptoKey& key, WebKit::WebCryptoOperationResult& result) 55 void MockWebCrypto::encrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W ebKit::WebCryptoKey& key, const unsigned char* data, size_t dataSize, WebKit::We bCryptoResult result)
133 { 56 {
134 result.initializationSucceeded(new MockCryptoOperation(algorithm, Encrypt, r esult)); 57 result.completeWithError();
135 } 58 }
136 59
137 void MockWebCrypto::decrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W ebKit::WebCryptoKey& key, WebKit::WebCryptoOperationResult& result) 60 void MockWebCrypto::decrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W ebKit::WebCryptoKey& key, const unsigned char* data, size_t dataSize, WebKit::We bCryptoResult result)
138 { 61 {
139 result.initializationSucceeded(new MockCryptoOperation(algorithm, Decrypt, r esult)); 62 result.completeWithError();
140 } 63 }
141 64
142 void MockWebCrypto::sign(const WebKit::WebCryptoAlgorithm& algorithm, const WebK it::WebCryptoKey& key, WebKit::WebCryptoOperationResult& result) 65 void MockWebCrypto::sign(const WebKit::WebCryptoAlgorithm& algorithm, const WebK it::WebCryptoKey& key, const unsigned char* data, size_t dataSize, WebKit::WebCr yptoResult result)
143 { 66 {
144 result.initializationSucceeded(new MockCryptoOperation(algorithm, Sign, resu lt)); 67 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac)
68 return result.completeWithError();
69
70 std::string signedData = mockSign(data, dataSize);
71 result.completeWithBuffer(signedData.data(), signedData.size());
145 } 72 }
146 73
147 void MockWebCrypto::verifySignature(const WebKit::WebCryptoAlgorithm& algorithm, const WebKit::WebCryptoKey& key, const unsigned char* signature, size_t signatu reLength, WebKit::WebCryptoOperationResult& result) 74 void MockWebCrypto::verifySignature(const WebKit::WebCryptoAlgorithm& algorithm, const WebKit::WebCryptoKey& key, const unsigned char* signatureBytes, size_t si gnatureSize, const unsigned char* data, size_t dataSize, WebKit::WebCryptoResult result)
148 { 75 {
149 MockCryptoOperation* op = new MockCryptoOperation(algorithm, Verify, result) ; 76 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac)
150 op->setSignature(signature, signatureLength); 77 return result.completeWithError();
151 result.initializationSucceeded(op); 78
79 std::string signature = std::string(reinterpret_cast<const char*>(signatureB ytes), signatureSize);
80 std::string expectedSignature = mockSign(data, dataSize);
81 result.completeWithBoolean(expectedSignature == signature);
152 } 82 }
153 83
154 void MockWebCrypto::digest(const WebKit::WebCryptoAlgorithm& algorithm, WebKit:: WebCryptoOperationResult& result) 84 void MockWebCrypto::digest(const WebKit::WebCryptoAlgorithm& algorithm, const un signed char* data, size_t dataSize, WebKit::WebCryptoResult result)
155 { 85 {
156 result.initializationSucceeded(new MockCryptoOperation(algorithm, Digest, re sult)); 86 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdSha1)
87 return result.completeWithError();
88
89 std::string input = std::string(reinterpret_cast<const char*>(data), dataSiz e);
90
91 if (input.empty()) {
92 const unsigned char resultBytes[] = {0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0 x09};
93 result.completeWithBuffer(resultBytes, sizeof(resultBytes));
94 } else if (input == std::string("\x00", 1)) {
95 const unsigned char resultBytes[] = {0x5b, 0xa9, 0x3c, 0x9d, 0xb0, 0xcf, 0xf9, 0x3f, 0x52, 0xb5, 0x21, 0xd7, 0x42, 0x0e, 0x43, 0xf6, 0xed, 0xa2, 0x78, 0 x4f};
96 result.completeWithBuffer(resultBytes, sizeof(resultBytes));
97 } else if (input == std::string("\x00\x01\x02\x03\x04\x05", 6)) {
98 const unsigned char resultBytes[] = {0x86, 0x84, 0x60, 0xd9, 0x8d, 0x09, 0xd8, 0xbb, 0xb9, 0x3d, 0x7b, 0x6c, 0xdd, 0x15, 0xcc, 0x7f, 0xbe, 0xc6, 0x76, 0 xb9};
99 result.completeWithBuffer(resultBytes, sizeof(resultBytes));
100 } else {
101 result.completeWithError();
102 }
157 } 103 }
158 104
159 void MockWebCrypto::generateKey(const WebKit::WebCryptoAlgorithm& algorithm, boo l extractable, WebKit::WebCryptoKeyUsageMask usages, WebKit::WebCryptoKeyOperati onResult& result) 105 void MockWebCrypto::generateKey(const WebKit::WebCryptoAlgorithm& algorithm, boo l extractable, WebKit::WebCryptoKeyUsageMask usages, WebKit::WebCryptoResult res ult)
160 { 106 {
161 result.completeWithKey(WebKit::WebCryptoKey::create(0, WebKit::WebCryptoKeyT ypePrivate, extractable, algorithm, usages)); 107 result.completeWithKey(WebKit::WebCryptoKey::create(0, WebKit::WebCryptoKeyT ypePrivate, extractable, algorithm, usages));
162 } 108 }
163 109
164 void MockWebCrypto::importKey(WebKit::WebCryptoKeyFormat, const unsigned char* k eyData, size_t keyDataSize, const WebKit::WebCryptoAlgorithm& algorithm, bool ex tractable, WebKit::WebCryptoKeyUsageMask usages, WebKit::WebCryptoKeyOperationRe sult& result) 110 void MockWebCrypto::importKey(WebKit::WebCryptoKeyFormat, const unsigned char* k eyData, size_t keyDataSize, const WebKit::WebCryptoAlgorithm& algorithm, bool ex tractable, WebKit::WebCryptoKeyUsageMask usages, WebKit::WebCryptoResult result)
165 { 111 {
166 std::string keyDataString(reinterpret_cast<const char*>(keyData), keyDataSiz e); 112 std::string keyDataString(reinterpret_cast<const char*>(keyData), keyDataSiz e);
167 113
168 if (keyDataString == "reject") { 114 if (keyDataString == "error")
169 result.completeWithError(); 115 return result.completeWithError();
170 } else if (keyDataString == "throw") { 116
171 result.initializationFailed(); 117 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePrivate;
172 } else { 118 if (keyDataString == "public")
173 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePrivate; 119 type = WebKit::WebCryptoKeyTypePublic;
174 if (keyDataString == "public") 120
175 type = WebKit::WebCryptoKeyTypePublic; 121 result.completeWithKey(WebKit::WebCryptoKey::create(0, type, extractable, al gorithm, usages));
176 result.completeWithKey(WebKit::WebCryptoKey::create(0, type, extractable , algorithm, usages));
177 }
178 } 122 }
179 123
180 } // namespace WebTestRunner 124 } // namespace WebTestRunner
OLDNEW
« no previous file with comments | « Source/testing/runner/MockWebCrypto.h ('k') | public/platform/WebCrypto.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698