1d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)/*
2d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * Copyright (C) 2014 Google Inc. All rights reserved.
3d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *
4d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
5d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * modification, are permitted provided that the following conditions are
6d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * met:
7d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *
8d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
9d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
10d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
11d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
12d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * in the documentation and/or other materials provided with the
13d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * distribution.
14d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
15d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
16d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * this software without specific prior written permission.
17d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *
18d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) */
30d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
31d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "config.h"
32d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "public/platform/WebCryptoKeyAlgorithm.h"
33d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
34d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "wtf/OwnPtr.h"
35d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "wtf/ThreadSafeRefCounted.h"
36d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
37d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)namespace blink {
38d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
3907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch// FIXME: Remove the need for this.
4007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben MurdochWebCryptoAlgorithm createHash(WebCryptoAlgorithmId hash)
4107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch{
4207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    return WebCryptoAlgorithm::adoptParamsAndCreate(hash, 0);
4307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch}
4407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
45d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)class WebCryptoKeyAlgorithmPrivate : public ThreadSafeRefCounted<WebCryptoKeyAlgorithmPrivate> {
46d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)public:
47d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    WebCryptoKeyAlgorithmPrivate(WebCryptoAlgorithmId id, PassOwnPtr<WebCryptoKeyAlgorithmParams> params)
48d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        : id(id)
49d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        , params(params)
50d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    {
51d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
52d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
53d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    WebCryptoAlgorithmId id;
54d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    OwnPtr<WebCryptoKeyAlgorithmParams> params;
55d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)};
56d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
57d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)WebCryptoKeyAlgorithm::WebCryptoKeyAlgorithm(WebCryptoAlgorithmId id, PassOwnPtr<WebCryptoKeyAlgorithmParams> params)
58d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    : m_private(adoptRef(new WebCryptoKeyAlgorithmPrivate(id, params)))
59d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
60d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
61d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
62d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)WebCryptoKeyAlgorithm WebCryptoKeyAlgorithm::adoptParamsAndCreate(WebCryptoAlgorithmId id, WebCryptoKeyAlgorithmParams* params)
63d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
64d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return WebCryptoKeyAlgorithm(id, adoptPtr(params));
65d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
66d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
6707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben MurdochWebCryptoKeyAlgorithm WebCryptoKeyAlgorithm::createAes(WebCryptoAlgorithmId id, unsigned short keyLengthBits)
6807a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch{
6907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    // FIXME: Verify that id is an AES algorithm.
7007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    // FIXME: Move this somewhere more general.
7107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    if (keyLengthBits != 128 && keyLengthBits != 192 && keyLengthBits != 256)
7207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        return WebCryptoKeyAlgorithm();
7307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    return WebCryptoKeyAlgorithm(id, adoptPtr(new WebCryptoAesKeyAlgorithmParams(keyLengthBits)));
7407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch}
7507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
7607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben MurdochWebCryptoKeyAlgorithm WebCryptoKeyAlgorithm::createHmac(WebCryptoAlgorithmId hash, unsigned keyLengthBits)
7707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch{
7807a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    if (!WebCryptoAlgorithm::isHash(hash))
7907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        return WebCryptoKeyAlgorithm();
8007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    return WebCryptoKeyAlgorithm(WebCryptoAlgorithmIdHmac, adoptPtr(new WebCryptoHmacKeyAlgorithmParams(createHash(hash), keyLengthBits)));
8107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch}
8207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
8307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben MurdochWebCryptoKeyAlgorithm WebCryptoKeyAlgorithm::createRsaHashed(WebCryptoAlgorithmId id, unsigned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExponentSize, WebCryptoAlgorithmId hash)
8407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch{
8507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    // FIXME: Verify that id is an RSA algorithm which expects a hash
8607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    if (!WebCryptoAlgorithm::isHash(hash))
8707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        return WebCryptoKeyAlgorithm();
8807a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    return WebCryptoKeyAlgorithm(id, adoptPtr(new WebCryptoRsaHashedKeyAlgorithmParams(modulusLengthBits, publicExponent, publicExponentSize, createHash(hash))));
8907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch}
9007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
91d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)bool WebCryptoKeyAlgorithm::isNull() const
92d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
93d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return m_private.isNull();
94d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
95d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
96d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)WebCryptoAlgorithmId WebCryptoKeyAlgorithm::id() const
97d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
98d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ASSERT(!isNull());
99d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return m_private->id;
100d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
101d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
102d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)WebCryptoKeyAlgorithmParamsType WebCryptoKeyAlgorithm::paramsType() const
103d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
104d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ASSERT(!isNull());
105d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (!m_private->params.get())
106d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return WebCryptoKeyAlgorithmParamsTypeNone;
107d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return m_private->params->type();
108d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
109d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
110d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)WebCryptoAesKeyAlgorithmParams* WebCryptoKeyAlgorithm::aesParams() const
111d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
112d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ASSERT(!isNull());
113d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (paramsType() == WebCryptoKeyAlgorithmParamsTypeAes)
114d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return static_cast<WebCryptoAesKeyAlgorithmParams*>(m_private->params.get());
115d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return 0;
116d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
117d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
118d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)WebCryptoHmacKeyAlgorithmParams* WebCryptoKeyAlgorithm::hmacParams() const
119d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
120d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ASSERT(!isNull());
121d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (paramsType() == WebCryptoKeyAlgorithmParamsTypeHmac)
122d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return static_cast<WebCryptoHmacKeyAlgorithmParams*>(m_private->params.get());
123d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return 0;
124d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
125d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
126d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)WebCryptoRsaHashedKeyAlgorithmParams* WebCryptoKeyAlgorithm::rsaHashedParams() const
127d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
128d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ASSERT(!isNull());
129d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (paramsType() == WebCryptoKeyAlgorithmParamsTypeRsaHashed)
130d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return static_cast<WebCryptoRsaHashedKeyAlgorithmParams*>(m_private->params.get());
131d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return 0;
132d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
133d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
134197021e6b966cfb06891637935ef33fff06433d1Ben Murdochvoid WebCryptoKeyAlgorithm::writeToDictionary(WebCryptoKeyAlgorithmDictionary* dict) const
135197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch{
136197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    ASSERT(!isNull());
137197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    dict->setString("name", WebCryptoAlgorithm::lookupAlgorithmInfo(id())->name);
138197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    m_private->params.get()->writeToDictionary(dict);
139197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch}
140197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
141d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)void WebCryptoKeyAlgorithm::assign(const WebCryptoKeyAlgorithm& other)
142d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
143d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    m_private = other.m_private;
144d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
145d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
146d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)void WebCryptoKeyAlgorithm::reset()
147d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
148d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    m_private.reset();
149d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
150d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
151d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)} // namespace blink
152