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