Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "seccomon.h" |
michael@0 | 6 | #include "secerr.h" |
michael@0 | 7 | #include "blapi.h" |
michael@0 | 8 | #include "pkcs11i.h" |
michael@0 | 9 | #include "softoken.h" |
michael@0 | 10 | |
michael@0 | 11 | static CK_RV |
michael@0 | 12 | jpake_mapStatus(SECStatus rv, CK_RV invalidArgsMapping) { |
michael@0 | 13 | int err; |
michael@0 | 14 | if (rv == SECSuccess) |
michael@0 | 15 | return CKR_OK; |
michael@0 | 16 | err = PORT_GetError(); |
michael@0 | 17 | switch (err) { |
michael@0 | 18 | /* XXX: SEC_ERROR_INVALID_ARGS might be caused by invalid template |
michael@0 | 19 | parameters. */ |
michael@0 | 20 | case SEC_ERROR_INVALID_ARGS: return invalidArgsMapping; |
michael@0 | 21 | case SEC_ERROR_BAD_SIGNATURE: return CKR_SIGNATURE_INVALID; |
michael@0 | 22 | case SEC_ERROR_NO_MEMORY: return CKR_HOST_MEMORY; |
michael@0 | 23 | } |
michael@0 | 24 | return CKR_FUNCTION_FAILED; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | /* If key is not NULL then the gx value will be stored as an attribute with |
michael@0 | 28 | the type given by the gxAttrType parameter. */ |
michael@0 | 29 | static CK_RV |
michael@0 | 30 | jpake_Sign(PLArenaPool * arena, const PQGParams * pqg, HASH_HashType hashType, |
michael@0 | 31 | const SECItem * signerID, const SECItem * x, |
michael@0 | 32 | CK_NSS_JPAKEPublicValue * out) |
michael@0 | 33 | { |
michael@0 | 34 | SECItem gx, gv, r; |
michael@0 | 35 | CK_RV crv; |
michael@0 | 36 | |
michael@0 | 37 | PORT_Assert(arena != NULL); |
michael@0 | 38 | |
michael@0 | 39 | gx.data = NULL; |
michael@0 | 40 | gv.data = NULL; |
michael@0 | 41 | r.data = NULL; |
michael@0 | 42 | crv = jpake_mapStatus(JPAKE_Sign(arena, pqg, hashType, signerID, x, NULL, |
michael@0 | 43 | NULL, &gx, &gv, &r), |
michael@0 | 44 | CKR_MECHANISM_PARAM_INVALID); |
michael@0 | 45 | if (crv == CKR_OK) { |
michael@0 | 46 | if ((out->pGX != NULL && out->ulGXLen >= gx.len) || |
michael@0 | 47 | (out->pGV != NULL && out->ulGVLen >= gv.len) || |
michael@0 | 48 | (out->pR != NULL && out->ulRLen >= r.len)) { |
michael@0 | 49 | PORT_Memcpy(out->pGX, gx.data, gx.len); |
michael@0 | 50 | PORT_Memcpy(out->pGV, gv.data, gv.len); |
michael@0 | 51 | PORT_Memcpy(out->pR, r.data, r.len); |
michael@0 | 52 | out->ulGXLen = gx.len; |
michael@0 | 53 | out->ulGVLen = gv.len; |
michael@0 | 54 | out->ulRLen = r.len; |
michael@0 | 55 | } else { |
michael@0 | 56 | crv = CKR_MECHANISM_PARAM_INVALID; |
michael@0 | 57 | } |
michael@0 | 58 | } |
michael@0 | 59 | return crv; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | static CK_RV |
michael@0 | 63 | jpake_Verify(PLArenaPool * arena, const PQGParams * pqg, |
michael@0 | 64 | HASH_HashType hashType, const SECItem * signerID, |
michael@0 | 65 | const CK_BYTE * peerIDData, CK_ULONG peerIDLen, |
michael@0 | 66 | const CK_NSS_JPAKEPublicValue * publicValueIn) |
michael@0 | 67 | { |
michael@0 | 68 | SECItem peerID, gx, gv, r; |
michael@0 | 69 | peerID.data = (unsigned char *) peerIDData; peerID.len = peerIDLen; |
michael@0 | 70 | gx.data = publicValueIn->pGX; gx.len = publicValueIn->ulGXLen; |
michael@0 | 71 | gv.data = publicValueIn->pGV; gv.len = publicValueIn->ulGVLen; |
michael@0 | 72 | r.data = publicValueIn->pR; r.len = publicValueIn->ulRLen; |
michael@0 | 73 | return jpake_mapStatus(JPAKE_Verify(arena, pqg, hashType, signerID, &peerID, |
michael@0 | 74 | &gx, &gv, &r), |
michael@0 | 75 | CKR_MECHANISM_PARAM_INVALID); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | #define NUM_ELEM(x) (sizeof (x) / sizeof (x)[0]) |
michael@0 | 79 | |
michael@0 | 80 | /* If the template has the key type set, ensure that it was set to the correct |
michael@0 | 81 | * value. If the template did not have the key type set, set it to the |
michael@0 | 82 | * correct value. |
michael@0 | 83 | */ |
michael@0 | 84 | static CK_RV |
michael@0 | 85 | jpake_enforceKeyType(SFTKObject * key, CK_KEY_TYPE keyType) { |
michael@0 | 86 | CK_RV crv; |
michael@0 | 87 | SFTKAttribute * keyTypeAttr = sftk_FindAttribute(key, CKA_KEY_TYPE); |
michael@0 | 88 | if (keyTypeAttr != NULL) { |
michael@0 | 89 | crv = *(CK_KEY_TYPE *)keyTypeAttr->attrib.pValue == keyType |
michael@0 | 90 | ? CKR_OK |
michael@0 | 91 | : CKR_TEMPLATE_INCONSISTENT; |
michael@0 | 92 | sftk_FreeAttribute(keyTypeAttr); |
michael@0 | 93 | } else { |
michael@0 | 94 | crv = sftk_forceAttribute(key, CKA_KEY_TYPE, &keyType, sizeof keyType); |
michael@0 | 95 | } |
michael@0 | 96 | return crv; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | static CK_RV |
michael@0 | 100 | jpake_MultipleSecItem2Attribute(SFTKObject * key, const SFTKItemTemplate * attrs, |
michael@0 | 101 | size_t attrsCount) |
michael@0 | 102 | { |
michael@0 | 103 | size_t i; |
michael@0 | 104 | |
michael@0 | 105 | for (i = 0; i < attrsCount; ++i) { |
michael@0 | 106 | CK_RV crv = sftk_forceAttribute(key, attrs[i].type, attrs[i].item->data, |
michael@0 | 107 | attrs[i].item->len); |
michael@0 | 108 | if (crv != CKR_OK) |
michael@0 | 109 | return crv; |
michael@0 | 110 | } |
michael@0 | 111 | return CKR_OK; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | CK_RV |
michael@0 | 115 | jpake_Round1(HASH_HashType hashType, CK_NSS_JPAKERound1Params * params, |
michael@0 | 116 | SFTKObject * key) |
michael@0 | 117 | { |
michael@0 | 118 | CK_RV crv; |
michael@0 | 119 | PQGParams pqg; |
michael@0 | 120 | PLArenaPool * arena; |
michael@0 | 121 | SECItem signerID; |
michael@0 | 122 | SFTKItemTemplate templateAttrs[] = { |
michael@0 | 123 | { CKA_PRIME, &pqg.prime }, |
michael@0 | 124 | { CKA_SUBPRIME, &pqg.subPrime }, |
michael@0 | 125 | { CKA_BASE, &pqg.base }, |
michael@0 | 126 | { CKA_NSS_JPAKE_SIGNERID, &signerID } |
michael@0 | 127 | }; |
michael@0 | 128 | SECItem x2, gx1, gx2; |
michael@0 | 129 | const SFTKItemTemplate generatedAttrs[] = { |
michael@0 | 130 | { CKA_NSS_JPAKE_X2, &x2 }, |
michael@0 | 131 | { CKA_NSS_JPAKE_GX1, &gx1 }, |
michael@0 | 132 | { CKA_NSS_JPAKE_GX2, &gx2 }, |
michael@0 | 133 | }; |
michael@0 | 134 | SECItem x1; |
michael@0 | 135 | |
michael@0 | 136 | PORT_Assert(params != NULL); |
michael@0 | 137 | PORT_Assert(key != NULL); |
michael@0 | 138 | |
michael@0 | 139 | arena = PORT_NewArena(NSS_SOFTOKEN_DEFAULT_CHUNKSIZE); |
michael@0 | 140 | if (arena == NULL) |
michael@0 | 141 | crv = CKR_HOST_MEMORY; |
michael@0 | 142 | |
michael@0 | 143 | crv = sftk_MultipleAttribute2SecItem(arena, key, templateAttrs, |
michael@0 | 144 | NUM_ELEM(templateAttrs)); |
michael@0 | 145 | |
michael@0 | 146 | if (crv == CKR_OK && (signerID.data == NULL || signerID.len == 0)) |
michael@0 | 147 | crv = CKR_TEMPLATE_INCOMPLETE; |
michael@0 | 148 | |
michael@0 | 149 | /* generate x1, g^x1 and the proof of knowledge of x1 */ |
michael@0 | 150 | if (crv == CKR_OK) { |
michael@0 | 151 | x1.data = NULL; |
michael@0 | 152 | crv = jpake_mapStatus(DSA_NewRandom(arena, &pqg.subPrime, &x1), |
michael@0 | 153 | CKR_TEMPLATE_INCONSISTENT); |
michael@0 | 154 | } |
michael@0 | 155 | if (crv == CKR_OK) |
michael@0 | 156 | crv = jpake_Sign(arena, &pqg, hashType, &signerID, &x1, ¶ms->gx1); |
michael@0 | 157 | |
michael@0 | 158 | /* generate x2, g^x2 and the proof of knowledge of x2 */ |
michael@0 | 159 | if (crv == CKR_OK) { |
michael@0 | 160 | x2.data = NULL; |
michael@0 | 161 | crv = jpake_mapStatus(DSA_NewRandom(arena, &pqg.subPrime, &x2), |
michael@0 | 162 | CKR_TEMPLATE_INCONSISTENT); |
michael@0 | 163 | } |
michael@0 | 164 | if (crv == CKR_OK) |
michael@0 | 165 | crv = jpake_Sign(arena, &pqg, hashType, &signerID, &x2, ¶ms->gx2); |
michael@0 | 166 | |
michael@0 | 167 | /* Save the values needed for round 2 into CKA_VALUE */ |
michael@0 | 168 | if (crv == CKR_OK) { |
michael@0 | 169 | gx1.data = params->gx1.pGX; |
michael@0 | 170 | gx1.len = params->gx1.ulGXLen; |
michael@0 | 171 | gx2.data = params->gx2.pGX; |
michael@0 | 172 | gx2.len = params->gx2.ulGXLen; |
michael@0 | 173 | crv = jpake_MultipleSecItem2Attribute(key, generatedAttrs, |
michael@0 | 174 | NUM_ELEM(generatedAttrs)); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | PORT_FreeArena(arena, PR_TRUE); |
michael@0 | 178 | return crv; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | CK_RV |
michael@0 | 182 | jpake_Round2(HASH_HashType hashType, CK_NSS_JPAKERound2Params * params, |
michael@0 | 183 | SFTKObject * sourceKey, SFTKObject * key) |
michael@0 | 184 | { |
michael@0 | 185 | CK_RV crv; |
michael@0 | 186 | PLArenaPool * arena; |
michael@0 | 187 | PQGParams pqg; |
michael@0 | 188 | SECItem signerID, x2, gx1, gx2; |
michael@0 | 189 | SFTKItemTemplate sourceAttrs[] = { |
michael@0 | 190 | { CKA_PRIME, &pqg.prime }, |
michael@0 | 191 | { CKA_SUBPRIME, &pqg.subPrime }, |
michael@0 | 192 | { CKA_BASE, &pqg.base }, |
michael@0 | 193 | { CKA_NSS_JPAKE_SIGNERID, &signerID }, |
michael@0 | 194 | { CKA_NSS_JPAKE_X2, &x2 }, |
michael@0 | 195 | { CKA_NSS_JPAKE_GX1, &gx1 }, |
michael@0 | 196 | { CKA_NSS_JPAKE_GX2, &gx2 }, |
michael@0 | 197 | }; |
michael@0 | 198 | SECItem x2s, gx3, gx4; |
michael@0 | 199 | const SFTKItemTemplate copiedAndGeneratedAttrs[] = { |
michael@0 | 200 | { CKA_NSS_JPAKE_SIGNERID, &signerID }, |
michael@0 | 201 | { CKA_PRIME, &pqg.prime }, |
michael@0 | 202 | { CKA_SUBPRIME, &pqg.subPrime }, |
michael@0 | 203 | { CKA_NSS_JPAKE_X2, &x2 }, |
michael@0 | 204 | { CKA_NSS_JPAKE_X2S, &x2s }, |
michael@0 | 205 | { CKA_NSS_JPAKE_GX1, &gx1 }, |
michael@0 | 206 | { CKA_NSS_JPAKE_GX2, &gx2 }, |
michael@0 | 207 | { CKA_NSS_JPAKE_GX3, &gx3 }, |
michael@0 | 208 | { CKA_NSS_JPAKE_GX4, &gx4 } |
michael@0 | 209 | }; |
michael@0 | 210 | SECItem peerID; |
michael@0 | 211 | |
michael@0 | 212 | PORT_Assert(params != NULL); |
michael@0 | 213 | PORT_Assert(sourceKey != NULL); |
michael@0 | 214 | PORT_Assert(key != NULL); |
michael@0 | 215 | |
michael@0 | 216 | arena = PORT_NewArena(NSS_SOFTOKEN_DEFAULT_CHUNKSIZE); |
michael@0 | 217 | if (arena == NULL) |
michael@0 | 218 | crv = CKR_HOST_MEMORY; |
michael@0 | 219 | |
michael@0 | 220 | /* TODO: check CKK_NSS_JPAKE_ROUND1 */ |
michael@0 | 221 | |
michael@0 | 222 | crv = sftk_MultipleAttribute2SecItem(arena, sourceKey, sourceAttrs, |
michael@0 | 223 | NUM_ELEM(sourceAttrs)); |
michael@0 | 224 | |
michael@0 | 225 | /* Get the peer's ID out of the template and sanity-check it. */ |
michael@0 | 226 | if (crv == CKR_OK) |
michael@0 | 227 | crv = sftk_Attribute2SecItem(arena, &peerID, key, |
michael@0 | 228 | CKA_NSS_JPAKE_PEERID); |
michael@0 | 229 | if (crv == CKR_OK && (peerID.data == NULL || peerID.len == 0)) |
michael@0 | 230 | crv = CKR_TEMPLATE_INCOMPLETE; |
michael@0 | 231 | if (crv == CKR_OK && SECITEM_CompareItem(&signerID, &peerID) == SECEqual) |
michael@0 | 232 | crv = CKR_TEMPLATE_INCONSISTENT; |
michael@0 | 233 | |
michael@0 | 234 | /* Verify zero-knowledge proofs for g^x3 and g^x4 */ |
michael@0 | 235 | if (crv == CKR_OK) |
michael@0 | 236 | crv = jpake_Verify(arena, &pqg, hashType, &signerID, |
michael@0 | 237 | peerID.data, peerID.len, ¶ms->gx3); |
michael@0 | 238 | if (crv == CKR_OK) |
michael@0 | 239 | crv = jpake_Verify(arena, &pqg, hashType, &signerID, |
michael@0 | 240 | peerID.data, peerID.len, ¶ms->gx4); |
michael@0 | 241 | |
michael@0 | 242 | /* Calculate the base and x2s for A=base^x2s */ |
michael@0 | 243 | if (crv == CKR_OK) { |
michael@0 | 244 | SECItem s; |
michael@0 | 245 | s.data = params->pSharedKey; |
michael@0 | 246 | s.len = params->ulSharedKeyLen; |
michael@0 | 247 | gx3.data = params->gx3.pGX; |
michael@0 | 248 | gx3.len = params->gx3.ulGXLen; |
michael@0 | 249 | gx4.data = params->gx4.pGX; |
michael@0 | 250 | gx4.len = params->gx4.ulGXLen; |
michael@0 | 251 | pqg.base.data = NULL; |
michael@0 | 252 | x2s.data = NULL; |
michael@0 | 253 | crv = jpake_mapStatus(JPAKE_Round2(arena, &pqg.prime, &pqg.subPrime, |
michael@0 | 254 | &gx1, &gx3, &gx4, &pqg.base, |
michael@0 | 255 | &x2, &s, &x2s), |
michael@0 | 256 | CKR_MECHANISM_PARAM_INVALID); |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | /* Generate A=base^x2s and its zero-knowledge proof. */ |
michael@0 | 260 | if (crv == CKR_OK) |
michael@0 | 261 | crv = jpake_Sign(arena, &pqg, hashType, &signerID, &x2s, ¶ms->A); |
michael@0 | 262 | |
michael@0 | 263 | /* Copy P and Q from the ROUND1 key to the ROUND2 key and save the values |
michael@0 | 264 | needed for the final key material derivation into CKA_VALUE. */ |
michael@0 | 265 | if (crv == CKR_OK) |
michael@0 | 266 | crv = sftk_forceAttribute(key, CKA_PRIME, pqg.prime.data, |
michael@0 | 267 | pqg.prime.len); |
michael@0 | 268 | if (crv == CKR_OK) |
michael@0 | 269 | crv = sftk_forceAttribute(key, CKA_SUBPRIME, pqg.subPrime.data, |
michael@0 | 270 | pqg.subPrime.len); |
michael@0 | 271 | if (crv == CKR_OK) { |
michael@0 | 272 | crv = jpake_MultipleSecItem2Attribute(key, copiedAndGeneratedAttrs, |
michael@0 | 273 | NUM_ELEM(copiedAndGeneratedAttrs)); |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | if (crv == CKR_OK) |
michael@0 | 277 | crv = jpake_enforceKeyType(key, CKK_NSS_JPAKE_ROUND2); |
michael@0 | 278 | |
michael@0 | 279 | PORT_FreeArena(arena, PR_TRUE); |
michael@0 | 280 | return crv; |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | CK_RV |
michael@0 | 284 | jpake_Final(HASH_HashType hashType, const CK_NSS_JPAKEFinalParams * param, |
michael@0 | 285 | SFTKObject * sourceKey, SFTKObject * key) |
michael@0 | 286 | { |
michael@0 | 287 | PLArenaPool * arena; |
michael@0 | 288 | SECItem K; |
michael@0 | 289 | PQGParams pqg; |
michael@0 | 290 | CK_RV crv; |
michael@0 | 291 | SECItem peerID, signerID, x2s, x2, gx1, gx2, gx3, gx4; |
michael@0 | 292 | SFTKItemTemplate sourceAttrs[] = { |
michael@0 | 293 | { CKA_NSS_JPAKE_PEERID, &peerID }, |
michael@0 | 294 | { CKA_NSS_JPAKE_SIGNERID, &signerID }, |
michael@0 | 295 | { CKA_PRIME, &pqg.prime }, |
michael@0 | 296 | { CKA_SUBPRIME, &pqg.subPrime }, |
michael@0 | 297 | { CKA_NSS_JPAKE_X2, &x2 }, |
michael@0 | 298 | { CKA_NSS_JPAKE_X2S, &x2s }, |
michael@0 | 299 | { CKA_NSS_JPAKE_GX1, &gx1 }, |
michael@0 | 300 | { CKA_NSS_JPAKE_GX2, &gx2 }, |
michael@0 | 301 | { CKA_NSS_JPAKE_GX3, &gx3 }, |
michael@0 | 302 | { CKA_NSS_JPAKE_GX4, &gx4 } |
michael@0 | 303 | }; |
michael@0 | 304 | |
michael@0 | 305 | PORT_Assert(param != NULL); |
michael@0 | 306 | PORT_Assert(sourceKey != NULL); |
michael@0 | 307 | PORT_Assert(key != NULL); |
michael@0 | 308 | |
michael@0 | 309 | arena = PORT_NewArena(NSS_SOFTOKEN_DEFAULT_CHUNKSIZE); |
michael@0 | 310 | if (arena == NULL) |
michael@0 | 311 | crv = CKR_HOST_MEMORY; |
michael@0 | 312 | |
michael@0 | 313 | /* TODO: verify key type CKK_NSS_JPAKE_ROUND2 */ |
michael@0 | 314 | |
michael@0 | 315 | crv = sftk_MultipleAttribute2SecItem(arena, sourceKey, sourceAttrs, |
michael@0 | 316 | NUM_ELEM(sourceAttrs)); |
michael@0 | 317 | |
michael@0 | 318 | /* Calculate base for B=base^x4s */ |
michael@0 | 319 | if (crv == CKR_OK) { |
michael@0 | 320 | pqg.base.data = NULL; |
michael@0 | 321 | crv = jpake_mapStatus(JPAKE_Round2(arena, &pqg.prime, &pqg.subPrime, |
michael@0 | 322 | &gx1, &gx2, &gx3, &pqg.base, |
michael@0 | 323 | NULL, NULL, NULL), |
michael@0 | 324 | CKR_MECHANISM_PARAM_INVALID); |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | /* Verify zero-knowledge proof for B */ |
michael@0 | 328 | if (crv == CKR_OK) |
michael@0 | 329 | crv = jpake_Verify(arena, &pqg, hashType, &signerID, |
michael@0 | 330 | peerID.data, peerID.len, ¶m->B); |
michael@0 | 331 | if (crv == CKR_OK) { |
michael@0 | 332 | SECItem B; |
michael@0 | 333 | B.data = param->B.pGX; |
michael@0 | 334 | B.len = param->B.ulGXLen; |
michael@0 | 335 | K.data = NULL; |
michael@0 | 336 | crv = jpake_mapStatus(JPAKE_Final(arena, &pqg.prime, &pqg.subPrime, |
michael@0 | 337 | &x2, &gx4, &x2s, &B, &K), |
michael@0 | 338 | CKR_MECHANISM_PARAM_INVALID); |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | /* Save key material into CKA_VALUE. */ |
michael@0 | 342 | if (crv == CKR_OK) |
michael@0 | 343 | crv = sftk_forceAttribute(key, CKA_VALUE, K.data, K.len); |
michael@0 | 344 | |
michael@0 | 345 | if (crv == CKR_OK) |
michael@0 | 346 | crv = jpake_enforceKeyType(key, CKK_GENERIC_SECRET); |
michael@0 | 347 | |
michael@0 | 348 | PORT_FreeArena(arena, PR_TRUE); |
michael@0 | 349 | return crv; |
michael@0 | 350 | } |