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: andre@0: #include "seccomon.h" andre@0: #include "secerr.h" andre@0: #include "blapi.h" andre@0: #include "pkcs11i.h" andre@0: #include "softoken.h" andre@0: andre@0: static CK_RV andre@0: jpake_mapStatus(SECStatus rv, CK_RV invalidArgsMapping) { andre@0: int err; andre@0: if (rv == SECSuccess) andre@0: return CKR_OK; andre@0: err = PORT_GetError(); andre@0: switch (err) { andre@0: /* XXX: SEC_ERROR_INVALID_ARGS might be caused by invalid template andre@0: parameters. */ andre@0: case SEC_ERROR_INVALID_ARGS: return invalidArgsMapping; andre@0: case SEC_ERROR_BAD_SIGNATURE: return CKR_SIGNATURE_INVALID; andre@0: case SEC_ERROR_NO_MEMORY: return CKR_HOST_MEMORY; andre@0: } andre@0: return CKR_FUNCTION_FAILED; andre@0: } andre@0: andre@0: /* If key is not NULL then the gx value will be stored as an attribute with andre@0: the type given by the gxAttrType parameter. */ andre@0: static CK_RV andre@0: jpake_Sign(PLArenaPool * arena, const PQGParams * pqg, HASH_HashType hashType, andre@0: const SECItem * signerID, const SECItem * x, andre@0: CK_NSS_JPAKEPublicValue * out) andre@0: { andre@0: SECItem gx, gv, r; andre@0: CK_RV crv; andre@0: andre@0: PORT_Assert(arena != NULL); andre@0: andre@0: gx.data = NULL; andre@0: gv.data = NULL; andre@0: r.data = NULL; andre@0: crv = jpake_mapStatus(JPAKE_Sign(arena, pqg, hashType, signerID, x, NULL, andre@0: NULL, &gx, &gv, &r), andre@0: CKR_MECHANISM_PARAM_INVALID); andre@0: if (crv == CKR_OK) { andre@0: if ((out->pGX != NULL && out->ulGXLen >= gx.len) || andre@0: (out->pGV != NULL && out->ulGVLen >= gv.len) || andre@0: (out->pR != NULL && out->ulRLen >= r.len)) { andre@0: PORT_Memcpy(out->pGX, gx.data, gx.len); andre@0: PORT_Memcpy(out->pGV, gv.data, gv.len); andre@0: PORT_Memcpy(out->pR, r.data, r.len); andre@0: out->ulGXLen = gx.len; andre@0: out->ulGVLen = gv.len; andre@0: out->ulRLen = r.len; andre@0: } else { andre@0: crv = CKR_MECHANISM_PARAM_INVALID; andre@0: } andre@0: } andre@0: return crv; andre@0: } andre@0: andre@0: static CK_RV andre@0: jpake_Verify(PLArenaPool * arena, const PQGParams * pqg, andre@0: HASH_HashType hashType, const SECItem * signerID, andre@0: const CK_BYTE * peerIDData, CK_ULONG peerIDLen, andre@0: const CK_NSS_JPAKEPublicValue * publicValueIn) andre@0: { andre@0: SECItem peerID, gx, gv, r; andre@0: peerID.data = (unsigned char *) peerIDData; peerID.len = peerIDLen; andre@0: gx.data = publicValueIn->pGX; gx.len = publicValueIn->ulGXLen; andre@0: gv.data = publicValueIn->pGV; gv.len = publicValueIn->ulGVLen; andre@0: r.data = publicValueIn->pR; r.len = publicValueIn->ulRLen; andre@0: return jpake_mapStatus(JPAKE_Verify(arena, pqg, hashType, signerID, &peerID, andre@0: &gx, &gv, &r), andre@0: CKR_MECHANISM_PARAM_INVALID); andre@0: } andre@0: andre@0: #define NUM_ELEM(x) (sizeof (x) / sizeof (x)[0]) andre@0: andre@0: /* If the template has the key type set, ensure that it was set to the correct andre@0: * value. If the template did not have the key type set, set it to the andre@0: * correct value. andre@0: */ andre@0: static CK_RV andre@0: jpake_enforceKeyType(SFTKObject * key, CK_KEY_TYPE keyType) { andre@0: CK_RV crv; andre@0: SFTKAttribute * keyTypeAttr = sftk_FindAttribute(key, CKA_KEY_TYPE); andre@0: if (keyTypeAttr != NULL) { andre@0: crv = *(CK_KEY_TYPE *)keyTypeAttr->attrib.pValue == keyType andre@0: ? CKR_OK andre@0: : CKR_TEMPLATE_INCONSISTENT; andre@0: sftk_FreeAttribute(keyTypeAttr); andre@0: } else { andre@0: crv = sftk_forceAttribute(key, CKA_KEY_TYPE, &keyType, sizeof keyType); andre@0: } andre@0: return crv; andre@0: } andre@0: andre@0: static CK_RV andre@0: jpake_MultipleSecItem2Attribute(SFTKObject * key, const SFTKItemTemplate * attrs, andre@0: size_t attrsCount) andre@0: { andre@0: size_t i; andre@0: andre@0: for (i = 0; i < attrsCount; ++i) { andre@0: CK_RV crv = sftk_forceAttribute(key, attrs[i].type, attrs[i].item->data, andre@0: attrs[i].item->len); andre@0: if (crv != CKR_OK) andre@0: return crv; andre@0: } andre@0: return CKR_OK; andre@0: } andre@0: andre@0: CK_RV andre@0: jpake_Round1(HASH_HashType hashType, CK_NSS_JPAKERound1Params * params, andre@0: SFTKObject * key) andre@0: { andre@0: CK_RV crv; andre@0: PQGParams pqg; andre@0: PLArenaPool * arena; andre@0: SECItem signerID; andre@0: SFTKItemTemplate templateAttrs[] = { andre@0: { CKA_PRIME, &pqg.prime }, andre@0: { CKA_SUBPRIME, &pqg.subPrime }, andre@0: { CKA_BASE, &pqg.base }, andre@0: { CKA_NSS_JPAKE_SIGNERID, &signerID } andre@0: }; andre@0: SECItem x2, gx1, gx2; andre@0: const SFTKItemTemplate generatedAttrs[] = { andre@0: { CKA_NSS_JPAKE_X2, &x2 }, andre@0: { CKA_NSS_JPAKE_GX1, &gx1 }, andre@0: { CKA_NSS_JPAKE_GX2, &gx2 }, andre@0: }; andre@0: SECItem x1; andre@0: andre@0: PORT_Assert(params != NULL); andre@0: PORT_Assert(key != NULL); andre@0: andre@0: arena = PORT_NewArena(NSS_SOFTOKEN_DEFAULT_CHUNKSIZE); andre@0: if (arena == NULL) andre@0: crv = CKR_HOST_MEMORY; andre@0: andre@0: crv = sftk_MultipleAttribute2SecItem(arena, key, templateAttrs, andre@0: NUM_ELEM(templateAttrs)); andre@0: andre@0: if (crv == CKR_OK && (signerID.data == NULL || signerID.len == 0)) andre@0: crv = CKR_TEMPLATE_INCOMPLETE; andre@0: andre@0: /* generate x1, g^x1 and the proof of knowledge of x1 */ andre@0: if (crv == CKR_OK) { andre@0: x1.data = NULL; andre@0: crv = jpake_mapStatus(DSA_NewRandom(arena, &pqg.subPrime, &x1), andre@0: CKR_TEMPLATE_INCONSISTENT); andre@0: } andre@0: if (crv == CKR_OK) andre@0: crv = jpake_Sign(arena, &pqg, hashType, &signerID, &x1, ¶ms->gx1); andre@0: andre@0: /* generate x2, g^x2 and the proof of knowledge of x2 */ andre@0: if (crv == CKR_OK) { andre@0: x2.data = NULL; andre@0: crv = jpake_mapStatus(DSA_NewRandom(arena, &pqg.subPrime, &x2), andre@0: CKR_TEMPLATE_INCONSISTENT); andre@0: } andre@0: if (crv == CKR_OK) andre@0: crv = jpake_Sign(arena, &pqg, hashType, &signerID, &x2, ¶ms->gx2); andre@0: andre@0: /* Save the values needed for round 2 into CKA_VALUE */ andre@0: if (crv == CKR_OK) { andre@0: gx1.data = params->gx1.pGX; andre@0: gx1.len = params->gx1.ulGXLen; andre@0: gx2.data = params->gx2.pGX; andre@0: gx2.len = params->gx2.ulGXLen; andre@0: crv = jpake_MultipleSecItem2Attribute(key, generatedAttrs, andre@0: NUM_ELEM(generatedAttrs)); andre@0: } andre@0: andre@0: PORT_FreeArena(arena, PR_TRUE); andre@0: return crv; andre@0: } andre@0: andre@0: CK_RV andre@0: jpake_Round2(HASH_HashType hashType, CK_NSS_JPAKERound2Params * params, andre@0: SFTKObject * sourceKey, SFTKObject * key) andre@0: { andre@0: CK_RV crv; andre@0: PLArenaPool * arena; andre@0: PQGParams pqg; andre@0: SECItem signerID, x2, gx1, gx2; andre@0: SFTKItemTemplate sourceAttrs[] = { andre@0: { CKA_PRIME, &pqg.prime }, andre@0: { CKA_SUBPRIME, &pqg.subPrime }, andre@0: { CKA_BASE, &pqg.base }, andre@0: { CKA_NSS_JPAKE_SIGNERID, &signerID }, andre@0: { CKA_NSS_JPAKE_X2, &x2 }, andre@0: { CKA_NSS_JPAKE_GX1, &gx1 }, andre@0: { CKA_NSS_JPAKE_GX2, &gx2 }, andre@0: }; andre@0: SECItem x2s, gx3, gx4; andre@0: const SFTKItemTemplate copiedAndGeneratedAttrs[] = { andre@0: { CKA_NSS_JPAKE_SIGNERID, &signerID }, andre@0: { CKA_PRIME, &pqg.prime }, andre@0: { CKA_SUBPRIME, &pqg.subPrime }, andre@0: { CKA_NSS_JPAKE_X2, &x2 }, andre@0: { CKA_NSS_JPAKE_X2S, &x2s }, andre@0: { CKA_NSS_JPAKE_GX1, &gx1 }, andre@0: { CKA_NSS_JPAKE_GX2, &gx2 }, andre@0: { CKA_NSS_JPAKE_GX3, &gx3 }, andre@0: { CKA_NSS_JPAKE_GX4, &gx4 } andre@0: }; andre@0: SECItem peerID; andre@0: andre@0: PORT_Assert(params != NULL); andre@0: PORT_Assert(sourceKey != NULL); andre@0: PORT_Assert(key != NULL); andre@0: andre@0: arena = PORT_NewArena(NSS_SOFTOKEN_DEFAULT_CHUNKSIZE); andre@0: if (arena == NULL) andre@0: crv = CKR_HOST_MEMORY; andre@0: andre@0: /* TODO: check CKK_NSS_JPAKE_ROUND1 */ andre@0: andre@0: crv = sftk_MultipleAttribute2SecItem(arena, sourceKey, sourceAttrs, andre@0: NUM_ELEM(sourceAttrs)); andre@0: andre@0: /* Get the peer's ID out of the template and sanity-check it. */ andre@0: if (crv == CKR_OK) andre@0: crv = sftk_Attribute2SecItem(arena, &peerID, key, andre@0: CKA_NSS_JPAKE_PEERID); andre@0: if (crv == CKR_OK && (peerID.data == NULL || peerID.len == 0)) andre@0: crv = CKR_TEMPLATE_INCOMPLETE; andre@0: if (crv == CKR_OK && SECITEM_CompareItem(&signerID, &peerID) == SECEqual) andre@0: crv = CKR_TEMPLATE_INCONSISTENT; andre@0: andre@0: /* Verify zero-knowledge proofs for g^x3 and g^x4 */ andre@0: if (crv == CKR_OK) andre@0: crv = jpake_Verify(arena, &pqg, hashType, &signerID, andre@0: peerID.data, peerID.len, ¶ms->gx3); andre@0: if (crv == CKR_OK) andre@0: crv = jpake_Verify(arena, &pqg, hashType, &signerID, andre@0: peerID.data, peerID.len, ¶ms->gx4); andre@0: andre@0: /* Calculate the base and x2s for A=base^x2s */ andre@0: if (crv == CKR_OK) { andre@0: SECItem s; andre@0: s.data = params->pSharedKey; andre@0: s.len = params->ulSharedKeyLen; andre@0: gx3.data = params->gx3.pGX; andre@0: gx3.len = params->gx3.ulGXLen; andre@0: gx4.data = params->gx4.pGX; andre@0: gx4.len = params->gx4.ulGXLen; andre@0: pqg.base.data = NULL; andre@0: x2s.data = NULL; andre@0: crv = jpake_mapStatus(JPAKE_Round2(arena, &pqg.prime, &pqg.subPrime, andre@0: &gx1, &gx3, &gx4, &pqg.base, andre@0: &x2, &s, &x2s), andre@0: CKR_MECHANISM_PARAM_INVALID); andre@0: } andre@0: andre@0: /* Generate A=base^x2s and its zero-knowledge proof. */ andre@0: if (crv == CKR_OK) andre@0: crv = jpake_Sign(arena, &pqg, hashType, &signerID, &x2s, ¶ms->A); andre@0: andre@0: /* Copy P and Q from the ROUND1 key to the ROUND2 key and save the values andre@0: needed for the final key material derivation into CKA_VALUE. */ andre@0: if (crv == CKR_OK) andre@0: crv = sftk_forceAttribute(key, CKA_PRIME, pqg.prime.data, andre@0: pqg.prime.len); andre@0: if (crv == CKR_OK) andre@0: crv = sftk_forceAttribute(key, CKA_SUBPRIME, pqg.subPrime.data, andre@0: pqg.subPrime.len); andre@0: if (crv == CKR_OK) { andre@0: crv = jpake_MultipleSecItem2Attribute(key, copiedAndGeneratedAttrs, andre@0: NUM_ELEM(copiedAndGeneratedAttrs)); andre@0: } andre@0: andre@0: if (crv == CKR_OK) andre@0: crv = jpake_enforceKeyType(key, CKK_NSS_JPAKE_ROUND2); andre@0: andre@0: PORT_FreeArena(arena, PR_TRUE); andre@0: return crv; andre@0: } andre@0: andre@0: CK_RV andre@0: jpake_Final(HASH_HashType hashType, const CK_NSS_JPAKEFinalParams * param, andre@0: SFTKObject * sourceKey, SFTKObject * key) andre@0: { andre@0: PLArenaPool * arena; andre@0: SECItem K; andre@0: PQGParams pqg; andre@0: CK_RV crv; andre@0: SECItem peerID, signerID, x2s, x2, gx1, gx2, gx3, gx4; andre@0: SFTKItemTemplate sourceAttrs[] = { andre@0: { CKA_NSS_JPAKE_PEERID, &peerID }, andre@0: { CKA_NSS_JPAKE_SIGNERID, &signerID }, andre@0: { CKA_PRIME, &pqg.prime }, andre@0: { CKA_SUBPRIME, &pqg.subPrime }, andre@0: { CKA_NSS_JPAKE_X2, &x2 }, andre@0: { CKA_NSS_JPAKE_X2S, &x2s }, andre@0: { CKA_NSS_JPAKE_GX1, &gx1 }, andre@0: { CKA_NSS_JPAKE_GX2, &gx2 }, andre@0: { CKA_NSS_JPAKE_GX3, &gx3 }, andre@0: { CKA_NSS_JPAKE_GX4, &gx4 } andre@0: }; andre@0: andre@0: PORT_Assert(param != NULL); andre@0: PORT_Assert(sourceKey != NULL); andre@0: PORT_Assert(key != NULL); andre@0: andre@0: arena = PORT_NewArena(NSS_SOFTOKEN_DEFAULT_CHUNKSIZE); andre@0: if (arena == NULL) andre@0: crv = CKR_HOST_MEMORY; andre@0: andre@0: /* TODO: verify key type CKK_NSS_JPAKE_ROUND2 */ andre@0: andre@0: crv = sftk_MultipleAttribute2SecItem(arena, sourceKey, sourceAttrs, andre@0: NUM_ELEM(sourceAttrs)); andre@0: andre@0: /* Calculate base for B=base^x4s */ andre@0: if (crv == CKR_OK) { andre@0: pqg.base.data = NULL; andre@0: crv = jpake_mapStatus(JPAKE_Round2(arena, &pqg.prime, &pqg.subPrime, andre@0: &gx1, &gx2, &gx3, &pqg.base, andre@0: NULL, NULL, NULL), andre@0: CKR_MECHANISM_PARAM_INVALID); andre@0: } andre@0: andre@0: /* Verify zero-knowledge proof for B */ andre@0: if (crv == CKR_OK) andre@0: crv = jpake_Verify(arena, &pqg, hashType, &signerID, andre@0: peerID.data, peerID.len, ¶m->B); andre@0: if (crv == CKR_OK) { andre@0: SECItem B; andre@0: B.data = param->B.pGX; andre@0: B.len = param->B.ulGXLen; andre@0: K.data = NULL; andre@0: crv = jpake_mapStatus(JPAKE_Final(arena, &pqg.prime, &pqg.subPrime, andre@0: &x2, &gx4, &x2s, &B, &K), andre@0: CKR_MECHANISM_PARAM_INVALID); andre@0: } andre@0: andre@0: /* Save key material into CKA_VALUE. */ andre@0: if (crv == CKR_OK) andre@0: crv = sftk_forceAttribute(key, CKA_VALUE, K.data, K.len); andre@0: andre@0: if (crv == CKR_OK) andre@0: crv = jpake_enforceKeyType(key, CKK_GENERIC_SECRET); andre@0: andre@0: PORT_FreeArena(arena, PR_TRUE); andre@0: return crv; andre@0: }