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)#ifndef WebCryptoKeyAlgorithm_h
32d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#define WebCryptoKeyAlgorithm_h
33d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
34d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "WebCommon.h"
35d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "WebCryptoAlgorithm.h"
36d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "WebCryptoKeyAlgorithmParams.h"
37d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "WebPrivatePtr.h"
38d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
39d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#if INSIDE_BLINK
40d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "wtf/PassOwnPtr.h"
41d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#endif
42d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
43d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)namespace blink {
44d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
45d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)class WebCryptoKeyAlgorithmPrivate;
46d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
47d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)// WebCryptoKeyAlgorithm represents the algorithm used to generate a key.
48d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)//   * Immutable
49d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)//   * Threadsafe
50d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)//   * Copiable (cheaply)
51d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)class WebCryptoKeyAlgorithm {
52d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)public:
53d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    WebCryptoKeyAlgorithm() { }
54d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
55d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#if INSIDE_BLINK
56d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    BLINK_PLATFORM_EXPORT WebCryptoKeyAlgorithm(WebCryptoAlgorithmId, PassOwnPtr<WebCryptoKeyAlgorithmParams>);
57d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#endif
58d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
5907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    // FIXME: Delete this in favor of the create*() functions.
60d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    BLINK_PLATFORM_EXPORT static WebCryptoKeyAlgorithm adoptParamsAndCreate(WebCryptoAlgorithmId, WebCryptoKeyAlgorithmParams*);
61d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
6207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    BLINK_PLATFORM_EXPORT static WebCryptoKeyAlgorithm createAes(WebCryptoAlgorithmId, unsigned short keyLengthBits);
6307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    BLINK_PLATFORM_EXPORT static WebCryptoKeyAlgorithm createHmac(WebCryptoAlgorithmId hash, unsigned keyLengthBits);
6407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    BLINK_PLATFORM_EXPORT static WebCryptoKeyAlgorithm createRsaHashed(WebCryptoAlgorithmId, unsigned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExponentSize, WebCryptoAlgorithmId hash);
6507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
66d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ~WebCryptoKeyAlgorithm() { reset(); }
67d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
68d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    WebCryptoKeyAlgorithm(const WebCryptoKeyAlgorithm& other) { assign(other); }
69d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    WebCryptoKeyAlgorithm& operator=(const WebCryptoKeyAlgorithm& other)
70d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    {
71d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        assign(other);
72d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return *this;
73d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
74d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
75d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    BLINK_PLATFORM_EXPORT bool isNull() const;
76d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
77d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    BLINK_PLATFORM_EXPORT WebCryptoAlgorithmId id() const;
78d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
79d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    BLINK_PLATFORM_EXPORT WebCryptoKeyAlgorithmParamsType paramsType() const;
80d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
81d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Returns the type-specific parameters for this key. If the requested
82d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // parameters are not applicable (for instance an HMAC key does not have
83d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // any AES parameters) then returns 0.
84d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    BLINK_PLATFORM_EXPORT WebCryptoAesKeyAlgorithmParams* aesParams() const;
85d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    BLINK_PLATFORM_EXPORT WebCryptoHmacKeyAlgorithmParams* hmacParams() const;
86d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    BLINK_PLATFORM_EXPORT WebCryptoRsaHashedKeyAlgorithmParams* rsaHashedParams() const;
87d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
88197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // Write the algorithm parameters to a dictionary.
89197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    BLINK_PLATFORM_EXPORT void writeToDictionary(WebCryptoKeyAlgorithmDictionary*) const;
90197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
91d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)private:
92d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    BLINK_PLATFORM_EXPORT void assign(const WebCryptoKeyAlgorithm& other);
93d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    BLINK_PLATFORM_EXPORT void reset();
94d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
95d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    WebPrivatePtr<WebCryptoKeyAlgorithmPrivate> m_private;
96d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)};
97d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
98d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)} // namespace blink
99d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
100d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#endif
101