Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include <errno.h>
18 #include <string.h>
20 #include <openssl/bn.h>
21 #include <openssl/pem.h>
22 #include <openssl/x509_vfy.h>
23 #include <openssl/x509v3.h>
25 #include "gmid.h"
27 static sigset_t set;
29 void
30 block_signals(void)
31 {
32 sigset_t new;
34 sigemptyset(&new);
35 sigaddset(&new, SIGHUP);
36 sigprocmask(SIG_BLOCK, &new, &set);
37 }
39 void
40 unblock_signals(void)
41 {
42 sigprocmask(SIG_SETMASK, &set, NULL);
43 }
45 int
46 starts_with(const char *str, const char *prefix)
47 {
48 size_t i;
50 if (prefix == NULL)
51 return 0;
53 for (i = 0; prefix[i] != '\0'; ++i)
54 if (str[i] != prefix[i])
55 return 0;
56 return 1;
57 }
59 int
60 ends_with(const char *str, const char *sufx)
61 {
62 size_t i, j;
64 i = strlen(str);
65 j = strlen(sufx);
67 if (j > i)
68 return 0;
70 i -= j;
71 for (j = 0; str[i] != '\0'; i++, j++)
72 if (str[i] != sufx[j])
73 return 0;
74 return 1;
75 }
77 ssize_t
78 filesize(int fd)
79 {
80 ssize_t len;
82 if ((len = lseek(fd, 0, SEEK_END)) == -1)
83 return -1;
84 if (lseek(fd, 0, SEEK_SET) == -1)
85 return -1;
86 return len;
87 }
89 char *
90 absolutify_path(const char *path)
91 {
92 char *wd, *r;
94 if (*path == '/') {
95 if ((r = strdup(path)) == NULL)
96 err(1, "strdup");
97 return r;
98 }
100 wd = getcwd(NULL, 0);
101 if (asprintf(&r, "%s/%s", wd, path) == -1)
102 err(1, "asprintf");
103 free(wd);
104 return r;
107 char *
108 xstrdup(const char *s)
110 char *d;
112 if ((d = strdup(s)) == NULL)
113 err(1, "strdup");
114 return d;
117 void
118 gen_certificate(const char *host, const char *certpath, const char *keypath)
120 BIGNUM *e;
121 EVP_PKEY *pkey;
122 RSA *rsa;
123 X509 *x509;
124 X509_NAME *name;
125 FILE *f;
126 const char *org = "gmid";
128 log_notice(NULL,
129 "generating new certificate for %s (it could take a while)",
130 host);
132 if ((pkey = EVP_PKEY_new()) == NULL)
133 fatal("couldn't create a new private key");
135 if ((rsa = RSA_new()) == NULL)
136 fatal("couldn't generate rsa");
138 if ((e = BN_new()) == NULL)
139 fatal("couldn't allocate a bignum");
141 BN_set_word(e, 17);
142 if (!RSA_generate_key_ex(rsa, 4096, e, NULL))
143 fatal("couldn't generate a rsa key");
145 if (!EVP_PKEY_assign_RSA(pkey, rsa))
146 fatal("couldn't assign the key");
148 if ((x509 = X509_new()) == NULL)
149 fatal("couldn't generate the X509 certificate");
151 ASN1_INTEGER_set(X509_get_serialNumber(x509), 1);
152 X509_gmtime_adj(X509_get_notBefore(x509), 0);
153 X509_gmtime_adj(X509_get_notAfter(x509), 315360000L); /* 10 years */
155 if (!X509_set_pubkey(x509, pkey))
156 fatal("couldn't set the public key");
158 name = X509_get_subject_name(x509);
159 if (!X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, org, -1, -1, 0))
160 fatal("couldn't add N to cert");
161 if (!X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, host, -1, -1, 0))
162 fatal("couldn't add CN to cert");
163 X509_set_issuer_name(x509, name);
165 if (!X509_sign(x509, pkey, EVP_sha256()))
166 fatal("couldn't sign the certificate");
168 if ((f = fopen(keypath, "w")) == NULL)
169 fatal("fopen(%s): %s", keypath, strerror(errno));
170 if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL))
171 fatal("couldn't write private key");
172 fclose(f);
174 if ((f = fopen(certpath, "w")) == NULL)
175 fatal("fopen(%s): %s", certpath, strerror(errno));
176 if (!PEM_write_X509(f, x509))
177 fatal("couldn't write cert");
178 fclose(f);
180 BN_free(e);
181 X509_free(x509);
182 RSA_free(rsa);
185 X509_STORE *
186 load_ca(const char *path)
188 FILE *f = NULL;
189 X509 *x = NULL;
190 X509_STORE *store;
192 if ((store = X509_STORE_new()) == NULL)
193 return NULL;
195 if ((f = fopen(path, "r")) == NULL)
196 goto err;
198 if ((x = PEM_read_X509(f, NULL, NULL, NULL)) == NULL)
199 goto err;
201 if (X509_check_ca(x) == 0)
202 goto err;
204 if (!X509_STORE_add_cert(store, x))
205 goto err;
207 X509_free(x);
208 fclose(f);
209 return store;
211 err:
212 X509_STORE_free(store);
213 if (x != NULL)
214 X509_free(x);
215 if (f != NULL)
216 fclose(f);
217 return NULL;
220 int
221 validate_against_ca(X509_STORE *ca, const uint8_t *chain, size_t len)
223 X509 *client;
224 BIO *m;
225 X509_STORE_CTX *ctx = NULL;
226 int ret = 0;
228 if ((m = BIO_new_mem_buf(chain, len)) == NULL)
229 return 0;
231 if ((client = PEM_read_bio_X509(m, NULL, NULL, NULL)) == NULL)
232 goto end;
234 if ((ctx = X509_STORE_CTX_new()) == NULL)
235 goto end;
237 if (!X509_STORE_CTX_init(ctx, ca, client, NULL))
238 goto end;
240 ret = X509_verify_cert(ctx);
241 fprintf(stderr, "openssl x509_verify_cert: %d\n", ret);
243 end:
244 BIO_free(m);
245 if (client != NULL)
246 X509_free(client);
247 if (ctx != NULL)
248 X509_STORE_CTX_free(ctx);
249 return ret;