andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: /* Thse functions are stub functions which will get replaced with calls through andre@0: * PKCS #11. andre@0: */ andre@0: andre@0: #include "pk11func.h" andre@0: #include "secmod.h" andre@0: #include "secmodi.h" andre@0: #include "secmodti.h" andre@0: #include "pkcs11t.h" andre@0: #include "pk11pqg.h" andre@0: #include "secerr.h" andre@0: andre@0: andre@0: /* Generate PQGParams and PQGVerify structs. andre@0: * Length of P specified by L. andre@0: * if L is greater than 1024 then the resulting verify parameters will be andre@0: * DSA2. andre@0: * Length of Q specified by N. If zero, The PKCS #11 module will andre@0: * pick an appropriately sized Q for P. If N is specified and L = 1024, then andre@0: * the resulting verify parameters will be DSA2, Otherwise DSA1 parameters andre@0: * will be returned. andre@0: * Length of SEED in bytes specified in seedBytes. andre@0: * andre@0: * The underlying PKCS #11 module will check the values for L, N, andre@0: * and seedBytes. The rules for softoken are: andre@0: * andre@0: * If L <= 1024, then L must be between 512 and 1024 in increments of 64 bits. andre@0: * If L <= 1024, then N must be 0 or 160. andre@0: * If L >= 1024, then L and N must match the following table: andre@0: * L=1024 N=0 or 160 andre@0: * L=2048 N=0 or 224 andre@0: * L=2048 N=256 andre@0: * L=3072 N=0 or 256 andre@0: * if L <= 1024 andre@0: * seedBbytes must be in the range [20..256]. andre@0: * if L >= 1024 andre@0: * seedBbytes must be in the range [20..L/16]. andre@0: */ andre@0: extern SECStatus andre@0: PK11_PQG_ParamGenV2(unsigned int L, unsigned int N, andre@0: unsigned int seedBytes, PQGParams **pParams, PQGVerify **pVfy) andre@0: { andre@0: PK11SlotInfo *slot = NULL; andre@0: CK_ATTRIBUTE genTemplate[5]; andre@0: CK_ATTRIBUTE *attrs = genTemplate; andre@0: int count = sizeof(genTemplate)/sizeof(genTemplate[0]); andre@0: CK_MECHANISM mechanism; andre@0: CK_OBJECT_HANDLE objectID = CK_INVALID_HANDLE; andre@0: CK_RV crv; andre@0: CK_ATTRIBUTE pTemplate[] = { andre@0: { CKA_PRIME, NULL, 0 }, andre@0: { CKA_SUBPRIME, NULL, 0 }, andre@0: { CKA_BASE, NULL, 0 }, andre@0: }; andre@0: CK_ATTRIBUTE vTemplate[] = { andre@0: { CKA_NETSCAPE_PQG_COUNTER, NULL, 0 }, andre@0: { CKA_NETSCAPE_PQG_SEED, NULL, 0 }, andre@0: { CKA_NETSCAPE_PQG_H, NULL, 0 }, andre@0: }; andre@0: CK_ULONG primeBits = L; andre@0: CK_ULONG subPrimeBits = N; andre@0: int pTemplateCount = sizeof(pTemplate)/sizeof(pTemplate[0]); andre@0: int vTemplateCount = sizeof(vTemplate)/sizeof(vTemplate[0]); andre@0: PLArenaPool *parena = NULL; andre@0: PLArenaPool *varena = NULL; andre@0: PQGParams *params = NULL; andre@0: PQGVerify *verify = NULL; andre@0: CK_ULONG seedBits = seedBytes*8; andre@0: andre@0: *pParams = NULL; andre@0: *pVfy = NULL; andre@0: andre@0: if (primeBits == (CK_ULONG)-1) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: goto loser; andre@0: } andre@0: PK11_SETATTRS(attrs, CKA_PRIME_BITS,&primeBits,sizeof(primeBits)); attrs++; andre@0: if (subPrimeBits != 0) { andre@0: PK11_SETATTRS(attrs, CKA_SUB_PRIME_BITS, andre@0: &subPrimeBits, sizeof(subPrimeBits)); attrs++; andre@0: } andre@0: if (seedBits != 0) { andre@0: PK11_SETATTRS(attrs, CKA_NETSCAPE_PQG_SEED_BITS, andre@0: &seedBits, sizeof(seedBits)); attrs++; andre@0: } andre@0: count = attrs - genTemplate; andre@0: PR_ASSERT(count <= sizeof(genTemplate)/sizeof(CK_ATTRIBUTE)); andre@0: andre@0: slot = PK11_GetInternalSlot(); andre@0: if (slot == NULL) { andre@0: /* set error */ andre@0: PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);/* shouldn't happen */ andre@0: goto loser; andre@0: } andre@0: andre@0: /* make sure the internal slot can handle DSA2 type parameters. */ andre@0: if (primeBits > 1024) { andre@0: CK_MECHANISM_INFO mechanism_info; andre@0: andre@0: if (!slot->isThreadSafe) PK11_EnterSlotMonitor(slot); andre@0: crv = PK11_GETTAB(slot)->C_GetMechanismInfo(slot->slotID, andre@0: CKM_DSA_PARAMETER_GEN, &mechanism_info); andre@0: if (!slot->isThreadSafe) PK11_ExitSlotMonitor(slot); andre@0: /* a bug in the old softoken left CKM_DSA_PARAMETER_GEN off of the andre@0: * mechanism List. If we get a failure asking for this value, we know andre@0: * it can't handle DSA2 */ andre@0: if ((crv != CKR_OK) || (mechanism_info.ulMaxKeySize < primeBits)) { andre@0: PK11_FreeSlot(slot); andre@0: slot = PK11_GetBestSlotWithAttributes(CKM_DSA_PARAMETER_GEN, 0, andre@0: primeBits, NULL); andre@0: if (slot == NULL) { andre@0: PORT_SetError(SEC_ERROR_NO_TOKEN); /* can happen */ andre@0: goto loser; andre@0: } andre@0: /* ditch seedBits in this case, they are NSS specific and at andre@0: * this point we have a token that claims to handle DSA2 */ andre@0: if (seedBits) { andre@0: attrs--; andre@0: } andre@0: } andre@0: } andre@0: andre@0: /* Initialize the Key Gen Mechanism */ andre@0: mechanism.mechanism = CKM_DSA_PARAMETER_GEN; andre@0: mechanism.pParameter = NULL; andre@0: mechanism.ulParameterLen = 0; andre@0: andre@0: PK11_EnterSlotMonitor(slot); andre@0: crv = PK11_GETTAB(slot)->C_GenerateKey(slot->session, andre@0: &mechanism, genTemplate, count, &objectID); andre@0: PK11_ExitSlotMonitor(slot); andre@0: andre@0: if (crv != CKR_OK) { andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: goto loser; andre@0: } andre@0: andre@0: parena = PORT_NewArena(60); andre@0: if (!parena) { andre@0: goto loser; andre@0: } andre@0: andre@0: crv = PK11_GetAttributes(parena, slot, objectID, pTemplate, pTemplateCount); andre@0: if (crv != CKR_OK) { andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: goto loser; andre@0: } andre@0: andre@0: andre@0: params = (PQGParams *)PORT_ArenaAlloc(parena,sizeof(PQGParams)); andre@0: if (params == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* fill in Params */ andre@0: params->arena = parena; andre@0: params->prime.type = siUnsignedInteger; andre@0: params->prime.data = pTemplate[0].pValue; andre@0: params->prime.len = pTemplate[0].ulValueLen; andre@0: params->subPrime.type = siUnsignedInteger; andre@0: params->subPrime.data = pTemplate[1].pValue; andre@0: params->subPrime.len = pTemplate[1].ulValueLen; andre@0: params->base.type = siUnsignedInteger; andre@0: params->base.data = pTemplate[2].pValue; andre@0: params->base.len = pTemplate[2].ulValueLen; andre@0: andre@0: andre@0: varena = PORT_NewArena(60); andre@0: if (!varena) { andre@0: goto loser; andre@0: } andre@0: andre@0: crv = PK11_GetAttributes(varena, slot, objectID, vTemplate, vTemplateCount); andre@0: if (crv != CKR_OK) { andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: goto loser; andre@0: } andre@0: andre@0: andre@0: verify = (PQGVerify *)PORT_ArenaAlloc(varena,sizeof(PQGVerify)); andre@0: if (verify == NULL) { andre@0: goto loser; andre@0: } andre@0: /* fill in Params */ andre@0: verify->arena = varena; andre@0: verify->counter = (unsigned int)(*(CK_ULONG*)vTemplate[0].pValue); andre@0: verify->seed.type = siUnsignedInteger; andre@0: verify->seed.data = vTemplate[1].pValue; andre@0: verify->seed.len = vTemplate[1].ulValueLen; andre@0: verify->h.type = siUnsignedInteger; andre@0: verify->h.data = vTemplate[2].pValue; andre@0: verify->h.len = vTemplate[2].ulValueLen; andre@0: andre@0: PK11_DestroyObject(slot,objectID); andre@0: PK11_FreeSlot(slot); andre@0: andre@0: *pParams = params; andre@0: *pVfy = verify; andre@0: andre@0: return SECSuccess; andre@0: andre@0: loser: andre@0: if (objectID != CK_INVALID_HANDLE) { andre@0: PK11_DestroyObject(slot,objectID); andre@0: } andre@0: if (parena != NULL) { andre@0: PORT_FreeArena(parena,PR_FALSE); andre@0: } andre@0: if (varena != NULL) { andre@0: PORT_FreeArena(varena,PR_FALSE); andre@0: } andre@0: if (slot) { andre@0: PK11_FreeSlot(slot); andre@0: } andre@0: return SECFailure; andre@0: } andre@0: andre@0: /* Generate PQGParams and PQGVerify structs. andre@0: * Length of P specified by j. Length of h will match length of P. andre@0: * Length of SEED in bytes specified in seedBytes. andre@0: * seedBbytes must be in the range [20..255] or an error will result. andre@0: */ andre@0: extern SECStatus andre@0: PK11_PQG_ParamGenSeedLen( unsigned int j, unsigned int seedBytes, andre@0: PQGParams **pParams, PQGVerify **pVfy) andre@0: { andre@0: unsigned int primeBits = PQG_INDEX_TO_PBITS(j); andre@0: return PK11_PQG_ParamGenV2(primeBits, 0, seedBytes, pParams, pVfy); andre@0: } andre@0: andre@0: /* Generate PQGParams and PQGVerify structs. andre@0: * Length of seed and length of h both equal length of P. andre@0: * All lengths are specified by "j", according to the table above. andre@0: */ andre@0: extern SECStatus andre@0: PK11_PQG_ParamGen(unsigned int j, PQGParams **pParams, PQGVerify **pVfy) andre@0: { andre@0: unsigned int primeBits = PQG_INDEX_TO_PBITS(j); andre@0: return PK11_PQG_ParamGenV2(primeBits, 0, 0, pParams, pVfy); andre@0: } andre@0: andre@0: /* Test PQGParams for validity as DSS PQG values. andre@0: * If vfy is non-NULL, test PQGParams to make sure they were generated andre@0: * using the specified seed, counter, and h values. andre@0: * andre@0: * Return value indicates whether Verification operation ran successfully andre@0: * to completion, but does not indicate if PQGParams are valid or not. andre@0: * If return value is SECSuccess, then *pResult has these meanings: andre@0: * SECSuccess: PQGParams are valid. andre@0: * SECFailure: PQGParams are invalid. andre@0: */ andre@0: andre@0: extern SECStatus andre@0: PK11_PQG_VerifyParams(const PQGParams *params, const PQGVerify *vfy, andre@0: SECStatus *result) andre@0: { andre@0: CK_ATTRIBUTE keyTempl[] = { andre@0: { CKA_CLASS, NULL, 0 }, andre@0: { CKA_KEY_TYPE, NULL, 0 }, andre@0: { CKA_PRIME, NULL, 0 }, andre@0: { CKA_SUBPRIME, NULL, 0 }, andre@0: { CKA_BASE, NULL, 0 }, andre@0: { CKA_TOKEN, NULL, 0 }, andre@0: { CKA_NETSCAPE_PQG_COUNTER, NULL, 0 }, andre@0: { CKA_NETSCAPE_PQG_SEED, NULL, 0 }, andre@0: { CKA_NETSCAPE_PQG_H, NULL, 0 }, andre@0: }; andre@0: CK_ATTRIBUTE *attrs; andre@0: CK_BBOOL ckfalse = CK_FALSE; andre@0: CK_OBJECT_CLASS class = CKO_KG_PARAMETERS; andre@0: CK_KEY_TYPE keyType = CKK_DSA; andre@0: SECStatus rv = SECSuccess; andre@0: PK11SlotInfo *slot; andre@0: int keyCount; andre@0: CK_OBJECT_HANDLE objectID; andre@0: CK_ULONG counter; andre@0: CK_RV crv; andre@0: andre@0: attrs = keyTempl; andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &class, sizeof(class)); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_KEY_TYPE, &keyType, sizeof(keyType)); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_PRIME, params->prime.data, andre@0: params->prime.len); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_SUBPRIME, params->subPrime.data, andre@0: params->subPrime.len); attrs++; andre@0: if (params->base.len) { andre@0: PK11_SETATTRS(attrs, CKA_BASE,params->base.data,params->base.len); andre@0: attrs++; andre@0: } andre@0: PK11_SETATTRS(attrs, CKA_TOKEN, &ckfalse, sizeof(ckfalse)); attrs++; andre@0: if (vfy) { andre@0: if (vfy->counter != -1) { andre@0: counter = vfy->counter; andre@0: PK11_SETATTRS(attrs, CKA_NETSCAPE_PQG_COUNTER, andre@0: &counter, sizeof(counter)); attrs++; andre@0: } andre@0: PK11_SETATTRS(attrs, CKA_NETSCAPE_PQG_SEED, andre@0: vfy->seed.data, vfy->seed.len); attrs++; andre@0: if (vfy->h.len) { andre@0: PK11_SETATTRS(attrs, CKA_NETSCAPE_PQG_H, andre@0: vfy->h.data, vfy->h.len); attrs++; andre@0: } andre@0: } andre@0: andre@0: keyCount = attrs - keyTempl; andre@0: PORT_Assert(keyCount <= sizeof(keyTempl)/sizeof(keyTempl[0])); andre@0: andre@0: andre@0: slot = PK11_GetInternalSlot(); andre@0: if (slot == NULL) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: PK11_EnterSlotMonitor(slot); andre@0: crv = PK11_GETTAB(slot)->C_CreateObject(slot->session, keyTempl, keyCount, andre@0: &objectID); andre@0: PK11_ExitSlotMonitor(slot); andre@0: andre@0: /* throw away the keys, we only wanted the return code */ andre@0: PK11_DestroyObject(slot,objectID); andre@0: PK11_FreeSlot(slot); andre@0: andre@0: *result = SECSuccess; andre@0: if (crv == CKR_ATTRIBUTE_VALUE_INVALID) { andre@0: *result = SECFailure; andre@0: } else if (crv != CKR_OK) { andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: rv = SECFailure; andre@0: } andre@0: return rv; andre@0: andre@0: } andre@0: andre@0: andre@0: andre@0: /************************************************************************** andre@0: * Free the PQGParams struct and the things it points to. * andre@0: **************************************************************************/ andre@0: extern void andre@0: PK11_PQG_DestroyParams(PQGParams *params) { andre@0: if (params == NULL) andre@0: return; andre@0: if (params->arena != NULL) { andre@0: PORT_FreeArena(params->arena, PR_FALSE); /* don't zero it */ andre@0: } else { andre@0: SECITEM_FreeItem(¶ms->prime, PR_FALSE); /* don't free prime */ andre@0: SECITEM_FreeItem(¶ms->subPrime, PR_FALSE); /* don't free subPrime */ andre@0: SECITEM_FreeItem(¶ms->base, PR_FALSE); /* don't free base */ andre@0: PORT_Free(params); andre@0: } andre@0: } andre@0: andre@0: /************************************************************************** andre@0: * Free the PQGVerify struct and the things it points to. * andre@0: **************************************************************************/ andre@0: extern void andre@0: PK11_PQG_DestroyVerify(PQGVerify *vfy) { andre@0: if (vfy == NULL) andre@0: return; andre@0: if (vfy->arena != NULL) { andre@0: PORT_FreeArena(vfy->arena, PR_FALSE); /* don't zero it */ andre@0: } else { andre@0: SECITEM_FreeItem(&vfy->seed, PR_FALSE); /* don't free seed */ andre@0: SECITEM_FreeItem(&vfy->h, PR_FALSE); /* don't free h */ andre@0: PORT_Free(vfy); andre@0: } andre@0: } andre@0: andre@0: #define PQG_DEFAULT_CHUNKSIZE 2048 /* bytes */ andre@0: andre@0: /************************************************************************** andre@0: * Return a pointer to a new PQGParams struct that is constructed from * andre@0: * copies of the arguments passed in. * andre@0: * Return NULL on failure. * andre@0: **************************************************************************/ andre@0: extern PQGParams * andre@0: PK11_PQG_NewParams(const SECItem * prime, const SECItem * subPrime, andre@0: const SECItem * base) { andre@0: PLArenaPool *arena; andre@0: PQGParams *dest; andre@0: SECStatus status; andre@0: andre@0: arena = PORT_NewArena(PQG_DEFAULT_CHUNKSIZE); andre@0: if (arena == NULL) andre@0: goto loser; andre@0: andre@0: dest = (PQGParams*)PORT_ArenaZAlloc(arena, sizeof(PQGParams)); andre@0: if (dest == NULL) andre@0: goto loser; andre@0: andre@0: dest->arena = arena; andre@0: andre@0: status = SECITEM_CopyItem(arena, &dest->prime, prime); andre@0: if (status != SECSuccess) andre@0: goto loser; andre@0: andre@0: status = SECITEM_CopyItem(arena, &dest->subPrime, subPrime); andre@0: if (status != SECSuccess) andre@0: goto loser; andre@0: andre@0: status = SECITEM_CopyItem(arena, &dest->base, base); andre@0: if (status != SECSuccess) andre@0: goto loser; andre@0: andre@0: return dest; andre@0: andre@0: loser: andre@0: if (arena != NULL) andre@0: PORT_FreeArena(arena, PR_FALSE); andre@0: return NULL; andre@0: } andre@0: andre@0: andre@0: /************************************************************************** andre@0: * Fills in caller's "prime" SECItem with the prime value in params. andre@0: * Contents can be freed by calling SECITEM_FreeItem(prime, PR_FALSE); andre@0: **************************************************************************/ andre@0: extern SECStatus andre@0: PK11_PQG_GetPrimeFromParams(const PQGParams *params, SECItem * prime) { andre@0: return SECITEM_CopyItem(NULL, prime, ¶ms->prime); andre@0: } andre@0: andre@0: andre@0: /************************************************************************** andre@0: * Fills in caller's "subPrime" SECItem with the prime value in params. andre@0: * Contents can be freed by calling SECITEM_FreeItem(subPrime, PR_FALSE); andre@0: **************************************************************************/ andre@0: extern SECStatus andre@0: PK11_PQG_GetSubPrimeFromParams(const PQGParams *params, SECItem * subPrime) { andre@0: return SECITEM_CopyItem(NULL, subPrime, ¶ms->subPrime); andre@0: } andre@0: andre@0: andre@0: /************************************************************************** andre@0: * Fills in caller's "base" SECItem with the base value in params. andre@0: * Contents can be freed by calling SECITEM_FreeItem(base, PR_FALSE); andre@0: **************************************************************************/ andre@0: extern SECStatus andre@0: PK11_PQG_GetBaseFromParams(const PQGParams *params, SECItem *base) { andre@0: return SECITEM_CopyItem(NULL, base, ¶ms->base); andre@0: } andre@0: andre@0: andre@0: /************************************************************************** andre@0: * Return a pointer to a new PQGVerify struct that is constructed from * andre@0: * copies of the arguments passed in. * andre@0: * Return NULL on failure. * andre@0: **************************************************************************/ andre@0: extern PQGVerify * andre@0: PK11_PQG_NewVerify(unsigned int counter, const SECItem * seed, andre@0: const SECItem * h) { andre@0: PLArenaPool *arena; andre@0: PQGVerify * dest; andre@0: SECStatus status; andre@0: andre@0: arena = PORT_NewArena(PQG_DEFAULT_CHUNKSIZE); andre@0: if (arena == NULL) andre@0: goto loser; andre@0: andre@0: dest = (PQGVerify*)PORT_ArenaZAlloc(arena, sizeof(PQGVerify)); andre@0: if (dest == NULL) andre@0: goto loser; andre@0: andre@0: dest->arena = arena; andre@0: dest->counter = counter; andre@0: andre@0: status = SECITEM_CopyItem(arena, &dest->seed, seed); andre@0: if (status != SECSuccess) andre@0: goto loser; andre@0: andre@0: status = SECITEM_CopyItem(arena, &dest->h, h); andre@0: if (status != SECSuccess) andre@0: goto loser; andre@0: andre@0: return dest; andre@0: andre@0: loser: andre@0: if (arena != NULL) andre@0: PORT_FreeArena(arena, PR_FALSE); andre@0: return NULL; andre@0: } andre@0: andre@0: andre@0: /************************************************************************** andre@0: * Returns "counter" value from the PQGVerify. andre@0: **************************************************************************/ andre@0: extern unsigned int andre@0: PK11_PQG_GetCounterFromVerify(const PQGVerify *verify) { andre@0: return verify->counter; andre@0: } andre@0: andre@0: /************************************************************************** andre@0: * Fills in caller's "seed" SECItem with the seed value in verify. andre@0: * Contents can be freed by calling SECITEM_FreeItem(seed, PR_FALSE); andre@0: **************************************************************************/ andre@0: extern SECStatus andre@0: PK11_PQG_GetSeedFromVerify(const PQGVerify *verify, SECItem *seed) { andre@0: return SECITEM_CopyItem(NULL, seed, &verify->seed); andre@0: } andre@0: andre@0: andre@0: /************************************************************************** andre@0: * Fills in caller's "h" SECItem with the h value in verify. andre@0: * Contents can be freed by calling SECITEM_FreeItem(h, PR_FALSE); andre@0: **************************************************************************/ andre@0: extern SECStatus andre@0: PK11_PQG_GetHFromVerify(const PQGVerify *verify, SECItem * h) { andre@0: return SECITEM_CopyItem(NULL, h, &verify->h); andre@0: }