cmspubkey.c

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

C
532
字号
/* * 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. *//* * CMS public key crypto * * $Id: cmspubkey.c,v 1.2 2000/06/13 21:56:30 chrisk%netscape.com Exp $ */#include "cmslocal.h"#include "cert.h"#include "key.h"#include "secasn1.h"#include "secitem.h"#include "secoid.h"#include "pk11func.h"#include "secerr.h"/* ====== RSA ======================================================================= *//* * NSS_CMSUtil_EncryptSymKey_RSA - wrap a symmetric key with RSA * * this function takes a symmetric key and encrypts it using an RSA public key * according to PKCS#1 and RFC2633 (S/MIME) */SECStatusNSS_CMSUtil_EncryptSymKey_RSA(PLArenaPool *poolp, CERTCertificate *cert, PK11SymKey *bulkkey,			SECItem *encKey){    SECOidTag certalgtag;	/* the certificate's encryption algorithm */    SECOidTag encalgtag;	/* the algorithm used for key exchange/agreement */    SECStatus rv;    SECKEYPublicKey *publickey;    int data_len;    void *mark;    /* sanity check */    certalgtag = SECOID_GetAlgorithmTag(&(cert->subjectPublicKeyInfo.algorithm));    PORT_Assert(certalgtag == SEC_OID_PKCS1_RSA_ENCRYPTION);    encalgtag = SEC_OID_PKCS1_RSA_ENCRYPTION;    publickey = CERT_ExtractPublicKey(cert);    if (publickey == NULL)	goto loser;	    mark = PORT_ArenaMark(poolp);    /* allocate memory for the encrypted key */    data_len = SECKEY_PublicKeyStrength(publickey);	/* block size (assumed to be > keylen) */    encKey->data = (unsigned char*)PORT_ArenaAlloc(poolp, data_len);    encKey->len = data_len;    if (encKey->data == NULL)	goto loser;    /* encrypt the key now */    rv = PK11_PubWrapSymKey(PK11_AlgtagToMechanism(SEC_OID_PKCS1_RSA_ENCRYPTION),				publickey, bulkkey, encKey);    SECKEY_DestroyPublicKey(publickey);    if (rv != SECSuccess)	goto loser;    PORT_ArenaUnmark(poolp, mark);    return SECSuccess;loser:    PORT_ArenaRelease(poolp, mark);    return SECFailure;}/* * NSS_CMSUtil_DecryptSymKey_RSA - unwrap a RSA-wrapped symmetric key * * this function takes an RSA-wrapped symmetric key and unwraps it, returning a symmetric * key handle. Please note that the actual unwrapped key data may not be allowed to leave * a hardware token... */PK11SymKey *NSS_CMSUtil_DecryptSymKey_RSA(SECKEYPrivateKey *privkey, SECItem *encKey, SECOidTag bulkalgtag){    /* that's easy */    return PK11_PubUnwrapSymKey(privkey, encKey, PK11_AlgtagToMechanism(bulkalgtag), CKA_DECRYPT, 0);}/* ====== MISSI (Fortezza) ========================================================== */extern const SEC_ASN1Template NSS_SMIMEKEAParamTemplateAllParams[];SECStatusNSS_CMSUtil_EncryptSymKey_MISSI(PLArenaPool *poolp, CERTCertificate *cert, PK11SymKey *bulkkey,			SECOidTag symalgtag, SECItem *encKey, SECItem **pparams, void *pwfn_arg){    SECOidTag certalgtag;	/* the certificate's encryption algorithm */    SECOidTag encalgtag;	/* the algorithm used for key exchange/agreement */    SECStatus rv = SECFailure;    SECItem *params = NULL;    SECStatus err;    PK11SymKey *tek;    CERTCertificate *ourCert;    SECKEYPublicKey *ourPubKey, *publickey = NULL;    SECKEYPrivateKey *ourPrivKey = NULL;    NSSCMSKEATemplateSelector whichKEA;    NSSCMSSMIMEKEAParameters keaParams;    PLArenaPool *arena;    extern const SEC_ASN1Template *nss_cms_get_kea_template(NSSCMSKEATemplateSelector whichTemplate);    /* Clear keaParams, since cleanup code checks the lengths */    (void) memset(&keaParams, 0, sizeof(keaParams));    certalgtag = SECOID_GetAlgorithmTag(&(cert->subjectPublicKeyInfo.algorithm));    PORT_Assert(certalgtag == SEC_OID_MISSI_KEA_DSS_OLD ||		certalgtag == SEC_OID_MISSI_KEA_DSS ||		certalgtag == SEC_OID_MISSI_KEA);#define SMIME_FORTEZZA_RA_LENGTH 128#define SMIME_FORTEZZA_IV_LENGTH 24#define SMIME_FORTEZZA_MAX_KEY_SIZE 256    /* We really want to show our KEA tag as the key exchange algorithm tag. */    encalgtag = SEC_OID_NETSCAPE_SMIME_KEA;    /* Get the public key of the recipient. */    publickey = CERT_ExtractPublicKey(cert);    if (publickey == NULL) goto loser;    /* Find our own cert, and extract its keys. */    ourCert = PK11_FindBestKEAMatch(cert, pwfn_arg);    if (ourCert == NULL) goto loser;    arena = PORT_NewArena(1024);    if (arena == NULL)	goto loser;    ourPubKey = CERT_ExtractPublicKey(ourCert);    if (ourPubKey == NULL) {	CERT_DestroyCertificate(ourCert);	goto loser;    }    /* While we're here, copy the public key into the outgoing     * KEA parameters. */    SECITEM_CopyItem(arena, &(keaParams.originatorKEAKey), &(ourPubKey->u.fortezza.KEAKey));    SECKEY_DestroyPublicKey(ourPubKey);    ourPubKey = NULL;    /* Extract our private key in order to derive the KEA key. */    ourPrivKey = PK11_FindKeyByAnyCert(ourCert, pwfn_arg);    CERT_DestroyCertificate(ourCert); /* we're done with this */    if (!ourPrivKey)	goto loser;    /* Prepare raItem with 128 bytes (filled with zeros). */    keaParams.originatorRA.data = (unsigned char *)PORT_ArenaAlloc(arena,SMIME_FORTEZZA_RA_LENGTH);    keaParams.originatorRA.len = SMIME_FORTEZZA_RA_LENGTH;    /* Generate the TEK (token exchange key) which we use     * to wrap the bulk encryption key. (keaparams.originatorRA) will be     * filled with a random seed which we need to send to     * the recipient. (user keying material in RFC2630/DSA speak) */    tek = PK11_PubDerive(ourPrivKey, publickey, PR_TRUE,			 &keaParams.originatorRA, NULL,			 CKM_KEA_KEY_DERIVE, CKM_SKIPJACK_WRAP,			 CKA_WRAP, 0,  pwfn_arg);    SECKEY_DestroyPublicKey(publickey);    SECKEY_DestroyPrivateKey(ourPrivKey);    publickey = NULL;    ourPrivKey = NULL;        if (!tek)	goto loser;    /* allocate space for the wrapped key data */    encKey->data = (unsigned char *)PORT_ArenaAlloc(poolp, SMIME_FORTEZZA_MAX_KEY_SIZE);    encKey->len = SMIME_FORTEZZA_MAX_KEY_SIZE;    if (encKey->data == NULL) {	PK11_FreeSymKey(tek);	goto loser;    }    /* Wrap the bulk key. What we do with the resulting data       depends on whether we're using Skipjack to wrap the key. */    switch (PK11_AlgtagToMechanism(symalgtag)) {    case CKM_SKIPJACK_CBC64:    case CKM_SKIPJACK_ECB64:    case CKM_SKIPJACK_OFB64:    case CKM_SKIPJACK_CFB64:    case CKM_SKIPJACK_CFB32:    case CKM_SKIPJACK_CFB16:    case CKM_SKIPJACK_CFB8:	/* SKIPJACK, we use the wrap mechanism because we can do it on the hardware */	err = PK11_WrapSymKey(CKM_SKIPJACK_WRAP, NULL, tek, bulkkey, encKey);	whichKEA = NSSCMSKEAUsesSkipjack;	break;    default:	/* Not SKIPJACK, we encrypt the raw key data */	keaParams.nonSkipjackIV.data = 	  (unsigned char *)PORT_ArenaAlloc(arena, SMIME_FORTEZZA_IV_LENGTH);	keaParams.nonSkipjackIV.len = SMIME_FORTEZZA_IV_LENGTH;	err = PK11_WrapSymKey(CKM_SKIPJACK_CBC64, &keaParams.nonSkipjackIV, tek, bulkkey, encKey);	if (err != SECSuccess)	    goto loser;	if (encKey->len != PK11_GetKeyLength(bulkkey)) {	    /* The size of the encrypted key is not the same as	       that of the original bulk key, presumably due to	       padding. Encode and store the real size of the	       bulk key. */	    if (SEC_ASN1EncodeInteger(arena, &keaParams.bulkKeySize, PK11_GetKeyLength(bulkkey)) == NULL)		err = (SECStatus)PORT_GetError();	    else		/* use full template for encoding */		whichKEA = NSSCMSKEAUsesNonSkipjackWithPaddedEncKey;	}	else	    /* enc key length == bulk key length */	    whichKEA = NSSCMSKEAUsesNonSkipjack; 	break;    }    PK11_FreeSymKey(tek);    if (err != SECSuccess)	goto loser;    /* Encode the KEA parameters into the recipient info. */    params = SEC_ASN1EncodeItem(poolp, NULL, &keaParams, nss_cms_get_kea_template(whichKEA));    if (params == NULL)	goto loser;    /* pass back the algorithm params */    *pparams = params;    rv = SECSuccess;

⌨️ 快捷键说明

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