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.
22 #include <openssl/bn.h>
23 #include <openssl/pem.h>
24 #include <openssl/x509_vfy.h>
25 #include <openssl/x509v3.h>
28 starts_with(const char *str, const char *prefix)
35 for (i = 0; prefix[i] != '\0'; ++i)
36 if (str[i] != prefix[i])
42 ends_with(const char *str, const char *sufx)
53 for (j = 0; str[i] != '\0'; i++, j++)
54 if (str[i] != sufx[j])
64 if ((len = lseek(fd, 0, SEEK_END)) == -1)
66 if (lseek(fd, 0, SEEK_SET) == -1)
72 absolutify_path(const char *path)
77 if ((r = strdup(path)) == NULL)
83 if (asprintf(&r, "%s/%s", wd, path) == -1)
90 xstrdup(const char *s)
94 if ((d = strdup(s)) == NULL)
100 xcalloc(size_t nmemb, size_t size)
104 if ((d = calloc(nmemb, size)) == NULL)
110 gen_certificate(const char *hostname, const char *certpath, const char *keypath)
118 const unsigned char *host = (const unsigned char*)hostname;
121 "generating new certificate for %s (it could take a while)",
124 if ((pkey = EVP_PKEY_new()) == NULL)
125 fatal("couldn't create a new private key");
127 if ((rsa = RSA_new()) == NULL)
128 fatal("couldn't generate rsa");
130 if ((e = BN_new()) == NULL)
131 fatal("couldn't allocate a bignum");
133 BN_set_word(e, RSA_F4);
134 if (!RSA_generate_key_ex(rsa, 4096, e, NULL))
135 fatal("couldn't generate a rsa key");
137 if (!EVP_PKEY_assign_RSA(pkey, rsa))
138 fatal("couldn't assign the key");
140 if ((x509 = X509_new()) == NULL)
141 fatal("couldn't generate the X509 certificate");
143 ASN1_INTEGER_set(X509_get_serialNumber(x509), 0);
144 X509_gmtime_adj(X509_get_notBefore(x509), 0);
145 X509_gmtime_adj(X509_get_notAfter(x509), 315360000L); /* 10 years */
146 X509_set_version(x509, 3);
148 if (!X509_set_pubkey(x509, pkey))
149 fatal("couldn't set the public key");
151 name = X509_get_subject_name(x509);
152 if (!X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, host, -1, -1, 0))
153 fatal("couldn't add CN to cert");
154 X509_set_issuer_name(x509, name);
156 if (!X509_sign(x509, pkey, EVP_sha256()))
157 fatal("couldn't sign the certificate");
159 if ((f = fopen(keypath, "w")) == NULL)
160 fatal("fopen(%s): %s", keypath, strerror(errno));
161 if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL))
162 fatal("couldn't write private key");
165 if ((f = fopen(certpath, "w")) == NULL)
166 fatal("fopen(%s): %s", certpath, strerror(errno));
167 if (!PEM_write_X509(f, x509))
168 fatal("couldn't write cert");
177 load_ca(const char *path)
183 if ((store = X509_STORE_new()) == NULL)
186 if ((f = fopen(path, "r")) == NULL)
189 if ((x = PEM_read_X509(f, NULL, NULL, NULL)) == NULL)
192 if (X509_check_ca(x) == 0)
195 if (!X509_STORE_add_cert(store, x))
203 X509_STORE_free(store);
212 validate_against_ca(X509_STORE *ca, const uint8_t *chain, size_t len)
216 X509_STORE_CTX *ctx = NULL;
219 if ((m = BIO_new_mem_buf(chain, len)) == NULL)
222 if ((client = PEM_read_bio_X509(m, NULL, NULL, NULL)) == NULL)
225 if ((ctx = X509_STORE_CTX_new()) == NULL)
228 if (!X509_STORE_CTX_init(ctx, ca, client, NULL))
231 ret = X509_verify_cert(ctx);
238 X509_STORE_CTX_free(ctx);
243 dispatch_imsg(struct imsgbuf *ibuf, imsg_handlerfn **handlers, size_t size)
249 if ((n = imsg_read(ibuf)) == -1) {
250 if (errno == EAGAIN || errno == EWOULDBLOCK)
259 if ((n = imsg_get(ibuf, &imsg)) == -1)
263 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
265 if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL)
267 handlers[i](ibuf, &imsg, datalen);