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 while (*src && (*src == ' ' || *src == '\t' || *src == '\n'))
42 if (alen > INT_MAX || size > (int)alen)
47 loc = strchr(b64table, src[i]);
51 a[i] = loc - b64table;
54 /* if nothing valid to process we have a zero length response */
64 a[j] |= (a[i] & 3) << 6;
66 a[j] = (unsigned char)((a[i] & 0x3c) >> 2);
69 a[j] |= (a[i] & 0xf) << 4;
71 a[j] = (unsigned char)((a[i] & 0x30) >> 4);
80 while (a[j] == 0 && j <= size)
89 * Convert a raw byte string into a null-terminated base64 ASCII string.
91 static char *t_tob64(char *dst, const unsigned char *src, int size)
93 int c, pos = size % 3;
94 unsigned char b0 = 0, b1 = 0, b2 = 0, notleading = 0;
108 c = (b0 & 0xfc) >> 2;
109 if (notleading || c != 0) {
110 *dst++ = b64table[c];
113 c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4);
114 if (notleading || c != 0) {
115 *dst++ = b64table[c];
118 c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6);
119 if (notleading || c != 0) {
120 *dst++ = b64table[c];
124 if (notleading || c != 0) {
125 *dst++ = b64table[c];
141 void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
143 if (user_pwd == NULL)
145 BN_free(user_pwd->s);
146 BN_clear_free(user_pwd->v);
147 OPENSSL_free(user_pwd->id);
148 OPENSSL_free(user_pwd->info);
149 OPENSSL_free(user_pwd);
152 static SRP_user_pwd *SRP_user_pwd_new(void)
154 SRP_user_pwd *ret = OPENSSL_malloc(sizeof(*ret));
166 static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
173 static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
176 if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
178 return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
181 static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
184 unsigned char tmp[MAX_LEN];
190 len = t_fromb64(tmp, sizeof(tmp), v);
193 if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
195 len = t_fromb64(tmp, sizeof(tmp), s);
198 vinfo->s = BN_bin2bn(tmp, len, NULL);
199 if (vinfo->s == NULL)
208 static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
212 return (vinfo->s != NULL && vinfo->v != NULL);
215 static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
221 if ((ret = SRP_user_pwd_new()) == NULL)
224 SRP_user_pwd_set_gN(ret, src->g, src->N);
225 if (!SRP_user_pwd_set_ids(ret, src->id, src->info)
226 || !SRP_user_pwd_set_sv_BN(ret, BN_dup(src->s), BN_dup(src->v))) {
227 SRP_user_pwd_free(ret);
233 SRP_VBASE *SRP_VBASE_new(char *seed_key)
235 SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
239 if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
240 || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
244 vb->default_g = NULL;
245 vb->default_N = NULL;
247 if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
248 sk_SRP_user_pwd_free(vb->users_pwd);
249 sk_SRP_gN_cache_free(vb->gN_cache);
256 void SRP_VBASE_free(SRP_VBASE *vb)
260 sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
261 sk_SRP_gN_cache_free(vb->gN_cache);
262 OPENSSL_free(vb->seed_key);
266 static SRP_gN_cache *SRP_gN_new_init(const char *ch)
268 unsigned char tmp[MAX_LEN];
270 SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
275 len = t_fromb64(tmp, sizeof(tmp), ch);
279 if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
282 if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
285 OPENSSL_free(newgN->b64_bn);
291 static void SRP_gN_free(SRP_gN_cache *gN_cache)
293 if (gN_cache == NULL)
295 OPENSSL_free(gN_cache->b64_bn);
296 BN_free(gN_cache->bn);
297 OPENSSL_free(gN_cache);
300 static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
306 for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
307 gN = sk_SRP_gN_value(gN_tab, i);
308 if (gN && (id == NULL || strcmp(gN->id, id) == 0))
312 return SRP_get_default_gN(id);
315 static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
318 if (gN_cache == NULL)
321 /* search if we have already one... */
322 for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
323 SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
324 if (strcmp(cache->b64_bn, ch) == 0)
327 { /* it is the first time that we find it */
328 SRP_gN_cache *newgN = SRP_gN_new_init(ch);
330 if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
339 * this function parses verifier file. Format is:
340 * string(index):base64(N):base64(g):0
341 * string(username):base64(v):base64(salt):int(index)
344 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
347 STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
348 char *last_index = NULL;
353 SRP_user_pwd *user_pwd = NULL;
355 TXT_DB *tmpdb = NULL;
356 BIO *in = BIO_new(BIO_s_file());
358 error_code = SRP_ERR_OPEN_FILE;
360 if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
363 error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
365 if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
368 error_code = SRP_ERR_MEMORY;
371 last_index = SRP_get_default_gN(NULL)->id;
373 for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
374 pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
375 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
377 * we add this couple in the internal Stack
380 if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
383 if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
384 || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
386 || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
388 || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
393 if (vb->seed_key != NULL) {
394 last_index = pp[DB_srpid];
396 } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
397 /* it is a user .... */
400 if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
401 error_code = SRP_ERR_MEMORY;
402 if ((user_pwd = SRP_user_pwd_new()) == NULL)
405 SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
406 if (!SRP_user_pwd_set_ids
407 (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
410 error_code = SRP_ERR_VBASE_BN_LIB;
411 if (!SRP_user_pwd_set_sv
412 (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
415 if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
417 user_pwd = NULL; /* abandon responsibility */
422 if (last_index != NULL) {
423 /* this means that we want to simulate a default user */
425 if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
426 error_code = SRP_ERR_VBASE_BN_LIB;
429 vb->default_g = gN->g;
430 vb->default_N = gN->N;
433 error_code = SRP_NO_ERROR;
437 * there may be still some leaks to fix, if this fails, the application
438 * terminates most likely
442 OPENSSL_free(gN->id);
446 SRP_user_pwd_free(user_pwd);
451 sk_SRP_gN_free(SRP_gN_tab);
457 static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
465 for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
466 user = sk_SRP_user_pwd_value(vb->users_pwd, i);
467 if (strcmp(user->id, username) == 0)
474 #if OPENSSL_API_COMPAT < 0x10100000L
476 * DEPRECATED: use SRP_VBASE_get1_by_user instead.
477 * This method ignores the configured seed and fails for an unknown user.
478 * Ownership of the returned pointer is not released to the caller.
479 * In other words, caller must not free the result.
481 SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
483 return find_user(vb, username);
488 * Ownership of the returned pointer is released to the caller.
489 * In other words, caller must free the result once done.
491 SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
494 unsigned char digv[SHA_DIGEST_LENGTH];
495 unsigned char digs[SHA_DIGEST_LENGTH];
496 EVP_MD_CTX *ctxt = NULL;
501 if ((user = find_user(vb, username)) != NULL)
502 return srp_user_pwd_dup(user);
504 if ((vb->seed_key == NULL) ||
505 (vb->default_g == NULL) || (vb->default_N == NULL))
508 /* if the user is unknown we set parameters as well if we have a seed_key */
510 if ((user = SRP_user_pwd_new()) == NULL)
513 SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
515 if (!SRP_user_pwd_set_ids(user, username, NULL))
518 if (RAND_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
520 ctxt = EVP_MD_CTX_new();
522 || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
523 || !EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key))
524 || !EVP_DigestUpdate(ctxt, username, strlen(username))
525 || !EVP_DigestFinal_ex(ctxt, digs, NULL))
527 EVP_MD_CTX_free(ctxt);
529 if (SRP_user_pwd_set_sv_BN(user,
530 BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
531 BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
535 EVP_MD_CTX_free(ctxt);
536 SRP_user_pwd_free(user);
541 * create a verifier (*salt,*verifier,g and N are in base64)
543 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
544 char **verifier, const char *N, const char *g)
547 char *result = NULL, *vf = NULL;
548 const BIGNUM *N_bn = NULL, *g_bn = NULL;
549 BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;
550 unsigned char tmp[MAX_LEN];
551 unsigned char tmp2[MAX_LEN];
552 char *defgNid = NULL;
555 if ((user == NULL) ||
556 (pass == NULL) || (salt == NULL) || (verifier == NULL))
560 if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)
562 N_bn_alloc = BN_bin2bn(tmp, len, NULL);
564 if ((len = t_fromb64(tmp, sizeof(tmp) ,g)) <= 0)
566 g_bn_alloc = BN_bin2bn(tmp, len, NULL);
570 SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
579 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
582 s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
584 if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)
586 s = BN_bin2bn(tmp2, len, NULL);
589 if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
593 vfsize = BN_num_bytes(v) * 2;
594 if (((vf = OPENSSL_malloc(vfsize)) == NULL))
596 t_tob64(vf, tmp, BN_num_bytes(v));
601 if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
604 t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);
615 OPENSSL_clear_free(vf, vfsize);
622 * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
623 * then the provided salt will be used. On successful exit *verifier will point
624 * to a newly allocated BIGNUM containing the verifier and (if a salt was not
625 * provided) *salt will be populated with a newly allocated BIGNUM containing a
627 * The caller is responsible for freeing the allocated *salt and *verifier
630 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
631 BIGNUM **verifier, const BIGNUM *N,
636 BN_CTX *bn_ctx = BN_CTX_new();
637 unsigned char tmp2[MAX_LEN];
638 BIGNUM *salttmp = NULL;
640 if ((user == NULL) ||
643 (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
647 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
650 salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
655 x = SRP_Calc_x(salttmp, user, pass);
657 *verifier = BN_new();
658 if (*verifier == NULL)
661 if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
662 BN_clear_free(*verifier);
670 if (salt != NULL && *salt != salttmp)
671 BN_clear_free(salttmp);