annotate nss/lib/softoken/legacydb/keydb.c @ 4:b513267f632f tip

Build DBM module
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 05 Aug 2014 18:58:03 +0200
parents 150b72113545
children
rev   line source
3
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1 /* This Source Code Form is subject to the terms of the Mozilla Public
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
4
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
5 #include "lowkeyi.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
6 #include "secasn1.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
7 #include "secder.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
8 #include "secoid.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
9 #include "blapi.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
10 #include "secitem.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
11 #include "pcert.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
12 #include "mcom_db.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
13 #include "secerr.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
14
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
15 #include "keydbi.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
16 #include "lgdb.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
17
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
18 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
19 * Record keys for keydb
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
20 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
21 #define SALT_STRING "global-salt"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
22 #define VERSION_STRING "Version"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
23 #define KEYDB_PW_CHECK_STRING "password-check"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
24 #define KEYDB_PW_CHECK_LEN 14
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
25 #define KEYDB_FAKE_PW_CHECK_STRING "fake-password-check"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
26 #define KEYDB_FAKE_PW_CHECK_LEN 19
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
27
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
28 /* Size of the global salt for key database */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
29 #define SALT_LENGTH 16
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
30
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
31 SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
32
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
33 const SEC_ASN1Template nsslowkey_EncryptedPrivateKeyInfoTemplate[] = {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
34 { SEC_ASN1_SEQUENCE,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
35 0, NULL, sizeof(NSSLOWKEYEncryptedPrivateKeyInfo) },
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
36 { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
37 offsetof(NSSLOWKEYEncryptedPrivateKeyInfo,algorithm),
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
38 SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
39 { SEC_ASN1_OCTET_STRING,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
40 offsetof(NSSLOWKEYEncryptedPrivateKeyInfo,encryptedData) },
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
41 { 0 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
42 };
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
43
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
44 const SEC_ASN1Template nsslowkey_PointerToEncryptedPrivateKeyInfoTemplate[] = {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
45 { SEC_ASN1_POINTER, 0, nsslowkey_EncryptedPrivateKeyInfoTemplate }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
46 };
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
47
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
48
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
49 /* ====== Default key databse encryption algorithm ====== */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
50 static void
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
51 sec_destroy_dbkey(NSSLOWKEYDBKey *dbkey)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
52 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
53 if ( dbkey && dbkey->arena ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
54 PORT_FreeArena(dbkey->arena, PR_FALSE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
55 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
56 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
57
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
58 static void
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
59 free_dbt(DBT *dbt)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
60 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
61 if ( dbt ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
62 PORT_Free(dbt->data);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
63 PORT_Free(dbt);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
64 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
65
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
66 return;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
67 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
68
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
69 static int keydb_Get(NSSLOWKEYDBHandle *db, DBT *key, DBT *data,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
70 unsigned int flags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
71 static int keydb_Put(NSSLOWKEYDBHandle *db, DBT *key, DBT *data,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
72 unsigned int flags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
73 static int keydb_Sync(NSSLOWKEYDBHandle *db, unsigned int flags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
74 static int keydb_Del(NSSLOWKEYDBHandle *db, DBT *key, unsigned int flags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
75 static int keydb_Seq(NSSLOWKEYDBHandle *db, DBT *key, DBT *data,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
76 unsigned int flags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
77 static void keydb_Close(NSSLOWKEYDBHandle *db);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
78
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
79 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
80 * format of key database entries for version 3 of database:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
81 * byte offset field
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
82 * ----------- -----
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
83 * 0 version
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
84 * 1 salt-len
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
85 * 2 nn-len
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
86 * 3.. salt-data
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
87 * ... nickname
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
88 * ... encrypted-key-data
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
89 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
90 static DBT *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
91 encode_dbkey(NSSLOWKEYDBKey *dbkey,unsigned char version)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
92 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
93 DBT *bufitem = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
94 unsigned char *buf;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
95 int nnlen;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
96 char *nn;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
97
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
98 bufitem = (DBT *)PORT_ZAlloc(sizeof(DBT));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
99 if ( bufitem == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
100 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
101 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
102
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
103 if ( dbkey->nickname ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
104 nn = dbkey->nickname;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
105 nnlen = PORT_Strlen(nn) + 1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
106 } else {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
107 nn = "";
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
108 nnlen = 1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
109 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
110
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
111 /* compute the length of the record */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
112 /* 1 + 1 + 1 == version number header + salt length + nn len */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
113 bufitem->size = dbkey->salt.len + nnlen + dbkey->derPK.len + 1 + 1 + 1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
114
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
115 bufitem->data = (void *)PORT_ZAlloc(bufitem->size);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
116 if ( bufitem->data == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
117 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
118 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
119
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
120 buf = (unsigned char *)bufitem->data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
121
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
122 /* set version number */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
123 buf[0] = version;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
124
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
125 /* set length of salt */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
126 PORT_Assert(dbkey->salt.len < 256);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
127 buf[1] = dbkey->salt.len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
128
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
129 /* set length of nickname */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
130 PORT_Assert(nnlen < 256);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
131 buf[2] = nnlen;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
132
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
133 /* copy salt */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
134 PORT_Memcpy(&buf[3], dbkey->salt.data, dbkey->salt.len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
135
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
136 /* copy nickname */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
137 PORT_Memcpy(&buf[3 + dbkey->salt.len], nn, nnlen);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
138
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
139 /* copy encrypted key */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
140 PORT_Memcpy(&buf[3 + dbkey->salt.len + nnlen], dbkey->derPK.data,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
141 dbkey->derPK.len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
142
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
143 return(bufitem);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
144
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
145 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
146 if ( bufitem ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
147 free_dbt(bufitem);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
148 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
149
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
150 return(NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
151 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
152
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
153 static NSSLOWKEYDBKey *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
154 decode_dbkey(DBT *bufitem, int expectedVersion)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
155 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
156 NSSLOWKEYDBKey *dbkey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
157 PLArenaPool *arena = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
158 unsigned char *buf;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
159 int version;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
160 int keyoff;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
161 int nnlen;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
162 int saltoff;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
163
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
164 buf = (unsigned char *)bufitem->data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
165
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
166 version = buf[0];
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
167
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
168 if ( version != expectedVersion ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
169 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
170 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
171
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
172 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
173 if ( arena == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
174 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
175 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
176
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
177 dbkey = (NSSLOWKEYDBKey *)PORT_ArenaZAlloc(arena, sizeof(NSSLOWKEYDBKey));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
178 if ( dbkey == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
179 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
180 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
181
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
182 dbkey->arena = arena;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
183 dbkey->salt.data = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
184 dbkey->derPK.data = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
185
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
186 dbkey->salt.len = buf[1];
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
187 dbkey->salt.data = (unsigned char *)PORT_ArenaZAlloc(arena, dbkey->salt.len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
188 if ( dbkey->salt.data == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
189 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
190 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
191
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
192 saltoff = 2;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
193 keyoff = 2 + dbkey->salt.len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
194
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
195 if ( expectedVersion >= 3 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
196 nnlen = buf[2];
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
197 if ( nnlen ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
198 dbkey->nickname = (char *)PORT_ArenaZAlloc(arena, nnlen + 1);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
199 if ( dbkey->nickname ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
200 PORT_Memcpy(dbkey->nickname, &buf[keyoff+1], nnlen);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
201 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
202 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
203 keyoff += ( nnlen + 1 );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
204 saltoff = 3;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
205 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
206
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
207 PORT_Memcpy(dbkey->salt.data, &buf[saltoff], dbkey->salt.len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
208
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
209 dbkey->derPK.len = bufitem->size - keyoff;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
210 dbkey->derPK.data = (unsigned char *)PORT_ArenaZAlloc(arena,dbkey->derPK.len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
211 if ( dbkey->derPK.data == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
212 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
213 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
214
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
215 PORT_Memcpy(dbkey->derPK.data, &buf[keyoff], dbkey->derPK.len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
216
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
217 return(dbkey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
218
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
219 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
220
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
221 if ( arena ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
222 PORT_FreeArena(arena, PR_FALSE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
223 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
224
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
225 return(NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
226 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
227
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
228 static NSSLOWKEYDBKey *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
229 get_dbkey(NSSLOWKEYDBHandle *handle, DBT *index)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
230 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
231 NSSLOWKEYDBKey *dbkey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
232 DBT entry;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
233 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
234
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
235 /* get it from the database */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
236 ret = keydb_Get(handle, index, &entry, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
237 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
238 PORT_SetError(SEC_ERROR_BAD_DATABASE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
239 return NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
240 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
241
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
242 /* set up dbkey struct */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
243
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
244 dbkey = decode_dbkey(&entry, handle->version);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
245
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
246 return(dbkey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
247 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
248
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
249 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
250 put_dbkey(NSSLOWKEYDBHandle *handle, DBT *index, NSSLOWKEYDBKey *dbkey, PRBool update)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
251 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
252 DBT *keydata = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
253 int status;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
254
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
255 keydata = encode_dbkey(dbkey, handle->version);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
256 if ( keydata == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
257 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
258 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
259
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
260 /* put it in the database */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
261 if ( update ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
262 status = keydb_Put(handle, index, keydata, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
263 } else {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
264 status = keydb_Put(handle, index, keydata, R_NOOVERWRITE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
265 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
266
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
267 if ( status ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
268 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
269 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
270
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
271 /* sync the database */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
272 status = keydb_Sync(handle, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
273 if ( status ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
274 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
275 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
276
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
277 free_dbt(keydata);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
278 return(SECSuccess);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
279
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
280 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
281 if ( keydata ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
282 free_dbt(keydata);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
283 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
284
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
285 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
286 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
287
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
288 SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
289 nsslowkey_TraverseKeys(NSSLOWKEYDBHandle *handle,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
290 SECStatus (* keyfunc)(DBT *k, DBT *d, void *pdata),
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
291 void *udata )
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
292 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
293 DBT data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
294 DBT key;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
295 SECStatus status;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
296 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
297
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
298 if (handle == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
299 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
300 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
301
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
302 ret = keydb_Seq(handle, &key, &data, R_FIRST);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
303 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
304 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
305 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
306
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
307 do {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
308 /* skip version record */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
309 if ( data.size > 1 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
310 if ( key.size == ( sizeof(SALT_STRING) - 1 ) ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
311 if ( PORT_Memcmp(key.data, SALT_STRING, key.size) == 0 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
312 continue;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
313 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
314 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
315
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
316 /* skip password check */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
317 if ( key.size == KEYDB_PW_CHECK_LEN ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
318 if ( PORT_Memcmp(key.data, KEYDB_PW_CHECK_STRING,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
319 KEYDB_PW_CHECK_LEN) == 0 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
320 continue;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
321 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
322 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
323
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
324 status = (* keyfunc)(&key, &data, udata);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
325 if (status != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
326 return(status);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
327 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
328 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
329 } while ( keydb_Seq(handle, &key, &data, R_NEXT) == 0 );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
330
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
331 return(SECSuccess);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
332 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
333
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
334 #ifdef notdef
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
335 typedef struct keyNode {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
336 struct keyNode *next;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
337 DBT key;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
338 } keyNode;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
339
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
340 typedef struct {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
341 PLArenaPool *arena;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
342 keyNode *head;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
343 } keyList;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
344
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
345 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
346 sec_add_key_to_list(DBT *key, DBT *data, void *arg)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
347 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
348 keyList *keylist;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
349 keyNode *node;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
350 void *keydata;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
351
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
352 keylist = (keyList *)arg;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
353
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
354 /* allocate the node struct */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
355 node = (keyNode*)PORT_ArenaZAlloc(keylist->arena, sizeof(keyNode));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
356 if ( node == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
357 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
358 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
359
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
360 /* allocate room for key data */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
361 keydata = PORT_ArenaZAlloc(keylist->arena, key->size);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
362 if ( keydata == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
363 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
364 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
365
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
366 /* link node into list */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
367 node->next = keylist->head;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
368 keylist->head = node;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
369
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
370 /* copy key into node */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
371 PORT_Memcpy(keydata, key->data, key->size);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
372 node->key.size = key->size;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
373 node->key.data = keydata;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
374
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
375 return(SECSuccess);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
376 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
377 #endif
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
378
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
379 static SECItem *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
380 decodeKeyDBGlobalSalt(DBT *saltData)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
381 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
382 SECItem *saltitem;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
383
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
384 saltitem = (SECItem *)PORT_ZAlloc(sizeof(SECItem));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
385 if ( saltitem == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
386 return(NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
387 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
388
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
389 saltitem->data = (unsigned char *)PORT_ZAlloc(saltData->size);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
390 if ( saltitem->data == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
391 PORT_Free(saltitem);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
392 return(NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
393 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
394
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
395 saltitem->len = saltData->size;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
396 PORT_Memcpy(saltitem->data, saltData->data, saltitem->len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
397
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
398 return(saltitem);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
399 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
400
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
401 static SECItem *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
402 GetKeyDBGlobalSalt(NSSLOWKEYDBHandle *handle)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
403 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
404 DBT saltKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
405 DBT saltData;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
406 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
407
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
408 saltKey.data = SALT_STRING;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
409 saltKey.size = sizeof(SALT_STRING) - 1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
410
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
411 ret = keydb_Get(handle, &saltKey, &saltData, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
412 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
413 return(NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
414 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
415
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
416 return(decodeKeyDBGlobalSalt(&saltData));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
417 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
418
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
419 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
420 StoreKeyDBGlobalSalt(NSSLOWKEYDBHandle *handle, SECItem *salt)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
421 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
422 DBT saltKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
423 DBT saltData;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
424 int status;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
425
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
426 saltKey.data = SALT_STRING;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
427 saltKey.size = sizeof(SALT_STRING) - 1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
428
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
429 saltData.data = (void *)salt->data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
430 saltData.size = salt->len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
431
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
432 /* put global salt into the database now */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
433 status = keydb_Put(handle, &saltKey, &saltData, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
434 if ( status ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
435 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
436 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
437
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
438 return(SECSuccess);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
439 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
440
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
441 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
442 makeGlobalVersion(NSSLOWKEYDBHandle *handle)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
443 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
444 unsigned char version;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
445 DBT versionData;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
446 DBT versionKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
447 int status;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
448
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
449 version = NSSLOWKEY_DB_FILE_VERSION;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
450 versionData.data = &version;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
451 versionData.size = 1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
452 versionKey.data = VERSION_STRING;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
453 versionKey.size = sizeof(VERSION_STRING)-1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
454
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
455 /* put version string into the database now */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
456 status = keydb_Put(handle, &versionKey, &versionData, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
457 if ( status ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
458 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
459 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
460 handle->version = version;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
461
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
462 return(SECSuccess);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
463 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
464
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
465
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
466 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
467 makeGlobalSalt(NSSLOWKEYDBHandle *handle)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
468 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
469 DBT saltKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
470 DBT saltData;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
471 unsigned char saltbuf[16];
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
472 int status;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
473
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
474 saltKey.data = SALT_STRING;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
475 saltKey.size = sizeof(SALT_STRING) - 1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
476
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
477 saltData.data = (void *)saltbuf;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
478 saltData.size = sizeof(saltbuf);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
479 RNG_GenerateGlobalRandomBytes(saltbuf, sizeof(saltbuf));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
480
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
481 /* put global salt into the database now */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
482 status = keydb_Put(handle, &saltKey, &saltData, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
483 if ( status ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
484 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
485 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
486
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
487 return(SECSuccess);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
488 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
489
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
490 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
491 encodePWCheckEntry(PLArenaPool *arena, SECItem *entry, SECOidTag alg,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
492 SECItem *encCheck);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
493
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
494 static unsigned char
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
495 nsslowkey_version(NSSLOWKEYDBHandle *handle)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
496 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
497 DBT versionKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
498 DBT versionData;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
499 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
500 versionKey.data = VERSION_STRING;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
501 versionKey.size = sizeof(VERSION_STRING)-1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
502
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
503 if (handle->db == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
504 return 255;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
505 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
506
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
507 /* lookup version string in database */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
508 ret = keydb_Get( handle, &versionKey, &versionData, 0 );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
509
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
510 /* error accessing the database */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
511 if ( ret < 0 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
512 return 255;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
513 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
514
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
515 if ( ret >= 1 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
516 return 0;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
517 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
518 return *( (unsigned char *)versionData.data);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
519 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
520
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
521 static PRBool
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
522 seckey_HasAServerKey(NSSLOWKEYDBHandle *handle)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
523 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
524 DBT key;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
525 DBT data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
526 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
527 PRBool found = PR_FALSE;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
528
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
529 ret = keydb_Seq(handle, &key, &data, R_FIRST);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
530 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
531 return PR_FALSE;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
532 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
533
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
534 do {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
535 /* skip version record */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
536 if ( data.size > 1 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
537 /* skip salt */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
538 if ( key.size == ( sizeof(SALT_STRING) - 1 ) ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
539 if ( PORT_Memcmp(key.data, SALT_STRING, key.size) == 0 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
540 continue;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
541 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
542 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
543 /* skip pw check entry */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
544 if ( key.size == KEYDB_PW_CHECK_LEN ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
545 if ( PORT_Memcmp(key.data, KEYDB_PW_CHECK_STRING,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
546 KEYDB_PW_CHECK_LEN) == 0 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
547 continue;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
548 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
549 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
550
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
551 /* keys stored by nickname will have 0 as the last byte of the
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
552 * db key. Other keys must be stored by modulus. We will not
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
553 * update those because they are left over from a keygen that
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
554 * never resulted in a cert.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
555 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
556 if ( ((unsigned char *)key.data)[key.size-1] != 0 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
557 continue;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
558 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
559
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
560 if (PORT_Strcmp(key.data,"Server-Key") == 0) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
561 found = PR_TRUE;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
562 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
563 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
564
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
565 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
566 } while ( keydb_Seq(handle, &key, &data, R_NEXT) == 0 );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
567
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
568 return found;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
569 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
570
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
571 /* forward declare local create function */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
572 static NSSLOWKEYDBHandle * nsslowkey_NewHandle(DB *dbHandle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
573
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
574 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
575 * currently updates key database from v2 to v3
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
576 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
577 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
578 nsslowkey_UpdateKeyDBPass1(NSSLOWKEYDBHandle *handle)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
579 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
580 SECStatus rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
581 DBT checkKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
582 DBT checkData;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
583 DBT saltKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
584 DBT saltData;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
585 DBT key;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
586 DBT data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
587 unsigned char version;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
588 NSSLOWKEYDBKey *dbkey = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
589 NSSLOWKEYDBHandle *update = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
590 SECItem *oldSalt = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
591 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
592 SECItem checkitem;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
593
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
594 if ( handle->updatedb == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
595 return SECSuccess;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
596 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
597
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
598 /* create a full DB Handle for our update so we
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
599 * can use the correct locks for the db primatives */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
600 update = nsslowkey_NewHandle(handle->updatedb);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
601 if ( update == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
602 return SECSuccess;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
603 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
604
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
605 /* update has now inherited the database handle */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
606 handle->updatedb = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
607
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
608 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
609 * check the version record
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
610 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
611 version = nsslowkey_version(update);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
612 if (version != 2) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
613 goto done;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
614 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
615
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
616 saltKey.data = SALT_STRING;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
617 saltKey.size = sizeof(SALT_STRING) - 1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
618
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
619 ret = keydb_Get(update, &saltKey, &saltData, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
620 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
621 /* no salt in old db, so it is corrupted */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
622 goto done;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
623 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
624
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
625 oldSalt = decodeKeyDBGlobalSalt(&saltData);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
626 if ( oldSalt == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
627 /* bad salt in old db, so it is corrupted */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
628 goto done;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
629 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
630
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
631 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
632 * look for a pw check entry
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
633 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
634 checkKey.data = KEYDB_PW_CHECK_STRING;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
635 checkKey.size = KEYDB_PW_CHECK_LEN;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
636
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
637 ret = keydb_Get(update, &checkKey, &checkData, 0 );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
638 if (ret) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
639 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
640 * if we have a key, but no KEYDB_PW_CHECK_STRING, then this must
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
641 * be an old server database, and it does have a password associated
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
642 * with it. Put a fake entry in so we can identify this db when we do
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
643 * get the password for it.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
644 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
645 if (seckey_HasAServerKey(update)) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
646 DBT fcheckKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
647 DBT fcheckData;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
648
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
649 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
650 * include a fake string
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
651 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
652 fcheckKey.data = KEYDB_FAKE_PW_CHECK_STRING;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
653 fcheckKey.size = KEYDB_FAKE_PW_CHECK_LEN;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
654 fcheckData.data = "1";
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
655 fcheckData.size = 1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
656 /* put global salt into the new database now */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
657 ret = keydb_Put( handle, &saltKey, &saltData, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
658 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
659 goto done;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
660 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
661 ret = keydb_Put( handle, &fcheckKey, &fcheckData, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
662 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
663 goto done;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
664 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
665 } else {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
666 goto done;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
667 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
668 } else {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
669 /* put global salt into the new database now */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
670 ret = keydb_Put( handle, &saltKey, &saltData, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
671 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
672 goto done;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
673 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
674
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
675 dbkey = decode_dbkey(&checkData, 2);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
676 if ( dbkey == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
677 goto done;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
678 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
679 checkitem = dbkey->derPK;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
680 dbkey->derPK.data = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
681
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
682 /* format the new pw check entry */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
683 rv = encodePWCheckEntry(NULL, &dbkey->derPK, SEC_OID_RC4, &checkitem);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
684 if ( rv != SECSuccess ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
685 goto done;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
686 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
687
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
688 rv = put_dbkey(handle, &checkKey, dbkey, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
689 if ( rv != SECSuccess ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
690 goto done;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
691 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
692
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
693 /* free the dbkey */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
694 sec_destroy_dbkey(dbkey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
695 dbkey = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
696 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
697
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
698
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
699 /* now traverse the database */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
700 ret = keydb_Seq(update, &key, &data, R_FIRST);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
701 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
702 goto done;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
703 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
704
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
705 do {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
706 /* skip version record */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
707 if ( data.size > 1 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
708 /* skip salt */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
709 if ( key.size == ( sizeof(SALT_STRING) - 1 ) ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
710 if ( PORT_Memcmp(key.data, SALT_STRING, key.size) == 0 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
711 continue;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
712 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
713 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
714 /* skip pw check entry */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
715 if ( key.size == checkKey.size ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
716 if ( PORT_Memcmp(key.data, checkKey.data, key.size) == 0 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
717 continue;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
718 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
719 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
720
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
721 /* keys stored by nickname will have 0 as the last byte of the
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
722 * db key. Other keys must be stored by modulus. We will not
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
723 * update those because they are left over from a keygen that
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
724 * never resulted in a cert.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
725 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
726 if ( ((unsigned char *)key.data)[key.size-1] != 0 ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
727 continue;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
728 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
729
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
730 dbkey = decode_dbkey(&data, 2);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
731 if ( dbkey == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
732 continue;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
733 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
734
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
735 /* This puts the key into the new database with the same
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
736 * index (nickname) that it had before. The second pass
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
737 * of the update will have the password. It will decrypt
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
738 * and re-encrypt the entries using a new algorithm.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
739 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
740 dbkey->nickname = (char *)key.data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
741 rv = put_dbkey(handle, &key, dbkey, PR_FALSE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
742 dbkey->nickname = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
743
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
744 sec_destroy_dbkey(dbkey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
745 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
746 } while ( keydb_Seq(update, &key, &data, R_NEXT) == 0 );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
747
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
748 dbkey = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
749
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
750 done:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
751 /* sync the database */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
752 ret = keydb_Sync(handle, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
753
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
754 nsslowkey_CloseKeyDB(update);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
755
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
756 if ( oldSalt ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
757 SECITEM_FreeItem(oldSalt, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
758 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
759
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
760 if ( dbkey ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
761 sec_destroy_dbkey(dbkey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
762 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
763
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
764 return(SECSuccess);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
765 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
766
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
767 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
768 openNewDB(const char *appName, const char *prefix, const char *dbname,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
769 NSSLOWKEYDBHandle *handle, NSSLOWKEYDBNameFunc namecb, void *cbarg)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
770 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
771 SECStatus rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
772 int status = RDB_FAIL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
773 char *updname = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
774 DB *updatedb = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
775 PRBool updated = PR_FALSE;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
776 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
777
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
778 if (appName) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
779 handle->db = rdbopen( appName, prefix, "key", NO_CREATE, &status);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
780 } else {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
781 handle->db = dbopen( dbname, NO_CREATE, 0600, DB_HASH, 0 );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
782 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
783 /* if create fails then we lose */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
784 if ( handle->db == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
785 return (status == RDB_RETRY) ? SECWouldBlock: SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
786 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
787
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
788 /* force a transactional read, which will verify that one and only one
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
789 * process attempts the update. */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
790 if (nsslowkey_version(handle) == NSSLOWKEY_DB_FILE_VERSION) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
791 /* someone else has already updated the database for us */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
792 db_InitComplete(handle->db);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
793 return SECSuccess;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
794 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
795
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
796 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
797 * if we are creating a multiaccess database, see if there is a
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
798 * local database we can update from.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
799 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
800 if (appName) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
801 NSSLOWKEYDBHandle *updateHandle;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
802 updatedb = dbopen( dbname, NO_RDONLY, 0600, DB_HASH, 0 );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
803 if (!updatedb) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
804 goto noupdate;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
805 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
806
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
807 /* nsslowkey_version needs a full handle because it calls
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
808 * the kdb_Get() function, which needs to lock.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
809 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
810 updateHandle = nsslowkey_NewHandle(updatedb);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
811 if (!updateHandle) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
812 updatedb->close(updatedb);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
813 goto noupdate;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
814 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
815
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
816 handle->version = nsslowkey_version(updateHandle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
817 if (handle->version != NSSLOWKEY_DB_FILE_VERSION) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
818 nsslowkey_CloseKeyDB(updateHandle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
819 goto noupdate;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
820 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
821
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
822 /* copy the new DB from the old one */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
823 db_Copy(handle->db, updatedb);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
824 nsslowkey_CloseKeyDB(updateHandle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
825 db_InitComplete(handle->db);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
826 return SECSuccess;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
827 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
828 noupdate:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
829
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
830 /* update the version number */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
831 rv = makeGlobalVersion(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
832 if ( rv != SECSuccess ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
833 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
834 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
835
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
836 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
837 * try to update from v2 db
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
838 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
839 updname = (*namecb)(cbarg, 2);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
840 if ( updname != NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
841 handle->updatedb = dbopen( updname, NO_RDONLY, 0600, DB_HASH, 0 );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
842 PORT_Free( updname );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
843
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
844 if ( handle->updatedb ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
845 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
846 * Try to update the db using a null password. If the db
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
847 * doesn't have a password, then this will work. If it does
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
848 * have a password, then this will fail and we will do the
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
849 * update later
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
850 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
851 rv = nsslowkey_UpdateKeyDBPass1(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
852 if ( rv == SECSuccess ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
853 updated = PR_TRUE;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
854 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
855 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
856
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
857 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
858
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
859 /* we are using the old salt if we updated from an old db */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
860 if ( ! updated ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
861 rv = makeGlobalSalt(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
862 if ( rv != SECSuccess ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
863 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
864 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
865 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
866
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
867 /* sync the database */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
868 ret = keydb_Sync(handle, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
869 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
870 rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
871 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
872 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
873 rv = SECSuccess;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
874
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
875 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
876 db_InitComplete(handle->db);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
877 return rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
878 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
879
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
880
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
881 static DB *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
882 openOldDB(const char *appName, const char *prefix, const char *dbname,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
883 PRBool openflags) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
884 DB *db = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
885
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
886 if (appName) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
887 db = rdbopen( appName, prefix, "key", openflags, NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
888 } else {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
889 db = dbopen( dbname, openflags, 0600, DB_HASH, 0 );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
890 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
891
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
892 return db;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
893 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
894
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
895 /* check for correct version number */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
896 static PRBool
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
897 verifyVersion(NSSLOWKEYDBHandle *handle)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
898 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
899 int version = nsslowkey_version(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
900
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
901 handle->version = version;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
902 if (version != NSSLOWKEY_DB_FILE_VERSION ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
903 if (handle->db) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
904 keydb_Close(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
905 handle->db = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
906 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
907 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
908 return handle->db != NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
909 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
910
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
911 static NSSLOWKEYDBHandle *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
912 nsslowkey_NewHandle(DB *dbHandle)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
913 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
914 NSSLOWKEYDBHandle *handle;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
915 handle = (NSSLOWKEYDBHandle *)PORT_ZAlloc (sizeof(NSSLOWKEYDBHandle));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
916 if (handle == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
917 PORT_SetError (SEC_ERROR_NO_MEMORY);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
918 return NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
919 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
920
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
921 handle->appname = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
922 handle->dbname = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
923 handle->global_salt = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
924 handle->updatedb = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
925 handle->db = dbHandle;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
926 handle->ref = 1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
927 handle->lock = PZ_NewLock(nssILockKeyDB);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
928
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
929 return handle;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
930 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
931
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
932 NSSLOWKEYDBHandle *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
933 nsslowkey_OpenKeyDB(PRBool readOnly, const char *appName, const char *prefix,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
934 NSSLOWKEYDBNameFunc namecb, void *cbarg)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
935 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
936 NSSLOWKEYDBHandle *handle = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
937 SECStatus rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
938 int openflags;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
939 char *dbname = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
940
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
941
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
942 handle = nsslowkey_NewHandle(NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
943
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
944 openflags = readOnly ? NO_RDONLY : NO_RDWR;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
945
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
946
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
947 dbname = (*namecb)(cbarg, NSSLOWKEY_DB_FILE_VERSION);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
948 if ( dbname == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
949 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
950 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
951 handle->appname = appName ? PORT_Strdup(appName) : NULL ;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
952 handle->dbname = (appName == NULL) ? PORT_Strdup(dbname) :
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
953 (prefix ? PORT_Strdup(prefix) : NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
954 handle->readOnly = readOnly;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
955
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
956
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
957
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
958 handle->db = openOldDB(appName, prefix, dbname, openflags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
959 if (handle->db) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
960 verifyVersion(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
961 if (handle->version == 255) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
962 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
963 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
964 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
965
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
966 /* if first open fails, try to create a new DB */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
967 if ( handle->db == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
968 if ( readOnly ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
969 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
970 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
971
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
972 rv = openNewDB(appName, prefix, dbname, handle, namecb, cbarg);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
973 /* two processes started to initialize the database at the same time.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
974 * The multiprocess code blocked the second one, then had it retry to
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
975 * see if it can just open the database normally */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
976 if (rv == SECWouldBlock) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
977 handle->db = openOldDB(appName,prefix,dbname, openflags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
978 verifyVersion(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
979 if (handle->db == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
980 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
981 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
982 } else if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
983 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
984 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
985 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
986
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
987 handle->global_salt = GetKeyDBGlobalSalt(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
988 if ( dbname )
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
989 PORT_Free( dbname );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
990 return handle;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
991
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
992 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
993
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
994 if ( dbname )
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
995 PORT_Free( dbname );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
996 PORT_SetError(SEC_ERROR_BAD_DATABASE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
997 nsslowkey_CloseKeyDB(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
998 return NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
999 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1000
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1001 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1002 * Close the database
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1003 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1004 void
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1005 nsslowkey_CloseKeyDB(NSSLOWKEYDBHandle *handle)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1006 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1007 if (handle != NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1008 if (handle->db != NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1009 keydb_Close(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1010 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1011 if (handle->updatedb) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1012 handle->updatedb->close(handle->updatedb);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1013 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1014 if (handle->dbname) PORT_Free(handle->dbname);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1015 if (handle->appname) PORT_Free(handle->appname);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1016 if (handle->global_salt) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1017 SECITEM_FreeItem(handle->global_salt,PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1018 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1019 if (handle->lock != NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1020 SKIP_AFTER_FORK(PZ_DestroyLock(handle->lock));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1021 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1022
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1023 PORT_Free(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1024 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1025 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1026
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1027 /* Get the key database version */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1028 int
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1029 nsslowkey_GetKeyDBVersion(NSSLOWKEYDBHandle *handle)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1030 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1031 PORT_Assert(handle != NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1032
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1033 return handle->version;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1034 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1035
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1036 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1037 * Delete a private key that was stored in the database
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1038 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1039 SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1040 nsslowkey_DeleteKey(NSSLOWKEYDBHandle *handle, const SECItem *pubkey)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1041 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1042 DBT namekey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1043 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1044
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1045 if (handle == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1046 PORT_SetError(SEC_ERROR_BAD_DATABASE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1047 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1048 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1049
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1050 /* set up db key and data */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1051 namekey.data = pubkey->data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1052 namekey.size = pubkey->len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1053
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1054 /* delete it from the database */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1055 ret = keydb_Del(handle, &namekey, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1056 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1057 PORT_SetError(SEC_ERROR_BAD_DATABASE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1058 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1059 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1060
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1061 /* sync the database */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1062 ret = keydb_Sync(handle, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1063 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1064 PORT_SetError(SEC_ERROR_BAD_DATABASE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1065 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1066 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1067
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1068 return(SECSuccess);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1069 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1070
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1071 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1072 * Store a key in the database, indexed by its public key modulus.(value!)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1073 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1074 SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1075 nsslowkey_StoreKeyByPublicKey(NSSLOWKEYDBHandle *handle,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1076 NSSLOWKEYPrivateKey *privkey,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1077 SECItem *pubKeyData,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1078 char *nickname,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1079 SDB *sdb)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1080 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1081 return nsslowkey_StoreKeyByPublicKeyAlg(handle, privkey, pubKeyData,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1082 nickname, sdb, PR_FALSE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1083 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1084
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1085 SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1086 nsslowkey_UpdateNickname(NSSLOWKEYDBHandle *handle,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1087 NSSLOWKEYPrivateKey *privkey,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1088 SECItem *pubKeyData,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1089 char *nickname,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1090 SDB *sdb)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1091 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1092 return nsslowkey_StoreKeyByPublicKeyAlg(handle, privkey, pubKeyData,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1093 nickname, sdb, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1094 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1095
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1096 /* see if the symetric CKA_ID already Exists.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1097 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1098 PRBool
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1099 nsslowkey_KeyForIDExists(NSSLOWKEYDBHandle *handle, SECItem *id)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1100 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1101 DBT namekey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1102 DBT dummy;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1103 int status;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1104
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1105 namekey.data = (char *)id->data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1106 namekey.size = id->len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1107 status = keydb_Get(handle, &namekey, &dummy, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1108 if ( status ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1109 return PR_FALSE;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1110 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1111
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1112 return PR_TRUE;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1113 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1114
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1115 /* see if the public key for this cert is in the database filed
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1116 * by modulus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1117 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1118 PRBool
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1119 nsslowkey_KeyForCertExists(NSSLOWKEYDBHandle *handle, NSSLOWCERTCertificate *cert)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1120 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1121 NSSLOWKEYPublicKey *pubkey = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1122 DBT namekey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1123 DBT dummy;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1124 int status;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1125
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1126 /* get cert's public key */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1127 pubkey = nsslowcert_ExtractPublicKey(cert);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1128 if ( pubkey == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1129 return PR_FALSE;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1130 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1131
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1132 /* TNH - make key from NSSLOWKEYPublicKey */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1133 switch (pubkey->keyType) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1134 case NSSLOWKEYRSAKey:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1135 namekey.data = pubkey->u.rsa.modulus.data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1136 namekey.size = pubkey->u.rsa.modulus.len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1137 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1138 case NSSLOWKEYDSAKey:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1139 namekey.data = pubkey->u.dsa.publicValue.data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1140 namekey.size = pubkey->u.dsa.publicValue.len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1141 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1142 case NSSLOWKEYDHKey:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1143 namekey.data = pubkey->u.dh.publicValue.data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1144 namekey.size = pubkey->u.dh.publicValue.len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1145 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1146 #ifndef NSS_DISABLE_ECC
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1147 case NSSLOWKEYECKey:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1148 namekey.data = pubkey->u.ec.publicValue.data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1149 namekey.size = pubkey->u.ec.publicValue.len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1150 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1151 #endif /* NSS_DISABLE_ECC */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1152 default:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1153 /* XXX We don't do Fortezza or DH yet. */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1154 return PR_FALSE;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1155 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1156
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1157 if (handle->version != 3) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1158 unsigned char buf[SHA1_LENGTH];
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1159 SHA1_HashBuf(buf,namekey.data,namekey.size);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1160 /* NOTE: don't use pubkey after this! it's now thrashed */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1161 PORT_Memcpy(namekey.data,buf,sizeof(buf));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1162 namekey.size = sizeof(buf);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1163 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1164
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1165 status = keydb_Get(handle, &namekey, &dummy, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1166 /* some databases have the key stored as a signed value */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1167 if (status) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1168 unsigned char *buf = (unsigned char *)PORT_Alloc(namekey.size+1);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1169 if (buf) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1170 PORT_Memcpy(&buf[1], namekey.data, namekey.size);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1171 buf[0] = 0;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1172 namekey.data = buf;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1173 namekey.size ++;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1174 status = keydb_Get(handle, &namekey, &dummy, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1175 PORT_Free(buf);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1176 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1177 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1178 lg_nsslowkey_DestroyPublicKey(pubkey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1179 if ( status ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1180 return PR_FALSE;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1181 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1182
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1183 return PR_TRUE;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1184 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1185
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1186 typedef struct NSSLowPasswordDataParamStr {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1187 SECItem salt;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1188 SECItem iter;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1189 } NSSLowPasswordDataParam;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1190
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1191 static const SEC_ASN1Template NSSLOWPasswordParamTemplate[] =
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1192 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1193 {SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSSLowPasswordDataParam) },
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1194 {SEC_ASN1_OCTET_STRING, offsetof(NSSLowPasswordDataParam, salt) },
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1195 {SEC_ASN1_INTEGER, offsetof(NSSLowPasswordDataParam, iter) },
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1196 {0}
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1197 };
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1198 struct LGEncryptedDataInfoStr {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1199 SECAlgorithmID algorithm;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1200 SECItem encryptedData;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1201 };
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1202 typedef struct LGEncryptedDataInfoStr LGEncryptedDataInfo;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1203
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1204 const SEC_ASN1Template lg_EncryptedDataInfoTemplate[] = {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1205 { SEC_ASN1_SEQUENCE,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1206 0, NULL, sizeof(LGEncryptedDataInfo) },
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1207 { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1208 offsetof(LGEncryptedDataInfo,algorithm),
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1209 SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1210 { SEC_ASN1_OCTET_STRING,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1211 offsetof(LGEncryptedDataInfo,encryptedData) },
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1212 { 0 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1213 };
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1214
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1215 static SECItem *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1216 nsslowkey_EncodePW(SECOidTag alg, const SECItem *salt, SECItem *data)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1217 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1218 NSSLowPasswordDataParam param;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1219 LGEncryptedDataInfo edi;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1220 PLArenaPool *arena;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1221 unsigned char one = 1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1222 SECItem *epw = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1223 SECItem *encParam;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1224 SECStatus rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1225
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1226 param.salt = *salt;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1227 param.iter.type = siBuffer; /* encode as signed integer */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1228 param.iter.data = &one;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1229 param.iter.len = 1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1230 edi.encryptedData = *data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1231
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1232 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1233 if (arena == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1234 return NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1235 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1236
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1237 encParam = SEC_ASN1EncodeItem(arena, NULL, &param,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1238 NSSLOWPasswordParamTemplate);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1239 if (encParam == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1240 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1241 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1242 rv = SECOID_SetAlgorithmID(arena, &edi.algorithm, alg, encParam);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1243 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1244 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1245 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1246 epw = SEC_ASN1EncodeItem(NULL, NULL, &edi, lg_EncryptedDataInfoTemplate);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1247
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1248 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1249 PORT_FreeArena(arena, PR_FALSE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1250 return epw;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1251 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1252
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1253 static SECItem *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1254 nsslowkey_DecodePW(const SECItem *derData, SECOidTag *alg, SECItem *salt)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1255 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1256 NSSLowPasswordDataParam param;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1257 LGEncryptedDataInfo edi;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1258 PLArenaPool *arena;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1259 SECItem *pwe = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1260 SECStatus rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1261
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1262 salt->data = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1263 param.iter.type = siBuffer; /* decode as signed integer */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1264
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1265 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1266 if (arena == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1267 return NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1268 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1269
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1270 rv = SEC_QuickDERDecodeItem(arena, &edi, lg_EncryptedDataInfoTemplate,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1271 derData);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1272 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1273 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1274 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1275 *alg = SECOID_GetAlgorithmTag(&edi.algorithm);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1276 rv = SEC_QuickDERDecodeItem(arena, &param, NSSLOWPasswordParamTemplate,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1277 &edi.algorithm.parameters);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1278 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1279 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1280 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1281 rv = SECITEM_CopyItem(NULL, salt, &param.salt);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1282 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1283 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1284 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1285 pwe = SECITEM_DupItem(&edi.encryptedData);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1286
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1287 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1288 if (!pwe && salt->data) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1289 PORT_Free(salt->data);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1290 salt->data = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1291 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1292 PORT_FreeArena(arena, PR_FALSE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1293 return pwe;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1294 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1295
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1296
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1297 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1298 * check to see if the user has a password
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1299 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1300 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1301 nsslowkey_GetPWCheckEntry(NSSLOWKEYDBHandle *handle,NSSLOWKEYPasswordEntry *entry)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1302 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1303 DBT checkkey; /*, checkdata; */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1304 NSSLOWKEYDBKey *dbkey = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1305 SECItem *global_salt = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1306 SECItem *item = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1307 SECItem entryData, oid;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1308 SECItem none = { siBuffer, NULL, 0 };
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1309 SECStatus rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1310 SECOidTag algorithm;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1311
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1312 if (handle == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1313 /* PORT_SetError */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1314 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1315 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1316
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1317 global_salt = GetKeyDBGlobalSalt(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1318 if (!global_salt) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1319 global_salt = &none;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1320 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1321 if (global_salt->len > sizeof(entry->data)) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1322 /* PORT_SetError */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1323 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1324 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1325
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1326 PORT_Memcpy(entry->data, global_salt->data, global_salt->len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1327 entry->salt.data = entry->data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1328 entry->salt.len = global_salt->len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1329 entry->value.data = &entry->data[entry->salt.len];
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1330
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1331 checkkey.data = KEYDB_PW_CHECK_STRING;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1332 checkkey.size = KEYDB_PW_CHECK_LEN;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1333 dbkey = get_dbkey(handle, &checkkey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1334 if (dbkey == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1335 /* handle 'FAKE' check here */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1336 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1337 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1338
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1339 oid.len = dbkey->derPK.data[0];
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1340 oid.data = &dbkey->derPK.data[1];
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1341
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1342 if (dbkey->derPK.len < (KEYDB_PW_CHECK_LEN + 1 +oid.len)) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1343 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1344 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1345 algorithm = SECOID_FindOIDTag(&oid);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1346 entryData.type = siBuffer;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1347 entryData.len = dbkey->derPK.len - (oid.len+1);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1348 entryData.data = &dbkey->derPK.data[oid.len+1];
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1349
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1350 item = nsslowkey_EncodePW(algorithm, &dbkey->salt, &entryData);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1351 if (!item || (item->len + entry->salt.len) > sizeof(entry->data)) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1352 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1353 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1354 PORT_Memcpy(entry->value.data, item->data, item->len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1355 entry->value.len = item->len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1356 rv = SECSuccess;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1357
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1358 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1359 if (item) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1360 SECITEM_FreeItem(item, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1361 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1362 if (dbkey) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1363 sec_destroy_dbkey(dbkey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1364 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1365 if (global_salt != &none) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1366 SECITEM_FreeItem(global_salt,PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1367 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1368 return rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1369 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1370
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1371 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1372 * check to see if the user has a password
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1373 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1374 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1375 nsslowkey_PutPWCheckEntry(NSSLOWKEYDBHandle *handle,NSSLOWKEYPasswordEntry *entry)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1376 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1377 DBT checkkey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1378 NSSLOWKEYDBKey *dbkey = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1379 SECItem *item = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1380 SECItem salt;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1381 SECOidTag algid;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1382 SECStatus rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1383 PLArenaPool *arena;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1384 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1385
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1386 if (handle == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1387 /* PORT_SetError */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1388 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1389 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1390
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1391 checkkey.data = KEYDB_PW_CHECK_STRING;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1392 checkkey.size = KEYDB_PW_CHECK_LEN;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1393
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1394 salt.data = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1395 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1396 if (arena == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1397 return SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1398 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1399
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1400 item = nsslowkey_DecodePW(&entry->value, &algid, &salt);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1401 if (item == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1402 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1403 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1404
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1405 dbkey = PORT_ArenaZNew(arena, NSSLOWKEYDBKey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1406 if (dbkey == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1407 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1408 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1409
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1410 dbkey->arena = arena;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1411
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1412 rv = SECITEM_CopyItem(arena, &dbkey->salt, &salt);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1413 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1414 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1415 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1416
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1417 rv = encodePWCheckEntry(arena, &dbkey->derPK, algid, item);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1418 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1419 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1420 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1421
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1422 rv = put_dbkey(handle, &checkkey, dbkey, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1423 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1424 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1425 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1426
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1427 if (handle->global_salt) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1428 SECITEM_FreeItem(handle->global_salt, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1429 handle->global_salt = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1430 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1431 rv = StoreKeyDBGlobalSalt(handle, &entry->salt);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1432 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1433 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1434 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1435 ret = keydb_Sync(handle, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1436 if ( ret ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1437 rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1438 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1439 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1440 handle->global_salt = GetKeyDBGlobalSalt(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1441
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1442 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1443 if (item) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1444 SECITEM_FreeItem(item, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1445 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1446 if (arena) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1447 PORT_FreeArena(arena, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1448 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1449 if (salt.data) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1450 PORT_Free(salt.data);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1451 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1452 return rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1453 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1454
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1455 #ifdef EC_DEBUG
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1456 #define SEC_PRINT(str1, str2, num, sitem) \
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1457 printf("pkcs11c.c:%s:%s (keytype=%d) [len=%d]\n", \
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1458 str1, str2, num, sitem->len); \
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1459 for (i = 0; i < sitem->len; i++) { \
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1460 printf("%02x:", sitem->data[i]); \
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1461 } \
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1462 printf("\n")
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1463 #else
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1464 #define SEC_PRINT(a, b, c, d)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1465 #endif /* EC_DEBUG */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1466
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1467
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1468 SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1469 seckey_encrypt_private_key( PLArenaPool *permarena, NSSLOWKEYPrivateKey *pk,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1470 SDB *sdbpw, SECItem *result)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1471 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1472 NSSLOWKEYPrivateKeyInfo *pki = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1473 SECStatus rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1474 PLArenaPool *temparena = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1475 SECItem *der_item = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1476 SECItem *cipherText = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1477 SECItem *dummy = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1478 #ifndef NSS_DISABLE_ECC
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1479 SECItem *fordebug = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1480 int savelen;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1481 #endif
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1482
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1483 temparena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1484 if(temparena == NULL)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1485 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1486
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1487 /* allocate structures */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1488 pki = (NSSLOWKEYPrivateKeyInfo *)PORT_ArenaZAlloc(temparena,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1489 sizeof(NSSLOWKEYPrivateKeyInfo));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1490 der_item = (SECItem *)PORT_ArenaZAlloc(temparena, sizeof(SECItem));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1491 if((pki == NULL) || (der_item == NULL))
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1492 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1493
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1494
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1495 /* setup private key info */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1496 dummy = SEC_ASN1EncodeInteger(temparena, &(pki->version),
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1497 NSSLOWKEY_PRIVATE_KEY_INFO_VERSION);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1498 if(dummy == NULL)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1499 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1500
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1501 /* Encode the key, and set the algorithm (with params) */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1502 switch (pk->keyType) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1503 case NSSLOWKEYRSAKey:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1504 lg_prepare_low_rsa_priv_key_for_asn1(pk);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1505 dummy = SEC_ASN1EncodeItem(temparena, &(pki->privateKey), pk,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1506 lg_nsslowkey_RSAPrivateKeyTemplate);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1507 if (dummy == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1508 rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1509 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1510 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1511
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1512 rv = SECOID_SetAlgorithmID(temparena, &(pki->algorithm),
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1513 SEC_OID_PKCS1_RSA_ENCRYPTION, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1514 if (rv == SECFailure) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1515 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1516 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1517
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1518 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1519 case NSSLOWKEYDSAKey:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1520 lg_prepare_low_dsa_priv_key_for_asn1(pk);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1521 dummy = SEC_ASN1EncodeItem(temparena, &(pki->privateKey), pk,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1522 lg_nsslowkey_DSAPrivateKeyTemplate);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1523 if (dummy == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1524 rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1525 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1526 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1527
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1528 lg_prepare_low_pqg_params_for_asn1(&pk->u.dsa.params);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1529 dummy = SEC_ASN1EncodeItem(temparena, NULL, &pk->u.dsa.params,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1530 lg_nsslowkey_PQGParamsTemplate);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1531 if (dummy == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1532 rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1533 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1534 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1535
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1536 rv = SECOID_SetAlgorithmID(temparena, &(pki->algorithm),
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1537 SEC_OID_ANSIX9_DSA_SIGNATURE, dummy);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1538 if (rv == SECFailure) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1539 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1540 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1541
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1542 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1543 case NSSLOWKEYDHKey:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1544 lg_prepare_low_dh_priv_key_for_asn1(pk);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1545 dummy = SEC_ASN1EncodeItem(temparena, &(pki->privateKey), pk,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1546 lg_nsslowkey_DHPrivateKeyTemplate);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1547 if (dummy == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1548 rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1549 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1550 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1551
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1552 rv = SECOID_SetAlgorithmID(temparena, &(pki->algorithm),
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1553 SEC_OID_X942_DIFFIE_HELMAN_KEY, dummy);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1554 if (rv == SECFailure) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1555 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1556 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1557 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1558 #ifndef NSS_DISABLE_ECC
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1559 case NSSLOWKEYECKey:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1560 lg_prepare_low_ec_priv_key_for_asn1(pk);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1561 /* Public value is encoded as a bit string so adjust length
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1562 * to be in bits before ASN encoding and readjust
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1563 * immediately after.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1564 *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1565 * Since the SECG specification recommends not including the
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1566 * parameters as part of ECPrivateKey, we zero out the curveOID
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1567 * length before encoding and restore it later.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1568 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1569 pk->u.ec.publicValue.len <<= 3;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1570 savelen = pk->u.ec.ecParams.curveOID.len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1571 pk->u.ec.ecParams.curveOID.len = 0;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1572 dummy = SEC_ASN1EncodeItem(temparena, &(pki->privateKey), pk,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1573 lg_nsslowkey_ECPrivateKeyTemplate);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1574 pk->u.ec.ecParams.curveOID.len = savelen;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1575 pk->u.ec.publicValue.len >>= 3;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1576
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1577 if (dummy == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1578 rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1579 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1580 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1581
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1582 dummy = &pk->u.ec.ecParams.DEREncoding;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1583
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1584 /* At this point dummy should contain the encoded params */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1585 rv = SECOID_SetAlgorithmID(temparena, &(pki->algorithm),
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1586 SEC_OID_ANSIX962_EC_PUBLIC_KEY, dummy);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1587
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1588 if (rv == SECFailure) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1589 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1590 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1591
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1592 fordebug = &(pki->privateKey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1593 SEC_PRINT("seckey_encrypt_private_key()", "PrivateKey",
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1594 pk->keyType, fordebug);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1595
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1596 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1597 #endif /* NSS_DISABLE_ECC */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1598 default:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1599 /* We don't support DH or Fortezza private keys yet */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1600 PORT_Assert(PR_FALSE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1601 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1602 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1603
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1604 /* setup encrypted private key info */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1605 dummy = SEC_ASN1EncodeItem(temparena, der_item, pki,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1606 lg_nsslowkey_PrivateKeyInfoTemplate);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1607
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1608 SEC_PRINT("seckey_encrypt_private_key()", "PrivateKeyInfo",
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1609 pk->keyType, der_item);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1610
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1611 if(dummy == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1612 rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1613 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1614 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1615
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1616 rv = lg_util_encrypt(temparena, sdbpw, dummy, &cipherText);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1617 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1618 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1619 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1620
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1621 rv = SECITEM_CopyItem ( permarena, result, cipherText);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1622
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1623 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1624
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1625 if(temparena != NULL)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1626 PORT_FreeArena(temparena, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1627
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1628 return rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1629 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1630
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1631 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1632 seckey_put_private_key(NSSLOWKEYDBHandle *keydb, DBT *index, SDB *sdbpw,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1633 NSSLOWKEYPrivateKey *pk, char *nickname, PRBool update)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1634 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1635 NSSLOWKEYDBKey *dbkey = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1636 PLArenaPool *arena = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1637 SECStatus rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1638
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1639 if((keydb == NULL) || (index == NULL) || (sdbpw == NULL) ||
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1640 (pk == NULL))
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1641 return SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1642
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1643 arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1644 if(arena == NULL)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1645 return SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1646
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1647 dbkey = (NSSLOWKEYDBKey *)PORT_ArenaZAlloc(arena, sizeof(NSSLOWKEYDBKey));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1648 if(dbkey == NULL)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1649 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1650 dbkey->arena = arena;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1651 dbkey->nickname = nickname;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1652
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1653 rv = seckey_encrypt_private_key(arena, pk, sdbpw, &dbkey->derPK);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1654 if(rv != SECSuccess)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1655 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1656
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1657 rv = put_dbkey(keydb, index, dbkey, update);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1658
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1659 /* let success fall through */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1660 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1661 if(arena != NULL)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1662 PORT_FreeArena(arena, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1663
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1664 return rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1665 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1666
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1667 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1668 * Store a key in the database, indexed by its public key modulus.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1669 * Note that the nickname is optional. It was only used by keyutil.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1670 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1671 SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1672 nsslowkey_StoreKeyByPublicKeyAlg(NSSLOWKEYDBHandle *handle,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1673 NSSLOWKEYPrivateKey *privkey,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1674 SECItem *pubKeyData,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1675 char *nickname,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1676 SDB *sdbpw,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1677 PRBool update)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1678 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1679 DBT namekey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1680 SECStatus rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1681
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1682 if (handle == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1683 PORT_SetError(SEC_ERROR_BAD_DATABASE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1684 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1685 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1686
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1687 /* set up db key and data */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1688 namekey.data = pubKeyData->data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1689 namekey.size = pubKeyData->len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1690
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1691 /* encrypt the private key */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1692 rv = seckey_put_private_key(handle, &namekey, sdbpw, privkey, nickname,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1693 update);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1694
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1695 return(rv);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1696 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1697
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1698 static NSSLOWKEYPrivateKey *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1699 seckey_decrypt_private_key(SECItem*epki,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1700 SDB *sdbpw)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1701 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1702 NSSLOWKEYPrivateKey *pk = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1703 NSSLOWKEYPrivateKeyInfo *pki = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1704 SECStatus rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1705 PLArenaPool *temparena = NULL, *permarena = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1706 SECItem *dest = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1707 #ifndef NSS_DISABLE_ECC
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1708 SECItem *fordebug = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1709 #endif
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1710
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1711 if((epki == NULL) || (sdbpw == NULL))
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1712 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1713
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1714 temparena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1715 permarena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1716 if((temparena == NULL) || (permarena == NULL))
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1717 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1718
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1719 /* allocate temporary items */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1720 pki = (NSSLOWKEYPrivateKeyInfo *)PORT_ArenaZAlloc(temparena,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1721 sizeof(NSSLOWKEYPrivateKeyInfo));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1722
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1723 /* allocate permanent arena items */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1724 pk = (NSSLOWKEYPrivateKey *)PORT_ArenaZAlloc(permarena,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1725 sizeof(NSSLOWKEYPrivateKey));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1726
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1727 if((pk == NULL) || (pki == NULL))
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1728 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1729
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1730 pk->arena = permarena;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1731
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1732 rv = lg_util_decrypt(sdbpw, epki, &dest);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1733 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1734 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1735 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1736
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1737 if(dest != NULL)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1738 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1739 SECItem newPrivateKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1740 SECItem newAlgParms;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1741
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1742 SEC_PRINT("seckey_decrypt_private_key()", "PrivateKeyInfo", -1,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1743 dest);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1744
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1745 rv = SEC_QuickDERDecodeItem(temparena, pki,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1746 lg_nsslowkey_PrivateKeyInfoTemplate, dest);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1747 if(rv == SECSuccess)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1748 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1749 switch(SECOID_GetAlgorithmTag(&pki->algorithm)) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1750 case SEC_OID_X500_RSA_ENCRYPTION:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1751 case SEC_OID_PKCS1_RSA_ENCRYPTION:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1752 pk->keyType = NSSLOWKEYRSAKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1753 lg_prepare_low_rsa_priv_key_for_asn1(pk);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1754 if (SECSuccess != SECITEM_CopyItem(permarena, &newPrivateKey,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1755 &pki->privateKey) ) break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1756 rv = SEC_QuickDERDecodeItem(permarena, pk,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1757 lg_nsslowkey_RSAPrivateKeyTemplate,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1758 &newPrivateKey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1759 if (rv == SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1760 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1761 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1762 /* Try decoding with the alternative template, but only allow
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1763 * a zero-length modulus for a secret key object.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1764 * See bug 715073.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1765 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1766 rv = SEC_QuickDERDecodeItem(permarena, pk,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1767 lg_nsslowkey_RSAPrivateKeyTemplate2,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1768 &newPrivateKey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1769 /* A publicExponent of 0 is the defining property of a secret
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1770 * key disguised as an RSA key. When decoding with the
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1771 * alternative template, only accept a secret key with an
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1772 * improperly encoded modulus and a publicExponent of 0.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1773 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1774 if (rv == SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1775 if (pk->u.rsa.modulus.len == 2 &&
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1776 pk->u.rsa.modulus.data[0] == SEC_ASN1_INTEGER &&
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1777 pk->u.rsa.modulus.data[1] == 0 &&
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1778 pk->u.rsa.publicExponent.len == 1 &&
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1779 pk->u.rsa.publicExponent.data[0] == 0) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1780 /* Fix the zero-length integer by setting it to 0. */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1781 pk->u.rsa.modulus.data = pk->u.rsa.publicExponent.data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1782 pk->u.rsa.modulus.len = pk->u.rsa.publicExponent.len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1783 } else {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1784 PORT_SetError(SEC_ERROR_BAD_DER);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1785 rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1786 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1787 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1788 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1789 case SEC_OID_ANSIX9_DSA_SIGNATURE:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1790 pk->keyType = NSSLOWKEYDSAKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1791 lg_prepare_low_dsa_priv_key_for_asn1(pk);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1792 if (SECSuccess != SECITEM_CopyItem(permarena, &newPrivateKey,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1793 &pki->privateKey) ) break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1794 rv = SEC_QuickDERDecodeItem(permarena, pk,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1795 lg_nsslowkey_DSAPrivateKeyTemplate,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1796 &newPrivateKey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1797 if (rv != SECSuccess)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1798 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1799 lg_prepare_low_pqg_params_for_asn1(&pk->u.dsa.params);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1800 if (SECSuccess != SECITEM_CopyItem(permarena, &newAlgParms,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1801 &pki->algorithm.parameters) ) break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1802 rv = SEC_QuickDERDecodeItem(permarena, &pk->u.dsa.params,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1803 lg_nsslowkey_PQGParamsTemplate,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1804 &newAlgParms);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1805 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1806 case SEC_OID_X942_DIFFIE_HELMAN_KEY:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1807 pk->keyType = NSSLOWKEYDHKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1808 lg_prepare_low_dh_priv_key_for_asn1(pk);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1809 if (SECSuccess != SECITEM_CopyItem(permarena, &newPrivateKey,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1810 &pki->privateKey) ) break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1811 rv = SEC_QuickDERDecodeItem(permarena, pk,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1812 lg_nsslowkey_DHPrivateKeyTemplate,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1813 &newPrivateKey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1814 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1815 #ifndef NSS_DISABLE_ECC
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1816 case SEC_OID_ANSIX962_EC_PUBLIC_KEY:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1817 pk->keyType = NSSLOWKEYECKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1818 lg_prepare_low_ec_priv_key_for_asn1(pk);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1819
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1820 fordebug = &pki->privateKey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1821 SEC_PRINT("seckey_decrypt_private_key()", "PrivateKey",
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1822 pk->keyType, fordebug);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1823 if (SECSuccess != SECITEM_CopyItem(permarena, &newPrivateKey,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1824 &pki->privateKey) ) break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1825 rv = SEC_QuickDERDecodeItem(permarena, pk,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1826 lg_nsslowkey_ECPrivateKeyTemplate,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1827 &newPrivateKey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1828 if (rv != SECSuccess)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1829 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1830
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1831 lg_prepare_low_ecparams_for_asn1(&pk->u.ec.ecParams);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1832
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1833 rv = SECITEM_CopyItem(permarena,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1834 &pk->u.ec.ecParams.DEREncoding,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1835 &pki->algorithm.parameters);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1836
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1837 if (rv != SECSuccess)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1838 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1839
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1840 /* Fill out the rest of EC params */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1841 rv = LGEC_FillParams(permarena, &pk->u.ec.ecParams.DEREncoding,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1842 &pk->u.ec.ecParams);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1843
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1844 if (rv != SECSuccess)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1845 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1846
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1847 if (pk->u.ec.publicValue.len != 0) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1848 pk->u.ec.publicValue.len >>= 3;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1849 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1850
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1851 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1852 #endif /* NSS_DISABLE_ECC */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1853 default:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1854 rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1855 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1856 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1857 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1858 else if(PORT_GetError() == SEC_ERROR_BAD_DER)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1859 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1860 PORT_SetError(SEC_ERROR_BAD_PASSWORD);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1861 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1862 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1863 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1864
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1865 /* let success fall through */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1866 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1867 if(temparena != NULL)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1868 PORT_FreeArena(temparena, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1869 if(dest != NULL)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1870 SECITEM_ZfreeItem(dest, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1871
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1872 if(rv != SECSuccess)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1873 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1874 if(permarena != NULL)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1875 PORT_FreeArena(permarena, PR_TRUE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1876 pk = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1877 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1878
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1879 return pk;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1880 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1881
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1882 static NSSLOWKEYPrivateKey *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1883 seckey_decode_encrypted_private_key(NSSLOWKEYDBKey *dbkey, SDB *sdbpw)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1884 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1885 if( ( dbkey == NULL ) || ( sdbpw == NULL ) ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1886 return NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1887 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1888
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1889 return seckey_decrypt_private_key(&(dbkey->derPK), sdbpw);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1890 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1891
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1892 static NSSLOWKEYPrivateKey *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1893 seckey_get_private_key(NSSLOWKEYDBHandle *keydb, DBT *index, char **nickname,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1894 SDB *sdbpw)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1895 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1896 NSSLOWKEYDBKey *dbkey = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1897 NSSLOWKEYPrivateKey *pk = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1898
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1899 if( ( keydb == NULL ) || ( index == NULL ) || ( sdbpw == NULL ) ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1900 return NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1901 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1902
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1903 dbkey = get_dbkey(keydb, index);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1904 if(dbkey == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1905 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1906 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1907
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1908 if ( nickname ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1909 if ( dbkey->nickname && ( dbkey->nickname[0] != 0 ) ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1910 *nickname = PORT_Strdup(dbkey->nickname);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1911 } else {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1912 *nickname = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1913 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1914 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1915
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1916 pk = seckey_decode_encrypted_private_key(dbkey, sdbpw);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1917
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1918 /* let success fall through */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1919 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1920
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1921 if ( dbkey != NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1922 sec_destroy_dbkey(dbkey);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1923 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1924
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1925 return pk;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1926 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1927
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1928 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1929 * Find a key in the database, indexed by its public key modulus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1930 * This is used to find keys that have been stored before their
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1931 * certificate arrives. Once the certificate arrives the key
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1932 * is looked up by the public modulus in the certificate, and the
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1933 * re-stored by its nickname.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1934 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1935 NSSLOWKEYPrivateKey *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1936 nsslowkey_FindKeyByPublicKey(NSSLOWKEYDBHandle *handle, SECItem *modulus,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1937 SDB *sdbpw)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1938 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1939 DBT namekey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1940 NSSLOWKEYPrivateKey *pk = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1941
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1942 if (handle == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1943 PORT_SetError(SEC_ERROR_BAD_DATABASE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1944 return NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1945 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1946
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1947 /* set up db key */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1948 namekey.data = modulus->data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1949 namekey.size = modulus->len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1950
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1951 pk = seckey_get_private_key(handle, &namekey, NULL, sdbpw);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1952
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1953 /* no need to free dbkey, since its on the stack, and the data it
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1954 * points to is owned by the database
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1955 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1956 return(pk);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1957 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1958
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1959 char *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1960 nsslowkey_FindKeyNicknameByPublicKey(NSSLOWKEYDBHandle *handle,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1961 SECItem *modulus, SDB *sdbpw)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1962 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1963 DBT namekey;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1964 NSSLOWKEYPrivateKey *pk = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1965 char *nickname = NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1966
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1967 if (handle == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1968 PORT_SetError(SEC_ERROR_BAD_DATABASE);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1969 return NULL;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1970 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1971
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1972 /* set up db key */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1973 namekey.data = modulus->data;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1974 namekey.size = modulus->len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1975
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1976 pk = seckey_get_private_key(handle, &namekey, &nickname, sdbpw);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1977 if (pk) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1978 lg_nsslowkey_DestroyPrivateKey(pk);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1979 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1980
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1981 /* no need to free dbkey, since its on the stack, and the data it
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1982 * points to is owned by the database
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1983 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1984 return(nickname);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1985 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1986 /* ===== ENCODING ROUTINES ===== */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1987
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1988 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1989 encodePWCheckEntry(PLArenaPool *arena, SECItem *entry, SECOidTag alg,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1990 SECItem *encCheck)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1991 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1992 SECOidData *oidData;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1993 SECStatus rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1994
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1995 oidData = SECOID_FindOIDByTag(alg);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1996 if ( oidData == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1997 rv = SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1998 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1999 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2000
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2001 entry->len = 1 + oidData->oid.len + encCheck->len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2002 if ( arena ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2003 entry->data = (unsigned char *)PORT_ArenaAlloc(arena, entry->len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2004 } else {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2005 entry->data = (unsigned char *)PORT_Alloc(entry->len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2006 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2007
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2008 if ( entry->data == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2009 goto loser;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2010 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2011
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2012 /* first length of oid */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2013 entry->data[0] = (unsigned char)oidData->oid.len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2014 /* next oid itself */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2015 PORT_Memcpy(&entry->data[1], oidData->oid.data, oidData->oid.len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2016 /* finally the encrypted check string */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2017 PORT_Memcpy(&entry->data[1+oidData->oid.len], encCheck->data,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2018 encCheck->len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2019
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2020 return(SECSuccess);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2021
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2022 loser:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2023 return(SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2024 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2025
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2026
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2027 #define MAX_DB_SIZE 0xffff
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2028 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2029 * Clear out all the keys in the existing database
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2030 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2031 static SECStatus
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2032 nsslowkey_ResetKeyDB(NSSLOWKEYDBHandle *handle)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2033 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2034 SECStatus rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2035 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2036 int errors = 0;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2037
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2038 if ( handle->db == NULL ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2039 return(SECSuccess);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2040 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2041
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2042 if (handle->readOnly) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2043 /* set an error code */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2044 return SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2045 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2046
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2047 if (handle->appname == NULL && handle->dbname == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2048 return SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2049 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2050
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2051 keydb_Close(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2052 if (handle->appname) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2053 handle->db=
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2054 rdbopen(handle->appname, handle->dbname, "key", NO_CREATE, NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2055 } else {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2056 handle->db = dbopen( handle->dbname, NO_CREATE, 0600, DB_HASH, 0 );
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2057 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2058 if (handle->db == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2059 /* set an error code */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2060 return SECFailure;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2061 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2062
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2063 rv = makeGlobalVersion(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2064 if ( rv != SECSuccess ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2065 errors++;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2066 goto done;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2067 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2068
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2069 if (handle->global_salt) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2070 rv = StoreKeyDBGlobalSalt(handle, handle->global_salt);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2071 } else {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2072 rv = makeGlobalSalt(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2073 if ( rv == SECSuccess ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2074 handle->global_salt = GetKeyDBGlobalSalt(handle);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2075 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2076 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2077 if ( rv != SECSuccess ) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2078 errors++;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2079 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2080
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2081 done:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2082 /* sync the database */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2083 ret = keydb_Sync(handle, 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2084 db_InitComplete(handle->db);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2085
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2086 return (errors == 0 ? SECSuccess : SECFailure);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2087 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2088
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2089 static int
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2090 keydb_Get(NSSLOWKEYDBHandle *kdb, DBT *key, DBT *data, unsigned int flags)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2091 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2092 PRStatus prstat;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2093 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2094 PRLock *kdbLock = kdb->lock;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2095 DB *db = kdb->db;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2096
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2097 PORT_Assert(kdbLock != NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2098 PZ_Lock(kdbLock);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2099
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2100 ret = (* db->get)(db, key, data, flags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2101
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2102 prstat = PZ_Unlock(kdbLock);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2103
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2104 return(ret);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2105 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2106
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2107 static int
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2108 keydb_Put(NSSLOWKEYDBHandle *kdb, DBT *key, DBT *data, unsigned int flags)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2109 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2110 PRStatus prstat;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2111 int ret = 0;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2112 PRLock *kdbLock = kdb->lock;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2113 DB *db = kdb->db;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2114
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2115 PORT_Assert(kdbLock != NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2116 PZ_Lock(kdbLock);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2117
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2118 ret = (* db->put)(db, key, data, flags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2119
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2120 prstat = PZ_Unlock(kdbLock);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2121
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2122 return(ret);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2123 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2124
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2125 static int
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2126 keydb_Sync(NSSLOWKEYDBHandle *kdb, unsigned int flags)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2127 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2128 PRStatus prstat;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2129 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2130 PRLock *kdbLock = kdb->lock;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2131 DB *db = kdb->db;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2132
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2133 PORT_Assert(kdbLock != NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2134 PZ_Lock(kdbLock);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2135
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2136 ret = (* db->sync)(db, flags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2137
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2138 prstat = PZ_Unlock(kdbLock);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2139
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2140 return(ret);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2141 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2142
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2143 static int
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2144 keydb_Del(NSSLOWKEYDBHandle *kdb, DBT *key, unsigned int flags)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2145 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2146 PRStatus prstat;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2147 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2148 PRLock *kdbLock = kdb->lock;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2149 DB *db = kdb->db;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2150
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2151 PORT_Assert(kdbLock != NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2152 PZ_Lock(kdbLock);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2153
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2154 ret = (* db->del)(db, key, flags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2155
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2156 prstat = PZ_Unlock(kdbLock);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2157
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2158 return(ret);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2159 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2160
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2161 static int
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2162 keydb_Seq(NSSLOWKEYDBHandle *kdb, DBT *key, DBT *data, unsigned int flags)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2163 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2164 PRStatus prstat;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2165 int ret;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2166 PRLock *kdbLock = kdb->lock;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2167 DB *db = kdb->db;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2168
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2169 PORT_Assert(kdbLock != NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2170 PZ_Lock(kdbLock);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2171
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2172 ret = (* db->seq)(db, key, data, flags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2173
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2174 prstat = PZ_Unlock(kdbLock);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2175
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2176 return(ret);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2177 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2178
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2179 static void
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2180 keydb_Close(NSSLOWKEYDBHandle *kdb)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2181 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2182 PRStatus prstat;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2183 PRLock *kdbLock = kdb->lock;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2184 DB *db = kdb->db;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2185
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2186 PORT_Assert(kdbLock != NULL);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2187 SKIP_AFTER_FORK(PZ_Lock(kdbLock));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2188
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2189 (* db->close)(db);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2190
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2191 SKIP_AFTER_FORK(prstat = PZ_Unlock(kdbLock));
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2192
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2193 return;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2194 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2195
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2196 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2197 * SDB Entry Points for the Key DB
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2198 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2199
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2200 CK_RV
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2201 lg_GetMetaData(SDB *sdb, const char *id, SECItem *item1, SECItem *item2)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2202 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2203 NSSLOWKEYDBHandle *keydb;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2204 NSSLOWKEYPasswordEntry entry;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2205 SECStatus rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2206
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2207 keydb = lg_getKeyDB(sdb);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2208 if (keydb == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2209 return CKR_TOKEN_WRITE_PROTECTED;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2210 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2211 if (PORT_Strcmp(id,"password") != 0) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2212 /* shouldn't happen */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2213 return CKR_GENERAL_ERROR; /* no extra data stored */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2214 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2215 rv = nsslowkey_GetPWCheckEntry(keydb, &entry);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2216 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2217 return CKR_GENERAL_ERROR;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2218 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2219 item1->len = entry.salt.len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2220 PORT_Memcpy(item1->data, entry.salt.data, item1->len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2221 item2->len = entry.value.len;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2222 PORT_Memcpy(item2->data, entry.value.data, item2->len);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2223 return CKR_OK;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2224 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2225
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2226 CK_RV
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2227 lg_PutMetaData(SDB *sdb, const char *id,
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2228 const SECItem *item1, const SECItem *item2)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2229 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2230 NSSLOWKEYDBHandle *keydb;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2231 NSSLOWKEYPasswordEntry entry;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2232 SECStatus rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2233
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2234 keydb = lg_getKeyDB(sdb);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2235 if (keydb == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2236 return CKR_TOKEN_WRITE_PROTECTED;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2237 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2238 if (PORT_Strcmp(id,"password") != 0) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2239 /* shouldn't happen */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2240 return CKR_GENERAL_ERROR; /* no extra data stored */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2241 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2242 entry.salt = *item1;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2243 entry.value = *item2;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2244 rv = nsslowkey_PutPWCheckEntry(keydb, &entry);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2245 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2246 return CKR_GENERAL_ERROR;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2247 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2248 return CKR_OK;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2249 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2250
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2251 CK_RV
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2252 lg_Reset(SDB *sdb)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2253 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2254 NSSLOWKEYDBHandle *keydb;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2255 SECStatus rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2256
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2257 keydb = lg_getKeyDB(sdb);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2258 if (keydb == NULL) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2259 return CKR_TOKEN_WRITE_PROTECTED;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2260 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2261 rv = nsslowkey_ResetKeyDB(keydb);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2262 if (rv != SECSuccess) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2263 return CKR_GENERAL_ERROR;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2264 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2265 return CKR_OK;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2266 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2267
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)