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
)
449 pc
= (policy_context
*)pctx
;
450 switch (pc
->sk
->key_type
) {
452 return br_rsa_ssl_decrypt(
453 &br_rsa_i31_private
, &pc
->sk
->key
.rsa
,
456 r
= br_ec_all_m15
.mul(data
, *len
, pc
->sk
->key
.ec
.x
,
457 pc
->sk
->key
.ec
.xlen
, pc
->sk
->key
.ec
.curve
);
458 xoff
= br_ec_all_m15
.xoff(pc
->sk
->key
.ec
.curve
, &xlen
);
459 memmove(data
, data
+ xoff
, xlen
);
463 fprintf(stderr
, "ERROR: unknown private key type (%d)\n",
464 (int)pc
->sk
->key_type
);
470 sp_do_sign(const br_ssl_server_policy_class
**pctx
,
471 unsigned algo_id
, unsigned char *data
, size_t hv_len
, size_t len
)
474 unsigned char hv
[64];
476 pc
= (policy_context
*)pctx
;
477 if (algo_id
>= 0xFF00) {
479 memcpy(hv
, data
, hv_len
);
481 const br_hash_class
*hc
;
482 br_hash_compat_context zc
;
485 fprintf(stderr
, "Callback hashing, algo = 0x%04X,"
487 algo_id
, (unsigned long)hv_len
);
490 hc
= get_hash_impl(algo_id
);
494 "ERROR: unsupported hash function %u\n",
499 hc
->init(&zc
.vtable
);
500 hc
->update(&zc
.vtable
, data
, hv_len
);
501 hc
->out(&zc
.vtable
, hv
);
502 hv_len
= (hc
->desc
>> BR_HASHDESC_OUT_OFF
)
503 & BR_HASHDESC_OUT_MASK
;
505 switch (pc
->sk
->key_type
) {
508 const unsigned char *hash_oid
;
509 const br_hash_class
*hc
;
512 hash_oid
= get_hash_oid(algo_id
);
513 if (hash_oid
== NULL
&& algo_id
!= 0) {
515 fprintf(stderr
, "ERROR: cannot RSA-sign with"
516 " unknown hash function: %u\n",
521 sig_len
= (pc
->sk
->key
.rsa
.n_bitlen
+ 7) >> 3;
524 fprintf(stderr
, "ERROR: cannot RSA-sign,"
525 " buffer is too small"
526 " (sig=%lu, buf=%lu)\n",
527 (unsigned long)sig_len
,
532 x
= br_rsa_i31_pkcs1_sign(hash_oid
, hv
, hv_len
,
533 &pc
->sk
->key
.rsa
, data
);
536 fprintf(stderr
, "ERROR: RSA-sign failure\n");
543 hc
= get_hash_impl(algo_id
);
546 fprintf(stderr
, "ERROR: cannot ECDSA-sign with"
547 " unknown hash function: %u\n",
554 fprintf(stderr
, "ERROR: cannot ECDSA-sign"
555 " (output buffer = %lu)\n",
560 sig_len
= br_ecdsa_i31_sign_asn1(&br_ec_all_m15
,
561 hc
, hv
, &pc
->sk
->key
.ec
, data
);
564 fprintf(stderr
, "ERROR: ECDSA-sign failure\n");
575 static const br_ssl_server_policy_class policy_vtable
= {
576 sizeof(policy_context
),
583 free_alpn(void *alpn
)
585 xfree(*(char **)alpn
);
590 do_server(int argc
, char *argv
[])
596 const char *bind_name
;
599 cipher_suite
*suites
;
604 br_x509_certificate
*chain
;
606 int cert_signer_algo
;
608 anchor_list anchors
= VEC_INIT
;
609 VECTOR(const char *) alpn_names
= VEC_INIT
;
610 br_x509_minimal_context xc
;
611 const br_hash_class
*dnhash
;
613 br_ssl_server_context cc
;
615 br_ssl_session_cache_lru lru
;
616 unsigned char *iobuf
, *cache
;
617 size_t iobuf_len
, cache_len
;
640 cache_len
= (size_t)-1;
644 for (i
= 0; i
< argc
; i
++) {
650 goto server_exit_error
;
652 if (eqstr(arg
, "-v") || eqstr(arg
, "-verbose")) {
654 } else if (eqstr(arg
, "-q") || eqstr(arg
, "-quiet")) {
656 } else if (eqstr(arg
, "-trace")) {
658 } else if (eqstr(arg
, "-b")) {
661 "ERROR: no argument for '-b'\n");
663 goto server_exit_error
;
665 if (bind_name
!= NULL
) {
666 fprintf(stderr
, "ERROR: duplicate bind host\n");
668 goto server_exit_error
;
671 } else if (eqstr(arg
, "-p")) {
674 "ERROR: no argument for '-p'\n");
676 goto server_exit_error
;
679 fprintf(stderr
, "ERROR: duplicate bind port\n");
681 goto server_exit_error
;
684 } else if (eqstr(arg
, "-mono")) {
686 } else if (eqstr(arg
, "-buf")) {
689 "ERROR: no argument for '-buf'\n");
691 goto server_exit_error
;
694 if (iobuf_len
!= 0) {
696 "ERROR: duplicate I/O buffer length\n");
698 goto server_exit_error
;
700 iobuf_len
= parse_size(arg
);
701 if (iobuf_len
== (size_t)-1) {
703 goto server_exit_error
;
705 } else if (eqstr(arg
, "-cache")) {
708 "ERROR: no argument for '-cache'\n");
710 goto server_exit_error
;
713 if (cache_len
!= (size_t)-1) {
714 fprintf(stderr
, "ERROR: duplicate session"
717 goto server_exit_error
;
719 cache_len
= parse_size(arg
);
720 if (cache_len
== (size_t)-1) {
722 goto server_exit_error
;
724 } else if (eqstr(arg
, "-cert")) {
727 "ERROR: no argument for '-cert'\n");
729 goto server_exit_error
;
733 "ERROR: duplicate certificate chain\n");
735 goto server_exit_error
;
738 chain
= read_certificates(arg
, &chain_len
);
739 if (chain
== NULL
|| chain_len
== 0) {
740 goto server_exit_error
;
742 } else if (eqstr(arg
, "-key")) {
745 "ERROR: no argument for '-key'\n");
747 goto server_exit_error
;
751 "ERROR: duplicate private key\n");
753 goto server_exit_error
;
756 sk
= read_private_key(arg
);
758 goto server_exit_error
;
760 } else if (eqstr(arg
, "-CA")) {
763 "ERROR: no argument for '-CA'\n");
765 goto server_exit_error
;
768 if (read_trust_anchors(&anchors
, arg
) == 0) {
770 goto server_exit_error
;
772 } else if (eqstr(arg
, "-anon_ok")) {
773 flags
|= BR_OPT_TOLERATE_NO_CLIENT_AUTH
;
774 } else if (eqstr(arg
, "-list")) {
777 } else if (eqstr(arg
, "-vmin")) {
780 "ERROR: no argument for '-vmin'\n");
782 goto server_exit_error
;
787 "ERROR: duplicate minimum version\n");
789 goto server_exit_error
;
791 vmin
= parse_version(arg
, strlen(arg
));
794 "ERROR: unrecognised version '%s'\n",
797 goto server_exit_error
;
799 } else if (eqstr(arg
, "-vmax")) {
802 "ERROR: no argument for '-vmax'\n");
804 goto server_exit_error
;
809 "ERROR: duplicate maximum version\n");
811 goto server_exit_error
;
813 vmax
= parse_version(arg
, strlen(arg
));
816 "ERROR: unrecognised version '%s'\n",
819 goto server_exit_error
;
821 } else if (eqstr(arg
, "-cs")) {
824 "ERROR: no argument for '-cs'\n");
826 goto server_exit_error
;
829 if (suites
!= NULL
) {
830 fprintf(stderr
, "ERROR: duplicate list"
831 " of cipher suites\n");
833 goto server_exit_error
;
835 suites
= parse_suites(arg
, &num_suites
);
836 if (suites
== NULL
) {
838 goto server_exit_error
;
840 } else if (eqstr(arg
, "-hf")) {
845 "ERROR: no argument for '-hf'\n");
847 goto server_exit_error
;
850 x
= parse_hash_functions(arg
);
853 goto server_exit_error
;
856 } else if (eqstr(arg
, "-cbhash")) {
858 } else if (eqstr(arg
, "-serverpref")) {
859 flags
|= BR_OPT_ENFORCE_SERVER_PREFERENCES
;
860 } else if (eqstr(arg
, "-noreneg")) {
861 flags
|= BR_OPT_NO_RENEGOTIATION
;
862 } else if (eqstr(arg
, "-alpn")) {
865 "ERROR: no argument for '-alpn'\n");
867 goto server_exit_error
;
869 VEC_ADD(alpn_names
, xstrdup(argv
[i
]));
870 } else if (eqstr(arg
, "-strictalpn")) {
871 flags
|= BR_OPT_FAIL_ON_ALPN_MISMATCH
;
873 fprintf(stderr
, "ERROR: unknown option: '%s'\n", arg
);
875 goto server_exit_error
;
888 fprintf(stderr
, "ERROR: impossible minimum/maximum protocol"
889 " version combination\n");
891 goto server_exit_error
;
893 if (suites
== NULL
) {
896 for (u
= 0; cipher_suites
[u
].name
; u
++) {
897 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
903 suites
= xmalloc(num_suites
* sizeof *suites
);
905 for (u
= 0; cipher_suites
[u
].name
; u
++) {
906 if ((cipher_suites
[u
].req
& REQ_TLS12
) == 0
909 suites
[num_suites
++] = cipher_suites
[u
];
914 hfuns
= (unsigned)-1;
916 if (chain
== NULL
|| chain_len
== 0) {
917 fprintf(stderr
, "ERROR: no certificate chain provided\n");
918 goto server_exit_error
;
921 fprintf(stderr
, "ERROR: no private key provided\n");
922 goto server_exit_error
;
924 switch (sk
->key_type
) {
931 curve
= sk
->key
.ec
.curve
;
932 supp
= br_ec_all_m15
.supported_curves
;
933 if (curve
> 31 || !((supp
>> curve
) & 1)) {
934 fprintf(stderr
, "ERROR: private key curve (%d)"
935 " is not supported\n", curve
);
936 goto server_exit_error
;
940 fprintf(stderr
, "ERROR: unsupported private key type (%d)\n",
944 cert_signer_algo
= get_cert_signer_algo(chain
);
945 if (cert_signer_algo
== 0) {
946 goto server_exit_error
;
951 switch (cert_signer_algo
) {
952 case BR_KEYTYPE_RSA
: csas
= "RSA"; break;
953 case BR_KEYTYPE_EC
: csas
= "EC"; break;
958 fprintf(stderr
, "Issuing CA key type: %d (%s)\n",
959 cert_signer_algo
, csas
);
961 if (iobuf_len
== 0) {
963 iobuf_len
= BR_SSL_BUFSIZE_BIDI
;
965 iobuf_len
= BR_SSL_BUFSIZE_MONO
;
968 iobuf
= xmalloc(iobuf_len
);
969 if (cache_len
== (size_t)-1) {
972 cache
= xmalloc(cache_len
);
975 * Compute implementation requirements and inject implementations.
977 suite_ids
= xmalloc(num_suites
* sizeof *suite_ids
);
978 br_ssl_server_zero(&cc
);
979 br_ssl_engine_set_versions(&cc
.eng
, vmin
, vmax
);
980 br_ssl_engine_set_all_flags(&cc
.eng
, flags
);
981 if (vmin
<= BR_TLS11
) {
982 if (!(hfuns
& (1 << br_md5_ID
))) {
983 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need MD5\n");
984 goto server_exit_error
;
986 if (!(hfuns
& (1 << br_sha1_ID
))) {
987 fprintf(stderr
, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
988 goto server_exit_error
;
991 for (u
= 0; u
< num_suites
; u
++) {
995 suite_ids
[u
] = suites
[u
].suite
;
996 if ((req
& REQ_TLS12
) != 0 && vmax
< BR_TLS12
) {
998 "ERROR: cipher suite %s requires TLS 1.2\n",
1000 goto server_exit_error
;
1002 if ((req
& REQ_SHA1
) != 0 && !(hfuns
& (1 << br_sha1_ID
))) {
1004 "ERROR: cipher suite %s requires SHA-1\n",
1006 goto server_exit_error
;
1008 if ((req
& REQ_SHA256
) != 0 && !(hfuns
& (1 << br_sha256_ID
))) {
1010 "ERROR: cipher suite %s requires SHA-256\n",
1012 goto server_exit_error
;
1014 if ((req
& REQ_SHA384
) != 0 && !(hfuns
& (1 << br_sha384_ID
))) {
1016 "ERROR: cipher suite %s requires SHA-384\n",
1018 goto server_exit_error
;
1020 /* TODO: algorithm implementation selection */
1021 if ((req
& REQ_AESCBC
) != 0) {
1022 br_ssl_engine_set_aes_cbc(&cc
.eng
,
1023 &br_aes_ct_cbcenc_vtable
,
1024 &br_aes_ct_cbcdec_vtable
);
1025 br_ssl_engine_set_cbc(&cc
.eng
,
1026 &br_sslrec_in_cbc_vtable
,
1027 &br_sslrec_out_cbc_vtable
);
1029 if ((req
& REQ_AESGCM
) != 0) {
1030 br_ssl_engine_set_aes_ctr(&cc
.eng
,
1031 &br_aes_ct_ctr_vtable
);
1032 br_ssl_engine_set_ghash(&cc
.eng
,
1034 br_ssl_engine_set_gcm(&cc
.eng
,
1035 &br_sslrec_in_gcm_vtable
,
1036 &br_sslrec_out_gcm_vtable
);
1038 if ((req
& REQ_CHAPOL
) != 0) {
1039 br_ssl_engine_set_chacha20(&cc
.eng
,
1040 &br_chacha20_ct_run
);
1041 br_ssl_engine_set_poly1305(&cc
.eng
,
1042 &br_poly1305_ctmul_run
);
1043 br_ssl_engine_set_chapol(&cc
.eng
,
1044 &br_sslrec_in_chapol_vtable
,
1045 &br_sslrec_out_chapol_vtable
);
1047 if ((req
& REQ_3DESCBC
) != 0) {
1048 br_ssl_engine_set_des_cbc(&cc
.eng
,
1049 &br_des_ct_cbcenc_vtable
,
1050 &br_des_ct_cbcdec_vtable
);
1051 br_ssl_engine_set_cbc(&cc
.eng
,
1052 &br_sslrec_in_cbc_vtable
,
1053 &br_sslrec_out_cbc_vtable
);
1055 if ((req
& (REQ_ECDHE_RSA
| REQ_ECDHE_ECDSA
)) != 0) {
1056 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_all_m15
);
1059 br_ssl_engine_set_suites(&cc
.eng
, suite_ids
, num_suites
);
1062 for (u
= 0; hash_functions
[u
].name
; u
++) {
1063 const br_hash_class
*hc
;
1066 hc
= hash_functions
[u
].hclass
;
1067 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
) & BR_HASHDESC_ID_MASK
;
1068 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
1070 br_ssl_engine_set_hash(&cc
.eng
, id
, hc
);
1073 if (vmin
<= BR_TLS11
) {
1074 br_ssl_engine_set_prf10(&cc
.eng
, &br_tls10_prf
);
1076 if (vmax
>= BR_TLS12
) {
1077 if ((hfuns
& ((unsigned)1 << br_sha256_ID
)) != 0) {
1078 br_ssl_engine_set_prf_sha256(&cc
.eng
,
1079 &br_tls12_sha256_prf
);
1081 if ((hfuns
& ((unsigned)1 << br_sha384_ID
)) != 0) {
1082 br_ssl_engine_set_prf_sha384(&cc
.eng
,
1083 &br_tls12_sha384_prf
);
1087 br_ssl_session_cache_lru_init(&lru
, cache
, cache_len
);
1088 br_ssl_server_set_cache(&cc
, &lru
.vtable
);
1090 if (VEC_LEN(alpn_names
) != 0) {
1091 br_ssl_engine_set_protocol_names(&cc
.eng
,
1092 &VEC_ELT(alpn_names
, 0), VEC_LEN(alpn_names
));
1096 * Set the policy handler (that chooses the actual cipher suite,
1097 * selects the certificate chain, and runs the private key
1100 pc
.vtable
= &policy_vtable
;
1101 pc
.verbose
= verbose
;
1103 pc
.chain_len
= chain_len
;
1104 pc
.cert_signer_algo
= cert_signer_algo
;
1107 br_ssl_server_set_policy(&cc
, &pc
.vtable
);
1110 * If trust anchors have been configured, then set an X.509
1111 * validation engine and activate client certificate
1114 if (VEC_LEN(anchors
) != 0) {
1115 br_x509_minimal_init(&xc
, dnhash
,
1116 &VEC_ELT(anchors
, 0), VEC_LEN(anchors
));
1117 for (u
= 0; hash_functions
[u
].name
; u
++) {
1118 const br_hash_class
*hc
;
1121 hc
= hash_functions
[u
].hclass
;
1122 id
= (hc
->desc
>> BR_HASHDESC_ID_OFF
)
1123 & BR_HASHDESC_ID_MASK
;
1124 if ((hfuns
& ((unsigned)1 << id
)) != 0) {
1125 br_x509_minimal_set_hash(&xc
, id
, hc
);
1128 br_ssl_engine_set_rsavrfy(&cc
.eng
, &br_rsa_i31_pkcs1_vrfy
);
1129 br_ssl_engine_set_ec(&cc
.eng
, &br_ec_all_m15
);
1130 br_ssl_engine_set_ecdsa(&cc
.eng
, &br_ecdsa_i31_vrfy_asn1
);
1131 br_x509_minimal_set_rsa(&xc
, &br_rsa_i31_pkcs1_vrfy
);
1132 br_x509_minimal_set_ecdsa(&xc
,
1133 &br_ec_all_m15
, &br_ecdsa_i31_vrfy_asn1
);
1134 br_ssl_engine_set_x509(&cc
.eng
, &xc
.vtable
);
1135 br_ssl_server_set_trust_anchor_names_alt(&cc
,
1136 &VEC_ELT(anchors
, 0), VEC_LEN(anchors
));
1139 br_ssl_engine_set_buffer(&cc
.eng
, iobuf
, iobuf_len
, bidi
);
1142 * We need to ignore SIGPIPE.
1144 signal(SIGPIPE
, SIG_IGN
);
1147 * Open the server socket.
1149 server_fd
= host_bind(bind_name
, port
, verbose
);
1150 if (server_fd
< 0) {
1151 goto server_exit_error
;
1155 * Process incoming clients, one at a time. Note that we do not
1156 * accept any client until the previous connection has finished:
1157 * this is voluntary, since the tool uses stdin/stdout for
1158 * application data, and thus cannot really run two connections
1164 fd
= accept_client(server_fd
, verbose
);
1166 goto server_exit_error
;
1168 br_ssl_server_reset(&cc
);
1169 x
= run_ssl_engine(&cc
.eng
, fd
,
1170 (verbose
? RUN_ENGINE_VERBOSE
: 0)
1171 | (trace
? RUN_ENGINE_TRACE
: 0));
1175 goto server_exit_error
;
1180 * Release allocated structures.
1185 free_certificates(chain
, chain_len
);
1186 free_private_key(sk
);
1187 VEC_CLEAREXT(anchors
, &free_ta_contents
);
1188 VEC_CLEAREXT(alpn_names
, &free_alpn
);