2 * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef OPENSSL_NO_SRP
11 # include "internal/cryptlib.h"
12 # include <openssl/sha.h>
13 # include <openssl/srp.h>
14 # include <openssl/evp.h>
15 # include <openssl/buffer.h>
16 # include <openssl/rand.h>
17 # include <openssl/txt_db.h>
19 # define SRP_RANDOM_SALT_LEN 20
22 static char b64table[] =
23 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
26 * the following two conversion routines have been inspired by code from
31 * Convert a base64 string into raw byte array representation.
33 static int t_fromb64(unsigned char *a, size_t alen, const char *src)
39 if (alen == 0 || alen > INT_MAX)
42 while (*src && (*src == ' ' || *src == '\t' || *src == '\n'))
45 if (size < 0 || size >= (int)alen)
50 loc = strchr(b64table, src[i]);
54 a[i] = loc - b64table;
57 /* if nothing valid to process we have a zero length response */
67 a[j] |= (a[i] & 3) << 6;
69 a[j] = (unsigned char)((a[i] & 0x3c) >> 2);
72 a[j] |= (a[i] & 0xf) << 4;
74 a[j] = (unsigned char)((a[i] & 0x30) >> 4);
83 while (j <= size && a[j] == 0)
92 * Convert a raw byte string into a null-terminated base64 ASCII string.
94 static char *t_tob64(char *dst, const unsigned char *src, int size)
96 int c, pos = size % 3;
97 unsigned char b0 = 0, b1 = 0, b2 = 0, notleading = 0;
111 c = (b0 & 0xfc) >> 2;
112 if (notleading || c != 0) {
113 *dst++ = b64table[c];
116 c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4);
117 if (notleading || c != 0) {
118 *dst++ = b64table[c];
121 c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6);
122 if (notleading || c != 0) {
123 *dst++ = b64table[c];
127 if (notleading || c != 0) {
128 *dst++ = b64table[c];
144 void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
146 if (user_pwd == NULL)
148 BN_free(user_pwd->s);
149 BN_clear_free(user_pwd->v);
150 OPENSSL_free(user_pwd->id);
151 OPENSSL_free(user_pwd->info);
152 OPENSSL_free(user_pwd);
155 static SRP_user_pwd *SRP_user_pwd_new(void)
157 SRP_user_pwd *ret = OPENSSL_malloc(sizeof(*ret));
169 static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
176 static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
179 if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
181 return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
184 static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
187 unsigned char tmp[MAX_LEN];
193 len = t_fromb64(tmp, sizeof(tmp), v);
196 if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
198 len = t_fromb64(tmp, sizeof(tmp), s);
201 vinfo->s = BN_bin2bn(tmp, len, NULL);
202 if (vinfo->s == NULL)
211 static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
215 return (vinfo->s != NULL && vinfo->v != NULL);
218 static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
224 if ((ret = SRP_user_pwd_new()) == NULL)
227 SRP_user_pwd_set_gN(ret, src->g, src->N);
228 if (!SRP_user_pwd_set_ids(ret, src->id, src->info)
229 || !SRP_user_pwd_set_sv_BN(ret, BN_dup(src->s), BN_dup(src->v))) {
230 SRP_user_pwd_free(ret);
236 SRP_VBASE *SRP_VBASE_new(char *seed_key)
238 SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
242 if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
243 || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
247 vb->default_g = NULL;
248 vb->default_N = NULL;
250 if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
251 sk_SRP_user_pwd_free(vb->users_pwd);
252 sk_SRP_gN_cache_free(vb->gN_cache);
259 void SRP_VBASE_free(SRP_VBASE *vb)
263 sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
264 sk_SRP_gN_cache_free(vb->gN_cache);
265 OPENSSL_free(vb->seed_key);
269 static SRP_gN_cache *SRP_gN_new_init(const char *ch)
271 unsigned char tmp[MAX_LEN];
273 SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
278 len = t_fromb64(tmp, sizeof(tmp), ch);
282 if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
285 if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
288 OPENSSL_free(newgN->b64_bn);
294 static void SRP_gN_free(SRP_gN_cache *gN_cache)
296 if (gN_cache == NULL)
298 OPENSSL_free(gN_cache->b64_bn);
299 BN_free(gN_cache->bn);
300 OPENSSL_free(gN_cache);
303 static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
309 for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
310 gN = sk_SRP_gN_value(gN_tab, i);
311 if (gN && (id == NULL || strcmp(gN->id, id) == 0))
315 return SRP_get_default_gN(id);
318 static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
321 if (gN_cache == NULL)
324 /* search if we have already one... */
325 for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
326 SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
327 if (strcmp(cache->b64_bn, ch) == 0)
330 { /* it is the first time that we find it */
331 SRP_gN_cache *newgN = SRP_gN_new_init(ch);
333 if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
342 * this function parses verifier file. Format is:
343 * string(index):base64(N):base64(g):0
344 * string(username):base64(v):base64(salt):int(index)
347 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
350 STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
351 char *last_index = NULL;
356 SRP_user_pwd *user_pwd = NULL;
358 TXT_DB *tmpdb = NULL;
359 BIO *in = BIO_new(BIO_s_file());
361 error_code = SRP_ERR_OPEN_FILE;
363 if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
366 error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
368 if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
371 error_code = SRP_ERR_MEMORY;
374 last_index = SRP_get_default_gN(NULL)->id;
376 for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
377 pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
378 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
380 * we add this couple in the internal Stack
383 if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
386 if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
387 || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
389 || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
391 || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
396 if (vb->seed_key != NULL) {
397 last_index = pp[DB_srpid];
399 } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
400 /* it is a user .... */
403 if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
404 error_code = SRP_ERR_MEMORY;
405 if ((user_pwd = SRP_user_pwd_new()) == NULL)
408 SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
409 if (!SRP_user_pwd_set_ids
410 (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
413 error_code = SRP_ERR_VBASE_BN_LIB;
414 if (!SRP_user_pwd_set_sv
415 (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
418 if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
420 user_pwd = NULL; /* abandon responsibility */
425 if (last_index != NULL) {
426 /* this means that we want to simulate a default user */
428 if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
429 error_code = SRP_ERR_VBASE_BN_LIB;
432 vb->default_g = gN->g;
433 vb->default_N = gN->N;
436 error_code = SRP_NO_ERROR;
440 * there may be still some leaks to fix, if this fails, the application
441 * terminates most likely
445 OPENSSL_free(gN->id);
449 SRP_user_pwd_free(user_pwd);
454 sk_SRP_gN_free(SRP_gN_tab);
460 static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
468 for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
469 user = sk_SRP_user_pwd_value(vb->users_pwd, i);
470 if (strcmp(user->id, username) == 0)
477 #if OPENSSL_API_COMPAT < 0x10100000L
479 * DEPRECATED: use SRP_VBASE_get1_by_user instead.
480 * This method ignores the configured seed and fails for an unknown user.
481 * Ownership of the returned pointer is not released to the caller.
482 * In other words, caller must not free the result.
484 SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
486 return find_user(vb, username);
491 * Ownership of the returned pointer is released to the caller.
492 * In other words, caller must free the result once done.
494 SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
497 unsigned char digv[SHA_DIGEST_LENGTH];
498 unsigned char digs[SHA_DIGEST_LENGTH];
499 EVP_MD_CTX *ctxt = NULL;
504 if ((user = find_user(vb, username)) != NULL)
505 return srp_user_pwd_dup(user);
507 if ((vb->seed_key == NULL) ||
508 (vb->default_g == NULL) || (vb->default_N == NULL))
511 /* if the user is unknown we set parameters as well if we have a seed_key */
513 if ((user = SRP_user_pwd_new()) == NULL)
516 SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
518 if (!SRP_user_pwd_set_ids(user, username, NULL))
521 if (RAND_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
523 ctxt = EVP_MD_CTX_new();
525 || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
526 || !EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key))
527 || !EVP_DigestUpdate(ctxt, username, strlen(username))
528 || !EVP_DigestFinal_ex(ctxt, digs, NULL))
530 EVP_MD_CTX_free(ctxt);
532 if (SRP_user_pwd_set_sv_BN(user,
533 BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
534 BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
538 EVP_MD_CTX_free(ctxt);
539 SRP_user_pwd_free(user);
544 * create a verifier (*salt,*verifier,g and N are in base64)
546 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
547 char **verifier, const char *N, const char *g)
550 char *result = NULL, *vf = NULL;
551 const BIGNUM *N_bn = NULL, *g_bn = NULL;
552 BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;
553 unsigned char tmp[MAX_LEN];
554 unsigned char tmp2[MAX_LEN];
555 char *defgNid = NULL;
558 if ((user == NULL) ||
559 (pass == NULL) || (salt == NULL) || (verifier == NULL))
563 if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)
565 N_bn_alloc = BN_bin2bn(tmp, len, NULL);
567 if ((len = t_fromb64(tmp, sizeof(tmp) ,g)) <= 0)
569 g_bn_alloc = BN_bin2bn(tmp, len, NULL);
573 SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
582 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
585 s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
587 if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)
589 s = BN_bin2bn(tmp2, len, NULL);
592 if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
596 vfsize = BN_num_bytes(v) * 2;
597 if (((vf = OPENSSL_malloc(vfsize)) == NULL))
599 t_tob64(vf, tmp, BN_num_bytes(v));
604 if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
607 t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);
618 OPENSSL_clear_free(vf, vfsize);
625 * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
626 * then the provided salt will be used. On successful exit *verifier will point
627 * to a newly allocated BIGNUM containing the verifier and (if a salt was not
628 * provided) *salt will be populated with a newly allocated BIGNUM containing a
630 * The caller is responsible for freeing the allocated *salt and *verifier
633 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
634 BIGNUM **verifier, const BIGNUM *N,
639 BN_CTX *bn_ctx = BN_CTX_new();
640 unsigned char tmp2[MAX_LEN];
641 BIGNUM *salttmp = NULL;
643 if ((user == NULL) ||
646 (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
650 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
653 salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
658 x = SRP_Calc_x(salttmp, user, pass);
660 *verifier = BN_new();
661 if (*verifier == NULL)
664 if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
665 BN_clear_free(*verifier);
673 if (salt != NULL && *salt != salttmp)
674 BN_clear_free(salttmp);