source: EcnlProtoTool/trunk/openssl-1.1.0e/ssl/tls_srp.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 13.1 KB
Line 
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
18int 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 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;
47 return (1);
48}
49
50int SSL_SRP_CTX_free(struct ssl_st *s)
51{
52 if (s == NULL)
53 return 0;
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;
67 s->srp_ctx.N = NULL;
68 s->srp_ctx.g = NULL;
69 s->srp_ctx.s = NULL;
70 s->srp_ctx.B = NULL;
71 s->srp_ctx.A = NULL;
72 s->srp_ctx.a = NULL;
73 s->srp_ctx.b = NULL;
74 s->srp_ctx.v = 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;
79 return (1);
80}
81
82int SSL_SRP_CTX_init(struct ssl_st *s)
83{
84 SSL_CTX *ctx;
85
86 if ((s == NULL) || ((ctx = s->ctx) == NULL))
87 return 0;
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;
98
99 s->srp_ctx.N = NULL;
100 s->srp_ctx.g = NULL;
101 s->srp_ctx.s = NULL;
102 s->srp_ctx.B = NULL;
103 s->srp_ctx.A = NULL;
104 s->srp_ctx.a = NULL;
105 s->srp_ctx.b = NULL;
106 s->srp_ctx.v = NULL;
107 s->srp_ctx.login = NULL;
108 s->srp_ctx.info = ctx->srp_ctx.info;
109 s->srp_ctx.strength = ctx->srp_ctx.strength;
110
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);
128 goto err;
129 }
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);
133 goto err;
134 }
135 s->srp_ctx.srp_Mask = ctx->srp_ctx.srp_Mask;
136
137 return (1);
138 err:
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);
148 return (0);
149}
150
151int SSL_CTX_SRP_CTX_init(struct ssl_ctx_st *ctx)
152{
153 if (ctx == NULL)
154 return 0;
155
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;
163
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;
176
177 return (1);
178}
179
180/* server side */
181int SSL_srp_server_param_with_username(SSL *s, int *ad)
182{
183 unsigned char b[SSL_MAX_MASTER_KEY_LENGTH];
184 int al;
185
186 *ad = SSL_AD_UNKNOWN_PSK_IDENTITY;
187 if ((s->srp_ctx.TLS_ext_srp_username_callback != NULL) &&
188 ((al =
189 s->srp_ctx.TLS_ext_srp_username_callback(s, ad,
190 s->srp_ctx.SRP_cb_arg)) !=
191 SSL_ERROR_NONE))
192 return al;
193
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;
199
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));
204
205 /* Calculate: B = (kv + g^b) % N */
206
207 return ((s->srp_ctx.B =
208 SRP_Calc_B(s->srp_ctx.b, s->srp_ctx.N, s->srp_ctx.g,
209 s->srp_ctx.v)) !=
210 NULL) ? SSL_ERROR_NONE : SSL3_AL_FATAL;
211}
212
213/*
214 * If the server just has the raw password, make up a verifier entry on the
215 * fly
216 */
217int SSL_set_srp_server_param_pw(SSL *s, const char *user, const char *pass,
218 const char *grp)
219{
220 SRP_gN *GN = SRP_get_default_gN(grp);
221 if (GN == NULL)
222 return -1;
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);
226 s->srp_ctx.v = NULL;
227 BN_clear_free(s->srp_ctx.s);
228 s->srp_ctx.s = NULL;
229 if (!SRP_create_verifier_BN
230 (user, pass, &s->srp_ctx.s, &s->srp_ctx.v, GN->N, GN->g))
231 return -1;
232
233 return 1;
234}
235
236int SSL_set_srp_server_param(SSL *s, const BIGNUM *N, const BIGNUM *g,
237 BIGNUM *sa, BIGNUM *v, char *info)
238{
239 if (N != NULL) {
240 if (s->srp_ctx.N != NULL) {
241 if (!BN_copy(s->srp_ctx.N, N)) {
242 BN_free(s->srp_ctx.N);
243 s->srp_ctx.N = NULL;
244 }
245 } else
246 s->srp_ctx.N = BN_dup(N);
247 }
248 if (g != NULL) {
249 if (s->srp_ctx.g != NULL) {
250 if (!BN_copy(s->srp_ctx.g, g)) {
251 BN_free(s->srp_ctx.g);
252 s->srp_ctx.g = NULL;
253 }
254 } else
255 s->srp_ctx.g = BN_dup(g);
256 }
257 if (sa != NULL) {
258 if (s->srp_ctx.s != NULL) {
259 if (!BN_copy(s->srp_ctx.s, sa)) {
260 BN_free(s->srp_ctx.s);
261 s->srp_ctx.s = NULL;
262 }
263 } else
264 s->srp_ctx.s = BN_dup(sa);
265 }
266 if (v != NULL) {
267 if (s->srp_ctx.v != NULL) {
268 if (!BN_copy(s->srp_ctx.v, v)) {
269 BN_free(s->srp_ctx.v);
270 s->srp_ctx.v = NULL;
271 }
272 } else
273 s->srp_ctx.v = BN_dup(v);
274 }
275 s->srp_ctx.info = info;
276
277 if (!(s->srp_ctx.N) ||
278 !(s->srp_ctx.g) || !(s->srp_ctx.s) || !(s->srp_ctx.v))
279 return -1;
280
281 return 1;
282}
283
284int srp_generate_server_master_secret(SSL *s)
285{
286 BIGNUM *K = NULL, *u = NULL;
287 int ret = -1, tmp_len = 0;
288 unsigned char *tmp = NULL;
289
290 if (!SRP_Verify_A_mod_N(s->srp_ctx.A, s->srp_ctx.N))
291 goto err;
292 if ((u = SRP_Calc_u(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N)) == NULL)
293 goto err;
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)
296 goto err;
297
298 tmp_len = BN_num_bytes(K);
299 if ((tmp = OPENSSL_malloc(tmp_len)) == NULL)
300 goto err;
301 BN_bn2bin(K, tmp);
302 ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);
303 err:
304 BN_clear_free(K);
305 BN_clear_free(u);
306 return ret;
307}
308
309/* client side */
310int srp_generate_client_master_secret(SSL *s)
311{
312 BIGNUM *x = NULL, *u = NULL, *K = NULL;
313 int ret = -1, tmp_len = 0;
314 char *passwd = NULL;
315 unsigned char *tmp = NULL;
316
317 /*
318 * Checks if b % n == 0
319 */
320 if (SRP_Verify_B_mod_N(s->srp_ctx.B, s->srp_ctx.N) == 0)
321 goto err;
322 if ((u = SRP_Calc_u(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N)) == NULL)
323 goto err;
324 if (s->srp_ctx.SRP_give_srp_client_pwd_callback == NULL)
325 goto err;
326 if (!
327 (passwd =
328 s->srp_ctx.SRP_give_srp_client_pwd_callback(s, s->srp_ctx.SRP_cb_arg)))
329 goto err;
330 if ((x = SRP_Calc_x(s->srp_ctx.s, s->srp_ctx.login, passwd)) == NULL)
331 goto err;
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)
334 goto err;
335
336 tmp_len = BN_num_bytes(K);
337 if ((tmp = OPENSSL_malloc(tmp_len)) == NULL)
338 goto err;
339 BN_bn2bin(K, tmp);
340 ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);
341 err:
342 BN_clear_free(K);
343 BN_clear_free(x);
344 if (passwd != NULL)
345 OPENSSL_clear_free(passwd, strlen(passwd));
346 BN_clear_free(u);
347 return ret;
348}
349
350int srp_verify_server_param(SSL *s, int *al)
351{
352 SRP_CTX *srp = &s->srp_ctx;
353 /*
354 * Sanity check parameters: we can quickly check B % N == 0 by checking B
355 * != 0 since B < N
356 */
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;
360 return 0;
361 }
362
363 if (BN_num_bits(srp->N) < srp->strength) {
364 *al = TLS1_AD_INSUFFICIENT_SECURITY;
365 return 0;
366 }
367
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;
371 return 0;
372 }
373 } else if (!SRP_check_known_gN_param(srp->g, srp->N)) {
374 *al = TLS1_AD_INSUFFICIENT_SECURITY;
375 return 0;
376 }
377
378 return 1;
379}
380
381int SRP_Calc_A_param(SSL *s)
382{
383 unsigned char rnd[SSL_MAX_MASTER_KEY_LENGTH];
384
385 if (RAND_bytes(rnd, sizeof(rnd)) <= 0)
386 return 0;
387 s->srp_ctx.a = BN_bin2bn(rnd, sizeof(rnd), s->srp_ctx.a);
388 OPENSSL_cleanse(rnd, sizeof(rnd));
389
390 if (!(s->srp_ctx.A = SRP_Calc_A(s->srp_ctx.a, s->srp_ctx.N, s->srp_ctx.g)))
391 return 0;
392
393 return 1;
394}
395
396BIGNUM *SSL_get_srp_g(SSL *s)
397{
398 if (s->srp_ctx.g != NULL)
399 return s->srp_ctx.g;
400 return s->ctx->srp_ctx.g;
401}
402
403BIGNUM *SSL_get_srp_N(SSL *s)
404{
405 if (s->srp_ctx.N != NULL)
406 return s->srp_ctx.N;
407 return s->ctx->srp_ctx.N;
408}
409
410char *SSL_get_srp_username(SSL *s)
411{
412 if (s->srp_ctx.login != NULL)
413 return s->srp_ctx.login;
414 return s->ctx->srp_ctx.login;
415}
416
417char *SSL_get_srp_userinfo(SSL *s)
418{
419 if (s->srp_ctx.info != NULL)
420 return s->srp_ctx.info;
421 return s->ctx->srp_ctx.info;
422}
423
424# define tls1_ctx_ctrl ssl3_ctx_ctrl
425# define tls1_ctx_callback_ctrl ssl3_ctx_callback_ctrl
426
427int SSL_CTX_set_srp_username(SSL_CTX *ctx, char *name)
428{
429 return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME, 0, name);
430}
431
432int SSL_CTX_set_srp_password(SSL_CTX *ctx, char *password)
433{
434 return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_PASSWORD, 0, password);
435}
436
437int SSL_CTX_set_srp_strength(SSL_CTX *ctx, int strength)
438{
439 return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_STRENGTH, strength,
440 NULL);
441}
442
443int SSL_CTX_set_srp_verify_param_callback(SSL_CTX *ctx,
444 int (*cb) (SSL *, void *))
445{
446 return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_SRP_VERIFY_PARAM_CB,
447 (void (*)(void))cb);
448}
449
450int SSL_CTX_set_srp_cb_arg(SSL_CTX *ctx, void *arg)
451{
452 return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_SRP_ARG, 0, arg);
453}
454
455int SSL_CTX_set_srp_username_callback(SSL_CTX *ctx,
456 int (*cb) (SSL *, int *, void *))
457{
458 return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME_CB,
459 (void (*)(void))cb);
460}
461
462int SSL_CTX_set_srp_client_pwd_callback(SSL_CTX *ctx,
463 char *(*cb) (SSL *, void *))
464{
465 return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_SRP_GIVE_CLIENT_PWD_CB,
466 (void (*)(void))cb);
467}
468
469#endif
Note: See TracBrowser for help on using the repository browser.