2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 int flag2, flag3, bflag, cflag, hflag, Nflag, Oflag, Vflag, vflag;
22 const char *cert, *key;
27 dprintf(2, "%s: timer expired\n", getprogname());
32 main(int argc, char **argv)
35 struct tls_config *conf;
37 char iribuf[GEMINI_URL_LEN], buf[GEMINI_URL_LEN];
38 const char *parse_err = "unknown error", *port = "1965", *errstr;
41 int ch, handshake, timer;
45 while ((ch = getopt(argc, argv, "23C:cbH:hK:NOT:Vv")) != -1) {
78 timer = strtonum(optarg, 1, 1000, &errstr);
80 errx(1, "timeout is %s: %s", errstr, optarg);
81 signal(SIGALRM, timeout);
91 fprintf(stderr, "USAGE: %s [-23cbhNVv] [-H hostname]\n",
99 if ((bflag + cflag + hflag + Vflag) > 1)
100 errx(1, "only one of bchr flags can be used.");
102 if (flag2 + flag3 > 1)
103 errx(1, "only -2 or -3 can be specified at the same time.");
105 if ((cert != NULL && key == NULL) || (cert == NULL && key != NULL))
106 errx(1, "missing certificate or key");
109 errx(1, "missing IRI");
111 if (strlcpy(iribuf, argv[0], sizeof(iribuf)) >= sizeof(iribuf))
112 errx(1, "request too long: %s", argv[0]);
113 if (strlcpy(buf, argv[0], sizeof(buf)) >= sizeof(iribuf))
114 errx(1, "request too long: %s", argv[0]);
115 if (strlcat(buf, "\r\n", sizeof(buf)) >= sizeof(buf))
116 errx(1, "request too long: %s", argv[0]);
118 if (!parse_iri(iribuf, &iri, &parse_err))
119 errx(1, "invalid IRI: %s", parse_err);
124 if ((conf = tls_config_new()) == NULL)
125 errx(1, "tls_config_new");
127 tls_config_insecure_noverifycert(conf);
129 tls_config_insecure_noverifyname(conf);
132 tls_config_ocsp_require_stapling(conf);
134 if (flag2 && tls_config_set_protocols(conf, TLS_PROTOCOL_TLSv1_2) == -1)
135 errx(1, "cannot set TLSv1.2");
136 if (flag3 && tls_config_set_protocols(conf, TLS_PROTOCOL_TLSv1_3) == -1)
137 errx(1, "cannot set TLSv1.3");
139 if (cert != NULL && tls_config_set_keypair_file(conf, cert, key))
140 errx(1, "couldn't load cert: %s", cert);
142 if ((ctx = tls_client()) == NULL)
143 errx(1, "tls_client creation failed");
145 if (tls_configure(ctx, conf) == -1)
146 errx(1, "tls_configure: %s", tls_error(ctx));
148 if (*iri.port != '\0')
151 if (hostname == NULL)
154 if (tls_connect_servername(ctx, iri.host, port, hostname) == -1)
155 errx(1, "tls_connect: %s", tls_error(ctx));
157 for (handshake = 0; !handshake;) {
158 switch (tls_handshake(ctx)) {
168 if (tls_write(ctx, buf, strlen(buf)) == -1)
169 errx(1, "tls_write: %s", tls_error(ctx));
172 switch (len = tls_read(ctx, buf, sizeof(buf))) {
176 case TLS_WANT_POLLIN:
177 case TLS_WANT_POLLOUT:
183 if ((t = strchr(buf, '\r')) != NULL)
185 else if ((t = strchr(buf, '\n')) != NULL)
201 t = strchr(buf, '\r');
203 t = strchr(buf, '\n');
206 write(1, buf, t - buf);