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 BN_free(ctx
->srp_ctx
.N
);
24 BN_free(ctx
->srp_ctx
.g
);
25 BN_free(ctx
->srp_ctx
.s
);
26 BN_free(ctx
->srp_ctx
.B
);
27 BN_free(ctx
->srp_ctx
.A
);
28 BN_free(ctx
->srp_ctx
.a
);
29 BN_free(ctx
->srp_ctx
.b
);
30 BN_free(ctx
->srp_ctx
.v
);
31 ctx
->srp_ctx
.TLS_ext_srp_username_callback
= NULL
;
32 ctx
->srp_ctx
.SRP_cb_arg
= NULL
;
33 ctx
->srp_ctx
.SRP_verify_param_callback
= NULL
;
34 ctx
->srp_ctx
.SRP_give_srp_client_pwd_callback
= NULL
;
35 ctx
->srp_ctx
.N
= NULL
;
36 ctx
->srp_ctx
.g
= NULL
;
37 ctx
->srp_ctx
.s
= NULL
;
38 ctx
->srp_ctx
.B
= NULL
;
39 ctx
->srp_ctx
.A
= NULL
;
40 ctx
->srp_ctx
.a
= NULL
;
41 ctx
->srp_ctx
.b
= NULL
;
42 ctx
->srp_ctx
.v
= NULL
;
43 ctx
->srp_ctx
.login
= NULL
;
44 ctx
->srp_ctx
.info
= NULL
;
45 ctx
->srp_ctx
.strength
= SRP_MINIMAL_N
;
46 ctx
->srp_ctx
.srp_Mask
= 0;
50 int SSL_SRP_CTX_free(struct ssl_st
*s
)
54 OPENSSL_free(s
->srp_ctx
.login
);
55 BN_free(s
->srp_ctx
.N
);
56 BN_free(s
->srp_ctx
.g
);
57 BN_free(s
->srp_ctx
.s
);
58 BN_free(s
->srp_ctx
.B
);
59 BN_free(s
->srp_ctx
.A
);
60 BN_free(s
->srp_ctx
.a
);
61 BN_free(s
->srp_ctx
.b
);
62 BN_free(s
->srp_ctx
.v
);
63 s
->srp_ctx
.TLS_ext_srp_username_callback
= NULL
;
64 s
->srp_ctx
.SRP_cb_arg
= NULL
;
65 s
->srp_ctx
.SRP_verify_param_callback
= NULL
;
66 s
->srp_ctx
.SRP_give_srp_client_pwd_callback
= NULL
;
75 s
->srp_ctx
.login
= NULL
;
76 s
->srp_ctx
.info
= NULL
;
77 s
->srp_ctx
.strength
= SRP_MINIMAL_N
;
78 s
->srp_ctx
.srp_Mask
= 0;
82 int SSL_SRP_CTX_init(struct ssl_st
*s
)
86 if ((s
== NULL
) || ((ctx
= s
->ctx
) == NULL
))
88 s
->srp_ctx
.SRP_cb_arg
= ctx
->srp_ctx
.SRP_cb_arg
;
89 /* set client Hello login callback */
90 s
->srp_ctx
.TLS_ext_srp_username_callback
=
91 ctx
->srp_ctx
.TLS_ext_srp_username_callback
;
92 /* set SRP N/g param callback for verification */
93 s
->srp_ctx
.SRP_verify_param_callback
=
94 ctx
->srp_ctx
.SRP_verify_param_callback
;
95 /* set SRP client passwd callback */
96 s
->srp_ctx
.SRP_give_srp_client_pwd_callback
=
97 ctx
->srp_ctx
.SRP_give_srp_client_pwd_callback
;
107 s
->srp_ctx
.login
= NULL
;
108 s
->srp_ctx
.info
= ctx
->srp_ctx
.info
;
109 s
->srp_ctx
.strength
= ctx
->srp_ctx
.strength
;
111 if (((ctx
->srp_ctx
.N
!= NULL
) &&
112 ((s
->srp_ctx
.N
= BN_dup(ctx
->srp_ctx
.N
)) == NULL
)) ||
113 ((ctx
->srp_ctx
.g
!= NULL
) &&
114 ((s
->srp_ctx
.g
= BN_dup(ctx
->srp_ctx
.g
)) == NULL
)) ||
115 ((ctx
->srp_ctx
.s
!= NULL
) &&
116 ((s
->srp_ctx
.s
= BN_dup(ctx
->srp_ctx
.s
)) == NULL
)) ||
117 ((ctx
->srp_ctx
.B
!= NULL
) &&
118 ((s
->srp_ctx
.B
= BN_dup(ctx
->srp_ctx
.B
)) == NULL
)) ||
119 ((ctx
->srp_ctx
.A
!= NULL
) &&
120 ((s
->srp_ctx
.A
= BN_dup(ctx
->srp_ctx
.A
)) == NULL
)) ||
121 ((ctx
->srp_ctx
.a
!= NULL
) &&
122 ((s
->srp_ctx
.a
= BN_dup(ctx
->srp_ctx
.a
)) == NULL
)) ||
123 ((ctx
->srp_ctx
.v
!= NULL
) &&
124 ((s
->srp_ctx
.v
= BN_dup(ctx
->srp_ctx
.v
)) == NULL
)) ||
125 ((ctx
->srp_ctx
.b
!= NULL
) &&
126 ((s
->srp_ctx
.b
= BN_dup(ctx
->srp_ctx
.b
)) == NULL
))) {
127 SSLerr(SSL_F_SSL_SRP_CTX_INIT
, ERR_R_BN_LIB
);
130 if ((ctx
->srp_ctx
.login
!= NULL
) &&
131 ((s
->srp_ctx
.login
= OPENSSL_strdup(ctx
->srp_ctx
.login
)) == NULL
)) {
132 SSLerr(SSL_F_SSL_SRP_CTX_INIT
, ERR_R_INTERNAL_ERROR
);
135 s
->srp_ctx
.srp_Mask
= ctx
->srp_ctx
.srp_Mask
;
139 OPENSSL_free(s
->srp_ctx
.login
);
140 BN_free(s
->srp_ctx
.N
);
141 BN_free(s
->srp_ctx
.g
);
142 BN_free(s
->srp_ctx
.s
);
143 BN_free(s
->srp_ctx
.B
);
144 BN_free(s
->srp_ctx
.A
);
145 BN_free(s
->srp_ctx
.a
);
146 BN_free(s
->srp_ctx
.b
);
147 BN_free(s
->srp_ctx
.v
);
151 int SSL_CTX_SRP_CTX_init(struct ssl_ctx_st
*ctx
)
156 ctx
->srp_ctx
.SRP_cb_arg
= NULL
;
157 /* set client Hello login callback */
158 ctx
->srp_ctx
.TLS_ext_srp_username_callback
= NULL
;
159 /* set SRP N/g param callback for verification */
160 ctx
->srp_ctx
.SRP_verify_param_callback
= NULL
;
161 /* set SRP client passwd callback */
162 ctx
->srp_ctx
.SRP_give_srp_client_pwd_callback
= NULL
;
164 ctx
->srp_ctx
.N
= NULL
;
165 ctx
->srp_ctx
.g
= NULL
;
166 ctx
->srp_ctx
.s
= NULL
;
167 ctx
->srp_ctx
.B
= NULL
;
168 ctx
->srp_ctx
.A
= NULL
;
169 ctx
->srp_ctx
.a
= NULL
;
170 ctx
->srp_ctx
.b
= NULL
;
171 ctx
->srp_ctx
.v
= NULL
;
172 ctx
->srp_ctx
.login
= NULL
;
173 ctx
->srp_ctx
.srp_Mask
= 0;
174 ctx
->srp_ctx
.info
= NULL
;
175 ctx
->srp_ctx
.strength
= SRP_MINIMAL_N
;
181 int SSL_srp_server_param_with_username(SSL
*s
, int *ad
)
183 unsigned char b
[SSL_MAX_MASTER_KEY_LENGTH
];
186 *ad
= SSL_AD_UNKNOWN_PSK_IDENTITY
;
187 if ((s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
) &&
189 s
->srp_ctx
.TLS_ext_srp_username_callback(s
, ad
,
190 s
->srp_ctx
.SRP_cb_arg
)) !=
194 *ad
= SSL_AD_INTERNAL_ERROR
;
195 if ((s
->srp_ctx
.N
== NULL
) ||
196 (s
->srp_ctx
.g
== NULL
) ||
197 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.v
== NULL
))
198 return SSL3_AL_FATAL
;
200 if (RAND_bytes(b
, sizeof(b
)) <= 0)
201 return SSL3_AL_FATAL
;
202 s
->srp_ctx
.b
= BN_bin2bn(b
, sizeof(b
), NULL
);
203 OPENSSL_cleanse(b
, sizeof(b
));
205 /* Calculate: B = (kv + g^b) % N */
207 return ((s
->srp_ctx
.B
=
208 SRP_Calc_B(s
->srp_ctx
.b
, s
->srp_ctx
.N
, s
->srp_ctx
.g
,
210 NULL
) ? SSL_ERROR_NONE
: SSL3_AL_FATAL
;
214 * If the server just has the raw password, make up a verifier entry on the
217 int SSL_set_srp_server_param_pw(SSL
*s
, const char *user
, const char *pass
,
220 SRP_gN
*GN
= SRP_get_default_gN(grp
);
223 s
->srp_ctx
.N
= BN_dup(GN
->N
);
224 s
->srp_ctx
.g
= BN_dup(GN
->g
);
225 BN_clear_free(s
->srp_ctx
.v
);
227 BN_clear_free(s
->srp_ctx
.s
);
229 if (!SRP_create_verifier_BN
230 (user
, pass
, &s
->srp_ctx
.s
, &s
->srp_ctx
.v
, GN
->N
, GN
->g
))
236 int SSL_set_srp_server_param(SSL
*s
, const BIGNUM
*N
, const BIGNUM
*g
,
237 BIGNUM
*sa
, BIGNUM
*v
, char *info
)
240 if (s
->srp_ctx
.N
!= NULL
) {
241 if (!BN_copy(s
->srp_ctx
.N
, N
)) {
242 BN_free(s
->srp_ctx
.N
);
246 s
->srp_ctx
.N
= BN_dup(N
);
249 if (s
->srp_ctx
.g
!= NULL
) {
250 if (!BN_copy(s
->srp_ctx
.g
, g
)) {
251 BN_free(s
->srp_ctx
.g
);
255 s
->srp_ctx
.g
= BN_dup(g
);
258 if (s
->srp_ctx
.s
!= NULL
) {
259 if (!BN_copy(s
->srp_ctx
.s
, sa
)) {
260 BN_free(s
->srp_ctx
.s
);
264 s
->srp_ctx
.s
= BN_dup(sa
);
267 if (s
->srp_ctx
.v
!= NULL
) {
268 if (!BN_copy(s
->srp_ctx
.v
, v
)) {
269 BN_free(s
->srp_ctx
.v
);
273 s
->srp_ctx
.v
= BN_dup(v
);
275 s
->srp_ctx
.info
= info
;
277 if (!(s
->srp_ctx
.N
) ||
278 !(s
->srp_ctx
.g
) || !(s
->srp_ctx
.s
) || !(s
->srp_ctx
.v
))
284 int srp_generate_server_master_secret(SSL
*s
)
286 BIGNUM
*K
= NULL
, *u
= NULL
;
287 int ret
= -1, tmp_len
= 0;
288 unsigned char *tmp
= NULL
;
290 if (!SRP_Verify_A_mod_N(s
->srp_ctx
.A
, s
->srp_ctx
.N
))
292 if ((u
= SRP_Calc_u(s
->srp_ctx
.A
, s
->srp_ctx
.B
, s
->srp_ctx
.N
)) == NULL
)
294 if ((K
= SRP_Calc_server_key(s
->srp_ctx
.A
, s
->srp_ctx
.v
, u
, s
->srp_ctx
.b
,
295 s
->srp_ctx
.N
)) == NULL
)
298 tmp_len
= BN_num_bytes(K
);
299 if ((tmp
= OPENSSL_malloc(tmp_len
)) == NULL
)
302 ret
= ssl_generate_master_secret(s
, tmp
, tmp_len
, 1);
310 int srp_generate_client_master_secret(SSL
*s
)
312 BIGNUM
*x
= NULL
, *u
= NULL
, *K
= NULL
;
313 int ret
= -1, tmp_len
= 0;
315 unsigned char *tmp
= NULL
;
318 * Checks if b % n == 0
320 if (SRP_Verify_B_mod_N(s
->srp_ctx
.B
, s
->srp_ctx
.N
) == 0)
322 if ((u
= SRP_Calc_u(s
->srp_ctx
.A
, s
->srp_ctx
.B
, s
->srp_ctx
.N
)) == NULL
)
324 if (s
->srp_ctx
.SRP_give_srp_client_pwd_callback
== NULL
)
328 s
->srp_ctx
.SRP_give_srp_client_pwd_callback(s
, s
->srp_ctx
.SRP_cb_arg
)))
330 if ((x
= SRP_Calc_x(s
->srp_ctx
.s
, s
->srp_ctx
.login
, passwd
)) == NULL
)
332 if ((K
= SRP_Calc_client_key(s
->srp_ctx
.N
, s
->srp_ctx
.B
, s
->srp_ctx
.g
, x
,
333 s
->srp_ctx
.a
, u
)) == NULL
)
336 tmp_len
= BN_num_bytes(K
);
337 if ((tmp
= OPENSSL_malloc(tmp_len
)) == NULL
)
340 ret
= ssl_generate_master_secret(s
, tmp
, tmp_len
, 1);
345 OPENSSL_clear_free(passwd
, strlen(passwd
));
350 int srp_verify_server_param(SSL
*s
, int *al
)
352 SRP_CTX
*srp
= &s
->srp_ctx
;
354 * Sanity check parameters: we can quickly check B % N == 0 by checking B
357 if (BN_ucmp(srp
->g
, srp
->N
) >= 0 || BN_ucmp(srp
->B
, srp
->N
) >= 0
358 || BN_is_zero(srp
->B
)) {
359 *al
= SSL3_AD_ILLEGAL_PARAMETER
;
363 if (BN_num_bits(srp
->N
) < srp
->strength
) {
364 *al
= TLS1_AD_INSUFFICIENT_SECURITY
;
368 if (srp
->SRP_verify_param_callback
) {
369 if (srp
->SRP_verify_param_callback(s
, srp
->SRP_cb_arg
) <= 0) {
370 *al
= TLS1_AD_INSUFFICIENT_SECURITY
;
373 } else if (!SRP_check_known_gN_param(srp
->g
, srp
->N
)) {
374 *al
= TLS1_AD_INSUFFICIENT_SECURITY
;
381 int SRP_Calc_A_param(SSL
*s
)
383 unsigned char rnd
[SSL_MAX_MASTER_KEY_LENGTH
];
385 if (RAND_bytes(rnd
, sizeof(rnd
)) <= 0)
387 s
->srp_ctx
.a
= BN_bin2bn(rnd
, sizeof(rnd
), s
->srp_ctx
.a
);
388 OPENSSL_cleanse(rnd
, sizeof(rnd
));
390 if (!(s
->srp_ctx
.A
= SRP_Calc_A(s
->srp_ctx
.a
, s
->srp_ctx
.N
, s
->srp_ctx
.g
)))
396 BIGNUM
*SSL_get_srp_g(SSL
*s
)
398 if (s
->srp_ctx
.g
!= NULL
)
400 return s
->ctx
->srp_ctx
.g
;
403 BIGNUM
*SSL_get_srp_N(SSL
*s
)
405 if (s
->srp_ctx
.N
!= NULL
)
407 return s
->ctx
->srp_ctx
.N
;
410 char *SSL_get_srp_username(SSL
*s
)
412 if (s
->srp_ctx
.login
!= NULL
)
413 return s
->srp_ctx
.login
;
414 return s
->ctx
->srp_ctx
.login
;
417 char *SSL_get_srp_userinfo(SSL
*s
)
419 if (s
->srp_ctx
.info
!= NULL
)
420 return s
->srp_ctx
.info
;
421 return s
->ctx
->srp_ctx
.info
;
424 # define tls1_ctx_ctrl ssl3_ctx_ctrl
425 # define tls1_ctx_callback_ctrl ssl3_ctx_callback_ctrl
427 int SSL_CTX_set_srp_username(SSL_CTX
*ctx
, char *name
)
429 return tls1_ctx_ctrl(ctx
, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME
, 0, name
);
432 int SSL_CTX_set_srp_password(SSL_CTX
*ctx
, char *password
)
434 return tls1_ctx_ctrl(ctx
, SSL_CTRL_SET_TLS_EXT_SRP_PASSWORD
, 0, password
);
437 int SSL_CTX_set_srp_strength(SSL_CTX
*ctx
, int strength
)
439 return tls1_ctx_ctrl(ctx
, SSL_CTRL_SET_TLS_EXT_SRP_STRENGTH
, strength
,
443 int SSL_CTX_set_srp_verify_param_callback(SSL_CTX
*ctx
,
444 int (*cb
) (SSL
*, void *))
446 return tls1_ctx_callback_ctrl(ctx
, SSL_CTRL_SET_SRP_VERIFY_PARAM_CB
,
450 int SSL_CTX_set_srp_cb_arg(SSL_CTX
*ctx
, void *arg
)
452 return tls1_ctx_ctrl(ctx
, SSL_CTRL_SET_SRP_ARG
, 0, arg
);
455 int SSL_CTX_set_srp_username_callback(SSL_CTX
*ctx
,
456 int (*cb
) (SSL
*, int *, void *))
458 return tls1_ctx_callback_ctrl(ctx
, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME_CB
,
462 int SSL_CTX_set_srp_client_pwd_callback(SSL_CTX
*ctx
,
463 char *(*cb
) (SSL
*, void *))
465 return tls1_ctx_callback_ctrl(ctx
, SSL_CTRL_SET_SRP_GIVE_CLIENT_PWD_CB
,