pk11cert.c

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 2,345 行 · 第 1/5 页

C
2,345
字号
    NSSCMSRecipient *rl;    int rlIndex;    certHandle = pk11_AllFindCertObjectByRecipientNew(recipientlist, wincx, &rlIndex);    if (certHandle == CK_INVALID_KEY) {	return NULL;    }    rl = recipientlist[rlIndex];    /* at this point, rl->slot is set */    /* authenticate to the token */    if (PK11_Authenticate(rl->slot, PR_TRUE, wincx) != SECSuccess) {	PK11_FreeSlot(rl->slot);	rl->slot = NULL;	return -1;    }    /* try to get a private key handle for the cert we found */    keyHandle = PK11_MatchItem(rl->slot, certHandle, CKO_PRIVATE_KEY);    if (keyHandle == CK_INVALID_KEY) { 	PK11_FreeSlot(rl->slot);	rl->slot = NULL;	return -1;    }    /* make a private key out of the handle */    rl->privkey = PK11_MakePrivKey(rl->slot, nullKey, PR_TRUE, keyHandle, wincx);    if (rl->privkey == NULL) {	PK11_FreeSlot(rl->slot);	rl->slot = NULL;	return -1;    }    /* make a cert from the cert handle */    rl->cert = PK11_MakeCertFromHandle(rl->slot, certHandle, NULL);    if (rl->cert == NULL) {	PK11_FreeSlot(rl->slot);	SECKEY_DestroyPrivateKey(rl->privkey);	rl->slot = NULL;	rl->privkey = NULL;	return NULL;    }    return rlIndex;}CERTCertificate *PK11_FindCertByIssuerAndSN(PK11SlotInfo **slotPtr, CERTIssuerAndSN *issuerSN,							 void *wincx){    CK_OBJECT_HANDLE certHandle;    CERTCertificate *cert = NULL;    CK_ATTRIBUTE searchTemplate[] = {	{ CKA_ISSUER, NULL, 0 },	{ CKA_SERIAL_NUMBER, NULL, 0}    };    int count = sizeof(searchTemplate)/sizeof(CK_ATTRIBUTE);    CK_ATTRIBUTE *attrs = searchTemplate;    PK11_SETATTRS(attrs, CKA_ISSUER, issuerSN->derIssuer.data, 					issuerSN->derIssuer.len); attrs++;    PK11_SETATTRS(attrs, CKA_SERIAL_NUMBER, issuerSN->serialNumber.data, 						issuerSN->serialNumber.len);    certHandle = pk11_FindCertObjectByTemplate					(slotPtr,searchTemplate,count,wincx);    if (certHandle == CK_INVALID_KEY) {	return NULL;    }    cert = PK11_MakeCertFromHandle(*slotPtr,certHandle,NULL);    if (cert == NULL) {	PK11_FreeSlot(*slotPtr);	return NULL;    }    return cert;}CK_OBJECT_HANDLEPK11_FindObjectForCert(CERTCertificate *cert, void *wincx, PK11SlotInfo **pSlot){    CK_OBJECT_HANDLE certHandle;    CK_ATTRIBUTE searchTemplate	= { CKA_VALUE, NULL, 0 };        PK11_SETATTRS(&searchTemplate, CKA_VALUE, cert->derCert.data,		  cert->derCert.len);    if (cert->slot) {	certHandle = pk11_getcerthandle(cert->slot,cert,&searchTemplate,1);	if (certHandle != CK_INVALID_KEY) {	    *pSlot = PK11_ReferenceSlot(cert->slot);	    return certHandle;	}    }    certHandle = pk11_FindCertObjectByTemplate(pSlot,&searchTemplate,1,wincx);    if (certHandle != CK_INVALID_KEY) {	if (cert->slot == NULL) {	    cert->slot = PK11_ReferenceSlot(*pSlot);	    cert->pkcs11ID = certHandle;	    cert->ownSlot = PR_FALSE;	}    }    return(certHandle);}SECKEYPrivateKey *PK11_FindKeyByAnyCert(CERTCertificate *cert, void *wincx){    CK_OBJECT_HANDLE certHandle;    CK_OBJECT_HANDLE keyHandle;    PK11SlotInfo *slot = NULL;    SECKEYPrivateKey *privKey;    SECStatus rv;    certHandle = PK11_FindObjectForCert(cert, wincx, &slot);    if (certHandle == CK_INVALID_KEY) {	 return NULL;    }    rv = PK11_Authenticate(slot, PR_TRUE, wincx);    if (rv != SECSuccess) {	PK11_FreeSlot(slot);	return NULL;    }    keyHandle = PK11_MatchItem(slot,certHandle,CKO_PRIVATE_KEY);    if (keyHandle == CK_INVALID_KEY) { 	PK11_FreeSlot(slot);	return NULL;    }    privKey =  PK11_MakePrivKey(slot, nullKey, PR_TRUE, keyHandle, wincx);    PK11_FreeSlot(slot);    return privKey;}CK_OBJECT_HANDLEpk11_FindPubKeyByAnyCert(CERTCertificate *cert, PK11SlotInfo **slot, void *wincx){    CK_OBJECT_HANDLE certHandle;    CK_OBJECT_HANDLE keyHandle;    certHandle = PK11_FindObjectForCert(cert, wincx, slot);    if (certHandle == CK_INVALID_KEY) {	 return CK_INVALID_KEY;    }    keyHandle = PK11_MatchItem(*slot,certHandle,CKO_PUBLIC_KEY);    if (keyHandle == CK_INVALID_KEY) { 	PK11_FreeSlot(*slot);	return CK_INVALID_KEY;    }    return keyHandle;}SECKEYPrivateKey *PK11_FindKeyByKeyID(PK11SlotInfo *slot, SECItem *keyID, void *wincx){    CK_OBJECT_HANDLE keyHandle;    SECKEYPrivateKey *privKey;    keyHandle = pk11_FindPrivateKeyFromCertID(slot, keyID);    if (keyHandle == CK_INVALID_KEY) { 	return NULL;    }    privKey =  PK11_MakePrivKey(slot, nullKey, PR_TRUE, keyHandle, wincx);    return privKey;}/* * find the number of certs in the slot with the same subject name */intPK11_NumberCertsForCertSubject(CERTCertificate *cert){    CK_OBJECT_CLASS certClass = CKO_CERTIFICATE;    CK_ATTRIBUTE theTemplate[] = {	{ CKA_CLASS, NULL, 0 },	{ CKA_SUBJECT, NULL, 0 },    };    CK_ATTRIBUTE *attr = theTemplate;   int templateSize = sizeof(theTemplate)/sizeof(theTemplate[0]);    PK11_SETATTRS(attr,CKA_CLASS, &certClass, sizeof(certClass)); attr++;    PK11_SETATTRS(attr,CKA_SUBJECT,cert->derSubject.data,cert->derSubject.len);    if ((cert->slot == NULL) || (cert->slot->isInternal)) {	return 0;    }    return PK11_NumberObjectsFor(cert->slot,theTemplate,templateSize);}/* *  Walk all the certs with the same subject */SECStatusPK11_TraverseCertsForSubject(CERTCertificate *cert,        SECStatus(* callback)(CERTCertificate*, void *), void *arg){    if(!cert) {	return SECFailure;    }    return PK11_TraverseCertsForSubjectInSlot(cert, cert->slot, callback, arg);}SECStatusPK11_TraverseCertsForSubjectInSlot(CERTCertificate *cert, PK11SlotInfo *slot,	SECStatus(* callback)(CERTCertificate*, void *), void *arg){    pk11DoCertCallback caller;    pk11TraverseSlotCert callarg;    CK_OBJECT_CLASS certClass = CKO_CERTIFICATE;    CK_ATTRIBUTE theTemplate[] = {	{ CKA_CLASS, NULL, 0 },	{ CKA_SUBJECT, NULL, 0 },    };    CK_ATTRIBUTE *attr = theTemplate;   int templateSize = sizeof(theTemplate)/sizeof(theTemplate[0]);    PK11_SETATTRS(attr,CKA_CLASS, &certClass, sizeof(certClass)); attr++;    PK11_SETATTRS(attr,CKA_SUBJECT,cert->derSubject.data,cert->derSubject.len);    if ((slot == NULL) || (slot->isInternal)) {	return SECSuccess;    }    caller.noslotcallback = callback;    caller.callback = NULL;    caller.callbackArg = arg;    callarg.callback = pk11_DoCerts;    callarg.callbackArg = (void *) & caller;    callarg.findTemplate = theTemplate;    callarg.templateCount = templateSize;        return PK11_TraverseSlot(slot, &callarg);}SECStatusPK11_TraverseCertsForNicknameInSlot(SECItem *nickname, PK11SlotInfo *slot,	SECStatus(* callback)(CERTCertificate*, void *), void *arg){    pk11DoCertCallback caller;    pk11TraverseSlotCert callarg;    CK_OBJECT_CLASS certClass = CKO_CERTIFICATE;    CK_ATTRIBUTE theTemplate[] = {	{ CKA_CLASS, NULL, 0 },	{ CKA_LABEL, NULL, 0 },    };    CK_ATTRIBUTE *attr = theTemplate;    int templateSize = sizeof(theTemplate)/sizeof(theTemplate[0]);    if(!nickname) {	return SECSuccess;    }    PK11_SETATTRS(attr,CKA_CLASS, &certClass, sizeof(certClass)); attr++;    PK11_SETATTRS(attr,CKA_LABEL,nickname->data,nickname->len);    if ((slot == NULL) || (slot->isInternal)) {	return SECSuccess;    }    caller.noslotcallback = callback;    caller.callback = NULL;    caller.callbackArg = arg;    callarg.callback = pk11_DoCerts;    callarg.callbackArg = (void *) & caller;    callarg.findTemplate = theTemplate;    callarg.templateCount = templateSize;    return PK11_TraverseSlot(slot, &callarg);}SECStatusPK11_TraverseCertsInSlot(PK11SlotInfo *slot,	SECStatus(* callback)(CERTCertificate*, void *), void *arg){    pk11DoCertCallback caller;    pk11TraverseSlotCert callarg;    CK_OBJECT_CLASS certClass = CKO_CERTIFICATE;    CK_ATTRIBUTE theTemplate[] = {	{ CKA_CLASS, NULL, 0 },    };    CK_ATTRIBUTE *attr = theTemplate;    int templateSize = sizeof(theTemplate)/sizeof(theTemplate[0]);    PK11_SETATTRS(attr,CKA_CLASS, &certClass, sizeof(certClass)); attr++;    if (slot == NULL) {	return SECSuccess;    }    caller.noslotcallback = callback;    caller.callback = NULL;    caller.callbackArg = arg;    callarg.callback = pk11_DoCerts;    callarg.callbackArg = (void *) & caller;    callarg.findTemplate = theTemplate;    callarg.templateCount = templateSize;    return PK11_TraverseSlot(slot, &callarg);}/* * return the certificate associated with a derCert  */CERTCertificate *PK11_FindCertFromDERCert(PK11SlotInfo *slot, CERTCertificate *cert,								 void *wincx){    CK_OBJECT_CLASS certClass = CKO_CERTIFICATE;    CK_ATTRIBUTE theTemplate[] = {	{ CKA_VALUE, NULL, 0 },	{ CKA_CLASS, NULL, 0 }    };    /* if you change the array, change the variable below as well */    int tsize = sizeof(theTemplate)/sizeof(theTemplate[0]);    CK_OBJECT_HANDLE certh;    CK_ATTRIBUTE *attrs = theTemplate;    SECStatus rv;    PK11_SETATTRS(attrs, CKA_VALUE, cert->derCert.data, 						cert->derCert.len); attrs++;    PK11_SETATTRS(attrs, CKA_CLASS, &certClass, sizeof(certClass));    /*     * issue the find     */    if ( !PK11_IsFriendly(slot)) {	rv = PK11_Authenticate(slot, PR_TRUE, wincx);	if (rv != SECSuccess) return NULL;    }    certh = pk11_getcerthandle(slot,cert,theTemplate,tsize);    if (certh == CK_INVALID_KEY) {	return NULL;    }    return PK11_MakeCertFromHandle(slot, certh, NULL);} /* * return the certificate associated with a derCert  */CERTCertificate *PK11_FindCertFromDERSubjectAndNickname(PK11SlotInfo *slot, 					CERTCertificate *cert, 					char *nickname, void *wincx){    CK_OBJECT_CLASS certClass = CKO_CERTIFICATE;    CK_ATTRIBUTE theTemplate[] = {	{ CKA_SUBJECT, NULL, 0 },	{ CKA_LABEL, NULL, 0 },	{ CKA_CLASS, NULL, 0 }    };    /* if you change the array, change the variable below as well */    int tsize = sizeof(theTemplate)/sizeof(theTemplate[0]);    CK_OBJECT_HANDLE certh;    CK_ATTRIBUTE *attrs = theTemplate;    SECStatus rv;    PK11_SETATTRS(attrs, CKA_SUBJECT, cert->derSubject.data, 						cert->derSubject.len); attrs++;    PK11_SETATTRS(attrs, CKA_LABEL, nickname, PORT_Strlen(nickname));    PK11_SETATTRS(attrs, CKA_CLASS, &certClass, sizeof(certClass));    /*     * issue the find     */    if ( !PK11_IsFriendly(slot)) {	rv = PK11_Authenticate(slot, PR_TRUE, wincx);	if (rv != SECSuccess) return NULL;    }    certh = pk11_getcerthandle(slot,cert,theTemplate,tsize);    if (certh == CK_INVALID_KEY) {	return NULL;    }    return PK11_MakeCertFromHandle(slot, certh, NULL);}/* * import a cert for a private key we have already generated. Set the label * on both to be the nickname. */static CK_OBJECT_HANDLE pk11_findKeyObjectByDERCert(PK11SlotInfo *slot, CERTCertificate *cert, 								void *wincx){    SECItem *keyID;    CK_OBJECT_HANDLE key;    SECStatus rv;    if((slot == NULL) || (cert == NULL)) {	return CK_INVALID_KEY;    }    keyID = pk11_mkcertKeyID(cert);    if(keyID == NULL) {	return CK_INVALID_KEY;    }    key = CK_INVALID_KEY;    rv = PK11_Authenticate(slot, PR_TRUE, wincx);    if (rv != SECSuccess) goto loser;    key = pk11_FindPrivateKeyFromCertID(slot, keyID);loser:    SECITEM_ZfreeItem(keyID, PR_TRUE);    return key;}SECKEYPrivateKey *PK11_FindKeyByDERCert(PK11SlotInfo *slot, CERTCertificate *cert, 								void *wincx){    CK_OBJECT_HANDLE keyHandle;    if((slot == NULL) || (cert == NULL)) {	return NULL;    }    keyHandle = pk11_findKeyObjectByDERCert(slot, cert, wincx);    if (keyHandle == CK_INVALID_KEY) {	return NULL;    }    return PK11_MakePrivKey(slot,nullKey,PR_TRUE,keyHandle,wincx);}SECStatusPK11_ImportCertForKeyToSlot(PK11SlotInfo *slot, CERTCertificate *cert, 						char *nickname, 						PRBool addCertUsage,void *wincx){    CK_OBJECT_HANDLE keyHandle;    if((slot == NULL) || (cert == NULL) || (nickname == NULL)) {	return SECFailure;    }    keyHandle = pk11_findKeyObjectByDERCert(slot, cert, wincx);    if (keyHandle == CK_INVALID_KEY) {	return SECFailure;    }    return PK11_ImportCert(slot, cert, keyHandle, nickname, addCertUsage);}   /* remove when the real version comes out */#define SEC_OID_MISSI_KEA 300  /* until we have v3 stuff merged */PRBoolKEAPQGCompare(CERTCertificate *server,CERTCertificate *cert) {    if ( SECKEY_KEAParamCompare(server,cert) == SECEqual ) {        return PR_TRUE;    } else {	return PR_FALSE;    }}PRBoolPK11_FortezzaHasKEA(CERTCertificate *cert) {   /* look at the subject and see if it is a KEA for MISSI key */   SECOidData *oid;   if ((cert->trust == NULL) ||       ((cert->trust->sslFlags & CERTDB

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?