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

Side by Side Diff: Tools/DumpRenderTree/chromium/TestRunner/src/MockWebCrypto.cpp

Issue 21561004: WebCrypto: Add crypto.subtle.verify() to the platform interfaces. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Add missing file common.js 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
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 23 matching lines...) Expand all
34 #include "public/platform/WebCryptoAlgorithm.h" 34 #include "public/platform/WebCryptoAlgorithm.h"
35 #include <string> 35 #include <string>
36 #include <string.h> 36 #include <string.h>
37 37
38 using namespace WebKit; 38 using namespace WebKit;
39 39
40 namespace WebTestRunner { 40 namespace WebTestRunner {
41 41
42 namespace { 42 namespace {
43 43
44 enum Operation {
45 Encrypt,
46 Decrypt,
47 Sign,
48 Verify,
49 Digest,
50 };
51
44 class MockCryptoOperation : public WebKit::WebCryptoOperation { 52 class MockCryptoOperation : public WebKit::WebCryptoOperation {
45 public: 53 public:
46 MockCryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, const WebKi t::WebCryptoOperationResult& result) : m_algorithm(algorithm), m_result(result) { } 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) { }
47 58
48 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE 59 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE
49 { 60 {
50 // Don't buffer too much data. 61 // Don't buffer too much data.
51 if (m_data.size() + size > 6) { 62 if (m_data.size() + size > 6) {
52 m_result.completeWithError(); 63 m_result.completeWithError();
53 delete this; 64 delete this;
54 return; 65 return;
55 } 66 }
56 67
57 if (size) 68 if (size)
58 m_data.append(reinterpret_cast<const char*>(bytes), size); 69 m_data.append(reinterpret_cast<const char*>(bytes), size);
59 } 70 }
60 71
61 virtual void abort() OVERRIDE 72 virtual void abort() OVERRIDE
62 { 73 {
63 delete this; 74 delete this;
64 } 75 }
65 76
66 virtual void finish() OVERRIDE 77 virtual void finish() OVERRIDE
67 { 78 {
68 const unsigned char digest1[20] = {0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0 x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x0 9}; 79 if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdSha1 && m_operation == Digest) {
69 const unsigned char digest2[20] = {0x5b, 0xa9, 0x3c, 0x9d, 0xb0, 0xcf, 0 xf9, 0x3f, 0x52, 0xb5, 0x21, 0xd7, 0x42, 0x0e, 0x43, 0xf6, 0xed, 0xa2, 0x78, 0x4 f};
70 const unsigned char digest3[20] = {0x86, 0x84, 0x60, 0xd9, 0x8d, 0x09, 0 xd8, 0xbb, 0xb9, 0x3d, 0x7b, 0x6c, 0xdd, 0x15, 0xcc, 0x7f, 0xbe, 0xc6, 0x76, 0xb 9};
71
72 const unsigned char* result = 0;
73 size_t resultSize;
74
75 if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdSha1) {
76 resultSize = 20;
77
78 if (m_data.empty()) { 80 if (m_data.empty()) {
79 result = digest1; 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));
80 } else if (m_data == std::string("\x00", 1)) { 83 } else if (m_data == std::string("\x00", 1)) {
81 result = digest2; 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));
82 } else if (m_data == std::string("\x00\x01\x02\x03\x04\x05", 6)) { 86 } else if (m_data == std::string("\x00\x01\x02\x03\x04\x05", 6)) {
83 result = digest3; 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();
84 } 91 }
85 } 92 } else if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdHmac && m_ope ration == Sign) {
86 93 std::string result = "signed HMAC:" + m_data;
87 if (result) { 94 completeWithArrayBuffer(result.data(), result.size());
88 WebKit::WebArrayBuffer buffer = WebKit::WebArrayBuffer::create(resul tSize, 1); 95 } else if (m_algorithm.id() == WebKit::WebCryptoAlgorithmIdHmac && m_ope ration == Verify) {
89 memcpy(buffer.data(), result, resultSize); 96 std::string expectedSignature = "signed HMAC:" + m_data;
90 m_result.completeWithArrayBuffer(buffer); 97 m_result.completeWithBoolean(expectedSignature == m_signature);
91 } else { 98 } else {
92 m_result.completeWithError(); 99 m_result.completeWithError();
93 } 100 }
94 delete this; 101 delete this;
95 } 102 }
96 103
97 protected: 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
98 WebKit::WebCryptoAlgorithm m_algorithm; 117 WebKit::WebCryptoAlgorithm m_algorithm;
118 Operation m_operation;
99 WebKit::WebCryptoOperationResult m_result; 119 WebKit::WebCryptoOperationResult m_result;
100 std::string m_data; 120 std::string m_data;
121 std::string m_signature;
101 }; 122 };
102 123
103 } // namespace 124 } // namespace
104 125
105 MockWebCrypto* MockWebCrypto::get() 126 MockWebCrypto* MockWebCrypto::get()
106 { 127 {
107 static MockWebCrypto crypto; 128 static MockWebCrypto crypto;
108 return &crypto; 129 return &crypto;
109 } 130 }
110 131
111 void MockWebCrypto::encrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W ebKit::WebCryptoKey& key, WebKit::WebCryptoOperationResult& result) 132 void MockWebCrypto::encrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W ebKit::WebCryptoKey& key, WebKit::WebCryptoOperationResult& result)
112 { 133 {
113 result.initializationSucceeded(new MockCryptoOperation(algorithm, result)); 134 result.initializationSucceeded(new MockCryptoOperation(algorithm, Encrypt, r esult));
114 } 135 }
115 136
116 void MockWebCrypto::decrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W ebKit::WebCryptoKey& key, WebKit::WebCryptoOperationResult& result) 137 void MockWebCrypto::decrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W ebKit::WebCryptoKey& key, WebKit::WebCryptoOperationResult& result)
117 { 138 {
118 result.initializationSucceeded(new MockCryptoOperation(algorithm, result)); 139 result.initializationSucceeded(new MockCryptoOperation(algorithm, Decrypt, r esult));
119 } 140 }
120 141
121 void MockWebCrypto::sign(const WebKit::WebCryptoAlgorithm& algorithm, const WebK it::WebCryptoKey& key, WebKit::WebCryptoOperationResult& result) 142 void MockWebCrypto::sign(const WebKit::WebCryptoAlgorithm& algorithm, const WebK it::WebCryptoKey& key, WebKit::WebCryptoOperationResult& result)
122 { 143 {
123 result.initializationSucceeded(new MockCryptoOperation(algorithm, result)); 144 result.initializationSucceeded(new MockCryptoOperation(algorithm, Sign, resu lt));
145 }
146
147 void MockWebCrypto::verifySignature(const WebKit::WebCryptoAlgorithm& algorithm, const WebKit::WebCryptoKey& key, const unsigned char* signature, size_t signatu reLength, WebKit::WebCryptoOperationResult& result)
148 {
149 MockCryptoOperation* op = new MockCryptoOperation(algorithm, Verify, result) ;
150 op->setSignature(signature, signatureLength);
151 result.initializationSucceeded(op);
124 } 152 }
125 153
126 void MockWebCrypto::digest(const WebKit::WebCryptoAlgorithm& algorithm, WebKit:: WebCryptoOperationResult& result) 154 void MockWebCrypto::digest(const WebKit::WebCryptoAlgorithm& algorithm, WebKit:: WebCryptoOperationResult& result)
127 { 155 {
128 result.initializationSucceeded(new MockCryptoOperation(algorithm, result)); 156 result.initializationSucceeded(new MockCryptoOperation(algorithm, Digest, re sult));
129 } 157 }
130 158
131 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) 159 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)
132 { 160 {
133 std::string keyDataString(reinterpret_cast<const char*>(keyData), keyDataSiz e); 161 std::string keyDataString(reinterpret_cast<const char*>(keyData), keyDataSiz e);
134 162
135 if (keyDataString == "reject") { 163 if (keyDataString == "reject") {
136 result.completeWithError(); 164 result.completeWithError();
137 } else if (keyDataString == "throw") { 165 } else if (keyDataString == "throw") {
138 result.initializationFailed(); 166 result.initializationFailed();
139 } else { 167 } else {
140 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePrivate; 168 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePrivate;
141 if (keyDataString == "public") 169 if (keyDataString == "public")
142 type = WebKit::WebCryptoKeyTypePublic; 170 type = WebKit::WebCryptoKeyTypePublic;
143 result.completeWithKey(WebKit::WebCryptoKey::create(0, type, extractable , algorithm, usages)); 171 result.completeWithKey(WebKit::WebCryptoKey::create(0, type, extractable , algorithm, usages));
144 } 172 }
145 } 173 }
146 174
147 } // namespace WebTestRunner 175 } // namespace WebTestRunner
OLDNEW
« no previous file with comments | « Tools/DumpRenderTree/chromium/TestRunner/src/MockWebCrypto.h ('k') | public/platform/WebCrypto.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698