2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
44 host_bind(const char *host
, const char *port
, int verbose
)
46 struct addrinfo hints
, *si
, *p
;
50 memset(&hints
, 0, sizeof hints
);
51 hints
.ai_family
= PF_UNSPEC
;
52 hints
.ai_socktype
= SOCK_STREAM
;
53 err
= getaddrinfo(host
, port
, &hints
, &si
);
55 fprintf(stderr
, "ERROR: getaddrinfo(): %s\n",
60 for (p
= si
; p
!= NULL
; p
= p
->ai_next
) {
62 struct sockaddr_in sa4
;
63 struct sockaddr_in6 sa6
;
68 sa
= (struct sockaddr
*)p
->ai_addr
;
69 if (sa
->sa_family
== AF_INET
) {
70 sa4
= *(struct sockaddr_in
*)sa
;
71 sa
= (struct sockaddr
*)&sa4
;
75 sa4
.sin_addr
.s_addr
= INADDR_ANY
;
77 } else if (sa
->sa_family
== AF_INET6
) {
78 sa6
= *(struct sockaddr_in6
*)sa
;
79 sa
= (struct sockaddr
*)&sa6
;
81 addr
= &sa6
.sin6_addr
;
83 sa6
.sin6_addr
= in6addr_any
;
87 sa_len
= p
->ai_addrlen
;
90 char tmp
[INET6_ADDRSTRLEN
+ 50];
93 if (!inet_ntop(p
->ai_family
, addr
,
96 strcpy(tmp
, "<invalid>");
99 sprintf(tmp
, "<unknown family: %d>",
102 fprintf(stderr
, "binding to: %s\n", tmp
);
104 fd
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
112 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &opt
, sizeof opt
);
114 setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &opt
, sizeof opt
);
115 if (bind(fd
, sa
, sa_len
) < 0) {
126 fprintf(stderr
, "ERROR: failed to bind\n");
130 if (listen(fd
, 5) < 0) {
138 fprintf(stderr
, "bound.\n");
144 accept_client(int server_fd
, int verbose
)
151 fd
= accept(server_fd
, &sa
, &sa_len
);
159 char tmp
[INET6_ADDRSTRLEN
+ 50];
163 switch (sa
.sa_family
) {
165 name
= inet_ntop(AF_INET
,
166 &((struct sockaddr_in
*)&sa
)->sin_addr
,
170 name
= inet_ntop(AF_INET6
,
171 &((struct sockaddr_in6
*)&sa
)->sin6_addr
,
176 sprintf(tmp
, "<unknown: %lu>",
177 (unsigned long)sa
.sa_family
);
180 fprintf(stderr
, "accepting connection from: %s\n", name
);
184 * We make the socket non-blocking, since we are going to use
185 * poll() to organise I/O.
187 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
195 "usage: brssl server [ options ]\n");
199 " -q suppress verbose messages\n");
201 " -trace activate extra debug messages (dump of all packets)\n");
203 " -b name bind to a specific address or host name\n");
205 " -p port bind to a specific port (default: 4433)\n");
207 " -mono use monodirectional buffering\n");
209 " -buf length set the I/O buffer length (in bytes)\n");
211 " -cache length set the session cache storage length (in bytes)\n");
213 " -cert fname read certificate chain from file 'fname'\n");
215 " -key fname read private key from file 'fname'\n");
217 " -CA file add trust anchors from 'file' (for client auth)\n");
219 " -anon_ok request but do not require a client certificate\n");
221 " -list list supported names (protocols, algorithms...)\n");
223 " -vmin name set minimum supported version (default: TLS-1.0)\n");
225 " -vmax name set maximum supported version (default: TLS-1.2)\n");
227 " -cs names set list of supported cipher suites (comma-separated)\n");
229 " -hf names add support for some hash functions (comma-separated)\n");
231 " -cbhash test hashing in policy callback\n");
233 " -serverpref enforce server's preferences for cipher suites\n");
235 " -noreneg prohibit renegotiations\n");
237 " -alpn name add protocol name to list of protocols (ALPN extension)\n");
239 " -strictalpn fail on ALPN mismatch\n");
244 const br_ssl_server_policy_class
*vtable
;
246 br_x509_certificate
*chain
;
248 int cert_signer_algo
;
254 print_hashes(unsigned chashes
)
258 for (i
= 2; i
<= 6; i
++) {
259 if ((chashes
>> i
) & 1) {
263 case 3: z
= 224; break;
264 case 4: z
= 256; break;
265 case 5: z
= 384; break;
266 case 6: z
= 512; break;
271 fprintf(stderr
, " sha%d", z
);
277 choose_hash(unsigned chashes
)
281 for (hash_id
= 6; hash_id
>= 2; hash_id
--) {
282 if (((chashes
>> hash_id
) & 1) != 0) {
287 * Normally unreachable.
293 sp_choose(const br_ssl_server_policy_class
**pctx
,
294 const br_ssl_server_context
*cc
,
295 br_ssl_server_choices
*choices
)
298 const br_suite_translated
*st
;
302 pc
= (policy_context
*)pctx
;
303 st
= br_ssl_server_get_client_suites(cc
, &st_num
);
304 chashes
= br_ssl_server_get_client_hashes(cc
);
306 fprintf(stderr
, "Client parameters:\n");
307 fprintf(stderr
, " Maximum version: ");
308 switch (cc
->client_max_version
) {
310 fprintf(stderr
, "SSL 3.0");
313 fprintf(stderr
, "TLS 1.0");
316 fprintf(stderr
, "TLS 1.1");
319 fprintf(stderr
, "TLS 1.2");
322 fprintf(stderr
, "unknown (0x%04X)",
323 (unsigned)cc
->client_max_version
);
326 fprintf(stderr
, "\n");
327 fprintf(stderr
, " Compatible cipher suites:\n");
328 for (u
= 0; u
< st_num
; u
++) {
331 get_suite_name_ext(st
[u
][0], csn
, sizeof csn
);
332 fprintf(stderr
, " %s\n", csn
);
334 fprintf(stderr
, " Common sign+hash functions:\n");
335 if ((chashes
& 0xFF) != 0) {
336 fprintf(stderr
, " with RSA:");
337 print_hashes(chashes
);
338 fprintf(stderr
, "\n");
340 if ((chashes
>> 8) != 0) {
341 fprintf(stderr
, " with ECDSA:");
342 print_hashes(chashes
>> 8);
343 fprintf(stderr
, "\n");
346 for (u
= 0; u
< st_num
; u
++) {
352 if (pc
->sk
->key_type
== BR_KEYTYPE_RSA
) {
353 choices
->cipher_suite
= st
[u
][0];
357 case BR_SSLKEYX_ECDHE_RSA
:
358 if (pc
->sk
->key_type
== BR_KEYTYPE_RSA
) {
359 choices
->cipher_suite
= st
[u
][0];
360 if (br_ssl_engine_get_version(&cc
->eng
)
364 choices
->algo_id
= 0x0001;
366 choices
->algo_id
= 0xFF00;
371 id
= choose_hash(chashes
);
376 choices
->algo_id
= 0xFF00 + id
;
382 case BR_SSLKEYX_ECDHE_ECDSA
:
383 if (pc
->sk
->key_type
== BR_KEYTYPE_EC
) {
384 choices
->cipher_suite
= st
[u
][0];
385 if (br_ssl_engine_get_version(&cc
->eng
)
389 choices
->algo_id
= 0x0203;
397 id
= choose_hash(chashes
>> 8);
409 case BR_SSLKEYX_ECDH_RSA
:
410 if (pc
->sk
->key_type
== BR_KEYTYPE_EC
411 && pc
->cert_signer_algo
== BR_KEYTYPE_RSA
)
413 choices
->cipher_suite
= st
[u
][0];
417 case BR_SSLKEYX_ECDH_ECDSA
:
418 if (pc
->sk
->key_type
== BR_KEYTYPE_EC
419 && pc
->cert_signer_algo
== BR_KEYTYPE_EC
)
421 choices
->cipher_suite
= st
[u
][0];
430 choices
->chain
= pc
->chain
;
431 choices
->chain_len
= pc
->chain_len
;
435 get_suite_name_ext(choices
->cipher_suite
, csn
, sizeof csn
);
436 fprintf(stderr
, "Using: %s\n", csn
);
442 sp_do_keyx(const br_ssl_server_policy_class
**pctx
,
443 unsigned char *data
, size_t len
)
447 pc
= (policy_context
*)pctx
;
448 switch (pc
->sk
->key_type
) {
450 return br_rsa_ssl_decrypt(
451 &br_rsa_i31_private
, &pc
->sk
->key
.rsa
,
454 return br_ec_prime_i31
.mul(data
, len
, pc
->sk
->key
.ec
.x
,
455 pc
->sk
->key
.ec
.xlen
, pc
->sk
->key
.ec
.curve
);
457 fprintf(stderr
, "ERROR: unknown private key type (%d)\n",
458 (int)pc
->sk
->key_type
);
464 sp_do_sign(const br_ssl_server_policy_class
**pctx
,
465 unsigned algo_id
, unsigned char *data
, size_t hv_len
, size_t len
)
468 unsigned char hv
[64];
470 pc
= (policy_context
*)pctx
;
471 if (algo_id
>= 0xFF00) {
473 memcpy(hv
, data
, hv_len
);
475 const br_hash_class
*hc
;
476 br_hash_compat_context zc
;
479 fprintf(stderr
, "Callback hashing, algo = 0x%04X,"
481 algo_id
, (unsigned long)hv_len
);
484 hc
= get_hash_impl(algo_id
);
488 "ERROR: unsupported hash function %u\n",
493 hc
->init(&zc
.vtable
);
494 hc
->update(&zc
.vtable
, data
, hv_len
);
495 hc
->out(&zc
.vtable
, hv
);
496 hv_len
= (hc
->desc
>> BR_HASHDESC_OUT_OFF
)
497 & BR_HASHDESC_OUT_MASK
;
499 switch (pc
->sk
->key_type
) {
502 const unsigned char *hash_oid
;
503 const br_hash_class
*hc
;
506 hash_oid
= get_hash_oid(algo_id
);
507 if (hash_oid
== NULL
&& algo_id
!= 0) {
509 fprintf(stderr
, "ERROR: cannot RSA-sign with"
510 " unknown hash function: %u\n",
515 sig_len
= (pc
->sk
->key
.rsa
.n_bitlen
+ 7) >> 3;
518 fprintf(stderr
, "ERROR: cannot RSA-sign,"
519 " buffer is too small"
520 " (sig=%lu, buf=%lu)\n",
521 (unsigned long)sig_len
,
526 x
= br_rsa_i31_pkcs1_sign(hash_oid
, hv
, hv_len
,
527 &pc
->sk
->key
.rsa
, data
);
530 fprintf(stderr
, "ERROR: RSA-sign failure\n");
537 hc
= get_hash_impl(algo_id
);
540 fprintf(stderr
, "ERROR: cannot ECDSA-sign with"
541 " unknown hash function: %u\n",
548 fprintf(stderr
, "ERROR: cannot ECDSA-sign"
549 " (output buffer = %lu)\n",
554 sig_len
= br_ecdsa_i31_sign_asn1(&br_ec_prime_i31
,
555 hc
, hv
, &pc
->sk
->key
.ec
, data
);
558 fprintf(stderr
, "ERROR: ECDSA-sign failure\n");
569 static const br_ssl_server_policy_class policy_vtable
= {
570 sizeof(policy_context
),
577 free_alpn(void *alpn
)
579 xfree(*(char **)alpn
);
584 do_server(int argc
, char *argv
[])
590 const char *bind_name
;
593 cipher_suite
*suites
;
598 br_x509_certificate
*chain
;
600 int cert_signer_algo
;
602 anchor_list anchors
= VEC_INIT
;
603 VECTOR(const char *) alpn_names
= VEC_INIT
;
604 br_x509_minimal_context xc
;
605 const br_hash_class
*dnhash
;
607 br_ssl_server_context cc
;
609 br_ssl_session_cache_lru lru
;
610 unsigned char *iobuf
, *cache
;
611 size_t iobuf_len
, cache_len
;
634 cache_len
= (size_t)-1;
638 for (i
= 0; i
< argc
; i
++) {
644 goto server_exit_error
;
646 if (eqstr(arg
, "-v") || eqstr(arg
, "-verbose")) {
648 } else if (eqstr(arg
, "-q") || eqstr(arg
, "-quiet")) {
650 } else if (eqstr(arg
, "-trace")) {
652 } else if (eqstr(arg
, "-b")) {
655 "ERROR: no argument for '-b'\n");
657 goto server_exit_error
;
659 if (bind_name
!= NULL
) {
660 fprintf(stderr
, "ERROR: duplicate bind host\n");
662 goto server_exit_error
;
665 } else if (eqstr(arg
, "-p")) {
668 "ERROR: no argument for '-p'\n");
670 goto server_exit_error
;
673 fprintf(stderr
, "ERROR: duplicate bind port\n");
675 goto server_exit_error
;
678 } else if (eqstr(arg
, "-mono")) {
680 } else if (eqstr(arg
, "-buf")) {
683 "ERROR: no argument for '-buf'\n");
685 goto server_exit_error
;
688 if (iobuf_len
!= 0) {
690 "ERROR: duplicate I/O buffer length\n");
692 goto server_exit_error
;
694 iobuf_len
= parse_size(arg
);
695 if (iobuf_len
== (size_t)-1) {
697 goto server_exit_error
;
699 } else if (eqstr(arg
, "-cache")) {
702 "ERROR: no argument for '-cache'\n");
704 goto server_exit_error
;
707 if (cache_len
!= (size_t)-1) {
708 fprintf(stderr
, "ERROR: duplicate session"
711 goto server_exit_error
;
713 cache_len
= parse_size(arg
);
714 if (cache_len
== (size_t)-1) {
716 goto server_exit_error
;
718 } else if (eqstr(arg
, "-cert")) {
721 "ERROR: no argument for '-cert'\n");
723 goto server_exit_error
;
727 "ERROR: duplicate certificate chain\n");
729 goto server_exit_error
;
732 chain
= read_certificates(arg
, &chain_len
);
733 if (chain
== NULL
|| chain_len
== 0) {
734 goto server_exit_error
;
736 } else if (eqstr(arg
, "-key")) {
739 "ERROR: no argument for '-key'\n");
741 goto server_exit_error
;
745 "ERROR: duplicate private key\n");
747 goto server_exit_error
;
750 sk
= read_private_key(arg
);
752 goto server_exit_error
;
754 } else if (eqstr(arg
, "-CA")) {
757 "ERROR: no argument for '-CA'\n");
759 goto server_exit_error
;
762 if (read_trust_anchors(&anchors
, arg
) == 0) {
764 goto server_exit_error
;
766 } else if (eqstr(arg
, "-anon_ok")) {
767 flags
|= BR_OPT_TOLERATE_NO_CLIENT_AUTH
;
768 } else if (eqstr(arg
, "-list")) {
771 } else if (eqstr(arg
, "-vmin")) {
774 "ERROR: no argument for '-vmin'\n");
776 goto server_exit_error
;
781 "ERROR: duplicate minimum version\n");
783 goto server_exit_error
;
785 vmin
= parse_version(arg
, strlen(arg
));
788 "ERROR: unrecognised version '%s'\n",
791 goto server_exit_error
;
793 } else if (eqstr(arg
, "-vmax")) {
796 "ERROR: no argument for '-vmax'\n");
798 goto server_exit_error
;
803 "ERROR: duplicate maximum version\n");
805 goto server_exit_error
;
807 vmax
= parse_version(arg
, strlen(arg
));
810 "ERROR: unrecognised version '%s'\n",
813 goto server_exit_error
;
815 } else if (eqstr(arg
, "-cs")) {
818 "ERROR: no argument for '-cs'\n");
820 goto server_exit_error
;
823 if (suites
!= NULL
) {
824 fprintf(stderr
, "ERROR: duplicate list"
825 " of cipher suites\n");
827 goto server_exit_error
;
829 suites
= parse_suites(arg
, &num_suites
);
830 if (suites
== NULL
) {
832 goto server_exit_error
;
834 } else if (eqstr(arg
, "-hf")) {
839 "ERROR: no argument for '-hf'\n");
841 goto server_exit_error
;
844 x
= parse_hash_functions(arg
);
847 goto server_exit_error
;
850 } else if (eqstr(arg
, "-cbhash")) {
852 } else if (eqstr(arg
, "-serverpref")) {
853 flags
|= BR_OPT_ENFORCE_SERVER_PREFERENCES
;
854 } else if (eqstr(arg
, "-noreneg")) {
855 flags
|= BR_OPT_NO_RENEGOTIATION
;
856 } else if (eqstr(arg
, "-alpn")) {
859 "ERROR: no argument for '-alpn'\n");
861 goto server_exit_error
;
863 VEC_ADD(alpn_names
, xstrdup(argv
[i
]));
864 } else if (eqstr(arg
, "-strictalpn")) {
865 flags
|= BR_OPT_FAIL_ON_ALPN_MISMATCH
;
867 fprintf(stderr
, "ERROR: unknown option: '%s'\n", arg
);
869 goto server_exit_error
;
882 fprintf(stderr
, "ERROR: impossible minimum/maximum protocol"
883 " version combination\n");
885 goto server_exit_error
;
887 if (suites
== NULL
) {
890 for (u
= 0; cipher_suites
[u
].name
; u
++) {
891 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
897 suites
= xmalloc(num_suites
* sizeof *suites
);
899 for (u
= 0; cipher_suites
[u
].name
; u
++) {
900 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
903 suites
[num_suites
++] = cipher_suites
[u
];
908 hfuns
= (unsigned)-1;
910 if (chain
== NULL
|| chain_len
== 0) {
911 fprintf(stderr
, "ERROR: no certificate chain provided\n");
912 goto server_exit_error
;
915 fprintf(stderr
, "ERROR: no private key provided\n");
916 goto server_exit_error
;
918 switch (sk
->key_type
) {
925 curve
= sk
->key
.ec
.curve
;
926 supp
= br_ec_prime_i31
.supported_curves
;
927 if (curve
> 31 || !((supp
>> curve
) & 1)) {
928 fprintf(stderr
, "ERROR: private key curve (%d)"
929 " is not supported\n", curve
);
930 goto server_exit_error
;
934 fprintf(stderr
, "ERROR: unsupported private key type (%d)\n",
938 cert_signer_algo
= get_cert_signer_algo(chain
);
939 if (cert_signer_algo
== 0) {
940 goto server_exit_error
;
945 switch (cert_signer_algo
) {
946 case BR_KEYTYPE_RSA
: csas
= "RSA"; break;
947 case BR_KEYTYPE_EC
: csas
= "EC"; break;
952 fprintf(stderr
, "Issuing CA key type: %d (%s)\n",
953 cert_signer_algo
, csas
);
955 if (iobuf_len
== 0) {
957 iobuf_len
= BR_SSL_BUFSIZE_BIDI
;
959 iobuf_len
= BR_SSL_BUFSIZE_MONO
;
962 iobuf
= xmalloc(iobuf_len
);
963 if (cache_len
== (size_t)-1) {
966 cache
= xmalloc(cache_len
);
969 * Compute implementation requirements and inject implementations.
971 suite_ids
= xmalloc(num_suites
* sizeof *suite_ids
);
972 br_ssl_server_zero(&cc
);
973 br_ssl_engine_set_versions(&cc
.eng
, vmin
, vmax
);
974 br_ssl_engine_set_all_flags(&cc
.eng
, flags
);
975 if (vmin
<= BR_TLS11
) {
976 if (!(hfuns
& (1 << br_md5_ID
))) {
977 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need MD5\n");
978 goto server_exit_error
;
980 if (!(hfuns
& (1 << br_sha1_ID
))) {
981 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
982 goto server_exit_error
;
985 for (u
= 0; u
< num_suites
; u
++) {
989 suite_ids
[u
] = suites
[u
].suite
;
990 if ((req
& REQ_TLS12
) != 0 && vmax
< BR_TLS12
) {
992 "ERROR: cipher suite %s requires TLS 1.2\n",
994 goto server_exit_error
;
996 if ((req
& REQ_SHA1
) != 0 && !(hfuns
& (1 << br_sha1_ID
))) {
998 "ERROR: cipher suite %s requires SHA-1\n",
1000 goto server_exit_error
;
1002 if ((req
& REQ_SHA256
) != 0 && !(hfuns
& (1 << br_sha256_ID
))) {
1004 "ERROR: cipher suite %s requires SHA-256\n",
1006 goto server_exit_error
;
1008 if ((req
& REQ_SHA384
) != 0 && !(hfuns
& (1 << br_sha384_ID
))) {
1010 "ERROR: cipher suite %s requires SHA-384\n",
1012 goto server_exit_error
;
1014 /* TODO: algorithm implementation selection */
1015 if ((req
& REQ_AESCBC
) != 0) {
1016 br_ssl_engine_set_aes_cbc(&cc
.eng
,
1017 &br_aes_ct_cbcenc_vtable
,
1018 &br_aes_ct_cbcdec_vtable
);
1019 br_ssl_engine_set_cbc(&cc
.eng
,
1020 &br_sslrec_in_cbc_vtable
,
1021 &br_sslrec_out_cbc_vtable
);
1023 if ((req
& REQ_AESGCM
) != 0) {
1024 br_ssl_engine_set_aes_ctr(&cc
.eng
,
1025 &br_aes_ct_ctr_vtable
);
1026 br_ssl_engine_set_ghash(&cc
.eng
,
1028 br_ssl_engine_set_gcm(&cc
.eng
,
1029 &br_sslrec_in_gcm_vtable
,
1030 &br_sslrec_out_gcm_vtable
);
1032 if ((req
& REQ_CHAPOL
) != 0) {
1033 br_ssl_engine_set_chacha20(&cc
.eng
,
1034 &br_chacha20_ct_run
);
1035 br_ssl_engine_set_poly1305(&cc
.eng
,
1036 &br_poly1305_ctmul_run
);
1037 br_ssl_engine_set_chapol(&cc
.eng
,
1038 &br_sslrec_in_chapol_vtable
,
1039 &br_sslrec_out_chapol_vtable
);
1041 if ((req
& REQ_3DESCBC
) != 0) {
1042 br_ssl_engine_set_des_cbc(&cc
.eng
,
1043 &br_des_ct_cbcenc_vtable
,
1044 &br_des_ct_cbcdec_vtable
);
1045 br_ssl_engine_set_cbc(&cc
.eng
,
1046 &br_sslrec_in_cbc_vtable
,
1047 &br_sslrec_out_cbc_vtable
);
1049 if ((req
& (REQ_ECDHE_RSA
| REQ_ECDHE_ECDSA
)) != 0) {
1050 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_prime_i31
);
1053 br_ssl_engine_set_suites(&cc
.eng
, suite_ids
, num_suites
);
1056 for (u
= 0; hash_functions
[u
].name
; u
++) {
1057 const br_hash_class
*hc
;
1060 hc
= hash_functions
[u
].hclass
;
1061 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
) & BR_HASHDESC_ID_MASK
;
1062 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
1064 br_ssl_engine_set_hash(&cc
.eng
, id
, hc
);
1067 if (vmin
<= BR_TLS11
) {
1068 br_ssl_engine_set_prf10(&cc
.eng
, &br_tls10_prf
);
1070 if (vmax
>= BR_TLS12
) {
1071 if ((hfuns
& ((unsigned)1 << br_sha256_ID
)) != 0) {
1072 br_ssl_engine_set_prf_sha256(&cc
.eng
,
1073 &br_tls12_sha256_prf
);
1075 if ((hfuns
& ((unsigned)1 << br_sha384_ID
)) != 0) {
1076 br_ssl_engine_set_prf_sha384(&cc
.eng
,
1077 &br_tls12_sha384_prf
);
1081 br_ssl_session_cache_lru_init(&lru
, cache
, cache_len
);
1082 br_ssl_server_set_cache(&cc
, &lru
.vtable
);
1084 if (VEC_LEN(alpn_names
) != 0) {
1085 br_ssl_engine_set_protocol_names(&cc
.eng
,
1086 &VEC_ELT(alpn_names
, 0), VEC_LEN(alpn_names
));
1090 * Set the policy handler (that chooses the actual cipher suite,
1091 * selects the certificate chain, and runs the private key
1094 pc
.vtable
= &policy_vtable
;
1095 pc
.verbose
= verbose
;
1097 pc
.chain_len
= chain_len
;
1098 pc
.cert_signer_algo
= cert_signer_algo
;
1101 br_ssl_server_set_policy(&cc
, &pc
.vtable
);
1104 * If trust anchors have been configured, then set an X.509
1105 * validation engine and activate client certificate
1108 if (VEC_LEN(anchors
) != 0) {
1109 br_x509_minimal_init(&xc
, dnhash
,
1110 &VEC_ELT(anchors
, 0), VEC_LEN(anchors
));
1111 for (u
= 0; hash_functions
[u
].name
; u
++) {
1112 const br_hash_class
*hc
;
1115 hc
= hash_functions
[u
].hclass
;
1116 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
)
1117 & BR_HASHDESC_ID_MASK
;
1118 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
1119 br_x509_minimal_set_hash(&xc
, id
, hc
);
1122 br_ssl_engine_set_rsavrfy(&cc
.eng
, &br_rsa_i31_pkcs1_vrfy
);
1123 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_prime_i31
);
1124 br_ssl_engine_set_ecdsa(&cc
.eng
, &br_ecdsa_i31_vrfy_asn1
);
1125 br_x509_minimal_set_rsa(&xc
, &br_rsa_i31_pkcs1_vrfy
);
1126 br_x509_minimal_set_ecdsa(&xc
,
1127 &br_ec_prime_i31
, &br_ecdsa_i31_vrfy_asn1
);
1128 br_ssl_engine_set_x509(&cc
.eng
, &xc
.vtable
);
1129 br_ssl_server_set_trust_anchor_names_alt(&cc
,
1130 &VEC_ELT(anchors
, 0), VEC_LEN(anchors
));
1133 br_ssl_engine_set_buffer(&cc
.eng
, iobuf
, iobuf_len
, bidi
);
1136 * We need to ignore SIGPIPE.
1138 signal(SIGPIPE
, SIG_IGN
);
1141 * Open the server socket.
1143 server_fd
= host_bind(bind_name
, port
, verbose
);
1144 if (server_fd
< 0) {
1145 goto server_exit_error
;
1149 * Process incoming clients, one at a time. Note that we do not
1150 * accept any client until the previous connection has finished:
1151 * this is voluntary, since the tool uses stdin/stdout for
1152 * application data, and thus cannot really run two connections
1158 fd
= accept_client(server_fd
, verbose
);
1160 goto server_exit_error
;
1162 br_ssl_server_reset(&cc
);
1163 x
= run_ssl_engine(&cc
.eng
, fd
,
1164 (verbose
? RUN_ENGINE_VERBOSE
: 0)
1165 | (trace
? RUN_ENGINE_TRACE
: 0));
1169 goto server_exit_error
;
1174 * Release allocated structures.
1179 free_certificates(chain
, chain_len
);
1180 free_private_key(sk
);
1181 VEC_CLEAREXT(anchors
, &free_ta_contents
);
1182 VEC_CLEAREXT(alpn_names
, &free_alpn
);