PageRenderTime 79ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/security/nss/lib/softoken/legacydb/pcertdb.c

http://github.com/zpao/v8monkey
C | 5388 lines | 3939 code | 871 blank | 578 comment | 914 complexity | cb1cf42a254592733b815c8c285e0205 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is the Netscape security libraries.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Netscape Communications Corporation.
  18. * Portions created by the Initial Developer are Copyright (C) 1994-2000
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of
  24. * either the GNU General Public License Version 2 or later (the "GPL"), or
  25. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. * in which case the provisions of the GPL or the LGPL are applicable instead
  27. * of those above. If you wish to allow use of your version of this file only
  28. * under the terms of either the GPL or the LGPL, and not to allow others to
  29. * use your version of this file under the terms of the MPL, indicate your
  30. * decision by deleting the provisions above and replace them with the notice
  31. * and other provisions required by the GPL or the LGPL. If you do not delete
  32. * the provisions above, a recipient may use your version of this file under
  33. * the terms of any one of the MPL, the GPL or the LGPL.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. /*
  37. * Permanent Certificate database handling code
  38. *
  39. * $Id: pcertdb.c,v 1.12 2010/07/20 01:26:04 wtc%google.com Exp $
  40. */
  41. #include "lowkeyti.h"
  42. #include "pcert.h"
  43. #include "mcom_db.h"
  44. #include "pcert.h"
  45. #include "secitem.h"
  46. #include "secder.h"
  47. #include "secerr.h"
  48. #include "lgdb.h"
  49. /* forward declaration */
  50. NSSLOWCERTCertificate *
  51. nsslowcert_FindCertByDERCertNoLocking(NSSLOWCERTCertDBHandle *handle, SECItem *derCert);
  52. static SECStatus
  53. nsslowcert_UpdateSMimeProfile(NSSLOWCERTCertDBHandle *dbhandle,
  54. char *emailAddr, SECItem *derSubject, SECItem *emailProfile,
  55. SECItem *profileTime);
  56. static SECStatus
  57. nsslowcert_UpdatePermCert(NSSLOWCERTCertDBHandle *dbhandle,
  58. NSSLOWCERTCertificate *cert, char *nickname, NSSLOWCERTCertTrust *trust);
  59. static SECStatus
  60. nsslowcert_UpdateCrl(NSSLOWCERTCertDBHandle *handle, SECItem *derCrl,
  61. SECItem *crlKey, char *url, PRBool isKRL);
  62. static NSSLOWCERTCertificate *certListHead = NULL;
  63. static NSSLOWCERTTrust *trustListHead = NULL;
  64. static certDBEntryCert *entryListHead = NULL;
  65. static int certListCount = 0;
  66. static int trustListCount = 0;
  67. static int entryListCount = 0;
  68. #define MAX_CERT_LIST_COUNT 10
  69. #define MAX_TRUST_LIST_COUNT 10
  70. #define MAX_ENTRY_LIST_COUNT 10
  71. /*
  72. * the following functions are wrappers for the db library that implement
  73. * a global lock to make the database thread safe.
  74. */
  75. static PZLock *dbLock = NULL;
  76. static PZLock *certRefCountLock = NULL;
  77. static PZLock *certTrustLock = NULL;
  78. static PZLock *freeListLock = NULL;
  79. void
  80. certdb_InitDBLock(NSSLOWCERTCertDBHandle *handle)
  81. {
  82. if (dbLock == NULL) {
  83. dbLock = PZ_NewLock(nssILockCertDB);
  84. PORT_Assert(dbLock != NULL);
  85. }
  86. }
  87. SECStatus
  88. nsslowcert_InitLocks(void)
  89. {
  90. if (freeListLock == NULL) {
  91. freeListLock = PZ_NewLock(nssILockRefLock);
  92. if (freeListLock == NULL) {
  93. return SECFailure;
  94. }
  95. }
  96. if (certRefCountLock == NULL) {
  97. certRefCountLock = PZ_NewLock(nssILockRefLock);
  98. if (certRefCountLock == NULL) {
  99. return SECFailure;
  100. }
  101. }
  102. if (certTrustLock == NULL ) {
  103. certTrustLock = PZ_NewLock(nssILockCertDB);
  104. if (certTrustLock == NULL) {
  105. return SECFailure;
  106. }
  107. }
  108. return SECSuccess;
  109. }
  110. /*
  111. * Acquire the global lock on the cert database.
  112. * This lock is currently used for the following operations:
  113. * adding or deleting a cert to either the temp or perm databases
  114. * converting a temp to perm or perm to temp
  115. * changing (maybe just adding!?) the trust of a cert
  116. * chaning the DB status checking Configuration
  117. */
  118. static void
  119. nsslowcert_LockDB(NSSLOWCERTCertDBHandle *handle)
  120. {
  121. PZ_EnterMonitor(handle->dbMon);
  122. return;
  123. }
  124. /*
  125. * Free the global cert database lock.
  126. */
  127. static void
  128. nsslowcert_UnlockDB(NSSLOWCERTCertDBHandle *handle)
  129. {
  130. PRStatus prstat;
  131. prstat = PZ_ExitMonitor(handle->dbMon);
  132. PORT_Assert(prstat == PR_SUCCESS);
  133. return;
  134. }
  135. /*
  136. * Acquire the cert reference count lock
  137. * There is currently one global lock for all certs, but I'm putting a cert
  138. * arg here so that it will be easy to make it per-cert in the future if
  139. * that turns out to be necessary.
  140. */
  141. static void
  142. nsslowcert_LockCertRefCount(NSSLOWCERTCertificate *cert)
  143. {
  144. PORT_Assert(certRefCountLock != NULL);
  145. PZ_Lock(certRefCountLock);
  146. return;
  147. }
  148. /*
  149. * Free the cert reference count lock
  150. */
  151. static void
  152. nsslowcert_UnlockCertRefCount(NSSLOWCERTCertificate *cert)
  153. {
  154. PRStatus prstat;
  155. PORT_Assert(certRefCountLock != NULL);
  156. prstat = PZ_Unlock(certRefCountLock);
  157. PORT_Assert(prstat == PR_SUCCESS);
  158. return;
  159. }
  160. /*
  161. * Acquire the cert trust lock
  162. * There is currently one global lock for all certs, but I'm putting a cert
  163. * arg here so that it will be easy to make it per-cert in the future if
  164. * that turns out to be necessary.
  165. */
  166. static void
  167. nsslowcert_LockCertTrust(NSSLOWCERTCertificate *cert)
  168. {
  169. PORT_Assert(certTrustLock != NULL);
  170. PZ_Lock(certTrustLock);
  171. return;
  172. }
  173. /*
  174. * Free the cert trust lock
  175. */
  176. static void
  177. nsslowcert_UnlockCertTrust(NSSLOWCERTCertificate *cert)
  178. {
  179. PRStatus prstat;
  180. PORT_Assert(certTrustLock != NULL);
  181. prstat = PZ_Unlock(certTrustLock);
  182. PORT_Assert(prstat == PR_SUCCESS);
  183. return;
  184. }
  185. /*
  186. * Acquire the cert reference count lock
  187. * There is currently one global lock for all certs, but I'm putting a cert
  188. * arg here so that it will be easy to make it per-cert in the future if
  189. * that turns out to be necessary.
  190. */
  191. static void
  192. nsslowcert_LockFreeList(void)
  193. {
  194. PORT_Assert(freeListLock != NULL);
  195. SKIP_AFTER_FORK(PZ_Lock(freeListLock));
  196. return;
  197. }
  198. /*
  199. * Free the cert reference count lock
  200. */
  201. static void
  202. nsslowcert_UnlockFreeList(void)
  203. {
  204. PRStatus prstat = PR_SUCCESS;
  205. PORT_Assert(freeListLock != NULL);
  206. SKIP_AFTER_FORK(prstat = PZ_Unlock(freeListLock));
  207. PORT_Assert(prstat == PR_SUCCESS);
  208. return;
  209. }
  210. NSSLOWCERTCertificate *
  211. nsslowcert_DupCertificate(NSSLOWCERTCertificate *c)
  212. {
  213. if (c) {
  214. nsslowcert_LockCertRefCount(c);
  215. ++c->referenceCount;
  216. nsslowcert_UnlockCertRefCount(c);
  217. }
  218. return c;
  219. }
  220. static int
  221. certdb_Get(DB *db, DBT *key, DBT *data, unsigned int flags)
  222. {
  223. PRStatus prstat;
  224. int ret;
  225. PORT_Assert(dbLock != NULL);
  226. PZ_Lock(dbLock);
  227. ret = (* db->get)(db, key, data, flags);
  228. prstat = PZ_Unlock(dbLock);
  229. return(ret);
  230. }
  231. static int
  232. certdb_Put(DB *db, DBT *key, DBT *data, unsigned int flags)
  233. {
  234. PRStatus prstat;
  235. int ret = 0;
  236. PORT_Assert(dbLock != NULL);
  237. PZ_Lock(dbLock);
  238. ret = (* db->put)(db, key, data, flags);
  239. prstat = PZ_Unlock(dbLock);
  240. return(ret);
  241. }
  242. static int
  243. certdb_Sync(DB *db, unsigned int flags)
  244. {
  245. PRStatus prstat;
  246. int ret;
  247. PORT_Assert(dbLock != NULL);
  248. PZ_Lock(dbLock);
  249. ret = (* db->sync)(db, flags);
  250. prstat = PZ_Unlock(dbLock);
  251. return(ret);
  252. }
  253. #define DB_NOT_FOUND -30991 /* from DBM 3.2 */
  254. static int
  255. certdb_Del(DB *db, DBT *key, unsigned int flags)
  256. {
  257. PRStatus prstat;
  258. int ret;
  259. PORT_Assert(dbLock != NULL);
  260. PZ_Lock(dbLock);
  261. ret = (* db->del)(db, key, flags);
  262. prstat = PZ_Unlock(dbLock);
  263. /* don't fail if the record is already deleted */
  264. if (ret == DB_NOT_FOUND) {
  265. ret = 0;
  266. }
  267. return(ret);
  268. }
  269. static int
  270. certdb_Seq(DB *db, DBT *key, DBT *data, unsigned int flags)
  271. {
  272. PRStatus prstat;
  273. int ret;
  274. PORT_Assert(dbLock != NULL);
  275. PZ_Lock(dbLock);
  276. ret = (* db->seq)(db, key, data, flags);
  277. prstat = PZ_Unlock(dbLock);
  278. return(ret);
  279. }
  280. static void
  281. certdb_Close(DB *db)
  282. {
  283. PRStatus prstat = PR_SUCCESS;
  284. PORT_Assert(dbLock != NULL);
  285. SKIP_AFTER_FORK(PZ_Lock(dbLock));
  286. (* db->close)(db);
  287. SKIP_AFTER_FORK(prstat = PZ_Unlock(dbLock));
  288. return;
  289. }
  290. void
  291. pkcs11_freeNickname(char *nickname, char *space)
  292. {
  293. if (nickname && nickname != space) {
  294. PORT_Free(nickname);
  295. }
  296. }
  297. char *
  298. pkcs11_copyNickname(char *nickname,char *space, int spaceLen)
  299. {
  300. int len;
  301. char *copy = NULL;
  302. len = PORT_Strlen(nickname)+1;
  303. if (len <= spaceLen) {
  304. copy = space;
  305. PORT_Memcpy(copy,nickname,len);
  306. } else {
  307. copy = PORT_Strdup(nickname);
  308. }
  309. return copy;
  310. }
  311. void
  312. pkcs11_freeStaticData (unsigned char *data, unsigned char *space)
  313. {
  314. if (data && data != space) {
  315. PORT_Free(data);
  316. }
  317. }
  318. unsigned char *
  319. pkcs11_allocStaticData(int len, unsigned char *space, int spaceLen)
  320. {
  321. unsigned char *data = NULL;
  322. if (len <= spaceLen) {
  323. data = space;
  324. } else {
  325. data = (unsigned char *) PORT_Alloc(len);
  326. }
  327. return data;
  328. }
  329. unsigned char *
  330. pkcs11_copyStaticData(unsigned char *data, int len,
  331. unsigned char *space, int spaceLen)
  332. {
  333. unsigned char *copy = pkcs11_allocStaticData(len, space, spaceLen);
  334. if (copy) {
  335. PORT_Memcpy(copy,data,len);
  336. }
  337. return copy;
  338. }
  339. /*
  340. * destroy a database entry
  341. */
  342. static void
  343. DestroyDBEntry(certDBEntry *entry)
  344. {
  345. PRArenaPool *arena = entry->common.arena;
  346. /* must be one of our certDBEntry from the free list */
  347. if (arena == NULL) {
  348. certDBEntryCert *certEntry;
  349. if ( entry->common.type != certDBEntryTypeCert) {
  350. return;
  351. }
  352. certEntry = (certDBEntryCert *)entry;
  353. pkcs11_freeStaticData(certEntry->derCert.data, certEntry->derCertSpace);
  354. pkcs11_freeNickname(certEntry->nickname, certEntry->nicknameSpace);
  355. nsslowcert_LockFreeList();
  356. if (entryListCount > MAX_ENTRY_LIST_COUNT) {
  357. PORT_Free(certEntry);
  358. } else {
  359. entryListCount++;
  360. PORT_Memset(certEntry, 0, sizeof( *certEntry));
  361. certEntry->next = entryListHead;
  362. entryListHead = certEntry;
  363. }
  364. nsslowcert_UnlockFreeList();
  365. return;
  366. }
  367. /* Zero out the entry struct, so that any further attempts to use it
  368. * will cause an exception (e.g. null pointer reference). */
  369. PORT_Memset(&entry->common, 0, sizeof entry->common);
  370. PORT_FreeArena(arena, PR_FALSE);
  371. return;
  372. }
  373. /* forward references */
  374. static void nsslowcert_DestroyCertificateNoLocking(NSSLOWCERTCertificate *cert);
  375. static SECStatus
  376. DeleteDBEntry(NSSLOWCERTCertDBHandle *handle, certDBEntryType type, SECItem *dbkey)
  377. {
  378. DBT key;
  379. int ret;
  380. /* init the database key */
  381. key.data = dbkey->data;
  382. key.size = dbkey->len;
  383. dbkey->data[0] = (unsigned char)type;
  384. /* delete entry from database */
  385. ret = certdb_Del(handle->permCertDB, &key, 0 );
  386. if ( ret != 0 ) {
  387. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  388. goto loser;
  389. }
  390. ret = certdb_Sync(handle->permCertDB, 0);
  391. if ( ret ) {
  392. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  393. goto loser;
  394. }
  395. return(SECSuccess);
  396. loser:
  397. return(SECFailure);
  398. }
  399. static SECStatus
  400. ReadDBEntry(NSSLOWCERTCertDBHandle *handle, certDBEntryCommon *entry,
  401. SECItem *dbkey, SECItem *dbentry, PRArenaPool *arena)
  402. {
  403. DBT data, key;
  404. int ret;
  405. unsigned char *buf;
  406. /* init the database key */
  407. key.data = dbkey->data;
  408. key.size = dbkey->len;
  409. dbkey->data[0] = (unsigned char)entry->type;
  410. /* read entry from database */
  411. ret = certdb_Get(handle->permCertDB, &key, &data, 0 );
  412. if ( ret != 0 ) {
  413. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  414. goto loser;
  415. }
  416. /* validate the entry */
  417. if ( data.size < SEC_DB_ENTRY_HEADER_LEN ) {
  418. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  419. goto loser;
  420. }
  421. buf = (unsigned char *)data.data;
  422. /* version 7 has the same schema, we may be using a v7 db if we openned
  423. * the databases readonly. */
  424. if (!((buf[0] == (unsigned char)CERT_DB_FILE_VERSION)
  425. || (buf[0] == (unsigned char) CERT_DB_V7_FILE_VERSION))) {
  426. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  427. goto loser;
  428. }
  429. if ( buf[1] != (unsigned char)entry->type ) {
  430. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  431. goto loser;
  432. }
  433. /* copy out header information */
  434. entry->version = (unsigned int)buf[0];
  435. entry->type = (certDBEntryType)buf[1];
  436. entry->flags = (unsigned int)buf[2];
  437. /* format body of entry for return to caller */
  438. dbentry->len = data.size - SEC_DB_ENTRY_HEADER_LEN;
  439. if ( dbentry->len ) {
  440. if (arena) {
  441. dbentry->data = (unsigned char *)
  442. PORT_ArenaAlloc(arena, dbentry->len);
  443. if ( dbentry->data == NULL ) {
  444. PORT_SetError(SEC_ERROR_NO_MEMORY);
  445. goto loser;
  446. }
  447. PORT_Memcpy(dbentry->data, &buf[SEC_DB_ENTRY_HEADER_LEN],
  448. dbentry->len);
  449. } else {
  450. dbentry->data = &buf[SEC_DB_ENTRY_HEADER_LEN];
  451. }
  452. } else {
  453. dbentry->data = NULL;
  454. }
  455. return(SECSuccess);
  456. loser:
  457. return(SECFailure);
  458. }
  459. /**
  460. ** Implement low level database access
  461. **/
  462. static SECStatus
  463. WriteDBEntry(NSSLOWCERTCertDBHandle *handle, certDBEntryCommon *entry,
  464. SECItem *dbkey, SECItem *dbentry)
  465. {
  466. int ret;
  467. DBT data, key;
  468. unsigned char *buf;
  469. data.data = dbentry->data;
  470. data.size = dbentry->len;
  471. buf = (unsigned char*)data.data;
  472. buf[0] = (unsigned char)entry->version;
  473. buf[1] = (unsigned char)entry->type;
  474. buf[2] = (unsigned char)entry->flags;
  475. key.data = dbkey->data;
  476. key.size = dbkey->len;
  477. dbkey->data[0] = (unsigned char)entry->type;
  478. /* put the record into the database now */
  479. ret = certdb_Put(handle->permCertDB, &key, &data, 0);
  480. if ( ret != 0 ) {
  481. goto loser;
  482. }
  483. ret = certdb_Sync( handle->permCertDB, 0 );
  484. if ( ret ) {
  485. goto loser;
  486. }
  487. return(SECSuccess);
  488. loser:
  489. return(SECFailure);
  490. }
  491. /*
  492. * encode a database cert record
  493. */
  494. static SECStatus
  495. EncodeDBCertEntry(certDBEntryCert *entry, PRArenaPool *arena, SECItem *dbitem)
  496. {
  497. unsigned int nnlen;
  498. unsigned char *buf;
  499. char *nn;
  500. char zbuf = 0;
  501. if ( entry->nickname ) {
  502. nn = entry->nickname;
  503. } else {
  504. nn = &zbuf;
  505. }
  506. nnlen = PORT_Strlen(nn) + 1;
  507. /* allocate space for encoded database record, including space
  508. * for low level header
  509. */
  510. dbitem->len = entry->derCert.len + nnlen + DB_CERT_ENTRY_HEADER_LEN +
  511. SEC_DB_ENTRY_HEADER_LEN;
  512. dbitem->data = (unsigned char *)PORT_ArenaAlloc(arena, dbitem->len);
  513. if ( dbitem->data == NULL) {
  514. PORT_SetError(SEC_ERROR_NO_MEMORY);
  515. goto loser;
  516. }
  517. /* fill in database record */
  518. buf = &dbitem->data[SEC_DB_ENTRY_HEADER_LEN];
  519. buf[0] = (PRUint8)( entry->trust.sslFlags >> 8 );
  520. buf[1] = (PRUint8)( entry->trust.sslFlags );
  521. buf[2] = (PRUint8)( entry->trust.emailFlags >> 8 );
  522. buf[3] = (PRUint8)( entry->trust.emailFlags );
  523. buf[4] = (PRUint8)( entry->trust.objectSigningFlags >> 8 );
  524. buf[5] = (PRUint8)( entry->trust.objectSigningFlags );
  525. buf[6] = (PRUint8)( entry->derCert.len >> 8 );
  526. buf[7] = (PRUint8)( entry->derCert.len );
  527. buf[8] = (PRUint8)( nnlen >> 8 );
  528. buf[9] = (PRUint8)( nnlen );
  529. PORT_Memcpy(&buf[DB_CERT_ENTRY_HEADER_LEN], entry->derCert.data,
  530. entry->derCert.len);
  531. PORT_Memcpy(&buf[DB_CERT_ENTRY_HEADER_LEN + entry->derCert.len],
  532. nn, nnlen);
  533. return(SECSuccess);
  534. loser:
  535. return(SECFailure);
  536. }
  537. /*
  538. * encode a database key for a cert record
  539. */
  540. static SECStatus
  541. EncodeDBCertKey(const SECItem *certKey, PRArenaPool *arena, SECItem *dbkey)
  542. {
  543. unsigned int len = certKey->len + SEC_DB_KEY_HEADER_LEN;
  544. if (len > NSS_MAX_LEGACY_DB_KEY_SIZE)
  545. goto loser;
  546. if (arena) {
  547. dbkey->data = (unsigned char *)PORT_ArenaAlloc(arena, len);
  548. } else {
  549. if (dbkey->len < len) {
  550. dbkey->data = (unsigned char *)PORT_Alloc(len);
  551. }
  552. }
  553. dbkey->len = len;
  554. if ( dbkey->data == NULL ) {
  555. goto loser;
  556. }
  557. PORT_Memcpy(&dbkey->data[SEC_DB_KEY_HEADER_LEN],
  558. certKey->data, certKey->len);
  559. dbkey->data[0] = certDBEntryTypeCert;
  560. return(SECSuccess);
  561. loser:
  562. return(SECFailure);
  563. }
  564. static SECStatus
  565. EncodeDBGenericKey(const SECItem *certKey, PRArenaPool *arena, SECItem *dbkey,
  566. certDBEntryType entryType)
  567. {
  568. /*
  569. * we only allow _one_ KRL key!
  570. */
  571. if (entryType == certDBEntryTypeKeyRevocation) {
  572. dbkey->len = SEC_DB_KEY_HEADER_LEN;
  573. dbkey->data = (unsigned char *)PORT_ArenaAlloc(arena, dbkey->len);
  574. if ( dbkey->data == NULL ) {
  575. goto loser;
  576. }
  577. dbkey->data[0] = (unsigned char) entryType;
  578. return(SECSuccess);
  579. }
  580. dbkey->len = certKey->len + SEC_DB_KEY_HEADER_LEN;
  581. if (dbkey->len > NSS_MAX_LEGACY_DB_KEY_SIZE)
  582. goto loser;
  583. dbkey->data = (unsigned char *)PORT_ArenaAlloc(arena, dbkey->len);
  584. if ( dbkey->data == NULL ) {
  585. goto loser;
  586. }
  587. PORT_Memcpy(&dbkey->data[SEC_DB_KEY_HEADER_LEN],
  588. certKey->data, certKey->len);
  589. dbkey->data[0] = (unsigned char) entryType;
  590. return(SECSuccess);
  591. loser:
  592. return(SECFailure);
  593. }
  594. static SECStatus
  595. DecodeDBCertEntry(certDBEntryCert *entry, SECItem *dbentry)
  596. {
  597. unsigned int nnlen;
  598. unsigned int headerlen;
  599. int lenoff;
  600. /* allow updates of old versions of the database */
  601. switch ( entry->common.version ) {
  602. case 5:
  603. headerlen = DB_CERT_V5_ENTRY_HEADER_LEN;
  604. lenoff = 3;
  605. break;
  606. case 6:
  607. /* should not get here */
  608. PORT_Assert(0);
  609. headerlen = DB_CERT_V6_ENTRY_HEADER_LEN;
  610. lenoff = 3;
  611. break;
  612. case 7:
  613. case 8:
  614. headerlen = DB_CERT_ENTRY_HEADER_LEN;
  615. lenoff = 6;
  616. break;
  617. default:
  618. /* better not get here */
  619. PORT_Assert(0);
  620. headerlen = DB_CERT_V5_ENTRY_HEADER_LEN;
  621. lenoff = 3;
  622. break;
  623. }
  624. /* is record long enough for header? */
  625. if ( dbentry->len < headerlen ) {
  626. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  627. goto loser;
  628. }
  629. /* is database entry correct length? */
  630. entry->derCert.len = ( ( dbentry->data[lenoff] << 8 ) |
  631. dbentry->data[lenoff+1] );
  632. nnlen = ( ( dbentry->data[lenoff+2] << 8 ) | dbentry->data[lenoff+3] );
  633. lenoff = dbentry->len - ( entry->derCert.len + nnlen + headerlen );
  634. if ( lenoff ) {
  635. if ( lenoff < 0 || (lenoff & 0xffff) != 0 ) {
  636. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  637. goto loser;
  638. }
  639. /* The cert size exceeded 64KB. Reconstruct the correct length. */
  640. entry->derCert.len += lenoff;
  641. }
  642. /* copy the dercert */
  643. entry->derCert.data = pkcs11_copyStaticData(&dbentry->data[headerlen],
  644. entry->derCert.len,entry->derCertSpace,sizeof(entry->derCertSpace));
  645. if ( entry->derCert.data == NULL ) {
  646. PORT_SetError(SEC_ERROR_NO_MEMORY);
  647. goto loser;
  648. }
  649. /* copy the nickname */
  650. if ( nnlen > 1 ) {
  651. entry->nickname = (char *)pkcs11_copyStaticData(
  652. &dbentry->data[headerlen+entry->derCert.len], nnlen,
  653. (unsigned char *)entry->nicknameSpace,
  654. sizeof(entry->nicknameSpace));
  655. if ( entry->nickname == NULL ) {
  656. PORT_SetError(SEC_ERROR_NO_MEMORY);
  657. goto loser;
  658. }
  659. } else {
  660. entry->nickname = NULL;
  661. }
  662. if ( entry->common.version < 7 ) {
  663. /* allow updates of v5 db */
  664. entry->trust.sslFlags = dbentry->data[0];
  665. entry->trust.emailFlags = dbentry->data[1];
  666. entry->trust.objectSigningFlags = dbentry->data[2];
  667. } else {
  668. entry->trust.sslFlags = ( dbentry->data[0] << 8 ) | dbentry->data[1];
  669. entry->trust.emailFlags = ( dbentry->data[2] << 8 ) | dbentry->data[3];
  670. entry->trust.objectSigningFlags =
  671. ( dbentry->data[4] << 8 ) | dbentry->data[5];
  672. }
  673. return(SECSuccess);
  674. loser:
  675. return(SECFailure);
  676. }
  677. /*
  678. * Create a new certDBEntryCert from existing data
  679. */
  680. static certDBEntryCert *
  681. NewDBCertEntry(SECItem *derCert, char *nickname,
  682. NSSLOWCERTCertTrust *trust, int flags)
  683. {
  684. certDBEntryCert *entry;
  685. PRArenaPool *arena = NULL;
  686. int nnlen;
  687. arena = PORT_NewArena( DER_DEFAULT_CHUNKSIZE );
  688. if ( !arena ) {
  689. goto loser;
  690. }
  691. entry = PORT_ArenaZNew(arena, certDBEntryCert);
  692. if ( entry == NULL ) {
  693. goto loser;
  694. }
  695. /* fill in the dbCert */
  696. entry->common.arena = arena;
  697. entry->common.type = certDBEntryTypeCert;
  698. entry->common.version = CERT_DB_FILE_VERSION;
  699. entry->common.flags = flags;
  700. if ( trust ) {
  701. entry->trust = *trust;
  702. }
  703. entry->derCert.data = (unsigned char *)PORT_ArenaAlloc(arena, derCert->len);
  704. if ( !entry->derCert.data ) {
  705. goto loser;
  706. }
  707. entry->derCert.len = derCert->len;
  708. PORT_Memcpy(entry->derCert.data, derCert->data, derCert->len);
  709. nnlen = ( nickname ? strlen(nickname) + 1 : 0 );
  710. if ( nnlen ) {
  711. entry->nickname = (char *)PORT_ArenaAlloc(arena, nnlen);
  712. if ( !entry->nickname ) {
  713. goto loser;
  714. }
  715. PORT_Memcpy(entry->nickname, nickname, nnlen);
  716. } else {
  717. entry->nickname = 0;
  718. }
  719. return(entry);
  720. loser:
  721. /* allocation error, free arena and return */
  722. if ( arena ) {
  723. PORT_FreeArena(arena, PR_FALSE);
  724. }
  725. PORT_SetError(SEC_ERROR_NO_MEMORY);
  726. return(0);
  727. }
  728. /*
  729. * Decode a version 4 DBCert from the byte stream database format
  730. * and construct a current database entry struct
  731. */
  732. static certDBEntryCert *
  733. DecodeV4DBCertEntry(unsigned char *buf, int len)
  734. {
  735. certDBEntryCert *entry;
  736. int certlen;
  737. int nnlen;
  738. PRArenaPool *arena;
  739. /* make sure length is at least long enough for the header */
  740. if ( len < DBCERT_V4_HEADER_LEN ) {
  741. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  742. return(0);
  743. }
  744. /* get other lengths */
  745. certlen = buf[3] << 8 | buf[4];
  746. nnlen = buf[5] << 8 | buf[6];
  747. /* make sure DB entry is the right size */
  748. if ( ( certlen + nnlen + DBCERT_V4_HEADER_LEN ) != len ) {
  749. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  750. return(0);
  751. }
  752. /* allocate arena */
  753. arena = PORT_NewArena( DER_DEFAULT_CHUNKSIZE );
  754. if ( !arena ) {
  755. PORT_SetError(SEC_ERROR_NO_MEMORY);
  756. return(0);
  757. }
  758. /* allocate structure and members */
  759. entry = (certDBEntryCert *) PORT_ArenaAlloc(arena, sizeof(certDBEntryCert));
  760. if ( !entry ) {
  761. goto loser;
  762. }
  763. entry->common.arena = arena;
  764. entry->common.version = CERT_DB_FILE_VERSION;
  765. entry->common.type = certDBEntryTypeCert;
  766. entry->common.flags = 0;
  767. entry->trust.sslFlags = buf[0];
  768. entry->trust.emailFlags = buf[1];
  769. entry->trust.objectSigningFlags = buf[2];
  770. entry->derCert.data = (unsigned char *)PORT_ArenaAlloc(arena, certlen);
  771. if ( !entry->derCert.data ) {
  772. goto loser;
  773. }
  774. entry->derCert.len = certlen;
  775. PORT_Memcpy(entry->derCert.data, &buf[DBCERT_V4_HEADER_LEN], certlen);
  776. if ( nnlen ) {
  777. entry->nickname = (char *) PORT_ArenaAlloc(arena, nnlen);
  778. if ( !entry->nickname ) {
  779. goto loser;
  780. }
  781. PORT_Memcpy(entry->nickname, &buf[DBCERT_V4_HEADER_LEN + certlen], nnlen);
  782. if (PORT_Strcmp(entry->nickname, "Server-Cert") == 0) {
  783. entry->trust.sslFlags |= CERTDB_USER;
  784. }
  785. } else {
  786. entry->nickname = 0;
  787. }
  788. return(entry);
  789. loser:
  790. PORT_FreeArena(arena, PR_FALSE);
  791. PORT_SetError(SEC_ERROR_NO_MEMORY);
  792. return(0);
  793. }
  794. /*
  795. * Encode a Certificate database entry into byte stream suitable for
  796. * the database
  797. */
  798. static SECStatus
  799. WriteDBCertEntry(NSSLOWCERTCertDBHandle *handle, certDBEntryCert *entry)
  800. {
  801. SECItem dbitem, dbkey;
  802. PRArenaPool *tmparena = NULL;
  803. SECItem tmpitem;
  804. SECStatus rv;
  805. tmparena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  806. if ( tmparena == NULL ) {
  807. goto loser;
  808. }
  809. rv = EncodeDBCertEntry(entry, tmparena, &dbitem);
  810. if ( rv != SECSuccess ) {
  811. goto loser;
  812. }
  813. /* get the database key and format it */
  814. rv = nsslowcert_KeyFromDERCert(tmparena, &entry->derCert, &tmpitem);
  815. if ( rv == SECFailure ) {
  816. goto loser;
  817. }
  818. rv = EncodeDBCertKey(&tmpitem, tmparena, &dbkey);
  819. if ( rv == SECFailure ) {
  820. goto loser;
  821. }
  822. /* now write it to the database */
  823. rv = WriteDBEntry(handle, &entry->common, &dbkey, &dbitem);
  824. if ( rv != SECSuccess ) {
  825. goto loser;
  826. }
  827. PORT_FreeArena(tmparena, PR_FALSE);
  828. return(SECSuccess);
  829. loser:
  830. if ( tmparena ) {
  831. PORT_FreeArena(tmparena, PR_FALSE);
  832. }
  833. return(SECFailure);
  834. }
  835. /*
  836. * delete a certificate entry
  837. */
  838. static SECStatus
  839. DeleteDBCertEntry(NSSLOWCERTCertDBHandle *handle, SECItem *certKey)
  840. {
  841. SECItem dbkey;
  842. SECStatus rv;
  843. dbkey.data= NULL;
  844. dbkey.len = 0;
  845. rv = EncodeDBCertKey(certKey, NULL, &dbkey);
  846. if ( rv != SECSuccess ) {
  847. goto loser;
  848. }
  849. rv = DeleteDBEntry(handle, certDBEntryTypeCert, &dbkey);
  850. if ( rv == SECFailure ) {
  851. goto loser;
  852. }
  853. PORT_Free(dbkey.data);
  854. return(SECSuccess);
  855. loser:
  856. if (dbkey.data) {
  857. PORT_Free(dbkey.data);
  858. }
  859. return(SECFailure);
  860. }
  861. static certDBEntryCert *
  862. CreateCertEntry(void)
  863. {
  864. certDBEntryCert *entry;
  865. nsslowcert_LockFreeList();
  866. entry = entryListHead;
  867. if (entry) {
  868. entryListCount--;
  869. entryListHead = entry->next;
  870. }
  871. PORT_Assert(entryListCount >= 0);
  872. nsslowcert_UnlockFreeList();
  873. if (entry) {
  874. return entry;
  875. }
  876. return PORT_ZNew(certDBEntryCert);
  877. }
  878. static void
  879. DestroyCertEntryFreeList(void)
  880. {
  881. certDBEntryCert *entry;
  882. nsslowcert_LockFreeList();
  883. while (NULL != (entry = entryListHead)) {
  884. entryListCount--;
  885. entryListHead = entry->next;
  886. PORT_Free(entry);
  887. }
  888. PORT_Assert(!entryListCount);
  889. entryListCount = 0;
  890. nsslowcert_UnlockFreeList();
  891. }
  892. /*
  893. * Read a certificate entry
  894. */
  895. static certDBEntryCert *
  896. ReadDBCertEntry(NSSLOWCERTCertDBHandle *handle, const SECItem *certKey)
  897. {
  898. certDBEntryCert *entry;
  899. SECItem dbkey;
  900. SECItem dbentry;
  901. SECStatus rv;
  902. unsigned char buf[512];
  903. dbkey.data = buf;
  904. dbkey.len = sizeof(buf);
  905. entry = CreateCertEntry();
  906. if ( entry == NULL ) {
  907. PORT_SetError(SEC_ERROR_NO_MEMORY);
  908. goto loser;
  909. }
  910. entry->common.arena = NULL;
  911. entry->common.type = certDBEntryTypeCert;
  912. rv = EncodeDBCertKey(certKey, NULL, &dbkey);
  913. if ( rv != SECSuccess ) {
  914. goto loser;
  915. }
  916. rv = ReadDBEntry(handle, &entry->common, &dbkey, &dbentry, NULL);
  917. if ( rv == SECFailure ) {
  918. goto loser;
  919. }
  920. rv = DecodeDBCertEntry(entry, &dbentry);
  921. if ( rv != SECSuccess ) {
  922. goto loser;
  923. }
  924. pkcs11_freeStaticData(dbkey.data,buf);
  925. dbkey.data = NULL;
  926. return(entry);
  927. loser:
  928. pkcs11_freeStaticData(dbkey.data,buf);
  929. dbkey.data = NULL;
  930. if ( entry ) {
  931. DestroyDBEntry((certDBEntry *)entry);
  932. }
  933. return(NULL);
  934. }
  935. /*
  936. * encode a database cert record
  937. */
  938. static SECStatus
  939. EncodeDBCrlEntry(certDBEntryRevocation *entry, PRArenaPool *arena, SECItem *dbitem)
  940. {
  941. unsigned int nnlen = 0;
  942. unsigned char *buf;
  943. if (entry->url) {
  944. nnlen = PORT_Strlen(entry->url) + 1;
  945. }
  946. /* allocate space for encoded database record, including space
  947. * for low level header
  948. */
  949. dbitem->len = entry->derCrl.len + nnlen
  950. + SEC_DB_ENTRY_HEADER_LEN + DB_CRL_ENTRY_HEADER_LEN;
  951. dbitem->data = (unsigned char *)PORT_ArenaAlloc(arena, dbitem->len);
  952. if ( dbitem->data == NULL) {
  953. PORT_SetError(SEC_ERROR_NO_MEMORY);
  954. goto loser;
  955. }
  956. /* fill in database record */
  957. buf = &dbitem->data[SEC_DB_ENTRY_HEADER_LEN];
  958. buf[0] = (PRUint8)( entry->derCrl.len >> 8 );
  959. buf[1] = (PRUint8)( entry->derCrl.len );
  960. buf[2] = (PRUint8)( nnlen >> 8 );
  961. buf[3] = (PRUint8)( nnlen );
  962. PORT_Memcpy(&buf[DB_CRL_ENTRY_HEADER_LEN], entry->derCrl.data,
  963. entry->derCrl.len);
  964. if (nnlen != 0) {
  965. PORT_Memcpy(&buf[DB_CRL_ENTRY_HEADER_LEN + entry->derCrl.len],
  966. entry->url, nnlen);
  967. }
  968. return(SECSuccess);
  969. loser:
  970. return(SECFailure);
  971. }
  972. static SECStatus
  973. DecodeDBCrlEntry(certDBEntryRevocation *entry, SECItem *dbentry)
  974. {
  975. unsigned int urlLen;
  976. int lenDiff;
  977. /* is record long enough for header? */
  978. if ( dbentry->len < DB_CRL_ENTRY_HEADER_LEN ) {
  979. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  980. goto loser;
  981. }
  982. /* is database entry correct length? */
  983. entry->derCrl.len = ( ( dbentry->data[0] << 8 ) | dbentry->data[1] );
  984. urlLen = ( ( dbentry->data[2] << 8 ) | dbentry->data[3] );
  985. lenDiff = dbentry->len -
  986. (entry->derCrl.len + urlLen + DB_CRL_ENTRY_HEADER_LEN);
  987. if (lenDiff) {
  988. if (lenDiff < 0 || (lenDiff & 0xffff) != 0) {
  989. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  990. goto loser;
  991. }
  992. /* CRL entry is greater than 64 K. Hack to make this continue to work */
  993. entry->derCrl.len += lenDiff;
  994. }
  995. /* copy the der CRL */
  996. entry->derCrl.data = (unsigned char *)PORT_ArenaAlloc(entry->common.arena,
  997. entry->derCrl.len);
  998. if ( entry->derCrl.data == NULL ) {
  999. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1000. goto loser;
  1001. }
  1002. PORT_Memcpy(entry->derCrl.data, &dbentry->data[DB_CRL_ENTRY_HEADER_LEN],
  1003. entry->derCrl.len);
  1004. /* copy the url */
  1005. entry->url = NULL;
  1006. if (urlLen != 0) {
  1007. entry->url = (char *)PORT_ArenaAlloc(entry->common.arena, urlLen);
  1008. if ( entry->url == NULL ) {
  1009. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1010. goto loser;
  1011. }
  1012. PORT_Memcpy(entry->url,
  1013. &dbentry->data[DB_CRL_ENTRY_HEADER_LEN + entry->derCrl.len],
  1014. urlLen);
  1015. }
  1016. return(SECSuccess);
  1017. loser:
  1018. return(SECFailure);
  1019. }
  1020. /*
  1021. * Create a new certDBEntryRevocation from existing data
  1022. */
  1023. static certDBEntryRevocation *
  1024. NewDBCrlEntry(SECItem *derCrl, char * url, certDBEntryType crlType, int flags)
  1025. {
  1026. certDBEntryRevocation *entry;
  1027. PRArenaPool *arena = NULL;
  1028. int nnlen;
  1029. arena = PORT_NewArena( DER_DEFAULT_CHUNKSIZE );
  1030. if ( !arena ) {
  1031. goto loser;
  1032. }
  1033. entry = PORT_ArenaZNew(arena, certDBEntryRevocation);
  1034. if ( entry == NULL ) {
  1035. goto loser;
  1036. }
  1037. /* fill in the dbRevolcation */
  1038. entry->common.arena = arena;
  1039. entry->common.type = crlType;
  1040. entry->common.version = CERT_DB_FILE_VERSION;
  1041. entry->common.flags = flags;
  1042. entry->derCrl.data = (unsigned char *)PORT_ArenaAlloc(arena, derCrl->len);
  1043. if ( !entry->derCrl.data ) {
  1044. goto loser;
  1045. }
  1046. if (url) {
  1047. nnlen = PORT_Strlen(url) + 1;
  1048. entry->url = (char *)PORT_ArenaAlloc(arena, nnlen);
  1049. if ( !entry->url ) {
  1050. goto loser;
  1051. }
  1052. PORT_Memcpy(entry->url, url, nnlen);
  1053. } else {
  1054. entry->url = NULL;
  1055. }
  1056. entry->derCrl.len = derCrl->len;
  1057. PORT_Memcpy(entry->derCrl.data, derCrl->data, derCrl->len);
  1058. return(entry);
  1059. loser:
  1060. /* allocation error, free arena and return */
  1061. if ( arena ) {
  1062. PORT_FreeArena(arena, PR_FALSE);
  1063. }
  1064. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1065. return(0);
  1066. }
  1067. static SECStatus
  1068. WriteDBCrlEntry(NSSLOWCERTCertDBHandle *handle, certDBEntryRevocation *entry,
  1069. SECItem *crlKey )
  1070. {
  1071. SECItem dbkey;
  1072. PRArenaPool *tmparena = NULL;
  1073. SECItem encodedEntry;
  1074. SECStatus rv;
  1075. tmparena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1076. if ( tmparena == NULL ) {
  1077. goto loser;
  1078. }
  1079. rv = EncodeDBCrlEntry(entry, tmparena, &encodedEntry);
  1080. if ( rv == SECFailure ) {
  1081. goto loser;
  1082. }
  1083. rv = EncodeDBGenericKey(crlKey, tmparena, &dbkey, entry->common.type);
  1084. if ( rv == SECFailure ) {
  1085. goto loser;
  1086. }
  1087. /* now write it to the database */
  1088. rv = WriteDBEntry(handle, &entry->common, &dbkey, &encodedEntry);
  1089. if ( rv != SECSuccess ) {
  1090. goto loser;
  1091. }
  1092. PORT_FreeArena(tmparena, PR_FALSE);
  1093. return(SECSuccess);
  1094. loser:
  1095. if ( tmparena ) {
  1096. PORT_FreeArena(tmparena, PR_FALSE);
  1097. }
  1098. return(SECFailure);
  1099. }
  1100. /*
  1101. * delete a crl entry
  1102. */
  1103. static SECStatus
  1104. DeleteDBCrlEntry(NSSLOWCERTCertDBHandle *handle, const SECItem *crlKey,
  1105. certDBEntryType crlType)
  1106. {
  1107. SECItem dbkey;
  1108. PRArenaPool *arena = NULL;
  1109. SECStatus rv;
  1110. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1111. if ( arena == NULL ) {
  1112. goto loser;
  1113. }
  1114. rv = EncodeDBGenericKey(crlKey, arena, &dbkey, crlType);
  1115. if ( rv != SECSuccess ) {
  1116. goto loser;
  1117. }
  1118. rv = DeleteDBEntry(handle, crlType, &dbkey);
  1119. if ( rv == SECFailure ) {
  1120. goto loser;
  1121. }
  1122. PORT_FreeArena(arena, PR_FALSE);
  1123. return(SECSuccess);
  1124. loser:
  1125. if ( arena ) {
  1126. PORT_FreeArena(arena, PR_FALSE);
  1127. }
  1128. return(SECFailure);
  1129. }
  1130. /*
  1131. * Read a certificate entry
  1132. */
  1133. static certDBEntryRevocation *
  1134. ReadDBCrlEntry(NSSLOWCERTCertDBHandle *handle, SECItem *certKey,
  1135. certDBEntryType crlType)
  1136. {
  1137. PRArenaPool *arena = NULL;
  1138. PRArenaPool *tmparena = NULL;
  1139. certDBEntryRevocation *entry;
  1140. SECItem dbkey;
  1141. SECItem dbentry;
  1142. SECStatus rv;
  1143. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1144. if ( arena == NULL ) {
  1145. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1146. goto loser;
  1147. }
  1148. tmparena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1149. if ( tmparena == NULL ) {
  1150. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1151. goto loser;
  1152. }
  1153. entry = (certDBEntryRevocation *)
  1154. PORT_ArenaAlloc(arena, sizeof(certDBEntryRevocation));
  1155. if ( entry == NULL ) {
  1156. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1157. goto loser;
  1158. }
  1159. entry->common.arena = arena;
  1160. entry->common.type = crlType;
  1161. rv = EncodeDBGenericKey(certKey, tmparena, &dbkey, crlType);
  1162. if ( rv != SECSuccess ) {
  1163. goto loser;
  1164. }
  1165. rv = ReadDBEntry(handle, &entry->common, &dbkey, &dbentry, NULL);
  1166. if ( rv == SECFailure ) {
  1167. goto loser;
  1168. }
  1169. rv = DecodeDBCrlEntry(entry, &dbentry);
  1170. if ( rv != SECSuccess ) {
  1171. goto loser;
  1172. }
  1173. PORT_FreeArena(tmparena, PR_FALSE);
  1174. return(entry);
  1175. loser:
  1176. if ( tmparena ) {
  1177. PORT_FreeArena(tmparena, PR_FALSE);
  1178. }
  1179. if ( arena ) {
  1180. PORT_FreeArena(arena, PR_FALSE);
  1181. }
  1182. return(NULL);
  1183. }
  1184. void
  1185. nsslowcert_DestroyDBEntry(certDBEntry *entry)
  1186. {
  1187. DestroyDBEntry(entry);
  1188. return;
  1189. }
  1190. /*
  1191. * Encode a database nickname record
  1192. */
  1193. static SECStatus
  1194. EncodeDBNicknameEntry(certDBEntryNickname *entry, PRArenaPool *arena,
  1195. SECItem *dbitem)
  1196. {
  1197. unsigned char *buf;
  1198. /* allocate space for encoded database record, including space
  1199. * for low level header
  1200. */
  1201. dbitem->len = entry->subjectName.len + DB_NICKNAME_ENTRY_HEADER_LEN +
  1202. SEC_DB_ENTRY_HEADER_LEN;
  1203. dbitem->data = (unsigned char *)PORT_ArenaAlloc(arena, dbitem->len);
  1204. if ( dbitem->data == NULL) {
  1205. goto loser;
  1206. }
  1207. /* fill in database record */
  1208. buf = &dbitem->data[SEC_DB_ENTRY_HEADER_LEN];
  1209. buf[0] = (PRUint8)( entry->subjectName.len >> 8 );
  1210. buf[1] = (PRUint8)( entry->subjectName.len );
  1211. PORT_Memcpy(&buf[DB_NICKNAME_ENTRY_HEADER_LEN], entry->subjectName.data,
  1212. entry->subjectName.len);
  1213. return(SECSuccess);
  1214. loser:
  1215. return(SECFailure);
  1216. }
  1217. /*
  1218. * Encode a database key for a nickname record
  1219. */
  1220. static SECStatus
  1221. EncodeDBNicknameKey(char *nickname, PRArenaPool *arena,
  1222. SECItem *dbkey)
  1223. {
  1224. unsigned int nnlen;
  1225. nnlen = PORT_Strlen(nickname) + 1; /* includes null */
  1226. /* now get the database key and format it */
  1227. dbkey->len = nnlen + SEC_DB_KEY_HEADER_LEN;
  1228. if (dbkey->len > NSS_MAX_LEGACY_DB_KEY_SIZE)
  1229. goto loser;
  1230. dbkey->data = (unsigned char *)PORT_ArenaAlloc(arena, dbkey->len);
  1231. if ( dbkey->data == NULL ) {
  1232. goto loser;
  1233. }
  1234. PORT_Memcpy(&dbkey->data[SEC_DB_KEY_HEADER_LEN], nickname, nnlen);
  1235. dbkey->data[0] = certDBEntryTypeNickname;
  1236. return(SECSuccess);
  1237. loser:
  1238. return(SECFailure);
  1239. }
  1240. static SECStatus
  1241. DecodeDBNicknameEntry(certDBEntryNickname *entry, SECItem *dbentry,
  1242. char *nickname)
  1243. {
  1244. int lenDiff;
  1245. /* is record long enough for header? */
  1246. if ( dbentry->len < DB_NICKNAME_ENTRY_HEADER_LEN ) {
  1247. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  1248. goto loser;
  1249. }
  1250. /* is database entry correct length? */
  1251. entry->subjectName.len = ( ( dbentry->data[0] << 8 ) | dbentry->data[1] );
  1252. lenDiff = dbentry->len -
  1253. (entry->subjectName.len + DB_NICKNAME_ENTRY_HEADER_LEN);
  1254. if (lenDiff) {
  1255. if (lenDiff < 0 || (lenDiff & 0xffff) != 0 ) {
  1256. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  1257. goto loser;
  1258. }
  1259. /* The entry size exceeded 64KB. Reconstruct the correct length. */
  1260. entry->subjectName.len += lenDiff;
  1261. }
  1262. /* copy the certkey */
  1263. entry->subjectName.data =
  1264. (unsigned char *)PORT_ArenaAlloc(entry->common.arena,
  1265. entry->subjectName.len);
  1266. if ( entry->subjectName.data == NULL ) {
  1267. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1268. goto loser;
  1269. }
  1270. PORT_Memcpy(entry->subjectName.data,
  1271. &dbentry->data[DB_NICKNAME_ENTRY_HEADER_LEN],
  1272. entry->subjectName.len);
  1273. entry->subjectName.type = siBuffer;
  1274. entry->nickname = (char *)PORT_ArenaAlloc(entry->common.arena,
  1275. PORT_Strlen(nickname)+1);
  1276. if ( entry->nickname ) {
  1277. PORT_Strcpy(entry->nickname, nickname);
  1278. }
  1279. return(SECSuccess);
  1280. loser:
  1281. return(SECFailure);
  1282. }
  1283. /*
  1284. * create a new nickname entry
  1285. */
  1286. static certDBEntryNickname *
  1287. NewDBNicknameEntry(char *nickname, SECItem *subjectName, unsigned int flags)
  1288. {
  1289. PRArenaPool *arena = NULL;
  1290. certDBEntryNickname *entry;
  1291. int nnlen;
  1292. SECStatus rv;
  1293. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1294. if ( arena == NULL ) {
  1295. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1296. goto loser;
  1297. }
  1298. entry = (certDBEntryNickname *)PORT_ArenaAlloc(arena,
  1299. sizeof(certDBEntryNickname));
  1300. if ( entry == NULL ) {
  1301. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1302. goto loser;
  1303. }
  1304. /* init common fields */
  1305. entry->common.arena = arena;
  1306. entry->common.type = certDBEntryTypeNickname;
  1307. entry->common.version = CERT_DB_FILE_VERSION;
  1308. entry->common.flags = flags;
  1309. /* copy the nickname */
  1310. nnlen = PORT_Strlen(nickname) + 1;
  1311. entry->nickname = (char*)PORT_ArenaAlloc(arena, nnlen);
  1312. if ( entry->nickname == NULL ) {
  1313. goto loser;
  1314. }
  1315. PORT_Memcpy(entry->nickname, nickname, nnlen);
  1316. rv = SECITEM_CopyItem(arena, &entry->subjectName, subjectName);
  1317. if ( rv != SECSuccess ) {
  1318. goto loser;
  1319. }
  1320. return(entry);
  1321. loser:
  1322. if ( arena ) {
  1323. PORT_FreeArena(arena, PR_FALSE);
  1324. }
  1325. return(NULL);
  1326. }
  1327. /*
  1328. * delete a nickname entry
  1329. */
  1330. static SECStatus
  1331. DeleteDBNicknameEntry(NSSLOWCERTCertDBHandle *handle, char *nickname)
  1332. {
  1333. PRArenaPool *arena = NULL;
  1334. SECStatus rv;
  1335. SECItem dbkey;
  1336. if ( nickname == NULL ) {
  1337. return(SECSuccess);
  1338. }
  1339. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1340. if ( arena == NULL ) {
  1341. goto loser;
  1342. }
  1343. rv = EncodeDBNicknameKey(nickname, arena, &dbkey);
  1344. if ( rv != SECSuccess ) {
  1345. goto loser;
  1346. }
  1347. rv = DeleteDBEntry(handle, certDBEntryTypeNickname, &dbkey);
  1348. if ( rv == SECFailure ) {
  1349. goto loser;
  1350. }
  1351. PORT_FreeArena(arena, PR_FALSE);
  1352. return(SECSuccess);
  1353. loser:
  1354. if ( arena ) {
  1355. PORT_FreeArena(arena, PR_FALSE);
  1356. }
  1357. return(SECFailure);
  1358. }
  1359. /*
  1360. * Read a nickname entry
  1361. */
  1362. static certDBEntryNickname *
  1363. ReadDBNicknameEntry(NSSLOWCERTCertDBHandle *handle, char *nickname)
  1364. {
  1365. PRArenaPool *arena = NULL;
  1366. PRArenaPool *tmparena = NULL;
  1367. certDBEntryNickname *entry;
  1368. SECItem dbkey;
  1369. SECItem dbentry;
  1370. SECStatus rv;
  1371. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1372. if ( arena == NULL ) {
  1373. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1374. goto loser;
  1375. }
  1376. tmparena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1377. if ( tmparena == NULL ) {
  1378. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1379. goto loser;
  1380. }
  1381. entry = (certDBEntryNickname *)PORT_ArenaAlloc(arena,
  1382. sizeof(certDBEntryNickname));
  1383. if ( entry == NULL ) {
  1384. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1385. goto loser;
  1386. }
  1387. entry->common.arena = arena;
  1388. entry->common.type = certDBEntryTypeNickname;
  1389. rv = EncodeDBNicknameKey(nickname, tmparena, &dbkey);
  1390. if ( rv != SECSuccess ) {
  1391. goto loser;
  1392. }
  1393. rv = ReadDBEntry(handle, &entry->common, &dbkey, &dbentry, tmparena);
  1394. if ( rv == SECFailure ) {
  1395. goto loser;
  1396. }
  1397. /* is record long enough for header? */
  1398. if ( dbentry.len < DB_NICKNAME_ENTRY_HEADER_LEN ) {
  1399. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  1400. goto loser;
  1401. }
  1402. rv = DecodeDBNicknameEntry(entry, &dbentry, nickname);
  1403. if ( rv != SECSuccess ) {
  1404. goto loser;
  1405. }
  1406. PORT_FreeArena(tmparena, PR_FALSE);
  1407. return(entry);
  1408. loser:
  1409. if ( tmparena ) {
  1410. PORT_FreeArena(tmparena, PR_FALSE);
  1411. }
  1412. if ( arena ) {
  1413. PORT_FreeArena(arena, PR_FALSE);
  1414. }
  1415. return(NULL);
  1416. }
  1417. /*
  1418. * Encode a nickname entry into byte stream suitable for
  1419. * the database
  1420. */
  1421. static SECStatus
  1422. WriteDBNicknameEntry(NSSLOWCERTCertDBHandle *handle, certDBEntryNickname *entry)
  1423. {
  1424. SECItem dbitem, dbkey;
  1425. PRArenaPool *tmparena = NULL;
  1426. SECStatus rv;
  1427. tmparena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1428. if ( tmparena == NULL ) {
  1429. goto loser;
  1430. }
  1431. rv = EncodeDBNicknameEntry(entry, tmparena, &dbitem);
  1432. if ( rv != SECSuccess ) {
  1433. goto loser;
  1434. }
  1435. rv = EncodeDBNicknameKey(entry->nickname, tmparena, &dbkey);
  1436. if ( rv != SECSuccess ) {
  1437. goto loser;
  1438. }
  1439. /* now write it to the database */
  1440. rv = WriteDBEntry(handle, &entry->common, &dbkey, &dbitem);
  1441. if ( rv != SECSuccess ) {
  1442. goto loser;
  1443. }
  1444. PORT_FreeArena(tmparena, PR_FALSE);
  1445. return(SECSuccess);
  1446. loser:
  1447. if ( tmparena ) {
  1448. PORT_FreeArena(tmparena, PR_FALSE);
  1449. }
  1450. return(SECFailure);
  1451. }
  1452. static SECStatus
  1453. EncodeDBSMimeEntry(certDBEntrySMime *entry, PRArenaPool *arena,
  1454. SECItem *dbitem)
  1455. {
  1456. unsigned char *buf;
  1457. /* allocate space for encoded database record, including space
  1458. * for low level header
  1459. */
  1460. dbitem->len = entry->subjectName.len + entry->smimeOptions.len +
  1461. entry->optionsDate.len +
  1462. DB_SMIME_ENTRY_HEADER_LEN + SEC_DB_ENTRY_HEADER_LEN;
  1463. dbitem->data = (unsigned char *)PORT_ArenaAlloc(arena, dbitem->len);
  1464. if ( dbitem->data == NULL) {
  1465. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1466. goto loser;
  1467. }
  1468. /* fill in database record */
  1469. buf = &dbitem->data[SEC_DB_ENTRY_HEADER_LEN];
  1470. buf[0] = (PRUint8)( entry->subjectName.len >> 8 );
  1471. buf[1] = (PRUint8)( entry->subjectName.len );
  1472. buf[2] = (PRUint8)( entry->smimeOptions.len >> 8 );
  1473. buf[3] = (PRUint8)( entry->smimeOptions.len );
  1474. buf[4] = (PRUint8)( entry->optionsDate.len >> 8 );
  1475. buf[5] = (PRUint8)( entry->optionsDate.len );
  1476. /* if no smime options, then there should not be an options date either */
  1477. PORT_Assert( ! ( ( entry->smimeOptions.len == 0 ) &&
  1478. ( entry->optionsDate.len != 0 ) ) );
  1479. PORT_Memcpy(&buf[DB_SMIME_ENTRY_HEADER_LEN], entry->subjectName.data,
  1480. entry->subjectName.len);
  1481. if ( entry->smimeOptions.len ) {
  1482. PORT_Memcpy(&buf[DB_SMIME_ENTRY_HEADER_LEN+entry->subjectName.len],
  1483. entry->smimeOptions.data,
  1484. entry->smimeOptions.len);
  1485. PORT_Memcpy(&buf[DB_SMIME_ENTRY_HEADER_LEN + entry->subjectName.len +
  1486. entry->smimeOptions.len],
  1487. entry->optionsDate.data,
  1488. entry->optionsDate.len);
  1489. }
  1490. return(SECSuccess);
  1491. loser:
  1492. return(SECFailure);
  1493. }
  1494. /*
  1495. * Encode a database key for a SMIME record
  1496. */
  1497. static SECStatus
  1498. EncodeDBSMimeKey(char *emailAddr, PRArenaPool *arena,
  1499. SECItem *dbkey)
  1500. {
  1501. unsigned int addrlen;
  1502. addrlen = PORT_Strlen(emailAddr) + 1; /* includes null */
  1503. /* now get the database key and format it */
  1504. dbkey->len = addrlen + SEC_DB_KEY_HEADER_LEN;
  1505. if (dbkey->len > NSS_MAX_LEGACY_DB_KEY_SIZE)
  1506. goto loser;
  1507. dbkey->data = (unsigned char *)PORT_ArenaAlloc(arena, dbkey->len);
  1508. if ( dbkey->data == NULL ) {
  1509. goto loser;
  1510. }
  1511. PORT_Memcpy(&dbkey->data[SEC_DB_KEY_HEADER_LEN], emailAddr, addrlen);
  1512. dbkey->data[0] = certDBEntryTypeSMimeProfile;
  1513. return(SECSuccess);
  1514. loser:
  1515. return(SECFailure);
  1516. }
  1517. /*
  1518. * Decode a database SMIME record
  1519. */
  1520. static SECStatus
  1521. DecodeDBSMimeEntry(certDBEntrySMime *entry, SECItem *dbentry, char *emailAddr)
  1522. {
  1523. int lenDiff;
  1524. /* is record long enough for header? */
  1525. if ( dbentry->len < DB_SMIME_ENTRY_HEADER_LEN ) {
  1526. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  1527. goto loser;
  1528. }
  1529. /* is database entry correct length? */
  1530. entry->subjectName.len = (( dbentry->data[0] << 8 ) | dbentry->data[1] );
  1531. entry->smimeOptions.len = (( dbentry->data[2] << 8 ) | dbentry->data[3] );
  1532. entry->optionsDate.len = (( dbentry->data[4] << 8 ) | dbentry->data[5] );
  1533. lenDiff = dbentry->len - (entry->subjectName.len +
  1534. entry->smimeOptions.len +
  1535. entry->optionsDate.len +
  1536. DB_SMIME_ENTRY_HEADER_LEN);
  1537. if (lenDiff) {
  1538. if (lenDiff < 0 || (lenDiff & 0xffff) != 0 ) {
  1539. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  1540. goto loser;
  1541. }
  1542. /* The entry size exceeded 64KB. Reconstruct the correct length. */
  1543. entry->subjectName.len += lenDiff;
  1544. }
  1545. /* copy the subject name */
  1546. entry->subjectName.data =
  1547. (unsigned char *)PORT_ArenaAlloc(entry->common.arena,
  1548. entry->subjectName.len);
  1549. if ( entry->subjectName.data == NULL ) {
  1550. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1551. goto loser;
  1552. }
  1553. PORT_Memcpy(entry->subjectName.data,
  1554. &dbentry->data[DB_SMIME_ENTRY_HEADER_LEN],
  1555. entry->subjectName.len);
  1556. /* copy the smime options */
  1557. if ( entry->smimeOptions.len ) {
  1558. entry->smimeOptions.data =
  1559. (unsigned char *)PORT_ArenaAlloc(entry->common.arena,
  1560. entry->smimeOptions.len);
  1561. if ( entry->smimeOptions.data == NULL ) {
  1562. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1563. goto loser;
  1564. }
  1565. PORT_Memcpy(entry->smimeOptions.data,
  1566. &dbentry->data[DB_SMIME_ENTRY_HEADER_LEN +
  1567. entry->subjectName.len],
  1568. entry->smimeOptions.len);
  1569. }
  1570. if ( entry->optionsDate.len ) {
  1571. entry->optionsDate.data =
  1572. (unsigned char *)PORT_ArenaAlloc(entry->common.arena,
  1573. entry->optionsDate.len);
  1574. if ( entry->optionsDate.data == NULL ) {
  1575. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1576. goto loser;
  1577. }
  1578. PORT_Memcpy(entry->optionsDate.data,
  1579. &dbentry->data[DB_SMIME_ENTRY_HEADER_LEN +
  1580. entry->subjectName.len +
  1581. entry->smimeOptions.len],
  1582. entry->optionsDate.len);
  1583. }
  1584. /* both options and options date must either exist or not exist */
  1585. if ( ( ( entry->optionsDate.len == 0 ) ||
  1586. ( entry->smimeOptions.len == 0 ) ) &&
  1587. entry->smimeOptions.len != entry->optionsDate.len ) {
  1588. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  1589. goto loser;
  1590. }
  1591. entry->emailAddr = (char *)PORT_ArenaAlloc(entry->common.arena,
  1592. PORT_Strlen(emailAddr)+1);
  1593. if ( entry->emailAddr ) {
  1594. PORT_Strcpy(entry->emailAddr, emailAddr);
  1595. }
  1596. return(SECSuccess);
  1597. loser:
  1598. return(SECFailure);
  1599. }
  1600. /*
  1601. * create a new SMIME entry
  1602. */
  1603. static certDBEntrySMime *
  1604. NewDBSMimeEntry(char *emailAddr, SECItem *subjectName, SECItem *smimeOptions,
  1605. SECItem *optionsDate, unsigned int flags)
  1606. {
  1607. PRArenaPool *arena = NULL;
  1608. certDBEntrySMime *entry;
  1609. int addrlen;
  1610. SECStatus rv;
  1611. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1612. if ( arena == NULL ) {
  1613. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1614. goto loser;
  1615. }
  1616. entry = (certDBEntrySMime *)PORT_ArenaAlloc(arena,
  1617. sizeof(certDBEntrySMime));
  1618. if ( entry == NULL ) {
  1619. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1620. goto loser;
  1621. }
  1622. /* init common fields */
  1623. entry->common.arena = arena;
  1624. entry->common.type = certDBEntryTypeSMimeProfile;
  1625. entry->common.version = CERT_DB_FILE_VERSION;
  1626. entry->common.flags = flags;
  1627. /* copy the email addr */
  1628. addrlen = PORT_Strlen(emailAddr) + 1;
  1629. entry->emailAddr = (char*)PORT_ArenaAlloc(arena, addrlen);
  1630. if ( entry->emailAddr == NULL ) {
  1631. goto loser;
  1632. }
  1633. PORT_Memcpy(entry->emailAddr, emailAddr, addrlen);
  1634. /* copy the subject name */
  1635. rv = SECITEM_CopyItem(arena, &entry->subjectName, subjectName);
  1636. if ( rv != SECSuccess ) {
  1637. goto loser;
  1638. }
  1639. /* copy the smime options */
  1640. if ( smimeOptions ) {
  1641. rv = SECITEM_CopyItem(arena, &entry->smimeOptions, smimeOptions);
  1642. if ( rv != SECSuccess ) {
  1643. goto loser;
  1644. }
  1645. } else {
  1646. PORT_Assert(optionsDate == NULL);
  1647. entry->smimeOptions.data = NULL;
  1648. entry->smimeOptions.len = 0;
  1649. }
  1650. /* copy the options date */
  1651. if ( optionsDate ) {
  1652. rv = SECITEM_CopyItem(arena, &entry->optionsDate, optionsDate);
  1653. if ( rv != SECSuccess ) {
  1654. goto loser;
  1655. }
  1656. } else {
  1657. PORT_Assert(smimeOptions == NULL);
  1658. entry->optionsDate.data = NULL;
  1659. entry->optionsDate.len = 0;
  1660. }
  1661. return(entry);
  1662. loser:
  1663. if ( arena ) {
  1664. PORT_FreeArena(arena, PR_FALSE);
  1665. }
  1666. return(NULL);
  1667. }
  1668. /*
  1669. * delete a SMIME entry
  1670. */
  1671. static SECStatus
  1672. DeleteDBSMimeEntry(NSSLOWCERTCertDBHandle *handle, char *emailAddr)
  1673. {
  1674. PRArenaPool *arena = NULL;
  1675. SECStatus rv;
  1676. SECItem dbkey;
  1677. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1678. if ( arena == NULL ) {
  1679. goto loser;
  1680. }
  1681. rv = EncodeDBSMimeKey(emailAddr, arena, &dbkey);
  1682. if ( rv != SECSuccess ) {
  1683. goto loser;
  1684. }
  1685. rv = DeleteDBEntry(handle, certDBEntryTypeSMimeProfile, &dbkey);
  1686. if ( rv == SECFailure ) {
  1687. goto loser;
  1688. }
  1689. PORT_FreeArena(arena, PR_FALSE);
  1690. return(SECSuccess);
  1691. loser:
  1692. if ( arena ) {
  1693. PORT_FreeArena(arena, PR_FALSE);
  1694. }
  1695. return(SECFailure);
  1696. }
  1697. /*
  1698. * Read a SMIME entry
  1699. */
  1700. certDBEntrySMime *
  1701. nsslowcert_ReadDBSMimeEntry(NSSLOWCERTCertDBHandle *handle, char *emailAddr)
  1702. {
  1703. PRArenaPool *arena = NULL;
  1704. PRArenaPool *tmparena = NULL;
  1705. certDBEntrySMime *entry;
  1706. SECItem dbkey;
  1707. SECItem dbentry;
  1708. SECStatus rv;
  1709. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1710. if ( arena == NULL ) {
  1711. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1712. goto loser;
  1713. }
  1714. tmparena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1715. if ( tmparena == NULL ) {
  1716. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1717. goto loser;
  1718. }
  1719. entry = (certDBEntrySMime *)PORT_ArenaAlloc(arena,
  1720. sizeof(certDBEntrySMime));
  1721. if ( entry == NULL ) {
  1722. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1723. goto loser;
  1724. }
  1725. entry->common.arena = arena;
  1726. entry->common.type = certDBEntryTypeSMimeProfile;
  1727. rv = EncodeDBSMimeKey(emailAddr, tmparena, &dbkey);
  1728. if ( rv != SECSuccess ) {
  1729. goto loser;
  1730. }
  1731. rv = ReadDBEntry(handle, &entry->common, &dbkey, &dbentry, tmparena);
  1732. if ( rv == SECFailure ) {
  1733. goto loser;
  1734. }
  1735. /* is record long enough for header? */
  1736. if ( dbentry.len < DB_SMIME_ENTRY_HEADER_LEN ) {
  1737. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  1738. goto loser;
  1739. }
  1740. rv = DecodeDBSMimeEntry(entry, &dbentry, emailAddr);
  1741. if ( rv != SECSuccess ) {
  1742. goto loser;
  1743. }
  1744. PORT_FreeArena(tmparena, PR_FALSE);
  1745. return(entry);
  1746. loser:
  1747. if ( tmparena ) {
  1748. PORT_FreeArena(tmparena, PR_FALSE);
  1749. }
  1750. if ( arena ) {
  1751. PORT_FreeArena(arena, PR_FALSE);
  1752. }
  1753. return(NULL);
  1754. }
  1755. /*
  1756. * Encode a SMIME entry into byte stream suitable for
  1757. * the database
  1758. */
  1759. static SECStatus
  1760. WriteDBSMimeEntry(NSSLOWCERTCertDBHandle *handle, certDBEntrySMime *entry)
  1761. {
  1762. SECItem dbitem, dbkey;
  1763. PRArenaPool *tmparena = NULL;
  1764. SECStatus rv;
  1765. tmparena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  1766. if ( tmparena == NULL ) {
  1767. goto loser;
  1768. }
  1769. rv = EncodeDBSMimeEntry(entry, tmparena, &dbitem);
  1770. if ( rv != SECSuccess ) {
  1771. goto loser;
  1772. }
  1773. rv = EncodeDBSMimeKey(entry->emailAddr, tmparena, &dbkey);
  1774. if ( rv != SECSuccess ) {
  1775. goto loser;
  1776. }
  1777. /* now write it to the database */
  1778. rv = WriteDBEntry(handle, &entry->common, &dbkey, &dbitem);
  1779. if ( rv != SECSuccess ) {
  1780. goto loser;
  1781. }
  1782. PORT_FreeArena(tmparena, PR_FALSE);
  1783. return(SECSuccess);
  1784. loser:
  1785. if ( tmparena ) {
  1786. PORT_FreeArena(tmparena, PR_FALSE);
  1787. }
  1788. return(SECFailure);
  1789. }
  1790. /*
  1791. * Encode a database subject record
  1792. */
  1793. static SECStatus
  1794. EncodeDBSubjectEntry(certDBEntrySubject *entry, PRArenaPool *arena,
  1795. SECItem *dbitem)
  1796. {
  1797. unsigned char *buf;
  1798. int len;
  1799. unsigned int ncerts;
  1800. unsigned int i;
  1801. unsigned char *tmpbuf;
  1802. unsigned int nnlen = 0;
  1803. unsigned int eaddrslen = 0;
  1804. int keyidoff;
  1805. SECItem *certKeys = entry->certKeys;
  1806. SECItem *keyIDs = entry->keyIDs;;
  1807. if ( entry->nickname ) {
  1808. nnlen = PORT_Strlen(entry->nickname) + 1;
  1809. }
  1810. if ( entry->emailAddrs ) {
  1811. eaddrslen = 2;
  1812. for (i=0; i < entry->nemailAddrs; i++) {
  1813. eaddrslen += PORT_Strlen(entry->emailAddrs[i]) + 1 + 2;
  1814. }
  1815. }
  1816. ncerts = entry->ncerts;
  1817. /* compute the length of the entry */
  1818. keyidoff = DB_SUBJECT_ENTRY_HEADER_LEN + nnlen ;
  1819. len = keyidoff + (4 * ncerts) + eaddrslen;
  1820. for ( i = 0; i < ncerts; i++ ) {
  1821. if (keyIDs[i].len > 0xffff ||
  1822. (certKeys[i].len > 0xffff)) {
  1823. PORT_SetError(SEC_ERROR_INPUT_LEN);
  1824. goto loser;
  1825. }
  1826. len += certKeys[i].len;
  1827. len += keyIDs[i].len;
  1828. }
  1829. /* allocate space for encoded database record, including space
  1830. * for low level header
  1831. */
  1832. dbitem->len = len + SEC_DB_ENTRY_HEADER_LEN;
  1833. dbitem->data = (unsigned char *)PORT_ArenaAlloc(arena, dbitem->len);
  1834. if ( dbitem->data == NULL) {
  1835. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1836. goto loser;
  1837. }
  1838. /* fill in database record */
  1839. buf = &dbitem->data[SEC_DB_ENTRY_HEADER_LEN];
  1840. buf[0] = (PRUint8)( ncerts >> 8 );
  1841. buf[1] = (PRUint8)( ncerts );
  1842. buf[2] = (PRUint8)( nnlen >> 8 );
  1843. buf[3] = (PRUint8)( nnlen );
  1844. /* v7 email field is NULL in v8 */
  1845. buf[4] = 0;
  1846. buf[5] = 0;
  1847. PORT_Memcpy(&buf[DB_SUBJECT_ENTRY_HEADER_LEN], entry->nickname, nnlen);
  1848. tmpbuf = &buf[keyidoff];
  1849. for ( i = 0; i < ncerts; i++ ) {
  1850. tmpbuf[0] = (PRUint8)( certKeys[i].len >> 8 );
  1851. tmpbuf[1] = (PRUint8)( certKeys[i].len );
  1852. tmpbuf += 2;
  1853. }
  1854. for ( i = 0; i < ncerts; i++ ) {
  1855. tmpbuf[0] = (PRUint8)( keyIDs[i].len >> 8 );
  1856. tmpbuf[1] = (PRUint8)( keyIDs[i].len );
  1857. tmpbuf += 2;
  1858. }
  1859. for ( i = 0; i < ncerts; i++ ) {
  1860. PORT_Memcpy(tmpbuf, certKeys[i].data, certKeys[i].len);
  1861. tmpbuf += certKeys[i].len;
  1862. }
  1863. for ( i = 0; i < ncerts; i++ ) {
  1864. PORT_Memcpy(tmpbuf, keyIDs[i].data, keyIDs[i].len);
  1865. tmpbuf += keyIDs[i].len;
  1866. }
  1867. if (entry->emailAddrs) {
  1868. tmpbuf[0] = (PRUint8)( entry->nemailAddrs >> 8 );
  1869. tmpbuf[1] = (PRUint8)( entry->nemailAddrs );
  1870. tmpbuf += 2;
  1871. for (i=0; i < entry->nemailAddrs; i++) {
  1872. int nameLen = PORT_Strlen(entry->emailAddrs[i]) + 1;
  1873. tmpbuf[0] = (PRUint8)( nameLen >> 8 );
  1874. tmpbuf[1] = (PRUint8)( nameLen );
  1875. tmpbuf += 2;
  1876. PORT_Memcpy(tmpbuf,entry->emailAddrs[i],nameLen);
  1877. tmpbuf +=nameLen;
  1878. }
  1879. }
  1880. PORT_Assert(tmpbuf == &buf[len]);
  1881. return(SECSuccess);
  1882. loser:
  1883. return(SECFailure);
  1884. }
  1885. /*
  1886. * Encode a database key for a subject record
  1887. */
  1888. static SECStatus
  1889. EncodeDBSubjectKey(SECItem *derSubject, PRArenaPool *arena,
  1890. SECItem *dbkey)
  1891. {
  1892. dbkey->len = derSubject->len + SEC_DB_KEY_HEADER_LEN;
  1893. if (dbkey->len > NSS_MAX_LEGACY_DB_KEY_SIZE)
  1894. goto loser;
  1895. dbkey->data = (unsigned char *)PORT_ArenaAlloc(arena, dbkey->len);
  1896. if ( dbkey->data == NULL ) {
  1897. goto loser;
  1898. }
  1899. PORT_Memcpy(&dbkey->data[SEC_DB_KEY_HEADER_LEN], derSubject->data,
  1900. derSubject->len);
  1901. dbkey->data[0] = certDBEntryTypeSubject;
  1902. return(SECSuccess);
  1903. loser:
  1904. return(SECFailure);
  1905. }
  1906. static SECStatus
  1907. DecodeDBSubjectEntry(certDBEntrySubject *entry, SECItem *dbentry,
  1908. const SECItem *derSubject)
  1909. {
  1910. PRArenaPool *arena = entry->common.arena;
  1911. unsigned char *tmpbuf;
  1912. unsigned char *end;
  1913. void *mark = PORT_ArenaMark(arena);
  1914. unsigned int eaddrlen;
  1915. unsigned int i;
  1916. unsigned int keyidoff;
  1917. unsigned int len;
  1918. unsigned int ncerts = 0;
  1919. unsigned int nnlen;
  1920. SECStatus rv;
  1921. rv = SECITEM_CopyItem(arena, &entry->derSubject, derSubject);
  1922. if ( rv != SECSuccess ) {
  1923. goto loser;
  1924. }
  1925. /* is record long enough for header? */
  1926. if ( dbentry->len < DB_SUBJECT_ENTRY_HEADER_LEN ) {
  1927. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  1928. goto loser;
  1929. }
  1930. entry->ncerts = ncerts = (( dbentry->data[0] << 8 ) | dbentry->data[1] );
  1931. nnlen = (( dbentry->data[2] << 8 ) | dbentry->data[3] );
  1932. eaddrlen = (( dbentry->data[4] << 8 ) | dbentry->data[5] );
  1933. keyidoff = DB_SUBJECT_ENTRY_HEADER_LEN + nnlen + eaddrlen;
  1934. len = keyidoff + (4 * ncerts);
  1935. if ( dbentry->len < len) {
  1936. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  1937. goto loser;
  1938. }
  1939. entry->certKeys = PORT_ArenaNewArray(arena, SECItem, ncerts);
  1940. entry->keyIDs = PORT_ArenaNewArray(arena, SECItem, ncerts);
  1941. if ( ( entry->certKeys == NULL ) || ( entry->keyIDs == NULL ) ) {
  1942. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1943. goto loser;
  1944. }
  1945. if ( nnlen > 1 ) { /* null terminator is stored */
  1946. entry->nickname = (char *)PORT_ArenaAlloc(arena, nnlen);
  1947. if ( entry->nickname == NULL ) {
  1948. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1949. goto loser;
  1950. }
  1951. PORT_Memcpy(entry->nickname,
  1952. &dbentry->data[DB_SUBJECT_ENTRY_HEADER_LEN],
  1953. nnlen);
  1954. } else {
  1955. entry->nickname = NULL;
  1956. }
  1957. /* if we have an old style email entry, there is only one */
  1958. entry->nemailAddrs = 0;
  1959. if ( eaddrlen > 1 ) { /* null terminator is stored */
  1960. entry->emailAddrs = PORT_ArenaNewArray(arena, char *, 2);
  1961. if ( entry->emailAddrs == NULL ) {
  1962. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1963. goto loser;
  1964. }
  1965. entry->emailAddrs[0] = (char *)PORT_ArenaAlloc(arena, eaddrlen);
  1966. if ( entry->emailAddrs[0] == NULL ) {
  1967. PORT_SetError(SEC_ERROR_NO_MEMORY);
  1968. goto loser;
  1969. }
  1970. PORT_Memcpy(entry->emailAddrs[0],
  1971. &dbentry->data[DB_SUBJECT_ENTRY_HEADER_LEN+nnlen],
  1972. eaddrlen);
  1973. entry->nemailAddrs = 1;
  1974. } else {
  1975. entry->emailAddrs = NULL;
  1976. }
  1977. /* collect the lengths of the certKeys and keyIDs, and total the
  1978. * overall length.
  1979. */
  1980. tmpbuf = &dbentry->data[keyidoff];
  1981. for ( i = 0; i < ncerts; i++ ) {
  1982. unsigned int itemlen = ( tmpbuf[0] << 8 ) | tmpbuf[1];
  1983. entry->certKeys[i].len = itemlen;
  1984. len += itemlen;
  1985. tmpbuf += 2;
  1986. }
  1987. for ( i = 0; i < ncerts; i++ ) {
  1988. unsigned int itemlen = ( tmpbuf[0] << 8 ) | tmpbuf[1] ;
  1989. entry->keyIDs[i].len = itemlen;
  1990. len += itemlen;
  1991. tmpbuf += 2;
  1992. }
  1993. /* is encoded entry large enough ? */
  1994. if ( len > dbentry->len ){
  1995. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  1996. goto loser;
  1997. }
  1998. for ( i = 0; i < ncerts; i++ ) {
  1999. unsigned int kLen = entry->certKeys[i].len;
  2000. entry->certKeys[i].data = (unsigned char *)PORT_ArenaAlloc(arena, kLen);
  2001. if ( entry->certKeys[i].data == NULL ) {
  2002. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2003. goto loser;
  2004. }
  2005. PORT_Memcpy(entry->certKeys[i].data, tmpbuf, kLen);
  2006. tmpbuf += kLen;
  2007. }
  2008. for ( i = 0; i < ncerts; i++ ) {
  2009. unsigned int iLen = entry->keyIDs[i].len;
  2010. entry->keyIDs[i].data = (unsigned char *)PORT_ArenaAlloc(arena, iLen);
  2011. if ( entry->keyIDs[i].data == NULL ) {
  2012. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2013. goto loser;
  2014. }
  2015. PORT_Memcpy(entry->keyIDs[i].data, tmpbuf, iLen);
  2016. tmpbuf += iLen;
  2017. }
  2018. end = dbentry->data + dbentry->len;
  2019. if ((eaddrlen == 0) && (end - tmpbuf > 1)) {
  2020. /* read in the additional email addresses */
  2021. entry->nemailAddrs = (((unsigned int)tmpbuf[0]) << 8) | tmpbuf[1];
  2022. tmpbuf += 2;
  2023. if (end - tmpbuf < 2 * (int)entry->nemailAddrs)
  2024. goto loser;
  2025. entry->emailAddrs = PORT_ArenaNewArray(arena, char *, entry->nemailAddrs);
  2026. if (entry->emailAddrs == NULL) {
  2027. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2028. goto loser;
  2029. }
  2030. for (i=0; i < entry->nemailAddrs; i++) {
  2031. int nameLen;
  2032. if (end - tmpbuf < 2) {
  2033. goto loser;
  2034. }
  2035. nameLen = (((int)tmpbuf[0]) << 8) | tmpbuf[1];
  2036. tmpbuf += 2;
  2037. if (end - tmpbuf < nameLen) {
  2038. goto loser;
  2039. }
  2040. entry->emailAddrs[i] = PORT_ArenaAlloc(arena,nameLen);
  2041. if (entry->emailAddrs == NULL) {
  2042. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2043. goto loser;
  2044. }
  2045. PORT_Memcpy(entry->emailAddrs[i], tmpbuf, nameLen);
  2046. tmpbuf += nameLen;
  2047. }
  2048. if (tmpbuf != end)
  2049. goto loser;
  2050. }
  2051. PORT_ArenaUnmark(arena, mark);
  2052. return(SECSuccess);
  2053. loser:
  2054. PORT_ArenaRelease(arena, mark); /* discard above allocations */
  2055. return(SECFailure);
  2056. }
  2057. /*
  2058. * create a new subject entry with a single cert
  2059. */
  2060. static certDBEntrySubject *
  2061. NewDBSubjectEntry(SECItem *derSubject, SECItem *certKey,
  2062. SECItem *keyID, char *nickname, char *emailAddr,
  2063. unsigned int flags)
  2064. {
  2065. PRArenaPool *arena = NULL;
  2066. certDBEntrySubject *entry;
  2067. SECStatus rv;
  2068. unsigned int nnlen;
  2069. unsigned int eaddrlen;
  2070. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  2071. if ( arena == NULL ) {
  2072. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2073. goto loser;
  2074. }
  2075. entry = (certDBEntrySubject *)PORT_ArenaAlloc(arena,
  2076. sizeof(certDBEntrySubject));
  2077. if ( entry == NULL ) {
  2078. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2079. goto loser;
  2080. }
  2081. /* init common fields */
  2082. entry->common.arena = arena;
  2083. entry->common.type = certDBEntryTypeSubject;
  2084. entry->common.version = CERT_DB_FILE_VERSION;
  2085. entry->common.flags = flags;
  2086. /* copy the subject */
  2087. rv = SECITEM_CopyItem(arena, &entry->derSubject, derSubject);
  2088. if ( rv != SECSuccess ) {
  2089. goto loser;
  2090. }
  2091. entry->ncerts = 1;
  2092. entry->nemailAddrs = 0;
  2093. /* copy nickname */
  2094. if ( nickname && ( *nickname != '\0' ) ) {
  2095. nnlen = PORT_Strlen(nickname) + 1;
  2096. entry->nickname = (char *)PORT_ArenaAlloc(arena, nnlen);
  2097. if ( entry->nickname == NULL ) {
  2098. goto loser;
  2099. }
  2100. PORT_Memcpy(entry->nickname, nickname, nnlen);
  2101. } else {
  2102. entry->nickname = NULL;
  2103. }
  2104. /* copy email addr */
  2105. if ( emailAddr && ( *emailAddr != '\0' ) ) {
  2106. emailAddr = nsslowcert_FixupEmailAddr(emailAddr);
  2107. if ( emailAddr == NULL ) {
  2108. entry->emailAddrs = NULL;
  2109. goto loser;
  2110. }
  2111. eaddrlen = PORT_Strlen(emailAddr) + 1;
  2112. entry->emailAddrs = (char **)PORT_ArenaAlloc(arena, sizeof(char *));
  2113. if ( entry->emailAddrs == NULL ) {
  2114. PORT_Free(emailAddr);
  2115. goto loser;
  2116. }
  2117. entry->emailAddrs[0] = PORT_ArenaStrdup(arena,emailAddr);
  2118. if (entry->emailAddrs[0]) {
  2119. entry->nemailAddrs = 1;
  2120. }
  2121. PORT_Free(emailAddr);
  2122. } else {
  2123. entry->emailAddrs = NULL;
  2124. }
  2125. /* allocate space for certKeys and keyIDs */
  2126. entry->certKeys = (SECItem *)PORT_ArenaAlloc(arena, sizeof(SECItem));
  2127. entry->keyIDs = (SECItem *)PORT_ArenaAlloc(arena, sizeof(SECItem));
  2128. if ( ( entry->certKeys == NULL ) || ( entry->keyIDs == NULL ) ) {
  2129. goto loser;
  2130. }
  2131. /* copy the certKey and keyID */
  2132. rv = SECITEM_CopyItem(arena, &entry->certKeys[0], certKey);
  2133. if ( rv != SECSuccess ) {
  2134. goto loser;
  2135. }
  2136. rv = SECITEM_CopyItem(arena, &entry->keyIDs[0], keyID);
  2137. if ( rv != SECSuccess ) {
  2138. goto loser;
  2139. }
  2140. return(entry);
  2141. loser:
  2142. if ( arena ) {
  2143. PORT_FreeArena(arena, PR_FALSE);
  2144. }
  2145. return(NULL);
  2146. }
  2147. /*
  2148. * delete a subject entry
  2149. */
  2150. static SECStatus
  2151. DeleteDBSubjectEntry(NSSLOWCERTCertDBHandle *handle, SECItem *derSubject)
  2152. {
  2153. SECItem dbkey;
  2154. PRArenaPool *arena = NULL;
  2155. SECStatus rv;
  2156. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  2157. if ( arena == NULL ) {
  2158. goto loser;
  2159. }
  2160. rv = EncodeDBSubjectKey(derSubject, arena, &dbkey);
  2161. if ( rv != SECSuccess ) {
  2162. goto loser;
  2163. }
  2164. rv = DeleteDBEntry(handle, certDBEntryTypeSubject, &dbkey);
  2165. if ( rv == SECFailure ) {
  2166. goto loser;
  2167. }
  2168. PORT_FreeArena(arena, PR_FALSE);
  2169. return(SECSuccess);
  2170. loser:
  2171. if ( arena ) {
  2172. PORT_FreeArena(arena, PR_FALSE);
  2173. }
  2174. return(SECFailure);
  2175. }
  2176. /*
  2177. * Read the subject entry
  2178. */
  2179. static certDBEntrySubject *
  2180. ReadDBSubjectEntry(NSSLOWCERTCertDBHandle *handle, SECItem *derSubject)
  2181. {
  2182. PRArenaPool *arena = NULL;
  2183. PRArenaPool *tmparena = NULL;
  2184. certDBEntrySubject *entry;
  2185. SECItem dbkey;
  2186. SECItem dbentry;
  2187. SECStatus rv;
  2188. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  2189. if ( arena == NULL ) {
  2190. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2191. goto loser;
  2192. }
  2193. tmparena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  2194. if ( tmparena == NULL ) {
  2195. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2196. goto loser;
  2197. }
  2198. entry = (certDBEntrySubject *)PORT_ArenaAlloc(arena,
  2199. sizeof(certDBEntrySubject));
  2200. if ( entry == NULL ) {
  2201. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2202. goto loser;
  2203. }
  2204. entry->common.arena = arena;
  2205. entry->common.type = certDBEntryTypeSubject;
  2206. rv = EncodeDBSubjectKey(derSubject, tmparena, &dbkey);
  2207. if ( rv != SECSuccess ) {
  2208. goto loser;
  2209. }
  2210. rv = ReadDBEntry(handle, &entry->common, &dbkey, &dbentry, tmparena);
  2211. if ( rv == SECFailure ) {
  2212. goto loser;
  2213. }
  2214. rv = DecodeDBSubjectEntry(entry, &dbentry, derSubject);
  2215. if ( rv == SECFailure ) {
  2216. goto loser;
  2217. }
  2218. PORT_FreeArena(tmparena, PR_FALSE);
  2219. return(entry);
  2220. loser:
  2221. if ( tmparena ) {
  2222. PORT_FreeArena(tmparena, PR_FALSE);
  2223. }
  2224. if ( arena ) {
  2225. PORT_FreeArena(arena, PR_FALSE);
  2226. }
  2227. return(NULL);
  2228. }
  2229. /*
  2230. * Encode a subject name entry into byte stream suitable for
  2231. * the database
  2232. */
  2233. static SECStatus
  2234. WriteDBSubjectEntry(NSSLOWCERTCertDBHandle *handle, certDBEntrySubject *entry)
  2235. {
  2236. SECItem dbitem, dbkey;
  2237. PRArenaPool *tmparena = NULL;
  2238. SECStatus rv;
  2239. tmparena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  2240. if ( tmparena == NULL ) {
  2241. goto loser;
  2242. }
  2243. rv = EncodeDBSubjectEntry(entry, tmparena, &dbitem);
  2244. if ( rv != SECSuccess ) {
  2245. goto loser;
  2246. }
  2247. rv = EncodeDBSubjectKey(&entry->derSubject, tmparena, &dbkey);
  2248. if ( rv != SECSuccess ) {
  2249. goto loser;
  2250. }
  2251. /* now write it to the database */
  2252. rv = WriteDBEntry(handle, &entry->common, &dbkey, &dbitem);
  2253. if ( rv != SECSuccess ) {
  2254. goto loser;
  2255. }
  2256. PORT_FreeArena(tmparena, PR_FALSE);
  2257. return(SECSuccess);
  2258. loser:
  2259. if ( tmparena ) {
  2260. PORT_FreeArena(tmparena, PR_FALSE);
  2261. }
  2262. return(SECFailure);
  2263. }
  2264. typedef enum { nsslowcert_remove, nsslowcert_add } nsslowcertUpdateType;
  2265. static SECStatus
  2266. nsslowcert_UpdateSubjectEmailAddr(NSSLOWCERTCertDBHandle *dbhandle,
  2267. SECItem *derSubject, char *emailAddr, nsslowcertUpdateType updateType)
  2268. {
  2269. certDBEntrySubject *entry = NULL;
  2270. int index = -1, i;
  2271. SECStatus rv;
  2272. if (emailAddr) {
  2273. emailAddr = nsslowcert_FixupEmailAddr(emailAddr);
  2274. if (emailAddr == NULL) {
  2275. return SECFailure;
  2276. }
  2277. } else {
  2278. return SECSuccess;
  2279. }
  2280. entry = ReadDBSubjectEntry(dbhandle,derSubject);
  2281. if (entry == NULL) {
  2282. rv = SECFailure;
  2283. goto done;
  2284. }
  2285. for (i=0; i < (int)(entry->nemailAddrs); i++) {
  2286. if (PORT_Strcmp(entry->emailAddrs[i],emailAddr) == 0) {
  2287. index = i;
  2288. }
  2289. }
  2290. if (updateType == nsslowcert_remove) {
  2291. if (index == -1) {
  2292. rv = SECSuccess;
  2293. goto done;
  2294. }
  2295. entry->nemailAddrs--;
  2296. for (i=index; i < (int)(entry->nemailAddrs); i++) {
  2297. entry->emailAddrs[i] = entry->emailAddrs[i+1];
  2298. }
  2299. } else {
  2300. char **newAddrs = NULL;
  2301. if (index != -1) {
  2302. rv = SECSuccess;
  2303. goto done;
  2304. }
  2305. newAddrs = (char **)PORT_ArenaAlloc(entry->common.arena,
  2306. (entry->nemailAddrs+1)* sizeof(char *));
  2307. if (!newAddrs) {
  2308. rv = SECFailure;
  2309. goto done;
  2310. }
  2311. for (i=0; i < (int)(entry->nemailAddrs); i++) {
  2312. newAddrs[i] = entry->emailAddrs[i];
  2313. }
  2314. newAddrs[entry->nemailAddrs] =
  2315. PORT_ArenaStrdup(entry->common.arena,emailAddr);
  2316. if (!newAddrs[entry->nemailAddrs]) {
  2317. rv = SECFailure;
  2318. goto done;
  2319. }
  2320. entry->emailAddrs = newAddrs;
  2321. entry->nemailAddrs++;
  2322. }
  2323. /* delete the subject entry */
  2324. DeleteDBSubjectEntry(dbhandle, derSubject);
  2325. /* write the new one */
  2326. rv = WriteDBSubjectEntry(dbhandle, entry);
  2327. done:
  2328. if (entry) DestroyDBEntry((certDBEntry *)entry);
  2329. if (emailAddr) PORT_Free(emailAddr);
  2330. return rv;
  2331. }
  2332. /*
  2333. * writes a nickname to an existing subject entry that does not currently
  2334. * have one
  2335. */
  2336. static SECStatus
  2337. AddNicknameToSubject(NSSLOWCERTCertDBHandle *dbhandle,
  2338. NSSLOWCERTCertificate *cert, char *nickname)
  2339. {
  2340. certDBEntrySubject *entry;
  2341. SECStatus rv;
  2342. if ( nickname == NULL ) {
  2343. return(SECFailure);
  2344. }
  2345. entry = ReadDBSubjectEntry(dbhandle,&cert->derSubject);
  2346. PORT_Assert(entry != NULL);
  2347. if ( entry == NULL ) {
  2348. goto loser;
  2349. }
  2350. PORT_Assert(entry->nickname == NULL);
  2351. if ( entry->nickname != NULL ) {
  2352. goto loser;
  2353. }
  2354. entry->nickname = PORT_ArenaStrdup(entry->common.arena, nickname);
  2355. if ( entry->nickname == NULL ) {
  2356. goto loser;
  2357. }
  2358. /* delete the subject entry */
  2359. DeleteDBSubjectEntry(dbhandle, &cert->derSubject);
  2360. /* write the new one */
  2361. rv = WriteDBSubjectEntry(dbhandle, entry);
  2362. if ( rv != SECSuccess ) {
  2363. goto loser;
  2364. }
  2365. return(SECSuccess);
  2366. loser:
  2367. return(SECFailure);
  2368. }
  2369. /*
  2370. * create a new version entry
  2371. */
  2372. static certDBEntryVersion *
  2373. NewDBVersionEntry(unsigned int flags)
  2374. {
  2375. PRArenaPool *arena = NULL;
  2376. certDBEntryVersion *entry;
  2377. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  2378. if ( arena == NULL ) {
  2379. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2380. goto loser;
  2381. }
  2382. entry = (certDBEntryVersion *)PORT_ArenaAlloc(arena,
  2383. sizeof(certDBEntryVersion));
  2384. if ( entry == NULL ) {
  2385. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2386. goto loser;
  2387. }
  2388. entry->common.arena = arena;
  2389. entry->common.type = certDBEntryTypeVersion;
  2390. entry->common.version = CERT_DB_FILE_VERSION;
  2391. entry->common.flags = flags;
  2392. return(entry);
  2393. loser:
  2394. if ( arena ) {
  2395. PORT_FreeArena(arena, PR_FALSE);
  2396. }
  2397. return(NULL);
  2398. }
  2399. /*
  2400. * Read the version entry
  2401. */
  2402. static certDBEntryVersion *
  2403. ReadDBVersionEntry(NSSLOWCERTCertDBHandle *handle)
  2404. {
  2405. PRArenaPool *arena = NULL;
  2406. PRArenaPool *tmparena = NULL;
  2407. certDBEntryVersion *entry;
  2408. SECItem dbkey;
  2409. SECItem dbentry;
  2410. SECStatus rv;
  2411. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  2412. if ( arena == NULL ) {
  2413. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2414. goto loser;
  2415. }
  2416. tmparena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  2417. if ( tmparena == NULL ) {
  2418. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2419. goto loser;
  2420. }
  2421. entry = PORT_ArenaZNew(arena, certDBEntryVersion);
  2422. if ( entry == NULL ) {
  2423. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2424. goto loser;
  2425. }
  2426. entry->common.arena = arena;
  2427. entry->common.type = certDBEntryTypeVersion;
  2428. /* now get the database key and format it */
  2429. dbkey.len = SEC_DB_VERSION_KEY_LEN + SEC_DB_KEY_HEADER_LEN;
  2430. dbkey.data = (unsigned char *)PORT_ArenaAlloc(tmparena, dbkey.len);
  2431. if ( dbkey.data == NULL ) {
  2432. goto loser;
  2433. }
  2434. PORT_Memcpy(&dbkey.data[SEC_DB_KEY_HEADER_LEN], SEC_DB_VERSION_KEY,
  2435. SEC_DB_VERSION_KEY_LEN);
  2436. rv = ReadDBEntry(handle, &entry->common, &dbkey, &dbentry, tmparena);
  2437. if (rv != SECSuccess) {
  2438. goto loser;
  2439. }
  2440. PORT_FreeArena(tmparena, PR_FALSE);
  2441. return(entry);
  2442. loser:
  2443. if ( tmparena ) {
  2444. PORT_FreeArena(tmparena, PR_FALSE);
  2445. }
  2446. if ( arena ) {
  2447. PORT_FreeArena(arena, PR_FALSE);
  2448. }
  2449. return(NULL);
  2450. }
  2451. /*
  2452. * Encode a version entry into byte stream suitable for
  2453. * the database
  2454. */
  2455. static SECStatus
  2456. WriteDBVersionEntry(NSSLOWCERTCertDBHandle *handle, certDBEntryVersion *entry)
  2457. {
  2458. SECItem dbitem, dbkey;
  2459. PRArenaPool *tmparena = NULL;
  2460. SECStatus rv;
  2461. tmparena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  2462. if ( tmparena == NULL ) {
  2463. goto loser;
  2464. }
  2465. /* allocate space for encoded database record, including space
  2466. * for low level header
  2467. */
  2468. dbitem.len = SEC_DB_ENTRY_HEADER_LEN;
  2469. dbitem.data = (unsigned char *)PORT_ArenaAlloc(tmparena, dbitem.len);
  2470. if ( dbitem.data == NULL) {
  2471. PORT_SetError(SEC_ERROR_NO_MEMORY);
  2472. goto loser;
  2473. }
  2474. /* now get the database key and format it */
  2475. dbkey.len = SEC_DB_VERSION_KEY_LEN + SEC_DB_KEY_HEADER_LEN;
  2476. dbkey.data = (unsigned char *)PORT_ArenaAlloc(tmparena, dbkey.len);
  2477. if ( dbkey.data == NULL ) {
  2478. goto loser;
  2479. }
  2480. PORT_Memcpy(&dbkey.data[SEC_DB_KEY_HEADER_LEN], SEC_DB_VERSION_KEY,
  2481. SEC_DB_VERSION_KEY_LEN);
  2482. /* now write it to the database */
  2483. rv = WriteDBEntry(handle, &entry->common, &dbkey, &dbitem);
  2484. if ( rv != SECSuccess ) {
  2485. goto loser;
  2486. }
  2487. PORT_FreeArena(tmparena, PR_FALSE);
  2488. return(SECSuccess);
  2489. loser:
  2490. if ( tmparena ) {
  2491. PORT_FreeArena(tmparena, PR_FALSE);
  2492. }
  2493. return(SECFailure);
  2494. }
  2495. /*
  2496. * cert is no longer a perm cert, but will remain a temp cert
  2497. */
  2498. static SECStatus
  2499. RemovePermSubjectNode(NSSLOWCERTCertificate *cert)
  2500. {
  2501. certDBEntrySubject *entry;
  2502. unsigned int i;
  2503. SECStatus rv;
  2504. entry = ReadDBSubjectEntry(cert->dbhandle,&cert->derSubject);
  2505. if ( entry == NULL ) {
  2506. return(SECFailure);
  2507. }
  2508. PORT_Assert(entry->ncerts);
  2509. rv = SECFailure;
  2510. if ( entry->ncerts > 1 ) {
  2511. for ( i = 0; i < entry->ncerts; i++ ) {
  2512. if ( SECITEM_CompareItem(&entry->certKeys[i], &cert->certKey) ==
  2513. SECEqual ) {
  2514. /* copy rest of list forward one entry */
  2515. for ( i = i + 1; i < entry->ncerts; i++ ) {
  2516. entry->certKeys[i-1] = entry->certKeys[i];
  2517. entry->keyIDs[i-1] = entry->keyIDs[i];
  2518. }
  2519. entry->ncerts--;
  2520. DeleteDBSubjectEntry(cert->dbhandle, &cert->derSubject);
  2521. rv = WriteDBSubjectEntry(cert->dbhandle, entry);
  2522. break;
  2523. }
  2524. }
  2525. } else {
  2526. /* no entries left, delete the perm entry in the DB */
  2527. if ( entry->emailAddrs ) {
  2528. /* if the subject had an email record, then delete it too */
  2529. for (i=0; i < entry->nemailAddrs; i++) {
  2530. DeleteDBSMimeEntry(cert->dbhandle, entry->emailAddrs[i]);
  2531. }
  2532. }
  2533. if ( entry->nickname ) {
  2534. DeleteDBNicknameEntry(cert->dbhandle, entry->nickname);
  2535. }
  2536. DeleteDBSubjectEntry(cert->dbhandle, &cert->derSubject);
  2537. }
  2538. DestroyDBEntry((certDBEntry *)entry);
  2539. return(rv);
  2540. }
  2541. /*
  2542. * add a cert to the perm subject list
  2543. */
  2544. static SECStatus
  2545. AddPermSubjectNode(certDBEntrySubject *entry, NSSLOWCERTCertificate *cert,
  2546. char *nickname)
  2547. {
  2548. SECItem *newCertKeys, *newKeyIDs;
  2549. unsigned int i, new_i;
  2550. SECStatus rv;
  2551. unsigned int ncerts;
  2552. PORT_Assert(entry);
  2553. ncerts = entry->ncerts;
  2554. if ( nickname && entry->nickname ) {
  2555. /* nicknames must be the same */
  2556. PORT_Assert(PORT_Strcmp(nickname, entry->nickname) == 0);
  2557. }
  2558. if ( ( entry->nickname == NULL ) && ( nickname != NULL ) ) {
  2559. /* copy nickname into the entry */
  2560. entry->nickname = PORT_ArenaStrdup(entry->common.arena, nickname);
  2561. if ( entry->nickname == NULL ) {
  2562. return(SECFailure);
  2563. }
  2564. }
  2565. /* a DB entry already exists, so add this cert */
  2566. newCertKeys = PORT_ArenaZNewArray(entry->common.arena, SECItem, ncerts + 1);
  2567. newKeyIDs = PORT_ArenaZNewArray(entry->common.arena, SECItem, ncerts + 1);
  2568. if ( ( newCertKeys == NULL ) || ( newKeyIDs == NULL ) ) {
  2569. return(SECFailure);
  2570. }
  2571. /* Step 1: copy certs older than "cert" into new entry. */
  2572. for ( i = 0, new_i=0; i < ncerts; i++ ) {
  2573. NSSLOWCERTCertificate *cmpcert;
  2574. PRBool isNewer;
  2575. cmpcert = nsslowcert_FindCertByKey(cert->dbhandle,
  2576. &entry->certKeys[i]);
  2577. /* The entry has been corrupted, remove it from the list */
  2578. if (!cmpcert) {
  2579. continue;
  2580. }
  2581. isNewer = nsslowcert_IsNewer(cert, cmpcert);
  2582. nsslowcert_DestroyCertificate(cmpcert);
  2583. if ( isNewer )
  2584. break;
  2585. /* copy this cert entry */
  2586. newCertKeys[new_i] = entry->certKeys[i];
  2587. newKeyIDs[new_i] = entry->keyIDs[i];
  2588. new_i++;
  2589. }
  2590. /* Step 2: Add "cert" to the entry. */
  2591. rv = SECITEM_CopyItem(entry->common.arena, &newCertKeys[new_i],
  2592. &cert->certKey);
  2593. if ( rv != SECSuccess ) {
  2594. return(SECFailure);
  2595. }
  2596. rv = SECITEM_CopyItem(entry->common.arena, &newKeyIDs[new_i],
  2597. &cert->subjectKeyID);
  2598. if ( rv != SECSuccess ) {
  2599. return(SECFailure);
  2600. }
  2601. new_i++;
  2602. /* Step 3: copy remaining certs (if any) from old entry to new. */
  2603. for ( ; i < ncerts; i++ ,new_i++) {
  2604. newCertKeys[new_i] = entry->certKeys[i];
  2605. newKeyIDs[new_i] = entry->keyIDs[i];
  2606. }
  2607. /* update certKeys and keyIDs */
  2608. entry->certKeys = newCertKeys;
  2609. entry->keyIDs = newKeyIDs;
  2610. /* set new count value */
  2611. entry->ncerts = new_i;
  2612. DeleteDBSubjectEntry(cert->dbhandle, &cert->derSubject);
  2613. rv = WriteDBSubjectEntry(cert->dbhandle, entry);
  2614. return(rv);
  2615. }
  2616. SECStatus
  2617. nsslowcert_TraversePermCertsForSubject(NSSLOWCERTCertDBHandle *handle,
  2618. SECItem *derSubject,
  2619. NSSLOWCERTCertCallback cb, void *cbarg)
  2620. {
  2621. certDBEntrySubject *entry;
  2622. unsigned int i;
  2623. NSSLOWCERTCertificate *cert;
  2624. SECStatus rv = SECSuccess;
  2625. entry = ReadDBSubjectEntry(handle, derSubject);
  2626. if ( entry == NULL ) {
  2627. return(SECFailure);
  2628. }
  2629. for( i = 0; i < entry->ncerts; i++ ) {
  2630. cert = nsslowcert_FindCertByKey(handle, &entry->certKeys[i]);
  2631. if (!cert) {
  2632. continue;
  2633. }
  2634. rv = (* cb)(cert, cbarg);
  2635. nsslowcert_DestroyCertificate(cert);
  2636. if ( rv == SECFailure ) {
  2637. break;
  2638. }
  2639. }
  2640. DestroyDBEntry((certDBEntry *)entry);
  2641. return(rv);
  2642. }
  2643. int
  2644. nsslowcert_NumPermCertsForSubject(NSSLOWCERTCertDBHandle *handle,
  2645. SECItem *derSubject)
  2646. {
  2647. certDBEntrySubject *entry;
  2648. int ret;
  2649. entry = ReadDBSubjectEntry(handle, derSubject);
  2650. if ( entry == NULL ) {
  2651. return(SECFailure);
  2652. }
  2653. ret = entry->ncerts;
  2654. DestroyDBEntry((certDBEntry *)entry);
  2655. return(ret);
  2656. }
  2657. SECStatus
  2658. nsslowcert_TraversePermCertsForNickname(NSSLOWCERTCertDBHandle *handle,
  2659. char *nickname, NSSLOWCERTCertCallback cb, void *cbarg)
  2660. {
  2661. certDBEntryNickname *nnentry = NULL;
  2662. certDBEntrySMime *smentry = NULL;
  2663. SECStatus rv;
  2664. SECItem *derSubject = NULL;
  2665. nnentry = ReadDBNicknameEntry(handle, nickname);
  2666. if ( nnentry ) {
  2667. derSubject = &nnentry->subjectName;
  2668. } else {
  2669. smentry = nsslowcert_ReadDBSMimeEntry(handle, nickname);
  2670. if ( smentry ) {
  2671. derSubject = &smentry->subjectName;
  2672. }
  2673. }
  2674. if ( derSubject ) {
  2675. rv = nsslowcert_TraversePermCertsForSubject(handle, derSubject,
  2676. cb, cbarg);
  2677. } else {
  2678. rv = SECFailure;
  2679. }
  2680. if ( nnentry ) {
  2681. DestroyDBEntry((certDBEntry *)nnentry);
  2682. }
  2683. if ( smentry ) {
  2684. DestroyDBEntry((certDBEntry *)smentry);
  2685. }
  2686. return(rv);
  2687. }
  2688. int
  2689. nsslowcert_NumPermCertsForNickname(NSSLOWCERTCertDBHandle *handle,
  2690. char *nickname)
  2691. {
  2692. certDBEntryNickname *entry;
  2693. int ret;
  2694. entry = ReadDBNicknameEntry(handle, nickname);
  2695. if ( entry ) {
  2696. ret = nsslowcert_NumPermCertsForSubject(handle, &entry->subjectName);
  2697. DestroyDBEntry((certDBEntry *)entry);
  2698. } else {
  2699. ret = 0;
  2700. }
  2701. return(ret);
  2702. }
  2703. /*
  2704. * add a nickname to a cert that doesn't have one
  2705. */
  2706. static SECStatus
  2707. AddNicknameToPermCert(NSSLOWCERTCertDBHandle *dbhandle,
  2708. NSSLOWCERTCertificate *cert, char *nickname)
  2709. {
  2710. certDBEntryCert *entry;
  2711. int rv;
  2712. entry = cert->dbEntry;
  2713. PORT_Assert(entry != NULL);
  2714. if ( entry == NULL ) {
  2715. goto loser;
  2716. }
  2717. pkcs11_freeNickname(entry->nickname,entry->nicknameSpace);
  2718. entry->nickname = NULL;
  2719. entry->nickname = pkcs11_copyNickname(nickname,entry->nicknameSpace,
  2720. sizeof(entry->nicknameSpace));
  2721. rv = WriteDBCertEntry(dbhandle, entry);
  2722. if ( rv ) {
  2723. goto loser;
  2724. }
  2725. pkcs11_freeNickname(cert->nickname,cert->nicknameSpace);
  2726. cert->nickname = NULL;
  2727. cert->nickname = pkcs11_copyNickname(nickname,cert->nicknameSpace,
  2728. sizeof(cert->nicknameSpace));
  2729. return(SECSuccess);
  2730. loser:
  2731. return(SECFailure);
  2732. }
  2733. /*
  2734. * add a nickname to a cert that is already in the perm database, but doesn't
  2735. * have one yet (it is probably an e-mail cert).
  2736. */
  2737. SECStatus
  2738. nsslowcert_AddPermNickname(NSSLOWCERTCertDBHandle *dbhandle,
  2739. NSSLOWCERTCertificate *cert, char *nickname)
  2740. {
  2741. SECStatus rv = SECFailure;
  2742. certDBEntrySubject *entry = NULL;
  2743. certDBEntryNickname *nicknameEntry = NULL;
  2744. nsslowcert_LockDB(dbhandle);
  2745. entry = ReadDBSubjectEntry(dbhandle, &cert->derSubject);
  2746. if (entry == NULL) goto loser;
  2747. if ( entry->nickname == NULL ) {
  2748. /* no nickname for subject */
  2749. rv = AddNicknameToSubject(dbhandle, cert, nickname);
  2750. if ( rv != SECSuccess ) {
  2751. goto loser;
  2752. }
  2753. rv = AddNicknameToPermCert(dbhandle, cert, nickname);
  2754. if ( rv != SECSuccess ) {
  2755. goto loser;
  2756. }
  2757. nicknameEntry = NewDBNicknameEntry(nickname, &cert->derSubject, 0);
  2758. if ( nicknameEntry == NULL ) {
  2759. goto loser;
  2760. }
  2761. rv = WriteDBNicknameEntry(dbhandle, nicknameEntry);
  2762. if ( rv != SECSuccess ) {
  2763. goto loser;
  2764. }
  2765. } else {
  2766. /* subject already has a nickname */
  2767. rv = AddNicknameToPermCert(dbhandle, cert, entry->nickname);
  2768. if ( rv != SECSuccess ) {
  2769. goto loser;
  2770. }
  2771. /* make sure nickname entry exists. If the database was corrupted,
  2772. * we may have lost the nickname entry. Add it back now */
  2773. nicknameEntry = ReadDBNicknameEntry(dbhandle, entry->nickname);
  2774. if (nicknameEntry == NULL ) {
  2775. nicknameEntry = NewDBNicknameEntry(entry->nickname,
  2776. &cert->derSubject, 0);
  2777. if ( nicknameEntry == NULL ) {
  2778. goto loser;
  2779. }
  2780. rv = WriteDBNicknameEntry(dbhandle, nicknameEntry);
  2781. if ( rv != SECSuccess ) {
  2782. goto loser;
  2783. }
  2784. }
  2785. }
  2786. rv = SECSuccess;
  2787. loser:
  2788. if (entry) {
  2789. DestroyDBEntry((certDBEntry *)entry);
  2790. }
  2791. if (nicknameEntry) {
  2792. DestroyDBEntry((certDBEntry *)nicknameEntry);
  2793. }
  2794. nsslowcert_UnlockDB(dbhandle);
  2795. return(rv);
  2796. }
  2797. static certDBEntryCert *
  2798. AddCertToPermDB(NSSLOWCERTCertDBHandle *handle, NSSLOWCERTCertificate *cert,
  2799. char *nickname, NSSLOWCERTCertTrust *trust)
  2800. {
  2801. certDBEntryCert *certEntry = NULL;
  2802. certDBEntryNickname *nicknameEntry = NULL;
  2803. certDBEntrySubject *subjectEntry = NULL;
  2804. int state = 0;
  2805. SECStatus rv;
  2806. PRBool donnentry = PR_FALSE;
  2807. if ( nickname ) {
  2808. donnentry = PR_TRUE;
  2809. }
  2810. subjectEntry = ReadDBSubjectEntry(handle, &cert->derSubject);
  2811. if ( subjectEntry && subjectEntry->nickname ) {
  2812. donnentry = PR_FALSE;
  2813. nickname = subjectEntry->nickname;
  2814. }
  2815. certEntry = NewDBCertEntry(&cert->derCert, nickname, trust, 0);
  2816. if ( certEntry == NULL ) {
  2817. goto loser;
  2818. }
  2819. if ( donnentry ) {
  2820. nicknameEntry = NewDBNicknameEntry(nickname, &cert->derSubject, 0);
  2821. if ( nicknameEntry == NULL ) {
  2822. goto loser;
  2823. }
  2824. }
  2825. rv = WriteDBCertEntry(handle, certEntry);
  2826. if ( rv != SECSuccess ) {
  2827. goto loser;
  2828. }
  2829. state = 1;
  2830. if ( nicknameEntry ) {
  2831. rv = WriteDBNicknameEntry(handle, nicknameEntry);
  2832. if ( rv != SECSuccess ) {
  2833. goto loser;
  2834. }
  2835. }
  2836. state = 2;
  2837. /* "Change" handles if necessary */
  2838. cert->dbhandle = handle;
  2839. /* add to or create new subject entry */
  2840. if ( subjectEntry ) {
  2841. /* REWRITE BASED ON SUBJECT ENTRY */
  2842. rv = AddPermSubjectNode(subjectEntry, cert, nickname);
  2843. if ( rv != SECSuccess ) {
  2844. goto loser;
  2845. }
  2846. } else {
  2847. /* make a new subject entry - this case is only used when updating
  2848. * an old version of the database. This is OK because the oldnickname
  2849. * db format didn't allow multiple certs with the same subject.
  2850. */
  2851. /* where does subjectKeyID and certKey come from? */
  2852. subjectEntry = NewDBSubjectEntry(&cert->derSubject, &cert->certKey,
  2853. &cert->subjectKeyID, nickname,
  2854. NULL, 0);
  2855. if ( subjectEntry == NULL ) {
  2856. goto loser;
  2857. }
  2858. rv = WriteDBSubjectEntry(handle, subjectEntry);
  2859. if ( rv != SECSuccess ) {
  2860. goto loser;
  2861. }
  2862. }
  2863. state = 3;
  2864. if ( nicknameEntry ) {
  2865. DestroyDBEntry((certDBEntry *)nicknameEntry);
  2866. }
  2867. if ( subjectEntry ) {
  2868. DestroyDBEntry((certDBEntry *)subjectEntry);
  2869. }
  2870. return(certEntry);
  2871. loser:
  2872. /* don't leave partial entry in the database */
  2873. if ( state > 0 ) {
  2874. rv = DeleteDBCertEntry(handle, &cert->certKey);
  2875. }
  2876. if ( ( state > 1 ) && donnentry ) {
  2877. rv = DeleteDBNicknameEntry(handle, nickname);
  2878. }
  2879. if ( state > 2 ) {
  2880. rv = DeleteDBSubjectEntry(handle, &cert->derSubject);
  2881. }
  2882. if ( certEntry ) {
  2883. DestroyDBEntry((certDBEntry *)certEntry);
  2884. }
  2885. if ( nicknameEntry ) {
  2886. DestroyDBEntry((certDBEntry *)nicknameEntry);
  2887. }
  2888. if ( subjectEntry ) {
  2889. DestroyDBEntry((certDBEntry *)subjectEntry);
  2890. }
  2891. return(NULL);
  2892. }
  2893. /* forward declaration */
  2894. static SECStatus
  2895. UpdateV7DB(NSSLOWCERTCertDBHandle *handle, DB *updatedb);
  2896. /*
  2897. * version 8 uses the same schema as version 7. The only differences are
  2898. * 1) version 8 db uses the blob shim to store data entries > 32k.
  2899. * 2) version 8 db sets the db block size to 32k.
  2900. * both of these are dealt with by the handle.
  2901. */
  2902. static SECStatus
  2903. UpdateV8DB(NSSLOWCERTCertDBHandle *handle, DB *updatedb)
  2904. {
  2905. return UpdateV7DB(handle,updatedb);
  2906. }
  2907. /*
  2908. * we could just blindly sequence through reading key data pairs and writing
  2909. * them back out, but some cert.db's have gotten quite large and may have some
  2910. * subtle corruption problems, so instead we cycle through the certs and
  2911. * CRL's and S/MIME profiles and rebuild our subject lists from those records.
  2912. */
  2913. static SECStatus
  2914. UpdateV7DB(NSSLOWCERTCertDBHandle *handle, DB *updatedb)
  2915. {
  2916. DBT key, data;
  2917. int ret;
  2918. NSSLOWCERTCertificate *cert;
  2919. PRBool isKRL = PR_FALSE;
  2920. certDBEntryType entryType;
  2921. SECItem dbEntry, dbKey;
  2922. certDBEntryRevocation crlEntry;
  2923. certDBEntryCert certEntry;
  2924. certDBEntrySMime smimeEntry;
  2925. SECStatus rv;
  2926. ret = (* updatedb->seq)(updatedb, &key, &data, R_FIRST);
  2927. if ( ret ) {
  2928. return(SECFailure);
  2929. }
  2930. do {
  2931. unsigned char *dataBuf = (unsigned char *)data.data;
  2932. unsigned char *keyBuf = (unsigned char *)key.data;
  2933. dbEntry.data = &dataBuf[SEC_DB_ENTRY_HEADER_LEN];
  2934. dbEntry.len = data.size - SEC_DB_ENTRY_HEADER_LEN;
  2935. entryType = (certDBEntryType) keyBuf[0];
  2936. dbKey.data = &keyBuf[SEC_DB_KEY_HEADER_LEN];
  2937. dbKey.len = key.size - SEC_DB_KEY_HEADER_LEN;
  2938. if ((dbEntry.len <= 0) || (dbKey.len <= 0)) {
  2939. continue;
  2940. }
  2941. switch (entryType) {
  2942. /* these entries will get regenerated as we read the
  2943. * rest of the data from the database */
  2944. case certDBEntryTypeVersion:
  2945. case certDBEntryTypeSubject:
  2946. case certDBEntryTypeContentVersion:
  2947. case certDBEntryTypeNickname:
  2948. /* smime profiles need entries created after the certs have
  2949. * been imported, loop over them in a second run */
  2950. case certDBEntryTypeSMimeProfile:
  2951. break;
  2952. case certDBEntryTypeCert:
  2953. /* decode Entry */
  2954. certEntry.common.version = (unsigned int)dataBuf[0];
  2955. certEntry.common.type = entryType;
  2956. certEntry.common.flags = (unsigned int)dataBuf[2];
  2957. rv = DecodeDBCertEntry(&certEntry,&dbEntry);
  2958. if (rv != SECSuccess) {
  2959. break;
  2960. }
  2961. /* should we check for existing duplicates? */
  2962. cert = nsslowcert_DecodeDERCertificate(&certEntry.derCert,
  2963. certEntry.nickname);
  2964. if (cert) {
  2965. nsslowcert_UpdatePermCert(handle, cert, certEntry.nickname,
  2966. &certEntry.trust);
  2967. nsslowcert_DestroyCertificate(cert);
  2968. }
  2969. /* free any data the decode may have allocated. */
  2970. pkcs11_freeStaticData(certEntry.derCert.data,
  2971. certEntry.derCertSpace);
  2972. pkcs11_freeNickname(certEntry.nickname, certEntry.nicknameSpace);
  2973. break;
  2974. case certDBEntryTypeKeyRevocation:
  2975. isKRL = PR_TRUE;
  2976. /* fall through */
  2977. case certDBEntryTypeRevocation:
  2978. crlEntry.common.version = (unsigned int)dataBuf[0];
  2979. crlEntry.common.type = entryType;
  2980. crlEntry.common.flags = (unsigned int)dataBuf[2];
  2981. crlEntry.common.arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  2982. if (crlEntry.common.arena == NULL) {
  2983. break;
  2984. }
  2985. rv = DecodeDBCrlEntry(&crlEntry,&dbEntry);
  2986. if (rv != SECSuccess) {
  2987. break;
  2988. }
  2989. nsslowcert_UpdateCrl(handle, &crlEntry.derCrl, &dbKey,
  2990. crlEntry.url, isKRL);
  2991. /* free data allocated by the decode */
  2992. PORT_FreeArena(crlEntry.common.arena, PR_FALSE);
  2993. crlEntry.common.arena = NULL;
  2994. break;
  2995. default:
  2996. break;
  2997. }
  2998. } while ( (* updatedb->seq)(updatedb, &key, &data, R_NEXT) == 0 );
  2999. /* now loop again updating just the SMimeProfile. */
  3000. ret = (* updatedb->seq)(updatedb, &key, &data, R_FIRST);
  3001. if ( ret ) {
  3002. return(SECFailure);
  3003. }
  3004. do {
  3005. unsigned char *dataBuf = (unsigned char *)data.data;
  3006. unsigned char *keyBuf = (unsigned char *)key.data;
  3007. dbEntry.data = &dataBuf[SEC_DB_ENTRY_HEADER_LEN];
  3008. dbEntry.len = data.size - SEC_DB_ENTRY_HEADER_LEN;
  3009. entryType = (certDBEntryType) keyBuf[0];
  3010. if (entryType != certDBEntryTypeSMimeProfile) {
  3011. continue;
  3012. }
  3013. dbKey.data = &keyBuf[SEC_DB_KEY_HEADER_LEN];
  3014. dbKey.len = key.size - SEC_DB_KEY_HEADER_LEN;
  3015. if ((dbEntry.len <= 0) || (dbKey.len <= 0)) {
  3016. continue;
  3017. }
  3018. smimeEntry.common.version = (unsigned int)dataBuf[0];
  3019. smimeEntry.common.type = entryType;
  3020. smimeEntry.common.flags = (unsigned int)dataBuf[2];
  3021. smimeEntry.common.arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  3022. /* decode entry */
  3023. rv = DecodeDBSMimeEntry(&smimeEntry,&dbEntry,(char *)dbKey.data);
  3024. if (rv == SECSuccess) {
  3025. nsslowcert_UpdateSMimeProfile(handle, smimeEntry.emailAddr,
  3026. &smimeEntry.subjectName, &smimeEntry.smimeOptions,
  3027. &smimeEntry.optionsDate);
  3028. }
  3029. PORT_FreeArena(smimeEntry.common.arena, PR_FALSE);
  3030. smimeEntry.common.arena = NULL;
  3031. } while ( (* updatedb->seq)(updatedb, &key, &data, R_NEXT) == 0 );
  3032. (* updatedb->close)(updatedb);
  3033. /* a database update is a good time to go back and verify the integrity of
  3034. * the keys and certs */
  3035. handle->dbVerify = PR_TRUE;
  3036. return(SECSuccess);
  3037. }
  3038. /*
  3039. * NOTE - Version 6 DB did not go out to the real world in a release,
  3040. * so we can remove this function in a later release.
  3041. */
  3042. static SECStatus
  3043. UpdateV6DB(NSSLOWCERTCertDBHandle *handle, DB *updatedb)
  3044. {
  3045. int ret;
  3046. DBT key, data;
  3047. unsigned char *buf, *tmpbuf = NULL;
  3048. certDBEntryType type;
  3049. certDBEntryNickname *nnEntry = NULL;
  3050. certDBEntrySubject *subjectEntry = NULL;
  3051. certDBEntrySMime *emailEntry = NULL;
  3052. char *nickname;
  3053. char *emailAddr;
  3054. SECStatus rv;
  3055. /*
  3056. * Sequence through the old database and copy all of the entries
  3057. * to the new database. Subject name entries will have the new
  3058. * fields inserted into them (with zero length).
  3059. */
  3060. ret = (* updatedb->seq)(updatedb, &key, &data, R_FIRST);
  3061. if ( ret ) {
  3062. return(SECFailure);
  3063. }
  3064. do {
  3065. buf = (unsigned char *)data.data;
  3066. if ( data.size >= 3 ) {
  3067. if ( buf[0] == 6 ) { /* version number */
  3068. type = (certDBEntryType)buf[1];
  3069. if ( type == certDBEntryTypeSubject ) {
  3070. /* expando subjecto entrieo */
  3071. tmpbuf = (unsigned char *)PORT_Alloc(data.size + 4);
  3072. if ( tmpbuf ) {
  3073. /* copy header stuff */
  3074. PORT_Memcpy(tmpbuf, buf, SEC_DB_ENTRY_HEADER_LEN + 2);
  3075. /* insert 4 more bytes of zero'd header */
  3076. PORT_Memset(&tmpbuf[SEC_DB_ENTRY_HEADER_LEN + 2],
  3077. 0, 4);
  3078. /* copy rest of the data */
  3079. PORT_Memcpy(&tmpbuf[SEC_DB_ENTRY_HEADER_LEN + 6],
  3080. &buf[SEC_DB_ENTRY_HEADER_LEN + 2],
  3081. data.size - (SEC_DB_ENTRY_HEADER_LEN + 2));
  3082. data.data = (void *)tmpbuf;
  3083. data.size += 4;
  3084. buf = tmpbuf;
  3085. }
  3086. } else if ( type == certDBEntryTypeCert ) {
  3087. /* expando certo entrieo */
  3088. tmpbuf = (unsigned char *)PORT_Alloc(data.size + 3);
  3089. if ( tmpbuf ) {
  3090. /* copy header stuff */
  3091. PORT_Memcpy(tmpbuf, buf, SEC_DB_ENTRY_HEADER_LEN);
  3092. /* copy trust flage, setting msb's to 0 */
  3093. tmpbuf[SEC_DB_ENTRY_HEADER_LEN] = 0;
  3094. tmpbuf[SEC_DB_ENTRY_HEADER_LEN+1] =
  3095. buf[SEC_DB_ENTRY_HEADER_LEN];
  3096. tmpbuf[SEC_DB_ENTRY_HEADER_LEN+2] = 0;
  3097. tmpbuf[SEC_DB_ENTRY_HEADER_LEN+3] =
  3098. buf[SEC_DB_ENTRY_HEADER_LEN+1];
  3099. tmpbuf[SEC_DB_ENTRY_HEADER_LEN+4] = 0;
  3100. tmpbuf[SEC_DB_ENTRY_HEADER_LEN+5] =
  3101. buf[SEC_DB_ENTRY_HEADER_LEN+2];
  3102. /* copy rest of the data */
  3103. PORT_Memcpy(&tmpbuf[SEC_DB_ENTRY_HEADER_LEN + 6],
  3104. &buf[SEC_DB_ENTRY_HEADER_LEN + 3],
  3105. data.size - (SEC_DB_ENTRY_HEADER_LEN + 3));
  3106. data.data = (void *)tmpbuf;
  3107. data.size += 3;
  3108. buf = tmpbuf;
  3109. }
  3110. }
  3111. /* update the record version number */
  3112. buf[0] = CERT_DB_FILE_VERSION;
  3113. /* copy to the new database */
  3114. ret = certdb_Put(handle->permCertDB, &key, &data, 0);
  3115. if ( tmpbuf ) {
  3116. PORT_Free(tmpbuf);
  3117. tmpbuf = NULL;
  3118. }
  3119. }
  3120. }
  3121. } while ( (* updatedb->seq)(updatedb, &key, &data, R_NEXT) == 0 );
  3122. ret = certdb_Sync(handle->permCertDB, 0);
  3123. ret = (* updatedb->seq)(updatedb, &key, &data, R_FIRST);
  3124. if ( ret ) {
  3125. return(SECFailure);
  3126. }
  3127. do {
  3128. buf = (unsigned char *)data.data;
  3129. if ( data.size >= 3 ) {
  3130. if ( buf[0] == CERT_DB_FILE_VERSION ) { /* version number */
  3131. type = (certDBEntryType)buf[1];
  3132. if ( type == certDBEntryTypeNickname ) {
  3133. nickname = &((char *)key.data)[1];
  3134. /* get the matching nickname entry in the new DB */
  3135. nnEntry = ReadDBNicknameEntry(handle, nickname);
  3136. if ( nnEntry == NULL ) {
  3137. goto endloop;
  3138. }
  3139. /* find the subject entry pointed to by nickname */
  3140. subjectEntry = ReadDBSubjectEntry(handle,
  3141. &nnEntry->subjectName);
  3142. if ( subjectEntry == NULL ) {
  3143. goto endloop;
  3144. }
  3145. subjectEntry->nickname =
  3146. (char *)PORT_ArenaAlloc(subjectEntry->common.arena,
  3147. key.size - 1);
  3148. if ( subjectEntry->nickname ) {
  3149. PORT_Memcpy(subjectEntry->nickname, nickname,
  3150. key.size - 1);
  3151. rv = WriteDBSubjectEntry(handle, subjectEntry);
  3152. }
  3153. } else if ( type == certDBEntryTypeSMimeProfile ) {
  3154. emailAddr = &((char *)key.data)[1];
  3155. /* get the matching smime entry in the new DB */
  3156. emailEntry = nsslowcert_ReadDBSMimeEntry(handle, emailAddr);
  3157. if ( emailEntry == NULL ) {
  3158. goto endloop;
  3159. }
  3160. /* find the subject entry pointed to by nickname */
  3161. subjectEntry = ReadDBSubjectEntry(handle,
  3162. &emailEntry->subjectName);
  3163. if ( subjectEntry == NULL ) {
  3164. goto endloop;
  3165. }
  3166. subjectEntry->emailAddrs = (char **)
  3167. PORT_ArenaAlloc(subjectEntry->common.arena,
  3168. sizeof(char *));
  3169. if ( subjectEntry->emailAddrs ) {
  3170. subjectEntry->emailAddrs[0] =
  3171. (char *)PORT_ArenaAlloc(subjectEntry->common.arena,
  3172. key.size - 1);
  3173. if ( subjectEntry->emailAddrs[0] ) {
  3174. PORT_Memcpy(subjectEntry->emailAddrs[0], emailAddr,
  3175. key.size - 1);
  3176. subjectEntry->nemailAddrs = 1;
  3177. rv = WriteDBSubjectEntry(handle, subjectEntry);
  3178. }
  3179. }
  3180. }
  3181. endloop:
  3182. if ( subjectEntry ) {
  3183. DestroyDBEntry((certDBEntry *)subjectEntry);
  3184. subjectEntry = NULL;
  3185. }
  3186. if ( nnEntry ) {
  3187. DestroyDBEntry((certDBEntry *)nnEntry);
  3188. nnEntry = NULL;
  3189. }
  3190. if ( emailEntry ) {
  3191. DestroyDBEntry((certDBEntry *)emailEntry);
  3192. emailEntry = NULL;
  3193. }
  3194. }
  3195. }
  3196. } while ( (* updatedb->seq)(updatedb, &key, &data, R_NEXT) == 0 );
  3197. ret = certdb_Sync(handle->permCertDB, 0);
  3198. (* updatedb->close)(updatedb);
  3199. return(SECSuccess);
  3200. }
  3201. static SECStatus
  3202. updateV5Callback(NSSLOWCERTCertificate *cert, SECItem *k, void *pdata)
  3203. {
  3204. NSSLOWCERTCertDBHandle *handle;
  3205. certDBEntryCert *entry;
  3206. NSSLOWCERTCertTrust *trust;
  3207. handle = (NSSLOWCERTCertDBHandle *)pdata;
  3208. trust = &cert->dbEntry->trust;
  3209. /* SSL user certs can be used for email if they have an email addr */
  3210. if ( cert->emailAddr && ( trust->sslFlags & CERTDB_USER ) &&
  3211. ( trust->emailFlags == 0 ) ) {
  3212. trust->emailFlags = CERTDB_USER;
  3213. }
  3214. /* servers didn't set the user flags on the server cert.. */
  3215. if (PORT_Strcmp(cert->dbEntry->nickname,"Server-Cert") == 0) {
  3216. trust->sslFlags |= CERTDB_USER;
  3217. }
  3218. entry = AddCertToPermDB(handle, cert, cert->dbEntry->nickname,
  3219. &cert->dbEntry->trust);
  3220. if ( entry ) {
  3221. DestroyDBEntry((certDBEntry *)entry);
  3222. }
  3223. return(SECSuccess);
  3224. }
  3225. static SECStatus
  3226. UpdateV5DB(NSSLOWCERTCertDBHandle *handle, DB *updatedb)
  3227. {
  3228. NSSLOWCERTCertDBHandle updatehandle;
  3229. SECStatus rv;
  3230. updatehandle.permCertDB = updatedb;
  3231. updatehandle.dbMon = PZ_NewMonitor(nssILockCertDB);
  3232. updatehandle.dbVerify = 0;
  3233. updatehandle.ref = 1; /* prevent premature close */
  3234. rv = nsslowcert_TraversePermCerts(&updatehandle, updateV5Callback,
  3235. (void *)handle);
  3236. PZ_DestroyMonitor(updatehandle.dbMon);
  3237. (* updatedb->close)(updatedb);
  3238. return(SECSuccess);
  3239. }
  3240. static PRBool
  3241. isV4DB(DB *db) {
  3242. DBT key,data;
  3243. int ret;
  3244. key.data = "Version";
  3245. key.size = 7;
  3246. ret = (*db->get)(db, &key, &data, 0);
  3247. if (ret) {
  3248. return PR_FALSE;
  3249. }
  3250. if ((data.size == 1) && (*(unsigned char *)data.data <= 4)) {
  3251. return PR_TRUE;
  3252. }
  3253. return PR_FALSE;
  3254. }
  3255. static SECStatus
  3256. UpdateV4DB(NSSLOWCERTCertDBHandle *handle, DB *updatedb)
  3257. {
  3258. DBT key, data;
  3259. certDBEntryCert *entry, *entry2;
  3260. int ret;
  3261. PRArenaPool *arena = NULL;
  3262. NSSLOWCERTCertificate *cert;
  3263. ret = (* updatedb->seq)(updatedb, &key, &data, R_FIRST);
  3264. if ( ret ) {
  3265. return(SECFailure);
  3266. }
  3267. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  3268. if (arena == NULL) {
  3269. return(SECFailure);
  3270. }
  3271. do {
  3272. if ( data.size != 1 ) { /* skip version number */
  3273. /* decode the old DB entry */
  3274. entry = (certDBEntryCert *)
  3275. DecodeV4DBCertEntry((unsigned char*)data.data, data.size);
  3276. if ( entry ) {
  3277. cert = nsslowcert_DecodeDERCertificate(&entry->derCert,
  3278. entry->nickname);
  3279. if ( cert != NULL ) {
  3280. /* add to new database */
  3281. entry2 = AddCertToPermDB(handle, cert, entry->nickname,
  3282. &entry->trust);
  3283. nsslowcert_DestroyCertificate(cert);
  3284. if ( entry2 ) {
  3285. DestroyDBEntry((certDBEntry *)entry2);
  3286. }
  3287. }
  3288. DestroyDBEntry((certDBEntry *)entry);
  3289. }
  3290. }
  3291. } while ( (* updatedb->seq)(updatedb, &key, &data, R_NEXT) == 0 );
  3292. PORT_FreeArena(arena, PR_FALSE);
  3293. (* updatedb->close)(updatedb);
  3294. return(SECSuccess);
  3295. }
  3296. /*
  3297. * return true if a database key conflict exists
  3298. */
  3299. PRBool
  3300. nsslowcert_CertDBKeyConflict(SECItem *derCert, NSSLOWCERTCertDBHandle *handle)
  3301. {
  3302. SECStatus rv;
  3303. DBT tmpdata;
  3304. DBT namekey;
  3305. int ret;
  3306. SECItem keyitem;
  3307. PRArenaPool *arena = NULL;
  3308. SECItem derKey;
  3309. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  3310. if ( arena == NULL ) {
  3311. goto loser;
  3312. }
  3313. /* get the db key of the cert */
  3314. rv = nsslowcert_KeyFromDERCert(arena, derCert, &derKey);
  3315. if ( rv != SECSuccess ) {
  3316. goto loser;
  3317. }
  3318. rv = EncodeDBCertKey(&derKey, arena, &keyitem);
  3319. if ( rv != SECSuccess ) {
  3320. goto loser;
  3321. }
  3322. namekey.data = keyitem.data;
  3323. namekey.size = keyitem.len;
  3324. ret = certdb_Get(handle->permCertDB, &namekey, &tmpdata, 0);
  3325. if ( ret == 0 ) {
  3326. goto loser;
  3327. }
  3328. PORT_FreeArena(arena, PR_FALSE);
  3329. return(PR_FALSE);
  3330. loser:
  3331. if ( arena ) {
  3332. PORT_FreeArena(arena, PR_FALSE);
  3333. }
  3334. return(PR_TRUE);
  3335. }
  3336. /*
  3337. * return true if a nickname conflict exists
  3338. * NOTE: caller must have already made sure that this exact cert
  3339. * doesn't exist in the DB
  3340. */
  3341. static PRBool
  3342. nsslowcert_CertNicknameConflict(char *nickname, SECItem *derSubject,
  3343. NSSLOWCERTCertDBHandle *handle)
  3344. {
  3345. PRBool rv;
  3346. certDBEntryNickname *entry;
  3347. if ( nickname == NULL ) {
  3348. return(PR_FALSE);
  3349. }
  3350. entry = ReadDBNicknameEntry(handle, nickname);
  3351. if ( entry == NULL ) {
  3352. /* no entry for this nickname, so no conflict */
  3353. return(PR_FALSE);
  3354. }
  3355. rv = PR_TRUE;
  3356. if ( SECITEM_CompareItem(derSubject, &entry->subjectName) == SECEqual ) {
  3357. /* if subject names are the same, then no conflict */
  3358. rv = PR_FALSE;
  3359. }
  3360. DestroyDBEntry((certDBEntry *)entry);
  3361. return(rv);
  3362. }
  3363. #ifdef DBM_USING_NSPR
  3364. #define NO_RDONLY PR_RDONLY
  3365. #define NO_RDWR PR_RDWR
  3366. #define NO_CREATE (PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE)
  3367. #else
  3368. #define NO_RDONLY O_RDONLY
  3369. #define NO_RDWR O_RDWR
  3370. #define NO_CREATE (O_RDWR | O_CREAT | O_TRUNC)
  3371. #endif
  3372. /*
  3373. * open an old database that needs to be updated
  3374. */
  3375. static DB *
  3376. nsslowcert_openolddb(NSSLOWCERTDBNameFunc namecb, void *cbarg, int version)
  3377. {
  3378. char * tmpname;
  3379. DB *updatedb = NULL;
  3380. tmpname = (* namecb)(cbarg, version); /* get v6 db name */
  3381. if ( tmpname ) {
  3382. updatedb = dbopen( tmpname, NO_RDONLY, 0600, DB_HASH, 0 );
  3383. PORT_Free(tmpname);
  3384. }
  3385. return updatedb;
  3386. }
  3387. static SECStatus
  3388. openNewCertDB(const char *appName, const char *prefix, const char *certdbname,
  3389. NSSLOWCERTCertDBHandle *handle, NSSLOWCERTDBNameFunc namecb, void *cbarg)
  3390. {
  3391. SECStatus rv;
  3392. certDBEntryVersion *versionEntry = NULL;
  3393. DB *updatedb = NULL;
  3394. int status = RDB_FAIL;
  3395. if (appName) {
  3396. handle->permCertDB=rdbopen( appName, prefix, "cert", NO_CREATE, &status);
  3397. } else {
  3398. handle->permCertDB=dbsopen(certdbname, NO_CREATE, 0600, DB_HASH, 0);
  3399. }
  3400. /* if create fails then we lose */
  3401. if ( handle->permCertDB == 0 ) {
  3402. return status == RDB_RETRY ? SECWouldBlock : SECFailure;
  3403. }
  3404. /* Verify version number; */
  3405. versionEntry = NewDBVersionEntry(0);
  3406. if ( versionEntry == NULL ) {
  3407. rv = SECFailure;
  3408. goto loser;
  3409. }
  3410. rv = WriteDBVersionEntry(handle, versionEntry);
  3411. DestroyDBEntry((certDBEntry *)versionEntry);
  3412. if ( rv != SECSuccess ) {
  3413. goto loser;
  3414. }
  3415. /* rv must already be Success here because of previous if statement */
  3416. /* try to upgrade old db here */
  3417. if (appName &&
  3418. (updatedb = dbsopen(certdbname, NO_RDONLY, 0600, DB_HASH, 0)) != NULL) {
  3419. rv = UpdateV8DB(handle, updatedb);
  3420. } else if ((updatedb = nsslowcert_openolddb(namecb,cbarg,7)) != NULL) {
  3421. rv = UpdateV7DB(handle, updatedb);
  3422. } else if ((updatedb = nsslowcert_openolddb(namecb,cbarg,6)) != NULL) {
  3423. rv = UpdateV6DB(handle, updatedb);
  3424. } else if ((updatedb = nsslowcert_openolddb(namecb,cbarg,5)) != NULL) {
  3425. rv = UpdateV5DB(handle, updatedb);
  3426. } else if ((updatedb = nsslowcert_openolddb(namecb,cbarg,4)) != NULL) {
  3427. /* NES has v5 format db's with v4 db names! */
  3428. if (isV4DB(updatedb)) {
  3429. rv = UpdateV4DB(handle,updatedb);
  3430. } else {
  3431. rv = UpdateV5DB(handle,updatedb);
  3432. }
  3433. }
  3434. loser:
  3435. db_InitComplete(handle->permCertDB);
  3436. return rv;
  3437. }
  3438. static int
  3439. nsslowcert_GetVersionNumber( NSSLOWCERTCertDBHandle *handle)
  3440. {
  3441. certDBEntryVersion *versionEntry = NULL;
  3442. int version = 0;
  3443. versionEntry = ReadDBVersionEntry(handle);
  3444. if ( versionEntry == NULL ) {
  3445. return 0;
  3446. }
  3447. version = versionEntry->common.version;
  3448. DestroyDBEntry((certDBEntry *)versionEntry);
  3449. return version;
  3450. }
  3451. /*
  3452. * Open the certificate database and index databases. Create them if
  3453. * they are not there or bad.
  3454. */
  3455. static SECStatus
  3456. nsslowcert_OpenPermCertDB(NSSLOWCERTCertDBHandle *handle, PRBool readOnly,
  3457. const char *appName, const char *prefix,
  3458. NSSLOWCERTDBNameFunc namecb, void *cbarg)
  3459. {
  3460. SECStatus rv;
  3461. int openflags;
  3462. char *certdbname;
  3463. int version = 0;
  3464. certdbname = (* namecb)(cbarg, CERT_DB_FILE_VERSION);
  3465. if ( certdbname == NULL ) {
  3466. return(SECFailure);
  3467. }
  3468. openflags = readOnly ? NO_RDONLY : NO_RDWR;
  3469. /*
  3470. * first open the permanent file based database.
  3471. */
  3472. if (appName) {
  3473. handle->permCertDB = rdbopen( appName, prefix, "cert", openflags, NULL);
  3474. } else {
  3475. handle->permCertDB = dbsopen( certdbname, openflags, 0600, DB_HASH, 0 );
  3476. }
  3477. /* check for correct version number */
  3478. if ( handle->permCertDB ) {
  3479. version = nsslowcert_GetVersionNumber(handle);
  3480. if ((version != CERT_DB_FILE_VERSION) &&
  3481. !(appName && version == CERT_DB_V7_FILE_VERSION)) {
  3482. goto loser;
  3483. }
  3484. } else if ( readOnly ) {
  3485. /* don't create if readonly */
  3486. /* Try openning a version 7 database */
  3487. handle->permCertDB = nsslowcert_openolddb(namecb,cbarg, 7);
  3488. if (!handle->permCertDB) {
  3489. goto loser;
  3490. }
  3491. if (nsslowcert_GetVersionNumber(handle) != 7) {
  3492. goto loser;
  3493. }
  3494. } else {
  3495. /* if first open fails, try to create a new DB */
  3496. rv = openNewCertDB(appName,prefix,certdbname,handle,namecb,cbarg);
  3497. if (rv == SECWouldBlock) {
  3498. /* only the rdb version can fail with wouldblock */
  3499. handle->permCertDB =
  3500. rdbopen( appName, prefix, "cert", openflags, NULL);
  3501. /* check for correct version number */
  3502. if ( !handle->permCertDB ) {
  3503. goto loser;
  3504. }
  3505. version = nsslowcert_GetVersionNumber(handle);
  3506. if ((version != CERT_DB_FILE_VERSION) &&
  3507. !(appName && version == CERT_DB_V7_FILE_VERSION)) {
  3508. goto loser;
  3509. }
  3510. } else if (rv != SECSuccess) {
  3511. goto loser;
  3512. }
  3513. }
  3514. PORT_Free(certdbname);
  3515. return (SECSuccess);
  3516. loser:
  3517. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  3518. if ( handle->permCertDB ) {
  3519. certdb_Close(handle->permCertDB);
  3520. handle->permCertDB = 0;
  3521. }
  3522. PORT_Free(certdbname);
  3523. return(SECFailure);
  3524. }
  3525. /*
  3526. * delete all DB records associated with a particular certificate
  3527. */
  3528. static SECStatus
  3529. DeletePermCert(NSSLOWCERTCertificate *cert)
  3530. {
  3531. SECStatus rv;
  3532. SECStatus ret;
  3533. ret = SECSuccess;
  3534. rv = DeleteDBCertEntry(cert->dbhandle, &cert->certKey);
  3535. if ( rv != SECSuccess ) {
  3536. ret = SECFailure;
  3537. }
  3538. rv = RemovePermSubjectNode(cert);
  3539. return(ret);
  3540. }
  3541. /*
  3542. * Delete a certificate from the permanent database.
  3543. */
  3544. SECStatus
  3545. nsslowcert_DeletePermCertificate(NSSLOWCERTCertificate *cert)
  3546. {
  3547. SECStatus rv;
  3548. nsslowcert_LockDB(cert->dbhandle);
  3549. /* delete the records from the permanent database */
  3550. rv = DeletePermCert(cert);
  3551. /* get rid of dbcert and stuff pointing to it */
  3552. DestroyDBEntry((certDBEntry *)cert->dbEntry);
  3553. cert->dbEntry = NULL;
  3554. cert->trust = NULL;
  3555. nsslowcert_UnlockDB(cert->dbhandle);
  3556. return(rv);
  3557. }
  3558. /*
  3559. * Traverse all of the entries in the database of a particular type
  3560. * call the given function for each one.
  3561. */
  3562. SECStatus
  3563. nsslowcert_TraverseDBEntries(NSSLOWCERTCertDBHandle *handle,
  3564. certDBEntryType type,
  3565. SECStatus (* callback)(SECItem *data, SECItem *key,
  3566. certDBEntryType type, void *pdata),
  3567. void *udata )
  3568. {
  3569. DBT data;
  3570. DBT key;
  3571. SECStatus rv = SECSuccess;
  3572. int ret;
  3573. SECItem dataitem;
  3574. SECItem keyitem;
  3575. unsigned char *buf;
  3576. unsigned char *keybuf;
  3577. ret = certdb_Seq(handle->permCertDB, &key, &data, R_FIRST);
  3578. if ( ret ) {
  3579. return(SECFailure);
  3580. }
  3581. /* here, ret is zero and rv is SECSuccess.
  3582. * Below here, ret is a count of successful calls to the callback function.
  3583. */
  3584. do {
  3585. buf = (unsigned char *)data.data;
  3586. if ( buf[1] == (unsigned char)type ) {
  3587. dataitem.len = data.size;
  3588. dataitem.data = buf;
  3589. dataitem.type = siBuffer;
  3590. keyitem.len = key.size - SEC_DB_KEY_HEADER_LEN;
  3591. keybuf = (unsigned char *)key.data;
  3592. keyitem.data = &keybuf[SEC_DB_KEY_HEADER_LEN];
  3593. keyitem.type = siBuffer;
  3594. /* type should equal keybuf[0]. */
  3595. rv = (* callback)(&dataitem, &keyitem, type, udata);
  3596. if ( rv == SECSuccess ) {
  3597. ++ret;
  3598. }
  3599. }
  3600. } while ( certdb_Seq(handle->permCertDB, &key, &data, R_NEXT) == 0 );
  3601. /* If any callbacks succeeded, or no calls to callbacks were made,
  3602. * then report success. Otherwise, report failure.
  3603. */
  3604. return (ret ? SECSuccess : rv);
  3605. }
  3606. /*
  3607. * Decode a certificate and enter it into the temporary certificate database.
  3608. * Deal with nicknames correctly
  3609. *
  3610. * This is the private entry point.
  3611. */
  3612. static NSSLOWCERTCertificate *
  3613. DecodeACert(NSSLOWCERTCertDBHandle *handle, certDBEntryCert *entry)
  3614. {
  3615. NSSLOWCERTCertificate *cert = NULL;
  3616. cert = nsslowcert_DecodeDERCertificate(&entry->derCert, entry->nickname );
  3617. if ( cert == NULL ) {
  3618. goto loser;
  3619. }
  3620. cert->dbhandle = handle;
  3621. cert->dbEntry = entry;
  3622. cert->trust = &entry->trust;
  3623. return(cert);
  3624. loser:
  3625. return(0);
  3626. }
  3627. static NSSLOWCERTTrust *
  3628. CreateTrust(void)
  3629. {
  3630. NSSLOWCERTTrust *trust = NULL;
  3631. nsslowcert_LockFreeList();
  3632. trust = trustListHead;
  3633. if (trust) {
  3634. trustListCount--;
  3635. trustListHead = trust->next;
  3636. }
  3637. PORT_Assert(trustListCount >= 0);
  3638. nsslowcert_UnlockFreeList();
  3639. if (trust) {
  3640. return trust;
  3641. }
  3642. return PORT_ZNew(NSSLOWCERTTrust);
  3643. }
  3644. static void
  3645. DestroyTrustFreeList(void)
  3646. {
  3647. NSSLOWCERTTrust *trust;
  3648. nsslowcert_LockFreeList();
  3649. while (NULL != (trust = trustListHead)) {
  3650. trustListCount--;
  3651. trustListHead = trust->next;
  3652. PORT_Free(trust);
  3653. }
  3654. PORT_Assert(!trustListCount);
  3655. trustListCount = 0;
  3656. nsslowcert_UnlockFreeList();
  3657. }
  3658. static NSSLOWCERTTrust *
  3659. DecodeTrustEntry(NSSLOWCERTCertDBHandle *handle, certDBEntryCert *entry,
  3660. const SECItem *dbKey)
  3661. {
  3662. NSSLOWCERTTrust *trust = CreateTrust();
  3663. if (trust == NULL) {
  3664. return trust;
  3665. }
  3666. trust->dbhandle = handle;
  3667. trust->dbEntry = entry;
  3668. trust->dbKey.data = pkcs11_copyStaticData(dbKey->data,dbKey->len,
  3669. trust->dbKeySpace, sizeof(trust->dbKeySpace));
  3670. if (!trust->dbKey.data) {
  3671. PORT_Free(trust);
  3672. return NULL;
  3673. }
  3674. trust->dbKey.len = dbKey->len;
  3675. trust->trust = &entry->trust;
  3676. trust->derCert = &entry->derCert;
  3677. return(trust);
  3678. }
  3679. typedef struct {
  3680. PermCertCallback certfunc;
  3681. NSSLOWCERTCertDBHandle *handle;
  3682. void *data;
  3683. } PermCertCallbackState;
  3684. /*
  3685. * traversal callback to decode certs and call callers callback
  3686. */
  3687. static SECStatus
  3688. certcallback(SECItem *dbdata, SECItem *dbkey, certDBEntryType type, void *data)
  3689. {
  3690. PermCertCallbackState *mystate;
  3691. SECStatus rv;
  3692. certDBEntryCert *entry;
  3693. SECItem entryitem;
  3694. NSSLOWCERTCertificate *cert;
  3695. PRArenaPool *arena = NULL;
  3696. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  3697. if ( arena == NULL ) {
  3698. goto loser;
  3699. }
  3700. entry = (certDBEntryCert *)PORT_ArenaAlloc(arena, sizeof(certDBEntryCert));
  3701. mystate = (PermCertCallbackState *)data;
  3702. entry->common.version = (unsigned int)dbdata->data[0];
  3703. entry->common.type = (certDBEntryType)dbdata->data[1];
  3704. entry->common.flags = (unsigned int)dbdata->data[2];
  3705. entry->common.arena = arena;
  3706. entryitem.len = dbdata->len - SEC_DB_ENTRY_HEADER_LEN;
  3707. entryitem.data = &dbdata->data[SEC_DB_ENTRY_HEADER_LEN];
  3708. rv = DecodeDBCertEntry(entry, &entryitem);
  3709. if (rv != SECSuccess ) {
  3710. goto loser;
  3711. }
  3712. entry->derCert.type = siBuffer;
  3713. /* note: Entry is 'inheritted'. */
  3714. cert = DecodeACert(mystate->handle, entry);
  3715. rv = (* mystate->certfunc)(cert, dbkey, mystate->data);
  3716. /* arena stored in entry destroyed by nsslowcert_DestroyCertificate */
  3717. nsslowcert_DestroyCertificateNoLocking(cert);
  3718. return(rv);
  3719. loser:
  3720. if ( arena ) {
  3721. PORT_FreeArena(arena, PR_FALSE);
  3722. }
  3723. return(SECFailure);
  3724. }
  3725. /*
  3726. * Traverse all of the certificates in the permanent database and
  3727. * call the given function for each one; expect the caller to have lock.
  3728. */
  3729. static SECStatus
  3730. TraversePermCertsNoLocking(NSSLOWCERTCertDBHandle *handle,
  3731. SECStatus (* certfunc)(NSSLOWCERTCertificate *cert,
  3732. SECItem *k,
  3733. void *pdata),
  3734. void *udata )
  3735. {
  3736. SECStatus rv;
  3737. PermCertCallbackState mystate;
  3738. mystate.certfunc = certfunc;
  3739. mystate.handle = handle;
  3740. mystate.data = udata;
  3741. rv = nsslowcert_TraverseDBEntries(handle, certDBEntryTypeCert, certcallback,
  3742. (void *)&mystate);
  3743. return(rv);
  3744. }
  3745. /*
  3746. * Traverse all of the certificates in the permanent database and
  3747. * call the given function for each one.
  3748. */
  3749. SECStatus
  3750. nsslowcert_TraversePermCerts(NSSLOWCERTCertDBHandle *handle,
  3751. SECStatus (* certfunc)(NSSLOWCERTCertificate *cert, SECItem *k,
  3752. void *pdata),
  3753. void *udata )
  3754. {
  3755. SECStatus rv;
  3756. nsslowcert_LockDB(handle);
  3757. rv = TraversePermCertsNoLocking(handle, certfunc, udata);
  3758. nsslowcert_UnlockDB(handle);
  3759. return(rv);
  3760. }
  3761. /*
  3762. * Close the database
  3763. */
  3764. void
  3765. nsslowcert_ClosePermCertDB(NSSLOWCERTCertDBHandle *handle)
  3766. {
  3767. if ( handle ) {
  3768. if ( handle->permCertDB ) {
  3769. certdb_Close( handle->permCertDB );
  3770. handle->permCertDB = NULL;
  3771. }
  3772. if (handle->dbMon) {
  3773. PZ_DestroyMonitor(handle->dbMon);
  3774. handle->dbMon = NULL;
  3775. }
  3776. PORT_Free(handle);
  3777. }
  3778. return;
  3779. }
  3780. /*
  3781. * Get the trust attributes from a certificate
  3782. */
  3783. SECStatus
  3784. nsslowcert_GetCertTrust(NSSLOWCERTCertificate *cert, NSSLOWCERTCertTrust *trust)
  3785. {
  3786. SECStatus rv;
  3787. nsslowcert_LockCertTrust(cert);
  3788. if ( cert->trust == NULL ) {
  3789. rv = SECFailure;
  3790. } else {
  3791. *trust = *cert->trust;
  3792. rv = SECSuccess;
  3793. }
  3794. nsslowcert_UnlockCertTrust(cert);
  3795. return(rv);
  3796. }
  3797. /*
  3798. * Change the trust attributes of a certificate and make them permanent
  3799. * in the database.
  3800. */
  3801. SECStatus
  3802. nsslowcert_ChangeCertTrust(NSSLOWCERTCertDBHandle *handle,
  3803. NSSLOWCERTCertificate *cert, NSSLOWCERTCertTrust *trust)
  3804. {
  3805. certDBEntryCert *entry;
  3806. int rv;
  3807. SECStatus ret;
  3808. nsslowcert_LockDB(handle);
  3809. nsslowcert_LockCertTrust(cert);
  3810. /* only set the trust on permanent certs */
  3811. if ( cert->trust == NULL ) {
  3812. ret = SECFailure;
  3813. goto done;
  3814. }
  3815. *cert->trust = *trust;
  3816. if ( cert->dbEntry == NULL ) {
  3817. ret = SECSuccess; /* not in permanent database */
  3818. goto done;
  3819. }
  3820. entry = cert->dbEntry;
  3821. entry->trust = *trust;
  3822. rv = WriteDBCertEntry(handle, entry);
  3823. if ( rv ) {
  3824. ret = SECFailure;
  3825. goto done;
  3826. }
  3827. ret = SECSuccess;
  3828. done:
  3829. nsslowcert_UnlockCertTrust(cert);
  3830. nsslowcert_UnlockDB(handle);
  3831. return(ret);
  3832. }
  3833. static SECStatus
  3834. nsslowcert_UpdatePermCert(NSSLOWCERTCertDBHandle *dbhandle,
  3835. NSSLOWCERTCertificate *cert, char *nickname, NSSLOWCERTCertTrust *trust)
  3836. {
  3837. char *oldnn;
  3838. certDBEntryCert *entry;
  3839. PRBool conflict;
  3840. SECStatus ret;
  3841. PORT_Assert(!cert->dbEntry);
  3842. /* don't add a conflicting nickname */
  3843. conflict = nsslowcert_CertNicknameConflict(nickname, &cert->derSubject,
  3844. dbhandle);
  3845. if ( conflict ) {
  3846. ret = SECFailure;
  3847. goto done;
  3848. }
  3849. /* save old nickname so that we can delete it */
  3850. oldnn = cert->nickname;
  3851. entry = AddCertToPermDB(dbhandle, cert, nickname, trust);
  3852. if ( entry == NULL ) {
  3853. ret = SECFailure;
  3854. goto done;
  3855. }
  3856. pkcs11_freeNickname(oldnn,cert->nicknameSpace);
  3857. cert->nickname = (entry->nickname) ? pkcs11_copyNickname(entry->nickname,
  3858. cert->nicknameSpace, sizeof(cert->nicknameSpace)) : NULL;
  3859. cert->trust = &entry->trust;
  3860. cert->dbEntry = entry;
  3861. ret = SECSuccess;
  3862. done:
  3863. return(ret);
  3864. }
  3865. SECStatus
  3866. nsslowcert_AddPermCert(NSSLOWCERTCertDBHandle *dbhandle,
  3867. NSSLOWCERTCertificate *cert, char *nickname, NSSLOWCERTCertTrust *trust)
  3868. {
  3869. SECStatus ret;
  3870. nsslowcert_LockDB(dbhandle);
  3871. ret = nsslowcert_UpdatePermCert(dbhandle, cert, nickname, trust);
  3872. nsslowcert_UnlockDB(dbhandle);
  3873. return(ret);
  3874. }
  3875. /*
  3876. * Open the certificate database and index databases. Create them if
  3877. * they are not there or bad.
  3878. */
  3879. SECStatus
  3880. nsslowcert_OpenCertDB(NSSLOWCERTCertDBHandle *handle, PRBool readOnly,
  3881. const char *appName, const char *prefix,
  3882. NSSLOWCERTDBNameFunc namecb, void *cbarg, PRBool openVolatile)
  3883. {
  3884. int rv;
  3885. certdb_InitDBLock(handle);
  3886. handle->dbMon = PZ_NewMonitor(nssILockCertDB);
  3887. PORT_Assert(handle->dbMon != NULL);
  3888. handle->dbVerify = PR_FALSE;
  3889. rv = nsslowcert_OpenPermCertDB(handle, readOnly, appName, prefix,
  3890. namecb, cbarg);
  3891. if ( rv ) {
  3892. goto loser;
  3893. }
  3894. return (SECSuccess);
  3895. loser:
  3896. PORT_SetError(SEC_ERROR_BAD_DATABASE);
  3897. return(SECFailure);
  3898. }
  3899. PRBool
  3900. nsslowcert_needDBVerify(NSSLOWCERTCertDBHandle *handle)
  3901. {
  3902. if (!handle) return PR_FALSE;
  3903. return handle->dbVerify;
  3904. }
  3905. void
  3906. nsslowcert_setDBVerify(NSSLOWCERTCertDBHandle *handle, PRBool value)
  3907. {
  3908. handle->dbVerify = value;
  3909. }
  3910. /*
  3911. * Lookup a certificate in the databases.
  3912. */
  3913. static NSSLOWCERTCertificate *
  3914. FindCertByKey(NSSLOWCERTCertDBHandle *handle, const SECItem *certKey, PRBool lockdb)
  3915. {
  3916. NSSLOWCERTCertificate *cert = NULL;
  3917. certDBEntryCert *entry;
  3918. PRBool locked = PR_FALSE;
  3919. if ( lockdb ) {
  3920. locked = PR_TRUE;
  3921. nsslowcert_LockDB(handle);
  3922. }
  3923. /* find in perm database */
  3924. entry = ReadDBCertEntry(handle, certKey);
  3925. if ( entry == NULL ) {
  3926. goto loser;
  3927. }
  3928. /* inherit entry */
  3929. cert = DecodeACert(handle, entry);
  3930. loser:
  3931. if (cert == NULL) {
  3932. if (entry) {
  3933. DestroyDBEntry((certDBEntry *)entry);
  3934. }
  3935. }
  3936. if ( locked ) {
  3937. nsslowcert_UnlockDB(handle);
  3938. }
  3939. return(cert);
  3940. }
  3941. /*
  3942. * Lookup a certificate in the databases.
  3943. */
  3944. static NSSLOWCERTTrust *
  3945. FindTrustByKey(NSSLOWCERTCertDBHandle *handle, const SECItem *certKey, PRBool lockdb)
  3946. {
  3947. NSSLOWCERTTrust *trust = NULL;
  3948. certDBEntryCert *entry;
  3949. PRBool locked = PR_FALSE;
  3950. if ( lockdb ) {
  3951. locked = PR_TRUE;
  3952. nsslowcert_LockDB(handle);
  3953. }
  3954. /* find in perm database */
  3955. entry = ReadDBCertEntry(handle, certKey);
  3956. if ( entry == NULL ) {
  3957. goto loser;
  3958. }
  3959. if (!nsslowcert_hasTrust(&entry->trust)) {
  3960. goto loser;
  3961. }
  3962. /* inherit entry */
  3963. trust = DecodeTrustEntry(handle, entry, certKey);
  3964. loser:
  3965. if (trust == NULL) {
  3966. if (entry) {
  3967. DestroyDBEntry((certDBEntry *)entry);
  3968. }
  3969. }
  3970. if ( locked ) {
  3971. nsslowcert_UnlockDB(handle);
  3972. }
  3973. return(trust);
  3974. }
  3975. /*
  3976. * Lookup a certificate in the databases without locking
  3977. */
  3978. NSSLOWCERTCertificate *
  3979. nsslowcert_FindCertByKey(NSSLOWCERTCertDBHandle *handle, const SECItem *certKey)
  3980. {
  3981. return(FindCertByKey(handle, certKey, PR_FALSE));
  3982. }
  3983. /*
  3984. * Lookup a trust object in the databases without locking
  3985. */
  3986. NSSLOWCERTTrust *
  3987. nsslowcert_FindTrustByKey(NSSLOWCERTCertDBHandle *handle, const SECItem *certKey)
  3988. {
  3989. return(FindTrustByKey(handle, certKey, PR_FALSE));
  3990. }
  3991. /*
  3992. * Generate a key from an issuerAndSerialNumber, and find the
  3993. * associated cert in the database.
  3994. */
  3995. NSSLOWCERTCertificate *
  3996. nsslowcert_FindCertByIssuerAndSN(NSSLOWCERTCertDBHandle *handle, NSSLOWCERTIssuerAndSN *issuerAndSN)
  3997. {
  3998. SECItem certKey;
  3999. SECItem *sn = &issuerAndSN->serialNumber;
  4000. SECItem *issuer = &issuerAndSN->derIssuer;
  4001. NSSLOWCERTCertificate *cert;
  4002. int data_left = sn->len-1;
  4003. int data_len = sn->len;
  4004. int index = 0;
  4005. /* automatically detect DER encoded serial numbers and remove the der
  4006. * encoding since the database expects unencoded data.
  4007. * if it's DER encoded, there must be at least 3 bytes, tag, len, data */
  4008. if ((sn->len >= 3) && (sn->data[0] == 0x2)) {
  4009. /* remove the der encoding of the serial number before generating the
  4010. * key.. */
  4011. data_left = sn->len-2;
  4012. data_len = sn->data[1];
  4013. index = 2;
  4014. /* extended length ? (not very likely for a serial number) */
  4015. if (data_len & 0x80) {
  4016. int len_count = data_len & 0x7f;
  4017. data_len = 0;
  4018. data_left -= len_count;
  4019. if (data_left > 0) {
  4020. while (len_count --) {
  4021. data_len = (data_len << 8) | sn->data[index++];
  4022. }
  4023. }
  4024. }
  4025. /* XXX leaving any leading zeros on the serial number for backwards
  4026. * compatibility
  4027. */
  4028. /* not a valid der, must be just an unlucky serial number value */
  4029. if (data_len != data_left) {
  4030. data_len = sn->len;
  4031. index = 0;
  4032. }
  4033. }
  4034. certKey.type = 0;
  4035. certKey.data = (unsigned char*)PORT_Alloc(sn->len + issuer->len);
  4036. certKey.len = data_len + issuer->len;
  4037. if ( certKey.data == NULL ) {
  4038. return(0);
  4039. }
  4040. /* first try the serial number as hand-decoded above*/
  4041. /* copy the serialNumber */
  4042. PORT_Memcpy(certKey.data, &sn->data[index], data_len);
  4043. /* copy the issuer */
  4044. PORT_Memcpy( &certKey.data[data_len],issuer->data,issuer->len);
  4045. cert = nsslowcert_FindCertByKey(handle, &certKey);
  4046. if (cert) {
  4047. PORT_Free(certKey.data);
  4048. return (cert);
  4049. }
  4050. /* didn't find it, try by der encoded serial number */
  4051. /* copy the serialNumber */
  4052. PORT_Memcpy(certKey.data, sn->data, sn->len);
  4053. /* copy the issuer */
  4054. PORT_Memcpy( &certKey.data[sn->len], issuer->data, issuer->len);
  4055. certKey.len = sn->len + issuer->len;
  4056. cert = nsslowcert_FindCertByKey(handle, &certKey);
  4057. PORT_Free(certKey.data);
  4058. return(cert);
  4059. }
  4060. /*
  4061. * Generate a key from an issuerAndSerialNumber, and find the
  4062. * associated cert in the database.
  4063. */
  4064. NSSLOWCERTTrust *
  4065. nsslowcert_FindTrustByIssuerAndSN(NSSLOWCERTCertDBHandle *handle,
  4066. NSSLOWCERTIssuerAndSN *issuerAndSN)
  4067. {
  4068. SECItem certKey;
  4069. SECItem *sn = &issuerAndSN->serialNumber;
  4070. SECItem *issuer = &issuerAndSN->derIssuer;
  4071. NSSLOWCERTTrust *trust;
  4072. unsigned char keyBuf[512];
  4073. int data_left = sn->len-1;
  4074. int data_len = sn->len;
  4075. int index = 0;
  4076. int len;
  4077. /* automatically detect DER encoded serial numbers and remove the der
  4078. * encoding since the database expects unencoded data.
  4079. * if it's DER encoded, there must be at least 3 bytes, tag, len, data */
  4080. if ((sn->len >= 3) && (sn->data[0] == 0x2)) {
  4081. /* remove the der encoding of the serial number before generating the
  4082. * key.. */
  4083. data_left = sn->len-2;
  4084. data_len = sn->data[1];
  4085. index = 2;
  4086. /* extended length ? (not very likely for a serial number) */
  4087. if (data_len & 0x80) {
  4088. int len_count = data_len & 0x7f;
  4089. data_len = 0;
  4090. data_left -= len_count;
  4091. if (data_left > 0) {
  4092. while (len_count --) {
  4093. data_len = (data_len << 8) | sn->data[index++];
  4094. }
  4095. }
  4096. }
  4097. /* XXX leaving any leading zeros on the serial number for backwards
  4098. * compatibility
  4099. */
  4100. /* not a valid der, must be just an unlucky serial number value */
  4101. if (data_len != data_left) {
  4102. data_len = sn->len;
  4103. index = 0;
  4104. }
  4105. }
  4106. certKey.type = 0;
  4107. certKey.len = data_len + issuer->len;
  4108. len = sn->len + issuer->len;
  4109. if (len > sizeof (keyBuf)) {
  4110. certKey.data = (unsigned char*)PORT_Alloc(len);
  4111. } else {
  4112. certKey.data = keyBuf;
  4113. }
  4114. if ( certKey.data == NULL ) {
  4115. return(0);
  4116. }
  4117. /* first try the serial number as hand-decoded above*/
  4118. /* copy the serialNumber */
  4119. PORT_Memcpy(certKey.data, &sn->data[index], data_len);
  4120. /* copy the issuer */
  4121. PORT_Memcpy( &certKey.data[data_len],issuer->data,issuer->len);
  4122. trust = nsslowcert_FindTrustByKey(handle, &certKey);
  4123. if (trust) {
  4124. pkcs11_freeStaticData(certKey.data, keyBuf);
  4125. return (trust);
  4126. }
  4127. if (index == 0) {
  4128. pkcs11_freeStaticData(certKey.data, keyBuf);
  4129. return NULL;
  4130. }
  4131. /* didn't find it, try by der encoded serial number */
  4132. /* copy the serialNumber */
  4133. PORT_Memcpy(certKey.data, sn->data, sn->len);
  4134. /* copy the issuer */
  4135. PORT_Memcpy( &certKey.data[sn->len], issuer->data, issuer->len);
  4136. certKey.len = sn->len + issuer->len;
  4137. trust = nsslowcert_FindTrustByKey(handle, &certKey);
  4138. pkcs11_freeStaticData(certKey.data, keyBuf);
  4139. return(trust);
  4140. }
  4141. /*
  4142. * look for the given DER certificate in the database
  4143. */
  4144. NSSLOWCERTCertificate *
  4145. nsslowcert_FindCertByDERCert(NSSLOWCERTCertDBHandle *handle, SECItem *derCert)
  4146. {
  4147. PRArenaPool *arena;
  4148. SECItem certKey;
  4149. SECStatus rv;
  4150. NSSLOWCERTCertificate *cert = NULL;
  4151. /* create a scratch arena */
  4152. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  4153. if ( arena == NULL ) {
  4154. return(NULL);
  4155. }
  4156. /* extract the database key from the cert */
  4157. rv = nsslowcert_KeyFromDERCert(arena, derCert, &certKey);
  4158. if ( rv != SECSuccess ) {
  4159. goto loser;
  4160. }
  4161. /* find the certificate */
  4162. cert = nsslowcert_FindCertByKey(handle, &certKey);
  4163. loser:
  4164. PORT_FreeArena(arena, PR_FALSE);
  4165. return(cert);
  4166. }
  4167. static void
  4168. DestroyCertificate(NSSLOWCERTCertificate *cert, PRBool lockdb)
  4169. {
  4170. int refCount;
  4171. NSSLOWCERTCertDBHandle *handle;
  4172. if ( cert ) {
  4173. handle = cert->dbhandle;
  4174. /*
  4175. * handle may be NULL, for example if the cert was created with
  4176. * nsslowcert_DecodeDERCertificate.
  4177. */
  4178. if ( lockdb && handle ) {
  4179. nsslowcert_LockDB(handle);
  4180. }
  4181. nsslowcert_LockCertRefCount(cert);
  4182. PORT_Assert(cert->referenceCount > 0);
  4183. refCount = --cert->referenceCount;
  4184. nsslowcert_UnlockCertRefCount(cert);
  4185. if ( ( refCount == 0 ) ) {
  4186. certDBEntryCert *entry = cert->dbEntry;
  4187. if ( entry ) {
  4188. DestroyDBEntry((certDBEntry *)entry);
  4189. }
  4190. pkcs11_freeNickname(cert->nickname,cert->nicknameSpace);
  4191. pkcs11_freeNickname(cert->emailAddr,cert->emailAddrSpace);
  4192. pkcs11_freeStaticData(cert->certKey.data,cert->certKeySpace);
  4193. cert->certKey.data = NULL;
  4194. cert->nickname = NULL;
  4195. /* zero cert before freeing. Any stale references to this cert
  4196. * after this point will probably cause an exception. */
  4197. PORT_Memset(cert, 0, sizeof *cert);
  4198. /* use reflock to protect the free list */
  4199. nsslowcert_LockFreeList();
  4200. if (certListCount > MAX_CERT_LIST_COUNT) {
  4201. PORT_Free(cert);
  4202. } else {
  4203. certListCount++;
  4204. cert->next = certListHead;
  4205. certListHead = cert;
  4206. }
  4207. nsslowcert_UnlockFreeList();
  4208. cert = NULL;
  4209. }
  4210. if ( lockdb && handle ) {
  4211. nsslowcert_UnlockDB(handle);
  4212. }
  4213. }
  4214. return;
  4215. }
  4216. NSSLOWCERTCertificate *
  4217. nsslowcert_CreateCert(void)
  4218. {
  4219. NSSLOWCERTCertificate *cert;
  4220. nsslowcert_LockFreeList();
  4221. cert = certListHead;
  4222. if (cert) {
  4223. certListHead = cert->next;
  4224. certListCount--;
  4225. }
  4226. PORT_Assert(certListCount >= 0);
  4227. nsslowcert_UnlockFreeList();
  4228. if (cert) {
  4229. return cert;
  4230. }
  4231. return PORT_ZNew(NSSLOWCERTCertificate);
  4232. }
  4233. static void
  4234. DestroyCertFreeList(void)
  4235. {
  4236. NSSLOWCERTCertificate *cert;
  4237. nsslowcert_LockFreeList();
  4238. while (NULL != (cert = certListHead)) {
  4239. certListCount--;
  4240. certListHead = cert->next;
  4241. PORT_Free(cert);
  4242. }
  4243. PORT_Assert(!certListCount);
  4244. certListCount = 0;
  4245. nsslowcert_UnlockFreeList();
  4246. }
  4247. void
  4248. nsslowcert_DestroyTrust(NSSLOWCERTTrust *trust)
  4249. {
  4250. certDBEntryCert *entry = trust->dbEntry;
  4251. if ( entry ) {
  4252. DestroyDBEntry((certDBEntry *)entry);
  4253. }
  4254. pkcs11_freeStaticData(trust->dbKey.data,trust->dbKeySpace);
  4255. PORT_Memset(trust, 0, sizeof(*trust));
  4256. nsslowcert_LockFreeList();
  4257. if (trustListCount > MAX_TRUST_LIST_COUNT) {
  4258. PORT_Free(trust);
  4259. } else {
  4260. trustListCount++;
  4261. trust->next = trustListHead;
  4262. trustListHead = trust;
  4263. }
  4264. nsslowcert_UnlockFreeList();
  4265. return;
  4266. }
  4267. void
  4268. nsslowcert_DestroyCertificate(NSSLOWCERTCertificate *cert)
  4269. {
  4270. DestroyCertificate(cert, PR_TRUE);
  4271. return;
  4272. }
  4273. static void
  4274. nsslowcert_DestroyCertificateNoLocking(NSSLOWCERTCertificate *cert)
  4275. {
  4276. DestroyCertificate(cert, PR_FALSE);
  4277. return;
  4278. }
  4279. /*
  4280. * Lookup a CRL in the databases. We mirror the same fast caching data base
  4281. * caching stuff used by certificates....?
  4282. */
  4283. certDBEntryRevocation *
  4284. nsslowcert_FindCrlByKey(NSSLOWCERTCertDBHandle *handle,
  4285. SECItem *crlKey, PRBool isKRL)
  4286. {
  4287. SECItem keyitem;
  4288. DBT key;
  4289. SECStatus rv;
  4290. PRArenaPool *arena = NULL;
  4291. certDBEntryRevocation *entry = NULL;
  4292. certDBEntryType crlType = isKRL ? certDBEntryTypeKeyRevocation
  4293. : certDBEntryTypeRevocation;
  4294. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  4295. if ( arena == NULL ) {
  4296. goto loser;
  4297. }
  4298. rv = EncodeDBGenericKey(crlKey, arena, &keyitem, crlType);
  4299. if ( rv != SECSuccess ) {
  4300. goto loser;
  4301. }
  4302. key.data = keyitem.data;
  4303. key.size = keyitem.len;
  4304. /* find in perm database */
  4305. entry = ReadDBCrlEntry(handle, crlKey, crlType);
  4306. if ( entry == NULL ) {
  4307. goto loser;
  4308. }
  4309. loser:
  4310. if ( arena ) {
  4311. PORT_FreeArena(arena, PR_FALSE);
  4312. }
  4313. return entry;
  4314. }
  4315. /*
  4316. * replace the existing URL in the data base with a new one
  4317. */
  4318. static SECStatus
  4319. nsslowcert_UpdateCrl(NSSLOWCERTCertDBHandle *handle, SECItem *derCrl,
  4320. SECItem *crlKey, char *url, PRBool isKRL)
  4321. {
  4322. SECStatus rv = SECFailure;
  4323. certDBEntryRevocation *entry = NULL;
  4324. certDBEntryType crlType = isKRL ? certDBEntryTypeKeyRevocation
  4325. : certDBEntryTypeRevocation;
  4326. DeleteDBCrlEntry(handle, crlKey, crlType);
  4327. /* Write the new entry into the data base */
  4328. entry = NewDBCrlEntry(derCrl, url, crlType, 0);
  4329. if (entry == NULL) goto done;
  4330. rv = WriteDBCrlEntry(handle, entry, crlKey);
  4331. if (rv != SECSuccess) goto done;
  4332. done:
  4333. if (entry) {
  4334. DestroyDBEntry((certDBEntry *)entry);
  4335. }
  4336. return rv;
  4337. }
  4338. SECStatus
  4339. nsslowcert_AddCrl(NSSLOWCERTCertDBHandle *handle, SECItem *derCrl,
  4340. SECItem *crlKey, char *url, PRBool isKRL)
  4341. {
  4342. SECStatus rv;
  4343. rv = nsslowcert_UpdateCrl(handle, derCrl, crlKey, url, isKRL);
  4344. return rv;
  4345. }
  4346. SECStatus
  4347. nsslowcert_DeletePermCRL(NSSLOWCERTCertDBHandle *handle, const SECItem *derName,
  4348. PRBool isKRL)
  4349. {
  4350. SECStatus rv;
  4351. certDBEntryType crlType = isKRL ? certDBEntryTypeKeyRevocation
  4352. : certDBEntryTypeRevocation;
  4353. rv = DeleteDBCrlEntry(handle, derName, crlType);
  4354. if (rv != SECSuccess) goto done;
  4355. done:
  4356. return rv;
  4357. }
  4358. PRBool
  4359. nsslowcert_hasTrust(NSSLOWCERTCertTrust *trust)
  4360. {
  4361. if (trust == NULL) {
  4362. return PR_FALSE;
  4363. }
  4364. return !((trust->sslFlags & CERTDB_TRUSTED_UNKNOWN) &&
  4365. (trust->emailFlags & CERTDB_TRUSTED_UNKNOWN) &&
  4366. (trust->objectSigningFlags & CERTDB_TRUSTED_UNKNOWN));
  4367. }
  4368. /*
  4369. * This function has the logic that decides if another person's cert and
  4370. * email profile from an S/MIME message should be saved. It can deal with
  4371. * the case when there is no profile.
  4372. */
  4373. static SECStatus
  4374. nsslowcert_UpdateSMimeProfile(NSSLOWCERTCertDBHandle *dbhandle,
  4375. char *emailAddr, SECItem *derSubject, SECItem *emailProfile,
  4376. SECItem *profileTime)
  4377. {
  4378. certDBEntrySMime *entry = NULL;
  4379. SECStatus rv = SECFailure;;
  4380. /* find our existing entry */
  4381. entry = nsslowcert_ReadDBSMimeEntry(dbhandle, emailAddr);
  4382. if ( entry ) {
  4383. /* keep our old db entry consistant for old applications. */
  4384. if (!SECITEM_ItemsAreEqual(derSubject, &entry->subjectName)) {
  4385. nsslowcert_UpdateSubjectEmailAddr(dbhandle, &entry->subjectName,
  4386. emailAddr, nsslowcert_remove);
  4387. }
  4388. DestroyDBEntry((certDBEntry *)entry);
  4389. entry = NULL;
  4390. }
  4391. /* now save the entry */
  4392. entry = NewDBSMimeEntry(emailAddr, derSubject, emailProfile,
  4393. profileTime, 0);
  4394. if ( entry == NULL ) {
  4395. rv = SECFailure;
  4396. goto loser;
  4397. }
  4398. nsslowcert_LockDB(dbhandle);
  4399. rv = DeleteDBSMimeEntry(dbhandle, emailAddr);
  4400. /* if delete fails, try to write new entry anyway... */
  4401. /* link subject entry back here */
  4402. rv = nsslowcert_UpdateSubjectEmailAddr(dbhandle, derSubject, emailAddr,
  4403. nsslowcert_add);
  4404. if ( rv != SECSuccess ) {
  4405. nsslowcert_UnlockDB(dbhandle);
  4406. goto loser;
  4407. }
  4408. rv = WriteDBSMimeEntry(dbhandle, entry);
  4409. if ( rv != SECSuccess ) {
  4410. nsslowcert_UnlockDB(dbhandle);
  4411. goto loser;
  4412. }
  4413. nsslowcert_UnlockDB(dbhandle);
  4414. rv = SECSuccess;
  4415. loser:
  4416. if ( entry ) {
  4417. DestroyDBEntry((certDBEntry *)entry);
  4418. }
  4419. return(rv);
  4420. }
  4421. SECStatus
  4422. nsslowcert_SaveSMimeProfile(NSSLOWCERTCertDBHandle *dbhandle, char *emailAddr,
  4423. SECItem *derSubject, SECItem *emailProfile, SECItem *profileTime)
  4424. {
  4425. SECStatus rv = SECFailure;;
  4426. rv = nsslowcert_UpdateSMimeProfile(dbhandle, emailAddr,
  4427. derSubject, emailProfile, profileTime);
  4428. return(rv);
  4429. }
  4430. void
  4431. nsslowcert_DestroyFreeLists(void)
  4432. {
  4433. if (freeListLock == NULL) {
  4434. return;
  4435. }
  4436. DestroyCertEntryFreeList();
  4437. DestroyTrustFreeList();
  4438. DestroyCertFreeList();
  4439. SKIP_AFTER_FORK(PZ_DestroyLock(freeListLock));
  4440. freeListLock = NULL;
  4441. }
  4442. void
  4443. nsslowcert_DestroyGlobalLocks(void)
  4444. {
  4445. if (dbLock) {
  4446. SKIP_AFTER_FORK(PZ_DestroyLock(dbLock));
  4447. dbLock = NULL;
  4448. }
  4449. if (certRefCountLock) {
  4450. SKIP_AFTER_FORK(PZ_DestroyLock(certRefCountLock));
  4451. certRefCountLock = NULL;
  4452. }
  4453. if (certTrustLock) {
  4454. SKIP_AFTER_FORK(PZ_DestroyLock(certTrustLock));
  4455. certTrustLock = NULL;
  4456. }
  4457. }
  4458. certDBEntry *
  4459. nsslowcert_DecodeAnyDBEntry(SECItem *dbData, const SECItem *dbKey,
  4460. certDBEntryType entryType, void *pdata)
  4461. {
  4462. PLArenaPool *arena = NULL;
  4463. certDBEntry *entry;
  4464. SECStatus rv;
  4465. SECItem dbEntry;
  4466. if ((dbData->len < SEC_DB_ENTRY_HEADER_LEN) || (dbKey->len == 0)) {
  4467. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  4468. goto loser;
  4469. }
  4470. dbEntry.data = &dbData->data[SEC_DB_ENTRY_HEADER_LEN];
  4471. dbEntry.len = dbData->len - SEC_DB_ENTRY_HEADER_LEN;
  4472. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  4473. if (arena == NULL) {
  4474. goto loser;
  4475. }
  4476. entry = PORT_ArenaZNew(arena, certDBEntry);
  4477. if (!entry)
  4478. goto loser;
  4479. entry->common.version = (unsigned int)dbData->data[0];
  4480. entry->common.flags = (unsigned int)dbData->data[2];
  4481. entry->common.type = entryType;
  4482. entry->common.arena = arena;
  4483. switch (entryType) {
  4484. case certDBEntryTypeContentVersion: /* This type appears to be unused */
  4485. case certDBEntryTypeVersion: /* This type has only the common hdr */
  4486. rv = SECSuccess;
  4487. break;
  4488. case certDBEntryTypeSubject:
  4489. rv = DecodeDBSubjectEntry(&entry->subject, &dbEntry, dbKey);
  4490. break;
  4491. case certDBEntryTypeNickname:
  4492. rv = DecodeDBNicknameEntry(&entry->nickname, &dbEntry,
  4493. (char *)dbKey->data);
  4494. break;
  4495. /* smime profiles need entries created after the certs have
  4496. * been imported, loop over them in a second run */
  4497. case certDBEntryTypeSMimeProfile:
  4498. rv = DecodeDBSMimeEntry(&entry->smime, &dbEntry, (char *)dbKey->data);
  4499. break;
  4500. case certDBEntryTypeCert:
  4501. rv = DecodeDBCertEntry(&entry->cert, &dbEntry);
  4502. break;
  4503. case certDBEntryTypeKeyRevocation:
  4504. case certDBEntryTypeRevocation:
  4505. rv = DecodeDBCrlEntry(&entry->revocation, &dbEntry);
  4506. break;
  4507. default:
  4508. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  4509. rv = SECFailure;
  4510. }
  4511. if (rv == SECSuccess)
  4512. return entry;
  4513. loser:
  4514. if (arena)
  4515. PORT_FreeArena(arena, PR_FALSE);
  4516. return NULL;
  4517. }