1 /*
2 * Copyright (C) 2014 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 "config.h"
32 #include "public/platform/WebCryptoKeyAlgorithm.h"
33
34 #include "wtf/OwnPtr.h"
35 #include "wtf/ThreadSafeRefCounted.h"
36
37 namespace blink {
38
39 // FIXME: Remove the need for this.
createHash(WebCryptoAlgorithmId hash)40 WebCryptoAlgorithm createHash(WebCryptoAlgorithmId hash)
41 {
42 return WebCryptoAlgorithm::adoptParamsAndCreate(hash, 0);
43 }
44
45 class WebCryptoKeyAlgorithmPrivate : public ThreadSafeRefCounted<WebCryptoKeyAlgorithmPrivate> {
46 public:
WebCryptoKeyAlgorithmPrivate(WebCryptoAlgorithmId id,PassOwnPtr<WebCryptoKeyAlgorithmParams> params)47 WebCryptoKeyAlgorithmPrivate(WebCryptoAlgorithmId id, PassOwnPtr<WebCryptoKeyAlgorithmParams> params)
48 : id(id)
49 , params(params)
50 {
51 }
52
53 WebCryptoAlgorithmId id;
54 OwnPtr<WebCryptoKeyAlgorithmParams> params;
55 };
56
WebCryptoKeyAlgorithm(WebCryptoAlgorithmId id,PassOwnPtr<WebCryptoKeyAlgorithmParams> params)57 WebCryptoKeyAlgorithm::WebCryptoKeyAlgorithm(WebCryptoAlgorithmId id, PassOwnPtr<WebCryptoKeyAlgorithmParams> params)
58 : m_private(adoptRef(new WebCryptoKeyAlgorithmPrivate(id, params)))
59 {
60 }
61
adoptParamsAndCreate(WebCryptoAlgorithmId id,WebCryptoKeyAlgorithmParams * params)62 WebCryptoKeyAlgorithm WebCryptoKeyAlgorithm::adoptParamsAndCreate(WebCryptoAlgorithmId id, WebCryptoKeyAlgorithmParams* params)
63 {
64 return WebCryptoKeyAlgorithm(id, adoptPtr(params));
65 }
66
createAes(WebCryptoAlgorithmId id,unsigned short keyLengthBits)67 WebCryptoKeyAlgorithm WebCryptoKeyAlgorithm::createAes(WebCryptoAlgorithmId id, unsigned short keyLengthBits)
68 {
69 // FIXME: Verify that id is an AES algorithm.
70 // FIXME: Move this somewhere more general.
71 if (keyLengthBits != 128 && keyLengthBits != 192 && keyLengthBits != 256)
72 return WebCryptoKeyAlgorithm();
73 return WebCryptoKeyAlgorithm(id, adoptPtr(new WebCryptoAesKeyAlgorithmParams(keyLengthBits)));
74 }
75
createHmac(WebCryptoAlgorithmId hash,unsigned keyLengthBits)76 WebCryptoKeyAlgorithm WebCryptoKeyAlgorithm::createHmac(WebCryptoAlgorithmId hash, unsigned keyLengthBits)
77 {
78 if (!WebCryptoAlgorithm::isHash(hash))
79 return WebCryptoKeyAlgorithm();
80 return WebCryptoKeyAlgorithm(WebCryptoAlgorithmIdHmac, adoptPtr(new WebCryptoHmacKeyAlgorithmParams(createHash(hash), keyLengthBits)));
81 }
82
createRsaHashed(WebCryptoAlgorithmId id,unsigned modulusLengthBits,const unsigned char * publicExponent,unsigned publicExponentSize,WebCryptoAlgorithmId hash)83 WebCryptoKeyAlgorithm WebCryptoKeyAlgorithm::createRsaHashed(WebCryptoAlgorithmId id, unsigned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExponentSize, WebCryptoAlgorithmId hash)
84 {
85 // FIXME: Verify that id is an RSA algorithm which expects a hash
86 if (!WebCryptoAlgorithm::isHash(hash))
87 return WebCryptoKeyAlgorithm();
88 return WebCryptoKeyAlgorithm(id, adoptPtr(new WebCryptoRsaHashedKeyAlgorithmParams(modulusLengthBits, publicExponent, publicExponentSize, createHash(hash))));
89 }
90
isNull() const91 bool WebCryptoKeyAlgorithm::isNull() const
92 {
93 return m_private.isNull();
94 }
95
id() const96 WebCryptoAlgorithmId WebCryptoKeyAlgorithm::id() const
97 {
98 ASSERT(!isNull());
99 return m_private->id;
100 }
101
paramsType() const102 WebCryptoKeyAlgorithmParamsType WebCryptoKeyAlgorithm::paramsType() const
103 {
104 ASSERT(!isNull());
105 if (!m_private->params.get())
106 return WebCryptoKeyAlgorithmParamsTypeNone;
107 return m_private->params->type();
108 }
109
aesParams() const110 WebCryptoAesKeyAlgorithmParams* WebCryptoKeyAlgorithm::aesParams() const
111 {
112 ASSERT(!isNull());
113 if (paramsType() == WebCryptoKeyAlgorithmParamsTypeAes)
114 return static_cast<WebCryptoAesKeyAlgorithmParams*>(m_private->params.get());
115 return 0;
116 }
117
hmacParams() const118 WebCryptoHmacKeyAlgorithmParams* WebCryptoKeyAlgorithm::hmacParams() const
119 {
120 ASSERT(!isNull());
121 if (paramsType() == WebCryptoKeyAlgorithmParamsTypeHmac)
122 return static_cast<WebCryptoHmacKeyAlgorithmParams*>(m_private->params.get());
123 return 0;
124 }
125
rsaHashedParams() const126 WebCryptoRsaHashedKeyAlgorithmParams* WebCryptoKeyAlgorithm::rsaHashedParams() const
127 {
128 ASSERT(!isNull());
129 if (paramsType() == WebCryptoKeyAlgorithmParamsTypeRsaHashed)
130 return static_cast<WebCryptoRsaHashedKeyAlgorithmParams*>(m_private->params.get());
131 return 0;
132 }
133
assign(const WebCryptoKeyAlgorithm & other)134 void WebCryptoKeyAlgorithm::assign(const WebCryptoKeyAlgorithm& other)
135 {
136 m_private = other.m_private;
137 }
138
reset()139 void WebCryptoKeyAlgorithm::reset()
140 {
141 m_private.reset();
142 }
143
144 } // namespace blink
145