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 * Note that SRP uses its own variant of base 64 encoding. A different base64
30 * alphabet is used and no padding '=' characters are added. Instead we pad to
31 * the front with 0 bytes and subsequently strip off leading encoded padding.
32 * This variant is used for compatibility with other SRP implementations -
33 * notably libsrp, but also others. It is also required for backwards
34 * compatibility in order to load verifier files from other OpenSSL versions.
38 * Convert a base64 string into raw byte array representation.
39 * Returns the length of the decoded data, or -1 on error.
41 static int t_fromb64(unsigned char *a, size_t alen, const char *src)
44 int outl = 0, outl2 = 0;
46 const unsigned char *pad = (const unsigned char *)"00";
48 while (*src == ' ' || *src == '\t' || *src == '\n')
51 padsize = 4 - (size & 3);
54 /* Four bytes in src become three bytes output. */
55 if (size > INT_MAX || ((size + padsize) / 4) * 3 > alen)
58 ctx = EVP_ENCODE_CTX_new();
63 * This should never occur because 1 byte of data always requires 2 bytes of
65 * 0 bytes unencoded = 0 bytes encoded
66 * 1 byte unencoded = 2 bytes encoded
67 * 2 bytes unencoded = 3 bytes encoded
68 * 3 bytes unencoded = 4 bytes encoded
69 * 4 bytes unencoded = 6 bytes encoded
77 /* Valid padsize values are now 0, 1 or 2 */
80 evp_encode_ctx_set_flags(ctx, EVP_ENCODE_CTX_USE_SRP_ALPHABET);
82 /* Add any encoded padding that is required */
84 && EVP_DecodeUpdate(ctx, a, &outl, pad, padsize) < 0) {
88 if (EVP_DecodeUpdate(ctx, a, &outl2, (const unsigned char *)src, size) < 0) {
93 EVP_DecodeFinal(ctx, a + outl, &outl2);
96 /* Strip off the leading padding */
98 if ((int)padsize >= outl) {
104 * If we added 1 byte of padding prior to encoding then we have 2 bytes
105 * of "real" data which gets spread across 4 encoded bytes like this:
106 * (6 bits pad)(2 bits pad | 4 bits data)(6 bits data)(6 bits data)
107 * So 1 byte of pre-encoding padding results in 1 full byte of encoded
109 * If we added 2 bytes of padding prior to encoding this gets encoded
111 * (6 bits pad)(6 bits pad)(4 bits pad | 2 bits data)(6 bits data)
112 * So 2 bytes of pre-encoding padding results in 2 full bytes of encoded
113 * padding, i.e. we have to strip the same number of bytes of padding
114 * from the encoded data as we added to the pre-encoded data.
116 memmove(a, a + padsize, outl - padsize);
121 EVP_ENCODE_CTX_free(ctx);
127 * Convert a raw byte string into a null-terminated base64 ASCII string.
128 * Returns 1 on success or 0 on error.
130 static int t_tob64(char *dst, const unsigned char *src, int size)
132 EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
133 int outl = 0, outl2 = 0;
134 unsigned char pad[2] = {0, 0};
141 evp_encode_ctx_set_flags(ctx, EVP_ENCODE_CTX_NO_NEWLINES
142 | EVP_ENCODE_CTX_USE_SRP_ALPHABET);
145 * We pad at the front with zero bytes until the length is a multiple of 3
146 * so that EVP_EncodeUpdate/EVP_EncodeFinal does not add any of its own "="
149 leadz = 3 - (size % 3);
151 && !EVP_EncodeUpdate(ctx, (unsigned char *)dst, &outl, pad,
153 EVP_ENCODE_CTX_free(ctx);
157 if (!EVP_EncodeUpdate(ctx, (unsigned char *)dst + outl, &outl2, src,
159 EVP_ENCODE_CTX_free(ctx);
163 EVP_EncodeFinal(ctx, (unsigned char *)dst + outl, &outl2);
166 /* Strip the encoded padding at the front */
168 memmove(dst, dst + leadz, outl - leadz);
169 dst[outl - leadz] = '\0';
172 EVP_ENCODE_CTX_free(ctx);
176 void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
178 if (user_pwd == NULL)
180 BN_free(user_pwd->s);
181 BN_clear_free(user_pwd->v);
182 OPENSSL_free(user_pwd->id);
183 OPENSSL_free(user_pwd->info);
184 OPENSSL_free(user_pwd);
187 static SRP_user_pwd *SRP_user_pwd_new(void)
191 if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
192 /* SRPerr(SRP_F_SRP_USER_PWD_NEW, ERR_R_MALLOC_FAILURE); */ /*ckerr_ignore*/
204 static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
211 static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
214 if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
216 return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
219 static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
222 unsigned char tmp[MAX_LEN];
228 len = t_fromb64(tmp, sizeof(tmp), v);
231 if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
233 len = t_fromb64(tmp, sizeof(tmp), s);
236 vinfo->s = BN_bin2bn(tmp, len, NULL);
237 if (vinfo->s == NULL)
246 static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
250 return (vinfo->s != NULL && vinfo->v != NULL);
253 static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
259 if ((ret = SRP_user_pwd_new()) == NULL)
262 SRP_user_pwd_set_gN(ret, src->g, src->N);
263 if (!SRP_user_pwd_set_ids(ret, src->id, src->info)
264 || !SRP_user_pwd_set_sv_BN(ret, BN_dup(src->s), BN_dup(src->v))) {
265 SRP_user_pwd_free(ret);
271 SRP_VBASE *SRP_VBASE_new(char *seed_key)
273 SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
277 if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
278 || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
282 vb->default_g = NULL;
283 vb->default_N = NULL;
285 if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
286 sk_SRP_user_pwd_free(vb->users_pwd);
287 sk_SRP_gN_cache_free(vb->gN_cache);
294 void SRP_VBASE_free(SRP_VBASE *vb)
298 sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
299 sk_SRP_gN_cache_free(vb->gN_cache);
300 OPENSSL_free(vb->seed_key);
304 static SRP_gN_cache *SRP_gN_new_init(const char *ch)
306 unsigned char tmp[MAX_LEN];
308 SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
313 len = t_fromb64(tmp, sizeof(tmp), ch);
317 if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
320 if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
323 OPENSSL_free(newgN->b64_bn);
329 static void SRP_gN_free(SRP_gN_cache *gN_cache)
331 if (gN_cache == NULL)
333 OPENSSL_free(gN_cache->b64_bn);
334 BN_free(gN_cache->bn);
335 OPENSSL_free(gN_cache);
338 static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
344 for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
345 gN = sk_SRP_gN_value(gN_tab, i);
346 if (gN && (id == NULL || strcmp(gN->id, id) == 0))
350 return SRP_get_default_gN(id);
353 static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
356 if (gN_cache == NULL)
359 /* search if we have already one... */
360 for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
361 SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
362 if (strcmp(cache->b64_bn, ch) == 0)
365 { /* it is the first time that we find it */
366 SRP_gN_cache *newgN = SRP_gN_new_init(ch);
368 if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
377 * this function parses verifier file. Format is:
378 * string(index):base64(N):base64(g):0
379 * string(username):base64(v):base64(salt):int(index)
382 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
385 STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
386 char *last_index = NULL;
391 SRP_user_pwd *user_pwd = NULL;
393 TXT_DB *tmpdb = NULL;
394 BIO *in = BIO_new(BIO_s_file());
396 error_code = SRP_ERR_OPEN_FILE;
398 if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
401 error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
403 if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
406 error_code = SRP_ERR_MEMORY;
409 last_index = SRP_get_default_gN(NULL)->id;
411 for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
412 pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
413 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
415 * we add this couple in the internal Stack
418 if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
421 if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
422 || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
424 || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
426 || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
431 if (vb->seed_key != NULL) {
432 last_index = pp[DB_srpid];
434 } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
435 /* it is a user .... */
438 if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
439 error_code = SRP_ERR_MEMORY;
440 if ((user_pwd = SRP_user_pwd_new()) == NULL)
443 SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
444 if (!SRP_user_pwd_set_ids
445 (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
448 error_code = SRP_ERR_VBASE_BN_LIB;
449 if (!SRP_user_pwd_set_sv
450 (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
453 if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
455 user_pwd = NULL; /* abandon responsibility */
460 if (last_index != NULL) {
461 /* this means that we want to simulate a default user */
463 if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
464 error_code = SRP_ERR_VBASE_BN_LIB;
467 vb->default_g = gN->g;
468 vb->default_N = gN->N;
471 error_code = SRP_NO_ERROR;
475 * there may be still some leaks to fix, if this fails, the application
476 * terminates most likely
480 OPENSSL_free(gN->id);
484 SRP_user_pwd_free(user_pwd);
489 sk_SRP_gN_free(SRP_gN_tab);
495 static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
503 for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
504 user = sk_SRP_user_pwd_value(vb->users_pwd, i);
505 if (strcmp(user->id, username) == 0)
512 # if OPENSSL_API_COMPAT < 0x10100000L
514 * DEPRECATED: use SRP_VBASE_get1_by_user instead.
515 * This method ignores the configured seed and fails for an unknown user.
516 * Ownership of the returned pointer is not released to the caller.
517 * In other words, caller must not free the result.
519 SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
521 return find_user(vb, username);
526 * Ownership of the returned pointer is released to the caller.
527 * In other words, caller must free the result once done.
529 SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
532 unsigned char digv[SHA_DIGEST_LENGTH];
533 unsigned char digs[SHA_DIGEST_LENGTH];
534 EVP_MD_CTX *ctxt = NULL;
539 if ((user = find_user(vb, username)) != NULL)
540 return srp_user_pwd_dup(user);
542 if ((vb->seed_key == NULL) ||
543 (vb->default_g == NULL) || (vb->default_N == NULL))
546 /* if the user is unknown we set parameters as well if we have a seed_key */
548 if ((user = SRP_user_pwd_new()) == NULL)
551 SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
553 if (!SRP_user_pwd_set_ids(user, username, NULL))
556 if (RAND_priv_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
558 ctxt = EVP_MD_CTX_new();
560 || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
561 || !EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key))
562 || !EVP_DigestUpdate(ctxt, username, strlen(username))
563 || !EVP_DigestFinal_ex(ctxt, digs, NULL))
565 EVP_MD_CTX_free(ctxt);
567 if (SRP_user_pwd_set_sv_BN(user,
568 BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
569 BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
573 EVP_MD_CTX_free(ctxt);
574 SRP_user_pwd_free(user);
579 * create a verifier (*salt,*verifier,g and N are in base64)
581 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
582 char **verifier, const char *N, const char *g)
585 char *result = NULL, *vf = NULL;
586 const BIGNUM *N_bn = NULL, *g_bn = NULL;
587 BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;
588 unsigned char tmp[MAX_LEN];
589 unsigned char tmp2[MAX_LEN];
590 char *defgNid = NULL;
593 if ((user == NULL) ||
594 (pass == NULL) || (salt == NULL) || (verifier == NULL))
598 if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)
600 N_bn_alloc = BN_bin2bn(tmp, len, NULL);
601 if (N_bn_alloc == NULL)
604 if ((len = t_fromb64(tmp, sizeof(tmp) ,g)) <= 0)
606 g_bn_alloc = BN_bin2bn(tmp, len, NULL);
607 if (g_bn_alloc == NULL)
612 SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
621 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
624 s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
626 if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)
628 s = BN_bin2bn(tmp2, len, NULL);
633 if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
636 if (BN_bn2bin(v, tmp) < 0)
638 vfsize = BN_num_bytes(v) * 2;
639 if (((vf = OPENSSL_malloc(vfsize)) == NULL))
641 if (!t_tob64(vf, tmp, BN_num_bytes(v)))
647 if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
650 if (!t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN)) {
651 OPENSSL_free(tmp_salt);
664 OPENSSL_clear_free(vf, vfsize);
671 * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
672 * then the provided salt will be used. On successful exit *verifier will point
673 * to a newly allocated BIGNUM containing the verifier and (if a salt was not
674 * provided) *salt will be populated with a newly allocated BIGNUM containing a
676 * The caller is responsible for freeing the allocated *salt and *verifier
679 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
680 BIGNUM **verifier, const BIGNUM *N,
685 BN_CTX *bn_ctx = BN_CTX_new();
686 unsigned char tmp2[MAX_LEN];
687 BIGNUM *salttmp = NULL;
689 if ((user == NULL) ||
692 (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
696 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
699 salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
706 x = SRP_Calc_x(salttmp, user, pass);
710 *verifier = BN_new();
711 if (*verifier == NULL)
714 if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
715 BN_clear_free(*verifier);
723 if (salt != NULL && *salt != salttmp)
724 BN_clear_free(salttmp);