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