1 /* crypto/srp/srp_vfy.c */
3 * Written by Christophe Renou (christophe.renou@edelweb.fr) with the
4 * precious help of Peter Sylvester (peter.sylvester@edelweb.fr) for the
5 * EdelKey project and contributed to the OpenSSL project 2004.
7 /* ====================================================================
8 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * 3. All advertising materials mentioning features or use of this
23 * software must display the following acknowledgment:
24 * "This product includes software developed by the OpenSSL Project
25 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
27 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28 * endorse or promote products derived from this software without
29 * prior written permission. For written permission, please contact
30 * licensing@OpenSSL.org.
32 * 5. Products derived from this software may not be called "OpenSSL"
33 * nor may "OpenSSL" appear in their names without prior written
34 * permission of the OpenSSL Project.
36 * 6. Redistributions of any form whatsoever must retain the following
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
41 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52 * OF THE POSSIBILITY OF SUCH DAMAGE.
53 * ====================================================================
55 * This product includes cryptographic software written by Eric Young
56 * (eay@cryptsoft.com). This product includes software written by Tim
57 * Hudson (tjh@cryptsoft.com).
60 #ifndef OPENSSL_NO_SRP
61 # include "cryptlib.h"
63 # include <openssl/srp.h>
64 # include <openssl/evp.h>
65 # include <openssl/buffer.h>
66 # include <openssl/rand.h>
67 # include <openssl/txt_db.h>
69 # define SRP_RANDOM_SALT_LEN 20
72 static char b64table[] =
73 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
76 * the following two conversion routines have been inspired by code from
81 * Convert a base64 string into raw byte array representation.
83 static int t_fromb64(unsigned char *a, size_t alen, const char *src)
89 if (alen == 0 || alen > INT_MAX)
92 while (*src && (*src == ' ' || *src == '\t' || *src == '\n'))
95 if (size < 0 || size >= (int)alen)
100 loc = strchr(b64table, src[i]);
101 if (loc == (char *)0)
104 a[i] = loc - b64table;
107 /* if nothing valid to process we have a zero length response */
117 a[j] |= (a[i] & 3) << 6;
119 a[j] = (unsigned char)((a[i] & 0x3c) >> 2);
122 a[j] |= (a[i] & 0xf) << 4;
124 a[j] = (unsigned char)((a[i] & 0x30) >> 4);
133 while (j <= size && a[j] == 0)
142 * Convert a raw byte string into a null-terminated base64 ASCII string.
144 static char *t_tob64(char *dst, const unsigned char *src, int size)
146 int c, pos = size % 3;
147 unsigned char b0 = 0, b1 = 0, b2 = 0, notleading = 0;
161 c = (b0 & 0xfc) >> 2;
162 if (notleading || c != 0) {
163 *dst++ = b64table[c];
166 c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4);
167 if (notleading || c != 0) {
168 *dst++ = b64table[c];
171 c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6);
172 if (notleading || c != 0) {
173 *dst++ = b64table[c];
177 if (notleading || c != 0) {
178 *dst++ = b64table[c];
194 void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
196 if (user_pwd == NULL)
198 BN_free(user_pwd->s);
199 BN_clear_free(user_pwd->v);
200 OPENSSL_free(user_pwd->id);
201 OPENSSL_free(user_pwd->info);
202 OPENSSL_free(user_pwd);
205 static SRP_user_pwd *SRP_user_pwd_new()
207 SRP_user_pwd *ret = OPENSSL_malloc(sizeof(SRP_user_pwd));
219 static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
226 static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
229 if (id != NULL && NULL == (vinfo->id = BUF_strdup(id)))
231 return (info == NULL || NULL != (vinfo->info = BUF_strdup(info)));
234 static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
237 unsigned char tmp[MAX_LEN];
243 len = t_fromb64(tmp, sizeof(tmp), v);
246 if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
248 len = t_fromb64(tmp, sizeof(tmp), s);
251 vinfo->s = BN_bin2bn(tmp, len, NULL);
252 if (vinfo->s == NULL)
261 static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
265 return (vinfo->s != NULL && vinfo->v != NULL);
268 static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
274 if ((ret = SRP_user_pwd_new()) == NULL)
277 SRP_user_pwd_set_gN(ret, src->g, src->N);
278 if (!SRP_user_pwd_set_ids(ret, src->id, src->info)
279 || !SRP_user_pwd_set_sv_BN(ret, BN_dup(src->s), BN_dup(src->v))) {
280 SRP_user_pwd_free(ret);
286 SRP_VBASE *SRP_VBASE_new(char *seed_key)
288 SRP_VBASE *vb = (SRP_VBASE *)OPENSSL_malloc(sizeof(SRP_VBASE));
292 if (!(vb->users_pwd = sk_SRP_user_pwd_new_null()) ||
293 !(vb->gN_cache = sk_SRP_gN_cache_new_null())) {
297 vb->default_g = NULL;
298 vb->default_N = NULL;
300 if ((seed_key != NULL) && (vb->seed_key = BUF_strdup(seed_key)) == NULL) {
301 sk_SRP_user_pwd_free(vb->users_pwd);
302 sk_SRP_gN_cache_free(vb->gN_cache);
309 int SRP_VBASE_free(SRP_VBASE *vb)
311 sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
312 sk_SRP_gN_cache_free(vb->gN_cache);
313 OPENSSL_free(vb->seed_key);
318 static SRP_gN_cache *SRP_gN_new_init(const char *ch)
320 unsigned char tmp[MAX_LEN];
323 SRP_gN_cache *newgN =
324 (SRP_gN_cache *)OPENSSL_malloc(sizeof(SRP_gN_cache));
328 len = t_fromb64(tmp, sizeof(tmp), ch);
332 if ((newgN->b64_bn = BUF_strdup(ch)) == NULL)
335 if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
338 OPENSSL_free(newgN->b64_bn);
344 static void SRP_gN_free(SRP_gN_cache *gN_cache)
346 if (gN_cache == NULL)
348 OPENSSL_free(gN_cache->b64_bn);
349 BN_free(gN_cache->bn);
350 OPENSSL_free(gN_cache);
353 static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
359 for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
360 gN = sk_SRP_gN_value(gN_tab, i);
361 if (gN && (id == NULL || strcmp(gN->id, id) == 0))
365 return SRP_get_default_gN(id);
368 static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
371 if (gN_cache == NULL)
374 /* search if we have already one... */
375 for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
376 SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
377 if (strcmp(cache->b64_bn, ch) == 0)
380 { /* it is the first time that we find it */
381 SRP_gN_cache *newgN = SRP_gN_new_init(ch);
383 if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
392 * this function parses verifier file. Format is:
393 * string(index):base64(N):base64(g):0
394 * string(username):base64(v):base64(salt):int(index)
397 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
400 STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
401 char *last_index = NULL;
406 SRP_user_pwd *user_pwd = NULL;
408 TXT_DB *tmpdb = NULL;
409 BIO *in = BIO_new(BIO_s_file());
411 error_code = SRP_ERR_OPEN_FILE;
413 if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
416 error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
418 if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
421 error_code = SRP_ERR_MEMORY;
424 last_index = SRP_get_default_gN(NULL)->id;
426 for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
427 pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
428 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
430 * we add this couple in the internal Stack
433 if ((gN = (SRP_gN *) OPENSSL_malloc(sizeof(SRP_gN))) == NULL)
436 if (!(gN->id = BUF_strdup(pp[DB_srpid]))
438 SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
439 || !(gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
440 || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
445 if (vb->seed_key != NULL) {
446 last_index = pp[DB_srpid];
448 } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
449 /* it is a user .... */
451 if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
452 error_code = SRP_ERR_MEMORY;
453 if ((user_pwd = SRP_user_pwd_new()) == NULL)
456 SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
457 if (!SRP_user_pwd_set_ids
458 (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
461 error_code = SRP_ERR_VBASE_BN_LIB;
462 if (!SRP_user_pwd_set_sv
463 (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
466 if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
468 user_pwd = NULL; /* abandon responsability */
473 if (last_index != NULL) {
474 /* this means that we want to simulate a default user */
476 if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
477 error_code = SRP_ERR_VBASE_BN_LIB;
480 vb->default_g = gN->g;
481 vb->default_N = gN->N;
484 error_code = SRP_NO_ERROR;
488 * there may be still some leaks to fix, if this fails, the application
489 * terminates most likely
493 OPENSSL_free(gN->id);
497 SRP_user_pwd_free(user_pwd);
504 sk_SRP_gN_free(SRP_gN_tab);
510 static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
518 for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
519 user = sk_SRP_user_pwd_value(vb->users_pwd, i);
520 if (strcmp(user->id, username) == 0)
528 * This method ignores the configured seed and fails for an unknown user.
529 * Ownership of the returned pointer is not released to the caller.
530 * In other words, caller must not free the result.
532 SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
534 return find_user(vb, username);
538 * Ownership of the returned pointer is released to the caller.
539 * In other words, caller must free the result once done.
541 SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
544 unsigned char digv[SHA_DIGEST_LENGTH];
545 unsigned char digs[SHA_DIGEST_LENGTH];
551 if ((user = find_user(vb, username)) != NULL)
552 return srp_user_pwd_dup(user);
554 if ((vb->seed_key == NULL) ||
555 (vb->default_g == NULL) || (vb->default_N == NULL))
558 /* if the user is unknown we set parameters as well if we have a seed_key */
560 if ((user = SRP_user_pwd_new()) == NULL)
563 SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
565 if (!SRP_user_pwd_set_ids(user, username, NULL))
568 if (RAND_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
570 EVP_MD_CTX_init(&ctxt);
571 EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
572 EVP_DigestUpdate(&ctxt, vb->seed_key, strlen(vb->seed_key));
573 EVP_DigestUpdate(&ctxt, username, strlen(username));
574 EVP_DigestFinal_ex(&ctxt, digs, NULL);
575 EVP_MD_CTX_cleanup(&ctxt);
576 if (SRP_user_pwd_set_sv_BN
577 (user, BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
578 BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
581 err:SRP_user_pwd_free(user);
586 * create a verifier (*salt,*verifier,g and N are in base64)
588 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
589 char **verifier, const char *N, const char *g)
592 char *result = NULL, *vf = NULL;
593 BIGNUM *N_bn = NULL, *g_bn = NULL, *s = NULL, *v = NULL;
594 unsigned char tmp[MAX_LEN];
595 unsigned char tmp2[MAX_LEN];
596 char *defgNid = NULL;
599 if ((user == NULL) ||
600 (pass == NULL) || (salt == NULL) || (verifier == NULL))
604 if (!(len = t_fromb64(tmp, sizeof(tmp), N)))
606 N_bn = BN_bin2bn(tmp, len, NULL);
607 if (!(len = t_fromb64(tmp, sizeof(tmp), g)))
609 g_bn = BN_bin2bn(tmp, len, NULL);
612 SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
621 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
624 s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
626 if (!(len = t_fromb64(tmp2, sizeof(tmp2), *salt)))
628 s = BN_bin2bn(tmp2, len, NULL);
631 if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
635 vfsize = BN_num_bytes(v) * 2;
636 if (((vf = OPENSSL_malloc(vfsize)) == NULL))
638 t_tob64(vf, tmp, BN_num_bytes(v));
643 if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
646 t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);
660 OPENSSL_cleanse(vf, vfsize);
668 * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
669 * then the provided salt will be used. On successful exit *verifier will point
670 * to a newly allocated BIGNUM containing the verifier and (if a salt was not
671 * provided) *salt will be populated with a newly allocated BIGNUM containing a
673 * The caller is responsible for freeing the allocated *salt and *verifier
676 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
677 BIGNUM **verifier, BIGNUM *N, BIGNUM *g)
681 BN_CTX *bn_ctx = BN_CTX_new();
682 unsigned char tmp2[MAX_LEN];
683 BIGNUM *salttmp = NULL;
685 if ((user == NULL) ||
688 (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
695 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
698 salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
703 x = SRP_Calc_x(salttmp, user, pass);
705 *verifier = BN_new();
706 if (*verifier == NULL)
709 if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
710 BN_clear_free(*verifier);
714 srp_bn_print(*verifier);
720 if (*salt != salttmp)
721 BN_clear_free(salttmp);