security/manager/ssl/src/nsKeyModule.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "nsComponentManagerUtils.h"
michael@0 6 #include "nsCOMPtr.h"
michael@0 7 #include "nsKeyModule.h"
michael@0 8 #include "nsString.h"
michael@0 9 #include "ScopedNSSTypes.h"
michael@0 10
michael@0 11 using namespace mozilla;
michael@0 12 using namespace mozilla::psm;
michael@0 13
michael@0 14 NS_IMPL_ISUPPORTS(nsKeyObject, nsIKeyObject)
michael@0 15
michael@0 16 nsKeyObject::nsKeyObject()
michael@0 17 : mKeyType(0), mSymKey(nullptr), mPrivateKey(nullptr),
michael@0 18 mPublicKey(nullptr)
michael@0 19 {
michael@0 20 }
michael@0 21
michael@0 22 nsKeyObject::~nsKeyObject()
michael@0 23 {
michael@0 24 CleanUp();
michael@0 25 }
michael@0 26
michael@0 27 void
michael@0 28 nsKeyObject::CleanUp()
michael@0 29 {
michael@0 30 switch (mKeyType) {
michael@0 31 case nsIKeyObject::SYM_KEY:
michael@0 32 PK11_FreeSymKey(mSymKey);
michael@0 33 break;
michael@0 34
michael@0 35 case nsIKeyObject::PRIVATE_KEY:
michael@0 36 PK11_DeleteTokenPrivateKey(mPrivateKey, true /* force */);
michael@0 37 break;
michael@0 38
michael@0 39 case nsIKeyObject::PUBLIC_KEY:
michael@0 40 PK11_DeleteTokenPublicKey(mPublicKey);
michael@0 41 break;
michael@0 42
michael@0 43 default:
michael@0 44 // probably not initialized, do nothing
michael@0 45 break;
michael@0 46 }
michael@0 47 mKeyType = 0;
michael@0 48 }
michael@0 49
michael@0 50 //////////////////////////////////////////////////////////////////////////////
michael@0 51 // nsIKeyObject
michael@0 52
michael@0 53 /* [noscript] void initKey (in short aKeyType, in voidPtr aKey); */
michael@0 54 NS_IMETHODIMP
michael@0 55 nsKeyObject::InitKey(int16_t aAlgorithm, void * aKey)
michael@0 56 {
michael@0 57 // Clear previous key data if it exists
michael@0 58 CleanUp();
michael@0 59
michael@0 60 switch (aAlgorithm) {
michael@0 61 case nsIKeyObject::RC4:
michael@0 62 case nsIKeyObject::HMAC:
michael@0 63 mSymKey = reinterpret_cast<PK11SymKey*>(aKey);
michael@0 64
michael@0 65 if (!mSymKey) {
michael@0 66 NS_ERROR("no symkey");
michael@0 67 break;
michael@0 68 }
michael@0 69 mKeyType = nsIKeyObject::SYM_KEY;
michael@0 70 break;
michael@0 71
michael@0 72 case nsIKeyObject::AES_CBC:
michael@0 73 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 74
michael@0 75 default:
michael@0 76 return NS_ERROR_INVALID_ARG;
michael@0 77 }
michael@0 78
michael@0 79 // One of these should have been created
michael@0 80 if (!mSymKey && !mPrivateKey && !mPublicKey)
michael@0 81 return NS_ERROR_FAILURE;
michael@0 82
michael@0 83 return NS_OK;
michael@0 84 }
michael@0 85
michael@0 86 /* [noscript] voidPtr getKeyObj (); */
michael@0 87 NS_IMETHODIMP
michael@0 88 nsKeyObject::GetKeyObj(void * *_retval)
michael@0 89 {
michael@0 90 if (mKeyType == 0)
michael@0 91 return NS_ERROR_NOT_INITIALIZED;
michael@0 92
michael@0 93 switch (mKeyType) {
michael@0 94 case nsIKeyObject::SYM_KEY:
michael@0 95 *_retval = (void*)mSymKey;
michael@0 96 break;
michael@0 97
michael@0 98 case nsIKeyObject::PRIVATE_KEY:
michael@0 99 *_retval = (void*)mPublicKey;
michael@0 100 break;
michael@0 101
michael@0 102 case nsIKeyObject::PUBLIC_KEY:
michael@0 103 *_retval = (void*)mPrivateKey;
michael@0 104 break;
michael@0 105
michael@0 106 default:
michael@0 107 // unknown key type? How did that happen?
michael@0 108 return NS_ERROR_FAILURE;
michael@0 109 }
michael@0 110 return NS_OK;
michael@0 111 }
michael@0 112
michael@0 113 /* short getType (); */
michael@0 114 NS_IMETHODIMP
michael@0 115 nsKeyObject::GetType(int16_t *_retval)
michael@0 116 {
michael@0 117 if (mKeyType == 0)
michael@0 118 return NS_ERROR_NOT_INITIALIZED;
michael@0 119
michael@0 120 *_retval = mKeyType;
michael@0 121 return NS_OK;
michael@0 122 }
michael@0 123
michael@0 124 //////////////////////////////////////////////////////////////////////////////
michael@0 125 // nsIKeyObjectFactory
michael@0 126
michael@0 127 NS_IMPL_ISUPPORTS(nsKeyObjectFactory, nsIKeyObjectFactory)
michael@0 128
michael@0 129 nsKeyObjectFactory::nsKeyObjectFactory()
michael@0 130 {
michael@0 131 }
michael@0 132
michael@0 133 /* nsIKeyObject lookupKeyByName (in ACString aName); */
michael@0 134 NS_IMETHODIMP
michael@0 135 nsKeyObjectFactory::LookupKeyByName(const nsACString & aName,
michael@0 136 nsIKeyObject **_retval)
michael@0 137 {
michael@0 138 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 139 }
michael@0 140
michael@0 141 NS_IMETHODIMP
michael@0 142 nsKeyObjectFactory::UnwrapKey(int16_t aAlgorithm, const uint8_t *aWrappedKey,
michael@0 143 uint32_t aWrappedKeyLen, nsIKeyObject **_retval)
michael@0 144 {
michael@0 145 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 146 }
michael@0 147
michael@0 148 NS_IMETHODIMP
michael@0 149 nsKeyObjectFactory::KeyFromString(int16_t aAlgorithm, const nsACString & aKey,
michael@0 150 nsIKeyObject **_retval)
michael@0 151 {
michael@0 152 CK_MECHANISM_TYPE cipherMech;
michael@0 153 CK_ATTRIBUTE_TYPE cipherOperation;
michael@0 154 switch (aAlgorithm)
michael@0 155 {
michael@0 156 case nsIKeyObject::HMAC:
michael@0 157 cipherMech = CKM_GENERIC_SECRET_KEY_GEN;
michael@0 158 cipherOperation = CKA_SIGN;
michael@0 159 break;
michael@0 160
michael@0 161 case nsIKeyObject::RC4:
michael@0 162 cipherMech = CKM_RC4;
michael@0 163 cipherOperation = CKA_ENCRYPT;
michael@0 164 break;
michael@0 165
michael@0 166 default:
michael@0 167 return NS_ERROR_INVALID_ARG;
michael@0 168 }
michael@0 169
michael@0 170 nsresult rv;
michael@0 171 nsCOMPtr<nsIKeyObject> key =
michael@0 172 do_CreateInstance(NS_KEYMODULEOBJECT_CONTRACTID, &rv);
michael@0 173 NS_ENSURE_SUCCESS(rv, rv);
michael@0 174
michael@0 175 // Convert the raw string into a SECItem
michael@0 176 const nsCString& flatKey = PromiseFlatCString(aKey);
michael@0 177 SECItem keyItem;
michael@0 178 keyItem.data = (unsigned char*)flatKey.get();
michael@0 179 keyItem.len = flatKey.Length();
michael@0 180
michael@0 181 ScopedPK11SlotInfo slot(PK11_GetBestSlot(cipherMech, nullptr));
michael@0 182 if (!slot) {
michael@0 183 NS_ERROR("no slot");
michael@0 184 return NS_ERROR_FAILURE;
michael@0 185 }
michael@0 186
michael@0 187 PK11SymKey* symKey = PK11_ImportSymKey(slot, cipherMech, PK11_OriginUnwrap,
michael@0 188 cipherOperation, &keyItem, nullptr);
michael@0 189 if (!symKey) {
michael@0 190 return NS_ERROR_FAILURE;
michael@0 191 }
michael@0 192
michael@0 193 rv = key->InitKey(aAlgorithm, (void*)symKey);
michael@0 194 NS_ENSURE_SUCCESS(rv, rv);
michael@0 195
michael@0 196 key.swap(*_retval);
michael@0 197 return NS_OK;
michael@0 198 }

mercurial