/src/mozilla-central/security/manager/ssl/nsKeyModule.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | |
5 | | #include "nsCOMPtr.h" |
6 | | #include "nsComponentManagerUtils.h" |
7 | | #include "nsKeyModule.h" |
8 | | #include "nsString.h" |
9 | | |
10 | | using namespace mozilla; |
11 | | using namespace mozilla::psm; |
12 | | |
13 | | NS_IMPL_ISUPPORTS(nsKeyObject, nsIKeyObject) |
14 | | |
15 | | nsKeyObject::nsKeyObject() |
16 | | : mSymKey(nullptr) |
17 | 0 | { |
18 | 0 | } |
19 | | |
20 | | ////////////////////////////////////////////////////////////////////////////// |
21 | | // nsIKeyObject |
22 | | |
23 | | NS_IMETHODIMP |
24 | | nsKeyObject::InitKey(int16_t aAlgorithm, PK11SymKey* aKey) |
25 | 0 | { |
26 | 0 | if (!aKey || aAlgorithm != nsIKeyObject::HMAC) { |
27 | 0 | return NS_ERROR_INVALID_ARG; |
28 | 0 | } |
29 | 0 | |
30 | 0 | mSymKey.reset(aKey); |
31 | 0 | return NS_OK; |
32 | 0 | } |
33 | | |
34 | | NS_IMETHODIMP |
35 | | nsKeyObject::GetKeyObj(PK11SymKey** _retval) |
36 | 0 | { |
37 | 0 | if (!_retval) { |
38 | 0 | return NS_ERROR_INVALID_ARG; |
39 | 0 | } |
40 | 0 | |
41 | 0 | *_retval = nullptr; |
42 | 0 |
|
43 | 0 | if (!mSymKey) { |
44 | 0 | return NS_ERROR_NOT_INITIALIZED; |
45 | 0 | } |
46 | 0 | |
47 | 0 | *_retval = mSymKey.get(); |
48 | 0 | return NS_OK; |
49 | 0 | } |
50 | | |
51 | | NS_IMETHODIMP |
52 | | nsKeyObject::GetType(int16_t *_retval) |
53 | 0 | { |
54 | 0 | if (!_retval) { |
55 | 0 | return NS_ERROR_INVALID_ARG; |
56 | 0 | } |
57 | 0 | *_retval = nsIKeyObject::SYM_KEY; |
58 | 0 | return NS_OK; |
59 | 0 | } |
60 | | |
61 | | ////////////////////////////////////////////////////////////////////////////// |
62 | | // nsIKeyObjectFactory |
63 | | |
64 | | NS_IMPL_ISUPPORTS(nsKeyObjectFactory, nsIKeyObjectFactory) |
65 | | |
66 | | NS_IMETHODIMP |
67 | | nsKeyObjectFactory::KeyFromString(int16_t aAlgorithm, const nsACString& aKey, |
68 | | nsIKeyObject** _retval) |
69 | 0 | { |
70 | 0 | if (!_retval || aAlgorithm != nsIKeyObject::HMAC) { |
71 | 0 | return NS_ERROR_INVALID_ARG; |
72 | 0 | } |
73 | 0 | |
74 | 0 | CK_MECHANISM_TYPE cipherMech = CKM_GENERIC_SECRET_KEY_GEN; |
75 | 0 | CK_ATTRIBUTE_TYPE cipherOperation = CKA_SIGN; |
76 | 0 |
|
77 | 0 | nsresult rv; |
78 | 0 | nsCOMPtr<nsIKeyObject> key( |
79 | 0 | do_CreateInstance(NS_KEYMODULEOBJECT_CONTRACTID, &rv)); |
80 | 0 | if (NS_FAILED(rv)) { |
81 | 0 | return rv; |
82 | 0 | } |
83 | 0 | |
84 | 0 | // Convert the raw string into a SECItem |
85 | 0 | const nsCString& flatKey = PromiseFlatCString(aKey); |
86 | 0 | SECItem keyItem; |
87 | 0 | keyItem.data = (unsigned char*)flatKey.get(); |
88 | 0 | keyItem.len = flatKey.Length(); |
89 | 0 |
|
90 | 0 | UniquePK11SlotInfo slot(PK11_GetBestSlot(cipherMech, nullptr)); |
91 | 0 | if (!slot) { |
92 | 0 | return NS_ERROR_FAILURE; |
93 | 0 | } |
94 | 0 | |
95 | 0 | UniquePK11SymKey symKey(PK11_ImportSymKey(slot.get(), cipherMech, |
96 | 0 | PK11_OriginUnwrap, cipherOperation, |
97 | 0 | &keyItem, nullptr)); |
98 | 0 | if (!symKey) { |
99 | 0 | return NS_ERROR_FAILURE; |
100 | 0 | } |
101 | 0 | |
102 | 0 | rv = key->InitKey(aAlgorithm, symKey.release()); |
103 | 0 | if (NS_FAILED(rv)) { |
104 | 0 | return rv; |
105 | 0 | } |
106 | 0 | |
107 | 0 | key.swap(*_retval); |
108 | 0 | return NS_OK; |
109 | 0 | } |