nss_decryptor_mac.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
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
15class 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
60enum 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
76struct SECItem {
77  SECItemType type;
78  unsigned char *data;
79  unsigned int len;
80};
81
82enum SECStatus {
83  SECWouldBlock = -2,
84  SECFailure = -1,
85  SECSuccess = 0
86};
87
88typedef int PRBool;
89#define PR_TRUE 1
90#define PR_FALSE 0
91
92typedef enum { PR_FAILURE = -1, PR_SUCCESS = 0 } PRStatus;
93
94typedef struct PK11SlotInfoStr PK11SlotInfo;
95
96typedef SECStatus (*NSSInitFunc)(const char *configdir);
97typedef SECStatus (*NSSShutdownFunc)(void);
98typedef PK11SlotInfo* (*PK11GetInternalKeySlotFunc)(void);
99typedef void (*PK11FreeSlotFunc)(PK11SlotInfo *slot);
100typedef SECStatus (*PK11CheckUserPasswordFunc)(PK11SlotInfo *slot, char *pw);
101typedef SECStatus
102    (*PK11AuthenticateFunc)(PK11SlotInfo *slot, PRBool loadCerts, void *wincx);
103typedef SECStatus
104    (*PK11SDRDecryptFunc)(SECItem *data, SECItem *result, void *cx);
105typedef void (*SECITEMFreeItemFunc)(SECItem *item, PRBool free_it);
106typedef void (*PLArenaFinishFunc)(void);
107typedef PRStatus (*PRCleanupFunc)(void);
108namespace webkit_glue {
109struct PasswordForm;
110}
111
112// A wrapper for Firefox NSS decrypt component.
113class 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 std::wstring& /* dll_path */,
124            const std::wstring& /* db_path */);
125
126  // Decrypts Firefox stored passwords. Before using this method,
127  // make sure Init() returns true.
128  string16 Decrypt(const std::string& crypt) const;
129
130  // Parses the Firefox password file content, decrypts the
131  // username/password and reads other related information.
132  // The result will be stored in |forms|.
133  void ParseSignons(const std::string& content,
134                    std::vector<webkit_glue::PasswordForm>* forms);
135
136  // Reads and parses the Firefox password sqlite db, decrypts the
137  // username/password and reads other related information.
138  // The result will be stored in |forms|.
139  bool ReadAndParseSignons(const FilePath& sqlite_file,
140                           std::vector<webkit_glue::PasswordForm>* forms);
141 private:
142  PK11SlotInfo* GetKeySlotForDB() const { return PK11_GetInternalKeySlot(); }
143  void FreeSlot(PK11SlotInfo* slot) const { PK11_FreeSlot(slot); }
144
145  // Methods in Firefox security components.
146  NSSInitFunc NSS_Init;
147  NSSShutdownFunc NSS_Shutdown;
148  PK11GetInternalKeySlotFunc PK11_GetInternalKeySlot;
149  PK11CheckUserPasswordFunc PK11_CheckUserPassword;
150  PK11FreeSlotFunc PK11_FreeSlot;
151  PK11AuthenticateFunc PK11_Authenticate;
152  PK11SDRDecryptFunc PK11SDR_Decrypt;
153  SECITEMFreeItemFunc SECITEM_FreeItem;
154
155  // Libraries necessary for decrypting the passwords.
156  static const wchar_t kNSS3Library[];
157
158  // True if NSS_Init() has been called
159  bool is_nss_initialized_;
160
161  DISALLOW_COPY_AND_ASSIGN(NSSDecryptor);
162};
163
164#endif  // CHROME_BROWSER_IMPORTER_NSS_DECRYPTOR_MAC_H_
165