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 "internal/evp_int.h"
17 # include <openssl/sha.h>
18 # include <openssl/srp.h>
19 # include <openssl/evp.h>
20 # include <openssl/buffer.h>
21 # include <openssl/rand.h>
22 # include <openssl/txt_db.h>
23 # include <openssl/err.h>
25 # define SRP_RANDOM_SALT_LEN 20
29 * Convert a base64 string into raw byte array representation.
30 * Returns the length of the decoded data, or -1 on error.
32 static int t_fromb64(unsigned char *a, size_t alen, const char *src)
35 int outl = 0, outl2 = 0;
36 size_t size = strlen(src);
41 ctx = EVP_ENCODE_CTX_new();
46 if (EVP_DecodeUpdate(ctx, a, &outl, (const unsigned char *)src, size) < 0) {
47 EVP_ENCODE_CTX_free(ctx);
50 EVP_DecodeFinal(ctx, a + outl, &outl2);
52 EVP_ENCODE_CTX_free(ctx);
57 * Convert a raw byte string into a null-terminated base64 ASCII string.
58 * Returns 1 on success or 0 on error.
60 static int t_tob64(char *dst, const unsigned char *src, int size)
62 EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
63 int outl = 0, outl2 = 0;
69 evp_encode_ctx_set_flags(ctx, EVP_ENCODE_CTX_NO_NEWLINES);
71 if (!EVP_EncodeUpdate(ctx, (unsigned char *)dst, &outl, src, size)) {
72 EVP_ENCODE_CTX_free(ctx);
75 EVP_EncodeFinal(ctx, (unsigned char *)dst + outl, &outl2);
77 EVP_ENCODE_CTX_free(ctx);
81 void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
86 BN_clear_free(user_pwd->v);
87 OPENSSL_free(user_pwd->id);
88 OPENSSL_free(user_pwd->info);
89 OPENSSL_free(user_pwd);
92 static SRP_user_pwd *SRP_user_pwd_new(void)
96 if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
97 /* SRPerr(SRP_F_SRP_USER_PWD_NEW, ERR_R_MALLOC_FAILURE); */
109 static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
116 static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
119 if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
121 return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
124 static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
127 unsigned char tmp[MAX_LEN];
133 len = t_fromb64(tmp, sizeof(tmp), v);
136 if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
138 len = t_fromb64(tmp, sizeof(tmp), s);
141 vinfo->s = BN_bin2bn(tmp, len, NULL);
142 if (vinfo->s == NULL)
151 static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
155 return (vinfo->s != NULL && vinfo->v != NULL);
158 static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
164 if ((ret = SRP_user_pwd_new()) == NULL)
167 SRP_user_pwd_set_gN(ret, src->g, src->N);
168 if (!SRP_user_pwd_set_ids(ret, src->id, src->info)
169 || !SRP_user_pwd_set_sv_BN(ret, BN_dup(src->s), BN_dup(src->v))) {
170 SRP_user_pwd_free(ret);
176 SRP_VBASE *SRP_VBASE_new(char *seed_key)
178 SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
182 if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
183 || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
187 vb->default_g = NULL;
188 vb->default_N = NULL;
190 if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
191 sk_SRP_user_pwd_free(vb->users_pwd);
192 sk_SRP_gN_cache_free(vb->gN_cache);
199 void SRP_VBASE_free(SRP_VBASE *vb)
203 sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
204 sk_SRP_gN_cache_free(vb->gN_cache);
205 OPENSSL_free(vb->seed_key);
209 static SRP_gN_cache *SRP_gN_new_init(const char *ch)
211 unsigned char tmp[MAX_LEN];
213 SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
218 len = t_fromb64(tmp, sizeof(tmp), ch);
222 if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
225 if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
228 OPENSSL_free(newgN->b64_bn);
234 static void SRP_gN_free(SRP_gN_cache *gN_cache)
236 if (gN_cache == NULL)
238 OPENSSL_free(gN_cache->b64_bn);
239 BN_free(gN_cache->bn);
240 OPENSSL_free(gN_cache);
243 static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
249 for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
250 gN = sk_SRP_gN_value(gN_tab, i);
251 if (gN && (id == NULL || strcmp(gN->id, id) == 0))
255 return SRP_get_default_gN(id);
258 static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
261 if (gN_cache == NULL)
264 /* search if we have already one... */
265 for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
266 SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
267 if (strcmp(cache->b64_bn, ch) == 0)
270 { /* it is the first time that we find it */
271 SRP_gN_cache *newgN = SRP_gN_new_init(ch);
273 if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
282 * this function parses verifier file. Format is:
283 * string(index):base64(N):base64(g):0
284 * string(username):base64(v):base64(salt):int(index)
287 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
290 STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
291 char *last_index = NULL;
296 SRP_user_pwd *user_pwd = NULL;
298 TXT_DB *tmpdb = NULL;
299 BIO *in = BIO_new(BIO_s_file());
301 error_code = SRP_ERR_OPEN_FILE;
303 if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
306 error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
308 if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
311 error_code = SRP_ERR_MEMORY;
314 last_index = SRP_get_default_gN(NULL)->id;
316 for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
317 pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
318 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
320 * we add this couple in the internal Stack
323 if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
326 if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
327 || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
329 || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
331 || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
336 if (vb->seed_key != NULL) {
337 last_index = pp[DB_srpid];
339 } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
340 /* it is a user .... */
343 if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
344 error_code = SRP_ERR_MEMORY;
345 if ((user_pwd = SRP_user_pwd_new()) == NULL)
348 SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
349 if (!SRP_user_pwd_set_ids
350 (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
353 error_code = SRP_ERR_VBASE_BN_LIB;
354 if (!SRP_user_pwd_set_sv
355 (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
358 if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
360 user_pwd = NULL; /* abandon responsibility */
365 if (last_index != NULL) {
366 /* this means that we want to simulate a default user */
368 if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
369 error_code = SRP_ERR_VBASE_BN_LIB;
372 vb->default_g = gN->g;
373 vb->default_N = gN->N;
376 error_code = SRP_NO_ERROR;
380 * there may be still some leaks to fix, if this fails, the application
381 * terminates most likely
385 OPENSSL_free(gN->id);
389 SRP_user_pwd_free(user_pwd);
394 sk_SRP_gN_free(SRP_gN_tab);
400 static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
408 for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
409 user = sk_SRP_user_pwd_value(vb->users_pwd, i);
410 if (strcmp(user->id, username) == 0)
417 # if OPENSSL_API_COMPAT < 0x10100000L
419 * DEPRECATED: use SRP_VBASE_get1_by_user instead.
420 * This method ignores the configured seed and fails for an unknown user.
421 * Ownership of the returned pointer is not released to the caller.
422 * In other words, caller must not free the result.
424 SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
426 return find_user(vb, username);
431 * Ownership of the returned pointer is released to the caller.
432 * In other words, caller must free the result once done.
434 SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
437 unsigned char digv[SHA_DIGEST_LENGTH];
438 unsigned char digs[SHA_DIGEST_LENGTH];
439 EVP_MD_CTX *ctxt = NULL;
444 if ((user = find_user(vb, username)) != NULL)
445 return srp_user_pwd_dup(user);
447 if ((vb->seed_key == NULL) ||
448 (vb->default_g == NULL) || (vb->default_N == NULL))
451 /* if the user is unknown we set parameters as well if we have a seed_key */
453 if ((user = SRP_user_pwd_new()) == NULL)
456 SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
458 if (!SRP_user_pwd_set_ids(user, username, NULL))
461 if (RAND_priv_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
463 ctxt = EVP_MD_CTX_new();
465 || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
466 || !EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key))
467 || !EVP_DigestUpdate(ctxt, username, strlen(username))
468 || !EVP_DigestFinal_ex(ctxt, digs, NULL))
470 EVP_MD_CTX_free(ctxt);
472 if (SRP_user_pwd_set_sv_BN(user,
473 BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
474 BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
478 EVP_MD_CTX_free(ctxt);
479 SRP_user_pwd_free(user);
484 * create a verifier (*salt,*verifier,g and N are in base64)
486 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
487 char **verifier, const char *N, const char *g)
490 char *result = NULL, *vf = NULL;
491 const BIGNUM *N_bn = NULL, *g_bn = NULL;
492 BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;
493 unsigned char tmp[MAX_LEN];
494 unsigned char tmp2[MAX_LEN];
495 char *defgNid = NULL;
498 if ((user == NULL) ||
499 (pass == NULL) || (salt == NULL) || (verifier == NULL))
503 if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)
505 N_bn_alloc = BN_bin2bn(tmp, len, NULL);
507 if ((len = t_fromb64(tmp, sizeof(tmp) ,g)) <= 0)
509 g_bn_alloc = BN_bin2bn(tmp, len, NULL);
513 SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
522 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
525 s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
527 if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)
529 s = BN_bin2bn(tmp2, len, NULL);
532 if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
536 vfsize = BN_num_bytes(v) * 2;
537 if (((vf = OPENSSL_malloc(vfsize)) == NULL))
539 t_tob64(vf, tmp, BN_num_bytes(v));
544 if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
547 t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);
558 OPENSSL_clear_free(vf, vfsize);
565 * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
566 * then the provided salt will be used. On successful exit *verifier will point
567 * to a newly allocated BIGNUM containing the verifier and (if a salt was not
568 * provided) *salt will be populated with a newly allocated BIGNUM containing a
570 * The caller is responsible for freeing the allocated *salt and *verifier
573 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
574 BIGNUM **verifier, const BIGNUM *N,
579 BN_CTX *bn_ctx = BN_CTX_new();
580 unsigned char tmp2[MAX_LEN];
581 BIGNUM *salttmp = NULL;
583 if ((user == NULL) ||
586 (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
590 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
593 salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
598 x = SRP_Calc_x(salttmp, user, pass);
600 *verifier = BN_new();
601 if (*verifier == NULL)
604 if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
605 BN_clear_free(*verifier);
613 if (salt != NULL && *salt != salttmp)
614 BN_clear_free(salttmp);