| 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 |
* Rob Crittenden ([email protected]) |
| 23 |
* |
| 24 |
* Alternatively, the contents of this file may be used under the terms of |
| 25 |
* either the GNU General Public License Version 2 or later (the "GPL"), or |
| 26 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 27 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
| 28 |
* of those above. If you wish to allow use of your version of this file only |
| 29 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
| 30 |
* use your version of this file under the terms of the MPL, indicate your |
| 31 |
* decision by deleting the provisions above and replace them with the notice |
| 32 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
| 33 |
* the provisions above, a recipient may use your version of this file under |
| 34 |
* the terms of any one of the MPL, the GPL or the LGPL. |
| 35 |
* |
| 36 |
* ***** END LICENSE BLOCK ***** */ |
| 37 |
|
| 38 |
#include "ckpem.h" |
| 39 |
|
| 40 |
#include <blapi.h> |
| 41 |
|
| 42 |
#include <stdlib.h> |
| 43 |
|
| 44 |
/* |
| 45 |
* pinstance.c |
| 46 |
* |
| 47 |
* This file implements the NSSCKMDInstance object for the |
| 48 |
* "PEM objects" cryptoki module. |
| 49 |
*/ |
| 50 |
|
| 51 |
static PRBool pemInitialized = PR_FALSE; |
| 52 |
|
| 53 |
LIST_HEAD(pem_objs); |
| 54 |
long pem_nobjs = 0L; |
| 55 |
int token_needsLogin[NUM_SLOTS]; |
| 56 |
NSSCKMDSlot *lastEventSlot; |
| 57 |
|
| 58 |
/* |
| 59 |
* simple cert decoder to avoid the cost of asn1 engine |
| 60 |
*/ |
| 61 |
static unsigned char * |
| 62 |
dataStart(unsigned char *buf, unsigned int length, |
| 63 |
unsigned int *data_length, |
| 64 |
PRBool includeTag, unsigned char *rettag) |
| 65 |
{ |
| 66 |
unsigned char tag; |
| 67 |
unsigned int used_length = 0; |
| 68 |
if (!length) |
| 69 |
return NULL; |
| 70 |
|
| 71 |
tag = buf[used_length++]; |
| 72 |
|
| 73 |
if (rettag) { |
| 74 |
*rettag = tag; |
| 75 |
} |
| 76 |
|
| 77 |
/* blow out when we come to the end */ |
| 78 |
if (tag == 0 || length <= used_length) { |
| 79 |
return NULL; |
| 80 |
} |
| 81 |
|
| 82 |
*data_length = buf[used_length++]; |
| 83 |
|
| 84 |
if (*data_length & 0x80) { |
| 85 |
int len_count = *data_length & 0x7f; |
| 86 |
|
| 87 |
*data_length = 0; |
| 88 |
|
| 89 |
while (len_count-- > 0) { |
| 90 |
if (length <= used_length) |
| 91 |
return NULL; |
| 92 |
|
| 93 |
*data_length = (*data_length << 8) | buf[used_length++]; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if (*data_length > (length - used_length)) { |
| 98 |
*data_length = length - used_length; |
| 99 |
return NULL; |
| 100 |
} |
| 101 |
if (includeTag) |
| 102 |
*data_length += used_length; |
| 103 |
|
| 104 |
return (buf + (includeTag ? 0 : used_length)); |
| 105 |
} |
| 106 |
|
| 107 |
static int |
| 108 |
GetCertFields(unsigned char *cert, int cert_length, |
| 109 |
SECItem * issuer, SECItem * serial, SECItem * derSN, |
| 110 |
SECItem * subject, SECItem * valid, SECItem * subjkey) |
| 111 |
{ |
| 112 |
unsigned char *buf; |
| 113 |
unsigned int buf_length; |
| 114 |
unsigned char *dummy; |
| 115 |
unsigned int dummylen; |
| 116 |
|
| 117 |
/* get past the signature wrap */ |
| 118 |
buf = dataStart(cert, cert_length, &buf_length, PR_FALSE, NULL); |
| 119 |
if (buf == NULL) |
| 120 |
return SECFailure; |
| 121 |
/* get into the raw cert data */ |
| 122 |
buf = dataStart(buf, buf_length, &buf_length, PR_FALSE, NULL); |
| 123 |
if (buf == NULL) |
| 124 |
return SECFailure; |
| 125 |
/* skip past any optional version number */ |
| 126 |
if ((buf[0] & 0xa0) == 0xa0) { |
| 127 |
dummy = dataStart(buf, buf_length, &dummylen, PR_FALSE, NULL); |
| 128 |
if (dummy == NULL) |
| 129 |
return SECFailure; |
| 130 |
buf_length -= (dummy - buf) + dummylen; |
| 131 |
buf = dummy + dummylen; |
| 132 |
} |
| 133 |
/* serial number */ |
| 134 |
if (derSN) { |
| 135 |
derSN->data = |
| 136 |
dataStart(buf, buf_length, &derSN->len, PR_TRUE, NULL); |
| 137 |
} |
| 138 |
serial->data = |
| 139 |
dataStart(buf, buf_length, &serial->len, PR_FALSE, NULL); |
| 140 |
if (serial->data == NULL) |
| 141 |
return SECFailure; |
| 142 |
buf_length -= (serial->data - buf) + serial->len; |
| 143 |
buf = serial->data + serial->len; |
| 144 |
/* skip the OID */ |
| 145 |
dummy = dataStart(buf, buf_length, &dummylen, PR_FALSE, NULL); |
| 146 |
if (dummy == NULL) |
| 147 |
return SECFailure; |
| 148 |
buf_length -= (dummy - buf) + dummylen; |
| 149 |
buf = dummy + dummylen; |
| 150 |
/* issuer */ |
| 151 |
issuer->data = dataStart(buf, buf_length, &issuer->len, PR_TRUE, NULL); |
| 152 |
if (issuer->data == NULL) |
| 153 |
return SECFailure; |
| 154 |
buf_length -= (issuer->data - buf) + issuer->len; |
| 155 |
buf = issuer->data + issuer->len; |
| 156 |
|
| 157 |
/* only wanted issuer/SN */ |
| 158 |
if (subject == NULL || valid == NULL || subjkey == NULL) { |
| 159 |
return SECSuccess; |
| 160 |
} |
| 161 |
/* validity */ |
| 162 |
valid->data = dataStart(buf, buf_length, &valid->len, PR_FALSE, NULL); |
| 163 |
if (valid->data == NULL) |
| 164 |
return SECFailure; |
| 165 |
buf_length -= (valid->data - buf) + valid->len; |
| 166 |
buf = valid->data + valid->len; |
| 167 |
/*subject */ |
| 168 |
subject->data = |
| 169 |
dataStart(buf, buf_length, &subject->len, PR_TRUE, NULL); |
| 170 |
if (subject->data == NULL) |
| 171 |
return SECFailure; |
| 172 |
buf_length -= (subject->data - buf) + subject->len; |
| 173 |
buf = subject->data + subject->len; |
| 174 |
/* subject key info */ |
| 175 |
subjkey->data = |
| 176 |
dataStart(buf, buf_length, &subjkey->len, PR_TRUE, NULL); |
| 177 |
if (subjkey->data == NULL) |
| 178 |
return SECFailure; |
| 179 |
buf_length -= (subjkey->data - buf) + subjkey->len; |
| 180 |
buf = subjkey->data + subjkey->len; |
| 181 |
return SECSuccess; |
| 182 |
} |
| 183 |
|
| 184 |
static CK_RV |
| 185 |
assignObjectID(pemInternalObject *o, const long objid) |
| 186 |
{ |
| 187 |
char id[24]; |
| 188 |
int len; |
| 189 |
|
| 190 |
sprintf(id, "%ld", objid); |
| 191 |
len = strlen(id) + 1; /* zero terminate */ |
| 192 |
o->id.size = len; |
| 193 |
o->id.data = NSS_ZAlloc(NULL, len); |
| 194 |
if (o->id.data == NULL) |
| 195 |
return CKR_HOST_MEMORY; |
| 196 |
|
| 197 |
memcpy(o->id.data, id, len); |
| 198 |
o->objid = objid; |
| 199 |
return CKR_OK; |
| 200 |
} |
| 201 |
|
| 202 |
static pemInternalObject * |
| 203 |
CreateObject(CK_OBJECT_CLASS objClass, |
| 204 |
pemObjectType type, SECItem * certDER, |
| 205 |
SECItem * keyDER, const char *filename, |
| 206 |
long objid, CK_SLOT_ID slotID) |
| 207 |
{ |
| 208 |
pemInternalObject *o; |
| 209 |
SECItem subject; |
| 210 |
SECItem issuer; |
| 211 |
SECItem serial; |
| 212 |
SECItem derSN; |
| 213 |
SECItem valid; |
| 214 |
SECItem subjkey; |
| 215 |
const char *nickname; |
| 216 |
|
| 217 |
o = NSS_ZNEW(NULL, pemInternalObject); |
| 218 |
if ((pemInternalObject *) NULL == o) { |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
|
| 222 |
nickname = strrchr(filename, '/'); |
| 223 |
if (nickname) |
| 224 |
nickname++; |
| 225 |
else |
| 226 |
nickname = filename; |
| 227 |
|
| 228 |
switch (objClass) { |
| 229 |
case CKO_CERTIFICATE: |
| 230 |
plog("Creating cert nick %s id %ld in slot %ld\n", nickname, objid, slotID); |
| 231 |
memset(&o->u.cert, 0, sizeof(o->u.cert)); |
| 232 |
break; |
| 233 |
case CKO_PRIVATE_KEY: |
| 234 |
plog("Creating key id %ld in slot %ld\n", objid, slotID); |
| 235 |
memset(&o->u.key, 0, sizeof(o->u.key)); |
| 236 |
/* more unique nicknames - https://bugzilla.redhat.com/689031#c66 */ |
| 237 |
nickname = filename; |
| 238 |
break; |
| 239 |
case CKO_NSS_TRUST: |
| 240 |
plog("Creating trust nick %s id %ld in slot %ld\n", nickname, objid, slotID); |
| 241 |
memset(&o->u.trust, 0, sizeof(o->u.trust)); |
| 242 |
break; |
| 243 |
} |
| 244 |
|
| 245 |
o->nickname = (char *) NSS_ZAlloc(NULL, strlen(nickname) + 1); |
| 246 |
if (o->nickname == NULL) |
| 247 |
goto fail; |
| 248 |
strcpy(o->nickname, nickname); |
| 249 |
|
| 250 |
if (CKR_OK != assignObjectID(o, objid)) |
| 251 |
goto fail; |
| 252 |
|
| 253 |
o->objClass = objClass; |
| 254 |
o->type = type; |
| 255 |
o->slotID = slotID; |
| 256 |
|
| 257 |
o->derCert = NSS_ZNEW(NULL, SECItem); |
| 258 |
if (o->derCert == NULL) |
| 259 |
goto fail; |
| 260 |
o->derCert->data = (void *) NSS_ZAlloc(NULL, certDER->len); |
| 261 |
if (o->derCert->data == NULL) |
| 262 |
goto fail; |
| 263 |
o->derCert->len = certDER->len; |
| 264 |
memcpy(o->derCert->data, certDER->data, certDER->len); |
| 265 |
|
| 266 |
switch (objClass) { |
| 267 |
case CKO_CERTIFICATE: |
| 268 |
case CKO_NSS_TRUST: |
| 269 |
if (SECSuccess != GetCertFields(o->derCert->data, o->derCert->len, |
| 270 |
&issuer, &serial, &derSN, &subject, |
| 271 |
&valid, &subjkey)) |
| 272 |
goto fail; |
| 273 |
|
| 274 |
o->u.cert.subject.data = (void *) NSS_ZAlloc(NULL, subject.len); |
| 275 |
if (o->u.cert.subject.data == NULL) |
| 276 |
goto fail; |
| 277 |
o->u.cert.subject.size = subject.len; |
| 278 |
memcpy(o->u.cert.subject.data, subject.data, subject.len); |
| 279 |
|
| 280 |
o->u.cert.issuer.data = (void *) NSS_ZAlloc(NULL, issuer.len); |
| 281 |
if (o->u.cert.issuer.data == NULL) { |
| 282 |
NSS_ZFreeIf(o->u.cert.subject.data); |
| 283 |
goto fail; |
| 284 |
} |
| 285 |
o->u.cert.issuer.size = issuer.len; |
| 286 |
memcpy(o->u.cert.issuer.data, issuer.data, issuer.len); |
| 287 |
|
| 288 |
o->u.cert.serial.data = (void *) NSS_ZAlloc(NULL, serial.len); |
| 289 |
if (o->u.cert.serial.data == NULL) { |
| 290 |
NSS_ZFreeIf(o->u.cert.issuer.data); |
| 291 |
NSS_ZFreeIf(o->u.cert.subject.data); |
| 292 |
goto fail; |
| 293 |
} |
| 294 |
o->u.cert.serial.size = serial.len; |
| 295 |
memcpy(o->u.cert.serial.data, serial.data, serial.len); |
| 296 |
break; |
| 297 |
case CKO_PRIVATE_KEY: |
| 298 |
o->u.key.key.privateKey = NSS_ZNEW(NULL, SECItem); |
| 299 |
if (o->u.key.key.privateKey == NULL) |
| 300 |
goto fail; |
| 301 |
o->u.key.key.privateKey->data = |
| 302 |
(void *) NSS_ZAlloc(NULL, keyDER->len); |
| 303 |
if (o->u.key.key.privateKey->data == NULL) { |
| 304 |
NSS_ZFreeIf(o->u.key.key.privateKey); |
| 305 |
goto fail; |
| 306 |
} |
| 307 |
|
| 308 |
/* store deep copy of original key DER so we can compare it later on */ |
| 309 |
o->u.key.key.privateKeyOrig = SECITEM_DupItem(keyDER); |
| 310 |
if (o->u.key.key.privateKeyOrig == NULL) { |
| 311 |
NSS_ZFreeIf(o->u.key.key.privateKey->data); |
| 312 |
NSS_ZFreeIf(o->u.key.key.privateKey); |
| 313 |
goto fail; |
| 314 |
} |
| 315 |
|
| 316 |
o->u.key.key.privateKey->len = keyDER->len; |
| 317 |
memcpy(o->u.key.key.privateKey->data, keyDER->data, keyDER->len); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
return o; |
| 322 |
|
| 323 |
fail: |
| 324 |
if (o) { |
| 325 |
if (o->derCert) { |
| 326 |
NSS_ZFreeIf(o->derCert->data); |
| 327 |
NSS_ZFreeIf(o->derCert); |
| 328 |
} |
| 329 |
NSS_ZFreeIf(o->id.data); |
| 330 |
NSS_ZFreeIf(o->nickname); |
| 331 |
NSS_ZFreeIf(o); |
| 332 |
} |
| 333 |
return NULL; |
| 334 |
} |
| 335 |
|
| 336 |
/* Compare the DER encoding of the internal object against those |
| 337 |
* of the provided certDER or keyDER according to its objClass. |
| 338 |
*/ |
| 339 |
static PRBool |
| 340 |
derEncodingsMatch(CK_OBJECT_CLASS objClass, pemInternalObject * obj, |
| 341 |
SECItem * certDER, SECItem * keyDER) |
| 342 |
{ |
| 343 |
SECComparison result; |
| 344 |
|
| 345 |
switch (objClass) { |
| 346 |
case CKO_CERTIFICATE: |
| 347 |
case CKO_NSS_TRUST: |
| 348 |
result = SECITEM_CompareItem(obj->derCert, certDER); |
| 349 |
break; |
| 350 |
|
| 351 |
case CKO_PRIVATE_KEY: |
| 352 |
result = SECITEM_CompareItem(obj->u.key.key.privateKeyOrig, keyDER); |
| 353 |
break; |
| 354 |
|
| 355 |
default: |
| 356 |
/* unhandled object class */ |
| 357 |
return PR_FALSE; |
| 358 |
} |
| 359 |
|
| 360 |
return SECEqual == result; |
| 361 |
} |
| 362 |
|
| 363 |
static CK_RV |
| 364 |
LinkSharedKeyObject(const long oldKeyIdx, const long newKeyIdx) |
| 365 |
{ |
| 366 |
pemInternalObject *obj; |
| 367 |
list_for_each_entry(obj, &pem_objs, gl_list) { |
| 368 |
CK_RV rv; |
| 369 |
if (obj->objid != oldKeyIdx) |
| 370 |
continue; |
| 371 |
|
| 372 |
NSS_ZFreeIf(obj->id.data); |
| 373 |
rv = assignObjectID(obj, newKeyIdx); |
| 374 |
if (CKR_OK != rv) |
| 375 |
return rv; |
| 376 |
} |
| 377 |
|
| 378 |
return CKR_OK; |
| 379 |
} |
| 380 |
|
| 381 |
/* return pointer to the internal object with given arrayIdx */ |
| 382 |
static pemInternalObject * |
| 383 |
FindObjectByArrayIdx(const long arrayIdx) |
| 384 |
{ |
| 385 |
pemInternalObject *obj; |
| 386 |
|
| 387 |
list_for_each_entry(obj, &pem_objs, gl_list) |
| 388 |
if (arrayIdx == obj->arrayIdx) |
| 389 |
return obj; |
| 390 |
|
| 391 |
return NULL; |
| 392 |
} |
| 393 |
|
| 394 |
pemInternalObject * |
| 395 |
AddObjectIfNeeded(CK_OBJECT_CLASS objClass, |
| 396 |
pemObjectType type, SECItem * certDER, |
| 397 |
SECItem * keyDER, const char *filename, |
| 398 |
long objid, CK_SLOT_ID slotID, PRBool *pAdded) |
| 399 |
{ |
| 400 |
pemInternalObject *curObj; |
| 401 |
|
| 402 |
const char *nickname = strrchr(filename, '/'); |
| 403 |
if (nickname |
| 404 |
/* more unique nicknames - https://bugzilla.redhat.com/689031#c66 */ |
| 405 |
&& CKO_PRIVATE_KEY != objClass) |
| 406 |
nickname++; |
| 407 |
else |
| 408 |
nickname = filename; |
| 409 |
|
| 410 |
if (pAdded) |
| 411 |
*pAdded = PR_FALSE; |
| 412 |
|
| 413 |
/* first look for the object in pem_objs, it might be already there */ |
| 414 |
list_for_each_entry(curObj, &pem_objs, gl_list) { |
| 415 |
/* Comparing DER encodings is dependable and frees the PEM module |
| 416 |
* from having to require clients to provide unique nicknames. |
| 417 |
*/ |
| 418 |
if ((curObj->objClass == objClass) |
| 419 |
&& (curObj->type == type) |
| 420 |
&& (curObj->slotID == slotID) |
| 421 |
&& derEncodingsMatch(objClass, curObj, certDER, keyDER)) { |
| 422 |
|
| 423 |
/* While adding a client certificate we (wrongly?) assumed that the |
| 424 |
* key object will follow right after the cert object. However, if |
| 425 |
* the key object is shared by multiple client certificates, such |
| 426 |
* an assumption does not hold. We have to update the references. |
| 427 |
*/ |
| 428 |
LinkSharedKeyObject(pem_nobjs, curObj->arrayIdx); |
| 429 |
|
| 430 |
if (CKO_CERTIFICATE == objClass) { |
| 431 |
const long ref = curObj->objid; |
| 432 |
if (0 < ref && ref < pem_nobjs && !FindObjectByArrayIdx(ref)) { |
| 433 |
/* The certificate we are going to reuse refers to an |
| 434 |
* object that has already been removed. Make it refer |
| 435 |
* to the object that will be added next (private key). |
| 436 |
*/ |
| 437 |
NSS_ZFreeIf(curObj->id.data); |
| 438 |
assignObjectID(curObj, pem_nobjs); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
plog("AddObjectIfNeeded: re-using internal object #%li\n", |
| 443 |
curObj->arrayIdx); |
| 444 |
curObj->refCount ++; |
| 445 |
return curObj; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
/* object not found, we need to create it */ |
| 450 |
pemInternalObject *io = CreateObject(objClass, type, certDER, keyDER, |
| 451 |
filename, objid, slotID); |
| 452 |
if (io == NULL) |
| 453 |
return NULL; |
| 454 |
|
| 455 |
/* initialize pointers to functions */ |
| 456 |
pem_CreateMDObject(NULL, io, NULL); |
| 457 |
|
| 458 |
/* add object to global list */ |
| 459 |
io->arrayIdx = pem_nobjs++; |
| 460 |
list_add_tail(&io->gl_list, &pem_objs); |
| 461 |
|
| 462 |
if (pAdded) |
| 463 |
*pAdded = PR_TRUE; |
| 464 |
|
| 465 |
io->refCount ++; |
| 466 |
return io; |
| 467 |
} |
| 468 |
|
| 469 |
CK_RV |
| 470 |
AddCertificate(char *certfile, char *keyfile, PRBool cacert, |
| 471 |
CK_SLOT_ID slotID) |
| 472 |
{ |
| 473 |
pemInternalObject *o = NULL; |
| 474 |
CK_RV error = 0; |
| 475 |
long objid; |
| 476 |
int i = 0; |
| 477 |
SECItem **objs = NULL; |
| 478 |
char *ivstring = NULL; |
| 479 |
int cipher; |
| 480 |
|
| 481 |
int nobjs = ReadDERFromFile(&objs, certfile, &cipher, &ivstring, |
| 482 |
/* certs only */ PR_TRUE); |
| 483 |
if (nobjs <= 0) { |
| 484 |
NSS_ZFreeIf(objs); |
| 485 |
return CKR_GENERAL_ERROR; |
| 486 |
} |
| 487 |
|
| 488 |
/* For now load as many certs as are in the file for CAs only */ |
| 489 |
if (cacert) { |
| 490 |
for (i = 0; i < nobjs; i++) { |
| 491 |
char nickname[1024]; |
| 492 |
objid = pem_nobjs + 1; |
| 493 |
|
| 494 |
snprintf(nickname, sizeof nickname, "%s - %d", certfile, i); |
| 495 |
|
| 496 |
o = AddObjectIfNeeded(CKO_CERTIFICATE, pemCert, objs[i], NULL, |
| 497 |
nickname, 0, slotID, NULL); |
| 498 |
if (o != NULL) { |
| 499 |
/* Add the CA trust object */ |
| 500 |
o = AddObjectIfNeeded(CKO_NSS_TRUST, pemTrust, objs[i], NULL, |
| 501 |
nickname, 0, slotID, NULL); |
| 502 |
} |
| 503 |
if (o == NULL) { |
| 504 |
error = CKR_GENERAL_ERROR; |
| 505 |
goto loser; |
| 506 |
} |
| 507 |
} /* for */ |
| 508 |
} else { |
| 509 |
PRBool found_error = PR_FALSE; |
| 510 |
|
| 511 |
objid = pem_nobjs + 1; |
| 512 |
o = AddObjectIfNeeded(CKO_CERTIFICATE, pemCert, objs[0], NULL, certfile, |
| 513 |
objid, slotID, NULL); |
| 514 |
|
| 515 |
if (o != NULL && keyfile != NULL) { /* add the private key */ |
| 516 |
SECItem **keyobjs = NULL; |
| 517 |
int kobjs = ReadDERFromFile(&keyobjs, keyfile, &cipher, &ivstring, |
| 518 |
/*certs only */ PR_FALSE); |
| 519 |
if (kobjs < 1) { |
| 520 |
found_error = PR_TRUE; |
| 521 |
} else { |
| 522 |
o = AddObjectIfNeeded(CKO_PRIVATE_KEY, pemBareKey, objs[0], |
| 523 |
keyobjs[0], certfile, objid, slotID, NULL); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
if (found_error || o == NULL) { |
| 528 |
error = CKR_GENERAL_ERROR; |
| 529 |
goto loser; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
NSS_ZFreeIf(objs); |
| 534 |
return CKR_OK; |
| 535 |
|
| 536 |
loser: |
| 537 |
NSS_ZFreeIf(objs); |
| 538 |
NSS_ZFreeIf(o); |
| 539 |
return error; |
| 540 |
} |
| 541 |
|
| 542 |
#define DynPtrList_default_capacity 4 |
| 543 |
#define DynPtrList_default_realloc_factor 2 |
| 544 |
|
| 545 |
static void* |
| 546 |
myDynPtrListAllocWrapper(size_t bytes) |
| 547 |
{ |
| 548 |
return NSS_ZAlloc(NULL, bytes); |
| 549 |
} |
| 550 |
|
| 551 |
static void* |
| 552 |
myDynPtrListReallocWrapper(void *ptr, size_t bytes) |
| 553 |
{ |
| 554 |
return NSS_ZRealloc(ptr, bytes); |
| 555 |
} |
| 556 |
|
| 557 |
static void |
| 558 |
myDynPtrListFreeWrapper(void *ptr) |
| 559 |
{ |
| 560 |
NSS_ZFreeIf(ptr); |
| 561 |
} |
| 562 |
|
| 563 |
/* returns NULL on failure. returns dpl if init was successful. */ |
| 564 |
void* |
| 565 |
pem_InitDynPtrList(DynPtrList *dpl, DynPtrListAllocFunction a, |
| 566 |
DynPtrListReallocFunction r, DynPtrListFreeFunction f) |
| 567 |
{ |
| 568 |
if (!dpl) |
| 569 |
return NULL; |
| 570 |
|
| 571 |
dpl->entries = 0; |
| 572 |
dpl->capacity = DynPtrList_default_capacity; |
| 573 |
|
| 574 |
dpl->alloc_function = a; |
| 575 |
dpl->realloc_function = r; |
| 576 |
dpl->free_function = f; |
| 577 |
|
| 578 |
dpl->pointers = (*dpl->alloc_function)(dpl->capacity * sizeof(void*)); |
| 579 |
if (!dpl->pointers) |
| 580 |
return NULL; |
| 581 |
|
| 582 |
return dpl; |
| 583 |
} |
| 584 |
|
| 585 |
void |
| 586 |
pem_FreeDynPtrList(DynPtrList *dpl) |
| 587 |
{ |
| 588 |
size_t i; |
| 589 |
for (i = 0; i < dpl->entries; ++i) { |
| 590 |
(*dpl->free_function)(dpl->pointers[i]); |
| 591 |
} |
| 592 |
NSS_ZFreeIf(dpl->pointers); |
| 593 |
dpl->pointers = NULL; |
| 594 |
dpl->capacity = 0; |
| 595 |
dpl->entries = 0; |
| 596 |
} |
| 597 |
|
| 598 |
/* returns NULL on failure. Returns str if it could be added.*/ |
| 599 |
void* |
| 600 |
pem_AddToDynPtrList(DynPtrList *dpl, char *ptr) |
| 601 |
{ |
| 602 |
const size_t max_size_t = ((size_t) -1); |
| 603 |
|
| 604 |
if (!dpl->capacity) |
| 605 |
return NULL; /* dpl not initialized */ |
| 606 |
|
| 607 |
if (dpl->capacity == dpl->entries) { |
| 608 |
/* capacity reached, must grow */ |
| 609 |
void **new_pointers = NULL; |
| 610 |
size_t new_capacity; |
| 611 |
|
| 612 |
if ( (((double)max_size_t) / dpl->capacity) < DynPtrList_default_realloc_factor) { |
| 613 |
new_capacity = max_size_t; |
| 614 |
} else { |
| 615 |
new_capacity = dpl->capacity * DynPtrList_default_realloc_factor; |
| 616 |
} |
| 617 |
|
| 618 |
if (dpl->capacity == new_capacity) { |
| 619 |
return NULL; /* cannot grow */ |
| 620 |
} |
| 621 |
|
| 622 |
new_pointers = (*dpl->realloc_function)(dpl->pointers, new_capacity); |
| 623 |
if (new_pointers == dpl->pointers) { |
| 624 |
return NULL; /* cannot grow */ |
| 625 |
} |
| 626 |
|
| 627 |
dpl->pointers = new_pointers; |
| 628 |
dpl->capacity = new_capacity; |
| 629 |
} |
| 630 |
|
| 631 |
dpl->pointers[dpl->entries] = ptr; |
| 632 |
++dpl->entries; |
| 633 |
return ptr; |
| 634 |
} |
| 635 |
|
| 636 |
CK_RV |
| 637 |
pem_Initialize |
| 638 |
( |
| 639 |
NSSCKMDInstance * mdInstance, |
| 640 |
NSSCKFWInstance * fwInstance, |
| 641 |
NSSUTF8 * configurationData |
| 642 |
) |
| 643 |
{ |
| 644 |
CK_RV rv; |
| 645 |
/* parse the initialization string */ |
| 646 |
char *modparms = NULL; |
| 647 |
DynPtrList certstrings; |
| 648 |
PRBool status, error = PR_FALSE; |
| 649 |
int i; |
| 650 |
CK_C_INITIALIZE_ARGS_PTR modArgs = NULL; |
| 651 |
|
| 652 |
if (!fwInstance) return CKR_ARGUMENTS_BAD; |
| 653 |
|
| 654 |
modArgs = NSSCKFWInstance_GetInitArgs(fwInstance); |
| 655 |
if (modArgs && |
| 656 |
((modArgs->flags & CKF_OS_LOCKING_OK) || (modArgs->CreateMutex != 0))) { |
| 657 |
return CKR_CANT_LOCK; |
| 658 |
} |
| 659 |
|
| 660 |
if (pemInitialized) { |
| 661 |
return CKR_OK; |
| 662 |
} |
| 663 |
|
| 664 |
RNG_RNGInit(); |
| 665 |
|
| 666 |
open_nss_pem_log(); |
| 667 |
|
| 668 |
plog("pem_Initialize\n"); |
| 669 |
|
| 670 |
if (!modArgs || !modArgs->LibraryParameters) { |
| 671 |
goto done; |
| 672 |
} |
| 673 |
|
| 674 |
modparms = (char *) modArgs->LibraryParameters; |
| 675 |
plog("Initialized with %s\n", modparms); |
| 676 |
|
| 677 |
/* |
| 678 |
* The initialization string format is a space-delimited file of |
| 679 |
* pairs of paths which are delimited by a semi-colon. The first |
| 680 |
* entry of the pair is the path to the certificate file. The |
| 681 |
* second is the path to the key file. |
| 682 |
* |
| 683 |
* CA certificates do not need the semi-colon. |
| 684 |
* |
| 685 |
* Example: |
| 686 |
* /etc/certs/server.pem;/etc/certs/server.key /etc/certs/ca.pem |
| 687 |
* |
| 688 |
*/ |
| 689 |
pem_InitDynPtrList(&certstrings, myDynPtrListAllocWrapper, |
| 690 |
myDynPtrListReallocWrapper, myDynPtrListFreeWrapper); |
| 691 |
status = pem_ParseString(modparms, ' ', &certstrings); |
| 692 |
if (status == PR_FALSE) { |
| 693 |
return CKR_ARGUMENTS_BAD; |
| 694 |
} |
| 695 |
|
| 696 |
for (i = 0; i < certstrings.entries && error != PR_TRUE; i++) { |
| 697 |
char *cert = (char*)certstrings.pointers[i]; |
| 698 |
DynPtrList certattrs; |
| 699 |
|
| 700 |
pem_InitDynPtrList(&certattrs, myDynPtrListAllocWrapper, |
| 701 |
myDynPtrListReallocWrapper, myDynPtrListFreeWrapper); |
| 702 |
status = pem_ParseString(cert, ';', &certattrs); |
| 703 |
if (status == PR_FALSE) { |
| 704 |
error = PR_TRUE; |
| 705 |
break; |
| 706 |
} |
| 707 |
|
| 708 |
if (error == PR_FALSE) { |
| 709 |
if (certattrs.entries == 1) /* CA certificate */ |
| 710 |
rv = AddCertificate(certattrs.pointers[0], NULL, PR_TRUE, i); |
| 711 |
else |
| 712 |
rv = AddCertificate(certattrs.pointers[0], certattrs.pointers[1], |
| 713 |
PR_FALSE, i); |
| 714 |
|
| 715 |
if (rv != CKR_OK) { |
| 716 |
error = PR_TRUE; |
| 717 |
status = PR_FALSE; |
| 718 |
} |
| 719 |
} |
| 720 |
pem_FreeDynPtrList(&certattrs); |
| 721 |
} |
| 722 |
pem_FreeDynPtrList(&certstrings); |
| 723 |
|
| 724 |
if (status == PR_FALSE) { |
| 725 |
return CKR_ARGUMENTS_BAD; |
| 726 |
} |
| 727 |
|
| 728 |
for (i = 0; i < NUM_SLOTS; i++) |
| 729 |
token_needsLogin[i] = PR_FALSE; |
| 730 |
|
| 731 |
done: |
| 732 |
|
| 733 |
PR_AtomicSet(&pemInitialized, PR_TRUE); |
| 734 |
|
| 735 |
return CKR_OK; |
| 736 |
} |
| 737 |
|
| 738 |
void |
| 739 |
pem_Finalize |
| 740 |
( |
| 741 |
NSSCKMDInstance * mdInstance, |
| 742 |
NSSCKFWInstance * fwInstance |
| 743 |
) |
| 744 |
{ |
| 745 |
plog("pem_Finalize\n"); |
| 746 |
if (!pemInitialized) |
| 747 |
return; |
| 748 |
|
| 749 |
INIT_LIST_HEAD(&pem_objs); |
| 750 |
pem_nobjs = 0L; |
| 751 |
|
| 752 |
PR_AtomicSet(&pemInitialized, PR_FALSE); |
| 753 |
} |
| 754 |
|
| 755 |
/* |
| 756 |
* NSSCKMDInstance methods |
| 757 |
*/ |
| 758 |
|
| 759 |
static CK_ULONG |
| 760 |
pem_mdInstance_GetNSlots |
| 761 |
( |
| 762 |
NSSCKMDInstance * mdInstance, |
| 763 |
NSSCKFWInstance * fwInstance, |
| 764 |
CK_RV * pError |
| 765 |
) |
| 766 |
{ |
| 767 |
return (CK_ULONG) NUM_SLOTS; |
| 768 |
} |
| 769 |
|
| 770 |
static CK_VERSION |
| 771 |
pem_mdInstance_GetCryptokiVersion |
| 772 |
( |
| 773 |
NSSCKMDInstance * mdInstance, |
| 774 |
NSSCKFWInstance * fwInstance |
| 775 |
) |
| 776 |
{ |
| 777 |
return pem_CryptokiVersion; |
| 778 |
} |
| 779 |
|
| 780 |
static NSSUTF8 * |
| 781 |
pem_mdInstance_GetManufacturerID |
| 782 |
( |
| 783 |
NSSCKMDInstance * mdInstance, |
| 784 |
NSSCKFWInstance * fwInstance, |
| 785 |
CK_RV * pError |
| 786 |
) |
| 787 |
{ |
| 788 |
return (NSSUTF8 *) pem_ManufacturerID; |
| 789 |
} |
| 790 |
|
| 791 |
static NSSUTF8 * |
| 792 |
pem_mdInstance_GetLibraryDescription |
| 793 |
( |
| 794 |
NSSCKMDInstance * mdInstance, |
| 795 |
NSSCKFWInstance * fwInstance, |
| 796 |
CK_RV * pError |
| 797 |
) |
| 798 |
{ |
| 799 |
return (NSSUTF8 *) pem_LibraryDescription; |
| 800 |
} |
| 801 |
|
| 802 |
static CK_VERSION |
| 803 |
pem_mdInstance_GetLibraryVersion |
| 804 |
( |
| 805 |
NSSCKMDInstance * mdInstance, |
| 806 |
NSSCKFWInstance * fwInstance |
| 807 |
) |
| 808 |
{ |
| 809 |
return pem_LibraryVersion; |
| 810 |
} |
| 811 |
|
| 812 |
static CK_RV |
| 813 |
pem_mdInstance_GetSlots |
| 814 |
( |
| 815 |
NSSCKMDInstance * mdInstance, |
| 816 |
NSSCKFWInstance * fwInstance, |
| 817 |
NSSCKMDSlot * slots[] |
| 818 |
) |
| 819 |
{ |
| 820 |
int i; |
| 821 |
CK_RV pError; |
| 822 |
|
| 823 |
for (i = 0; i < NUM_SLOTS; i++) { |
| 824 |
slots[i] = (NSSCKMDSlot *) pem_NewSlot(fwInstance, &pError); |
| 825 |
if (pError != CKR_OK) |
| 826 |
return pError; |
| 827 |
} |
| 828 |
return CKR_OK; |
| 829 |
} |
| 830 |
|
| 831 |
CK_BBOOL |
| 832 |
pem_mdInstance_ModuleHandlesSessionObjects |
| 833 |
( |
| 834 |
NSSCKMDInstance * mdInstance, |
| 835 |
NSSCKFWInstance * fwInstance |
| 836 |
) |
| 837 |
{ |
| 838 |
return CK_TRUE; |
| 839 |
} |
| 840 |
|
| 841 |
static NSSCKMDSlot * |
| 842 |
pem_mdInstance_WaitForSlotEvent( |
| 843 |
NSSCKMDInstance * mdInstance, |
| 844 |
NSSCKFWInstance * fwInstance, |
| 845 |
CK_BBOOL block, |
| 846 |
CK_RV * pError) |
| 847 |
{ |
| 848 |
return lastEventSlot; |
| 849 |
} |
| 850 |
|
| 851 |
NSS_IMPLEMENT_DATA const NSSCKMDInstance |
| 852 |
pem_mdInstance = { |
| 853 |
(void *) NULL, /* etc */ |
| 854 |
pem_Initialize, /* Initialize */ |
| 855 |
pem_Finalize, /* Finalize */ |
| 856 |
pem_mdInstance_GetNSlots, |
| 857 |
pem_mdInstance_GetCryptokiVersion, |
| 858 |
pem_mdInstance_GetManufacturerID, |
| 859 |
pem_mdInstance_GetLibraryDescription, |
| 860 |
pem_mdInstance_GetLibraryVersion, |
| 861 |
pem_mdInstance_ModuleHandlesSessionObjects, |
| 862 |
pem_mdInstance_GetSlots, |
| 863 |
pem_mdInstance_WaitForSlotEvent, |
| 864 |
(void *) NULL /* null terminator */ |
| 865 |
}; |