Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / child / webcrypto / nss / rsa_key_nss.h
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CONTENT_CHILD_WEBCRYPTO_NSS_RSA_KEY_NSS_H_
6 #define CONTENT_CHILD_WEBCRYPTO_NSS_RSA_KEY_NSS_H_
7
8 #include <pkcs11t.h>
9
10 #include "content/child/webcrypto/algorithm_implementation.h"
11
12 namespace content {
13
14 namespace webcrypto {
15
16 class PublicKeyNss;
17 class PrivateKeyNss;
18
19 // Base class for an RSA algorithm whose keys additionaly have a hash parameter
20 // bound to them. Provides functionality for generating, importing, and
21 // exporting keys.
22 class RsaHashedAlgorithm : public AlgorithmImplementation {
23  public:
24   // Constructs an RSA algorithm which will use the NSS flags |generate_flags|
25   // when generating keys. |all_public_key_usages| and |all_private_key_usages|
26   // are the set of WebCrypto key usages that are valid for created keys
27   // (public and private respectively).
28   //
29   // For instance if public keys support encryption and wrapping, and private
30   // keys support decryption and unwrapping callers should set:
31   //    all_public_key_usages = UsageEncrypt | UsageWrap
32   //    all_private_key_usages = UsageDecrypt | UsageUnwrap
33   // This information is used when importing or generating keys, to enforce
34   // that valid key usages are allowed.
35   RsaHashedAlgorithm(CK_FLAGS generate_flags,
36                      blink::WebCryptoKeyUsageMask all_public_key_usages,
37                      blink::WebCryptoKeyUsageMask all_private_key_usages)
38       : generate_flags_(generate_flags),
39         all_public_key_usages_(all_public_key_usages),
40         all_private_key_usages_(all_private_key_usages) {}
41
42   // For instance "RSA-OAEP-256".
43   virtual const char* GetJwkAlgorithm(
44       const blink::WebCryptoAlgorithmId hash) const = 0;
45
46   Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm,
47                      bool extractable,
48                      blink::WebCryptoKeyUsageMask usages,
49                      GenerateKeyResult* result) const override;
50
51   Status VerifyKeyUsagesBeforeImportKey(
52       blink::WebCryptoKeyFormat format,
53       blink::WebCryptoKeyUsageMask usages) const override;
54
55   Status ImportKeyPkcs8(const CryptoData& key_data,
56                         const blink::WebCryptoAlgorithm& algorithm,
57                         bool extractable,
58                         blink::WebCryptoKeyUsageMask usages,
59                         blink::WebCryptoKey* key) const override;
60
61   Status ImportKeySpki(const CryptoData& key_data,
62                        const blink::WebCryptoAlgorithm& algorithm,
63                        bool extractable,
64                        blink::WebCryptoKeyUsageMask usages,
65                        blink::WebCryptoKey* key) const override;
66
67   Status ExportKeyPkcs8(const blink::WebCryptoKey& key,
68                         std::vector<uint8_t>* buffer) const override;
69
70   Status ExportKeySpki(const blink::WebCryptoKey& key,
71                        std::vector<uint8_t>* buffer) const override;
72
73   Status ImportKeyJwk(const CryptoData& key_data,
74                       const blink::WebCryptoAlgorithm& algorithm,
75                       bool extractable,
76                       blink::WebCryptoKeyUsageMask usages,
77                       blink::WebCryptoKey* key) const override;
78
79   Status ExportKeyJwk(const blink::WebCryptoKey& key,
80                       std::vector<uint8_t>* buffer) const override;
81
82  private:
83   CK_FLAGS generate_flags_;
84   blink::WebCryptoKeyUsageMask all_public_key_usages_;
85   blink::WebCryptoKeyUsageMask all_private_key_usages_;
86 };
87
88 }  // namespace webcrypto
89
90 }  // namespace content
91
92 #endif  // CONTENT_CHILD_WEBCRYPTO_NSS_RSA_KEY_NSS_H_