2 d2b941f3 2021-01-28 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 44ee1bac 2021-01-27 op * Permission to use, copy, modify, and distribute this software for any
5 44ee1bac 2021-01-27 op * purpose with or without fee is hereby granted, provided that the above
6 44ee1bac 2021-01-27 op * copyright notice and this permission notice appear in all copies.
8 44ee1bac 2021-01-27 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 44ee1bac 2021-01-27 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 44ee1bac 2021-01-27 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 44ee1bac 2021-01-27 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 44ee1bac 2021-01-27 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 44ee1bac 2021-01-27 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 44ee1bac 2021-01-27 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 52418c8d 2021-02-12 op #include "gmid.h"
19 44ee1bac 2021-01-27 op #include <errno.h>
20 44ee1bac 2021-01-27 op #include <string.h>
22 4283d65f 2021-02-10 op #include <openssl/bn.h>
23 3abf91b0 2021-02-07 op #include <openssl/pem.h>
24 02be96c6 2021-02-09 op #include <openssl/x509_vfy.h>
25 02be96c6 2021-02-09 op #include <openssl/x509v3.h>
28 44ee1bac 2021-01-27 op starts_with(const char *str, const char *prefix)
32 44ee1bac 2021-01-27 op if (prefix == NULL)
35 44ee1bac 2021-01-27 op for (i = 0; prefix[i] != '\0'; ++i)
36 44ee1bac 2021-01-27 op if (str[i] != prefix[i])
42 44ee1bac 2021-01-27 op ends_with(const char *str, const char *sufx)
46 44ee1bac 2021-01-27 op i = strlen(str);
47 44ee1bac 2021-01-27 op j = strlen(sufx);
53 44ee1bac 2021-01-27 op for (j = 0; str[i] != '\0'; i++, j++)
54 44ee1bac 2021-01-27 op if (str[i] != sufx[j])
60 44ee1bac 2021-01-27 op filesize(int fd)
64 44ee1bac 2021-01-27 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
66 44ee1bac 2021-01-27 op if (lseek(fd, 0, SEEK_SET) == -1)
72 bcf5d929 2021-02-01 op absolutify_path(const char *path)
76 bcf5d929 2021-02-01 op if (*path == '/') {
77 bcf5d929 2021-02-01 op if ((r = strdup(path)) == NULL)
78 bcf5d929 2021-02-01 op err(1, "strdup");
82 bcf5d929 2021-02-01 op wd = getcwd(NULL, 0);
83 bcf5d929 2021-02-01 op if (asprintf(&r, "%s/%s", wd, path) == -1)
84 bcf5d929 2021-02-01 op err(1, "asprintf");
90 ca21e100 2021-02-04 op xstrdup(const char *s)
94 ca21e100 2021-02-04 op if ((d = strdup(s)) == NULL)
95 ca21e100 2021-02-04 op err(1, "strdup");
100 b8e64ccd 2021-03-31 op xcalloc(size_t nmemb, size_t size)
104 b8e64ccd 2021-03-31 op if ((d = calloc(nmemb, size)) == NULL)
105 b8e64ccd 2021-03-31 op err(1, "calloc");
110 52418c8d 2021-02-12 op gen_certificate(const char *hostname, const char *certpath, const char *keypath)
113 3abf91b0 2021-02-07 op EVP_PKEY *pkey;
116 3abf91b0 2021-02-07 op X509_NAME *name;
118 52418c8d 2021-02-12 op const unsigned char *host = (const unsigned char*)hostname;
120 3abf91b0 2021-02-07 op log_notice(NULL,
121 3abf91b0 2021-02-07 op "generating new certificate for %s (it could take a while)",
124 3abf91b0 2021-02-07 op if ((pkey = EVP_PKEY_new()) == NULL)
125 3abf91b0 2021-02-07 op fatal("couldn't create a new private key");
127 3abf91b0 2021-02-07 op if ((rsa = RSA_new()) == NULL)
128 4283d65f 2021-02-10 op fatal("couldn't generate rsa");
130 4283d65f 2021-02-10 op if ((e = BN_new()) == NULL)
131 4283d65f 2021-02-10 op fatal("couldn't allocate a bignum");
133 1eb3631d 2021-05-12 op BN_set_word(e, RSA_F4);
134 4283d65f 2021-02-10 op if (!RSA_generate_key_ex(rsa, 4096, e, NULL))
135 3abf91b0 2021-02-07 op fatal("couldn't generate a rsa key");
137 3abf91b0 2021-02-07 op if (!EVP_PKEY_assign_RSA(pkey, rsa))
138 3abf91b0 2021-02-07 op fatal("couldn't assign the key");
140 3abf91b0 2021-02-07 op if ((x509 = X509_new()) == NULL)
141 3abf91b0 2021-02-07 op fatal("couldn't generate the X509 certificate");
143 1eb3631d 2021-05-12 op ASN1_INTEGER_set(X509_get_serialNumber(x509), 0);
144 3abf91b0 2021-02-07 op X509_gmtime_adj(X509_get_notBefore(x509), 0);
145 3abf91b0 2021-02-07 op X509_gmtime_adj(X509_get_notAfter(x509), 315360000L); /* 10 years */
146 1eb3631d 2021-05-12 op X509_set_version(x509, 3);
148 3abf91b0 2021-02-07 op if (!X509_set_pubkey(x509, pkey))
149 3abf91b0 2021-02-07 op fatal("couldn't set the public key");
151 3abf91b0 2021-02-07 op name = X509_get_subject_name(x509);
152 3abf91b0 2021-02-07 op if (!X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, host, -1, -1, 0))
153 3abf91b0 2021-02-07 op fatal("couldn't add CN to cert");
154 3abf91b0 2021-02-07 op X509_set_issuer_name(x509, name);
156 3abf91b0 2021-02-07 op if (!X509_sign(x509, pkey, EVP_sha256()))
157 3abf91b0 2021-02-07 op fatal("couldn't sign the certificate");
159 3abf91b0 2021-02-07 op if ((f = fopen(keypath, "w")) == NULL)
160 3abf91b0 2021-02-07 op fatal("fopen(%s): %s", keypath, strerror(errno));
161 3abf91b0 2021-02-07 op if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL))
162 3abf91b0 2021-02-07 op fatal("couldn't write private key");
165 3abf91b0 2021-02-07 op if ((f = fopen(certpath, "w")) == NULL)
166 3abf91b0 2021-02-07 op fatal("fopen(%s): %s", certpath, strerror(errno));
167 3abf91b0 2021-02-07 op if (!PEM_write_X509(f, x509))
168 3abf91b0 2021-02-07 op fatal("couldn't write cert");
172 3abf91b0 2021-02-07 op X509_free(x509);
173 3abf91b0 2021-02-07 op RSA_free(rsa);
177 02be96c6 2021-02-09 op load_ca(const char *path)
179 02be96c6 2021-02-09 op FILE *f = NULL;
180 02be96c6 2021-02-09 op X509 *x = NULL;
181 02be96c6 2021-02-09 op X509_STORE *store;
183 02be96c6 2021-02-09 op if ((store = X509_STORE_new()) == NULL)
186 02be96c6 2021-02-09 op if ((f = fopen(path, "r")) == NULL)
189 02be96c6 2021-02-09 op if ((x = PEM_read_X509(f, NULL, NULL, NULL)) == NULL)
192 02be96c6 2021-02-09 op if (X509_check_ca(x) == 0)
195 02be96c6 2021-02-09 op if (!X509_STORE_add_cert(store, x))
198 02be96c6 2021-02-09 op X509_free(x);
200 02be96c6 2021-02-09 op return store;
203 02be96c6 2021-02-09 op X509_STORE_free(store);
204 02be96c6 2021-02-09 op if (x != NULL)
205 02be96c6 2021-02-09 op X509_free(x);
206 02be96c6 2021-02-09 op if (f != NULL)
212 02be96c6 2021-02-09 op validate_against_ca(X509_STORE *ca, const uint8_t *chain, size_t len)
214 02be96c6 2021-02-09 op X509 *client;
216 02be96c6 2021-02-09 op X509_STORE_CTX *ctx = NULL;
219 02be96c6 2021-02-09 op if ((m = BIO_new_mem_buf(chain, len)) == NULL)
222 02be96c6 2021-02-09 op if ((client = PEM_read_bio_X509(m, NULL, NULL, NULL)) == NULL)
225 02be96c6 2021-02-09 op if ((ctx = X509_STORE_CTX_new()) == NULL)
228 02be96c6 2021-02-09 op if (!X509_STORE_CTX_init(ctx, ca, client, NULL))
231 02be96c6 2021-02-09 op ret = X509_verify_cert(ctx);
235 02be96c6 2021-02-09 op if (client != NULL)
236 02be96c6 2021-02-09 op X509_free(client);
237 02be96c6 2021-02-09 op if (ctx != NULL)
238 02be96c6 2021-02-09 op X509_STORE_CTX_free(ctx);
243 bc99d868 2021-03-19 op dispatch_imsg(struct imsgbuf *ibuf, imsg_handlerfn **handlers, size_t size)
245 bc99d868 2021-03-19 op struct imsg imsg;
246 bc99d868 2021-03-19 op size_t datalen, i;
249 bc99d868 2021-03-19 op if ((n = imsg_read(ibuf)) == -1) {
250 bc99d868 2021-03-19 op if (errno == EAGAIN || errno == EWOULDBLOCK)
259 bc99d868 2021-03-19 op if ((n = imsg_get(ibuf, &imsg)) == -1)
263 bc99d868 2021-03-19 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
264 bc99d868 2021-03-19 op i = imsg.hdr.type;
265 bc99d868 2021-03-19 op if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL)
267 bc99d868 2021-03-19 op handlers[i](ibuf, &imsg, datalen);
268 bc99d868 2021-03-19 op imsg_free(&imsg);