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
)
263 ret
= ssl_generate_master_secret(s
, tmp
, tmp_len
, 1);
271 int srp_generate_client_master_secret(SSL
*s
)
273 BIGNUM
*x
= NULL
, *u
= NULL
, *K
= NULL
;
274 int ret
= -1, tmp_len
= 0;
276 unsigned char *tmp
= NULL
;
279 * Checks if b % n == 0
281 if (SRP_Verify_B_mod_N(s
->srp_ctx
.B
, s
->srp_ctx
.N
) == 0)
283 if ((u
= SRP_Calc_u(s
->srp_ctx
.A
, s
->srp_ctx
.B
, s
->srp_ctx
.N
)) == NULL
)
285 if (s
->srp_ctx
.SRP_give_srp_client_pwd_callback
== NULL
)
289 s
->srp_ctx
.SRP_give_srp_client_pwd_callback(s
, s
->srp_ctx
.SRP_cb_arg
)))
291 if ((x
= SRP_Calc_x(s
->srp_ctx
.s
, s
->srp_ctx
.login
, passwd
)) == NULL
)
293 if ((K
= SRP_Calc_client_key(s
->srp_ctx
.N
, s
->srp_ctx
.B
, s
->srp_ctx
.g
, x
,
294 s
->srp_ctx
.a
, u
)) == NULL
)
297 tmp_len
= BN_num_bytes(K
);
298 if ((tmp
= OPENSSL_malloc(tmp_len
)) == NULL
)
301 ret
= ssl_generate_master_secret(s
, tmp
, tmp_len
, 1);
306 OPENSSL_clear_free(passwd
, strlen(passwd
));
311 int srp_verify_server_param(SSL
*s
, int *al
)
313 SRP_CTX
*srp
= &s
->srp_ctx
;
315 * Sanity check parameters: we can quickly check B % N == 0 by checking B
318 if (BN_ucmp(srp
->g
, srp
->N
) >= 0 || BN_ucmp(srp
->B
, srp
->N
) >= 0
319 || BN_is_zero(srp
->B
)) {
320 *al
= SSL3_AD_ILLEGAL_PARAMETER
;
324 if (BN_num_bits(srp
->N
) < srp
->strength
) {
325 *al
= TLS1_AD_INSUFFICIENT_SECURITY
;
329 if (srp
->SRP_verify_param_callback
) {
330 if (srp
->SRP_verify_param_callback(s
, srp
->SRP_cb_arg
) <= 0) {
331 *al
= TLS1_AD_INSUFFICIENT_SECURITY
;
334 } else if (!SRP_check_known_gN_param(srp
->g
, srp
->N
)) {
335 *al
= TLS1_AD_INSUFFICIENT_SECURITY
;
342 int SRP_Calc_A_param(SSL
*s
)
344 unsigned char rnd
[SSL_MAX_MASTER_KEY_LENGTH
];
346 if (ssl_randbytes(s
, rnd
, sizeof(rnd
)) <= 0)
348 s
->srp_ctx
.a
= BN_bin2bn(rnd
, sizeof(rnd
), s
->srp_ctx
.a
);
349 OPENSSL_cleanse(rnd
, sizeof(rnd
));
351 if (!(s
->srp_ctx
.A
= SRP_Calc_A(s
->srp_ctx
.a
, s
->srp_ctx
.N
, s
->srp_ctx
.g
)))
357 BIGNUM
*SSL_get_srp_g(SSL
*s
)
359 if (s
->srp_ctx
.g
!= NULL
)
361 return s
->ctx
->srp_ctx
.g
;
364 BIGNUM
*SSL_get_srp_N(SSL
*s
)
366 if (s
->srp_ctx
.N
!= NULL
)
368 return s
->ctx
->srp_ctx
.N
;
371 char *SSL_get_srp_username(SSL
*s
)
373 if (s
->srp_ctx
.login
!= NULL
)
374 return s
->srp_ctx
.login
;
375 return s
->ctx
->srp_ctx
.login
;
378 char *SSL_get_srp_userinfo(SSL
*s
)
380 if (s
->srp_ctx
.info
!= NULL
)
381 return s
->srp_ctx
.info
;
382 return s
->ctx
->srp_ctx
.info
;
385 # define tls1_ctx_ctrl ssl3_ctx_ctrl
386 # define tls1_ctx_callback_ctrl ssl3_ctx_callback_ctrl
388 int SSL_CTX_set_srp_username(SSL_CTX
*ctx
, char *name
)
390 return tls1_ctx_ctrl(ctx
, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME
, 0, name
);
393 int SSL_CTX_set_srp_password(SSL_CTX
*ctx
, char *password
)
395 return tls1_ctx_ctrl(ctx
, SSL_CTRL_SET_TLS_EXT_SRP_PASSWORD
, 0, password
);
398 int SSL_CTX_set_srp_strength(SSL_CTX
*ctx
, int strength
)
400 return tls1_ctx_ctrl(ctx
, SSL_CTRL_SET_TLS_EXT_SRP_STRENGTH
, strength
,
404 int SSL_CTX_set_srp_verify_param_callback(SSL_CTX
*ctx
,
405 int (*cb
) (SSL
*, void *))
407 return tls1_ctx_callback_ctrl(ctx
, SSL_CTRL_SET_SRP_VERIFY_PARAM_CB
,
411 int SSL_CTX_set_srp_cb_arg(SSL_CTX
*ctx
, void *arg
)
413 return tls1_ctx_ctrl(ctx
, SSL_CTRL_SET_SRP_ARG
, 0, arg
);
416 int SSL_CTX_set_srp_username_callback(SSL_CTX
*ctx
,
417 int (*cb
) (SSL
*, int *, void *))
419 return tls1_ctx_callback_ctrl(ctx
, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME_CB
,
423 int SSL_CTX_set_srp_client_pwd_callback(SSL_CTX
*ctx
,
424 char *(*cb
) (SSL
*, void *))
426 return tls1_ctx_callback_ctrl(ctx
, SSL_CTRL_SET_SRP_GIVE_CLIENT_PWD_CB
,