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 44ee1bac 2021-01-27 op #include <errno.h>
18 44ee1bac 2021-01-27 op #include <string.h>
20 4283d65f 2021-02-10 op #include <openssl/bn.h>
21 3abf91b0 2021-02-07 op #include <openssl/pem.h>
22 02be96c6 2021-02-09 op #include <openssl/x509_vfy.h>
23 02be96c6 2021-02-09 op #include <openssl/x509v3.h>
25 44ee1bac 2021-01-27 op #include "gmid.h"
27 ca21e100 2021-02-04 op static sigset_t set;
30 ca21e100 2021-02-04 op block_signals(void)
34 ca21e100 2021-02-04 op sigemptyset(&new);
35 ca21e100 2021-02-04 op sigaddset(&new, SIGHUP);
36 ca21e100 2021-02-04 op sigprocmask(SIG_BLOCK, &new, &set);
40 ca21e100 2021-02-04 op unblock_signals(void)
42 ca21e100 2021-02-04 op sigprocmask(SIG_SETMASK, &set, NULL);
46 44ee1bac 2021-01-27 op starts_with(const char *str, const char *prefix)
50 44ee1bac 2021-01-27 op if (prefix == NULL)
53 44ee1bac 2021-01-27 op for (i = 0; prefix[i] != '\0'; ++i)
54 44ee1bac 2021-01-27 op if (str[i] != prefix[i])
60 44ee1bac 2021-01-27 op ends_with(const char *str, const char *sufx)
64 44ee1bac 2021-01-27 op i = strlen(str);
65 44ee1bac 2021-01-27 op j = strlen(sufx);
71 44ee1bac 2021-01-27 op for (j = 0; str[i] != '\0'; i++, j++)
72 44ee1bac 2021-01-27 op if (str[i] != sufx[j])
78 44ee1bac 2021-01-27 op filesize(int fd)
82 44ee1bac 2021-01-27 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
84 44ee1bac 2021-01-27 op if (lseek(fd, 0, SEEK_SET) == -1)
90 bcf5d929 2021-02-01 op absolutify_path(const char *path)
94 bcf5d929 2021-02-01 op if (*path == '/') {
95 bcf5d929 2021-02-01 op if ((r = strdup(path)) == NULL)
96 bcf5d929 2021-02-01 op err(1, "strdup");
100 bcf5d929 2021-02-01 op wd = getcwd(NULL, 0);
101 bcf5d929 2021-02-01 op if (asprintf(&r, "%s/%s", wd, path) == -1)
102 bcf5d929 2021-02-01 op err(1, "asprintf");
108 ca21e100 2021-02-04 op xstrdup(const char *s)
112 ca21e100 2021-02-04 op if ((d = strdup(s)) == NULL)
113 ca21e100 2021-02-04 op err(1, "strdup");
118 3abf91b0 2021-02-07 op gen_certificate(const char *host, const char *certpath, const char *keypath)
121 3abf91b0 2021-02-07 op EVP_PKEY *pkey;
124 3abf91b0 2021-02-07 op X509_NAME *name;
126 3abf91b0 2021-02-07 op const char *org = "gmid";
128 3abf91b0 2021-02-07 op log_notice(NULL,
129 3abf91b0 2021-02-07 op "generating new certificate for %s (it could take a while)",
132 3abf91b0 2021-02-07 op if ((pkey = EVP_PKEY_new()) == NULL)
133 3abf91b0 2021-02-07 op fatal("couldn't create a new private key");
135 3abf91b0 2021-02-07 op if ((rsa = RSA_new()) == NULL)
136 4283d65f 2021-02-10 op fatal("couldn't generate rsa");
138 4283d65f 2021-02-10 op if ((e = BN_new()) == NULL)
139 4283d65f 2021-02-10 op fatal("couldn't allocate a bignum");
141 4283d65f 2021-02-10 op BN_set_word(e, 17);
142 4283d65f 2021-02-10 op if (!RSA_generate_key_ex(rsa, 4096, e, NULL))
143 3abf91b0 2021-02-07 op fatal("couldn't generate a rsa key");
145 3abf91b0 2021-02-07 op if (!EVP_PKEY_assign_RSA(pkey, rsa))
146 3abf91b0 2021-02-07 op fatal("couldn't assign the key");
148 3abf91b0 2021-02-07 op if ((x509 = X509_new()) == NULL)
149 3abf91b0 2021-02-07 op fatal("couldn't generate the X509 certificate");
151 3abf91b0 2021-02-07 op ASN1_INTEGER_set(X509_get_serialNumber(x509), 1);
152 3abf91b0 2021-02-07 op X509_gmtime_adj(X509_get_notBefore(x509), 0);
153 3abf91b0 2021-02-07 op X509_gmtime_adj(X509_get_notAfter(x509), 315360000L); /* 10 years */
155 3abf91b0 2021-02-07 op if (!X509_set_pubkey(x509, pkey))
156 3abf91b0 2021-02-07 op fatal("couldn't set the public key");
158 3abf91b0 2021-02-07 op name = X509_get_subject_name(x509);
159 3abf91b0 2021-02-07 op if (!X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, org, -1, -1, 0))
160 3abf91b0 2021-02-07 op fatal("couldn't add N to cert");
161 3abf91b0 2021-02-07 op if (!X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, host, -1, -1, 0))
162 3abf91b0 2021-02-07 op fatal("couldn't add CN to cert");
163 3abf91b0 2021-02-07 op X509_set_issuer_name(x509, name);
165 3abf91b0 2021-02-07 op if (!X509_sign(x509, pkey, EVP_sha256()))
166 3abf91b0 2021-02-07 op fatal("couldn't sign the certificate");
168 3abf91b0 2021-02-07 op if ((f = fopen(keypath, "w")) == NULL)
169 3abf91b0 2021-02-07 op fatal("fopen(%s): %s", keypath, strerror(errno));
170 3abf91b0 2021-02-07 op if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL))
171 3abf91b0 2021-02-07 op fatal("couldn't write private key");
174 3abf91b0 2021-02-07 op if ((f = fopen(certpath, "w")) == NULL)
175 3abf91b0 2021-02-07 op fatal("fopen(%s): %s", certpath, strerror(errno));
176 3abf91b0 2021-02-07 op if (!PEM_write_X509(f, x509))
177 3abf91b0 2021-02-07 op fatal("couldn't write cert");
181 3abf91b0 2021-02-07 op X509_free(x509);
182 3abf91b0 2021-02-07 op RSA_free(rsa);
186 02be96c6 2021-02-09 op load_ca(const char *path)
188 02be96c6 2021-02-09 op FILE *f = NULL;
189 02be96c6 2021-02-09 op X509 *x = NULL;
190 02be96c6 2021-02-09 op X509_STORE *store;
192 02be96c6 2021-02-09 op if ((store = X509_STORE_new()) == NULL)
195 02be96c6 2021-02-09 op if ((f = fopen(path, "r")) == NULL)
198 02be96c6 2021-02-09 op if ((x = PEM_read_X509(f, NULL, NULL, NULL)) == NULL)
201 02be96c6 2021-02-09 op if (X509_check_ca(x) == 0)
204 02be96c6 2021-02-09 op if (!X509_STORE_add_cert(store, x))
207 02be96c6 2021-02-09 op X509_free(x);
209 02be96c6 2021-02-09 op return store;
212 02be96c6 2021-02-09 op X509_STORE_free(store);
213 02be96c6 2021-02-09 op if (x != NULL)
214 02be96c6 2021-02-09 op X509_free(x);
215 02be96c6 2021-02-09 op if (f != NULL)
221 02be96c6 2021-02-09 op validate_against_ca(X509_STORE *ca, const uint8_t *chain, size_t len)
223 02be96c6 2021-02-09 op X509 *client;
225 02be96c6 2021-02-09 op X509_STORE_CTX *ctx = NULL;
228 02be96c6 2021-02-09 op if ((m = BIO_new_mem_buf(chain, len)) == NULL)
231 02be96c6 2021-02-09 op if ((client = PEM_read_bio_X509(m, NULL, NULL, NULL)) == NULL)
234 02be96c6 2021-02-09 op if ((ctx = X509_STORE_CTX_new()) == NULL)
237 02be96c6 2021-02-09 op if (!X509_STORE_CTX_init(ctx, ca, client, NULL))
240 02be96c6 2021-02-09 op ret = X509_verify_cert(ctx);
241 02be96c6 2021-02-09 op fprintf(stderr, "openssl x509_verify_cert: %d\n", ret);
245 02be96c6 2021-02-09 op if (client != NULL)
246 02be96c6 2021-02-09 op X509_free(client);
247 02be96c6 2021-02-09 op if (ctx != NULL)
248 02be96c6 2021-02-09 op X509_STORE_CTX_free(ctx);