2 * Copyright 2004-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
5 * Licensed under the Apache License 2.0 (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 "crypto/evp.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
28 DEFINE_STACK_OF(SRP_user_pwd
)
29 DEFINE_STACK_OF(SRP_gN_cache
)
30 DEFINE_STACK_OF(SRP_gN
)
33 * Note that SRP uses its own variant of base 64 encoding. A different base64
34 * alphabet is used and no padding '=' characters are added. Instead we pad to
35 * the front with 0 bytes and subsequently strip off leading encoded padding.
36 * This variant is used for compatibility with other SRP implementations -
37 * notably libsrp, but also others. It is also required for backwards
38 * compatibility in order to load verifier files from other OpenSSL versions.
42 * Convert a base64 string into raw byte array representation.
43 * Returns the length of the decoded data, or -1 on error.
45 static int t_fromb64(unsigned char *a
, size_t alen
, const char *src
)
48 int outl
= 0, outl2
= 0;
50 const unsigned char *pad
= (const unsigned char *)"00";
52 while (*src
== ' ' || *src
== '\t' || *src
== '\n')
55 padsize
= 4 - (size
& 3);
58 /* Four bytes in src become three bytes output. */
59 if (size
> INT_MAX
|| ((size
+ padsize
) / 4) * 3 > alen
)
62 ctx
= EVP_ENCODE_CTX_new();
67 * This should never occur because 1 byte of data always requires 2 bytes of
69 * 0 bytes unencoded = 0 bytes encoded
70 * 1 byte unencoded = 2 bytes encoded
71 * 2 bytes unencoded = 3 bytes encoded
72 * 3 bytes unencoded = 4 bytes encoded
73 * 4 bytes unencoded = 6 bytes encoded
81 /* Valid padsize values are now 0, 1 or 2 */
84 evp_encode_ctx_set_flags(ctx
, EVP_ENCODE_CTX_USE_SRP_ALPHABET
);
86 /* Add any encoded padding that is required */
88 && EVP_DecodeUpdate(ctx
, a
, &outl
, pad
, padsize
) < 0) {
92 if (EVP_DecodeUpdate(ctx
, a
, &outl2
, (const unsigned char *)src
, size
) < 0) {
97 EVP_DecodeFinal(ctx
, a
+ outl
, &outl2
);
100 /* Strip off the leading padding */
102 if ((int)padsize
>= outl
) {
108 * If we added 1 byte of padding prior to encoding then we have 2 bytes
109 * of "real" data which gets spread across 4 encoded bytes like this:
110 * (6 bits pad)(2 bits pad | 4 bits data)(6 bits data)(6 bits data)
111 * So 1 byte of pre-encoding padding results in 1 full byte of encoded
113 * If we added 2 bytes of padding prior to encoding this gets encoded
115 * (6 bits pad)(6 bits pad)(4 bits pad | 2 bits data)(6 bits data)
116 * So 2 bytes of pre-encoding padding results in 2 full bytes of encoded
117 * padding, i.e. we have to strip the same number of bytes of padding
118 * from the encoded data as we added to the pre-encoded data.
120 memmove(a
, a
+ padsize
, outl
- padsize
);
125 EVP_ENCODE_CTX_free(ctx
);
131 * Convert a raw byte string into a null-terminated base64 ASCII string.
132 * Returns 1 on success or 0 on error.
134 static int t_tob64(char *dst
, const unsigned char *src
, int size
)
136 EVP_ENCODE_CTX
*ctx
= EVP_ENCODE_CTX_new();
137 int outl
= 0, outl2
= 0;
138 unsigned char pad
[2] = {0, 0};
145 evp_encode_ctx_set_flags(ctx
, EVP_ENCODE_CTX_NO_NEWLINES
146 | EVP_ENCODE_CTX_USE_SRP_ALPHABET
);
149 * We pad at the front with zero bytes until the length is a multiple of 3
150 * so that EVP_EncodeUpdate/EVP_EncodeFinal does not add any of its own "="
153 leadz
= 3 - (size
% 3);
155 && !EVP_EncodeUpdate(ctx
, (unsigned char *)dst
, &outl
, pad
,
157 EVP_ENCODE_CTX_free(ctx
);
161 if (!EVP_EncodeUpdate(ctx
, (unsigned char *)dst
+ outl
, &outl2
, src
,
163 EVP_ENCODE_CTX_free(ctx
);
167 EVP_EncodeFinal(ctx
, (unsigned char *)dst
+ outl
, &outl2
);
170 /* Strip the encoded padding at the front */
172 memmove(dst
, dst
+ leadz
, outl
- leadz
);
173 dst
[outl
- leadz
] = '\0';
176 EVP_ENCODE_CTX_free(ctx
);
180 void SRP_user_pwd_free(SRP_user_pwd
*user_pwd
)
182 if (user_pwd
== NULL
)
184 BN_free(user_pwd
->s
);
185 BN_clear_free(user_pwd
->v
);
186 OPENSSL_free(user_pwd
->id
);
187 OPENSSL_free(user_pwd
->info
);
188 OPENSSL_free(user_pwd
);
191 SRP_user_pwd
*SRP_user_pwd_new(void)
195 if ((ret
= OPENSSL_malloc(sizeof(*ret
))) == NULL
) {
196 /* SRPerr(SRP_F_SRP_USER_PWD_NEW, ERR_R_MALLOC_FAILURE); */ /*ckerr_ignore*/
208 void SRP_user_pwd_set_gN(SRP_user_pwd
*vinfo
, const BIGNUM
*g
,
215 int SRP_user_pwd_set1_ids(SRP_user_pwd
*vinfo
, const char *id
,
218 OPENSSL_free(vinfo
->id
);
219 OPENSSL_free(vinfo
->info
);
220 if (id
!= NULL
&& NULL
== (vinfo
->id
= OPENSSL_strdup(id
)))
222 return (info
== NULL
|| NULL
!= (vinfo
->info
= OPENSSL_strdup(info
)));
225 static int SRP_user_pwd_set_sv(SRP_user_pwd
*vinfo
, const char *s
,
228 unsigned char tmp
[MAX_LEN
];
234 len
= t_fromb64(tmp
, sizeof(tmp
), v
);
237 if (NULL
== (vinfo
->v
= BN_bin2bn(tmp
, len
, NULL
)))
239 len
= t_fromb64(tmp
, sizeof(tmp
), s
);
242 vinfo
->s
= BN_bin2bn(tmp
, len
, NULL
);
243 if (vinfo
->s
== NULL
)
252 int SRP_user_pwd_set0_sv(SRP_user_pwd
*vinfo
, BIGNUM
*s
, BIGNUM
*v
)
255 BN_clear_free(vinfo
->v
);
258 return (vinfo
->s
!= NULL
&& vinfo
->v
!= NULL
);
261 static SRP_user_pwd
*srp_user_pwd_dup(SRP_user_pwd
*src
)
267 if ((ret
= SRP_user_pwd_new()) == NULL
)
270 SRP_user_pwd_set_gN(ret
, src
->g
, src
->N
);
271 if (!SRP_user_pwd_set1_ids(ret
, src
->id
, src
->info
)
272 || !SRP_user_pwd_set0_sv(ret
, BN_dup(src
->s
), BN_dup(src
->v
))) {
273 SRP_user_pwd_free(ret
);
279 SRP_VBASE
*SRP_VBASE_new(char *seed_key
)
281 SRP_VBASE
*vb
= OPENSSL_malloc(sizeof(*vb
));
285 if ((vb
->users_pwd
= sk_SRP_user_pwd_new_null()) == NULL
286 || (vb
->gN_cache
= sk_SRP_gN_cache_new_null()) == NULL
) {
290 vb
->default_g
= NULL
;
291 vb
->default_N
= NULL
;
293 if ((seed_key
!= NULL
) && (vb
->seed_key
= OPENSSL_strdup(seed_key
)) == NULL
) {
294 sk_SRP_user_pwd_free(vb
->users_pwd
);
295 sk_SRP_gN_cache_free(vb
->gN_cache
);
302 void SRP_VBASE_free(SRP_VBASE
*vb
)
306 sk_SRP_user_pwd_pop_free(vb
->users_pwd
, SRP_user_pwd_free
);
307 sk_SRP_gN_cache_free(vb
->gN_cache
);
308 OPENSSL_free(vb
->seed_key
);
312 static SRP_gN_cache
*SRP_gN_new_init(const char *ch
)
314 unsigned char tmp
[MAX_LEN
];
316 SRP_gN_cache
*newgN
= OPENSSL_malloc(sizeof(*newgN
));
321 len
= t_fromb64(tmp
, sizeof(tmp
), ch
);
325 if ((newgN
->b64_bn
= OPENSSL_strdup(ch
)) == NULL
)
328 if ((newgN
->bn
= BN_bin2bn(tmp
, len
, NULL
)))
331 OPENSSL_free(newgN
->b64_bn
);
337 static void SRP_gN_free(SRP_gN_cache
*gN_cache
)
339 if (gN_cache
== NULL
)
341 OPENSSL_free(gN_cache
->b64_bn
);
342 BN_free(gN_cache
->bn
);
343 OPENSSL_free(gN_cache
);
346 static SRP_gN
*SRP_get_gN_by_id(const char *id
, STACK_OF(SRP_gN
) *gN_tab
)
351 if (gN_tab
!= NULL
) {
352 for (i
= 0; i
< sk_SRP_gN_num(gN_tab
); i
++) {
353 gN
= sk_SRP_gN_value(gN_tab
, i
);
354 if (gN
&& (id
== NULL
|| strcmp(gN
->id
, id
) == 0))
359 return SRP_get_default_gN(id
);
362 static BIGNUM
*SRP_gN_place_bn(STACK_OF(SRP_gN_cache
) *gN_cache
, char *ch
)
365 if (gN_cache
== NULL
)
368 /* search if we have already one... */
369 for (i
= 0; i
< sk_SRP_gN_cache_num(gN_cache
); i
++) {
370 SRP_gN_cache
*cache
= sk_SRP_gN_cache_value(gN_cache
, i
);
371 if (strcmp(cache
->b64_bn
, ch
) == 0)
374 { /* it is the first time that we find it */
375 SRP_gN_cache
*newgN
= SRP_gN_new_init(ch
);
377 if (sk_SRP_gN_cache_insert(gN_cache
, newgN
, 0) > 0)
386 * This function parses the verifier file generated by the srp app.
387 * The format for each entry is:
388 * V base64(verifier) base64(salt) username gNid userinfo(optional)
390 * I base64(N) base64(g)
391 * Note that base64 is the SRP variant of base64 encoding described
395 int SRP_VBASE_init(SRP_VBASE
*vb
, char *verifier_file
)
398 STACK_OF(SRP_gN
) *SRP_gN_tab
= sk_SRP_gN_new_null();
399 char *last_index
= NULL
;
404 SRP_user_pwd
*user_pwd
= NULL
;
406 TXT_DB
*tmpdb
= NULL
;
407 BIO
*in
= BIO_new(BIO_s_file());
409 error_code
= SRP_ERR_OPEN_FILE
;
411 if (in
== NULL
|| BIO_read_filename(in
, verifier_file
) <= 0)
414 error_code
= SRP_ERR_VBASE_INCOMPLETE_FILE
;
416 if ((tmpdb
= TXT_DB_read(in
, DB_NUMBER
)) == NULL
)
419 error_code
= SRP_ERR_MEMORY
;
422 last_index
= SRP_get_default_gN(NULL
)->id
;
424 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(tmpdb
->data
); i
++) {
425 pp
= sk_OPENSSL_PSTRING_value(tmpdb
->data
, i
);
426 if (pp
[DB_srptype
][0] == DB_SRP_INDEX
) {
428 * we add this couple in the internal Stack
431 if ((gN
= OPENSSL_malloc(sizeof(*gN
))) == NULL
)
434 if ((gN
->id
= OPENSSL_strdup(pp
[DB_srpid
])) == NULL
435 || (gN
->N
= SRP_gN_place_bn(vb
->gN_cache
, pp
[DB_srpverifier
]))
437 || (gN
->g
= SRP_gN_place_bn(vb
->gN_cache
, pp
[DB_srpsalt
]))
439 || sk_SRP_gN_insert(SRP_gN_tab
, gN
, 0) == 0)
444 if (vb
->seed_key
!= NULL
) {
445 last_index
= pp
[DB_srpid
];
447 } else if (pp
[DB_srptype
][0] == DB_SRP_VALID
) {
448 /* it is a user .... */
451 if ((lgN
= SRP_get_gN_by_id(pp
[DB_srpgN
], SRP_gN_tab
)) != NULL
) {
452 error_code
= SRP_ERR_MEMORY
;
453 if ((user_pwd
= SRP_user_pwd_new()) == NULL
)
456 SRP_user_pwd_set_gN(user_pwd
, lgN
->g
, lgN
->N
);
457 if (!SRP_user_pwd_set1_ids
458 (user_pwd
, pp
[DB_srpid
], pp
[DB_srpinfo
]))
461 error_code
= SRP_ERR_VBASE_BN_LIB
;
462 if (!SRP_user_pwd_set_sv
463 (user_pwd
, pp
[DB_srpsalt
], pp
[DB_srpverifier
]))
466 if (sk_SRP_user_pwd_insert(vb
->users_pwd
, user_pwd
, 0) == 0)
468 user_pwd
= NULL
; /* abandon responsibility */
473 if (last_index
!= NULL
) {
474 /* this means that we want to simulate a default user */
476 if (((gN
= SRP_get_gN_by_id(last_index
, SRP_gN_tab
)) == NULL
)) {
477 error_code
= SRP_ERR_VBASE_BN_LIB
;
480 vb
->default_g
= gN
->g
;
481 vb
->default_N
= gN
->N
;
484 error_code
= SRP_NO_ERROR
;
488 * there may be still some leaks to fix, if this fails, the application
489 * terminates most likely
493 OPENSSL_free(gN
->id
);
497 SRP_user_pwd_free(user_pwd
);
502 sk_SRP_gN_free(SRP_gN_tab
);
508 static SRP_user_pwd
*find_user(SRP_VBASE
*vb
, char *username
)
516 for (i
= 0; i
< sk_SRP_user_pwd_num(vb
->users_pwd
); i
++) {
517 user
= sk_SRP_user_pwd_value(vb
->users_pwd
, i
);
518 if (strcmp(user
->id
, username
) == 0)
525 int SRP_VBASE_add0_user(SRP_VBASE
*vb
, SRP_user_pwd
*user_pwd
)
527 if (sk_SRP_user_pwd_push(vb
->users_pwd
, user_pwd
) <= 0)
532 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
534 * DEPRECATED: use SRP_VBASE_get1_by_user instead.
535 * This method ignores the configured seed and fails for an unknown user.
536 * Ownership of the returned pointer is not released to the caller.
537 * In other words, caller must not free the result.
539 SRP_user_pwd
*SRP_VBASE_get_by_user(SRP_VBASE
*vb
, char *username
)
541 return find_user(vb
, username
);
546 * Ownership of the returned pointer is released to the caller.
547 * In other words, caller must free the result once done.
549 SRP_user_pwd
*SRP_VBASE_get1_by_user(SRP_VBASE
*vb
, char *username
)
552 unsigned char digv
[SHA_DIGEST_LENGTH
];
553 unsigned char digs
[SHA_DIGEST_LENGTH
];
554 EVP_MD_CTX
*ctxt
= NULL
;
559 if ((user
= find_user(vb
, username
)) != NULL
)
560 return srp_user_pwd_dup(user
);
562 if ((vb
->seed_key
== NULL
) ||
563 (vb
->default_g
== NULL
) || (vb
->default_N
== NULL
))
566 /* if the user is unknown we set parameters as well if we have a seed_key */
568 if ((user
= SRP_user_pwd_new()) == NULL
)
571 SRP_user_pwd_set_gN(user
, vb
->default_g
, vb
->default_N
);
573 if (!SRP_user_pwd_set1_ids(user
, username
, NULL
))
576 if (RAND_priv_bytes(digv
, SHA_DIGEST_LENGTH
) <= 0)
578 ctxt
= EVP_MD_CTX_new();
580 || !EVP_DigestInit_ex(ctxt
, EVP_sha1(), NULL
)
581 || !EVP_DigestUpdate(ctxt
, vb
->seed_key
, strlen(vb
->seed_key
))
582 || !EVP_DigestUpdate(ctxt
, username
, strlen(username
))
583 || !EVP_DigestFinal_ex(ctxt
, digs
, NULL
))
585 EVP_MD_CTX_free(ctxt
);
587 if (SRP_user_pwd_set0_sv(user
,
588 BN_bin2bn(digs
, SHA_DIGEST_LENGTH
, NULL
),
589 BN_bin2bn(digv
, SHA_DIGEST_LENGTH
, NULL
)))
593 EVP_MD_CTX_free(ctxt
);
594 SRP_user_pwd_free(user
);
599 * create a verifier (*salt,*verifier,g and N are in base64)
601 char *SRP_create_verifier_ex(const char *user
, const char *pass
, char **salt
,
602 char **verifier
, const char *N
, const char *g
,
603 OPENSSL_CTX
*libctx
, const char *propq
)
606 char *result
= NULL
, *vf
= NULL
;
607 const BIGNUM
*N_bn
= NULL
, *g_bn
= NULL
;
608 BIGNUM
*N_bn_alloc
= NULL
, *g_bn_alloc
= NULL
, *s
= NULL
, *v
= NULL
;
609 unsigned char tmp
[MAX_LEN
];
610 unsigned char tmp2
[MAX_LEN
];
611 char *defgNid
= NULL
;
614 if ((user
== NULL
) ||
615 (pass
== NULL
) || (salt
== NULL
) || (verifier
== NULL
))
619 if ((len
= t_fromb64(tmp
, sizeof(tmp
), N
)) <= 0)
621 N_bn_alloc
= BN_bin2bn(tmp
, len
, NULL
);
622 if (N_bn_alloc
== NULL
)
625 if ((len
= t_fromb64(tmp
, sizeof(tmp
) ,g
)) <= 0)
627 g_bn_alloc
= BN_bin2bn(tmp
, len
, NULL
);
628 if (g_bn_alloc
== NULL
)
633 SRP_gN
*gN
= SRP_get_default_gN(g
);
642 if (RAND_bytes_ex(libctx
, tmp2
, SRP_RANDOM_SALT_LEN
) <= 0)
645 s
= BN_bin2bn(tmp2
, SRP_RANDOM_SALT_LEN
, NULL
);
647 if ((len
= t_fromb64(tmp2
, sizeof(tmp2
), *salt
)) <= 0)
649 s
= BN_bin2bn(tmp2
, len
, NULL
);
654 if (!SRP_create_verifier_BN_ex(user
, pass
, &s
, &v
, N_bn
, g_bn
, libctx
,
658 if (BN_bn2bin(v
, tmp
) < 0)
660 vfsize
= BN_num_bytes(v
) * 2;
661 if (((vf
= OPENSSL_malloc(vfsize
)) == NULL
))
663 if (!t_tob64(vf
, tmp
, BN_num_bytes(v
)))
669 if ((tmp_salt
= OPENSSL_malloc(SRP_RANDOM_SALT_LEN
* 2)) == NULL
) {
672 if (!t_tob64(tmp_salt
, tmp2
, SRP_RANDOM_SALT_LEN
)) {
673 OPENSSL_free(tmp_salt
);
686 OPENSSL_clear_free(vf
, vfsize
);
692 char *SRP_create_verifier(const char *user
, const char *pass
, char **salt
,
693 char **verifier
, const char *N
, const char *g
)
695 return SRP_create_verifier_ex(user
, pass
, salt
, verifier
, N
, g
, NULL
, NULL
);
699 * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
700 * then the provided salt will be used. On successful exit *verifier will point
701 * to a newly allocated BIGNUM containing the verifier and (if a salt was not
702 * provided) *salt will be populated with a newly allocated BIGNUM containing a
704 * The caller is responsible for freeing the allocated *salt and *verifier
707 int SRP_create_verifier_BN_ex(const char *user
, const char *pass
, BIGNUM
**salt
,
708 BIGNUM
**verifier
, const BIGNUM
*N
,
709 const BIGNUM
*g
, OPENSSL_CTX
*libctx
,
714 BN_CTX
*bn_ctx
= BN_CTX_new_ex(libctx
);
715 unsigned char tmp2
[MAX_LEN
];
716 BIGNUM
*salttmp
= NULL
;
718 if ((user
== NULL
) ||
721 (verifier
== NULL
) || (N
== NULL
) || (g
== NULL
) || (bn_ctx
== NULL
))
725 if (RAND_bytes_ex(libctx
, tmp2
, SRP_RANDOM_SALT_LEN
) <= 0)
728 salttmp
= BN_bin2bn(tmp2
, SRP_RANDOM_SALT_LEN
, NULL
);
735 x
= SRP_Calc_x_ex(salttmp
, user
, pass
, libctx
, propq
);
739 *verifier
= BN_new();
740 if (*verifier
== NULL
)
743 if (!BN_mod_exp(*verifier
, g
, x
, N
, bn_ctx
)) {
744 BN_clear_free(*verifier
);
752 if (salt
!= NULL
&& *salt
!= salttmp
)
753 BN_clear_free(salttmp
);
759 int SRP_create_verifier_BN(const char *user
, const char *pass
, BIGNUM
**salt
,
760 BIGNUM
**verifier
, const BIGNUM
*N
,
763 return SRP_create_verifier_BN_ex(user
, pass
, salt
, verifier
, N
, g
, NULL
,