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