2 * Copyright (C) 2014 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef WebCryptoKeyAlgorithmParams_h
32 #define WebCryptoKeyAlgorithmParams_h
34 #include "WebCommon.h"
35 #include "WebCryptoAlgorithm.h"
36 #include "WebVector.h"
39 #include "platform/heap/Handle.h"
44 // Interface used for serializing WebCryptoKeyAlgorithmParams to a javascript
46 class WebCryptoKeyAlgorithmDictionary
{
51 virtual ~WebCryptoKeyAlgorithmDictionary() { }
53 virtual void setString(const char*, const char*) = 0;
54 virtual void setUint(const char*, unsigned) = 0;
55 virtual void setAlgorithm(const char*, const WebCryptoAlgorithm
&) = 0;
56 virtual void setUint8Array(const char*, const WebVector
<unsigned char>&) = 0;
59 enum WebCryptoKeyAlgorithmParamsType
{
60 WebCryptoKeyAlgorithmParamsTypeNone
,
61 WebCryptoKeyAlgorithmParamsTypeHmac
,
62 WebCryptoKeyAlgorithmParamsTypeAes
,
63 WebCryptoKeyAlgorithmParamsTypeRsaHashed
,
64 WebCryptoKeyAlgorithmParamsTypeEc
,
67 class WebCryptoKeyAlgorithmParams
{
69 virtual ~WebCryptoKeyAlgorithmParams() { }
70 virtual WebCryptoKeyAlgorithmParamsType
type() const
72 return WebCryptoKeyAlgorithmParamsTypeNone
;
75 virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary
*) const = 0;
78 class WebCryptoAesKeyAlgorithmParams
: public WebCryptoKeyAlgorithmParams
{
80 explicit WebCryptoAesKeyAlgorithmParams(unsigned short lengthBits
)
81 : m_lengthBits(lengthBits
)
85 unsigned short lengthBits() const
90 virtual WebCryptoKeyAlgorithmParamsType
type() const
92 return WebCryptoKeyAlgorithmParamsTypeAes
;
95 virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary
* dict
) const
97 dict
->setUint("length", m_lengthBits
);
101 unsigned short m_lengthBits
;
104 class WebCryptoHmacKeyAlgorithmParams
: public WebCryptoKeyAlgorithmParams
{
106 WebCryptoHmacKeyAlgorithmParams(const WebCryptoAlgorithm
& hash
, unsigned lengthBits
)
108 , m_lengthBits(lengthBits
)
112 const WebCryptoAlgorithm
& hash() const
117 unsigned lengthBits() const
122 virtual WebCryptoKeyAlgorithmParamsType
type() const
124 return WebCryptoKeyAlgorithmParamsTypeHmac
;
127 virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary
* dict
) const
129 dict
->setAlgorithm("hash", m_hash
);
130 dict
->setUint("length", m_lengthBits
);
134 WebCryptoAlgorithm m_hash
;
135 unsigned m_lengthBits
;
138 class WebCryptoRsaHashedKeyAlgorithmParams
: public WebCryptoKeyAlgorithmParams
{
140 WebCryptoRsaHashedKeyAlgorithmParams(unsigned modulusLengthBits
, const unsigned char* publicExponent
, unsigned publicExponentSize
, const WebCryptoAlgorithm
& hash
)
141 : m_modulusLengthBits(modulusLengthBits
)
142 , m_publicExponent(publicExponent
, publicExponentSize
)
147 unsigned modulusLengthBits() const
149 return m_modulusLengthBits
;
152 const WebVector
<unsigned char>& publicExponent() const
154 return m_publicExponent
;
157 const WebCryptoAlgorithm
& hash() const
162 virtual WebCryptoKeyAlgorithmParamsType
type() const
164 return WebCryptoKeyAlgorithmParamsTypeRsaHashed
;
167 virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary
* dict
) const
169 dict
->setAlgorithm("hash", m_hash
);
170 dict
->setUint("modulusLength", m_modulusLengthBits
);
171 dict
->setUint8Array("publicExponent", m_publicExponent
);
175 unsigned m_modulusLengthBits
;
176 WebVector
<unsigned char> m_publicExponent
;
177 WebCryptoAlgorithm m_hash
;
180 class WebCryptoEcKeyAlgorithmParams
: public WebCryptoKeyAlgorithmParams
{
182 explicit WebCryptoEcKeyAlgorithmParams(WebCryptoNamedCurve namedCurve
)
183 : m_namedCurve(namedCurve
)
187 WebCryptoNamedCurve
namedCurve() const
192 virtual WebCryptoKeyAlgorithmParamsType
type() const
194 return WebCryptoKeyAlgorithmParamsTypeEc
;
197 virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary
* dict
) const
199 switch (m_namedCurve
) {
200 case WebCryptoNamedCurveP256
:
201 dict
->setString("namedCurve", "P-256");
203 case WebCryptoNamedCurveP384
:
204 dict
->setString("namedCurve", "P-384");
206 case WebCryptoNamedCurveP521
:
207 dict
->setString("namedCurve", "P-521");
213 const WebCryptoNamedCurve m_namedCurve
;