2 * Copyright 2004-2023 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 /* All the SRP APIs in this file are deprecated */
15 #define OPENSSL_SUPPRESS_DEPRECATED
17 #ifndef OPENSSL_NO_SRP
18 # include "internal/cryptlib.h"
19 # include "crypto/evp.h"
20 # include <openssl/sha.h>
21 # include <openssl/srp.h>
22 # include <openssl/evp.h>
23 # include <openssl/buffer.h>
24 # include <openssl/rand.h>
25 # include <openssl/txt_db.h>
26 # include <openssl/err.h>
28 # define SRP_RANDOM_SALT_LEN 20
32 * Note that SRP uses its own variant of base 64 encoding. A different base64
33 * alphabet is used and no padding '=' characters are added. Instead we pad to
34 * the front with 0 bytes and subsequently strip off leading encoded padding.
35 * This variant is used for compatibility with other SRP implementations -
36 * notably libsrp, but also others. It is also required for backwards
37 * compatibility in order to load verifier files from other OpenSSL versions.
41 * Convert a base64 string into raw byte array representation.
42 * Returns the length of the decoded data, or -1 on error.
44 static int t_fromb64(unsigned char *a
, size_t alen
, const char *src
)
47 int outl
= 0, outl2
= 0;
49 const unsigned char *pad
= (const unsigned char *)"00";
51 while (*src
== ' ' || *src
== '\t' || *src
== '\n')
54 padsize
= 4 - (size
& 3);
57 /* Four bytes in src become three bytes output. */
58 if (size
> INT_MAX
|| ((size
+ padsize
) / 4) * 3 > alen
)
61 ctx
= EVP_ENCODE_CTX_new();
66 * This should never occur because 1 byte of data always requires 2 bytes of
68 * 0 bytes unencoded = 0 bytes encoded
69 * 1 byte unencoded = 2 bytes encoded
70 * 2 bytes unencoded = 3 bytes encoded
71 * 3 bytes unencoded = 4 bytes encoded
72 * 4 bytes unencoded = 6 bytes encoded
80 /* Valid padsize values are now 0, 1 or 2 */
83 evp_encode_ctx_set_flags(ctx
, EVP_ENCODE_CTX_USE_SRP_ALPHABET
);
85 /* Add any encoded padding that is required */
87 && EVP_DecodeUpdate(ctx
, a
, &outl
, pad
, padsize
) < 0) {
91 if (EVP_DecodeUpdate(ctx
, a
, &outl2
, (const unsigned char *)src
, size
) < 0) {
96 EVP_DecodeFinal(ctx
, a
+ outl
, &outl2
);
99 /* Strip off the leading padding */
101 if ((int)padsize
>= outl
) {
107 * If we added 1 byte of padding prior to encoding then we have 2 bytes
108 * of "real" data which gets spread across 4 encoded bytes like this:
109 * (6 bits pad)(2 bits pad | 4 bits data)(6 bits data)(6 bits data)
110 * So 1 byte of pre-encoding padding results in 1 full byte of encoded
112 * If we added 2 bytes of padding prior to encoding this gets encoded
114 * (6 bits pad)(6 bits pad)(4 bits pad | 2 bits data)(6 bits data)
115 * So 2 bytes of pre-encoding padding results in 2 full bytes of encoded
116 * padding, i.e. we have to strip the same number of bytes of padding
117 * from the encoded data as we added to the pre-encoded data.
119 memmove(a
, a
+ padsize
, outl
- padsize
);
124 EVP_ENCODE_CTX_free(ctx
);
130 * Convert a raw byte string into a null-terminated base64 ASCII string.
131 * Returns 1 on success or 0 on error.
133 static int t_tob64(char *dst
, const unsigned char *src
, int size
)
135 EVP_ENCODE_CTX
*ctx
= EVP_ENCODE_CTX_new();
136 int outl
= 0, outl2
= 0;
137 unsigned char pad
[2] = {0, 0};
144 evp_encode_ctx_set_flags(ctx
, EVP_ENCODE_CTX_NO_NEWLINES
145 | EVP_ENCODE_CTX_USE_SRP_ALPHABET
);
148 * We pad at the front with zero bytes until the length is a multiple of 3
149 * so that EVP_EncodeUpdate/EVP_EncodeFinal does not add any of its own "="
152 leadz
= 3 - (size
% 3);
154 && !EVP_EncodeUpdate(ctx
, (unsigned char *)dst
, &outl
, pad
,
156 EVP_ENCODE_CTX_free(ctx
);
160 if (!EVP_EncodeUpdate(ctx
, (unsigned char *)dst
+ outl
, &outl2
, src
,
162 EVP_ENCODE_CTX_free(ctx
);
166 EVP_EncodeFinal(ctx
, (unsigned char *)dst
+ outl
, &outl2
);
169 /* Strip the encoded padding at the front */
171 memmove(dst
, dst
+ leadz
, outl
- leadz
);
172 dst
[outl
- leadz
] = '\0';
175 EVP_ENCODE_CTX_free(ctx
);
179 void SRP_user_pwd_free(SRP_user_pwd
*user_pwd
)
181 if (user_pwd
== NULL
)
183 BN_free(user_pwd
->s
);
184 BN_clear_free(user_pwd
->v
);
185 OPENSSL_free(user_pwd
->id
);
186 OPENSSL_free(user_pwd
->info
);
187 OPENSSL_free(user_pwd
);
190 SRP_user_pwd
*SRP_user_pwd_new(void)
194 if ((ret
= OPENSSL_malloc(sizeof(*ret
))) == NULL
)
205 void SRP_user_pwd_set_gN(SRP_user_pwd
*vinfo
, const BIGNUM
*g
,
212 int SRP_user_pwd_set1_ids(SRP_user_pwd
*vinfo
, const char *id
,
215 OPENSSL_free(vinfo
->id
);
216 OPENSSL_free(vinfo
->info
);
217 if (id
!= NULL
&& NULL
== (vinfo
->id
= OPENSSL_strdup(id
)))
219 return (info
== NULL
|| NULL
!= (vinfo
->info
= OPENSSL_strdup(info
)));
222 static int SRP_user_pwd_set_sv(SRP_user_pwd
*vinfo
, const char *s
,
225 unsigned char tmp
[MAX_LEN
];
231 len
= t_fromb64(tmp
, sizeof(tmp
), v
);
234 if (NULL
== (vinfo
->v
= BN_bin2bn(tmp
, len
, NULL
)))
236 len
= t_fromb64(tmp
, sizeof(tmp
), s
);
239 vinfo
->s
= BN_bin2bn(tmp
, len
, NULL
);
240 if (vinfo
->s
== NULL
)
249 int SRP_user_pwd_set0_sv(SRP_user_pwd
*vinfo
, BIGNUM
*s
, BIGNUM
*v
)
252 BN_clear_free(vinfo
->v
);
255 return (vinfo
->s
!= NULL
&& vinfo
->v
!= NULL
);
258 static SRP_user_pwd
*srp_user_pwd_dup(SRP_user_pwd
*src
)
264 if ((ret
= SRP_user_pwd_new()) == NULL
)
267 SRP_user_pwd_set_gN(ret
, src
->g
, src
->N
);
268 if (!SRP_user_pwd_set1_ids(ret
, src
->id
, src
->info
)
269 || !SRP_user_pwd_set0_sv(ret
, BN_dup(src
->s
), BN_dup(src
->v
))) {
270 SRP_user_pwd_free(ret
);
276 SRP_VBASE
*SRP_VBASE_new(char *seed_key
)
278 SRP_VBASE
*vb
= OPENSSL_malloc(sizeof(*vb
));
282 if ((vb
->users_pwd
= sk_SRP_user_pwd_new_null()) == NULL
283 || (vb
->gN_cache
= sk_SRP_gN_cache_new_null()) == NULL
) {
284 sk_SRP_user_pwd_free(vb
->users_pwd
);
288 vb
->default_g
= NULL
;
289 vb
->default_N
= NULL
;
291 if ((seed_key
!= NULL
) && (vb
->seed_key
= OPENSSL_strdup(seed_key
)) == NULL
) {
292 sk_SRP_user_pwd_free(vb
->users_pwd
);
293 sk_SRP_gN_cache_free(vb
->gN_cache
);
300 void SRP_VBASE_free(SRP_VBASE
*vb
)
304 sk_SRP_user_pwd_pop_free(vb
->users_pwd
, SRP_user_pwd_free
);
305 sk_SRP_gN_cache_free(vb
->gN_cache
);
306 OPENSSL_free(vb
->seed_key
);
310 static SRP_gN_cache
*SRP_gN_new_init(const char *ch
)
312 unsigned char tmp
[MAX_LEN
];
314 SRP_gN_cache
*newgN
= OPENSSL_malloc(sizeof(*newgN
));
319 len
= t_fromb64(tmp
, sizeof(tmp
), ch
);
323 if ((newgN
->b64_bn
= OPENSSL_strdup(ch
)) == NULL
)
326 if ((newgN
->bn
= BN_bin2bn(tmp
, len
, NULL
)))
329 OPENSSL_free(newgN
->b64_bn
);
335 static void SRP_gN_free(SRP_gN_cache
*gN_cache
)
337 if (gN_cache
== NULL
)
339 OPENSSL_free(gN_cache
->b64_bn
);
340 BN_free(gN_cache
->bn
);
341 OPENSSL_free(gN_cache
);
344 static SRP_gN
*SRP_get_gN_by_id(const char *id
, STACK_OF(SRP_gN
) *gN_tab
)
349 if (gN_tab
!= NULL
) {
350 for (i
= 0; i
< sk_SRP_gN_num(gN_tab
); i
++) {
351 gN
= sk_SRP_gN_value(gN_tab
, i
);
352 if (gN
&& (id
== NULL
|| strcmp(gN
->id
, id
) == 0))
357 return SRP_get_default_gN(id
);
360 static BIGNUM
*SRP_gN_place_bn(STACK_OF(SRP_gN_cache
) *gN_cache
, char *ch
)
363 if (gN_cache
== NULL
)
366 /* search if we have already one... */
367 for (i
= 0; i
< sk_SRP_gN_cache_num(gN_cache
); i
++) {
368 SRP_gN_cache
*cache
= sk_SRP_gN_cache_value(gN_cache
, i
);
369 if (strcmp(cache
->b64_bn
, ch
) == 0)
372 { /* it is the first time that we find it */
373 SRP_gN_cache
*newgN
= SRP_gN_new_init(ch
);
375 if (sk_SRP_gN_cache_insert(gN_cache
, newgN
, 0) > 0)
384 * This function parses the verifier file generated by the srp app.
385 * The format for each entry is:
386 * V base64(verifier) base64(salt) username gNid userinfo(optional)
388 * I base64(N) base64(g)
389 * Note that base64 is the SRP variant of base64 encoding described
393 int SRP_VBASE_init(SRP_VBASE
*vb
, char *verifier_file
)
395 int error_code
= SRP_ERR_MEMORY
;
396 STACK_OF(SRP_gN
) *SRP_gN_tab
= sk_SRP_gN_new_null();
397 char *last_index
= NULL
;
402 SRP_user_pwd
*user_pwd
= NULL
;
404 TXT_DB
*tmpdb
= NULL
;
405 BIO
*in
= BIO_new(BIO_s_file());
407 if (SRP_gN_tab
== NULL
)
410 error_code
= SRP_ERR_OPEN_FILE
;
412 if (in
== NULL
|| BIO_read_filename(in
, verifier_file
) <= 0)
415 error_code
= SRP_ERR_VBASE_INCOMPLETE_FILE
;
417 if ((tmpdb
= TXT_DB_read(in
, DB_NUMBER
)) == NULL
)
420 error_code
= SRP_ERR_MEMORY
;
423 last_index
= SRP_get_default_gN(NULL
)->id
;
425 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(tmpdb
->data
); i
++) {
426 pp
= sk_OPENSSL_PSTRING_value(tmpdb
->data
, i
);
427 if (pp
[DB_srptype
][0] == DB_SRP_INDEX
) {
429 * we add this couple in the internal Stack
432 if ((gN
= OPENSSL_malloc(sizeof(*gN
))) == NULL
)
435 if ((gN
->id
= OPENSSL_strdup(pp
[DB_srpid
])) == NULL
436 || (gN
->N
= SRP_gN_place_bn(vb
->gN_cache
, pp
[DB_srpverifier
]))
438 || (gN
->g
= SRP_gN_place_bn(vb
->gN_cache
, pp
[DB_srpsalt
]))
440 || sk_SRP_gN_insert(SRP_gN_tab
, gN
, 0) == 0)
445 if (vb
->seed_key
!= NULL
) {
446 last_index
= pp
[DB_srpid
];
448 } else if (pp
[DB_srptype
][0] == DB_SRP_VALID
) {
449 /* it is a user .... */
452 if ((lgN
= SRP_get_gN_by_id(pp
[DB_srpgN
], SRP_gN_tab
)) != NULL
) {
453 error_code
= SRP_ERR_MEMORY
;
454 if ((user_pwd
= SRP_user_pwd_new()) == NULL
)
457 SRP_user_pwd_set_gN(user_pwd
, lgN
->g
, lgN
->N
);
458 if (!SRP_user_pwd_set1_ids
459 (user_pwd
, pp
[DB_srpid
], pp
[DB_srpinfo
]))
462 error_code
= SRP_ERR_VBASE_BN_LIB
;
463 if (!SRP_user_pwd_set_sv
464 (user_pwd
, pp
[DB_srpsalt
], pp
[DB_srpverifier
]))
467 if (sk_SRP_user_pwd_insert(vb
->users_pwd
, user_pwd
, 0) == 0)
469 user_pwd
= NULL
; /* abandon responsibility */
474 if (last_index
!= NULL
) {
475 /* this means that we want to simulate a default user */
477 if (((gN
= SRP_get_gN_by_id(last_index
, SRP_gN_tab
)) == NULL
)) {
478 error_code
= SRP_ERR_VBASE_BN_LIB
;
481 vb
->default_g
= gN
->g
;
482 vb
->default_N
= gN
->N
;
485 error_code
= SRP_NO_ERROR
;
489 * there may be still some leaks to fix, if this fails, the application
490 * terminates most likely
494 OPENSSL_free(gN
->id
);
498 SRP_user_pwd_free(user_pwd
);
503 sk_SRP_gN_free(SRP_gN_tab
);
509 static SRP_user_pwd
*find_user(SRP_VBASE
*vb
, char *username
)
517 for (i
= 0; i
< sk_SRP_user_pwd_num(vb
->users_pwd
); i
++) {
518 user
= sk_SRP_user_pwd_value(vb
->users_pwd
, i
);
519 if (strcmp(user
->id
, username
) == 0)
526 int SRP_VBASE_add0_user(SRP_VBASE
*vb
, SRP_user_pwd
*user_pwd
)
528 if (sk_SRP_user_pwd_push(vb
->users_pwd
, user_pwd
) <= 0)
533 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
535 * DEPRECATED: use SRP_VBASE_get1_by_user instead.
536 * This method ignores the configured seed and fails for an unknown user.
537 * Ownership of the returned pointer is not released to the caller.
538 * In other words, caller must not free the result.
540 SRP_user_pwd
*SRP_VBASE_get_by_user(SRP_VBASE
*vb
, char *username
)
542 return find_user(vb
, username
);
547 * Ownership of the returned pointer is released to the caller.
548 * In other words, caller must free the result once done.
550 SRP_user_pwd
*SRP_VBASE_get1_by_user(SRP_VBASE
*vb
, char *username
)
553 unsigned char digv
[SHA_DIGEST_LENGTH
];
554 unsigned char digs
[SHA_DIGEST_LENGTH
];
555 EVP_MD_CTX
*ctxt
= NULL
;
561 if ((user
= find_user(vb
, username
)) != NULL
)
562 return srp_user_pwd_dup(user
);
564 if ((vb
->seed_key
== NULL
) ||
565 (vb
->default_g
== NULL
) || (vb
->default_N
== NULL
))
568 /* if the user is unknown we set parameters as well if we have a seed_key */
570 if ((user
= SRP_user_pwd_new()) == NULL
)
573 SRP_user_pwd_set_gN(user
, vb
->default_g
, vb
->default_N
);
575 if (!SRP_user_pwd_set1_ids(user
, username
, NULL
))
578 if (RAND_priv_bytes(digv
, SHA_DIGEST_LENGTH
) <= 0)
580 md
= EVP_MD_fetch(NULL
, SN_sha1
, NULL
);
583 ctxt
= EVP_MD_CTX_new();
585 || !EVP_DigestInit_ex(ctxt
, md
, NULL
)
586 || !EVP_DigestUpdate(ctxt
, vb
->seed_key
, strlen(vb
->seed_key
))
587 || !EVP_DigestUpdate(ctxt
, username
, strlen(username
))
588 || !EVP_DigestFinal_ex(ctxt
, digs
, NULL
))
590 EVP_MD_CTX_free(ctxt
);
594 if (SRP_user_pwd_set0_sv(user
,
595 BN_bin2bn(digs
, SHA_DIGEST_LENGTH
, NULL
),
596 BN_bin2bn(digv
, SHA_DIGEST_LENGTH
, NULL
)))
601 EVP_MD_CTX_free(ctxt
);
602 SRP_user_pwd_free(user
);
607 * create a verifier (*salt,*verifier,g and N are in base64)
609 char *SRP_create_verifier_ex(const char *user
, const char *pass
, char **salt
,
610 char **verifier
, const char *N
, const char *g
,
611 OSSL_LIB_CTX
*libctx
, const char *propq
)
614 char *result
= NULL
, *vf
= NULL
;
615 const BIGNUM
*N_bn
= NULL
, *g_bn
= NULL
;
616 BIGNUM
*N_bn_alloc
= NULL
, *g_bn_alloc
= NULL
, *s
= NULL
, *v
= NULL
;
617 unsigned char tmp
[MAX_LEN
];
618 unsigned char tmp2
[MAX_LEN
];
619 char *defgNid
= NULL
;
622 if ((user
== NULL
) ||
623 (pass
== NULL
) || (salt
== NULL
) || (verifier
== NULL
))
627 if ((len
= t_fromb64(tmp
, sizeof(tmp
), N
)) <= 0)
629 N_bn_alloc
= BN_bin2bn(tmp
, len
, NULL
);
630 if (N_bn_alloc
== NULL
)
633 if ((len
= t_fromb64(tmp
, sizeof(tmp
), g
)) <= 0)
635 g_bn_alloc
= BN_bin2bn(tmp
, len
, NULL
);
636 if (g_bn_alloc
== NULL
)
641 SRP_gN
*gN
= SRP_get_default_gN(g
);
650 if (RAND_bytes_ex(libctx
, tmp2
, SRP_RANDOM_SALT_LEN
, 0) <= 0)
653 s
= BN_bin2bn(tmp2
, SRP_RANDOM_SALT_LEN
, NULL
);
655 if ((len
= t_fromb64(tmp2
, sizeof(tmp2
), *salt
)) <= 0)
657 s
= BN_bin2bn(tmp2
, len
, NULL
);
662 if (!SRP_create_verifier_BN_ex(user
, pass
, &s
, &v
, N_bn
, g_bn
, libctx
,
666 if (BN_bn2bin(v
, tmp
) < 0)
668 vfsize
= BN_num_bytes(v
) * 2;
669 if (((vf
= OPENSSL_malloc(vfsize
)) == NULL
))
671 if (!t_tob64(vf
, tmp
, BN_num_bytes(v
)))
677 if ((tmp_salt
= OPENSSL_malloc(SRP_RANDOM_SALT_LEN
* 2)) == NULL
) {
680 if (!t_tob64(tmp_salt
, tmp2
, SRP_RANDOM_SALT_LEN
)) {
681 OPENSSL_free(tmp_salt
);
694 OPENSSL_clear_free(vf
, vfsize
);
700 char *SRP_create_verifier(const char *user
, const char *pass
, char **salt
,
701 char **verifier
, const char *N
, const char *g
)
703 return SRP_create_verifier_ex(user
, pass
, salt
, verifier
, N
, g
, NULL
, NULL
);
707 * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
708 * then the provided salt will be used. On successful exit *verifier will point
709 * to a newly allocated BIGNUM containing the verifier and (if a salt was not
710 * provided) *salt will be populated with a newly allocated BIGNUM containing a
712 * The caller is responsible for freeing the allocated *salt and *verifier
715 int SRP_create_verifier_BN_ex(const char *user
, const char *pass
, BIGNUM
**salt
,
716 BIGNUM
**verifier
, const BIGNUM
*N
,
717 const BIGNUM
*g
, OSSL_LIB_CTX
*libctx
,
722 BN_CTX
*bn_ctx
= BN_CTX_new_ex(libctx
);
723 unsigned char tmp2
[MAX_LEN
];
724 BIGNUM
*salttmp
= NULL
, *verif
;
726 if ((user
== NULL
) ||
729 (verifier
== NULL
) || (N
== NULL
) || (g
== NULL
) || (bn_ctx
== NULL
))
733 if (RAND_bytes_ex(libctx
, tmp2
, SRP_RANDOM_SALT_LEN
, 0) <= 0)
736 salttmp
= BN_bin2bn(tmp2
, SRP_RANDOM_SALT_LEN
, NULL
);
743 x
= SRP_Calc_x_ex(salttmp
, user
, pass
, libctx
, propq
);
751 if (!BN_mod_exp(verif
, g
, x
, N
, bn_ctx
)) {
752 BN_clear_free(verif
);
761 if (salt
!= NULL
&& *salt
!= salttmp
)
762 BN_clear_free(salttmp
);
768 int SRP_create_verifier_BN(const char *user
, const char *pass
, BIGNUM
**salt
,
769 BIGNUM
**verifier
, const BIGNUM
*N
,
772 return SRP_create_verifier_BN_ex(user
, pass
, salt
, verifier
, N
, g
, NULL
,