Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / utility / importer / nss_decryptor_system_nss.cc
blobc6b3cbc2efa26298c37ac1d10afd98a45a8330cf
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"
7 #include <pk11pub.h>
8 #include <pk11sdr.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() {
18 if (db_slot_) {
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 =
32 base::StringPrintf(
33 "configDir='%s' tokenDescription='Firefox NSS database' "
34 "flags=readOnly",
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
56 * License.
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.
65 * Contributor(s):
66 * thayes@netscape.com
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
85 * mechanism
87 struct SDRResult
89 SECItem keyid;
90 SECAlgorithmID alg;
91 SECItem data;
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) },
101 { 0 }
104 static SECStatus
105 unpadBlock(SECItem *data, int blockSize, SECItem *result)
107 SECStatus rv = SECSuccess;
108 int padLength;
109 int i;
111 result->data = 0;
112 result->len = 0;
114 /* Remove the padding from the end if the input data */
115 if (data->len == 0 || data->len % blockSize != 0) {
116 rv = SECFailure;
117 goto loser;
120 padLength = data->data[data->len-1];
121 if (padLength > blockSize) { rv = SECFailure; goto loser; }
123 /* verify padding */
124 for (i=data->len - padLength; static_cast<uint32>(i) < data->len; i++) {
125 if (data->data[i] != padLength) {
126 rv = SECFailure;
127 goto loser;
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);
137 if (padLength < 2) {
138 return SECWouldBlock;
141 loser:
142 return rv;
145 /* decrypt a block */
146 static SECStatus
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;
153 SECStatus rv;
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,
167 in->data, in->len);
168 if (rv != SECSuccess) goto loser;
170 PK11_Finalize(ctx);
172 /* Remove the padding */
173 rv = unpadBlock(&paddedResult, PK11_GetBlockSize(type, 0), result);
174 if (rv) goto loser;
176 loser:
177 if (ctx) PK11_DestroyContext(ctx, PR_TRUE);
178 return rv;
181 SECStatus NSSDecryptor::PK11SDR_DecryptWithSlot(
182 PK11SlotInfo* slot, SECItem* data, SECItem* result, void* cx) const {
183 SECStatus rv = SECSuccess;
184 PK11SymKey *key = 0;
185 CK_MECHANISM_TYPE type;
186 SDRResult sdrResult;
187 SECItem *params = 0;
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) */
204 type = CKM_DES3_CBC;
205 key = PK11_FindFixedKey(slot, type, &sdrResult.keyid, cx);
206 if (!key) {
207 rv = SECFailure;
208 } else {
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
217 * this result.
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)
235 break;
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);
245 } else {
246 possibleResult = *result;
251 /* free the list */
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;
262 rv = SECSuccess;
265 loser:
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);
271 return rv;