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