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
23 * Convert a base64 string into raw byte array representation.
25 static int t_fromb64(unsigned char *a, size_t alen, const char *src)
27 size_t size = strlen(src);
29 /* Four bytes in src become three bytes output. */
30 if (size > INT_MAX || (size / 4) * 3 > alen)
33 return EVP_DecodeBlock(a, (unsigned char *)src, (int)size);
37 * Convert a raw byte string into a null-terminated base64 ASCII string.
39 static void t_tob64(char *dst, const unsigned char *src, int size)
41 EVP_EncodeBlock((unsigned char *)dst, src, size);
44 void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
49 BN_clear_free(user_pwd->v);
50 OPENSSL_free(user_pwd->id);
51 OPENSSL_free(user_pwd->info);
52 OPENSSL_free(user_pwd);
55 static SRP_user_pwd *SRP_user_pwd_new(void)
57 SRP_user_pwd *ret = OPENSSL_malloc(sizeof(*ret));
69 static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
76 static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
79 if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
81 return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
84 static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
87 unsigned char tmp[MAX_LEN];
93 len = t_fromb64(tmp, sizeof(tmp), v);
96 if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
98 len = t_fromb64(tmp, sizeof(tmp), s);
101 vinfo->s = BN_bin2bn(tmp, len, NULL);
102 if (vinfo->s == NULL)
111 static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
115 return (vinfo->s != NULL && vinfo->v != NULL);
118 static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
124 if ((ret = SRP_user_pwd_new()) == NULL)
127 SRP_user_pwd_set_gN(ret, src->g, src->N);
128 if (!SRP_user_pwd_set_ids(ret, src->id, src->info)
129 || !SRP_user_pwd_set_sv_BN(ret, BN_dup(src->s), BN_dup(src->v))) {
130 SRP_user_pwd_free(ret);
136 SRP_VBASE *SRP_VBASE_new(char *seed_key)
138 SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
142 if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
143 || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
147 vb->default_g = NULL;
148 vb->default_N = NULL;
150 if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
151 sk_SRP_user_pwd_free(vb->users_pwd);
152 sk_SRP_gN_cache_free(vb->gN_cache);
159 void SRP_VBASE_free(SRP_VBASE *vb)
163 sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
164 sk_SRP_gN_cache_free(vb->gN_cache);
165 OPENSSL_free(vb->seed_key);
169 static SRP_gN_cache *SRP_gN_new_init(const char *ch)
171 unsigned char tmp[MAX_LEN];
173 SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
178 len = t_fromb64(tmp, sizeof(tmp), ch);
182 if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
185 if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
188 OPENSSL_free(newgN->b64_bn);
194 static void SRP_gN_free(SRP_gN_cache *gN_cache)
196 if (gN_cache == NULL)
198 OPENSSL_free(gN_cache->b64_bn);
199 BN_free(gN_cache->bn);
200 OPENSSL_free(gN_cache);
203 static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
209 for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
210 gN = sk_SRP_gN_value(gN_tab, i);
211 if (gN && (id == NULL || strcmp(gN->id, id) == 0))
215 return SRP_get_default_gN(id);
218 static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
221 if (gN_cache == NULL)
224 /* search if we have already one... */
225 for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
226 SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
227 if (strcmp(cache->b64_bn, ch) == 0)
230 { /* it is the first time that we find it */
231 SRP_gN_cache *newgN = SRP_gN_new_init(ch);
233 if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
242 * this function parses verifier file. Format is:
243 * string(index):base64(N):base64(g):0
244 * string(username):base64(v):base64(salt):int(index)
247 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
250 STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
251 char *last_index = NULL;
256 SRP_user_pwd *user_pwd = NULL;
258 TXT_DB *tmpdb = NULL;
259 BIO *in = BIO_new(BIO_s_file());
261 error_code = SRP_ERR_OPEN_FILE;
263 if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
266 error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
268 if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
271 error_code = SRP_ERR_MEMORY;
274 last_index = SRP_get_default_gN(NULL)->id;
276 for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
277 pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
278 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
280 * we add this couple in the internal Stack
283 if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
286 if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
287 || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
289 || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
291 || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
296 if (vb->seed_key != NULL) {
297 last_index = pp[DB_srpid];
299 } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
300 /* it is a user .... */
303 if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
304 error_code = SRP_ERR_MEMORY;
305 if ((user_pwd = SRP_user_pwd_new()) == NULL)
308 SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
309 if (!SRP_user_pwd_set_ids
310 (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
313 error_code = SRP_ERR_VBASE_BN_LIB;
314 if (!SRP_user_pwd_set_sv
315 (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
318 if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
320 user_pwd = NULL; /* abandon responsibility */
325 if (last_index != NULL) {
326 /* this means that we want to simulate a default user */
328 if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
329 error_code = SRP_ERR_VBASE_BN_LIB;
332 vb->default_g = gN->g;
333 vb->default_N = gN->N;
336 error_code = SRP_NO_ERROR;
340 * there may be still some leaks to fix, if this fails, the application
341 * terminates most likely
345 OPENSSL_free(gN->id);
349 SRP_user_pwd_free(user_pwd);
354 sk_SRP_gN_free(SRP_gN_tab);
360 static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
368 for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
369 user = sk_SRP_user_pwd_value(vb->users_pwd, i);
370 if (strcmp(user->id, username) == 0)
377 # if OPENSSL_API_COMPAT < 0x10100000L
379 * DEPRECATED: use SRP_VBASE_get1_by_user instead.
380 * This method ignores the configured seed and fails for an unknown user.
381 * Ownership of the returned pointer is not released to the caller.
382 * In other words, caller must not free the result.
384 SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
386 return find_user(vb, username);
391 * Ownership of the returned pointer is released to the caller.
392 * In other words, caller must free the result once done.
394 SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
397 unsigned char digv[SHA_DIGEST_LENGTH];
398 unsigned char digs[SHA_DIGEST_LENGTH];
399 EVP_MD_CTX *ctxt = NULL;
404 if ((user = find_user(vb, username)) != NULL)
405 return srp_user_pwd_dup(user);
407 if ((vb->seed_key == NULL) ||
408 (vb->default_g == NULL) || (vb->default_N == NULL))
411 /* if the user is unknown we set parameters as well if we have a seed_key */
413 if ((user = SRP_user_pwd_new()) == NULL)
416 SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
418 if (!SRP_user_pwd_set_ids(user, username, NULL))
421 if (RAND_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
423 ctxt = EVP_MD_CTX_new();
425 || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
426 || !EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key))
427 || !EVP_DigestUpdate(ctxt, username, strlen(username))
428 || !EVP_DigestFinal_ex(ctxt, digs, NULL))
430 EVP_MD_CTX_free(ctxt);
432 if (SRP_user_pwd_set_sv_BN(user,
433 BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
434 BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
438 EVP_MD_CTX_free(ctxt);
439 SRP_user_pwd_free(user);
444 * create a verifier (*salt,*verifier,g and N are in base64)
446 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
447 char **verifier, const char *N, const char *g)
450 char *result = NULL, *vf = NULL;
451 const BIGNUM *N_bn = NULL, *g_bn = NULL;
452 BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;
453 unsigned char tmp[MAX_LEN];
454 unsigned char tmp2[MAX_LEN];
455 char *defgNid = NULL;
458 if ((user == NULL) ||
459 (pass == NULL) || (salt == NULL) || (verifier == NULL))
463 if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)
465 N_bn_alloc = BN_bin2bn(tmp, len, NULL);
467 if ((len = t_fromb64(tmp, sizeof(tmp) ,g)) <= 0)
469 g_bn_alloc = BN_bin2bn(tmp, len, NULL);
473 SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
482 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
485 s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
487 if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)
489 s = BN_bin2bn(tmp2, len, NULL);
492 if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
496 vfsize = BN_num_bytes(v) * 2;
497 if (((vf = OPENSSL_malloc(vfsize)) == NULL))
499 t_tob64(vf, tmp, BN_num_bytes(v));
504 if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
507 t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);
518 OPENSSL_clear_free(vf, vfsize);
525 * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
526 * then the provided salt will be used. On successful exit *verifier will point
527 * to a newly allocated BIGNUM containing the verifier and (if a salt was not
528 * provided) *salt will be populated with a newly allocated BIGNUM containing a
530 * The caller is responsible for freeing the allocated *salt and *verifier
533 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
534 BIGNUM **verifier, const BIGNUM *N,
539 BN_CTX *bn_ctx = BN_CTX_new();
540 unsigned char tmp2[MAX_LEN];
541 BIGNUM *salttmp = NULL;
543 if ((user == NULL) ||
546 (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
550 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
553 salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
558 x = SRP_Calc_x(salttmp, user, pass);
560 *verifier = BN_new();
561 if (*verifier == NULL)
564 if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
565 BN_clear_free(*verifier);
573 if (salt != NULL && *salt != salttmp)
574 BN_clear_free(salttmp);