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>
27 eae52ad4 2023-06-06 op #include "log.h"
30 44ee1bac 2021-01-27 op starts_with(const char *str, const char *prefix)
34 44ee1bac 2021-01-27 op if (prefix == NULL)
37 44ee1bac 2021-01-27 op for (i = 0; prefix[i] != '\0'; ++i)
38 44ee1bac 2021-01-27 op if (str[i] != prefix[i])
44 44ee1bac 2021-01-27 op ends_with(const char *str, const char *sufx)
48 44ee1bac 2021-01-27 op i = strlen(str);
49 44ee1bac 2021-01-27 op j = strlen(sufx);
55 44ee1bac 2021-01-27 op for (j = 0; str[i] != '\0'; i++, j++)
56 44ee1bac 2021-01-27 op if (str[i] != sufx[j])
62 44ee1bac 2021-01-27 op filesize(int fd)
66 44ee1bac 2021-01-27 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
68 44ee1bac 2021-01-27 op if (lseek(fd, 0, SEEK_SET) == -1)
74 bcf5d929 2021-02-01 op absolutify_path(const char *path)
78 bcf5d929 2021-02-01 op if (*path == '/') {
79 bcf5d929 2021-02-01 op if ((r = strdup(path)) == NULL)
80 bcf5d929 2021-02-01 op err(1, "strdup");
84 bcf5d929 2021-02-01 op wd = getcwd(NULL, 0);
85 bcf5d929 2021-02-01 op if (asprintf(&r, "%s/%s", wd, path) == -1)
86 bcf5d929 2021-02-01 op err(1, "asprintf");
92 ca21e100 2021-02-04 op xstrdup(const char *s)
96 ca21e100 2021-02-04 op if ((d = strdup(s)) == NULL)
97 ca21e100 2021-02-04 op err(1, "strdup");
102 b8e64ccd 2021-03-31 op xcalloc(size_t nmemb, size_t size)
106 b8e64ccd 2021-03-31 op if ((d = calloc(nmemb, size)) == NULL)
107 b8e64ccd 2021-03-31 op err(1, "calloc");
112 52418c8d 2021-02-12 op gen_certificate(const char *hostname, const char *certpath, const char *keypath)
115 3abf91b0 2021-02-07 op EVP_PKEY *pkey;
118 3abf91b0 2021-02-07 op X509_NAME *name;
120 52418c8d 2021-02-12 op const unsigned char *host = (const unsigned char*)hostname;
122 eae52ad4 2023-06-06 op log_info("generating new certificate for %s (it could take a while)",
125 3abf91b0 2021-02-07 op if ((pkey = EVP_PKEY_new()) == NULL)
126 df5058c9 2023-06-05 op fatalx("couldn't create a new private key");
128 3abf91b0 2021-02-07 op if ((rsa = RSA_new()) == NULL)
129 df5058c9 2023-06-05 op fatalx("couldn't generate rsa");
131 4283d65f 2021-02-10 op if ((e = BN_new()) == NULL)
132 df5058c9 2023-06-05 op fatalx("couldn't allocate a bignum");
134 1eb3631d 2021-05-12 op BN_set_word(e, RSA_F4);
135 4283d65f 2021-02-10 op if (!RSA_generate_key_ex(rsa, 4096, e, NULL))
136 df5058c9 2023-06-05 op fatalx("couldn't generate a rsa key");
138 3abf91b0 2021-02-07 op if (!EVP_PKEY_assign_RSA(pkey, rsa))
139 df5058c9 2023-06-05 op fatalx("couldn't assign the key");
141 3abf91b0 2021-02-07 op if ((x509 = X509_new()) == NULL)
142 df5058c9 2023-06-05 op fatalx("couldn't generate the X509 certificate");
144 1eb3631d 2021-05-12 op ASN1_INTEGER_set(X509_get_serialNumber(x509), 0);
145 3abf91b0 2021-02-07 op X509_gmtime_adj(X509_get_notBefore(x509), 0);
146 3abf91b0 2021-02-07 op X509_gmtime_adj(X509_get_notAfter(x509), 315360000L); /* 10 years */
147 1eb3631d 2021-05-12 op X509_set_version(x509, 3);
149 3abf91b0 2021-02-07 op if (!X509_set_pubkey(x509, pkey))
150 df5058c9 2023-06-05 op fatalx("couldn't set the public key");
152 3abf91b0 2021-02-07 op name = X509_get_subject_name(x509);
153 3abf91b0 2021-02-07 op if (!X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, host, -1, -1, 0))
154 df5058c9 2023-06-05 op fatalx("couldn't add CN to cert");
155 3abf91b0 2021-02-07 op X509_set_issuer_name(x509, name);
157 3abf91b0 2021-02-07 op if (!X509_sign(x509, pkey, EVP_sha256()))
158 df5058c9 2023-06-05 op fatalx("couldn't sign the certificate");
160 3abf91b0 2021-02-07 op if ((f = fopen(keypath, "w")) == NULL)
161 df5058c9 2023-06-05 op fatal("can't open %s", keypath);
162 3abf91b0 2021-02-07 op if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL))
163 df5058c9 2023-06-05 op fatalx("couldn't write private key");
166 3abf91b0 2021-02-07 op if ((f = fopen(certpath, "w")) == NULL)
167 df5058c9 2023-06-05 op fatal("can't open %s", certpath);
168 3abf91b0 2021-02-07 op if (!PEM_write_X509(f, x509))
169 df5058c9 2023-06-05 op fatalx("couldn't write cert");
173 3abf91b0 2021-02-07 op X509_free(x509);
174 3abf91b0 2021-02-07 op RSA_free(rsa);
176 eae52ad4 2023-06-06 op log_info("certificate successfully generated");
180 02be96c6 2021-02-09 op load_ca(const char *path)
182 02be96c6 2021-02-09 op FILE *f = NULL;
183 02be96c6 2021-02-09 op X509 *x = NULL;
184 02be96c6 2021-02-09 op X509_STORE *store;
186 02be96c6 2021-02-09 op if ((store = X509_STORE_new()) == NULL)
189 02be96c6 2021-02-09 op if ((f = fopen(path, "r")) == NULL)
192 02be96c6 2021-02-09 op if ((x = PEM_read_X509(f, NULL, NULL, NULL)) == NULL)
195 02be96c6 2021-02-09 op if (X509_check_ca(x) == 0)
198 02be96c6 2021-02-09 op if (!X509_STORE_add_cert(store, x))
201 02be96c6 2021-02-09 op X509_free(x);
203 02be96c6 2021-02-09 op return store;
206 02be96c6 2021-02-09 op X509_STORE_free(store);
207 02be96c6 2021-02-09 op if (x != NULL)
208 02be96c6 2021-02-09 op X509_free(x);
209 02be96c6 2021-02-09 op if (f != NULL)
215 02be96c6 2021-02-09 op validate_against_ca(X509_STORE *ca, const uint8_t *chain, size_t len)
217 02be96c6 2021-02-09 op X509 *client;
219 02be96c6 2021-02-09 op X509_STORE_CTX *ctx = NULL;
222 02be96c6 2021-02-09 op if ((m = BIO_new_mem_buf(chain, len)) == NULL)
225 02be96c6 2021-02-09 op if ((client = PEM_read_bio_X509(m, NULL, NULL, NULL)) == NULL)
228 02be96c6 2021-02-09 op if ((ctx = X509_STORE_CTX_new()) == NULL)
231 02be96c6 2021-02-09 op if (!X509_STORE_CTX_init(ctx, ca, client, NULL))
234 02be96c6 2021-02-09 op ret = X509_verify_cert(ctx);
238 02be96c6 2021-02-09 op if (client != NULL)
239 02be96c6 2021-02-09 op X509_free(client);
240 02be96c6 2021-02-09 op if (ctx != NULL)
241 02be96c6 2021-02-09 op X509_STORE_CTX_free(ctx);
246 bc99d868 2021-03-19 op dispatch_imsg(struct imsgbuf *ibuf, imsg_handlerfn **handlers, size_t size)
248 bc99d868 2021-03-19 op struct imsg imsg;
249 bc99d868 2021-03-19 op size_t datalen, i;
252 bc99d868 2021-03-19 op if ((n = imsg_read(ibuf)) == -1) {
253 bc99d868 2021-03-19 op if (errno == EAGAIN || errno == EWOULDBLOCK)
262 bc99d868 2021-03-19 op if ((n = imsg_get(ibuf, &imsg)) == -1)
266 bc99d868 2021-03-19 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
267 bc99d868 2021-03-19 op i = imsg.hdr.type;
268 bc99d868 2021-03-19 op if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL)
270 bc99d868 2021-03-19 op handlers[i](ibuf, &imsg, datalen);
271 bc99d868 2021-03-19 op imsg_free(&imsg);