Add ICU message format support
[chromium-blink-merge.git] / chrome / utility / importer / nss_decryptor_win.h
blob8744ce6d0ed7c7d77febd13709433ee8577fa6bd
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 #ifndef CHROME_UTILITY_IMPORTER_NSS_DECRYPTOR_WIN_H_
6 #define CHROME_UTILITY_IMPORTER_NSS_DECRYPTOR_WIN_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/native_library.h"
13 #include "base/strings/string16.h"
15 // The following declarations of functions and types are from Firefox
16 // NSS library.
17 // source code:
18 // security/nss/lib/util/seccomon.h
19 // security/nss/lib/nss/nss.h
20 // The license block is:
22 /* ***** BEGIN LICENSE BLOCK *****
23 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
25 * The contents of this file are subject to the Mozilla Public License Version
26 * 1.1 (the "License"); you may not use this file except in compliance with
27 * the License. You may obtain a copy of the License at
28 * http://www.mozilla.org/MPL/
30 * Software distributed under the License is distributed on an "AS IS" basis,
31 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
32 * for the specific language governing rights and limitations under the
33 * License.
35 * The Original Code is the Netscape security libraries.
37 * The Initial Developer of the Original Code is
38 * Netscape Communications Corporation.
39 * Portions created by the Initial Developer are Copyright (C) 1994-2000
40 * the Initial Developer. All Rights Reserved.
42 * Contributor(s):
44 * Alternatively, the contents of this file may be used under the terms of
45 * either the GNU General Public License Version 2 or later (the "GPL"), or
46 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
47 * in which case the provisions of the GPL or the LGPL are applicable instead
48 * of those above. If you wish to allow use of your version of this file only
49 * under the terms of either the GPL or the LGPL, and not to allow others to
50 * use your version of this file under the terms of the MPL, indicate your
51 * decision by deleting the provisions above and replace them with the notice
52 * and other provisions required by the GPL or the LGPL. If you do not delete
53 * the provisions above, a recipient may use your version of this file under
54 * the terms of any one of the MPL, the GPL or the LGPL.
56 * ***** END LICENSE BLOCK ***** */
58 enum SECItemType {
59 siBuffer = 0,
60 siClearDataBuffer = 1,
61 siCipherDataBuffer = 2,
62 siDERCertBuffer = 3,
63 siEncodedCertBuffer = 4,
64 siDERNameBuffer = 5,
65 siEncodedNameBuffer = 6,
66 siAsciiNameString = 7,
67 siAsciiString = 8,
68 siDEROID = 9,
69 siUnsignedInteger = 10,
70 siUTCTime = 11,
71 siGeneralizedTime = 12
74 struct SECItem {
75 SECItemType type;
76 unsigned char *data;
77 unsigned int len;
80 enum SECStatus {
81 SECWouldBlock = -2,
82 SECFailure = -1,
83 SECSuccess = 0
86 typedef int PRBool;
87 #define PR_TRUE 1
88 #define PR_FALSE 0
90 typedef enum { PR_FAILURE = -1, PR_SUCCESS = 0 } PRStatus;
92 typedef struct PK11SlotInfoStr PK11SlotInfo;
94 typedef SECStatus (*NSSInitFunc)(const char *configdir);
95 typedef SECStatus (*NSSShutdownFunc)(void);
96 typedef PK11SlotInfo* (*PK11GetInternalKeySlotFunc)(void);
97 typedef void (*PK11FreeSlotFunc)(PK11SlotInfo *slot);
98 typedef SECStatus (*PK11CheckUserPasswordFunc)(PK11SlotInfo *slot, char *pw);
99 typedef SECStatus
100 (*PK11AuthenticateFunc)(PK11SlotInfo *slot, PRBool loadCerts, void *wincx);
101 typedef SECStatus
102 (*PK11SDRDecryptFunc)(SECItem *data, SECItem *result, void *cx);
103 typedef void (*SECITEMFreeItemFunc)(SECItem *item, PRBool free_it);
104 typedef void (*PLArenaFinishFunc)(void);
105 typedef PRStatus (*PRCleanupFunc)(void);
107 struct FirefoxRawPasswordInfo;
109 namespace autofill {
110 struct PasswordForm;
113 // A wrapper for Firefox NSS decrypt component.
114 class NSSDecryptor {
115 public:
116 NSSDecryptor();
117 ~NSSDecryptor();
119 // Loads NSS3 library and returns true if successful.
120 // |dll_path| indicates the location of NSS3 DLL files, and |db_path|
121 // is the location of the database file that stores the keys.
122 bool Init(const base::FilePath& dll_path, const base::FilePath& db_path);
124 // Frees the libraries.
125 void Free();
127 // Decrypts Firefox stored passwords. Before using this method,
128 // make sure Init() returns true.
129 base::string16 Decrypt(const std::string& crypt) const;
131 // Parses the Firefox password file content, decrypts the
132 // username/password and reads other related information.
133 // The result will be stored in |forms|.
134 void ParseSignons(const base::FilePath& signon_file,
135 std::vector<autofill::PasswordForm>* forms);
137 // Reads and parses the Firefox password sqlite db, decrypts the
138 // username/password and reads other related information.
139 // The result will be stored in |forms|.
140 bool ReadAndParseSignons(const base::FilePath& sqlite_file,
141 std::vector<autofill::PasswordForm>* forms);
143 // Reads and parses the Firefox password file logins.json, decrypts the
144 // username/password and reads other related information.
145 // The result will be stored in |forms|.
146 bool ReadAndParseLogins(const base::FilePath& json_file,
147 std::vector<autofill::PasswordForm>* forms);
149 private:
150 // Call NSS initialization funcs.
151 bool InitNSS(const base::FilePath& db_path,
152 base::NativeLibrary plds4_dll,
153 base::NativeLibrary nspr4_dll);
155 PK11SlotInfo* GetKeySlotForDB() const { return PK11_GetInternalKeySlot(); }
157 void FreeSlot(PK11SlotInfo* slot) const { PK11_FreeSlot(slot); }
159 // Turns unprocessed information extracted from Firefox's password file
160 // into PasswordForm.
161 bool CreatePasswordFormFromRawInfo(
162 const FirefoxRawPasswordInfo& raw_password_info,
163 autofill::PasswordForm* form);
165 // Methods in Firefox security components.
166 NSSInitFunc NSS_Init;
167 NSSShutdownFunc NSS_Shutdown;
168 PK11GetInternalKeySlotFunc PK11_GetInternalKeySlot;
169 PK11CheckUserPasswordFunc PK11_CheckUserPassword;
170 PK11FreeSlotFunc PK11_FreeSlot;
171 PK11AuthenticateFunc PK11_Authenticate;
172 PK11SDRDecryptFunc PK11SDR_Decrypt;
173 SECITEMFreeItemFunc SECITEM_FreeItem;
174 PLArenaFinishFunc PL_ArenaFinish;
175 PRCleanupFunc PR_Cleanup;
177 // Libraries necessary for decrypting the passwords.
178 static const wchar_t kNSS3Library[];
179 static const wchar_t kSoftokn3Library[];
180 static const wchar_t kPLDS4Library[];
181 static const wchar_t kNSPR4Library[];
183 // NSS3 module handles.
184 base::NativeLibrary nss3_dll_;
185 base::NativeLibrary softokn3_dll_;
187 // True if NSS_Init() has been called
188 bool is_nss_initialized_;
190 DISALLOW_COPY_AND_ASSIGN(NSSDecryptor);
193 #endif // CHROME_UTILITY_IMPORTER_NSS_DECRYPTOR_WIN_H_