1 // Copyright (c) 2011 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.
5 #include "chrome/utility/importer/nss_decryptor_system_nss.h"
10 #include "base/basictypes.h"
11 #include "base/files/file_path.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "crypto/nss_util.h"
16 NSSDecryptor::NSSDecryptor() : is_nss_initialized_(false), db_slot_(NULL
) {}
17 NSSDecryptor::~NSSDecryptor() {
19 // Deliberately leave the user db open, just in case we need to open more
20 // than one, because there's an NSS bug with reopening user dbs.
21 // https://bugzilla.mozilla.org/show_bug.cgi?id=506140
22 // SECMOD_CloseUserDB(db_slot_);
23 PK11_FreeSlot(db_slot_
);
27 bool NSSDecryptor::Init(const base::FilePath
& dll_path
,
28 const base::FilePath
& db_path
) {
29 crypto::EnsureNSSInit();
30 is_nss_initialized_
= true;
31 const std::string modspec
=
33 "configDir='%s' tokenDescription='Firefox NSS database' "
35 db_path
.value().c_str());
36 db_slot_
= SECMOD_OpenUserDB(modspec
.c_str());
37 return db_slot_
!= NULL
;
40 // This method is based on some NSS code in
41 // security/nss/lib/pk11wrap/pk11sdr.c, CVS revision 1.22
42 // This code is copied because the implementation assumes the use of the
43 // internal key slot for decryption, but we need to use another slot.
44 // The license block is:
45 /* ***** BEGIN LICENSE BLOCK *****
46 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
48 * The contents of this file are subject to the Mozilla Public License Version
49 * 1.1 (the "License"); you may not use this file except in compliance with
50 * the License. You may obtain a copy of the License at
51 * http://www.mozilla.org/MPL/
53 * Software distributed under the License is distributed on an "AS IS" basis,
54 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
55 * for the specific language governing rights and limitations under the
58 * The Original Code is the Netscape security libraries.
60 * The Initial Developer of the Original Code is
61 * Netscape Communications Corporation.
62 * Portions created by the Initial Developer are Copyright (C) 1994-2000
63 * the Initial Developer. All Rights Reserved.
68 * Alternatively, the contents of this file may be used under the terms of
69 * either the GNU General Public License Version 2 or later (the "GPL"), or
70 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
71 * in which case the provisions of the GPL or the LGPL are applicable instead
72 * of those above. If you wish to allow use of your version of this file only
73 * under the terms of either the GPL or the LGPL, and not to allow others to
74 * use your version of this file under the terms of the MPL, indicate your
75 * decision by deleting the provisions above and replace them with the notice
76 * and other provisions required by the GPL or the LGPL. If you do not delete
77 * the provisions above, a recipient may use your version of this file under
78 * the terms of any one of the MPL, the GPL or the LGPL.
80 * ***** END LICENSE BLOCK ***** */
83 * Data structure and template for encoding the result of an SDR operation
84 * This is temporary. It should include the algorithm ID of the encryption
93 typedef struct SDRResult SDRResult
;
95 static SEC_ASN1Template g_template
[] = {
96 { SEC_ASN1_SEQUENCE
, 0, NULL
, sizeof (SDRResult
) },
97 { SEC_ASN1_OCTET_STRING
, offsetof(SDRResult
, keyid
) },
98 { SEC_ASN1_INLINE
| SEC_ASN1_XTRN
, offsetof(SDRResult
, alg
),
99 SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate
) },
100 { SEC_ASN1_OCTET_STRING
, offsetof(SDRResult
, data
) },
105 unpadBlock(SECItem
*data
, int blockSize
, SECItem
*result
)
107 SECStatus rv
= SECSuccess
;
114 /* Remove the padding from the end if the input data */
115 if (data
->len
== 0 || data
->len
% blockSize
!= 0) {
120 padLength
= data
->data
[data
->len
-1];
121 if (padLength
> blockSize
) { rv
= SECFailure
; goto loser
; }
124 for (i
=data
->len
- padLength
; static_cast<uint32
>(i
) < data
->len
; i
++) {
125 if (data
->data
[i
] != padLength
) {
131 result
->len
= data
->len
- padLength
;
132 result
->data
= (unsigned char *)PORT_Alloc(result
->len
);
133 if (!result
->data
) { rv
= SECFailure
; goto loser
; }
135 PORT_Memcpy(result
->data
, data
->data
, result
->len
);
138 return SECWouldBlock
;
145 /* decrypt a block */
147 pk11Decrypt(PK11SlotInfo
*slot
, PLArenaPool
*arena
,
148 CK_MECHANISM_TYPE type
, PK11SymKey
*key
,
149 SECItem
*params
, SECItem
*in
, SECItem
*result
)
151 PK11Context
*ctx
= 0;
152 SECItem paddedResult
;
155 paddedResult
.len
= 0;
156 paddedResult
.data
= 0;
158 ctx
= PK11_CreateContextBySymKey(type
, CKA_DECRYPT
, key
, params
);
159 if (!ctx
) { rv
= SECFailure
; goto loser
; }
161 paddedResult
.len
= in
->len
;
162 paddedResult
.data
= static_cast<unsigned char*>(
163 PORT_ArenaAlloc(arena
, paddedResult
.len
));
165 rv
= PK11_CipherOp(ctx
, paddedResult
.data
,
166 (int*)&paddedResult
.len
, paddedResult
.len
,
168 if (rv
!= SECSuccess
) goto loser
;
172 /* Remove the padding */
173 rv
= unpadBlock(&paddedResult
, PK11_GetBlockSize(type
, 0), result
);
177 if (ctx
) PK11_DestroyContext(ctx
, PR_TRUE
);
181 SECStatus
NSSDecryptor::PK11SDR_DecryptWithSlot(
182 PK11SlotInfo
* slot
, SECItem
* data
, SECItem
* result
, void* cx
) const {
183 SECStatus rv
= SECSuccess
;
185 CK_MECHANISM_TYPE type
;
188 SECItem possibleResult
= { siBuffer
, NULL
, 0 };
189 PLArenaPool
*arena
= 0;
191 arena
= PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE
);
192 if (!arena
) { rv
= SECFailure
; goto loser
; }
194 /* Decode the incoming data */
195 memset(&sdrResult
, 0, sizeof sdrResult
);
196 rv
= SEC_QuickDERDecodeItem(arena
, &sdrResult
, g_template
, data
);
197 if (rv
!= SECSuccess
) goto loser
; /* Invalid format */
199 /* Get the parameter values from the data */
200 params
= PK11_ParamFromAlgid(&sdrResult
.alg
);
201 if (!params
) { rv
= SECFailure
; goto loser
; }
203 /* Use triple-DES (Should look up the algorithm) */
205 key
= PK11_FindFixedKey(slot
, type
, &sdrResult
.keyid
, cx
);
209 rv
= pk11Decrypt(slot
, arena
, type
, key
, params
,
210 &sdrResult
.data
, result
);
214 * if the pad value was too small (1 or 2), then it's statistically
215 * 'likely' that (1 in 256) that we may not have the correct key.
216 * Check the other keys for a better match. If we find none, use
219 if (rv
== SECWouldBlock
)
220 possibleResult
= *result
;
223 * handle the case where your key indicies may have been broken
225 if (rv
!= SECSuccess
) {
226 PK11SymKey
*keyList
= PK11_ListFixedKeysInSlot(slot
, NULL
, cx
);
227 PK11SymKey
*testKey
= NULL
;
228 PK11SymKey
*nextKey
= NULL
;
230 for (testKey
= keyList
; testKey
;
231 testKey
= PK11_GetNextSymKey(testKey
)) {
232 rv
= pk11Decrypt(slot
, arena
, type
, testKey
, params
,
233 &sdrResult
.data
, result
);
234 if (rv
== SECSuccess
)
237 /* found a close match. If it's our first remember it */
238 if (rv
== SECWouldBlock
) {
239 if (possibleResult
.data
) {
240 /* this is unlikely but possible. If we hit this condition,
241 * we have no way of knowing which possibility to prefer.
242 * in this case we just match the key the application
243 * thought was the right one */
244 SECITEM_ZfreeItem(result
, PR_FALSE
);
246 possibleResult
= *result
;
252 for (testKey
= keyList
; testKey
; testKey
= nextKey
) {
253 nextKey
= PK11_GetNextSymKey(testKey
);
254 PK11_FreeSymKey(testKey
);
258 /* we didn't find a better key, use the one with a small pad value */
259 if ((rv
!= SECSuccess
) && (possibleResult
.data
)) {
260 *result
= possibleResult
;
261 possibleResult
.data
= NULL
;
266 if (arena
) PORT_FreeArena(arena
, PR_TRUE
);
267 if (key
) PK11_FreeSymKey(key
);
268 if (params
) SECITEM_ZfreeItem(params
, PR_TRUE
);
269 if (possibleResult
.data
) SECITEM_ZfreeItem(&possibleResult
, PR_FALSE
);