1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_CryptoKey_h
8 #define mozilla_dom_CryptoKey_h
11 #include "ErrorList.h"
12 #include "ScopedNSSTypes.h"
13 #include "js/RootingAPI.h"
15 #include "mozilla/AlreadyAddRefed.h"
16 #include "mozilla/Assertions.h"
17 #include "mozilla/RefPtr.h"
18 #include "mozilla/dom/BindingDeclarations.h"
19 #include "mozilla/dom/CryptoBuffer.h"
20 #include "mozilla/dom/KeyAlgorithmProxy.h"
21 #include "nsCycleCollectionParticipant.h"
22 #include "nsIGlobalObject.h"
23 #include "nsISupports.h"
24 #include "nsStringFwd.h"
25 #include "nsTArrayForwardDeclare.h"
26 #include "nsWrapperCache.h"
28 #define CRYPTOKEY_SC_VERSION 0x00000001
31 class nsIGlobalObject
;
33 struct JSStructuredCloneReader
;
34 struct JSStructuredCloneWriter
;
44 The internal structure of keys is dictated by the need for cloning.
45 We store everything besides the key data itself in a 32-bit bitmask,
46 with the following structure (byte-aligned for simplicity, in order
47 from least to most significant):
56 In the order of a hex value for a uint32_t
59 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
60 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 |~~~~~~~~~~~~~~~| Usage | Type |~~~~~~~~~~~~~|E|
62 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 Thus, internally, a key has the following fields:
65 * uint32_t - flags for extractable, usage, type
66 * KeyAlgorithm - the algorithm (which must serialize/deserialize itself)
67 * The actual keys (which the CryptoKey must serialize)
73 class CryptoKey final
: public nsISupports
, public nsWrapperCache
{
75 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
76 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(CryptoKey
)
78 static const uint32_t CLEAR_EXTRACTABLE
= 0xFFFFFFE;
79 static const uint32_t EXTRACTABLE
= 0x00000001;
81 static const uint32_t CLEAR_TYPE
= 0xFFFF00FF;
82 static const uint32_t TYPE_MASK
= 0x0000FF00;
90 static const uint32_t CLEAR_USAGES
= 0xFF00FFFF;
91 static const uint32_t USAGES_MASK
= 0x00FF0000;
97 DERIVEKEY
= 0x00100000,
98 DERIVEBITS
= 0x00200000,
100 UNWRAPKEY
= 0x00800000
103 explicit CryptoKey(nsIGlobalObject
* aWindow
);
105 nsIGlobalObject
* GetParentObject() const { return mGlobal
; }
107 virtual JSObject
* WrapObject(JSContext
* aCx
,
108 JS::Handle
<JSObject
*> aGivenProto
) override
;
111 void GetType(nsString
& aRetVal
) const;
112 bool Extractable() const;
113 void GetAlgorithm(JSContext
* cx
, JS::MutableHandle
<JSObject
*> aRetVal
,
114 ErrorResult
& aRv
) const;
115 void GetUsages(nsTArray
<nsString
>& aRetVal
) const;
117 // The below methods are not exposed to JS, but C++ can use
118 // them to manipulate the object
120 KeyAlgorithmProxy
& Algorithm();
121 const KeyAlgorithmProxy
& Algorithm() const;
122 KeyType
GetKeyType() const;
123 nsresult
SetType(const nsString
& aType
);
124 void SetType(KeyType aType
);
125 void SetExtractable(bool aExtractable
);
126 nsresult
AddPublicKeyData(SECKEYPublicKey
* aPublicKey
);
128 nsresult
AddUsage(const nsString
& aUsage
);
129 nsresult
AddAllowedUsage(const nsString
& aUsage
, const nsString
& aAlgorithm
);
130 nsresult
AddAllowedUsageIntersecting(const nsString
& aUsage
,
131 const nsString
& aAlgorithm
,
132 uint32_t aUsageMask
= USAGES_MASK
);
133 void AddUsage(KeyUsage aUsage
);
135 bool HasUsage(KeyUsage aUsage
);
136 bool HasUsageOtherThan(uint32_t aUsages
);
137 static bool IsRecognizedUsage(const nsString
& aUsage
);
138 static bool AllUsagesRecognized(const Sequence
<nsString
>& aUsages
);
139 static uint32_t GetAllowedUsagesForAlgorithm(const nsString
& aAlgorithm
);
141 nsresult
SetSymKey(const CryptoBuffer
& aSymKey
);
142 nsresult
SetPrivateKey(SECKEYPrivateKey
* aPrivateKey
);
143 nsresult
SetPublicKey(SECKEYPublicKey
* aPublicKey
);
145 // Accessors for the keys themselves
146 const CryptoBuffer
& GetSymKey() const;
147 UniqueSECKEYPrivateKey
GetPrivateKey() const;
148 UniqueSECKEYPublicKey
GetPublicKey() const;
150 // Serialization and deserialization convenience methods
152 // 1. The inputs aKeyData are non-const only because the NSS import
153 // functions lack the const modifier. They should not be modified.
154 // 2. All of the NSS key objects returned need to be freed by the caller.
155 static UniqueSECKEYPrivateKey
PrivateKeyFromPkcs8(CryptoBuffer
& aKeyData
);
156 static nsresult
PrivateKeyToPkcs8(SECKEYPrivateKey
* aPrivKey
,
157 CryptoBuffer
& aRetVal
);
159 static UniqueSECKEYPublicKey
PublicKeyFromSpki(CryptoBuffer
& aKeyData
);
160 static nsresult
PublicKeyToSpki(SECKEYPublicKey
* aPubKey
,
161 CryptoBuffer
& aRetVal
);
163 static UniqueSECKEYPrivateKey
PrivateKeyFromJwk(const JsonWebKey
& aJwk
);
164 static nsresult
PrivateKeyToJwk(SECKEYPrivateKey
* aPrivKey
,
165 JsonWebKey
& aRetVal
);
167 static UniqueSECKEYPublicKey
PublicKeyFromJwk(const JsonWebKey
& aKeyData
);
168 static nsresult
PublicKeyToJwk(SECKEYPublicKey
* aPubKey
, JsonWebKey
& aRetVal
);
170 static UniqueSECKEYPublicKey
PublicECKeyFromRaw(CryptoBuffer
& aKeyData
,
171 const nsString
& aNamedCurve
);
172 static nsresult
PublicECKeyToRaw(SECKEYPublicKey
* aPubKey
,
173 CryptoBuffer
& aRetVal
);
175 static UniqueSECKEYPublicKey
PublicOKPKeyFromRaw(CryptoBuffer
& aKeyData
,
176 const nsString
& aNamedCurve
);
178 static bool PublicKeyValid(SECKEYPublicKey
* aPubKey
);
180 // Structured clone methods use these to clone keys
181 bool WriteStructuredClone(JSContext
* aCx
,
182 JSStructuredCloneWriter
* aWriter
) const;
183 static already_AddRefed
<CryptoKey
> ReadStructuredClone(
184 JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
185 JSStructuredCloneReader
* aReader
);
188 ~CryptoKey() = default;
190 RefPtr
<nsIGlobalObject
> mGlobal
;
191 uint32_t mAttributes
; // see above
192 KeyAlgorithmProxy mAlgorithm
;
194 // Only one key handle should be set, according to the KeyType
195 CryptoBuffer mSymKey
;
196 UniqueSECKEYPrivateKey mPrivateKey
;
197 UniqueSECKEYPublicKey mPublicKey
;
201 } // namespace mozilla
203 #endif // mozilla_dom_CryptoKey_h