nss_decryptor_mac.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
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_MAC_H_
6#define CHROME_BROWSER_IMPORTER_NSS_DECRYPTOR_MAC_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/file_path.h"
13#include "base/logging.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
58enum 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
74struct SECItem {
75  SECItemType type;
76  unsigned char *data;
77  unsigned int len;
78};
79
80enum SECStatus {
81  SECWouldBlock = -2,
82  SECFailure = -1,
83  SECSuccess = 0
84};
85
86typedef int PRBool;
87#define PR_TRUE 1
88#define PR_FALSE 0
89
90typedef enum { PR_FAILURE = -1, PR_SUCCESS = 0 } PRStatus;
91
92typedef struct PK11SlotInfoStr PK11SlotInfo;
93
94typedef SECStatus (*NSSInitFunc)(const char *configdir);
95typedef SECStatus (*NSSShutdownFunc)(void);
96typedef PK11SlotInfo* (*PK11GetInternalKeySlotFunc)(void);
97typedef void (*PK11FreeSlotFunc)(PK11SlotInfo *slot);
98typedef SECStatus (*PK11CheckUserPasswordFunc)(PK11SlotInfo *slot, char *pw);
99typedef SECStatus
100    (*PK11AuthenticateFunc)(PK11SlotInfo *slot, PRBool loadCerts, void *wincx);
101typedef SECStatus
102    (*PK11SDRDecryptFunc)(SECItem *data, SECItem *result, void *cx);
103typedef void (*SECITEMFreeItemFunc)(SECItem *item, PRBool free_it);
104typedef void (*PLArenaFinishFunc)(void);
105typedef PRStatus (*PRCleanupFunc)(void);
106namespace webkit_glue {
107struct PasswordForm;
108}
109
110// A wrapper for Firefox NSS decrypt component.
111class NSSDecryptor {
112 public:
113  NSSDecryptor()
114      : NSS_Init(NULL), NSS_Shutdown(NULL), PK11_GetInternalKeySlot(NULL),
115        PK11_CheckUserPassword(NULL), PK11_FreeSlot(NULL),
116        PK11_Authenticate(NULL), PK11SDR_Decrypt(NULL), SECITEM_FreeItem(NULL),
117        is_nss_initialized_(false) {}
118  ~NSSDecryptor();
119
120  // Initializes NSS if it hasn't already been initialized.
121  bool Init(const std::wstring& /* dll_path */,
122            const std::wstring& /* db_path */);
123
124  // Decrypts Firefox stored passwords. Before using this method,
125  // make sure Init() returns true.
126  string16 Decrypt(const std::string& crypt) const;
127
128  // Parses the Firefox password file content, decrypts the
129  // username/password and reads other related information.
130  // The result will be stored in |forms|.
131  void ParseSignons(const std::string& content,
132                    std::vector<webkit_glue::PasswordForm>* forms);
133
134  // Reads and parses the Firefox password sqlite db, decrypts the
135  // username/password and reads other related information.
136  // The result will be stored in |forms|.
137  bool ReadAndParseSignons(const FilePath& sqlite_file,
138                           std::vector<webkit_glue::PasswordForm>* forms);
139 private:
140  PK11SlotInfo* GetKeySlotForDB() const { return PK11_GetInternalKeySlot(); }
141  void FreeSlot(PK11SlotInfo* slot) const { PK11_FreeSlot(slot); }
142
143  // Methods in Firefox security components.
144  NSSInitFunc NSS_Init;
145  NSSShutdownFunc NSS_Shutdown;
146  PK11GetInternalKeySlotFunc PK11_GetInternalKeySlot;
147  PK11CheckUserPasswordFunc PK11_CheckUserPassword;
148  PK11FreeSlotFunc PK11_FreeSlot;
149  PK11AuthenticateFunc PK11_Authenticate;
150  PK11SDRDecryptFunc PK11SDR_Decrypt;
151  SECITEMFreeItemFunc SECITEM_FreeItem;
152
153  // Libraries necessary for decrypting the passwords.
154  static const wchar_t kNSS3Library[];
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_BROWSER_IMPORTER_NSS_DECRYPTOR_MAC_H_
163