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