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 3abf91b0 2021-02-07 op #include <openssl/pem.h>
21 3abf91b0 2021-02-07 op #include <openssl/x509.h>
23 44ee1bac 2021-01-27 op #include "gmid.h"
25 ca21e100 2021-02-04 op static sigset_t set;
28 ca21e100 2021-02-04 op block_signals(void)
32 ca21e100 2021-02-04 op sigemptyset(&new);
33 ca21e100 2021-02-04 op sigaddset(&new, SIGHUP);
34 ca21e100 2021-02-04 op sigprocmask(SIG_BLOCK, &new, &set);
38 ca21e100 2021-02-04 op unblock_signals(void)
40 ca21e100 2021-02-04 op sigprocmask(SIG_SETMASK, &set, NULL);
44 44ee1bac 2021-01-27 op starts_with(const char *str, const char *prefix)
48 44ee1bac 2021-01-27 op if (prefix == NULL)
51 44ee1bac 2021-01-27 op for (i = 0; prefix[i] != '\0'; ++i)
52 44ee1bac 2021-01-27 op if (str[i] != prefix[i])
58 44ee1bac 2021-01-27 op ends_with(const char *str, const char *sufx)
62 44ee1bac 2021-01-27 op i = strlen(str);
63 44ee1bac 2021-01-27 op j = strlen(sufx);
69 44ee1bac 2021-01-27 op for (j = 0; str[i] != '\0'; i++, j++)
70 44ee1bac 2021-01-27 op if (str[i] != sufx[j])
76 44ee1bac 2021-01-27 op filesize(int fd)
80 44ee1bac 2021-01-27 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
82 44ee1bac 2021-01-27 op if (lseek(fd, 0, SEEK_SET) == -1)
88 bcf5d929 2021-02-01 op absolutify_path(const char *path)
92 bcf5d929 2021-02-01 op if (*path == '/') {
93 bcf5d929 2021-02-01 op if ((r = strdup(path)) == NULL)
94 bcf5d929 2021-02-01 op err(1, "strdup");
98 bcf5d929 2021-02-01 op wd = getcwd(NULL, 0);
99 bcf5d929 2021-02-01 op if (asprintf(&r, "%s/%s", wd, path) == -1)
100 bcf5d929 2021-02-01 op err(1, "asprintf");
106 ca21e100 2021-02-04 op xstrdup(const char *s)
110 ca21e100 2021-02-04 op if ((d = strdup(s)) == NULL)
111 ca21e100 2021-02-04 op err(1, "strdup");
116 3abf91b0 2021-02-07 op gen_certificate(const char *host, const char *certpath, const char *keypath)
119 3abf91b0 2021-02-07 op EVP_PKEY *pkey;
122 3abf91b0 2021-02-07 op X509_NAME *name;
124 3abf91b0 2021-02-07 op const char *org = "gmid";
126 3abf91b0 2021-02-07 op log_notice(NULL,
127 3abf91b0 2021-02-07 op "generating new certificate for %s (it could take a while)",
130 3abf91b0 2021-02-07 op if ((pkey = EVP_PKEY_new()) == NULL)
131 3abf91b0 2021-02-07 op fatal("couldn't create a new private key");
133 3abf91b0 2021-02-07 op if ((rsa = RSA_new()) == NULL)
134 3abf91b0 2021-02-07 op fatal("could'nt generate rsa");
137 3abf91b0 2021-02-07 op BN_set_word(&e, 17);
138 3abf91b0 2021-02-07 op if (!RSA_generate_key_ex(rsa, 4096, &e, NULL))
139 3abf91b0 2021-02-07 op fatal("couldn't generate a rsa key");
141 3abf91b0 2021-02-07 op if (!EVP_PKEY_assign_RSA(pkey, rsa))
142 3abf91b0 2021-02-07 op fatal("couldn't assign the key");
144 3abf91b0 2021-02-07 op if ((x509 = X509_new()) == NULL)
145 3abf91b0 2021-02-07 op fatal("couldn't generate the X509 certificate");
147 3abf91b0 2021-02-07 op ASN1_INTEGER_set(X509_get_serialNumber(x509), 1);
148 3abf91b0 2021-02-07 op X509_gmtime_adj(X509_get_notBefore(x509), 0);
149 3abf91b0 2021-02-07 op X509_gmtime_adj(X509_get_notAfter(x509), 315360000L); /* 10 years */
151 3abf91b0 2021-02-07 op if (!X509_set_pubkey(x509, pkey))
152 3abf91b0 2021-02-07 op fatal("couldn't set the public key");
154 3abf91b0 2021-02-07 op name = X509_get_subject_name(x509);
155 3abf91b0 2021-02-07 op if (!X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, org, -1, -1, 0))
156 3abf91b0 2021-02-07 op fatal("couldn't add N to cert");
157 3abf91b0 2021-02-07 op if (!X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, host, -1, -1, 0))
158 3abf91b0 2021-02-07 op fatal("couldn't add CN to cert");
159 3abf91b0 2021-02-07 op X509_set_issuer_name(x509, name);
161 3abf91b0 2021-02-07 op if (!X509_sign(x509, pkey, EVP_sha256()))
162 3abf91b0 2021-02-07 op fatal("couldn't sign the certificate");
164 3abf91b0 2021-02-07 op if ((f = fopen(keypath, "w")) == NULL)
165 3abf91b0 2021-02-07 op fatal("fopen(%s): %s", keypath, strerror(errno));
166 3abf91b0 2021-02-07 op if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL))
167 3abf91b0 2021-02-07 op fatal("couldn't write private key");
170 3abf91b0 2021-02-07 op if ((f = fopen(certpath, "w")) == NULL)
171 3abf91b0 2021-02-07 op fatal("fopen(%s): %s", certpath, strerror(errno));
172 3abf91b0 2021-02-07 op if (!PEM_write_X509(f, x509))
173 3abf91b0 2021-02-07 op fatal("couldn't write cert");
176 3abf91b0 2021-02-07 op X509_free(x509);
177 3abf91b0 2021-02-07 op RSA_free(rsa);