Deprecate the low level SRP APIs
[openssl.git] / ssl / tls_srp.c
1 /*
2  * Copyright 2004-2020 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
4  *
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
9  *
10  * Originally written by Christophe Renou and Peter Sylvester,
11  * for the EdelKey project.
12  */
13
14 /* We need to use the SRP deprecated APIs */
15 #define OPENSSL_SUPPRESS_DEPRECATED
16
17 #include <openssl/crypto.h>
18 #include <openssl/rand.h>
19 #include <openssl/err.h>
20 #include "ssl_local.h"
21
22 #ifndef OPENSSL_NO_SRP
23 # include <openssl/srp.h>
24
25 int SSL_CTX_SRP_CTX_free(struct ssl_ctx_st *ctx)
26 {
27     if (ctx == NULL)
28         return 0;
29     OPENSSL_free(ctx->srp_ctx.login);
30     OPENSSL_free(ctx->srp_ctx.info);
31     BN_free(ctx->srp_ctx.N);
32     BN_free(ctx->srp_ctx.g);
33     BN_free(ctx->srp_ctx.s);
34     BN_free(ctx->srp_ctx.B);
35     BN_free(ctx->srp_ctx.A);
36     BN_free(ctx->srp_ctx.a);
37     BN_free(ctx->srp_ctx.b);
38     BN_free(ctx->srp_ctx.v);
39     memset(&ctx->srp_ctx, 0, sizeof(ctx->srp_ctx));
40     ctx->srp_ctx.strength = SRP_MINIMAL_N;
41     return 1;
42 }
43
44 int SSL_SRP_CTX_free(struct ssl_st *s)
45 {
46     if (s == NULL)
47         return 0;
48     OPENSSL_free(s->srp_ctx.login);
49     OPENSSL_free(s->srp_ctx.info);
50     BN_free(s->srp_ctx.N);
51     BN_free(s->srp_ctx.g);
52     BN_free(s->srp_ctx.s);
53     BN_free(s->srp_ctx.B);
54     BN_free(s->srp_ctx.A);
55     BN_free(s->srp_ctx.a);
56     BN_free(s->srp_ctx.b);
57     BN_free(s->srp_ctx.v);
58     memset(&s->srp_ctx, 0, sizeof(s->srp_ctx));
59     s->srp_ctx.strength = SRP_MINIMAL_N;
60     return 1;
61 }
62
63 int SSL_SRP_CTX_init(struct ssl_st *s)
64 {
65     SSL_CTX *ctx;
66
67     if ((s == NULL) || ((ctx = s->ctx) == NULL))
68         return 0;
69
70     memset(&s->srp_ctx, 0, sizeof(s->srp_ctx));
71
72     s->srp_ctx.SRP_cb_arg = ctx->srp_ctx.SRP_cb_arg;
73     /* set client Hello login callback */
74     s->srp_ctx.TLS_ext_srp_username_callback =
75         ctx->srp_ctx.TLS_ext_srp_username_callback;
76     /* set SRP N/g param callback for verification */
77     s->srp_ctx.SRP_verify_param_callback =
78         ctx->srp_ctx.SRP_verify_param_callback;
79     /* set SRP client passwd callback */
80     s->srp_ctx.SRP_give_srp_client_pwd_callback =
81         ctx->srp_ctx.SRP_give_srp_client_pwd_callback;
82
83     s->srp_ctx.strength = ctx->srp_ctx.strength;
84
85     if (((ctx->srp_ctx.N != NULL) &&
86          ((s->srp_ctx.N = BN_dup(ctx->srp_ctx.N)) == NULL)) ||
87         ((ctx->srp_ctx.g != NULL) &&
88          ((s->srp_ctx.g = BN_dup(ctx->srp_ctx.g)) == NULL)) ||
89         ((ctx->srp_ctx.s != NULL) &&
90          ((s->srp_ctx.s = BN_dup(ctx->srp_ctx.s)) == NULL)) ||
91         ((ctx->srp_ctx.B != NULL) &&
92          ((s->srp_ctx.B = BN_dup(ctx->srp_ctx.B)) == NULL)) ||
93         ((ctx->srp_ctx.A != NULL) &&
94          ((s->srp_ctx.A = BN_dup(ctx->srp_ctx.A)) == NULL)) ||
95         ((ctx->srp_ctx.a != NULL) &&
96          ((s->srp_ctx.a = BN_dup(ctx->srp_ctx.a)) == NULL)) ||
97         ((ctx->srp_ctx.v != NULL) &&
98          ((s->srp_ctx.v = BN_dup(ctx->srp_ctx.v)) == NULL)) ||
99         ((ctx->srp_ctx.b != NULL) &&
100          ((s->srp_ctx.b = BN_dup(ctx->srp_ctx.b)) == NULL))) {
101         ERR_raise(ERR_LIB_SSL, ERR_R_BN_LIB);
102         goto err;
103     }
104     if ((ctx->srp_ctx.login != NULL) &&
105         ((s->srp_ctx.login = OPENSSL_strdup(ctx->srp_ctx.login)) == NULL)) {
106         ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
107         goto err;
108     }
109     if ((ctx->srp_ctx.info != NULL) &&
110         ((s->srp_ctx.info = OPENSSL_strdup(ctx->srp_ctx.info)) == NULL)) {
111         ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
112         goto err;
113     }
114     s->srp_ctx.srp_Mask = ctx->srp_ctx.srp_Mask;
115
116     return 1;
117  err:
118     OPENSSL_free(s->srp_ctx.login);
119     OPENSSL_free(s->srp_ctx.info);
120     BN_free(s->srp_ctx.N);
121     BN_free(s->srp_ctx.g);
122     BN_free(s->srp_ctx.s);
123     BN_free(s->srp_ctx.B);
124     BN_free(s->srp_ctx.A);
125     BN_free(s->srp_ctx.a);
126     BN_free(s->srp_ctx.b);
127     BN_free(s->srp_ctx.v);
128     memset(&s->srp_ctx, 0, sizeof(s->srp_ctx));
129     return 0;
130 }
131
132 int SSL_CTX_SRP_CTX_init(struct ssl_ctx_st *ctx)
133 {
134     if (ctx == NULL)
135         return 0;
136
137     memset(&ctx->srp_ctx, 0, sizeof(ctx->srp_ctx));
138     ctx->srp_ctx.strength = SRP_MINIMAL_N;
139
140     return 1;
141 }
142
143 /* server side */
144 int SSL_srp_server_param_with_username(SSL *s, int *ad)
145 {
146     unsigned char b[SSL_MAX_MASTER_KEY_LENGTH];
147     int al;
148
149     *ad = SSL_AD_UNKNOWN_PSK_IDENTITY;
150     if ((s->srp_ctx.TLS_ext_srp_username_callback != NULL) &&
151         ((al =
152           s->srp_ctx.TLS_ext_srp_username_callback(s, ad,
153                                                    s->srp_ctx.SRP_cb_arg)) !=
154          SSL_ERROR_NONE))
155         return al;
156
157     *ad = SSL_AD_INTERNAL_ERROR;
158     if ((s->srp_ctx.N == NULL) ||
159         (s->srp_ctx.g == NULL) ||
160         (s->srp_ctx.s == NULL) || (s->srp_ctx.v == NULL))
161         return SSL3_AL_FATAL;
162
163     if (RAND_priv_bytes_ex(s->ctx->libctx, b, sizeof(b)) <= 0)
164         return SSL3_AL_FATAL;
165     s->srp_ctx.b = BN_bin2bn(b, sizeof(b), NULL);
166     OPENSSL_cleanse(b, sizeof(b));
167
168     /* Calculate:  B = (kv + g^b) % N  */
169
170     return ((s->srp_ctx.B =
171              SRP_Calc_B_ex(s->srp_ctx.b, s->srp_ctx.N, s->srp_ctx.g,
172                            s->srp_ctx.v, s->ctx->libctx, s->ctx->propq)) !=
173             NULL) ? SSL_ERROR_NONE : SSL3_AL_FATAL;
174 }
175
176 /*
177  * If the server just has the raw password, make up a verifier entry on the
178  * fly
179  */
180 int SSL_set_srp_server_param_pw(SSL *s, const char *user, const char *pass,
181                                 const char *grp)
182 {
183     SRP_gN *GN = SRP_get_default_gN(grp);
184     if (GN == NULL)
185         return -1;
186     s->srp_ctx.N = BN_dup(GN->N);
187     s->srp_ctx.g = BN_dup(GN->g);
188     BN_clear_free(s->srp_ctx.v);
189     s->srp_ctx.v = NULL;
190     BN_clear_free(s->srp_ctx.s);
191     s->srp_ctx.s = NULL;
192     if (!SRP_create_verifier_BN_ex(user, pass, &s->srp_ctx.s, &s->srp_ctx.v,
193                                    GN->N, GN->g, s->ctx->libctx,
194                                    s->ctx->propq))
195         return -1;
196
197     return 1;
198 }
199
200 int SSL_set_srp_server_param(SSL *s, const BIGNUM *N, const BIGNUM *g,
201                              BIGNUM *sa, BIGNUM *v, char *info)
202 {
203     if (N != NULL) {
204         if (s->srp_ctx.N != NULL) {
205             if (!BN_copy(s->srp_ctx.N, N)) {
206                 BN_free(s->srp_ctx.N);
207                 s->srp_ctx.N = NULL;
208             }
209         } else
210             s->srp_ctx.N = BN_dup(N);
211     }
212     if (g != NULL) {
213         if (s->srp_ctx.g != NULL) {
214             if (!BN_copy(s->srp_ctx.g, g)) {
215                 BN_free(s->srp_ctx.g);
216                 s->srp_ctx.g = NULL;
217             }
218         } else
219             s->srp_ctx.g = BN_dup(g);
220     }
221     if (sa != NULL) {
222         if (s->srp_ctx.s != NULL) {
223             if (!BN_copy(s->srp_ctx.s, sa)) {
224                 BN_free(s->srp_ctx.s);
225                 s->srp_ctx.s = NULL;
226             }
227         } else
228             s->srp_ctx.s = BN_dup(sa);
229     }
230     if (v != NULL) {
231         if (s->srp_ctx.v != NULL) {
232             if (!BN_copy(s->srp_ctx.v, v)) {
233                 BN_free(s->srp_ctx.v);
234                 s->srp_ctx.v = NULL;
235             }
236         } else
237             s->srp_ctx.v = BN_dup(v);
238     }
239     if (info != NULL) {
240         if (s->srp_ctx.info)
241             OPENSSL_free(s->srp_ctx.info);
242         if ((s->srp_ctx.info = OPENSSL_strdup(info)) == NULL)
243             return -1;
244     }
245
246     if (!(s->srp_ctx.N) ||
247         !(s->srp_ctx.g) || !(s->srp_ctx.s) || !(s->srp_ctx.v))
248         return -1;
249
250     return 1;
251 }
252
253 int srp_generate_server_master_secret(SSL *s)
254 {
255     BIGNUM *K = NULL, *u = NULL;
256     int ret = -1, tmp_len = 0;
257     unsigned char *tmp = NULL;
258
259     if (!SRP_Verify_A_mod_N(s->srp_ctx.A, s->srp_ctx.N))
260         goto err;
261     if ((u = SRP_Calc_u_ex(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N,
262                            s->ctx->libctx, s->ctx->propq)) == NULL)
263         goto err;
264     if ((K = SRP_Calc_server_key(s->srp_ctx.A, s->srp_ctx.v, u, s->srp_ctx.b,
265                                  s->srp_ctx.N)) == NULL)
266         goto err;
267
268     tmp_len = BN_num_bytes(K);
269     if ((tmp = OPENSSL_malloc(tmp_len)) == NULL) {
270         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
271         goto err;
272     }
273     BN_bn2bin(K, tmp);
274     /* Calls SSLfatal() as required */
275     ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);
276  err:
277     BN_clear_free(K);
278     BN_clear_free(u);
279     return ret;
280 }
281
282 /* client side */
283 int srp_generate_client_master_secret(SSL *s)
284 {
285     BIGNUM *x = NULL, *u = NULL, *K = NULL;
286     int ret = -1, tmp_len = 0;
287     char *passwd = NULL;
288     unsigned char *tmp = NULL;
289
290     /*
291      * Checks if b % n == 0
292      */
293     if (SRP_Verify_B_mod_N(s->srp_ctx.B, s->srp_ctx.N) == 0
294             || (u = SRP_Calc_u_ex(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N,
295                                   s->ctx->libctx, s->ctx->propq))
296                == NULL
297             || s->srp_ctx.SRP_give_srp_client_pwd_callback == NULL) {
298         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
299         goto err;
300     }
301     if ((passwd = s->srp_ctx.SRP_give_srp_client_pwd_callback(s,
302                                                       s->srp_ctx.SRP_cb_arg))
303             == NULL) {
304         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
305         goto err;
306     }
307     if ((x = SRP_Calc_x_ex(s->srp_ctx.s, s->srp_ctx.login, passwd,
308                            s->ctx->libctx, s->ctx->propq)) == NULL
309             || (K = SRP_Calc_client_key_ex(s->srp_ctx.N, s->srp_ctx.B,
310                                            s->srp_ctx.g, x,
311                                            s->srp_ctx.a, u,
312                                            s->ctx->libctx,
313                                            s->ctx->propq)) == NULL) {
314         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
315         goto err;
316     }
317
318     tmp_len = BN_num_bytes(K);
319     if ((tmp = OPENSSL_malloc(tmp_len)) == NULL) {
320         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
321         goto err;
322     }
323     BN_bn2bin(K, tmp);
324     /* Calls SSLfatal() as required */
325     ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);
326  err:
327     BN_clear_free(K);
328     BN_clear_free(x);
329     if (passwd != NULL)
330         OPENSSL_clear_free(passwd, strlen(passwd));
331     BN_clear_free(u);
332     return ret;
333 }
334
335 int srp_verify_server_param(SSL *s)
336 {
337     SRP_CTX *srp = &s->srp_ctx;
338     /*
339      * Sanity check parameters: we can quickly check B % N == 0 by checking B
340      * != 0 since B < N
341      */
342     if (BN_ucmp(srp->g, srp->N) >= 0 || BN_ucmp(srp->B, srp->N) >= 0
343         || BN_is_zero(srp->B)) {
344         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_DATA);
345         return 0;
346     }
347
348     if (BN_num_bits(srp->N) < srp->strength) {
349         SSLfatal(s, SSL_AD_INSUFFICIENT_SECURITY, SSL_R_INSUFFICIENT_SECURITY);
350         return 0;
351     }
352
353     if (srp->SRP_verify_param_callback) {
354         if (srp->SRP_verify_param_callback(s, srp->SRP_cb_arg) <= 0) {
355             SSLfatal(s, SSL_AD_INSUFFICIENT_SECURITY, SSL_R_CALLBACK_FAILED);
356             return 0;
357         }
358     } else if (!SRP_check_known_gN_param(srp->g, srp->N)) {
359         SSLfatal(s, SSL_AD_INSUFFICIENT_SECURITY,
360                  SSL_R_INSUFFICIENT_SECURITY);
361         return 0;
362     }
363
364     return 1;
365 }
366
367 int SRP_Calc_A_param(SSL *s)
368 {
369     unsigned char rnd[SSL_MAX_MASTER_KEY_LENGTH];
370
371     if (RAND_priv_bytes_ex(s->ctx->libctx, rnd, sizeof(rnd)) <= 0)
372         return 0;
373     s->srp_ctx.a = BN_bin2bn(rnd, sizeof(rnd), s->srp_ctx.a);
374     OPENSSL_cleanse(rnd, sizeof(rnd));
375
376     if (!(s->srp_ctx.A = SRP_Calc_A(s->srp_ctx.a, s->srp_ctx.N, s->srp_ctx.g)))
377         return 0;
378
379     return 1;
380 }
381
382 BIGNUM *SSL_get_srp_g(SSL *s)
383 {
384     if (s->srp_ctx.g != NULL)
385         return s->srp_ctx.g;
386     return s->ctx->srp_ctx.g;
387 }
388
389 BIGNUM *SSL_get_srp_N(SSL *s)
390 {
391     if (s->srp_ctx.N != NULL)
392         return s->srp_ctx.N;
393     return s->ctx->srp_ctx.N;
394 }
395
396 char *SSL_get_srp_username(SSL *s)
397 {
398     if (s->srp_ctx.login != NULL)
399         return s->srp_ctx.login;
400     return s->ctx->srp_ctx.login;
401 }
402
403 char *SSL_get_srp_userinfo(SSL *s)
404 {
405     if (s->srp_ctx.info != NULL)
406         return s->srp_ctx.info;
407     return s->ctx->srp_ctx.info;
408 }
409
410 # define tls1_ctx_ctrl ssl3_ctx_ctrl
411 # define tls1_ctx_callback_ctrl ssl3_ctx_callback_ctrl
412
413 int SSL_CTX_set_srp_username(SSL_CTX *ctx, char *name)
414 {
415     return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME, 0, name);
416 }
417
418 int SSL_CTX_set_srp_password(SSL_CTX *ctx, char *password)
419 {
420     return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_PASSWORD, 0, password);
421 }
422
423 int SSL_CTX_set_srp_strength(SSL_CTX *ctx, int strength)
424 {
425     return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_STRENGTH, strength,
426                          NULL);
427 }
428
429 int SSL_CTX_set_srp_verify_param_callback(SSL_CTX *ctx,
430                                           int (*cb) (SSL *, void *))
431 {
432     return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_SRP_VERIFY_PARAM_CB,
433                                   (void (*)(void))cb);
434 }
435
436 int SSL_CTX_set_srp_cb_arg(SSL_CTX *ctx, void *arg)
437 {
438     return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_SRP_ARG, 0, arg);
439 }
440
441 int SSL_CTX_set_srp_username_callback(SSL_CTX *ctx,
442                                       int (*cb) (SSL *, int *, void *))
443 {
444     return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME_CB,
445                                   (void (*)(void))cb);
446 }
447
448 int SSL_CTX_set_srp_client_pwd_callback(SSL_CTX *ctx,
449                                         char *(*cb) (SSL *, void *))
450 {
451     return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_SRP_GIVE_CLIENT_PWD_CB,
452                                   (void (*)(void))cb);
453 }
454
455 #endif