crmfreq.c

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

C
698
字号
/* -*- Mode: C; tab-width: 8 -*-*//* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ *  * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *  * The Original Code is the Netscape security libraries. *  * The Initial Developer of the Original Code is Netscape * Communications Corporation.  Portions created by Netscape are  * Copyright (C) 1994-2000 Netscape Communications Corporation.  All * Rights Reserved. *  * Contributor(s): *  * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable  * instead of those above.  If you wish to allow use of your  * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL.  If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. */#include "crmf.h"#include "crmfi.h"#include "keyhi.h"#include "secder.h"/* * Macro that returns PR_TRUE if the pointer is not NULL. * If the pointer is NULL, then the macro will return PR_FALSE. */#define IS_NOT_NULL(ptr) ((ptr) == NULL) ? PR_FALSE : PR_TRUEconst unsigned char hexTrue  = 0xff;const unsigned char hexFalse = 0x00;SECStatuscrmf_encode_integer(PRArenaPool *poolp, SECItem *dest, long value) {    SECItem *dummy;    dummy = SEC_ASN1EncodeInteger(poolp, dest, value);    PORT_Assert (dummy == dest);    if (dummy == NULL) {        return SECFailure;    }    return SECSuccess;}static SECStatuscrmf_copy_secitem (PRArenaPool *poolp, SECItem *dest, SECItem *src){    return  SECITEM_CopyItem (poolp, dest, src); }PRBoolCRMF_DoesRequestHaveField (CRMFCertRequest       *inCertReq, 			   CRMFCertTemplateField  inField){      PORT_Assert(inCertReq != NULL);    if (inCertReq == NULL) {        return PR_FALSE;    }    switch (inField) {    case crmfVersion:        return inCertReq->certTemplate.version.data != NULL;    case crmfSerialNumber:        return inCertReq->certTemplate.serialNumber.data != NULL;    case crmfSigningAlg:        return inCertReq->certTemplate.signingAlg != NULL;    case crmfIssuer:        return inCertReq->certTemplate.issuer != NULL;    case crmfValidity:        return inCertReq->certTemplate.validity != NULL;    case crmfSubject:        return inCertReq->certTemplate.subject != NULL;    case crmfPublicKey:        return inCertReq->certTemplate.publicKey != NULL;    case crmfIssuerUID:        return inCertReq->certTemplate.issuerUID.data != NULL;    case crmfSubjectUID:        return inCertReq->certTemplate.subjectUID.data != NULL;    case crmfExtension:        return CRMF_CertRequestGetNumberOfExtensions(inCertReq) != 0;    }    return PR_FALSE;}CRMFCertRequest *CRMF_CreateCertRequest (long inRequestID) {    PRArenaPool     *poolp;    CRMFCertRequest *certReq;    SECStatus        rv;        poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);    if (poolp == NULL) {        goto loser;    }        certReq=PORT_ArenaZNew(poolp,CRMFCertRequest);    if (certReq == NULL) {        goto loser;    }    certReq->poolp = poolp;    certReq->requestID = inRequestID;        rv = crmf_encode_integer(poolp, &(certReq->certReqId), inRequestID);    if (rv != SECSuccess) {        goto loser;    }    return certReq; loser:    if (poolp) {        PORT_FreeArena(poolp, PR_FALSE);    }    return NULL;}SECStatusCRMF_DestroyCertRequest(CRMFCertRequest *inCertReq){    PORT_Assert(inCertReq != NULL);    if (inCertReq != NULL) {        if (inCertReq->certTemplate.extensions) {	    PORT_Free(inCertReq->certTemplate.extensions);	}	if (inCertReq->controls) {	    /* Right now we don't support EnveloppedData option,	     * so we won't go through and delete each occurrence of 	     * an EnveloppedData in the control.	     */	    PORT_Free(inCertReq->controls);	}	if (inCertReq->poolp) {	    PORT_FreeArena(inCertReq->poolp, PR_TRUE);	}    }    return SECSuccess;}static SECStatuscrmf_template_add_version(PRArenaPool *poolp, SECItem *dest, long version){    return (crmf_encode_integer(poolp, dest, version));}static SECStatuscrmf_template_add_serialnumber(PRArenaPool *poolp, SECItem *dest, long serial){    return (crmf_encode_integer(poolp, dest, serial));}SECStatuscrmf_template_copy_secalg (PRArenaPool *poolp, SECAlgorithmID **dest, 			   SECAlgorithmID* src){    SECStatus         rv;    void             *mark;    SECAlgorithmID   *mySecAlg;    if (poolp != NULL) {        mark = PORT_ArenaMark(poolp);    }    *dest = mySecAlg = PORT_ArenaZNew(poolp, SECAlgorithmID);    if (mySecAlg == NULL) {        goto loser;    }    rv = SECOID_CopyAlgorithmID(poolp, mySecAlg, src);    if (rv != SECSuccess) {        goto loser;    }    if (poolp != NULL) {        PORT_ArenaUnmark(poolp, mark);    }    return SECSuccess; loser:    *dest = NULL;    if (poolp != NULL) {        PORT_ArenaRelease(poolp, mark);    }    return SECFailure;}SECStatuscrmf_copy_cert_name(PRArenaPool *poolp, CERTName **dest, 		    CERTName *src){    CERTName *newName;    SECStatus rv;    void     *mark;    mark = PORT_ArenaMark(poolp);    *dest = newName = PORT_ArenaZNew(poolp, CERTName);    if (newName == NULL) {        goto loser;    }    rv = CERT_CopyName(poolp, newName, src);    if (rv != SECSuccess) {      goto loser;    }    PORT_ArenaUnmark(poolp, mark);    return SECSuccess; loser:    PORT_ArenaRelease(poolp, mark);    *dest = NULL;    return SECFailure;}static SECStatuscrmf_template_add_issuer (PRArenaPool *poolp, CERTName **dest, 			  CERTName* issuerName){    return crmf_copy_cert_name(poolp, dest, issuerName);}static SECStatuscrmf_encode_utctime(PRArenaPool *poolp, SECItem *destTime, PRTime time){    SECItem   tmpItem;    SECStatus rv;    rv = DER_TimeToUTCTime (&tmpItem, time);    if (rv != SECSuccess) {        return rv;    }    rv = SECITEM_CopyItem(poolp, destTime, &tmpItem);    PORT_Free(tmpItem.data);    return rv;}static SECStatuscrmf_template_add_validity (PRArenaPool *poolp, CRMFOptionalValidity **dest,			    CRMFValidityCreationInfo *info){    SECStatus             rv;    void                 *mark;     CRMFOptionalValidity *myValidity;    /*First off, let's make sure at least one of the two fields is present*/    if (!info  || (!info->notBefore && !info->notAfter)) {        return SECFailure;    }    mark = PORT_ArenaMark (poolp);    *dest = myValidity = PORT_ArenaZNew(poolp, CRMFOptionalValidity);    if (myValidity == NULL) {        goto loser;    }    if (info->notBefore) {        rv = crmf_encode_utctime (poolp, &myValidity->notBefore, 				  *info->notBefore);	if (rv != SECSuccess) {	    goto loser;	}    }    if (info->notAfter) {        rv = crmf_encode_utctime (poolp, &myValidity->notAfter,				  *info->notAfter);	if (rv != SECSuccess) {	    goto loser;	}    }    PORT_ArenaUnmark(poolp, mark);    return SECSuccess; loser:    PORT_ArenaRelease(poolp, mark);    *dest = NULL;    return SECFailure;}static SECStatuscrmf_template_add_subject (PRArenaPool *poolp, CERTName **dest,			   CERTName *subject){    return crmf_copy_cert_name(poolp, dest, subject);}SECStatuscrmf_template_add_public_key(PRArenaPool *poolp, 			     CERTSubjectPublicKeyInfo **dest,			     CERTSubjectPublicKeyInfo  *pubKey){    CERTSubjectPublicKeyInfo *spki;    SECStatus rv;    *dest = spki = (poolp == NULL) ?                              PORT_ZNew(CERTSubjectPublicKeyInfo) :                              PORT_ArenaZNew (poolp, CERTSubjectPublicKeyInfo);    if (spki == NULL) {        goto loser;    }    rv = SECKEY_CopySubjectPublicKeyInfo (poolp, spki, pubKey);    if (rv != SECSuccess) {        goto loser;    }    return SECSuccess; loser:    if (poolp == NULL && spki != NULL) {        SECKEY_DestroySubjectPublicKeyInfo(spki);    }    *dest = NULL;    return SECFailure;}static SECStatuscrmf_copy_bitstring (PRArenaPool *poolp, SECItem *dest, SECItem *src){    SECStatus rv;    int origLenBits, numBytesToCopy;        origLenBits = src->len;    numBytesToCopy = CRMF_BITS_TO_BYTES(origLenBits);    rv = crmf_copy_secitem(poolp, dest, src);    src->len = origLenBits;    dest->len = origLenBits;    return rv;}static SECStatuscrmf_template_add_issuer_uid(PRArenaPool *poolp, SECItem *dest,			     SECItem *issuerUID){    return crmf_copy_bitstring (poolp, dest, issuerUID);}static SECStatuscrmf_template_add_subject_uid(PRArenaPool *poolp, SECItem *dest, 			      SECItem *subjectUID){

⌨️ 快捷键说明

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