Home | History | Annotate | Download | only in importer
      1 // Copyright (c) 2010 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_MAC_H_
      6 #define CHROME_BROWSER_IMPORTER_NSS_DECRYPTOR_MAC_H_
      7 #pragma once
      8 
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/string16.h"
     14 
     15 class FilePath;
     16 
     17 // The following declarations of functions and types are from Firefox
     18 // NSS library.
     19 // source code:
     20 //   security/nss/lib/util/seccomon.h
     21 //   security/nss/lib/nss/nss.h
     22 // The license block is:
     23 
     24 /* ***** BEGIN LICENSE BLOCK *****
     25 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
     26 *
     27 * The contents of this file are subject to the Mozilla Public License Version
     28 * 1.1 (the "License"); you may not use this file except in compliance with
     29 * the License. You may obtain a copy of the License at
     30 * http://www.mozilla.org/MPL/
     31 *
     32 * Software distributed under the License is distributed on an "AS IS" basis,
     33 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
     34 * for the specific language governing rights and limitations under the
     35 * License.
     36 *
     37 * The Original Code is the Netscape security libraries.
     38 *
     39 * The Initial Developer of the Original Code is
     40 * Netscape Communications Corporation.
     41 * Portions created by the Initial Developer are Copyright (C) 1994-2000
     42 * the Initial Developer. All Rights Reserved.
     43 *
     44 * Contributor(s):
     45 *
     46 * Alternatively, the contents of this file may be used under the terms of
     47 * either the GNU General Public License Version 2 or later (the "GPL"), or
     48 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
     49 * in which case the provisions of the GPL or the LGPL are applicable instead
     50 * of those above. If you wish to allow use of your version of this file only
     51 * under the terms of either the GPL or the LGPL, and not to allow others to
     52 * use your version of this file under the terms of the MPL, indicate your
     53 * decision by deleting the provisions above and replace them with the notice
     54 * and other provisions required by the GPL or the LGPL. If you do not delete
     55 * the provisions above, a recipient may use your version of this file under
     56 * the terms of any one of the MPL, the GPL or the LGPL.
     57 *
     58 * ***** END LICENSE BLOCK ***** */
     59 
     60 enum SECItemType {
     61   siBuffer = 0,
     62   siClearDataBuffer = 1,
     63   siCipherDataBuffer = 2,
     64   siDERCertBuffer = 3,
     65   siEncodedCertBuffer = 4,
     66   siDERNameBuffer = 5,
     67   siEncodedNameBuffer = 6,
     68   siAsciiNameString = 7,
     69   siAsciiString = 8,
     70   siDEROID = 9,
     71   siUnsignedInteger = 10,
     72   siUTCTime = 11,
     73   siGeneralizedTime = 12
     74 };
     75 
     76 struct SECItem {
     77   SECItemType type;
     78   unsigned char *data;
     79   unsigned int len;
     80 };
     81 
     82 enum SECStatus {
     83   SECWouldBlock = -2,
     84   SECFailure = -1,
     85   SECSuccess = 0
     86 };
     87 
     88 typedef int PRBool;
     89 #define PR_TRUE 1
     90 #define PR_FALSE 0
     91 
     92 typedef enum { PR_FAILURE = -1, PR_SUCCESS = 0 } PRStatus;
     93 
     94 typedef struct PK11SlotInfoStr PK11SlotInfo;
     95 
     96 typedef SECStatus (*NSSInitFunc)(const char *configdir);
     97 typedef SECStatus (*NSSShutdownFunc)(void);
     98 typedef PK11SlotInfo* (*PK11GetInternalKeySlotFunc)(void);
     99 typedef void (*PK11FreeSlotFunc)(PK11SlotInfo *slot);
    100 typedef SECStatus (*PK11CheckUserPasswordFunc)(PK11SlotInfo *slot, char *pw);
    101 typedef SECStatus
    102     (*PK11AuthenticateFunc)(PK11SlotInfo *slot, PRBool loadCerts, void *wincx);
    103 typedef SECStatus
    104     (*PK11SDRDecryptFunc)(SECItem *data, SECItem *result, void *cx);
    105 typedef void (*SECITEMFreeItemFunc)(SECItem *item, PRBool free_it);
    106 typedef void (*PLArenaFinishFunc)(void);
    107 typedef PRStatus (*PRCleanupFunc)(void);
    108 namespace webkit_glue {
    109 struct PasswordForm;
    110 }
    111 
    112 // A wrapper for Firefox NSS decrypt component.
    113 class NSSDecryptor {
    114  public:
    115   NSSDecryptor()
    116       : NSS_Init(NULL), NSS_Shutdown(NULL), PK11_GetInternalKeySlot(NULL),
    117         PK11_CheckUserPassword(NULL), PK11_FreeSlot(NULL),
    118         PK11_Authenticate(NULL), PK11SDR_Decrypt(NULL), SECITEM_FreeItem(NULL),
    119         is_nss_initialized_(false) {}
    120   ~NSSDecryptor();
    121 
    122   // Initializes NSS if it hasn't already been initialized.
    123   bool Init(const FilePath& dll_path, const FilePath& db_path);
    124 
    125   // Decrypts Firefox stored passwords. Before using this method,
    126   // make sure Init() returns true.
    127   string16 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  private:
    141   PK11SlotInfo* GetKeySlotForDB() const { return PK11_GetInternalKeySlot(); }
    142   void FreeSlot(PK11SlotInfo* slot) const { PK11_FreeSlot(slot); }
    143 
    144   // Methods in Firefox security components.
    145   NSSInitFunc NSS_Init;
    146   NSSShutdownFunc NSS_Shutdown;
    147   PK11GetInternalKeySlotFunc PK11_GetInternalKeySlot;
    148   PK11CheckUserPasswordFunc PK11_CheckUserPassword;
    149   PK11FreeSlotFunc PK11_FreeSlot;
    150   PK11AuthenticateFunc PK11_Authenticate;
    151   PK11SDRDecryptFunc PK11SDR_Decrypt;
    152   SECITEMFreeItemFunc SECITEM_FreeItem;
    153 
    154   // True if NSS_Init() has been called
    155   bool is_nss_initialized_;
    156 
    157   DISALLOW_COPY_AND_ASSIGN(NSSDecryptor);
    158 };
    159 
    160 #endif  // CHROME_BROWSER_IMPORTER_NSS_DECRYPTOR_MAC_H_
    161