1 // Copyright (c) 2009 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. 4 5 #ifndef CHROME_BROWSER_IMPORTER_NSS_DECRYPTOR_WIN_H_ 6 #define CHROME_BROWSER_IMPORTER_NSS_DECRYPTOR_WIN_H_ 7 #pragma once 8 9 #include <string> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/native_library.h" 14 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: 21 22 /* ***** BEGIN LICENSE BLOCK ***** 23 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 24 * 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/ 29 * 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. 34 * 35 * The Original Code is the Netscape security libraries. 36 * 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. 41 * 42 * Contributor(s): 43 * 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. 55 * 56 * ***** END LICENSE BLOCK ***** */ 57 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 72 }; 73 74 struct SECItem { 75 SECItemType type; 76 unsigned char *data; 77 unsigned int len; 78 }; 79 80 enum SECStatus { 81 SECWouldBlock = -2, 82 SECFailure = -1, 83 SECSuccess = 0 84 }; 85 86 typedef int PRBool; 87 #define PR_TRUE 1 88 #define PR_FALSE 0 89 90 typedef enum { PR_FAILURE = -1, PR_SUCCESS = 0 } PRStatus; 91 92 typedef struct PK11SlotInfoStr PK11SlotInfo; 93 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); 106 107 namespace webkit_glue { 108 struct PasswordForm; 109 } 110 111 // A wrapper for Firefox NSS decrypt component. 112 class NSSDecryptor { 113 public: 114 NSSDecryptor(); 115 ~NSSDecryptor(); 116 117 // Loads NSS3 library and returns true if successful. 118 // |dll_path| indicates the location of NSS3 DLL files, and |db_path| 119 // is the location of the database file that stores the keys. 120 bool Init(const FilePath& dll_path, const FilePath& db_path); 121 122 // Frees the libraries. 123 void Free(); 124 125 // Decrypts Firefox stored passwords. Before using this method, 126 // make sure Init() returns true. 127 std::wstring Decrypt(const std::string& crypt) const; 128 129 // Parses the Firefox password file content, decrypts the 130 // username/password and reads other related information. 131 // The result will be stored in |forms|. 132 void ParseSignons(const std::string& content, 133 std::vector<webkit_glue::PasswordForm>* forms); 134 135 // Reads and parses the Firefox password sqlite db, decrypts the 136 // username/password and reads other related information. 137 // The result will be stored in |forms|. 138 bool ReadAndParseSignons(const FilePath& sqlite_file, 139 std::vector<webkit_glue::PasswordForm>* forms); 140 141 private: 142 // Call NSS initialization funcs. 143 bool InitNSS(const FilePath& db_path, 144 base::NativeLibrary plds4_dll, 145 base::NativeLibrary nspr4_dll); 146 147 PK11SlotInfo* GetKeySlotForDB() const { return PK11_GetInternalKeySlot(); } 148 void FreeSlot(PK11SlotInfo* slot) const { PK11_FreeSlot(slot); } 149 150 // Methods in Firefox security components. 151 NSSInitFunc NSS_Init; 152 NSSShutdownFunc NSS_Shutdown; 153 PK11GetInternalKeySlotFunc PK11_GetInternalKeySlot; 154 PK11CheckUserPasswordFunc PK11_CheckUserPassword; 155 PK11FreeSlotFunc PK11_FreeSlot; 156 PK11AuthenticateFunc PK11_Authenticate; 157 PK11SDRDecryptFunc PK11SDR_Decrypt; 158 SECITEMFreeItemFunc SECITEM_FreeItem; 159 PLArenaFinishFunc PL_ArenaFinish; 160 PRCleanupFunc PR_Cleanup; 161 162 // Libraries necessary for decrypting the passwords. 163 static const wchar_t kNSS3Library[]; 164 static const wchar_t kSoftokn3Library[]; 165 static const wchar_t kPLDS4Library[]; 166 static const wchar_t kNSPR4Library[]; 167 168 // NSS3 module handles. 169 base::NativeLibrary nss3_dll_; 170 base::NativeLibrary softokn3_dll_; 171 172 // True if NSS_Init() has been called 173 bool is_nss_initialized_; 174 175 DISALLOW_COPY_AND_ASSIGN(NSSDecryptor); 176 }; 177 178 #endif // CHROME_BROWSER_IMPORTER_NSS_DECRYPTOR_WIN_H_ 179