servget.c

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

C
1,008
字号
/* -*- 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 "cmmf.h"#include "cmmfi.h"#include "secitem.h"#include "keyhi.h"CRMFEncryptedKeyChoiceCRMF_EncryptedKeyGetChoice(CRMFEncryptedKey *inEncrKey){    PORT_Assert(inEncrKey != NULL);    if (inEncrKey == NULL) {        return crmfNoEncryptedKeyChoice;    }    return inEncrKey->encKeyChoice;}CRMFEncryptedValue*CRMF_EncryptedKeyGetEncryptedValue(CRMFEncryptedKey *inEncrKey){    CRMFEncryptedValue *newEncrValue = NULL;    SECStatus           rv;    PORT_Assert(inEncrKey != NULL);    if (inEncrKey == NULL ||	CRMF_EncryptedKeyGetChoice(inEncrKey) != crmfEncryptedValueChoice) {        goto loser;    }    newEncrValue = PORT_ZNew(CRMFEncryptedValue);    if (newEncrValue == NULL) {        goto loser;    }    rv = crmf_copy_encryptedvalue(NULL, &inEncrKey->value.encryptedValue,				  newEncrValue);    if (rv != SECSuccess) {        goto loser;    }    return newEncrValue; loser:    if (newEncrValue != NULL) {        CRMF_DestroyEncryptedValue(newEncrValue);    }    return NULL;}static SECItem*crmf_get_encvalue_bitstring(SECItem *srcItem){    SECItem   *newItem = NULL;    SECStatus rv;        if (srcItem->data == NULL) {        return NULL;    }    newItem = PORT_ZNew(SECItem);    if (newItem == NULL) {        goto loser;    }    rv = crmf_make_bitstring_copy(NULL, newItem, srcItem);    if (rv != SECSuccess) {        goto loser;    }    return newItem; loser:    if (newItem != NULL) {        SECITEM_FreeItem(newItem, PR_TRUE);    }    return NULL;}SECItem*CRMF_EncryptedValueGetEncSymmKey(CRMFEncryptedValue *inEncValue){    if (inEncValue == NULL) {        return NULL;    }    return crmf_get_encvalue_bitstring(&inEncValue->encSymmKey);}SECItem*CRMF_EncryptedValueGetEncValue(CRMFEncryptedValue *inEncrValue){    if (inEncrValue == NULL || inEncrValue->encValue.data == NULL) {        return NULL;    }    return crmf_get_encvalue_bitstring(&inEncrValue->encValue);}static SECAlgorithmID*crmf_get_encvalue_algid(SECAlgorithmID *srcAlg){    SECStatus       rv;    SECAlgorithmID *newAlgID;        if (srcAlg == NULL) {        return NULL;    }    rv = crmf_copy_encryptedvalue_secalg(NULL, srcAlg, &newAlgID);    if (rv != SECSuccess) {        return NULL;    }    return newAlgID;}SECAlgorithmID*CRMF_EncryptedValueGetIntendedAlg(CRMFEncryptedValue *inEncValue){    if (inEncValue == NULL) {        return NULL;    }    return crmf_get_encvalue_algid(inEncValue->intendedAlg);}SECAlgorithmID*CRMF_EncryptedValueGetKeyAlg(CRMFEncryptedValue *inEncValue){    if (inEncValue == NULL) {        return NULL;    }    return crmf_get_encvalue_algid(inEncValue->keyAlg);}SECAlgorithmID*CRMF_EncryptedValueGetSymmAlg(CRMFEncryptedValue *inEncValue){    if (inEncValue == NULL) {        return NULL;    }    return crmf_get_encvalue_algid(inEncValue->symmAlg);}SECItem*CRMF_EncryptedValueGetValueHint(CRMFEncryptedValue *inEncValue){    if (inEncValue == NULL || inEncValue->valueHint.data == NULL) {        return NULL;    }    return SECITEM_DupItem(&inEncValue->valueHint);}SECStatusCRMF_PKIArchiveOptionsGetArchiveRemGenPrivKey(CRMFPKIArchiveOptions *inOpt, 					      PRBool                *destVal){    if (inOpt == NULL || destVal == NULL ||	CRMF_PKIArchiveOptionsGetOptionType(inOpt) != crmfArchiveRemGenPrivKey){        return SECFailure;    }    *destVal = (inOpt->option.archiveRemGenPrivKey.data[0] == hexFalse)                                                                  ? PR_FALSE:                                                                   PR_TRUE;    return SECSuccess;}			     CRMFEncryptedKey*CRMF_PKIArchiveOptionsGetEncryptedPrivKey(CRMFPKIArchiveOptions *inOpts){    CRMFEncryptedKey *newEncrKey = NULL;    SECStatus         rv;    PORT_Assert(inOpts != NULL);    if (inOpts == NULL ||	CRMF_PKIArchiveOptionsGetOptionType(inOpts) != crmfEncryptedPrivateKey){        return NULL;    }    newEncrKey = PORT_ZNew(CRMFEncryptedKey);    if (newEncrKey == NULL) {        goto loser;    }    rv = crmf_copy_encryptedkey(NULL, &inOpts->option.encryptedKey,				newEncrKey);    if (rv != SECSuccess) {        goto loser;    }    return newEncrKey; loser:    if (newEncrKey != NULL) {        CRMF_DestroyEncryptedKey(newEncrKey);    }    return NULL;}SECItem*CRMF_PKIArchiveOptionsGetKeyGenParameters(CRMFPKIArchiveOptions *inOptions){    if (inOptions == NULL ||	CRMF_PKIArchiveOptionsGetOptionType(inOptions) != crmfKeyGenParameters ||	inOptions->option.keyGenParameters.data == NULL) {        return NULL;    }    return SECITEM_DupItem(&inOptions->option.keyGenParameters);}CRMFPKIArchiveOptionsTypeCRMF_PKIArchiveOptionsGetOptionType(CRMFPKIArchiveOptions *inOptions){    PORT_Assert (inOptions != NULL);    if (inOptions == NULL) {        return crmfNoArchiveOptions;    }    return inOptions->archOption;}static SECStatuscrmf_extract_long_from_item(SECItem *intItem, long *destLong){    *destLong = DER_GetInteger(intItem);    return (*destLong == -1) ? SECFailure : SECSuccess;}SECStatusCRMF_POPOPrivGetKeySubseqMess(CRMFPOPOPrivKey       *inKey,			      CRMFSubseqMessOptions *destOpt){    long      value;    SECStatus rv;    PORT_Assert(inKey != NULL);    if (inKey == NULL ||	inKey->messageChoice != crmfSubsequentMessage) {        return SECFailure;    }    rv = crmf_extract_long_from_item(&inKey->message.subsequentMessage,&value);    if (rv != SECSuccess) {        return SECFailure;    }    switch (value) {    case 0:        *destOpt = crmfEncrCert;	break;    case 1:        *destOpt = crmfChallengeResp;	break;    default:        rv = SECFailure;    }    if (rv != SECSuccess) {        return rv;    }    return SECSuccess;}CRMFPOPOPrivKeyChoiceCRMF_POPOPrivKeyGetChoice(CRMFPOPOPrivKey *inPrivKey){    PORT_Assert(inPrivKey != NULL);    if (inPrivKey != NULL) {        return inPrivKey->messageChoice;    }    return crmfNoMessage;}SECStatusCRMF_POPOPrivKeyGetDHMAC(CRMFPOPOPrivKey *inKey, SECItem *destMAC){    PORT_Assert(inKey != NULL);    if (inKey == NULL || inKey->message.dhMAC.data == NULL) {        return SECFailure;    }    return crmf_make_bitstring_copy(NULL, destMAC, &inKey->message.dhMAC);}SECStatusCRMF_POPOPrivKeyGetThisMessage(CRMFPOPOPrivKey  *inKey,			       SECItem          *destString){    PORT_Assert(inKey != NULL);    if (inKey == NULL           ||	inKey->messageChoice != crmfThisMessage) {        return SECFailure;    }    return crmf_make_bitstring_copy(NULL, destString, 				    &inKey->message.thisMessage);}SECAlgorithmID*CRMF_POPOSigningKeyGetAlgID(CRMFPOPOSigningKey *inSignKey){    SECAlgorithmID *newAlgId = NULL;    SECStatus       rv;    PORT_Assert(inSignKey != NULL);    if (inSignKey == NULL) {        return NULL;    }    newAlgId = PORT_ZNew(SECAlgorithmID);    if (newAlgId == NULL) {        goto loser;    }    rv = SECOID_CopyAlgorithmID(NULL, newAlgId, 				inSignKey->algorithmIdentifier);    if (rv != SECSuccess) {        goto loser;    }    return newAlgId; loser:    if (newAlgId != NULL) {        SECOID_DestroyAlgorithmID(newAlgId, PR_TRUE);    }    return NULL;}SECItem*CRMF_POPOSigningKeyGetInput(CRMFPOPOSigningKey *inSignKey){    PORT_Assert(inSignKey != NULL);    if (inSignKey == NULL || inSignKey->derInput.data == NULL) {        return NULL;    }    return SECITEM_DupItem(&inSignKey->derInput);}SECItem*CRMF_POPOSigningKeyGetSignature(CRMFPOPOSigningKey *inSignKey){    SECItem   *newSig = NULL;    SECStatus  rv;    PORT_Assert(inSignKey != NULL);    if (inSignKey == NULL) {        return NULL;    }    newSig = PORT_ZNew(SECItem);    if (newSig == NULL) {        goto loser;    }    rv = crmf_make_bitstring_copy(NULL, newSig, &inSignKey->signature);    if (rv != SECSuccess) {        goto loser;    }    return newSig; loser:    if (newSig != NULL) {        SECITEM_FreeItem(newSig, PR_TRUE);    }    return NULL;}static SECStatus crmf_copy_poposigningkey(PRArenaPool        *poolp, 			 CRMFPOPOSigningKey *inPopoSignKey,			 CRMFPOPOSigningKey *destPopoSignKey){    SECStatus rv;    /* We don't support use of the POPOSigningKeyInput, so we'll only      * store away the DER encoding.     */    if (inPopoSignKey->derInput.data != NULL) {        rv = SECITEM_CopyItem(poolp, &destPopoSignKey->derInput, 			      &inPopoSignKey->derInput);    }    destPopoSignKey->algorithmIdentifier = (poolp == NULL) ?                                          PORT_ZNew(SECAlgorithmID) :                                         PORT_ArenaZNew(poolp, SECAlgorithmID);    if (destPopoSignKey->algorithmIdentifier == NULL) {        goto loser;    }    rv = SECOID_CopyAlgorithmID(poolp, destPopoSignKey->algorithmIdentifier,				inPopoSignKey->algorithmIdentifier);    if (rv != SECSuccess) {        goto loser;    }        rv = crmf_make_bitstring_copy(poolp, &destPopoSignKey->signature, 				  &inPopoSignKey->signature);    if (rv != SECSuccess) {        goto loser;    }    return SECSuccess; loser:    if (destPopoSignKey && poolp == NULL) {        CRMF_DestroyPOPOSigningKey(destPopoSignKey);    }    return SECFailure;}static SECStatuscrmf_copy_popoprivkey(PRArenaPool     *poolp,		      CRMFPOPOPrivKey *srcPrivKey,		      CRMFPOPOPrivKey *destPrivKey){    SECStatus        rv;    destPrivKey->messageChoice = srcPrivKey->messageChoice;    switch (destPrivKey->messageChoice) {    case crmfThisMessage:    case crmfDHMAC:        /* I've got a union, so taking the address of one, will also give	 * me a pointer to the other (eg, message.dhMAC)	 */        rv = crmf_make_bitstring_copy(poolp, &destPrivKey->message.thisMessage,				      &srcPrivKey->message.thisMessage);	break;    case crmfSubsequentMessage:        rv = SECITEM_CopyItem(poolp, &destPrivKey->message.subsequentMessage,			      &srcPrivKey->message.subsequentMessage);	break;    default:        rv = SECFailure;    }    if (rv != SECSuccess) {        if (destPrivKey && poolp == NULL) {	    CRMF_DestroyPOPOPrivKey(destPrivKey);	}	return SECFailure;    }    return SECSuccess;}static CRMFProofOfPossession*crmf_copy_pop(PRArenaPool *poolp, CRMFProofOfPossession *srcPOP){    CRMFProofOfPossession *newPOP;    SECStatus              rv;    /*      * Proof Of Possession structures are always part of the Request     * message, so there will always be an arena for allocating memory.     */    if (poolp == NULL) {        return NULL;    }    newPOP = PORT_ArenaZNew(poolp, CRMFProofOfPossession);    if (newPOP == NULL) {        return NULL;    }    switch (srcPOP->popUsed) {    case crmfRAVerified:        newPOP->popChoice.raVerified.data = NULL;	newPOP->popChoice.raVerified.len  = 0;	break;    case crmfSignature:        rv = crmf_copy_poposigningkey(poolp, &srcPOP->popChoice.signature,				      &newPOP->popChoice.signature);	if (rv != SECSuccess) {	    goto loser;	}	break;    case crmfKeyEncipherment:    case crmfKeyAgreement:        /* We've got a union, so a pointer to one, is a pointer to the	 * other one.	 */        rv = crmf_copy_popoprivkey(poolp, &srcPOP->popChoice.keyEncipherment,				   &newPOP->popChoice.keyEncipherment);	if (rv != SECSuccess) {	    goto loser;	}	break;    default:        goto loser;    }    newPOP->popUsed = srcPOP->popUsed;    return newPOP; loser:    return NULL;}static CRMFCertReqMsg*crmf_copy_cert_req_msg(CRMFCertReqMsg *srcReqMsg){    CRMFCertReqMsg *newReqMsg;    PRArenaPool    *poolp;    poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);

⌨️ 快捷键说明

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