Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
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 "nsIKeyModule.h" |
michael@0 | 6 | #include "nsStreamCipher.h" |
michael@0 | 7 | #include "nsStreamUtils.h" |
michael@0 | 8 | #include "base64.h" |
michael@0 | 9 | |
michael@0 | 10 | NS_IMPL_ISUPPORTS(nsStreamCipher, nsIStreamCipher) |
michael@0 | 11 | |
michael@0 | 12 | nsStreamCipher::nsStreamCipher() |
michael@0 | 13 | : mContext(nullptr) |
michael@0 | 14 | { |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | nsStreamCipher::~nsStreamCipher() |
michael@0 | 18 | { |
michael@0 | 19 | if (mContext) |
michael@0 | 20 | PK11_DestroyContext(mContext, true /* free sub-objects */); |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | nsresult |
michael@0 | 24 | nsStreamCipher::InitWithIV_(nsIKeyObject *aKey, SECItem* aIV) |
michael@0 | 25 | { |
michael@0 | 26 | NS_ENSURE_ARG_POINTER(aKey); |
michael@0 | 27 | |
michael@0 | 28 | // Make sure we have a SYM_KEY. |
michael@0 | 29 | int16_t keyType; |
michael@0 | 30 | nsresult rv = aKey->GetType(&keyType); |
michael@0 | 31 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 32 | if (keyType != nsIKeyObject::SYM_KEY) |
michael@0 | 33 | return NS_ERROR_INVALID_ARG; |
michael@0 | 34 | |
michael@0 | 35 | if (mContext) |
michael@0 | 36 | PK11_DestroyContext(mContext, true /* free sub-objects */); |
michael@0 | 37 | |
michael@0 | 38 | // Get the PK11SymKey out of the key object and create the PK11Context. |
michael@0 | 39 | void* keyObj; |
michael@0 | 40 | rv = aKey->GetKeyObj(&keyObj); |
michael@0 | 41 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 42 | |
michael@0 | 43 | PK11SymKey *symkey = reinterpret_cast<PK11SymKey*>(keyObj); |
michael@0 | 44 | if (!symkey) |
michael@0 | 45 | return NS_ERROR_FAILURE; |
michael@0 | 46 | |
michael@0 | 47 | CK_MECHANISM_TYPE cipherMech = PK11_GetMechanism(symkey); |
michael@0 | 48 | |
michael@0 | 49 | SECItem *param = nullptr; |
michael@0 | 50 | // aIV may be null |
michael@0 | 51 | param = PK11_ParamFromIV(cipherMech, aIV); |
michael@0 | 52 | if (!param) |
michael@0 | 53 | return NS_ERROR_FAILURE; |
michael@0 | 54 | |
michael@0 | 55 | mContext = PK11_CreateContextBySymKey(cipherMech, CKA_ENCRYPT, |
michael@0 | 56 | symkey, param); |
michael@0 | 57 | |
michael@0 | 58 | SECITEM_FreeItem(param, true); |
michael@0 | 59 | |
michael@0 | 60 | // Something went wrong if mContext doesn't exist. |
michael@0 | 61 | if (!mContext) |
michael@0 | 62 | return NS_ERROR_FAILURE; |
michael@0 | 63 | |
michael@0 | 64 | // Everything went ok. |
michael@0 | 65 | mValue.Truncate(); |
michael@0 | 66 | return NS_OK; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | ///////////////////////////////////////////////////////////////////////////// |
michael@0 | 70 | // nsIStreamCipher |
michael@0 | 71 | |
michael@0 | 72 | NS_IMETHODIMP nsStreamCipher::Init(nsIKeyObject *aKey) |
michael@0 | 73 | { |
michael@0 | 74 | return InitWithIV_(aKey, nullptr); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | NS_IMETHODIMP nsStreamCipher::InitWithIV(nsIKeyObject *aKey, |
michael@0 | 78 | const uint8_t *aIV, uint32_t aIVLen) |
michael@0 | 79 | { |
michael@0 | 80 | SECItem IV; |
michael@0 | 81 | IV.data = (unsigned char*)aIV; |
michael@0 | 82 | IV.len = aIVLen; |
michael@0 | 83 | return InitWithIV_(aKey, &IV); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | NS_IMETHODIMP nsStreamCipher::Update(const uint8_t *aData, uint32_t aLen) |
michael@0 | 87 | { |
michael@0 | 88 | if (!mContext) |
michael@0 | 89 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 90 | |
michael@0 | 91 | unsigned char* output = new unsigned char[aLen]; |
michael@0 | 92 | unsigned char* input = (unsigned char*)aData; |
michael@0 | 93 | |
michael@0 | 94 | int32_t setLen; |
michael@0 | 95 | |
michael@0 | 96 | #ifdef DEBUG |
michael@0 | 97 | SECStatus rv = |
michael@0 | 98 | #endif |
michael@0 | 99 | PK11_CipherOp(mContext, output, &setLen, aLen, input, aLen); |
michael@0 | 100 | NS_ASSERTION(rv == SECSuccess, "failed to encrypt"); |
michael@0 | 101 | NS_ASSERTION((uint32_t)setLen == aLen, "data length should not change"); |
michael@0 | 102 | |
michael@0 | 103 | mValue.Append((const char*)output, aLen); |
michael@0 | 104 | |
michael@0 | 105 | delete [] output; |
michael@0 | 106 | |
michael@0 | 107 | return NS_OK; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | NS_IMETHODIMP nsStreamCipher::UpdateFromStream(nsIInputStream *aStream, |
michael@0 | 111 | int32_t aLen) |
michael@0 | 112 | { |
michael@0 | 113 | if (!mContext) |
michael@0 | 114 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 115 | |
michael@0 | 116 | nsCString inputString; |
michael@0 | 117 | nsresult rv = NS_ConsumeStream(aStream, aLen, inputString); |
michael@0 | 118 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 119 | |
michael@0 | 120 | return UpdateFromString(inputString); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | NS_IMETHODIMP nsStreamCipher::UpdateFromString(const nsACString& aInput) |
michael@0 | 124 | { |
michael@0 | 125 | if (!mContext) |
michael@0 | 126 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 127 | |
michael@0 | 128 | const nsCString& flatInput = PromiseFlatCString(aInput); |
michael@0 | 129 | unsigned char* input = (unsigned char*)flatInput.get(); |
michael@0 | 130 | uint32_t len = aInput.Length(); |
michael@0 | 131 | |
michael@0 | 132 | unsigned char* output = new unsigned char[len]; |
michael@0 | 133 | |
michael@0 | 134 | int32_t setLen; |
michael@0 | 135 | |
michael@0 | 136 | #ifdef DEBUG |
michael@0 | 137 | SECStatus rv = |
michael@0 | 138 | #endif |
michael@0 | 139 | PK11_CipherOp(mContext, output, &setLen, len, input, len); |
michael@0 | 140 | NS_ASSERTION(rv == SECSuccess, "failed to encrypt"); |
michael@0 | 141 | NS_ASSERTION((uint32_t)setLen == len, "data length should not change"); |
michael@0 | 142 | |
michael@0 | 143 | mValue.Append((const char*)output, len); |
michael@0 | 144 | delete [] output; |
michael@0 | 145 | |
michael@0 | 146 | return NS_OK; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | NS_IMETHODIMP nsStreamCipher::Finish(bool aASCII, nsACString & _retval) |
michael@0 | 150 | { |
michael@0 | 151 | if (!mContext) |
michael@0 | 152 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 153 | |
michael@0 | 154 | if (aASCII) { |
michael@0 | 155 | char *asciiData = BTOA_DataToAscii((unsigned char*)(mValue.get()), |
michael@0 | 156 | mValue.Length()); |
michael@0 | 157 | _retval.Assign(asciiData); |
michael@0 | 158 | PORT_Free(asciiData); |
michael@0 | 159 | } else { |
michael@0 | 160 | _retval.Assign(mValue); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | return NS_OK; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | NS_IMETHODIMP nsStreamCipher::Discard(int32_t aLen) |
michael@0 | 167 | { |
michael@0 | 168 | if (!mContext) |
michael@0 | 169 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 170 | |
michael@0 | 171 | unsigned char* output = new unsigned char[aLen]; |
michael@0 | 172 | unsigned char* input = new unsigned char[aLen]; |
michael@0 | 173 | |
michael@0 | 174 | int32_t setLen; |
michael@0 | 175 | |
michael@0 | 176 | #ifdef DEBUG |
michael@0 | 177 | SECStatus rv = |
michael@0 | 178 | #endif |
michael@0 | 179 | PK11_CipherOp(mContext, output, &setLen, aLen, input, aLen); |
michael@0 | 180 | NS_ASSERTION(rv == SECSuccess, "failed to encrypt"); |
michael@0 | 181 | NS_ASSERTION(setLen == aLen, "data length should not change"); |
michael@0 | 182 | |
michael@0 | 183 | delete [] output; |
michael@0 | 184 | delete [] input; |
michael@0 | 185 | return NS_OK; |
michael@0 | 186 | } |