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 f48e3b85 2021-05-24 op int flag2, flag3, bflag, cflag, hflag, Nflag, 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 321341f6 2021-07-06 op while ((ch = getopt(argc, argv, "23C:cbH:hK:NT:Vv")) != -1) {
57 f48e3b85 2021-05-24 op cert = optarg;
63 f48e3b85 2021-05-24 op hostname = optarg;
75 321341f6 2021-07-06 op timer = strtonum(optarg, 1, 1000, &errstr);
76 321341f6 2021-07-06 op if (errstr != NULL)
77 321341f6 2021-07-06 op errx(1, "timeout is %s: %s", errstr, optarg);
78 321341f6 2021-07-06 op signal(SIGALRM, timeout);
88 f48e3b85 2021-05-24 op fprintf(stderr, "USAGE: %s [-23cbhNVv] [-H hostname]\n",
93 f48e3b85 2021-05-24 op argc -= optind;
94 f48e3b85 2021-05-24 op argv += optind;
96 f48e3b85 2021-05-24 op if ((bflag + cflag + hflag + Vflag) > 1)
97 f48e3b85 2021-05-24 op errx(1, "only one of bchr flags can be used.");
99 f48e3b85 2021-05-24 op if (flag2 + flag3 > 1)
100 f48e3b85 2021-05-24 op errx(1, "only -2 or -3 can be specified at the same time.");
102 f48e3b85 2021-05-24 op if ((cert != NULL && key == NULL) || (cert == NULL && key != NULL))
103 f48e3b85 2021-05-24 op errx(1, "missing certificate or key");
105 f48e3b85 2021-05-24 op if (argc != 1)
106 f48e3b85 2021-05-24 op errx(1, "missing IRI");
108 f48e3b85 2021-05-24 op if (strlcpy(iribuf, argv[0], sizeof(iribuf)) >= sizeof(iribuf))
109 f48e3b85 2021-05-24 op errx(1, "request too long: %s", argv[0]);
110 f48e3b85 2021-05-24 op if (strlcpy(buf, argv[0], sizeof(buf)) >= sizeof(iribuf))
111 f48e3b85 2021-05-24 op errx(1, "request too long: %s", argv[0]);
112 f48e3b85 2021-05-24 op if (strlcat(buf, "\r\n", sizeof(buf)) >= sizeof(buf))
113 f48e3b85 2021-05-24 op errx(1, "request too long: %s", argv[0]);
115 f48e3b85 2021-05-24 op if (!parse_iri(iribuf, &iri, &parse_err))
116 f48e3b85 2021-05-24 op errx(1, "invalid IRI: %s", parse_err);
119 f48e3b85 2021-05-24 op errx(0, "IRI: OK");
121 f48e3b85 2021-05-24 op if ((conf = tls_config_new()) == NULL)
122 f48e3b85 2021-05-24 op errx(1, "tls_config_new");
124 f48e3b85 2021-05-24 op tls_config_insecure_noverifycert(conf);
126 f48e3b85 2021-05-24 op tls_config_insecure_noverifyname(conf);
128 f48e3b85 2021-05-24 op if (flag2 && tls_config_set_protocols(conf, TLS_PROTOCOL_TLSv1_2) == -1)
129 f48e3b85 2021-05-24 op errx(1, "cannot set TLSv1.2");
130 f48e3b85 2021-05-24 op if (flag3 && tls_config_set_protocols(conf, TLS_PROTOCOL_TLSv1_3) == -1)
131 f48e3b85 2021-05-24 op errx(1, "cannot set TLSv1.3");
133 f48e3b85 2021-05-24 op if (cert != NULL && tls_config_set_keypair_file(conf, cert, key))
134 f48e3b85 2021-05-24 op errx(1, "couldn't load cert: %s", cert);
136 f48e3b85 2021-05-24 op if ((ctx = tls_client()) == NULL)
137 f48e3b85 2021-05-24 op errx(1, "tls_client creation failed");
139 f48e3b85 2021-05-24 op if (tls_configure(ctx, conf) == -1)
140 f48e3b85 2021-05-24 op errx(1, "tls_configure: %s", tls_error(ctx));
142 f48e3b85 2021-05-24 op if (*iri.port != '\0')
143 f48e3b85 2021-05-24 op port = iri.port;
145 f48e3b85 2021-05-24 op if (hostname == NULL)
146 f48e3b85 2021-05-24 op hostname = iri.host;
148 f48e3b85 2021-05-24 op if (tls_connect_servername(ctx, iri.host, port, hostname) == -1)
149 f48e3b85 2021-05-24 op errx(1, "tls_connect: %s", tls_error(ctx));
151 f48e3b85 2021-05-24 op for (handshake = 0; !handshake;) {
152 f48e3b85 2021-05-24 op switch (tls_handshake(ctx)) {
155 f48e3b85 2021-05-24 op handshake = 1;
161 f48e3b85 2021-05-24 op printf("%s", buf);
162 f48e3b85 2021-05-24 op if (tls_write(ctx, buf, strlen(buf)) == -1)
163 f48e3b85 2021-05-24 op errx(1, "tls_write: %s", tls_error(ctx));
166 f48e3b85 2021-05-24 op switch (len = tls_read(ctx, buf, sizeof(buf))) {
170 f48e3b85 2021-05-24 op case TLS_WANT_POLLIN:
171 f48e3b85 2021-05-24 op case TLS_WANT_POLLOUT:
177 f48e3b85 2021-05-24 op if ((t = strchr(buf, '\r')) != NULL)
179 f48e3b85 2021-05-24 op else if ((t = strchr(buf, '\n')) != NULL)
183 f48e3b85 2021-05-24 op len -= t - buf;
184 f48e3b85 2021-05-24 op write(1, t, len);
189 f48e3b85 2021-05-24 op write(1, buf, 2);
190 f48e3b85 2021-05-24 op write(1, "\n", 1);
195 f48e3b85 2021-05-24 op t = strchr(buf, '\r');
196 f48e3b85 2021-05-24 op if (t == NULL)
197 f48e3b85 2021-05-24 op t = strchr(buf, '\n');
198 f48e3b85 2021-05-24 op if (t == NULL)
199 f48e3b85 2021-05-24 op t = &buf[len];
200 f48e3b85 2021-05-24 op write(1, buf, t - buf);
201 f48e3b85 2021-05-24 op write(1, "\n", 1);
205 f48e3b85 2021-05-24 op write(1, buf, len);
209 f48e3b85 2021-05-24 op tls_close(ctx);
210 f48e3b85 2021-05-24 op tls_free(ctx);