blapi_bsf.c

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

C
2,087
字号
	B_DestroyKeyObject(&publicKeyObj);	/*  publicKeyInfo data is shallow copy */	return (status == BE_SIGNATURE) ? SECFailure : SECSuccess;loser:	if (dsaVerifier != NULL_PTR)		B_DestroyAlgorithmObject(&dsaVerifier);	if (publicKeyObj != NULL_PTR)		B_DestroyKeyObject(&publicKeyObj);	return SECFailure;}SECStatus DSA_NewKeyFromSeed(PQGParams *params, unsigned char * seed,                   DSAPrivateKey **privKey){	PRArenaPool *arena;	DSAPrivateKey *privateKey;	/* BSAFE */	B_ALGORITHM_OBJ           dsaKeyGenObj = (B_ALGORITHM_OBJ)NULL_PTR;	B_ALGORITHM_OBJ           randomAlgorithm = NULL_PTR;	A_DSA_PRIVATE_KEY        *privateKeyInfo = (A_DSA_PRIVATE_KEY *)NULL_PTR;	A_DSA_PUBLIC_KEY         *publicKeyInfo = (A_DSA_PUBLIC_KEY *)NULL_PTR;	A_DSA_PARAMS              dsaParamInfo;	B_KEY_OBJ                 publicKeyObj = (B_KEY_OBJ)NULL_PTR;	B_KEY_OBJ                 privateKeyObj = (B_KEY_OBJ)NULL_PTR;	int status;	/*  Allocate space for key structure. */	arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE);	if (arena == NULL) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		goto loser;	}	privateKey = (DSAPrivateKey *)	              PORT_ArenaZAlloc(arena, sizeof(DSAPrivateKey));	if (privateKey == NULL) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		goto loser;	}	privateKey->params.arena = arena;	if ((status = B_CreateAlgorithmObject(&dsaKeyGenObj)) != 0) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		goto loser;	}	if ((status = B_CreateKeyObject(&publicKeyObj)) != 0) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		goto loser;	}	if ((status = B_CreateKeyObject(&privateKeyObj)) != 0) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		goto loser;	}	randomAlgorithm = generateRandomAlgorithm(DSA_SUBPRIME_LEN, seed);	if (randomAlgorithm == NULL_PTR) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		goto loser;	}	ITEMFROMSECITEM(dsaParamInfo.prime,    params->prime);	ITEMFROMSECITEM(dsaParamInfo.subPrime, params->subPrime);	ITEMFROMSECITEM(dsaParamInfo.base,     params->base);	if ((status = B_SetAlgorithmInfo(dsaKeyGenObj, AI_DSAKeyGen, 	                                 (POINTER)&dsaParamInfo)) != 0) {		PORT_SetError(SEC_ERROR_INVALID_ARGS);		goto loser;	}	if ((status = B_GenerateInit(dsaKeyGenObj, dsa_pk_gen_chooser,	                             (A_SURRENDER_CTX *)NULL_PTR)) != 0) {		PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);		goto loser;	}	if ((status = B_GenerateKeypair(dsaKeyGenObj, publicKeyObj, privateKeyObj,	                                randomAlgorithm,	                                (A_SURRENDER_CTX *)NULL_PTR)) != 0) {		PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);		goto loser;	}	if ((status = B_GetKeyInfo((POINTER *)&privateKeyInfo, privateKeyObj,	                           KI_DSAPrivate)) != 0) {		PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);		goto loser;	}	if ((status = B_GetKeyInfo((POINTER *)&publicKeyInfo, publicKeyObj,	                           KI_DSAPublic)) != 0) {		PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);		goto loser;	}	if ((status = dsaConvertKeyInfoToBLKey(privateKeyInfo, publicKeyInfo,	                                       privateKey)) != 0) {		goto loser;	}	B_DestroyAlgorithmObject(&dsaKeyGenObj);	B_DestroyAlgorithmObject(&randomAlgorithm);	B_DestroyKeyObject(&publicKeyObj);	B_DestroyKeyObject(&privateKeyObj);	dsaZFreePrivateKeyInfo(privateKeyInfo);	dsaZFreePublicKeyInfo(publicKeyInfo);	/* dsaParamInfo contains only public info, no need to ZFree */	*privKey = privateKey;	return SECSuccess;loser:	if (dsaKeyGenObj != NULL_PTR)		B_DestroyAlgorithmObject(&dsaKeyGenObj);	if (randomAlgorithm != NULL_PTR)		B_DestroyAlgorithmObject(&randomAlgorithm);	if (privateKeyObj != NULL_PTR)		B_DestroyKeyObject(&privateKeyObj);	if (publicKeyObj != NULL_PTR)		B_DestroyKeyObject(&publicKeyObj);	if (privateKeyInfo != (A_DSA_PRIVATE_KEY *)NULL_PTR)		dsaZFreePrivateKeyInfo(privateKeyInfo);	if (publicKeyInfo != (A_DSA_PUBLIC_KEY *)NULL_PTR)		dsaZFreePublicKeyInfo(publicKeyInfo);	if (arena != NULL)		PORT_FreeArena(arena, PR_TRUE);	*privKey = NULL;	return SECFailure;}SECStatus DSA_SignDigestWithSeed(DSAPrivateKey * key,                       SECItem *       signature,                       SECItem *       digest,                       unsigned char * seed){	B_ALGORITHM_OBJ           dsaSigner = (B_ALGORITHM_OBJ)NULL_PTR;	B_ALGORITHM_OBJ           randomAlgorithm = NULL_PTR;	B_KEY_OBJ                 privateKeyObj = (B_KEY_OBJ)NULL_PTR;	A_DSA_PRIVATE_KEY         privateKeyInfo;	const B_ALGORITHM_METHOD *dsa_sign_chooser[] = {	                            &AM_DSA_SIGN,	                            (B_ALGORITHM_METHOD *)NULL_PTR	};	int status;	unsigned int siglen;	randomAlgorithm = generateRandomAlgorithm(DSA_SUBPRIME_LEN, seed);	if ((status = B_CreateAlgorithmObject(&dsaSigner)) != 0) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		goto loser;	}	if ((status = B_CreateKeyObject(&privateKeyObj)) != 0) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		goto loser;	}	if ((status = dsaConvertBLKeyToPrKeyInfo(key, &privateKeyInfo)) != 0) {		PORT_SetError(SEC_ERROR_INVALID_ARGS);		goto loser;	}	if ((status = B_SetKeyInfo(privateKeyObj, KI_DSAPrivate, 	                           (POINTER)&privateKeyInfo)) != 0) {		PORT_SetError(SEC_ERROR_INVALID_ARGS);		goto loser;	}	if ((status = B_SetAlgorithmInfo(dsaSigner, AI_DSA, NULL_PTR)) != 0) {		PORT_SetError(SEC_ERROR_INVALID_ARGS);		goto loser;	}	if ((status = B_SignInit(dsaSigner, privateKeyObj, dsa_sign_chooser,	                         (A_SURRENDER_CTX *)NULL_PTR)) != 0) {		PORT_SetError(SEC_ERROR_INVALID_ARGS);		goto loser;	}	if ((status = B_SignUpdate(dsaSigner, digest->data, digest->len,	                           (A_SURRENDER_CTX *)NULL_PTR)) != 0) {		PORT_SetError(SEC_ERROR_INVALID_ARGS);		goto loser;	}	if ((status = B_SignFinal(dsaSigner, signature->data, &siglen,	                          signature->len, randomAlgorithm,	                          (A_SURRENDER_CTX *)NULL_PTR)) != 0) {		PORT_SetError(SEC_ERROR_INVALID_ARGS);		goto loser;	}	SECITEM_ReallocItem(NULL, signature, signature->len, siglen);	signature->len = siglen; /* shouldn't realloc do this? */	B_DestroyAlgorithmObject(&dsaSigner);	B_DestroyKeyObject(&privateKeyObj);	B_DestroyAlgorithmObject(&randomAlgorithm);	/*  privateKeyInfo is shallow copy */	return SECSuccess;loser:	if (dsaSigner != NULL_PTR)		B_DestroyAlgorithmObject(&dsaSigner);	if (privateKeyObj != NULL_PTR)		B_DestroyKeyObject(&privateKeyObj);	if (randomAlgorithm != NULL_PTR)		B_DestroyAlgorithmObject(&randomAlgorithm);	return SECFailure;}SECStatusPQG_ParamGen(unsigned int j, 	   /* input : determines length of P. */             PQGParams **pParams,  /* output: P Q and G returned here */             PQGVerify **pVfy)     /* output: counter and seed. */{	return PQG_ParamGenSeedLen(j, DSA_SUBPRIME_LEN, pParams, pVfy);}SECStatusPQG_ParamGenSeedLen(                  unsigned int j,         /* input : determines length of P. */                  unsigned int seedBytes, /* input : length of seed in bytes.*/                  PQGParams **pParams,    /* output: P Q and G returned here */                  PQGVerify **pVfy)       /* output: counter and seed. */{	B_DSA_PARAM_GEN_PARAMS    dsaParams;	B_ALGORITHM_OBJ           dsaKeyGenObj = (B_ALGORITHM_OBJ)NULL_PTR;	B_ALGORITHM_OBJ           dsaParamGenerator = (B_ALGORITHM_OBJ)NULL_PTR;	B_ALGORITHM_OBJ           randomAlgorithm = NULL_PTR;	A_DSA_PARAMS             *dsaParamInfo;	SECItem tmp;	PQGParams *params;	PRArenaPool *arena;	int status;	if (!pParams || j > 8) {		PORT_SetError(SEC_ERROR_INVALID_ARGS);		return SECFailure;	}	/*  Allocate space for key structure. */	arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE);	if (arena == NULL) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		goto loser;	}	params = (PQGParams *)PORT_ArenaZAlloc(arena, sizeof(PQGParams));	if (params == NULL) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		goto loser;	}	params->arena = arena;	if ((status = B_CreateAlgorithmObject(&dsaParamGenerator)) != 0) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		goto loser;	}	if ((status = B_CreateAlgorithmObject(&dsaKeyGenObj)) != 0) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		goto loser;	}	randomAlgorithm = generateRandomAlgorithm(seedBytes, NULL);	dsaParams.primeBits = 512 + (j * 64);	if ((status = B_SetAlgorithmInfo(dsaParamGenerator, AI_DSAParamGen,	                                 (POINTER)&dsaParams)) != 0) {		PORT_SetError(SEC_ERROR_INVALID_ARGS);		goto loser;	}	if ((status = B_GenerateInit(dsaParamGenerator, dsa_pk_gen_chooser,	                             (A_SURRENDER_CTX *)NULL_PTR)) != 0) {		PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);		goto loser;	}	if ((status = B_GenerateParameters(dsaParamGenerator, dsaKeyGenObj,	                                   randomAlgorithm,	                                   (A_SURRENDER_CTX *)NULL_PTR)) != 0) {		PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);		goto loser;	}	if ((status = B_GetAlgorithmInfo((POINTER *)&dsaParamInfo, dsaKeyGenObj,	                                 AI_DSAKeyGen)) != 0) {		PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);		goto loser;	}	SECITEMFROMITEM(arena, params->prime,    dsaParamInfo->prime);	SECITEMFROMITEM(arena, params->subPrime, dsaParamInfo->subPrime);	SECITEMFROMITEM(arena, params->base,     dsaParamInfo->base);	B_DestroyAlgorithmObject(&dsaKeyGenObj);	B_DestroyAlgorithmObject(&dsaParamGenerator);	B_DestroyAlgorithmObject(&randomAlgorithm);	dsaZFreeKeyInfoParams(dsaParamInfo);	*pParams = params;	return SECSuccess;loser:	if (dsaParamGenerator != NULL_PTR)		B_DestroyAlgorithmObject(&dsaParamGenerator);	if (dsaKeyGenObj != NULL_PTR)		B_DestroyAlgorithmObject(&dsaKeyGenObj);	if (randomAlgorithm != NULL_PTR)		B_DestroyAlgorithmObject(&randomAlgorithm);	if (dsaParamInfo != NULL)		dsaZFreeKeyInfoParams(dsaParamInfo);	if (arena != NULL)		PORT_FreeArena(arena, PR_TRUE);	*pParams = NULL;	return SECFailure;}SECStatusPQG_VerifyParams(const PQGParams *params,                  const PQGVerify *vfy, SECStatus *result){	/*  BSAFE does not provide access to h.	 *  Verification is thus skipped.	 */	PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);	return SECFailure;}/*  Destroy functions are implemented in util/pqgutil.c *//******************************************************************************* BLAPI implementation of RNG******************************************************************************/static SECItem globalseed;static B_ALGORITHM_OBJ globalrng = NULL_PTR;SECStatus RNG_RNGInit(void){	int status;	PRInt32 nBytes;	if (globalrng == NULL) {		globalseed.len = 20;		globalseed.data = (unsigned char *)PORT_Alloc(globalseed.len);	} else {		B_DestroyAlgorithmObject(&globalrng);	}	nBytes = RNG_GetNoise(globalseed.data, globalseed.len);	globalrng = generateRandomAlgorithm(globalseed.len, globalseed.data);	if (globalrng == NULL_PTR) {		PORT_SetError(PR_OUT_OF_MEMORY_ERROR);		return SECFailure;	}	return SECSuccess;}SECStatus RNG_RandomUpdate(void *data, size_t bytes){	int status;	if (data == NULL || bytes <= 0) {		PORT_SetError(SEC_ERROR_INVALID_ARGS);		return SECFailure;	}	if (globalrng == NULL_PTR) {		PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);		return SECFailure;	}	if ((status = B_RandomUpdate(globalrng, data, bytes,	                             (A_SURRENDER_CTX *)NULL_PTR)) != 0) {		PORT_SetError(SEC_ERROR_BAD_DATA);		return SECFailure;	}	return SECSuccess;}SECStatus RNG_GenerateGlobalRandomBytes(void *dest, size_t len){	int status;	if (dest == NULL) {		PORT_SetError(SEC_ERROR_INVALID_ARGS);		return SECFailure;	}	if (globalrng == NULL_PTR) {		PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);		return SECFailure;	}	if ((status = B_GenerateRandomBytes(globalrng, dest, len,	                                    (A_SURRENDER_CTX *)NULL_PTR)) != 0) {		PORT_SetError(SEC_ERROR_BAD_DATA);		return SECFailure;	}	return SECSuccess;}void RNG_RNGShutdown(void){	if (globalrng == NULL_PTR)		/* no-op */		return;	B_DestroyAlgorithmObject(&globalrng);	SECITEM_ZfreeItem(&globalseed, PR_FALSE);	globalrng = NULL_PTR;}

⌨️ 快捷键说明

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