OLD | NEW |
---|---|
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 #include "wtf/ArrayBufferView.h" | 42 #include "wtf/ArrayBufferView.h" |
43 | 43 |
44 namespace WebCore { | 44 namespace WebCore { |
45 | 45 |
46 // FIXME: Outstanding KeyOperations and CryptoOperations should be aborted when | 46 // FIXME: Outstanding KeyOperations and CryptoOperations should be aborted when |
47 // tearing down SubtleCrypto (to avoid problems completing a | 47 // tearing down SubtleCrypto (to avoid problems completing a |
48 // ScriptPromiseResolver which is no longer valid). | 48 // ScriptPromiseResolver which is no longer valid). |
49 | 49 |
50 namespace { | 50 namespace { |
51 | 51 |
52 // FIXME: Temporary | 52 bool keyCanBeUsedForAlgorithm(const WebKit::WebCryptoKey& key, const WebKit::Web CryptoAlgorithm& algorithm, ExceptionState& es) |
53 PassRefPtr<CryptoOperation> dummyOperation(const Dictionary& rawAlgorithm, Algor ithmOperation operationType, ExceptionState& es) | 53 { |
54 // FIXME: Need to enforce that the key's algorithm matches the operation, | |
55 // and that the key's usages allow it to be used with this operation. | |
56 return true; | |
57 } | |
58 | |
59 PassRefPtr<CryptoOperation> createCryptoOperation(const Dictionary& rawAlgorithm , Key* key, AlgorithmOperation operationType, ExceptionState& es) | |
54 { | 60 { |
55 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); | 61 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); |
56 if (!platformCrypto) { | 62 if (!platformCrypto) { |
57 es.throwDOMException(NotSupportedError); | 63 es.throwDOMException(NotSupportedError); |
58 return 0; | 64 return 0; |
59 } | 65 } |
60 | 66 |
61 WebKit::WebCryptoAlgorithm algorithm; | 67 WebKit::WebCryptoAlgorithm algorithm; |
62 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, es)) | 68 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, es)) |
63 return 0; | 69 return 0; |
64 | 70 |
71 // All operations other than Digest require a valid Key. | |
72 if (operationType != Digest) { | |
73 if (!key) { | |
74 es.throwDOMException(TypeError); | |
75 return 0; | |
76 } | |
77 | |
78 if (!keyCanBeUsedForAlgorithm(key->key(), algorithm, es)) { | |
79 return 0; | |
80 } | |
81 } | |
82 | |
65 RefPtr<CryptoOperationImpl> opImpl = CryptoOperationImpl::create(); | 83 RefPtr<CryptoOperationImpl> opImpl = CryptoOperationImpl::create(); |
66 WebKit::WebCryptoOperationResult result(opImpl.get()); | 84 WebKit::WebCryptoOperationResult result(opImpl.get()); |
67 platformCrypto->digest(algorithm, result); | 85 |
86 switch (operationType) { | |
87 case Encrypt: | |
88 platformCrypto->encrypt(algorithm, key->key(), result); | |
89 break; | |
90 case Decrypt: | |
91 platformCrypto->decrypt(algorithm, key->key(), result); | |
92 break; | |
93 case Sign: | |
94 platformCrypto->sign(algorithm, key->key(), result); | |
95 break; | |
96 case Digest: | |
97 platformCrypto->digest(algorithm, result); | |
98 break; | |
99 default: | |
100 ASSERT_NOT_REACHED(); | |
101 return 0; | |
102 } | |
103 | |
68 if (opImpl->throwInitializationError(es)) | 104 if (opImpl->throwInitializationError(es)) |
69 return 0; | 105 return 0; |
70 return CryptoOperation::create(algorithm, opImpl.get()); | 106 return CryptoOperation::create(algorithm, opImpl.get()); |
71 } | 107 } |
72 | 108 |
73 } // namespace | 109 } // namespace |
74 | 110 |
75 SubtleCrypto::SubtleCrypto() | 111 SubtleCrypto::SubtleCrypto() |
76 { | 112 { |
77 ScriptWrappable::init(this); | 113 ScriptWrappable::init(this); |
78 } | 114 } |
79 | 115 |
80 PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm , Key*, ExceptionState& es) | 116 PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm , Key* key, ExceptionState& es) |
81 { | 117 { |
82 return dummyOperation(rawAlgorithm, Encrypt, es); | 118 return createCryptoOperation(rawAlgorithm, key, Encrypt, es); |
83 } | 119 } |
84 | 120 |
85 PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm , Key*, ExceptionState& es) | 121 PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm , Key* key, ExceptionState& es) |
86 { | 122 { |
87 return dummyOperation(rawAlgorithm, Decrypt, es); | 123 return createCryptoOperation(rawAlgorithm, key, Decrypt, es); |
88 } | 124 } |
89 | 125 |
90 PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, K ey*, ExceptionState& es) | 126 PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, K ey* key, ExceptionState& es) |
91 { | 127 { |
92 return dummyOperation(rawAlgorithm, Sign, es); | 128 return createCryptoOperation(rawAlgorithm, key, Sign, es); |
93 } | 129 } |
94 | 130 |
95 PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawA lgorithm, Key*, ExceptionState& es) | 131 PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawA lgorithm, Key* key, ExceptionState& es) |
96 { | 132 { |
97 return dummyOperation(rawAlgorithm, Verify, es); | 133 // FIXME |
abarth-chromium
2013/07/31 00:59:59
Rather than an empty FIXME comment, you can call t
eroman
2013/07/31 01:23:16
Done.
| |
134 return 0; | |
98 } | 135 } |
99 | 136 |
100 PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm, ExceptionState& es) | 137 PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm, ExceptionState& es) |
101 { | 138 { |
102 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); | 139 return createCryptoOperation(rawAlgorithm, 0, Digest, es); |
103 if (!platformCrypto) { | |
104 es.throwDOMException(NotSupportedError); | |
105 return 0; | |
106 } | |
107 | |
108 WebKit::WebCryptoAlgorithm algorithm; | |
109 if (!normalizeAlgorithm(rawAlgorithm, Digest, algorithm, es)) | |
110 return 0; | |
111 | |
112 RefPtr<CryptoOperationImpl> opImpl = CryptoOperationImpl::create(); | |
113 WebKit::WebCryptoOperationResult result(opImpl.get()); | |
114 platformCrypto->digest(algorithm, result); | |
115 if (opImpl->throwInitializationError(es)) | |
116 return 0; | |
117 return CryptoOperation::create(algorithm, opImpl.get()); | |
118 } | 140 } |
119 | 141 |
120 ScriptObject SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* k eyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& es) | 142 ScriptObject SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* k eyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& es) |
121 { | 143 { |
122 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); | 144 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); |
123 if (!platformCrypto) { | 145 if (!platformCrypto) { |
124 es.throwDOMException(NotSupportedError); | 146 es.throwDOMException(NotSupportedError); |
125 return ScriptObject(); | 147 return ScriptObject(); |
126 } | 148 } |
127 | 149 |
(...skipping 20 matching lines...) Expand all Loading... | |
148 | 170 |
149 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas eAddress()); | 171 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas eAddress()); |
150 | 172 |
151 RefPtr<KeyOperation> keyOp = KeyOperation::create(); | 173 RefPtr<KeyOperation> keyOp = KeyOperation::create(); |
152 WebKit::WebCryptoKeyOperationResult result(keyOp.get()); | 174 WebKit::WebCryptoKeyOperationResult result(keyOp.get()); |
153 platformCrypto->importKey(format, keyDataBytes, keyData->byteLength(), algor ithm, extractable, keyUsages, result); | 175 platformCrypto->importKey(format, keyDataBytes, keyData->byteLength(), algor ithm, extractable, keyUsages, result); |
154 return keyOp->returnValue(es); | 176 return keyOp->returnValue(es); |
155 } | 177 } |
156 | 178 |
157 } // namespace WebCore | 179 } // namespace WebCore |
OLD | NEW |