source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/conf/conf_def.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: 15.9 KB
Line 
1/*
2 * Copyright 1995-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/* Part of the code in here was originally in conf.c, which is now removed */
11
12#include <stdio.h>
13#include <string.h>
14#include "internal/cryptlib.h"
15#include <openssl/stack.h>
16#include <openssl/lhash.h>
17#include <openssl/conf.h>
18#include <openssl/conf_api.h>
19#include "conf_def.h"
20#include <openssl/buffer.h>
21#include <openssl/err.h>
22
23static char *eat_ws(CONF *conf, char *p);
24static char *eat_alpha_numeric(CONF *conf, char *p);
25static void clear_comments(CONF *conf, char *p);
26static int str_copy(CONF *conf, char *section, char **to, char *from);
27static char *scan_quote(CONF *conf, char *p);
28static char *scan_dquote(CONF *conf, char *p);
29#define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
30
31static CONF *def_create(CONF_METHOD *meth);
32static int def_init_default(CONF *conf);
33static int def_init_WIN32(CONF *conf);
34static int def_destroy(CONF *conf);
35static int def_destroy_data(CONF *conf);
36static int def_load(CONF *conf, const char *name, long *eline);
37static int def_load_bio(CONF *conf, BIO *bp, long *eline);
38static int def_dump(const CONF *conf, BIO *bp);
39static int def_is_number(const CONF *conf, char c);
40static int def_to_int(const CONF *conf, char c);
41
42static CONF_METHOD default_method = {
43 "OpenSSL default",
44 def_create,
45 def_init_default,
46 def_destroy,
47 def_destroy_data,
48 def_load_bio,
49 def_dump,
50 def_is_number,
51 def_to_int,
52 def_load
53};
54
55static CONF_METHOD WIN32_method = {
56 "WIN32",
57 def_create,
58 def_init_WIN32,
59 def_destroy,
60 def_destroy_data,
61 def_load_bio,
62 def_dump,
63 def_is_number,
64 def_to_int,
65 def_load
66};
67
68CONF_METHOD *NCONF_default()
69{
70 return &default_method;
71}
72
73CONF_METHOD *NCONF_WIN32()
74{
75 return &WIN32_method;
76}
77
78static CONF *def_create(CONF_METHOD *meth)
79{
80 CONF *ret;
81
82 ret = OPENSSL_malloc(sizeof(*ret));
83 if (ret != NULL)
84 if (meth->init(ret) == 0) {
85 OPENSSL_free(ret);
86 ret = NULL;
87 }
88 return ret;
89}
90
91static int def_init_default(CONF *conf)
92{
93 if (conf == NULL)
94 return 0;
95
96 conf->meth = &default_method;
97 conf->meth_data = (void *)CONF_type_default;
98 conf->data = NULL;
99
100 return 1;
101}
102
103static int def_init_WIN32(CONF *conf)
104{
105 if (conf == NULL)
106 return 0;
107
108 conf->meth = &WIN32_method;
109 conf->meth_data = (void *)CONF_type_win32;
110 conf->data = NULL;
111
112 return 1;
113}
114
115static int def_destroy(CONF *conf)
116{
117 if (def_destroy_data(conf)) {
118 OPENSSL_free(conf);
119 return 1;
120 }
121 return 0;
122}
123
124static int def_destroy_data(CONF *conf)
125{
126 if (conf == NULL)
127 return 0;
128 _CONF_free_data(conf);
129 return 1;
130}
131
132static int def_load(CONF *conf, const char *name, long *line)
133{
134 int ret;
135 BIO *in = NULL;
136
137#ifdef OPENSSL_SYS_VMS
138 in = BIO_new_file(name, "r");
139#else
140 in = BIO_new_file(name, "rb");
141#endif
142 if (in == NULL) {
143 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
144 CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
145 else
146 CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
147 return 0;
148 }
149
150 ret = def_load_bio(conf, in, line);
151 BIO_free(in);
152
153 return ret;
154}
155
156static int def_load_bio(CONF *conf, BIO *in, long *line)
157{
158/* The macro BUFSIZE conflicts with a system macro in VxWorks */
159#define CONFBUFSIZE 512
160 int bufnum = 0, i, ii;
161 BUF_MEM *buff = NULL;
162 char *s, *p, *end;
163 int again;
164 long eline = 0;
165 char btmp[DECIMAL_SIZE(eline) + 1];
166 CONF_VALUE *v = NULL, *tv;
167 CONF_VALUE *sv = NULL;
168 char *section = NULL, *buf;
169 char *start, *psection, *pname;
170 void *h = (void *)(conf->data);
171
172 if ((buff = BUF_MEM_new()) == NULL) {
173 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
174 goto err;
175 }
176
177 section = OPENSSL_strdup("default");
178 if (section == NULL) {
179 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
180 goto err;
181 }
182
183 if (_CONF_new_data(conf) == 0) {
184 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
185 goto err;
186 }
187
188 sv = _CONF_new_section(conf, section);
189 if (sv == NULL) {
190 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
191 goto err;
192 }
193
194 bufnum = 0;
195 again = 0;
196 for (;;) {
197 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
198 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
199 goto err;
200 }
201 p = &(buff->data[bufnum]);
202 *p = '\0';
203 BIO_gets(in, p, CONFBUFSIZE - 1);
204 p[CONFBUFSIZE - 1] = '\0';
205 ii = i = strlen(p);
206 if (i == 0 && !again)
207 break;
208 again = 0;
209 while (i > 0) {
210 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
211 break;
212 else
213 i--;
214 }
215 /*
216 * we removed some trailing stuff so there is a new line on the end.
217 */
218 if (ii && i == ii)
219 again = 1; /* long line */
220 else {
221 p[i] = '\0';
222 eline++; /* another input line */
223 }
224
225 /* we now have a line with trailing \r\n removed */
226
227 /* i is the number of bytes */
228 bufnum += i;
229
230 v = NULL;
231 /* check for line continuation */
232 if (bufnum >= 1) {
233 /*
234 * If we have bytes and the last char '\\' and second last char
235 * is not '\\'
236 */
237 p = &(buff->data[bufnum - 1]);
238 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
239 bufnum--;
240 again = 1;
241 }
242 }
243 if (again)
244 continue;
245 bufnum = 0;
246 buf = buff->data;
247
248 clear_comments(conf, buf);
249 s = eat_ws(conf, buf);
250 if (IS_EOF(conf, *s))
251 continue; /* blank line */
252 if (*s == '[') {
253 char *ss;
254
255 s++;
256 start = eat_ws(conf, s);
257 ss = start;
258 again:
259 end = eat_alpha_numeric(conf, ss);
260 p = eat_ws(conf, end);
261 if (*p != ']') {
262 if (*p != '\0' && ss != p) {
263 ss = p;
264 goto again;
265 }
266 CONFerr(CONF_F_DEF_LOAD_BIO,
267 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
268 goto err;
269 }
270 *end = '\0';
271 if (!str_copy(conf, NULL, &section, start))
272 goto err;
273 if ((sv = _CONF_get_section(conf, section)) == NULL)
274 sv = _CONF_new_section(conf, section);
275 if (sv == NULL) {
276 CONFerr(CONF_F_DEF_LOAD_BIO,
277 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
278 goto err;
279 }
280 continue;
281 } else {
282 pname = s;
283 psection = NULL;
284 end = eat_alpha_numeric(conf, s);
285 if ((end[0] == ':') && (end[1] == ':')) {
286 *end = '\0';
287 end += 2;
288 psection = pname;
289 pname = end;
290 end = eat_alpha_numeric(conf, end);
291 }
292 p = eat_ws(conf, end);
293 if (*p != '=') {
294 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
295 goto err;
296 }
297 *end = '\0';
298 p++;
299 start = eat_ws(conf, p);
300 while (!IS_EOF(conf, *p))
301 p++;
302 p--;
303 while ((p != start) && (IS_WS(conf, *p)))
304 p--;
305 p++;
306 *p = '\0';
307
308 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
309 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
310 goto err;
311 }
312 if (psection == NULL)
313 psection = section;
314 v->name = OPENSSL_malloc(strlen(pname) + 1);
315 v->value = NULL;
316 if (v->name == NULL) {
317 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
318 goto err;
319 }
320 OPENSSL_strlcpy(v->name, pname, strlen(pname) + 1);
321 if (!str_copy(conf, psection, &(v->value), start))
322 goto err;
323
324 if (strcmp(psection, section) != 0) {
325 if ((tv = _CONF_get_section(conf, psection))
326 == NULL)
327 tv = _CONF_new_section(conf, psection);
328 if (tv == NULL) {
329 CONFerr(CONF_F_DEF_LOAD_BIO,
330 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
331 goto err;
332 }
333 } else
334 tv = sv;
335 if (_CONF_add_string(conf, tv, v) == 0) {
336 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
337 goto err;
338 }
339 v = NULL;
340 }
341 }
342 BUF_MEM_free(buff);
343 OPENSSL_free(section);
344 return (1);
345 err:
346 BUF_MEM_free(buff);
347 OPENSSL_free(section);
348 if (line != NULL)
349 *line = eline;
350 BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
351 ERR_add_error_data(2, "line ", btmp);
352 if (h != conf->data) {
353 CONF_free(conf->data);
354 conf->data = NULL;
355 }
356 if (v != NULL) {
357 OPENSSL_free(v->name);
358 OPENSSL_free(v->value);
359 OPENSSL_free(v);
360 }
361 return (0);
362}
363
364static void clear_comments(CONF *conf, char *p)
365{
366 for (;;) {
367 if (IS_FCOMMENT(conf, *p)) {
368 *p = '\0';
369 return;
370 }
371 if (!IS_WS(conf, *p)) {
372 break;
373 }
374 p++;
375 }
376
377 for (;;) {
378 if (IS_COMMENT(conf, *p)) {
379 *p = '\0';
380 return;
381 }
382 if (IS_DQUOTE(conf, *p)) {
383 p = scan_dquote(conf, p);
384 continue;
385 }
386 if (IS_QUOTE(conf, *p)) {
387 p = scan_quote(conf, p);
388 continue;
389 }
390 if (IS_ESC(conf, *p)) {
391 p = scan_esc(conf, p);
392 continue;
393 }
394 if (IS_EOF(conf, *p))
395 return;
396 else
397 p++;
398 }
399}
400
401static int str_copy(CONF *conf, char *section, char **pto, char *from)
402{
403 int q, r, rr = 0, to = 0, len = 0;
404 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
405 BUF_MEM *buf;
406
407 if ((buf = BUF_MEM_new()) == NULL)
408 return (0);
409
410 len = strlen(from) + 1;
411 if (!BUF_MEM_grow(buf, len))
412 goto err;
413
414 for (;;) {
415 if (IS_QUOTE(conf, *from)) {
416 q = *from;
417 from++;
418 while (!IS_EOF(conf, *from) && (*from != q)) {
419 if (IS_ESC(conf, *from)) {
420 from++;
421 if (IS_EOF(conf, *from))
422 break;
423 }
424 buf->data[to++] = *(from++);
425 }
426 if (*from == q)
427 from++;
428 } else if (IS_DQUOTE(conf, *from)) {
429 q = *from;
430 from++;
431 while (!IS_EOF(conf, *from)) {
432 if (*from == q) {
433 if (*(from + 1) == q) {
434 from++;
435 } else {
436 break;
437 }
438 }
439 buf->data[to++] = *(from++);
440 }
441 if (*from == q)
442 from++;
443 } else if (IS_ESC(conf, *from)) {
444 from++;
445 v = *(from++);
446 if (IS_EOF(conf, v))
447 break;
448 else if (v == 'r')
449 v = '\r';
450 else if (v == 'n')
451 v = '\n';
452 else if (v == 'b')
453 v = '\b';
454 else if (v == 't')
455 v = '\t';
456 buf->data[to++] = v;
457 } else if (IS_EOF(conf, *from))
458 break;
459 else if (*from == '$') {
460 /* try to expand it */
461 rrp = NULL;
462 s = &(from[1]);
463 if (*s == '{')
464 q = '}';
465 else if (*s == '(')
466 q = ')';
467 else
468 q = 0;
469
470 if (q)
471 s++;
472 cp = section;
473 e = np = s;
474 while (IS_ALPHA_NUMERIC(conf, *e))
475 e++;
476 if ((e[0] == ':') && (e[1] == ':')) {
477 cp = np;
478 rrp = e;
479 rr = *e;
480 *rrp = '\0';
481 e += 2;
482 np = e;
483 while (IS_ALPHA_NUMERIC(conf, *e))
484 e++;
485 }
486 r = *e;
487 *e = '\0';
488 rp = e;
489 if (q) {
490 if (r != q) {
491 CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
492 goto err;
493 }
494 e++;
495 }
496 /*-
497 * So at this point we have
498 * np which is the start of the name string which is
499 * '\0' terminated.
500 * cp which is the start of the section string which is
501 * '\0' terminated.
502 * e is the 'next point after'.
503 * r and rr are the chars replaced by the '\0'
504 * rp and rrp is where 'r' and 'rr' came from.
505 */
506 p = _CONF_get_string(conf, cp, np);
507 if (rrp != NULL)
508 *rrp = rr;
509 *rp = r;
510 if (p == NULL) {
511 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
512 goto err;
513 }
514 if (!BUF_MEM_grow_clean(buf,
515 (strlen(p) + buf->length - (e - from)))) {
516 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
517 goto err;
518 }
519 while (*p)
520 buf->data[to++] = *(p++);
521
522 /*
523 * Since we change the pointer 'from', we also have to change the
524 * perceived length of the string it points at. /RL
525 */
526 len -= e - from;
527 from = e;
528
529 /*
530 * In case there were no braces or parenthesis around the
531 * variable reference, we have to put back the character that was
532 * replaced with a '\0'. /RL
533 */
534 *rp = r;
535 } else
536 buf->data[to++] = *(from++);
537 }
538 buf->data[to] = '\0';
539 OPENSSL_free(*pto);
540 *pto = buf->data;
541 OPENSSL_free(buf);
542 return (1);
543 err:
544 BUF_MEM_free(buf);
545 return (0);
546}
547
548static char *eat_ws(CONF *conf, char *p)
549{
550 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
551 p++;
552 return (p);
553}
554
555static char *eat_alpha_numeric(CONF *conf, char *p)
556{
557 for (;;) {
558 if (IS_ESC(conf, *p)) {
559 p = scan_esc(conf, p);
560 continue;
561 }
562 if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p))
563 return (p);
564 p++;
565 }
566}
567
568static char *scan_quote(CONF *conf, char *p)
569{
570 int q = *p;
571
572 p++;
573 while (!(IS_EOF(conf, *p)) && (*p != q)) {
574 if (IS_ESC(conf, *p)) {
575 p++;
576 if (IS_EOF(conf, *p))
577 return (p);
578 }
579 p++;
580 }
581 if (*p == q)
582 p++;
583 return (p);
584}
585
586static char *scan_dquote(CONF *conf, char *p)
587{
588 int q = *p;
589
590 p++;
591 while (!(IS_EOF(conf, *p))) {
592 if (*p == q) {
593 if (*(p + 1) == q) {
594 p++;
595 } else {
596 break;
597 }
598 }
599 p++;
600 }
601 if (*p == q)
602 p++;
603 return (p);
604}
605
606static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
607{
608 if (a->name)
609 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
610 else
611 BIO_printf(out, "[[%s]]\n", a->section);
612}
613
614IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
615
616static int def_dump(const CONF *conf, BIO *out)
617{
618 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
619 return 1;
620}
621
622static int def_is_number(const CONF *conf, char c)
623{
624 return IS_NUMBER(conf, c);
625}
626
627static int def_to_int(const CONF *conf, char c)
628{
629 return c - '0';
630}
Note: See TracBrowser for help on using the repository browser.