cmssigdata.c

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

C
887
字号
/*  * NSS_CMSSignedData_GetCertificateList - retrieve the SignedData's certificate list */SECItem **NSS_CMSSignedData_GetCertificateList(NSSCMSSignedData *sigd){    return sigd->rawCerts;}SECStatusNSS_CMSSignedData_ImportCerts(NSSCMSSignedData *sigd, CERTCertDBHandle *certdb,				SECCertUsage certusage, PRBool keepcerts){    int certcount;    SECStatus rv;    int i;    certcount = NSS_CMSArray_Count((void **)sigd->rawCerts);    rv = CERT_ImportCerts(certdb, certusage, certcount, sigd->rawCerts, NULL,			  keepcerts, PR_FALSE, NULL);    /* XXX CRL handling */    if (sigd->signerInfos != NULL) {	/* fill in all signerinfo's certs */	for (i = 0; sigd->signerInfos[i] != NULL; i++)	    (void)NSS_CMSSignerInfo_GetSigningCertificate(sigd->signerInfos[i], certdb);    }    return rv;}/* * XXX the digests need to be passed in BETWEEN the decoding and the verification in case *     of external signatures! *//* * NSS_CMSSignedData_VerifySignerInfo - check the signatures. * * The digests were either calculated during decoding (and are stored in the * signedData itself) or set after decoding using NSS_CMSSignedData_SetDigests. * * The verification checks if the signing cert is valid and has a trusted chain * for the purpose specified by "certusage". */SECStatusNSS_CMSSignedData_VerifySignerInfo(NSSCMSSignedData *sigd, int i, 			    CERTCertDBHandle *certdb, SECCertUsage certusage){    NSSCMSSignerInfo *signerinfo;    NSSCMSContentInfo *cinfo;    SECOidData *algiddata;    SECItem *contentType, *digest;    cinfo = &(sigd->contentInfo);    signerinfo = sigd->signerInfos[i];    /* verify certificate */    if (NSS_CMSSignerInfo_VerifyCertificate(signerinfo, certdb, certusage) != SECSuccess)	return SECFailure;		/* error is set by NSS_CMSSignerInfo_VerifyCertificate */    /* find digest and contentType for signerinfo */    algiddata = NSS_CMSSignerInfo_GetDigestAlg(signerinfo);    digest = NSS_CMSSignedData_GetDigestByAlgTag(sigd, algiddata->offset);    contentType = NSS_CMSContentInfo_GetContentTypeOID(cinfo);    /* now verify signature */    return NSS_CMSSignerInfo_Verify(signerinfo, digest, contentType);}/* * NSS_CMSSignedData_VerifyCertsOnly - verify the certs in a certs-only message */SECStatusNSS_CMSSignedData_VerifyCertsOnly(NSSCMSSignedData *sigd,                                   CERTCertDBHandle *certdb,                                   SECCertUsage usage){    CERTCertificate *cert;    SECStatus rv = SECSuccess;    int i;    int count;    if (!sigd || !certdb || !sigd->rawCerts) {	PORT_SetError(SEC_ERROR_INVALID_ARGS);	return SECFailure;    }    count = NSS_CMSArray_Count((void**)sigd->rawCerts);    for (i=0; i < count; i++) {	if (sigd->certs && sigd->certs[i]) {	    cert = sigd->certs[i];	} else {	    cert = CERT_FindCertByDERCert(certdb, sigd->rawCerts[i]);	    if (!cert) {		rv = SECFailure;		break;	    }	}	rv |= CERT_VerifyCert(certdb, cert, PR_TRUE, usage, PR_Now(),                               NULL, NULL);    }    return rv;}/* * NSS_CMSSignedData_HasDigests - see if we have digests in place */PRBoolNSS_CMSSignedData_HasDigests(NSSCMSSignedData *sigd){    return (sigd->digests != NULL);}SECStatusNSS_CMSSignedData_AddCertList(NSSCMSSignedData *sigd, CERTCertificateList *certlist){    SECStatus rv;    PORT_Assert(certlist != NULL);    if (certlist == NULL)	return SECFailure;    /* XXX memory?? a certlist has an arena of its own and is not refcounted!?!? */    rv = NSS_CMSArray_Add(sigd->cmsg->poolp, (void ***)&(sigd->certLists), (void *)certlist);    return rv;}/* * NSS_CMSSignedData_AddCertChain - add cert and its entire chain to the set of certs  */SECStatusNSS_CMSSignedData_AddCertChain(NSSCMSSignedData *sigd, CERTCertificate *cert){    CERTCertificateList *certlist;    SECCertUsage usage;    SECStatus rv;    usage = certUsageEmailSigner;    /* do not include root */    certlist = CERT_CertChainFromCert(cert, usage, PR_FALSE);    if (certlist == NULL)	return SECFailure;    rv = NSS_CMSSignedData_AddCertList(sigd, certlist);    return rv;}SECStatusNSS_CMSSignedData_AddCertificate(NSSCMSSignedData *sigd, CERTCertificate *cert){    CERTCertificate *c;    SECStatus rv;    PORT_Assert(cert != NULL);    if (cert == NULL)	return SECFailure;    c = CERT_DupCertificate(cert);    rv = NSS_CMSArray_Add(sigd->cmsg->poolp, (void ***)&(sigd->certs), (void *)c);    return rv;}PRBoolNSS_CMSSignedData_ContainsCertsOrCrls(NSSCMSSignedData *sigd){    if (sigd->rawCerts != NULL && sigd->rawCerts[0] != NULL)	return PR_TRUE;    else if (sigd->crls != NULL && sigd->crls[0] != NULL)	return PR_TRUE;    else	return PR_FALSE;}SECStatusNSS_CMSSignedData_AddSignerInfo(NSSCMSSignedData *sigd,				NSSCMSSignerInfo *signerinfo){    void *mark;    SECStatus rv;    SECOidTag digestalgtag;    PLArenaPool *poolp;    poolp = sigd->cmsg->poolp;    mark = PORT_ArenaMark(poolp);    /* add signerinfo */    rv = NSS_CMSArray_Add(poolp, (void ***)&(sigd->signerInfos), (void *)signerinfo);    if (rv != SECSuccess)	goto loser;    /*     * add empty digest     * Empty because we don't have it yet. Either it gets created during encoding     * (if the data is present) or has to be set externally.     * XXX maybe pass it in optionally?     */    digestalgtag = NSS_CMSSignerInfo_GetDigestAlgTag(signerinfo);    rv = NSS_CMSSignedData_SetDigestValue(sigd, digestalgtag, NULL);    if (rv != SECSuccess)	goto loser;    /*     * The last thing to get consistency would be adding the digest.     */    PORT_ArenaUnmark(poolp, mark);    return SECSuccess;loser:    PORT_ArenaRelease (poolp, mark);    return SECFailure;}SECItem *NSS_CMSSignedData_GetDigestByAlgTag(NSSCMSSignedData *sigd, SECOidTag algtag){    int idx;    idx = NSS_CMSAlgArray_GetIndexByAlgTag(sigd->digestAlgorithms, algtag);    return sigd->digests[idx];}/* * NSS_CMSSignedData_SetDigests - set a signedData's digests member * * "digestalgs" - array of digest algorithm IDs * "digests"    - array of digests corresponding to the digest algorithms */SECStatusNSS_CMSSignedData_SetDigests(NSSCMSSignedData *sigd,				SECAlgorithmID **digestalgs,				SECItem **digests){    int cnt, i, idx;    if (sigd->digestAlgorithms == NULL) {	PORT_SetError(SEC_ERROR_INVALID_ARGS);	return SECFailure;    }    /* we assume that the digests array is just not there yet */    PORT_Assert(sigd->digests == NULL);    if (sigd->digests != NULL) {	PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);	return SECFailure;    }    /* now allocate one (same size as digestAlgorithms) */    cnt = NSS_CMSArray_Count((void **)sigd->digestAlgorithms);    sigd->digests = PORT_ArenaZAlloc(sigd->cmsg->poolp, (cnt + 1) * sizeof(SECItem *));    if (sigd->digests == NULL) {	PORT_SetError(SEC_ERROR_NO_MEMORY);	return SECFailure;    }    for (i = 0; sigd->digestAlgorithms[i] != NULL; i++) {	/* try to find the sigd's i'th digest algorithm in the array we passed in */	idx = NSS_CMSAlgArray_GetIndexByAlgID(digestalgs, sigd->digestAlgorithms[i]);	if (idx < 0) {	    PORT_SetError(SEC_ERROR_DIGEST_NOT_FOUND);	    return SECFailure;	}	/* found it - now set it */	if ((sigd->digests[i] = SECITEM_AllocItem(sigd->cmsg->poolp, NULL, 0)) == NULL ||	    SECITEM_CopyItem(sigd->cmsg->poolp, sigd->digests[i], digests[idx]) != SECSuccess)	{	    PORT_SetError(SEC_ERROR_NO_MEMORY);	    return SECFailure;	}    }    return SECSuccess;}SECStatusNSS_CMSSignedData_SetDigestValue(NSSCMSSignedData *sigd,				SECOidTag digestalgtag,				SECItem *digestdata){    SECItem *digest = NULL;    PLArenaPool *poolp;    void *mark;    int n;    poolp = sigd->cmsg->poolp;    mark = PORT_ArenaMark(poolp);    if (digestdata) {	/* copy digestdata item to arena (in case we have it and are not only making room) */	if (SECITEM_CopyItem(poolp, digest, digestdata) != SECSuccess)	    goto loser;    }    n = -1;    if (sigd->digestAlgorithms != NULL)	n = NSS_CMSAlgArray_GetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);    /* if not found, add a digest */    if (n < 0) {	if (NSS_CMSSignedData_AddDigest(poolp, sigd, digestalgtag, digest) != SECSuccess)	    goto loser;    } else {	/* replace NULL pointer with digest item (and leak previous value) */	sigd->digests[n] = digest;    }    PORT_ArenaUnmark(poolp, mark);    return SECSuccess;loser:    PORT_ArenaRelease(poolp, mark);    return SECFailure;}SECStatusNSS_CMSSignedData_AddDigest(PRArenaPool *poolp,				NSSCMSSignedData *sigd,				SECOidTag digestalgtag,				SECItem *digest){    SECAlgorithmID *digestalg;    void *mark;    mark = PORT_ArenaMark(poolp);    digestalg = PORT_ArenaZAlloc(poolp, sizeof(SECAlgorithmID));    if (digestalg == NULL)	goto loser;    if (SECOID_SetAlgorithmID (poolp, digestalg, digestalgtag, NULL) != SECSuccess) /* no params */	goto loser;    if (NSS_CMSArray_Add(poolp, (void ***)&(sigd->digestAlgorithms), (void *)digestalg) != SECSuccess ||	/* even if digest is NULL, add dummy to have same-size array */	NSS_CMSArray_Add(poolp, (void ***)&(sigd->digests), (void *)digest) != SECSuccess)    {	goto loser;    }    PORT_ArenaUnmark(poolp, mark);    return SECSuccess;loser:    PORT_ArenaRelease(poolp, mark);    return SECFailure;}SECItem *NSS_CMSSignedData_GetDigestValue(NSSCMSSignedData *sigd, SECOidTag digestalgtag){    int n;    if (sigd->digestAlgorithms == NULL)	return NULL;    n = NSS_CMSAlgArray_GetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);    return (n < 0) ? NULL : sigd->digests[n];}/* ============================================================================= * Misc. utility functions *//* * NSS_CMSSignedData_CreateCertsOnly - create a certs-only SignedData. * * cert          - base certificates that will be included * include_chain - if true, include the complete cert chain for cert * * More certs and chains can be added via AddCertificate and AddCertChain. * * An error results in a return value of NULL and an error set. * * XXXX CRLs */NSSCMSSignedData *NSS_CMSSignedData_CreateCertsOnly(NSSCMSMessage *cmsg, CERTCertificate *cert, PRBool include_chain){    NSSCMSSignedData *sigd;    void *mark;    PLArenaPool *poolp;    SECStatus rv;    poolp = cmsg->poolp;    mark = PORT_ArenaMark(poolp);    sigd = NSS_CMSSignedData_Create(cmsg);    if (sigd == NULL)	goto loser;    /* no signerinfos, thus no digestAlgorithms */    /* but certs */    if (include_chain) {	rv = NSS_CMSSignedData_AddCertChain(sigd, cert);    } else {	rv = NSS_CMSSignedData_AddCertificate(sigd, cert);    }    if (rv != SECSuccess)	goto loser;    /* RFC2630 5.2 sez:     * In the degenerate case where there are no signers, the     * EncapsulatedContentInfo value being "signed" is irrelevant.  In this     * case, the content type within the EncapsulatedContentInfo value being     * "signed" should be id-data (as defined in section 4), and the content     * field of the EncapsulatedContentInfo value should be omitted.     */    rv = NSS_CMSContentInfo_SetContent_Data(cmsg, &(sigd->contentInfo), NULL, PR_TRUE);    if (rv != SECSuccess)	goto loser;    PORT_ArenaUnmark(poolp, mark);    return sigd;loser:    if (sigd)	NSS_CMSSignedData_Destroy(sigd);    PORT_ArenaRelease(poolp, mark);    return NULL;}/* TODO: * NSS_CMSSignerInfo_GetReceiptRequest() * NSS_CMSSignedData_HasReceiptRequest() * easy way to iterate over signers */

⌨️ 快捷键说明

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