15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
57dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#ifndef CHROME_UTILITY_IMPORTER_NSS_DECRYPTOR_MAC_H_
67dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#define CHROME_UTILITY_IMPORTER_NSS_DECRYPTOR_MAC_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FilePath;
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The following declarations of functions and types are from Firefox
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NSS library.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// source code:
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   security/nss/lib/util/seccomon.h
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   security/nss/lib/nss/nss.h
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The license block is:
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* ***** BEGIN LICENSE BLOCK *****
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* Version: MPL 1.1/GPL 2.0/LGPL 2.1
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* The contents of this file are subject to the Mozilla Public License Version
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* 1.1 (the "License"); you may not use this file except in compliance with
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* the License. You may obtain a copy of the License at
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* http://www.mozilla.org/MPL/
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* Software distributed under the License is distributed on an "AS IS" basis,
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* for the specific language governing rights and limitations under the
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* License.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* The Original Code is the Netscape security libraries.
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* The Initial Developer of the Original Code is
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* Netscape Communications Corporation.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* Portions created by the Initial Developer are Copyright (C) 1994-2000
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* the Initial Developer. All Rights Reserved.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* Contributor(s):
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* Alternatively, the contents of this file may be used under the terms of
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* either the GNU General Public License Version 2 or later (the "GPL"), or
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* in which case the provisions of the GPL or the LGPL are applicable instead
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* of those above. If you wish to allow use of your version of this file only
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* under the terms of either the GPL or the LGPL, and not to allow others to
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* use your version of this file under the terms of the MPL, indicate your
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* decision by deleting the provisions above and replace them with the notice
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* and other provisions required by the GPL or the LGPL. If you do not delete
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* the provisions above, a recipient may use your version of this file under
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* the terms of any one of the MPL, the GPL or the LGPL.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)* ***** END LICENSE BLOCK ***** */
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum SECItemType {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siBuffer = 0,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siClearDataBuffer = 1,
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siCipherDataBuffer = 2,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siDERCertBuffer = 3,
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siEncodedCertBuffer = 4,
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siDERNameBuffer = 5,
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siEncodedNameBuffer = 6,
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siAsciiNameString = 7,
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siAsciiString = 8,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siDEROID = 9,
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siUnsignedInteger = 10,
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siUTCTime = 11,
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  siGeneralizedTime = 12
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct SECItem {
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SECItemType type;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned char *data;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned int len;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum SECStatus {
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SECWouldBlock = -2,
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SECFailure = -1,
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SECSuccess = 0
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int PRBool;
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PR_TRUE 1
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PR_FALSE 0
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef enum { PR_FAILURE = -1, PR_SUCCESS = 0 } PRStatus;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef struct PK11SlotInfoStr PK11SlotInfo;
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef SECStatus (*NSSInitFunc)(const char *configdir);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef SECStatus (*NSSShutdownFunc)(void);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef PK11SlotInfo* (*PK11GetInternalKeySlotFunc)(void);
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef void (*PK11FreeSlotFunc)(PK11SlotInfo *slot);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef SECStatus (*PK11CheckUserPasswordFunc)(PK11SlotInfo *slot, char *pw);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef SECStatus
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    (*PK11AuthenticateFunc)(PK11SlotInfo *slot, PRBool loadCerts, void *wincx);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef SECStatus
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    (*PK11SDRDecryptFunc)(SECItem *data, SECItem *result, void *cx);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef void (*SECITEMFreeItemFunc)(SECItem *item, PRBool free_it);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef void (*PLArenaFinishFunc)(void);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef PRStatus (*PRCleanupFunc)(void);
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)namespace autofill {
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct PasswordForm;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A wrapper for Firefox NSS decrypt component.
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NSSDecryptor {
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NSSDecryptor()
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : NSS_Init(NULL), NSS_Shutdown(NULL), PK11_GetInternalKeySlot(NULL),
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        PK11_CheckUserPassword(NULL), PK11_FreeSlot(NULL),
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        PK11_Authenticate(NULL), PK11SDR_Decrypt(NULL), SECITEM_FreeItem(NULL),
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        is_nss_initialized_(false) {}
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~NSSDecryptor();
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes NSS if it hasn't already been initialized.
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool Init(const base::FilePath& dll_path, const base::FilePath& db_path);
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Decrypts Firefox stored passwords. Before using this method,
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // make sure Init() returns true.
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 Decrypt(const std::string& crypt) const;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Parses the Firefox password file content, decrypts the
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // username/password and reads other related information.
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The result will be stored in |forms|.
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ParseSignons(const std::string& content,
13558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                    std::vector<autofill::PasswordForm>* forms);
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reads and parses the Firefox password sqlite db, decrypts the
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // username/password and reads other related information.
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The result will be stored in |forms|.
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool ReadAndParseSignons(const base::FilePath& sqlite_file,
14158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                           std::vector<autofill::PasswordForm>* forms);
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PK11SlotInfo* GetKeySlotForDB() const { return PK11_GetInternalKeySlot(); }
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void FreeSlot(PK11SlotInfo* slot) const { PK11_FreeSlot(slot); }
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Methods in Firefox security components.
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NSSInitFunc NSS_Init;
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NSSShutdownFunc NSS_Shutdown;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PK11GetInternalKeySlotFunc PK11_GetInternalKeySlot;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PK11CheckUserPasswordFunc PK11_CheckUserPassword;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PK11FreeSlotFunc PK11_FreeSlot;
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PK11AuthenticateFunc PK11_Authenticate;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PK11SDRDecryptFunc PK11SDR_Decrypt;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SECITEMFreeItemFunc SECITEM_FreeItem;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if NSS_Init() has been called
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_nss_initialized_;
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(NSSDecryptor);
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif  // CHROME_UTILITY_IMPORTER_NSS_DECRYPTOR_MAC_H_
163