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#ifndef WebCryptoKeyAlgorithmParams_h
32#define WebCryptoKeyAlgorithmParams_h
33
34#include "WebCommon.h"
35#include "WebCryptoAlgorithm.h"
36#include "WebVector.h"
37
38namespace blink {
39
40// Interface used for serializing WebCryptoKeyAlgorithmParams to a javascript
41// dictionary.
42class WebCryptoKeyAlgorithmDictionary {
43public:
44    virtual ~WebCryptoKeyAlgorithmDictionary() { }
45
46    virtual void setString(const char*, const char*) = 0;
47    virtual void setUint(const char*, unsigned) = 0;
48    virtual void setAlgorithm(const char*, const WebCryptoAlgorithm&) = 0;
49    virtual void setUint8Array(const char*, const WebVector<unsigned char>&) = 0;
50};
51
52enum WebCryptoKeyAlgorithmParamsType {
53    WebCryptoKeyAlgorithmParamsTypeNone,
54    WebCryptoKeyAlgorithmParamsTypeHmac,
55    WebCryptoKeyAlgorithmParamsTypeAes,
56    WebCryptoKeyAlgorithmParamsTypeRsaHashed
57};
58
59class WebCryptoKeyAlgorithmParams {
60public:
61    virtual ~WebCryptoKeyAlgorithmParams() { }
62    virtual WebCryptoKeyAlgorithmParamsType type() const
63    {
64        return WebCryptoKeyAlgorithmParamsTypeNone;
65    }
66
67    virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary*) const = 0;
68};
69
70class WebCryptoAesKeyAlgorithmParams : public WebCryptoKeyAlgorithmParams {
71public:
72    explicit WebCryptoAesKeyAlgorithmParams(unsigned short lengthBits)
73        : m_lengthBits(lengthBits)
74    {
75    }
76
77    unsigned short lengthBits() const
78    {
79        return m_lengthBits;
80    }
81
82    virtual WebCryptoKeyAlgorithmParamsType type() const
83    {
84        return WebCryptoKeyAlgorithmParamsTypeAes;
85    }
86
87    virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary* dict) const
88    {
89        dict->setUint("length", m_lengthBits);
90    }
91
92private:
93    unsigned short m_lengthBits;
94};
95
96class WebCryptoHmacKeyAlgorithmParams : public WebCryptoKeyAlgorithmParams {
97public:
98    WebCryptoHmacKeyAlgorithmParams(const WebCryptoAlgorithm& hash, unsigned lengthBits)
99        : m_hash(hash)
100        , m_lengthBits(lengthBits)
101    {
102    }
103
104    const WebCryptoAlgorithm& hash() const
105    {
106        return m_hash;
107    }
108
109    unsigned lengthBits() const
110    {
111        return m_lengthBits;
112    }
113
114    virtual WebCryptoKeyAlgorithmParamsType type() const
115    {
116        return WebCryptoKeyAlgorithmParamsTypeHmac;
117    }
118
119    virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary* dict) const
120    {
121        dict->setAlgorithm("hash", m_hash);
122        dict->setUint("length", m_lengthBits);
123    }
124
125private:
126    WebCryptoAlgorithm m_hash;
127    unsigned m_lengthBits;
128};
129
130class WebCryptoRsaHashedKeyAlgorithmParams : public WebCryptoKeyAlgorithmParams {
131public:
132    WebCryptoRsaHashedKeyAlgorithmParams(unsigned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExponentSize, const WebCryptoAlgorithm& hash)
133        : m_modulusLengthBits(modulusLengthBits)
134        , m_publicExponent(publicExponent, publicExponentSize)
135        , m_hash(hash)
136    {
137    }
138
139    unsigned modulusLengthBits() const
140    {
141        return m_modulusLengthBits;
142    }
143
144    const WebVector<unsigned char>& publicExponent() const
145    {
146        return m_publicExponent;
147    }
148
149    const WebCryptoAlgorithm& hash() const
150    {
151        return m_hash;
152    }
153
154    virtual WebCryptoKeyAlgorithmParamsType type() const
155    {
156        return WebCryptoKeyAlgorithmParamsTypeRsaHashed;
157    }
158
159    virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary* dict) const
160    {
161        dict->setAlgorithm("hash", m_hash);
162        dict->setUint("modulusLength", m_modulusLengthBits);
163        dict->setUint8Array("publicExponent", m_publicExponent);
164    }
165
166private:
167    unsigned m_modulusLengthBits;
168    WebVector<unsigned char> m_publicExponent;
169    WebCryptoAlgorithm m_hash;
170};
171
172} // namespace blink
173
174#endif
175