2 * Copyright 2004-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 #include <openssl/crypto.h>
11 #include <openssl/rand.h>
12 #include <openssl/err.h>
15 #ifndef OPENSSL_NO_SRP
16 # include <openssl/srp.h>
18 int SSL_CTX_SRP_CTX_free(struct ssl_ctx_st
*ctx
)
22 OPENSSL_free(ctx
->srp_ctx
.login
);
23 OPENSSL_free(ctx
->srp_ctx
.info
);
24 BN_free(ctx
->srp_ctx
.N
);
25 BN_free(ctx
->srp_ctx
.g
);
26 BN_free(ctx
->srp_ctx
.s
);
27 BN_free(ctx
->srp_ctx
.B
);
28 BN_free(ctx
->srp_ctx
.A
);
29 BN_free(ctx
->srp_ctx
.a
);
30 BN_free(ctx
->srp_ctx
.b
);
31 BN_free(ctx
->srp_ctx
.v
);
32 memset(&ctx
->srp_ctx
, 0, sizeof(ctx
->srp_ctx
));
33 ctx
->srp_ctx
.strength
= SRP_MINIMAL_N
;
37 int SSL_SRP_CTX_free(struct ssl_st
*s
)
41 OPENSSL_free(s
->srp_ctx
.login
);
42 OPENSSL_free(s
->srp_ctx
.info
);
43 BN_free(s
->srp_ctx
.N
);
44 BN_free(s
->srp_ctx
.g
);
45 BN_free(s
->srp_ctx
.s
);
46 BN_free(s
->srp_ctx
.B
);
47 BN_free(s
->srp_ctx
.A
);
48 BN_free(s
->srp_ctx
.a
);
49 BN_free(s
->srp_ctx
.b
);
50 BN_free(s
->srp_ctx
.v
);
51 memset(&s
->srp_ctx
, 0, sizeof(s
->srp_ctx
));
52 s
->srp_ctx
.strength
= SRP_MINIMAL_N
;
56 int SSL_SRP_CTX_init(struct ssl_st
*s
)
60 if ((s
== NULL
) || ((ctx
= s
->ctx
) == NULL
))
63 memset(&s
->srp_ctx
, 0, sizeof(s
->srp_ctx
));
65 s
->srp_ctx
.SRP_cb_arg
= ctx
->srp_ctx
.SRP_cb_arg
;
66 /* set client Hello login callback */
67 s
->srp_ctx
.TLS_ext_srp_username_callback
=
68 ctx
->srp_ctx
.TLS_ext_srp_username_callback
;
69 /* set SRP N/g param callback for verification */
70 s
->srp_ctx
.SRP_verify_param_callback
=
71 ctx
->srp_ctx
.SRP_verify_param_callback
;
72 /* set SRP client passwd callback */
73 s
->srp_ctx
.SRP_give_srp_client_pwd_callback
=
74 ctx
->srp_ctx
.SRP_give_srp_client_pwd_callback
;
76 s
->srp_ctx
.strength
= ctx
->srp_ctx
.strength
;
78 if (((ctx
->srp_ctx
.N
!= NULL
) &&
79 ((s
->srp_ctx
.N
= BN_dup(ctx
->srp_ctx
.N
)) == NULL
)) ||
80 ((ctx
->srp_ctx
.g
!= NULL
) &&
81 ((s
->srp_ctx
.g
= BN_dup(ctx
->srp_ctx
.g
)) == NULL
)) ||
82 ((ctx
->srp_ctx
.s
!= NULL
) &&
83 ((s
->srp_ctx
.s
= BN_dup(ctx
->srp_ctx
.s
)) == NULL
)) ||
84 ((ctx
->srp_ctx
.B
!= NULL
) &&
85 ((s
->srp_ctx
.B
= BN_dup(ctx
->srp_ctx
.B
)) == NULL
)) ||
86 ((ctx
->srp_ctx
.A
!= NULL
) &&
87 ((s
->srp_ctx
.A
= BN_dup(ctx
->srp_ctx
.A
)) == NULL
)) ||
88 ((ctx
->srp_ctx
.a
!= NULL
) &&
89 ((s
->srp_ctx
.a
= BN_dup(ctx
->srp_ctx
.a
)) == NULL
)) ||
90 ((ctx
->srp_ctx
.v
!= NULL
) &&
91 ((s
->srp_ctx
.v
= BN_dup(ctx
->srp_ctx
.v
)) == NULL
)) ||
92 ((ctx
->srp_ctx
.b
!= NULL
) &&
93 ((s
->srp_ctx
.b
= BN_dup(ctx
->srp_ctx
.b
)) == NULL
))) {
94 SSLerr(SSL_F_SSL_SRP_CTX_INIT
, ERR_R_BN_LIB
);
97 if ((ctx
->srp_ctx
.login
!= NULL
) &&
98 ((s
->srp_ctx
.login
= OPENSSL_strdup(ctx
->srp_ctx
.login
)) == NULL
)) {
99 SSLerr(SSL_F_SSL_SRP_CTX_INIT
, ERR_R_INTERNAL_ERROR
);
102 if ((ctx
->srp_ctx
.info
!= NULL
) &&
103 ((s
->srp_ctx
.info
= BUF_strdup(ctx
->srp_ctx
.info
)) == NULL
)) {
104 SSLerr(SSL_F_SSL_SRP_CTX_INIT
, ERR_R_INTERNAL_ERROR
);
107 s
->srp_ctx
.srp_Mask
= ctx
->srp_ctx
.srp_Mask
;
111 OPENSSL_free(s
->srp_ctx
.login
);
112 OPENSSL_free(s
->srp_ctx
.info
);
113 BN_free(s
->srp_ctx
.N
);
114 BN_free(s
->srp_ctx
.g
);
115 BN_free(s
->srp_ctx
.s
);
116 BN_free(s
->srp_ctx
.B
);
117 BN_free(s
->srp_ctx
.A
);
118 BN_free(s
->srp_ctx
.a
);
119 BN_free(s
->srp_ctx
.b
);
120 BN_free(s
->srp_ctx
.v
);
121 memset(&s
->srp_ctx
, 0, sizeof(s
->srp_ctx
));
125 int SSL_CTX_SRP_CTX_init(struct ssl_ctx_st
*ctx
)
130 memset(&ctx
->srp_ctx
, 0, sizeof(ctx
->srp_ctx
));
131 ctx
->srp_ctx
.strength
= SRP_MINIMAL_N
;
137 int SSL_srp_server_param_with_username(SSL
*s
, int *ad
)
139 unsigned char b
[SSL_MAX_MASTER_KEY_LENGTH
];
142 *ad
= SSL_AD_UNKNOWN_PSK_IDENTITY
;
143 if ((s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
) &&
145 s
->srp_ctx
.TLS_ext_srp_username_callback(s
, ad
,
146 s
->srp_ctx
.SRP_cb_arg
)) !=
150 *ad
= SSL_AD_INTERNAL_ERROR
;
151 if ((s
->srp_ctx
.N
== NULL
) ||
152 (s
->srp_ctx
.g
== NULL
) ||
153 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.v
== NULL
))
154 return SSL3_AL_FATAL
;
156 if (ssl_randbytes(s
, b
, sizeof(b
)) <= 0)
157 return SSL3_AL_FATAL
;
158 s
->srp_ctx
.b
= BN_bin2bn(b
, sizeof(b
), NULL
);
159 OPENSSL_cleanse(b
, sizeof(b
));
161 /* Calculate: B = (kv + g^b) % N */
163 return ((s
->srp_ctx
.B
=
164 SRP_Calc_B(s
->srp_ctx
.b
, s
->srp_ctx
.N
, s
->srp_ctx
.g
,
166 NULL
) ? SSL_ERROR_NONE
: SSL3_AL_FATAL
;
170 * If the server just has the raw password, make up a verifier entry on the
173 int SSL_set_srp_server_param_pw(SSL
*s
, const char *user
, const char *pass
,
176 SRP_gN
*GN
= SRP_get_default_gN(grp
);
179 s
->srp_ctx
.N
= BN_dup(GN
->N
);
180 s
->srp_ctx
.g
= BN_dup(GN
->g
);
181 BN_clear_free(s
->srp_ctx
.v
);
183 BN_clear_free(s
->srp_ctx
.s
);
185 if (!SRP_create_verifier_BN
186 (user
, pass
, &s
->srp_ctx
.s
, &s
->srp_ctx
.v
, GN
->N
, GN
->g
))
192 int SSL_set_srp_server_param(SSL
*s
, const BIGNUM
*N
, const BIGNUM
*g
,
193 BIGNUM
*sa
, BIGNUM
*v
, char *info
)
196 if (s
->srp_ctx
.N
!= NULL
) {
197 if (!BN_copy(s
->srp_ctx
.N
, N
)) {
198 BN_free(s
->srp_ctx
.N
);
202 s
->srp_ctx
.N
= BN_dup(N
);
205 if (s
->srp_ctx
.g
!= NULL
) {
206 if (!BN_copy(s
->srp_ctx
.g
, g
)) {
207 BN_free(s
->srp_ctx
.g
);
211 s
->srp_ctx
.g
= BN_dup(g
);
214 if (s
->srp_ctx
.s
!= NULL
) {
215 if (!BN_copy(s
->srp_ctx
.s
, sa
)) {
216 BN_free(s
->srp_ctx
.s
);
220 s
->srp_ctx
.s
= BN_dup(sa
);
223 if (s
->srp_ctx
.v
!= NULL
) {
224 if (!BN_copy(s
->srp_ctx
.v
, v
)) {
225 BN_free(s
->srp_ctx
.v
);
229 s
->srp_ctx
.v
= BN_dup(v
);
233 OPENSSL_free(s
->srp_ctx
.info
);
234 if ((s
->srp_ctx
.info
= BUF_strdup(info
)) == NULL
)
238 if (!(s
->srp_ctx
.N
) ||
239 !(s
->srp_ctx
.g
) || !(s
->srp_ctx
.s
) || !(s
->srp_ctx
.v
))
245 int srp_generate_server_master_secret(SSL
*s
)
247 BIGNUM
*K
= NULL
, *u
= NULL
;
248 int ret
= -1, tmp_len
= 0;
249 unsigned char *tmp
= NULL
;
251 if (!SRP_Verify_A_mod_N(s
->srp_ctx
.A
, s
->srp_ctx
.N
))
253 if ((u
= SRP_Calc_u(s
->srp_ctx
.A
, s
->srp_ctx
.B
, s
->srp_ctx
.N
)) == NULL
)
255 if ((K
= SRP_Calc_server_key(s
->srp_ctx
.A
, s
->srp_ctx
.v
, u
, s
->srp_ctx
.b
,
256 s
->srp_ctx
.N
)) == NULL
)
259 tmp_len
= BN_num_bytes(K
);
260 if ((tmp
= OPENSSL_malloc(tmp_len
)) == NULL
) {
261 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
262 SSL_F_SRP_GENERATE_SERVER_MASTER_SECRET
, ERR_R_MALLOC_FAILURE
);
266 /* Calls SSLfatal() as required */
267 ret
= ssl_generate_master_secret(s
, tmp
, tmp_len
, 1);
275 int srp_generate_client_master_secret(SSL
*s
)
277 BIGNUM
*x
= NULL
, *u
= NULL
, *K
= NULL
;
278 int ret
= -1, tmp_len
= 0;
280 unsigned char *tmp
= NULL
;
283 * Checks if b % n == 0
285 if (SRP_Verify_B_mod_N(s
->srp_ctx
.B
, s
->srp_ctx
.N
) == 0
286 || (u
= SRP_Calc_u(s
->srp_ctx
.A
, s
->srp_ctx
.B
, s
->srp_ctx
.N
))
288 || s
->srp_ctx
.SRP_give_srp_client_pwd_callback
== NULL
) {
289 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
290 SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET
, ERR_R_INTERNAL_ERROR
);
293 if ((passwd
= s
->srp_ctx
.SRP_give_srp_client_pwd_callback(s
,
294 s
->srp_ctx
.SRP_cb_arg
))
296 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
297 SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET
,
298 SSL_R_CALLBACK_FAILED
);
301 if ((x
= SRP_Calc_x(s
->srp_ctx
.s
, s
->srp_ctx
.login
, passwd
)) == NULL
302 || (K
= SRP_Calc_client_key(s
->srp_ctx
.N
, s
->srp_ctx
.B
,
304 s
->srp_ctx
.a
, u
)) == NULL
) {
305 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
306 SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET
, ERR_R_INTERNAL_ERROR
);
310 tmp_len
= BN_num_bytes(K
);
311 if ((tmp
= OPENSSL_malloc(tmp_len
)) == NULL
) {
312 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
313 SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET
, ERR_R_MALLOC_FAILURE
);
317 /* Calls SSLfatal() as required */
318 ret
= ssl_generate_master_secret(s
, tmp
, tmp_len
, 1);
323 OPENSSL_clear_free(passwd
, strlen(passwd
));
328 int srp_verify_server_param(SSL
*s
)
330 SRP_CTX
*srp
= &s
->srp_ctx
;
332 * Sanity check parameters: we can quickly check B % N == 0 by checking B
335 if (BN_ucmp(srp
->g
, srp
->N
) >= 0 || BN_ucmp(srp
->B
, srp
->N
) >= 0
336 || BN_is_zero(srp
->B
)) {
337 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_SRP_VERIFY_SERVER_PARAM
,
342 if (BN_num_bits(srp
->N
) < srp
->strength
) {
343 SSLfatal(s
, SSL_AD_INSUFFICIENT_SECURITY
, SSL_F_SRP_VERIFY_SERVER_PARAM
,
344 SSL_R_INSUFFICIENT_SECURITY
);
348 if (srp
->SRP_verify_param_callback
) {
349 if (srp
->SRP_verify_param_callback(s
, srp
->SRP_cb_arg
) <= 0) {
350 SSLfatal(s
, SSL_AD_INSUFFICIENT_SECURITY
,
351 SSL_F_SRP_VERIFY_SERVER_PARAM
,
352 SSL_R_CALLBACK_FAILED
);
355 } else if (!SRP_check_known_gN_param(srp
->g
, srp
->N
)) {
356 SSLfatal(s
, SSL_AD_INSUFFICIENT_SECURITY
, SSL_F_SRP_VERIFY_SERVER_PARAM
,
357 SSL_R_INSUFFICIENT_SECURITY
);
364 int SRP_Calc_A_param(SSL
*s
)
366 unsigned char rnd
[SSL_MAX_MASTER_KEY_LENGTH
];
368 if (ssl_randbytes(s
, rnd
, sizeof(rnd
)) <= 0)
370 s
->srp_ctx
.a
= BN_bin2bn(rnd
, sizeof(rnd
), s
->srp_ctx
.a
);
371 OPENSSL_cleanse(rnd
, sizeof(rnd
));
373 if (!(s
->srp_ctx
.A
= SRP_Calc_A(s
->srp_ctx
.a
, s
->srp_ctx
.N
, s
->srp_ctx
.g
)))
379 BIGNUM
*SSL_get_srp_g(SSL
*s
)
381 if (s
->srp_ctx
.g
!= NULL
)
383 return s
->ctx
->srp_ctx
.g
;
386 BIGNUM
*SSL_get_srp_N(SSL
*s
)
388 if (s
->srp_ctx
.N
!= NULL
)
390 return s
->ctx
->srp_ctx
.N
;
393 char *SSL_get_srp_username(SSL
*s
)
395 if (s
->srp_ctx
.login
!= NULL
)
396 return s
->srp_ctx
.login
;
397 return s
->ctx
->srp_ctx
.login
;
400 char *SSL_get_srp_userinfo(SSL
*s
)
402 if (s
->srp_ctx
.info
!= NULL
)
403 return s
->srp_ctx
.info
;
404 return s
->ctx
->srp_ctx
.info
;
407 # define tls1_ctx_ctrl ssl3_ctx_ctrl
408 # define tls1_ctx_callback_ctrl ssl3_ctx_callback_ctrl
410 int SSL_CTX_set_srp_username(SSL_CTX
*ctx
, char *name
)
412 return tls1_ctx_ctrl(ctx
, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME
, 0, name
);
415 int SSL_CTX_set_srp_password(SSL_CTX
*ctx
, char *password
)
417 return tls1_ctx_ctrl(ctx
, SSL_CTRL_SET_TLS_EXT_SRP_PASSWORD
, 0, password
);
420 int SSL_CTX_set_srp_strength(SSL_CTX
*ctx
, int strength
)
422 return tls1_ctx_ctrl(ctx
, SSL_CTRL_SET_TLS_EXT_SRP_STRENGTH
, strength
,
426 int SSL_CTX_set_srp_verify_param_callback(SSL_CTX
*ctx
,
427 int (*cb
) (SSL
*, void *))
429 return tls1_ctx_callback_ctrl(ctx
, SSL_CTRL_SET_SRP_VERIFY_PARAM_CB
,
433 int SSL_CTX_set_srp_cb_arg(SSL_CTX
*ctx
, void *arg
)
435 return tls1_ctx_ctrl(ctx
, SSL_CTRL_SET_SRP_ARG
, 0, arg
);
438 int SSL_CTX_set_srp_username_callback(SSL_CTX
*ctx
,
439 int (*cb
) (SSL
*, int *, void *))
441 return tls1_ctx_callback_ctrl(ctx
, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME_CB
,
445 int SSL_CTX_set_srp_client_pwd_callback(SSL_CTX
*ctx
,
446 char *(*cb
) (SSL
*, void *))
448 return tls1_ctx_callback_ctrl(ctx
, SSL_CTRL_SET_SRP_GIVE_CLIENT_PWD_CB
,