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