| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 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. | |
| 29 */ | |
| 30 | |
| 31 #include "MockWebCrypto.h" | |
| 32 | |
| 33 #include "public/platform/WebCryptoAlgorithm.h" | |
| 34 #include <string> | |
| 35 | |
| 36 using namespace WebKit; | |
| 37 | |
| 38 namespace WebTestRunner { | |
| 39 | |
| 40 namespace { | |
| 41 | |
| 42 std::string mockSign(const unsigned char* bytes, unsigned size) | |
| 43 { | |
| 44 return "signed HMAC:" + std::string(reinterpret_cast<const char*>(bytes), si
ze); | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 MockWebCrypto* MockWebCrypto::get() | |
| 50 { | |
| 51 static MockWebCrypto crypto; | |
| 52 return &crypto; | |
| 53 } | |
| 54 | |
| 55 void MockWebCrypto::encrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W
ebKit::WebCryptoKey& key, const unsigned char* data, unsigned dataSize, WebKit::
WebCryptoResult result) | |
| 56 { | |
| 57 result.completeWithError(); | |
| 58 } | |
| 59 | |
| 60 void MockWebCrypto::decrypt(const WebKit::WebCryptoAlgorithm& algorithm, const W
ebKit::WebCryptoKey& key, const unsigned char* data, unsigned dataSize, WebKit::
WebCryptoResult result) | |
| 61 { | |
| 62 result.completeWithError(); | |
| 63 } | |
| 64 | |
| 65 void MockWebCrypto::sign(const WebKit::WebCryptoAlgorithm& algorithm, const WebK
it::WebCryptoKey& key, const unsigned char* data, unsigned dataSize, WebKit::Web
CryptoResult result) | |
| 66 { | |
| 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()); | |
| 72 } | |
| 73 | |
| 74 void MockWebCrypto::verifySignature(const WebKit::WebCryptoAlgorithm& algorithm,
const WebKit::WebCryptoKey& key, const unsigned char* signatureBytes, unsigned
signatureSize, const unsigned char* data, unsigned dataSize, WebKit::WebCryptoRe
sult result) | |
| 75 { | |
| 76 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac) | |
| 77 return result.completeWithError(); | |
| 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); | |
| 82 } | |
| 83 | |
| 84 void MockWebCrypto::digest(const WebKit::WebCryptoAlgorithm& algorithm, const un
signed char* data, unsigned dataSize, WebKit::WebCryptoResult result) | |
| 85 { | |
| 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 } | |
| 103 } | |
| 104 | |
| 105 void MockWebCrypto::generateKey(const WebKit::WebCryptoAlgorithm& algorithm, boo
l extractable, WebKit::WebCryptoKeyUsageMask usages, WebKit::WebCryptoResult res
ult) | |
| 106 { | |
| 107 if (algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5) { | |
| 108 result.completeWithKeyPair(WebKit::WebCryptoKey::create(0, WebKit::WebCr
yptoKeyTypePublic, extractable, algorithm, usages), WebKit::WebCryptoKey::create
(0, WebKit::WebCryptoKeyTypePrivate, extractable, algorithm, usages)); | |
| 109 } else { | |
| 110 result.completeWithKey(WebKit::WebCryptoKey::create(0, WebKit::WebCrypto
KeyTypePrivate, extractable, algorithm, usages)); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void MockWebCrypto::importKey(WebKit::WebCryptoKeyFormat, const unsigned char* k
eyData, unsigned keyDataSize, const WebKit::WebCryptoAlgorithm& algorithm, bool
extractable, WebKit::WebCryptoKeyUsageMask usages, WebKit::WebCryptoResult resul
t) | |
| 115 { | |
| 116 std::string keyDataString(reinterpret_cast<const char*>(keyData), keyDataSiz
e); | |
| 117 | |
| 118 if (keyDataString == "error") | |
| 119 return result.completeWithError(); | |
| 120 | |
| 121 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePrivate; | |
| 122 if (keyDataString == "public") | |
| 123 type = WebKit::WebCryptoKeyTypePublic; | |
| 124 | |
| 125 result.completeWithKey(WebKit::WebCryptoKey::create(0, type, extractable, al
gorithm, usages)); | |
| 126 } | |
| 127 | |
| 128 void MockWebCrypto::exportKey(WebKit::WebCryptoKeyFormat format, const WebKit::W
ebCryptoKey& key, WebKit::WebCryptoResult result) | |
| 129 { | |
| 130 std::string buffer; | |
| 131 | |
| 132 if (format == WebKit::WebCryptoKeyFormatRaw) | |
| 133 buffer = "raw"; | |
| 134 else if (format == WebKit::WebCryptoKeyFormatPkcs8) | |
| 135 buffer = "pkcs8"; | |
| 136 | |
| 137 result.completeWithBuffer(buffer.data(), buffer.size()); | |
| 138 } | |
| 139 | |
| 140 | |
| 141 } // namespace WebTestRunner | |
| OLD | NEW |