2 * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
10 * Originally written by Christophe Renou and Peter Sylvester,
11 * for the EdelKey project.
14 #ifndef OPENSSL_NO_SRP
15 # include "internal/cryptlib.h"
16 # include <openssl/sha.h>
17 # include <openssl/srp.h>
18 # include <openssl/evp.h>
19 # include <openssl/buffer.h>
20 # include <openssl/rand.h>
21 # include <openssl/txt_db.h>
23 # define SRP_RANDOM_SALT_LEN 20
27 * Convert a base64 string into raw byte array representation.
29 static int t_fromb64(unsigned char *a, size_t alen, const char *src)
31 size_t size = strlen(src);
33 /* Four bytes in src become three bytes output. */
34 if (size > INT_MAX || (size / 4) * 3 > alen)
37 return EVP_DecodeBlock(a, (unsigned char *)src, (int)size);
41 * Convert a raw byte string into a null-terminated base64 ASCII string.
43 static void t_tob64(char *dst, const unsigned char *src, int size)
45 EVP_EncodeBlock((unsigned char *)dst, src, size);
48 void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
53 BN_clear_free(user_pwd->v);
54 OPENSSL_free(user_pwd->id);
55 OPENSSL_free(user_pwd->info);
56 OPENSSL_free(user_pwd);
59 static SRP_user_pwd *SRP_user_pwd_new(void)
61 SRP_user_pwd *ret = OPENSSL_malloc(sizeof(*ret));
73 static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
80 static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
83 if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
85 return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
88 static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
91 unsigned char tmp[MAX_LEN];
97 len = t_fromb64(tmp, sizeof(tmp), v);
100 if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
102 len = t_fromb64(tmp, sizeof(tmp), s);
105 vinfo->s = BN_bin2bn(tmp, len, NULL);
106 if (vinfo->s == NULL)
115 static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
119 return (vinfo->s != NULL && vinfo->v != NULL);
122 static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
128 if ((ret = SRP_user_pwd_new()) == NULL)
131 SRP_user_pwd_set_gN(ret, src->g, src->N);
132 if (!SRP_user_pwd_set_ids(ret, src->id, src->info)
133 || !SRP_user_pwd_set_sv_BN(ret, BN_dup(src->s), BN_dup(src->v))) {
134 SRP_user_pwd_free(ret);
140 SRP_VBASE *SRP_VBASE_new(char *seed_key)
142 SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
146 if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
147 || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
151 vb->default_g = NULL;
152 vb->default_N = NULL;
154 if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
155 sk_SRP_user_pwd_free(vb->users_pwd);
156 sk_SRP_gN_cache_free(vb->gN_cache);
163 void SRP_VBASE_free(SRP_VBASE *vb)
167 sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
168 sk_SRP_gN_cache_free(vb->gN_cache);
169 OPENSSL_free(vb->seed_key);
173 static SRP_gN_cache *SRP_gN_new_init(const char *ch)
175 unsigned char tmp[MAX_LEN];
177 SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
182 len = t_fromb64(tmp, sizeof(tmp), ch);
186 if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
189 if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
192 OPENSSL_free(newgN->b64_bn);
198 static void SRP_gN_free(SRP_gN_cache *gN_cache)
200 if (gN_cache == NULL)
202 OPENSSL_free(gN_cache->b64_bn);
203 BN_free(gN_cache->bn);
204 OPENSSL_free(gN_cache);
207 static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
213 for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
214 gN = sk_SRP_gN_value(gN_tab, i);
215 if (gN && (id == NULL || strcmp(gN->id, id) == 0))
219 return SRP_get_default_gN(id);
222 static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
225 if (gN_cache == NULL)
228 /* search if we have already one... */
229 for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
230 SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
231 if (strcmp(cache->b64_bn, ch) == 0)
234 { /* it is the first time that we find it */
235 SRP_gN_cache *newgN = SRP_gN_new_init(ch);
237 if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
246 * this function parses verifier file. Format is:
247 * string(index):base64(N):base64(g):0
248 * string(username):base64(v):base64(salt):int(index)
251 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
254 STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
255 char *last_index = NULL;
260 SRP_user_pwd *user_pwd = NULL;
262 TXT_DB *tmpdb = NULL;
263 BIO *in = BIO_new(BIO_s_file());
265 error_code = SRP_ERR_OPEN_FILE;
267 if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
270 error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
272 if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
275 error_code = SRP_ERR_MEMORY;
278 last_index = SRP_get_default_gN(NULL)->id;
280 for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
281 pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
282 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
284 * we add this couple in the internal Stack
287 if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
290 if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
291 || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
293 || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
295 || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
300 if (vb->seed_key != NULL) {
301 last_index = pp[DB_srpid];
303 } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
304 /* it is a user .... */
307 if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
308 error_code = SRP_ERR_MEMORY;
309 if ((user_pwd = SRP_user_pwd_new()) == NULL)
312 SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
313 if (!SRP_user_pwd_set_ids
314 (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
317 error_code = SRP_ERR_VBASE_BN_LIB;
318 if (!SRP_user_pwd_set_sv
319 (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
322 if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
324 user_pwd = NULL; /* abandon responsibility */
329 if (last_index != NULL) {
330 /* this means that we want to simulate a default user */
332 if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
333 error_code = SRP_ERR_VBASE_BN_LIB;
336 vb->default_g = gN->g;
337 vb->default_N = gN->N;
340 error_code = SRP_NO_ERROR;
344 * there may be still some leaks to fix, if this fails, the application
345 * terminates most likely
349 OPENSSL_free(gN->id);
353 SRP_user_pwd_free(user_pwd);
358 sk_SRP_gN_free(SRP_gN_tab);
364 static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
372 for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
373 user = sk_SRP_user_pwd_value(vb->users_pwd, i);
374 if (strcmp(user->id, username) == 0)
381 # if OPENSSL_API_COMPAT < 0x10100000L
383 * DEPRECATED: use SRP_VBASE_get1_by_user instead.
384 * This method ignores the configured seed and fails for an unknown user.
385 * Ownership of the returned pointer is not released to the caller.
386 * In other words, caller must not free the result.
388 SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
390 return find_user(vb, username);
395 * Ownership of the returned pointer is released to the caller.
396 * In other words, caller must free the result once done.
398 SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
401 unsigned char digv[SHA_DIGEST_LENGTH];
402 unsigned char digs[SHA_DIGEST_LENGTH];
403 EVP_MD_CTX *ctxt = NULL;
408 if ((user = find_user(vb, username)) != NULL)
409 return srp_user_pwd_dup(user);
411 if ((vb->seed_key == NULL) ||
412 (vb->default_g == NULL) || (vb->default_N == NULL))
415 /* if the user is unknown we set parameters as well if we have a seed_key */
417 if ((user = SRP_user_pwd_new()) == NULL)
420 SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
422 if (!SRP_user_pwd_set_ids(user, username, NULL))
425 if (RAND_priv_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
427 ctxt = EVP_MD_CTX_new();
429 || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
430 || !EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key))
431 || !EVP_DigestUpdate(ctxt, username, strlen(username))
432 || !EVP_DigestFinal_ex(ctxt, digs, NULL))
434 EVP_MD_CTX_free(ctxt);
436 if (SRP_user_pwd_set_sv_BN(user,
437 BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
438 BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
442 EVP_MD_CTX_free(ctxt);
443 SRP_user_pwd_free(user);
448 * create a verifier (*salt,*verifier,g and N are in base64)
450 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
451 char **verifier, const char *N, const char *g)
454 char *result = NULL, *vf = NULL;
455 const BIGNUM *N_bn = NULL, *g_bn = NULL;
456 BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;
457 unsigned char tmp[MAX_LEN];
458 unsigned char tmp2[MAX_LEN];
459 char *defgNid = NULL;
462 if ((user == NULL) ||
463 (pass == NULL) || (salt == NULL) || (verifier == NULL))
467 if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)
469 N_bn_alloc = BN_bin2bn(tmp, len, NULL);
471 if ((len = t_fromb64(tmp, sizeof(tmp) ,g)) <= 0)
473 g_bn_alloc = BN_bin2bn(tmp, len, NULL);
477 SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
486 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
489 s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
491 if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)
493 s = BN_bin2bn(tmp2, len, NULL);
496 if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
500 vfsize = BN_num_bytes(v) * 2;
501 if (((vf = OPENSSL_malloc(vfsize)) == NULL))
503 t_tob64(vf, tmp, BN_num_bytes(v));
508 if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
511 t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);
522 OPENSSL_clear_free(vf, vfsize);
529 * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
530 * then the provided salt will be used. On successful exit *verifier will point
531 * to a newly allocated BIGNUM containing the verifier and (if a salt was not
532 * provided) *salt will be populated with a newly allocated BIGNUM containing a
534 * The caller is responsible for freeing the allocated *salt and *verifier
537 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
538 BIGNUM **verifier, const BIGNUM *N,
543 BN_CTX *bn_ctx = BN_CTX_new();
544 unsigned char tmp2[MAX_LEN];
545 BIGNUM *salttmp = NULL;
547 if ((user == NULL) ||
550 (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
554 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
557 salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
562 x = SRP_Calc_x(salttmp, user, pass);
564 *verifier = BN_new();
565 if (*verifier == NULL)
568 if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
569 BN_clear_free(*verifier);
577 if (salt != NULL && *salt != salttmp)
578 BN_clear_free(salttmp);