| 1 |
/* |
| 2 |
* Copyright (c) 2017-2022 Joris Vink <[email protected]> |
| 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 |
*/ |
| 16 |
|
| 17 |
/* |
| 18 |
* The kore keymgr process is responsible for managing certificates |
| 19 |
* and their matching private keys. |
| 20 |
* |
| 21 |
* It is the only process in Kore that holds the private keys (the workers |
| 22 |
* do not have a copy of them in memory). |
| 23 |
* |
| 24 |
* When a worker requires the private key for signing it will send a message |
| 25 |
* to the keymgr with the to-be-signed data (KORE_MSG_KEYMGR_REQ). The keymgr |
| 26 |
* will perform the signing and respond with a KORE_MSG_KEYMGR_RESP message. |
| 27 |
* |
| 28 |
* The keymgr can transparently reload the private keys and certificates |
| 29 |
* for a configured domain when it receives a SIGUSR1. It it reloads them |
| 30 |
* it will send the newly loaded certificate chains to the worker processes |
| 31 |
* which will update their TLS contexts accordingly. |
| 32 |
* |
| 33 |
* If ACME is turned on the keymgr will also hold all account and domain |
| 34 |
* keys and will initiate the process of acquiring new certificates against |
| 35 |
* the ACME provider that is configured if those certificates do not exist |
| 36 |
* or are expired (or are expiring soon). |
| 37 |
*/ |
| 38 |
|
| 39 |
#include <sys/types.h> |
| 40 |
#include <sys/mman.h> |
| 41 |
#include <sys/stat.h> |
| 42 |
|
| 43 |
#include <openssl/err.h> |
| 44 |
#include <openssl/evp.h> |
| 45 |
#include <openssl/rsa.h> |
| 46 |
#include <openssl/rand.h> |
| 47 |
#include <openssl/pem.h> |
| 48 |
#include <openssl/sha.h> |
| 49 |
#include <openssl/x509.h> |
| 50 |
#include <openssl/x509v3.h> |
| 51 |
|
| 52 |
#include <ctype.h> |
| 53 |
#include <fcntl.h> |
| 54 |
#include <stdio.h> |
| 55 |
#include <stdlib.h> |
| 56 |
#include <stdint.h> |
| 57 |
#include <signal.h> |
| 58 |
#include <unistd.h> |
| 59 |
|
| 60 |
#include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/ |
| 61 |
|
| 62 |
#if defined(KORE_USE_ACME) |
| 63 |
#include "acme.h" |
| 64 |
#endif |
| 65 |
|
| •66 |
/* |
| 67 |
* Disable deprecated declaration warnings if we're building against |
| 68 |
* OpenSSL 3 as they marked all low-level APIs as deprecated. |
| 69 |
* |
| 70 |
* Work is being done to replace these, but for now let things build. |
| 71 |
*/ |
| 72 |
#if defined(OPENSSL_VERSION_MAJORcxx . . $ `<external>/4166512202731279593/opensslv.h:31:10`!) && OPENSSL_VERSION_MAJORcxx . . $ `<external>/4166512202731279593/opensslv.h:31:10`! >= 3 |
| 73 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 74 |
#endif |
| 75 |
|
| 76 |
#define RAND_TMP_FILEcxx . . $ `src/keymgr_openssl.c:76:9`! "rnd.tmp" |
| 77 |
#define RAND_POLL_INTERVALcxx . . $ `src/keymgr_openssl.c:77:9`! (1800 * 1000) |
| 78 |
#define RAND_FILE_SIZEcxx . . $ `src/keymgr_openssl.c:78:9`! 1024 |
| 79 |
|
| 80 |
#if defined(__linux__cxx . . $ `<built-in>:401:9`!) |
| 81 |
#include "seccomp.h"cxx . . $ `<file>/include/kore/seccomp.h`/ |
| 82 |
|
| 83 |
/* The syscalls our keymgr is allowed to perform, only. */ |
| 84 |
static struct sock_filtercxx . . $ sock_filter# filter_keymgrcxx . . $ filter_keymgr.[] = { |
| 85 |
/* Deny these, but with EACCESS instead of dying. */ |
| 86 |
KORE_SYSCALL_DENYcxx . . $ `include/kore/seccomp.h:92:9`!(ioctl, EACCEScxx . . $ `<external>/8987895939074593727/errno-base.h:17:9`!), |
| 87 |
|
| 88 |
/* Required to deal with private keys and certs. */ |
| 89 |
#if defined(SYS_opencxx . . $ `<external>/16530160117609833761/syscall.h:1167:10`!) |
| 90 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(open), |
| 91 |
#endif |
| 92 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(read), |
| 93 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(lseek), |
| 94 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(write), |
| 95 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(close), |
| 96 |
#if defined(SYS_statcxx . . $ `<external>/16530160117609833761/syscall.h:2311:10`!) |
| 97 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(stat), |
| 98 |
#endif |
| 99 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(fstat), |
| 100 |
#if defined(SYS_fstat64) |
| 101 |
KORE_SYSCALL_ALLOW(fstat64), |
| 102 |
#endif |
| 103 |
#if defined(SYS_newfstatatcxx . . $ `<external>/16530160117609833761/syscall.h:1119:10`!) |
| 104 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(newfstatat), |
| 105 |
#endif |
| 106 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(futex), |
| 107 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(writev), |
| 108 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(openat), |
| 109 |
#if defined(SYS_accesscxx . . $ `<external>/16530160117609833761/syscall.h:43:10`!) |
| 110 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(access), |
| 111 |
#endif |
| 112 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(faccessat), |
| 113 |
|
| 114 |
/* Net related. */ |
| 115 |
#if defined(SYS_pollcxx . . $ `<external>/16530160117609833761/syscall.h:1703:10`!) |
| 116 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(poll), |
| 117 |
#endif |
| 118 |
#if defined(SYS_send) |
| 119 |
KORE_SYSCALL_ALLOW(send), |
| 120 |
#endif |
| 121 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(sendto), |
| 122 |
#if defined(SYS_recv) |
| 123 |
KORE_SYSCALL_ALLOW(recv), |
| 124 |
#endif |
| 125 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(recvfrom), |
| 126 |
#if defined(SYS_epoll_waitcxx . . $ `<external>/16530160117609833761/syscall.h:303:10`!) |
| 127 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(epoll_wait), |
| 128 |
#endif |
| 129 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(epoll_pwait), |
| 130 |
|
| 131 |
/* Process things. */ |
| 132 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(exit), |
| 133 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(kill), |
| 134 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(getuid), |
| 135 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(getpid), |
| 136 |
#if defined(SYS_arch_prctlcxx . . $ `<external>/16530160117609833761/syscall.h:91:10`!) |
| 137 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(arch_prctl), |
| 138 |
#endif |
| 139 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(exit_group), |
| 140 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(sigaltstack), |
| 141 |
#if defined(SYS_sigreturn) |
| 142 |
KORE_SYSCALL_ALLOW(sigreturn), |
| 143 |
#endif |
| 144 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(rt_sigreturn), |
| 145 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(rt_sigaction), |
| 146 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(rt_sigprocmask), |
| 147 |
|
| 148 |
/* Other things. */ |
| 149 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(brk), |
| 150 |
#if defined(SYS_mmapcxx . . $ `<external>/16530160117609833761/syscall.h:999:10`!) |
| 151 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(mmap), |
| 152 |
#endif |
| 153 |
#if defined(SYS_mmap2) |
| 154 |
KORE_SYSCALL_ALLOW(mmap2), |
| 155 |
#endif |
| 156 |
#if defined(SYS_madvisecxx . . $ `<external>/16530160117609833761/syscall.h:935:10`!) |
| 157 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(madvise), |
| 158 |
#endif |
| 159 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(munmap), |
| 160 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(clock_gettime), |
| 161 |
#if defined(__NR_getrandomcxx . . $ `<external>/1941951872834623984/unistd_64.h:322:9`!) |
| 162 |
KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(getrandom), |
| 163 |
#endif |
| 164 |
|
| 165 |
#if defined(KORE_USE_ACME) |
| 166 |
#if defined(SYS_mkdir) |
| 167 |
KORE_SYSCALL_ALLOW(mkdir), |
| 168 |
#endif |
| 169 |
KORE_SYSCALL_ALLOW(mkdirat), |
| 170 |
KORE_SYSCALL_ALLOW(umask), |
| 171 |
#endif |
| 172 |
}; |
| 173 |
#endif |
| 174 |
|
| 175 |
struct keycxx . . $ key# { |
| 176 |
KORE_PRIVATE_KEYcxx . . $ KORE_PRIVATE_KEY# *pkeycxx . . $ key#pkey.; |
| 177 |
struct kore_domaincxx . . $ kore_domain# *domcxx . . $ key#dom.; |
| 178 |
TAILQ_ENTRYcxx . . $ `<external>/2546723208511035544/queue.h:382:9`!(key) listcxx . . $ key#list.; |
| 179 |
}; |
| 180 |
|
| •181 |
/* Helper for weird API designs (looking at you OpenSSL). */ |
| 182 |
union deconstcxx . . $ deconst# { |
| 183 |
void *pcxx . . $ deconst#p.; |
| 184 |
const void *cpcxx . . $ deconst#cp.; |
| 185 |
}; |
| 186 |
|
| 187 |
#if defined(KORE_USE_ACME) |
| 188 |
|
| 189 |
#define ACME_ORDER_STATE_INIT 1 |
| 190 |
#define ACME_ORDER_STATE_SUBMIT 2 |
| 191 |
|
| 192 |
#define ACME_X509_EXPIRATION 120 |
| 193 |
#define ACME_TLS_ALPN_01_OID "1.3.6.1.5.5.7.1.31" |
| 194 |
|
| 195 |
#define ACME_RENEWAL_THRESHOLD 5 |
| 196 |
#define ACME_RENEWAL_TIMER (3600 * 1000) |
| 197 |
|
| 198 |
/* UTCTIME in format of YYMMDDHHMMSSZ */ |
| 199 |
#define ASN1_UTCTIME_LEN 13 |
| 200 |
|
| 201 |
/* GENERALIZEDTIME in format of YYYYMMDDHHMMSSZ */ |
| 202 |
#define ASN1_GENERALIZEDTIME_LEN 15 |
| 203 |
|
| 204 |
/* Set to 1 when we receive KORE_ACME_PROC_READY. */ |
| 205 |
static int acmeproc_ready = 0; |
| 206 |
|
| 207 |
/* Renewal timer for all domains under acme control. */ |
| 208 |
static struct kore_timer *acme_renewal = NULL; |
| 209 |
|
| 210 |
/* oid for acme extension. */ |
| 211 |
static int acme_oid = -1; |
| 212 |
|
| 213 |
struct acme_order { |
| 214 |
int state; |
| 215 |
struct kore_timer *timer; |
| 216 |
char *domain; |
| 217 |
}; |
| 218 |
|
| 219 |
static char *keymgr_bignum_base64(const BIGNUM *); |
| 220 |
|
| 221 |
static void keymgr_acme_init(void); |
| 222 |
static void keymgr_acme_renewal(void *, u_int64_t); |
| 223 |
static void keymgr_acme_check(struct kore_domain *); |
| 224 |
static void keymgr_acme_sign(struct kore_msg *, const void *); |
| 225 |
static void keymgr_acme_ready(struct kore_msg *, const void *); |
| 226 |
static void keymgr_acme_domainkey(struct kore_domain *, struct key *); |
| 227 |
|
| 228 |
static void keymgr_acme_order_create(const char *); |
| 229 |
static void keymgr_acme_order_redo(void *, u_int64_t); |
| 230 |
static void keymgr_acme_order_start(void *, u_int64_t); |
| 231 |
|
| 232 |
static void keymgr_x509_ext(STACK_OF(X509_EXTENSION) *, |
| 233 |
int, const char *, ...) |
| 234 |
__attribute__((format (printf, 3, 4))); |
| 235 |
|
| 236 |
static void keymgr_acme_csr(const struct kore_keyreq *, struct key *); |
| 237 |
static void keymgr_acme_install_cert(const void *, size_t, struct key *); |
| 238 |
static void keymgr_acme_order_failed(const void *, size_t, struct key *); |
| 239 |
static void keymgr_acme_challenge_cert(const void *, size_t, struct key *); |
| 240 |
|
| 241 |
static int keymgr_x509_not_after(X509 *, time_t *); |
| 242 |
static int keymgr_asn1_convert_utctime(const ASN1_TIME *, time_t *); |
| 243 |
static int keymgr_asn1_convert_generalizedtime(const void *, |
| 244 |
size_t, time_t *); |
| 245 |
|
| 246 |
#endif /* KORE_USE_ACME */ |
| 247 |
|
| 248 |
static void keymgr_reloadcxx . . $ keymgr_reload(49f6e7a06ebc5aa8).(void); |
| 249 |
static void keymgr_load_randfilecxx . . $ keymgr_load_randfile(49f6e7a06ebc5aa8).(void); |
| 250 |
static void keymgr_save_randfilecxx . . $ keymgr_save_randfile(49f6e7a06ebc5aa8).(void); |
| 251 |
|
| 252 |
static struct keycxx . . $ key# *keymgr_load_privatekeycxx . . $ keymgr_load_privatekey(d3115bdf78b41f34).(const char *); |
| 253 |
static void keymgr_load_domain_privatekeycxx . . $ keymgr_load_domain_privatekey(be08e46f67814b96).(struct kore_domaincxx . . $ kore_domain# *); |
| 254 |
|
| 255 |
static void keymgr_msg_recvcxx . . $ keymgr_msg_recv(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *, const void *); |
| 256 |
static void keymgr_entropy_requestcxx . . $ keymgr_entropy_request(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *, const void *); |
| 257 |
static void keymgr_certificate_requestcxx . . $ keymgr_certificate_request(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *, const void *); |
| 258 |
static void keymgr_submit_certificatescxx . . $ keymgr_submit_certificates(6e434a51a561f2a1).(struct kore_domaincxx . . $ kore_domain# *, u_int16_tcxx . . $ u_int16_t#); |
| 259 |
static void keymgr_submit_filecxx . . $ keymgr_submit_file(16bcc9de4a3f0de8).(u_int8_tcxx . . $ u_int8_t#, struct kore_domaincxx . . $ kore_domain# *, |
| 260 |
const char *, u_int16_tcxx . . $ u_int16_t#, int); |
| 261 |
static void keymgr_x509_msgcxx . . $ keymgr_x509_msg(c543e85c8d3c9e32).(const char *, const void *, size_t, int, int); |
| 262 |
|
| 263 |
static void keymgr_rsa_encryptcxx . . $ keymgr_rsa_encrypt(f98bb2a91effe600).(struct kore_msgcxx . . $ kore_msg# *, const void *, |
| 264 |
struct keycxx . . $ key# *); |
| 265 |
|
| 266 |
#if defined(__OpenBSD__) |
| 267 |
#if defined(KORE_USE_ACME) |
| 268 |
static const char *keymgr_pledges = "stdio rpath wpath cpath"; |
| 269 |
#else |
| 270 |
static const char *keymgr_pledges = "stdio rpath"; |
| 271 |
#endif |
| 272 |
#endif |
| 273 |
|
| •274 |
static TAILQ_HEADcxx . . $ `<external>/2546723208511035544/queue.h:372:9`!(, key) keyscxx . . $ keys.; |
| 275 |
static int initializedcxx . . $ initialized. = 0; |
| 276 |
|
| 277 |
char *kore_rand_filecxx . . $ kore_rand_file. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 278 |
|
| 279 |
void |
| 280 |
kore_keymgr_runcxx . . $ kore_keymgr_run(49f6e7a06ebc5aa8).(void) |
| 281 |
{ |
| 282 |
int quit; |
| 283 |
u_int64_tcxx . . $ u_int64_t# now, netwait, last_seed; |
| 284 |
|
| 285 |
if (kore_keymgr_activecxx . . $ kore_keymgr_active. == 0) |
| 286 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("%s: called with kore_keymgr_active == 0", __func__); |
| 287 |
|
| 288 |
quit = 0; |
| 289 |
|
| 290 |
kore_server_closeallcxx . . $ kore_server_closeall(49f6e7a06ebc5aa8).(); |
| 291 |
kore_module_cleanupcxx . . $ kore_module_cleanup(49f6e7a06ebc5aa8).(); |
| 292 |
|
| 293 |
net_initcxx . . $ net_init(49f6e7a06ebc5aa8).(); |
| 294 |
kore_timer_initcxx . . $ kore_timer_init(49f6e7a06ebc5aa8).(); |
| 295 |
kore_connection_initcxx . . $ kore_connection_init(49f6e7a06ebc5aa8).(); |
| 296 |
kore_platform_event_initcxx . . $ kore_platform_event_init(49f6e7a06ebc5aa8).(); |
| 297 |
|
| 298 |
kore_msg_worker_initcxx . . $ kore_msg_worker_init(49f6e7a06ebc5aa8).(); |
| 299 |
kore_msg_registercxx . . $ kore_msg_register(96a8e3ca44e422f9).(KORE_MSG_KEYMGR_REQcxx . . $ `include/kore/kore.h:661:9`!, keymgr_msg_recvcxx . . $ keymgr_msg_recv(72fdcea79115518d).); |
| 300 |
kore_msg_registercxx . . $ kore_msg_register(96a8e3ca44e422f9).(KORE_MSG_ENTROPY_REQcxx . . $ `include/kore/kore.h:664:9`!, keymgr_entropy_requestcxx . . $ keymgr_entropy_request(72fdcea79115518d).); |
| 301 |
kore_msg_registercxx . . $ kore_msg_register(96a8e3ca44e422f9).(KORE_MSG_CERTIFICATE_REQcxx . . $ `include/kore/kore.h:667:9`!, keymgr_certificate_requestcxx . . $ keymgr_certificate_request(72fdcea79115518d).); |
| 302 |
|
| 303 |
#if defined(__linux__cxx . . $ `<built-in>:401:9`!) |
| 304 |
/* Drop all enabled seccomp filters, and add only ours. */ |
| 305 |
kore_seccomp_dropcxx . . $ kore_seccomp_drop(49f6e7a06ebc5aa8).(); |
| 306 |
kore_seccomp_filtercxx . . $ kore_seccomp_filter(60a8714db3c5c2a).("keymgr", filter_keymgrcxx . . $ filter_keymgr., |
| 307 |
KORE_FILTER_LENcxx . . $ `include/kore/seccomp.h:137:9`!(filter_keymgr)); |
| 308 |
#endif |
| 309 |
#if defined(KORE_USE_PYTHON) |
| 310 |
kore_msg_unregister(KORE_PYTHON_SEND_OBJ); |
| 311 |
#endif |
| 312 |
kore_worker_privsepcxx . . $ kore_worker_privsep(49f6e7a06ebc5aa8).(); |
| 313 |
|
| 314 |
if (kore_rand_filecxx . . $ kore_rand_file. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 315 |
keymgr_load_randfilecxx . . $ keymgr_load_randfile(49f6e7a06ebc5aa8).(); |
| 316 |
keymgr_save_randfilecxx . . $ keymgr_save_randfile(49f6e7a06ebc5aa8).(); |
| 317 |
} else if (!kore_quietcxx . . $ kore_quiet.) { |
| 318 |
kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "no rand_file location specified"); |
| 319 |
} |
| 320 |
|
| 321 |
RAND_pollcxx . . $ RAND_poll(b126dc7c1de90089).(); |
| 322 |
last_seed = 0; |
| 323 |
|
| 324 |
initializedcxx . . $ initialized. = 1; |
| 325 |
keymgr_reloadcxx . . $ keymgr_reload(49f6e7a06ebc5aa8).(); |
| 326 |
|
| 327 |
#if defined(__OpenBSD__) |
| 328 |
if (pledge(keymgr_pledges, NULL) == -1) |
| 329 |
fatalx("failed to pledge keymgr process"); |
| 330 |
#endif |
| 331 |
|
| 332 |
#if defined(KORE_USE_ACME) |
| 333 |
acme_oid = OBJ_create(ACME_TLS_ALPN_01_OID, "acme", "acmeIdentifier"); |
| 334 |
X509V3_EXT_add_alias(acme_oid, NID_subject_key_identifier); |
| 335 |
#endif |
| 336 |
|
| 337 |
kore_worker_startedcxx . . $ kore_worker_started(49f6e7a06ebc5aa8).(); |
| 338 |
|
| 339 |
while (quit != 1) { |
| 340 |
now = kore_time_mscxx . . $ kore_time_ms(f708dce9a8856c70).(); |
| 341 |
if ((now - last_seed) > RAND_POLL_INTERVALcxx . . $ `src/keymgr_openssl.c:77:9`!) { |
| 342 |
RAND_pollcxx . . $ RAND_poll(b126dc7c1de90089).(); |
| 343 |
last_seed = now; |
| 344 |
} |
| 345 |
|
| 346 |
netwait = kore_timer_next_runcxx . . $ kore_timer_next_run(ecdaa1632f49f591).(now); |
| 347 |
kore_platform_event_waitcxx . . $ kore_platform_event_wait(2c44cd906ae4e829).(netwait); |
| 348 |
|
| 349 |
if (sig_recvcxx . . $ sig_recv. != 0) { |
| 350 |
switch (sig_recvcxx . . $ sig_recv.) { |
| 351 |
case SIGQUITcxx . . $ `<external>/11773118241360591791/signum-generic.h:57:9`!: |
| 352 |
case SIGINTcxx . . $ `<external>/11773118241360591791/signum-generic.h:48:9`!: |
| 353 |
case SIGTERMcxx . . $ `<external>/11773118241360591791/signum-generic.h:53:9`!: |
| 354 |
quit = 1; |
| 355 |
break; |
| 356 |
case SIGUSR1cxx . . $ `<external>/1457673850468744829/signum-arch.h:49:9`!: |
| 357 |
keymgr_reloadcxx . . $ keymgr_reload(49f6e7a06ebc5aa8).(); |
| 358 |
break; |
| 359 |
default: |
| 360 |
break; |
| 361 |
} |
| 362 |
sig_recvcxx . . $ sig_recv. = 0; |
| 363 |
} |
| 364 |
|
| 365 |
if (quit) |
| 366 |
break; |
| 367 |
|
| 368 |
now = kore_time_mscxx . . $ kore_time_ms(f708dce9a8856c70).(); |
| 369 |
kore_timer_runcxx . . $ kore_timer_run(2c44cd906ae4e829).(now); |
| 370 |
kore_connection_prunecxx . . $ kore_connection_prune(d4f767463ce0a6b3).(KORE_CONNECTION_PRUNE_DISCONNECTcxx . . $ `include/kore/kore.h:214:9`!); |
| 371 |
} |
| 372 |
|
| 373 |
kore_keymgr_cleanupcxx . . $ kore_keymgr_cleanup(d4f767463ce0a6b3).(1); |
| 374 |
kore_platform_event_cleanupcxx . . $ kore_platform_event_cleanup(49f6e7a06ebc5aa8).(); |
| 375 |
kore_connection_cleanupcxx . . $ kore_connection_cleanup(49f6e7a06ebc5aa8).(); |
| 376 |
net_cleanupcxx . . $ net_cleanup(49f6e7a06ebc5aa8).(); |
| 377 |
} |
| 378 |
|
| 379 |
void |
| 380 |
kore_keymgr_cleanupcxx . . $ kore_keymgr_cleanup(d4f767463ce0a6b3).(int final) |
| 381 |
{ |
| 382 |
struct keycxx . . $ key# *key, *next; |
| 383 |
|
| 384 |
if (initializedcxx . . $ initialized. == 0) |
| 385 |
return; |
| 386 |
|
| 387 |
for (key = TAILQ_FIRSTcxx . . $ `<external>/2546723208511035544/queue.h:458:9`!(&keys); key != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; key = next) { |
| 388 |
next = TAILQ_NEXTcxx . . $ `<external>/2546723208511035544/queue.h:459:9`!(key, list); |
| 389 |
TAILQ_REMOVEcxx . . $ `<external>/2546723208511035544/queue.h:426:9`!(&keys, key, list); |
| 390 |
|
| 391 |
EVP_PKEY_freecxx . . $ EVP_PKEY_free(a36cc1e96a19ea9b).(key->pkeycxx . . $ key#pkey.); |
| 392 |
kore_freecxx . . $ kore_free(5d22bdacc48458e8).(key); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
static void |
| 397 |
keymgr_reloadcxx . . $ keymgr_reload(49f6e7a06ebc5aa8).(void) |
| 398 |
{ |
| 399 |
struct kore_servercxx . . $ kore_server# *srv; |
| 400 |
struct kore_domaincxx . . $ kore_domain# *dom; |
| 401 |
|
| 402 |
if (!kore_quietcxx . . $ kore_quiet.) |
| 403 |
kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "(re)loading certificates, keys and CRLs"); |
| 404 |
|
| 405 |
kore_keymgr_cleanupcxx . . $ kore_keymgr_cleanup(d4f767463ce0a6b3).(0); |
| 406 |
TAILQ_INITcxx . . $ `<external>/2546723208511035544/queue.h:387:9`!(&keys); |
| 407 |
|
| 408 |
#if defined(KORE_USE_ACME) |
| 409 |
keymgr_acme_init(); |
| 410 |
#endif |
| 411 |
|
| 412 |
kore_domain_callbackcxx . . $ kore_domain_callback(2a5b70316e33bad0).(keymgr_load_domain_privatekeycxx . . $ keymgr_load_domain_privatekey(be08e46f67814b96).); |
| 413 |
|
| 414 |
/* can't use kore_domain_callback() due to dst parameter. */ |
| 415 |
LIST_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:134:9`!(srv, &kore_servers, list) { |
| 416 |
if (srv->tlscxx . . $ kore_server#tls. == 0) |
| 417 |
continue; |
| 418 |
TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(dom, &srv->domains, list) |
| 419 |
keymgr_submit_certificatescxx . . $ keymgr_submit_certificates(6e434a51a561f2a1).(dom, KORE_MSG_WORKER_ALLcxx . . $ `include/kore/kore.h:680:9`!); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
static void |
| 424 |
keymgr_submit_certificatescxx . . $ keymgr_submit_certificates(6e434a51a561f2a1).(struct kore_domaincxx . . $ kore_domain# *dom, u_int16_tcxx . . $ u_int16_t# dst) |
| 425 |
{ |
| 426 |
if (accesscxx . . $ access(cb7559238bb26814).(dom->certfilecxx . . $ kore_domain#certfile., R_OKcxx . . $ `<external>/12628407106812493757/unistd.h:281:9`!) == -1) { |
| 427 |
#if defined(KORE_USE_ACME) |
| 428 |
if (dom->acme && errno == ENOENT) |
| 429 |
return; |
| 430 |
#endif |
| 431 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("cannot read '%s' for %s: %s", |
| 432 |
dom->certfilecxx . . $ kore_domain#certfile., dom->domaincxx . . $ kore_domain#domain., errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 433 |
} |
| 434 |
|
| 435 |
keymgr_submit_filecxx . . $ keymgr_submit_file(16bcc9de4a3f0de8).(KORE_MSG_CERTIFICATEcxx . . $ `include/kore/kore.h:666:9`!, dom, dom->certfilecxx . . $ kore_domain#certfile., dst, 0); |
| 436 |
|
| 437 |
if (dom->crlfilecxx . . $ kore_domain#crlfile. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 438 |
keymgr_submit_filecxx . . $ keymgr_submit_file(16bcc9de4a3f0de8).(KORE_MSG_CRLcxx . . $ `include/kore/kore.h:668:9`!, dom, dom->crlfilecxx . . $ kore_domain#crlfile., dst, 1); |
| 439 |
} |
| 440 |
|
| 441 |
static void |
| 442 |
keymgr_submit_filecxx . . $ keymgr_submit_file(16bcc9de4a3f0de8).(u_int8_tcxx . . $ u_int8_t# id, struct kore_domaincxx . . $ kore_domain# *dom, |
| 443 |
const char *file, u_int16_tcxx . . $ u_int16_t# dst, int can_fail) |
| 444 |
{ |
| 445 |
int fd; |
| 446 |
struct statcxx . . $ stat# st; |
| 447 |
u_int8_tcxx . . $ u_int8_t# *payload; |
| 448 |
|
| 449 |
if ((fd = opencxx . . $ open(cb7559238bb26814).(file, O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!)) == -1) { |
| 450 |
if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`! && can_fail) |
| 451 |
return; |
| 452 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("open(%s): %s", file, errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 453 |
} |
| 454 |
|
| 455 |
if (fstatcxx . . $ fstat(a43660ce2f600cb5).(fd, &st) == -1) |
| 456 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("stat(%s): %s", file, errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 457 |
|
| 458 |
if (!S_ISREGcxx . . $ `<external>/531885356569766532/stat.h:128:9`!(st.st_mode)) |
| 459 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("%s is not a file", file); |
| 460 |
|
| 461 |
payload = mmapcxx . . $ mmap(ff06c1c904cdf0e2).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, st.st_sizecxx . . $ stat#st_size., PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`!, MAP_SHAREDcxx . . $ `<external>/17226767460805440860/mman-linux.h:42:9`!, fd, 0); |
| 462 |
if (payload == MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`!) |
| 463 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("mmap(): %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 464 |
|
| 465 |
keymgr_x509_msgcxx . . $ keymgr_x509_msg(c543e85c8d3c9e32).(dom->domaincxx . . $ kore_domain#domain., payload, st.st_sizecxx . . $ stat#st_size., dst, id); |
| 466 |
|
| 467 |
(void)munmapcxx . . $ munmap(c0a5f96701d3f89).(payload, st.st_sizecxx . . $ stat#st_size.); |
| 468 |
closecxx . . $ close(7864480464b09eea).(fd); |
| 469 |
} |
| 470 |
|
| 471 |
static void |
| 472 |
keymgr_load_randfilecxx . . $ keymgr_load_randfile(49f6e7a06ebc5aa8).(void) |
| 473 |
{ |
| 474 |
int fd; |
| 475 |
struct statcxx . . $ stat# st; |
| 476 |
ssize_tcxx . . $ ssize_t# ret; |
| 477 |
size_t total; |
| 478 |
u_int8_tcxx . . $ u_int8_t# buf[RAND_FILE_SIZEcxx . . $ `src/keymgr_openssl.c:78:9`!]; |
| 479 |
|
| 480 |
if (kore_rand_filecxx . . $ kore_rand_file. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 481 |
return; |
| 482 |
|
| 483 |
if ((fd = opencxx . . $ open(cb7559238bb26814).(kore_rand_filecxx . . $ kore_rand_file., O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!)) == -1) |
| 484 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("open(%s): %s", kore_rand_filecxx . . $ kore_rand_file., errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 485 |
|
| 486 |
if (fstatcxx . . $ fstat(a43660ce2f600cb5).(fd, &st) == -1) |
| 487 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("stat(%s): %s", kore_rand_filecxx . . $ kore_rand_file., errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 488 |
if (!S_ISREGcxx . . $ `<external>/531885356569766532/stat.h:128:9`!(st.st_mode)) |
| 489 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("%s is not a file", kore_rand_filecxx . . $ kore_rand_file.); |
| 490 |
if (st.st_sizecxx . . $ stat#st_size. != RAND_FILE_SIZEcxx . . $ `src/keymgr_openssl.c:78:9`!) |
| 491 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("%s has an invalid size", kore_rand_filecxx . . $ kore_rand_file.); |
| 492 |
|
| 493 |
total = 0; |
| 494 |
|
| 495 |
while (total != RAND_FILE_SIZEcxx . . $ `src/keymgr_openssl.c:78:9`!) { |
| 496 |
ret = readcxx . . $ read(7e34d0a36f355389).(fd, buf, sizeof(buf)); |
| 497 |
if (ret == 0) |
| 498 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("EOF on %s", kore_rand_filecxx . . $ kore_rand_file.); |
| 499 |
|
| 500 |
if (ret == -1) { |
| 501 |
if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!) |
| 502 |
continue; |
| 503 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("read(%s): %s", kore_rand_filecxx . . $ kore_rand_file., errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 504 |
} |
| 505 |
|
| 506 |
total += (size_t)ret; |
| 507 |
RAND_seedcxx . . $ RAND_seed(a8b5d42151974564).(buf, (int)ret); |
| 508 |
OPENSSL_cleansecxx . . $ OPENSSL_cleanse(5500c4293b1bbef0).(buf, sizeof(buf)); |
| 509 |
} |
| 510 |
|
| 511 |
(void)closecxx . . $ close(7864480464b09eea).(fd); |
| 512 |
if (unlinkcxx . . $ unlink(5c4306d8553b3696).(kore_rand_filecxx . . $ kore_rand_file.) == -1) { |
| 513 |
kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "failed to unlink %s: %s", |
| 514 |
kore_rand_filecxx . . $ kore_rand_file., errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
static void |
| 519 |
keymgr_save_randfilecxx . . $ keymgr_save_randfile(49f6e7a06ebc5aa8).(void) |
| 520 |
{ |
| 521 |
int fd; |
| 522 |
struct statcxx . . $ stat# st; |
| 523 |
ssize_tcxx . . $ ssize_t# ret; |
| 524 |
u_int8_tcxx . . $ u_int8_t# buf[RAND_FILE_SIZEcxx . . $ `src/keymgr_openssl.c:78:9`!]; |
| 525 |
|
| 526 |
if (kore_rand_filecxx . . $ kore_rand_file. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 527 |
return; |
| 528 |
|
| 529 |
if (statcxx . . $ stat(a9cd1e23e96f423c).(RAND_TMP_FILEcxx . . $ `src/keymgr_openssl.c:76:9`!, &st) != -1) { |
| 530 |
kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "removing stale %s file", RAND_TMP_FILEcxx . . $ `src/keymgr_openssl.c:76:9`!); |
| 531 |
(void)unlinkcxx . . $ unlink(5c4306d8553b3696).(RAND_TMP_FILEcxx . . $ `src/keymgr_openssl.c:76:9`!); |
| 532 |
} |
| 533 |
|
| 534 |
if (RAND_bytescxx . . $ RAND_bytes(7405b416d0535413).(buf, sizeof(buf)) != 1) { |
| 535 |
kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "RAND_bytes: %s", ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!); |
| 536 |
goto cleanup; |
| 537 |
} |
| 538 |
|
| 539 |
if ((fd = opencxx . . $ open(ac5e327aedb29b06).(RAND_TMP_FILEcxx . . $ `src/keymgr_openssl.c:76:9`!, |
| 540 |
O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`! | O_TRUNCcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:56:10`! | O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`!, 0400)) == -1) { |
| 541 |
kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, |
| 542 |
"failed to open %s: %s - random data not written", |
| 543 |
RAND_TMP_FILEcxx . . $ `src/keymgr_openssl.c:76:9`!, errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 544 |
goto cleanup; |
| 545 |
} |
| 546 |
|
| 547 |
ret = writecxx . . $ write(41184a59d38b4f65).(fd, buf, sizeof(buf)); |
| 548 |
if (ret == -1 || (size_t)ret != sizeof(buf)) { |
| 549 |
kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "failed to write random data"); |
| 550 |
(void)closecxx . . $ close(7864480464b09eea).(fd); |
| 551 |
(void)unlinkcxx . . $ unlink(5c4306d8553b3696).(RAND_TMP_FILEcxx . . $ `src/keymgr_openssl.c:76:9`!); |
| 552 |
goto cleanup; |
| 553 |
} |
| 554 |
|
| 555 |
if (closecxx . . $ close(7864480464b09eea).(fd) == -1) |
| 556 |
kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "close(%s): %s", RAND_TMP_FILEcxx . . $ `src/keymgr_openssl.c:76:9`!, errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 557 |
|
| 558 |
if (renamecxx . . $ rename(5ae29d726ae57f88).(RAND_TMP_FILEcxx . . $ `src/keymgr_openssl.c:76:9`!, kore_rand_filecxx . . $ kore_rand_file.) == -1) { |
| 559 |
kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "rename(%s, %s): %s", |
| 560 |
RAND_TMP_FILEcxx . . $ `src/keymgr_openssl.c:76:9`!, kore_rand_filecxx . . $ kore_rand_file., errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 561 |
(void)unlinkcxx . . $ unlink(5c4306d8553b3696).(kore_rand_filecxx . . $ kore_rand_file.); |
| 562 |
(void)unlinkcxx . . $ unlink(5c4306d8553b3696).(RAND_TMP_FILEcxx . . $ `src/keymgr_openssl.c:76:9`!); |
| 563 |
} |
| 564 |
|
| 565 |
cleanup: |
| 566 |
OPENSSL_cleansecxx . . $ OPENSSL_cleanse(5500c4293b1bbef0).(buf, sizeof(buf)); |
| 567 |
} |
| 568 |
|
| 569 |
static void |
| 570 |
keymgr_load_domain_privatekeycxx . . $ keymgr_load_domain_privatekey(be08e46f67814b96).(struct kore_domaincxx . . $ kore_domain# *dom) |
| 571 |
{ |
| 572 |
struct keycxx . . $ key# *key; |
| 573 |
|
| 574 |
if (dom->servercxx . . $ kore_domain#server.->tlscxx . . $ kore_server#tls. == 0) |
| 575 |
return; |
| 576 |
|
| 577 |
key = keymgr_load_privatekeycxx . . $ keymgr_load_privatekey(d3115bdf78b41f34).(dom->certkeycxx . . $ kore_domain#certkey.); |
| 578 |
|
| 579 |
if (key->pkeycxx . . $ key#pkey. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 580 |
#if defined(KORE_USE_ACME) |
| 581 |
if (dom->acme) |
| 582 |
keymgr_acme_domainkey(dom, key); |
| 583 |
#endif |
| 584 |
if (key->pkeycxx . . $ key#pkey. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 585 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("failed to load private key for '%s' (%s)", |
| 586 |
dom->domaincxx . . $ kore_domain#domain., errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
key->domcxx . . $ key#dom. = dom; |
| 591 |
|
| 592 |
if (!kore_quietcxx . . $ kore_quiet.) |
| 593 |
kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "loaded private key for '%s'", dom->domaincxx . . $ kore_domain#domain.); |
| 594 |
} |
| 595 |
|
| 596 |
static struct keycxx . . $ key# * |
| 597 |
keymgr_load_privatekeycxx . . $ keymgr_load_privatekey(d3115bdf78b41f34).(const char *path) |
| 598 |
{ |
| 599 |
struct keycxx . . $ key# *key; |
| 600 |
|
| 601 |
key = kore_calloccxx . . $ kore_calloc(63a7bf2941bfcd7a).(1, sizeof(*key)); |
| 602 |
TAILQ_INSERT_TAILcxx . . $ `<external>/2546723208511035544/queue.h:402:9`!(&keys, key, list); |
| 603 |
|
| 604 |
/* Caller should check if pkey was loaded. */ |
| 605 |
if (path) |
| 606 |
key->pkeycxx . . $ key#pkey. = kore_tls_rsakey_loadcxx . . $ kore_tls_rsakey_load(49e461faa1428cff).(path); |
| 607 |
|
| 608 |
return (key); |
| 609 |
} |
| 610 |
|
| 611 |
static void |
| 612 |
keymgr_certificate_requestcxx . . $ keymgr_certificate_request(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *msg, const void *data) |
| 613 |
{ |
| 614 |
struct kore_servercxx . . $ kore_server# *srv; |
| 615 |
struct kore_domaincxx . . $ kore_domain# *dom; |
| 616 |
|
| 617 |
LIST_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:134:9`!(srv, &kore_servers, list) { |
| 618 |
if (srv->tlscxx . . $ kore_server#tls. == 0) |
| 619 |
continue; |
| 620 |
TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(dom, &srv->domains, list) |
| 621 |
keymgr_submit_certificatescxx . . $ keymgr_submit_certificates(6e434a51a561f2a1).(dom, msg->srccxx . . $ kore_msg#src.); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
static void |
| 626 |
keymgr_entropy_requestcxx . . $ keymgr_entropy_request(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *msg, const void *data) |
| 627 |
{ |
| 628 |
u_int8_tcxx . . $ u_int8_t# buf[RAND_FILE_SIZEcxx . . $ `src/keymgr_openssl.c:78:9`!]; |
| 629 |
|
| 630 |
if (RAND_bytescxx . . $ RAND_bytes(7405b416d0535413).(buf, sizeof(buf)) != 1) { |
| 631 |
kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, |
| 632 |
"failed to generate entropy for worker %u: %s", |
| 633 |
msg->srccxx . . $ kore_msg#src., ssl_errno_scxx . . $ `include/kore/kore.h:101:9`!); |
| 634 |
return; |
| 635 |
} |
| 636 |
|
| 637 |
/* No cleanse, this stuff is leaked in the kernel path anyway. */ |
| 638 |
kore_msg_sendcxx . . $ kore_msg_send(6afaa3cccbe95a50).(msg->srccxx . . $ kore_msg#src., KORE_MSG_ENTROPY_RESPcxx . . $ `include/kore/kore.h:665:9`!, buf, sizeof(buf)); |
| 639 |
} |
| 640 |
|
| 641 |
static void |
| 642 |
keymgr_msg_recvcxx . . $ keymgr_msg_recv(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *msg, const void *data) |
| 643 |
{ |
| 644 |
const struct kore_keyreqcxx . . $ kore_keyreq# *req; |
| 645 |
struct keycxx . . $ key# *key; |
| 646 |
|
| 647 |
if (msg->lengthcxx . . $ kore_msg#length. < sizeof(*req)) |
| 648 |
return; |
| 649 |
|
| 650 |
req = (const struct kore_keyreqcxx . . $ kore_keyreq# *)data; |
| 651 |
|
| 652 |
if (msg->lengthcxx . . $ kore_msg#length. != (sizeof(*req) + req->data_lencxx . . $ kore_keyreq#data_len.)) |
| 653 |
return; |
| 654 |
|
| 655 |
if (req->domaincxx . . $ kore_keyreq#domain.[KORE_DOMAINNAME_LENcxx . . $ `include/kore/kore.h:102:9`!] != '\0') |
| 656 |
return; |
| 657 |
|
| 658 |
key = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 659 |
TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(key, &keys, list) { |
| 660 |
if (key->domcxx . . $ key#dom. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 661 |
continue; |
| 662 |
if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(key->domcxx . . $ key#dom.->domaincxx . . $ kore_domain#domain., req->domaincxx . . $ kore_keyreq#domain.)) |
| 663 |
break; |
| 664 |
} |
| 665 |
|
| 666 |
if (key == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 667 |
return; |
| 668 |
|
| 669 |
switch (msg->idcxx . . $ kore_msg#id.) { |
| 670 |
case KORE_MSG_KEYMGR_REQcxx . . $ `include/kore/kore.h:661:9`!: |
| 671 |
switch (EVP_PKEY_idcxx . . $ `<external>/2497497914425334111/evp.h:1365:9`!(key->pkeycxx . . $ key#pkey.)) { |
| 672 |
case EVP_PKEY_RSAcxx . . $ `<external>/2497497914425334111/evp.h:63:9`!: |
| 673 |
keymgr_rsa_encryptcxx . . $ keymgr_rsa_encrypt(f98bb2a91effe600).(msg, data, key); |
| 674 |
break; |
| 675 |
default: |
| 676 |
break; |
| 677 |
} |
| 678 |
break; |
| 679 |
#if defined(KORE_USE_ACME) |
| 680 |
case KORE_ACME_CSR_REQUEST: |
| 681 |
keymgr_acme_csr(req, key); |
| 682 |
break; |
| 683 |
case KORE_ACME_ORDER_FAILED: |
| 684 |
keymgr_acme_order_failed(req->data, req->data_len, key); |
| 685 |
break; |
| 686 |
case KORE_ACME_CHALLENGE_CERT: |
| 687 |
keymgr_acme_challenge_cert(req->data, req->data_len, key); |
| 688 |
break; |
| 689 |
case KORE_ACME_INSTALL_CERT: |
| 690 |
keymgr_acme_install_cert(req->data, req->data_len, key); |
| 691 |
break; |
| 692 |
#endif |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
static void |
| 697 |
keymgr_rsa_encryptcxx . . $ keymgr_rsa_encrypt(f98bb2a91effe600).(struct kore_msgcxx . . $ kore_msg# *msg, const void *data, struct keycxx . . $ key# *key) |
| 698 |
{ |
| •699 |
union deconstcxx . . $ deconst# cp; |
| 700 |
int ret; |
| 701 |
RSAcxx . . $ RSA# *rsa; |
| 702 |
const struct kore_keyreqcxx . . $ kore_keyreq# *req; |
| 703 |
size_t keylen; |
| 704 |
u_int8_tcxx . . $ u_int8_t# buf[1024]; |
| 705 |
|
| 706 |
req = (const struct kore_keyreqcxx . . $ kore_keyreq# *)data; |
| •707 |
cp.cpcxx . . $ deconst#cp. = EVP_PKEY_get0_RSAcxx . . $ EVP_PKEY_get0_RSA(b6b77da49c324b2b).(key->pkeycxx . . $ key#pkey.); |
| 708 |
|
| 709 |
rsa = cp.pcxx . . $ deconst#p.; |
| 710 |
|
| 711 |
keylen = RSA_sizecxx . . $ RSA_size(e0c3c17b6cc9d160).(rsa); |
| 712 |
if (req->data_lencxx . . $ kore_keyreq#data_len. > keylen || keylen > sizeof(buf)) |
| 713 |
return; |
| 714 |
|
| 715 |
ret = RSA_private_encryptcxx . . $ RSA_private_encrypt(8dc5f0d62264ec93).(req->data_lencxx . . $ kore_keyreq#data_len., req->datacxx . . $ kore_keyreq#data., |
| 716 |
buf, rsa, req->paddingcxx . . $ kore_keyreq#padding.); |
| 717 |
if (ret != RSA_sizecxx . . $ RSA_size(e0c3c17b6cc9d160).(rsa)) |
| 718 |
return; |
| 719 |
|
| 720 |
kore_msg_sendcxx . . $ kore_msg_send(6afaa3cccbe95a50).(msg->srccxx . . $ kore_msg#src., KORE_MSG_KEYMGR_RESPcxx . . $ `include/kore/kore.h:662:9`!, buf, ret); |
| 721 |
} |
| 722 |
|
| 723 |
static void |
| 724 |
keymgr_x509_msgcxx . . $ keymgr_x509_msg(c543e85c8d3c9e32).(const char *domain, const void *data, size_t len, |
| 725 |
int target, int msg) |
| 726 |
{ |
| 727 |
struct kore_bufcxx . . $ kore_buf# buf; |
| 728 |
struct kore_x509_msgcxx . . $ kore_x509_msg# hdr; |
| 729 |
|
| 730 |
memsetcxx . . $ memset(d1de1e4fb215f1c6).(&hdr, 0, sizeof(hdr)); |
| 731 |
|
| 732 |
hdr.data_lencxx . . $ kore_x509_msg#data_len. = len; |
| 733 |
|
| 734 |
if (kore_strlcpycxx . . $ kore_strlcpy(55abe9014c6d0e39).(hdr.domaincxx . . $ kore_x509_msg#domain., domain, sizeof(hdr.domaincxx . . $ kore_x509_msg#domain.)) >= |
| 735 |
sizeof(hdr.domaincxx . . $ kore_x509_msg#domain.)) |
| 736 |
fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("%s: domain truncated", __func__); |
| 737 |
|
| 738 |
kore_buf_initcxx . . $ kore_buf_init(bd90e0817a56138f).(&buf, sizeof(hdr) + len); |
| 739 |
kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(&buf, &hdr, sizeof(hdr)); |
| 740 |
kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(&buf, data, len); |
| 741 |
|
| 742 |
kore_msg_sendcxx . . $ kore_msg_send(6afaa3cccbe95a50).(target, msg, buf.datacxx . . $ kore_buf#data., buf.offsetcxx . . $ kore_buf#offset.); |
| 743 |
kore_buf_cleanupcxx . . $ kore_buf_cleanup(2648f45d13d14762).(&buf); |
| 744 |
} |
| 745 |
|
| 746 |
#if defined(KORE_USE_ACME) |
| 747 |
static void |
| 748 |
keymgr_acme_init(void) |
| 749 |
{ |
| 750 |
RSA *rsa; |
| 751 |
struct key *key; |
| 752 |
char *e, *n; |
| 753 |
int needsreg; |
| 754 |
const BIGNUM *be, *bn; |
| 755 |
|
| 756 |
if (acme_provider == NULL) |
| 757 |
return; |
| 758 |
|
| 759 |
if (mkdir(KORE_ACME_CERTDIR, 0700) == -1) { |
| 760 |
if (errno != EEXIST) |
| 761 |
fatalx("mkdir(%s): %s", KORE_ACME_CERTDIR, errno_s); |
| 762 |
} |
| 763 |
|
| 764 |
umask(S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH); |
| 765 |
|
| 766 |
needsreg = 0; |
| 767 |
acmeproc_ready = 0; |
| 768 |
key = keymgr_load_privatekey(KORE_ACME_ACCOUNT_KEY); |
| 769 |
|
| 770 |
if (acme_renewal != NULL) |
| 771 |
kore_timer_remove(acme_renewal); |
| 772 |
|
| 773 |
acme_renewal = kore_timer_add(keymgr_acme_renewal, |
| 774 |
ACME_RENEWAL_TIMER, NULL, 0); |
| 775 |
|
| 776 |
if (key->pkey == NULL) { |
| 777 |
kore_log(LOG_INFO, "generating new ACME account key"); |
| 778 |
key->pkey = kore_tls_rsakey_generate(KORE_ACME_ACCOUNT_KEY); |
| 779 |
needsreg = 1; |
| 780 |
} else { |
| 781 |
kore_log(LOG_INFO, "loaded existing ACME account key"); |
| 782 |
} |
| 783 |
|
| 784 |
rsa = EVP_PKEY_get0_RSA(key->pkey); |
| 785 |
RSA_get0_key(rsa, &bn, &be, NULL); |
| 786 |
|
| 787 |
e = keymgr_bignum_base64(be); |
| 788 |
n = keymgr_bignum_base64(bn); |
| 789 |
|
| 790 |
kore_msg_send(KORE_WORKER_ACME, KORE_ACME_RSAKEY_E, e, strlen(e)); |
| 791 |
kore_msg_send(KORE_WORKER_ACME, KORE_ACME_RSAKEY_N, n, strlen(n)); |
| 792 |
|
| 793 |
kore_free(e); |
| 794 |
kore_free(n); |
| 795 |
|
| 796 |
if (needsreg) { |
| 797 |
kore_msg_send(KORE_WORKER_ACME, |
| 798 |
KORE_ACME_ACCOUNT_CREATE, NULL, 0); |
| 799 |
} else { |
| 800 |
kore_msg_send(KORE_WORKER_ACME, |
| 801 |
KORE_ACME_ACCOUNT_RESOLVE, NULL, 0); |
| 802 |
} |
| 803 |
|
| 804 |
kore_msg_register(KORE_ACME_SIGN, keymgr_acme_sign); |
| 805 |
kore_msg_register(KORE_ACME_CSR_REQUEST, keymgr_msg_recv); |
| 806 |
kore_msg_register(KORE_ACME_PROC_READY, keymgr_acme_ready); |
| 807 |
kore_msg_register(KORE_ACME_ORDER_FAILED, keymgr_msg_recv); |
| 808 |
kore_msg_register(KORE_ACME_INSTALL_CERT, keymgr_msg_recv); |
| 809 |
kore_msg_register(KORE_ACME_CHALLENGE_CERT, keymgr_msg_recv); |
| 810 |
} |
| 811 |
|
| 812 |
static void |
| 813 |
keymgr_acme_domainkey(struct kore_domain *dom, struct key *key) |
| 814 |
{ |
| 815 |
char *p; |
| 816 |
|
| 817 |
kore_log(LOG_INFO, "generated new domain key for %s", dom->domain); |
| 818 |
|
| 819 |
if ((p = strrchr(dom->certkey, '/')) == NULL) |
| 820 |
fatalx("invalid certkey path '%s'", dom->certkey); |
| 821 |
|
| 822 |
*p = '\0'; |
| 823 |
|
| 824 |
if (mkdir(dom->certkey, 0700) == -1) { |
| 825 |
if (errno != EEXIST) |
| 826 |
fatalx("mkdir(%s): %s", dom->certkey, errno_s); |
| 827 |
} |
| 828 |
|
| 829 |
*p = '/'; |
| 830 |
key->pkey = kore_tls_rsakey_generate(dom->certkey); |
| 831 |
} |
| 832 |
|
| 833 |
static void |
| 834 |
keymgr_acme_order_create(const char *domain) |
| 835 |
{ |
| 836 |
struct acme_order *order; |
| 837 |
|
| 838 |
order = kore_calloc(1, sizeof(*order)); |
| 839 |
|
| 840 |
order->state = ACME_ORDER_STATE_INIT; |
| 841 |
order->domain = kore_strdup(domain); |
| 842 |
order->timer = kore_timer_add(keymgr_acme_order_start, |
| 843 |
1000, order, KORE_TIMER_ONESHOT); |
| 844 |
} |
| 845 |
|
| 846 |
static void |
| 847 |
keymgr_acme_order_redo(void *udata, u_int64_t now) |
| 848 |
{ |
| 849 |
struct kore_domain *dom = udata; |
| 850 |
|
| 851 |
kore_log(LOG_INFO, "[%s] redoing order", dom->domain); |
| 852 |
keymgr_acme_order_create(dom->domain); |
| 853 |
} |
| 854 |
|
| 855 |
static void |
| 856 |
keymgr_acme_order_start(void *udata, u_int64_t now) |
| 857 |
{ |
| 858 |
struct acme_order *order = udata; |
| 859 |
|
| 860 |
switch (order->state) { |
| 861 |
case ACME_ORDER_STATE_INIT: |
| 862 |
if (acmeproc_ready == 0) |
| 863 |
break; |
| 864 |
order->state = ACME_ORDER_STATE_SUBMIT; |
| 865 |
/* fallthrough */ |
| 866 |
case ACME_ORDER_STATE_SUBMIT: |
| 867 |
kore_msg_send(KORE_WORKER_ACME, KORE_ACME_ORDER_CREATE, |
| 868 |
order->domain, strlen(order->domain)); |
| 869 |
kore_free(order->domain); |
| 870 |
kore_free(order); |
| 871 |
order = NULL; |
| 872 |
break; |
| 873 |
default: |
| 874 |
fatalx("%s: unknown order state %d", __func__, order->state); |
| 875 |
} |
| 876 |
|
| 877 |
if (order != NULL) { |
| 878 |
order->timer = kore_timer_add(keymgr_acme_order_start, |
| 879 |
5000, order, KORE_TIMER_ONESHOT); |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
static void |
| 884 |
keymgr_acme_ready(struct kore_msg *msg, const void *data) |
| 885 |
{ |
| 886 |
acmeproc_ready = 1; |
| 887 |
kore_log(LOG_INFO, "acme process ready to receive orders"); |
| 888 |
|
| 889 |
keymgr_acme_renewal(NULL, kore_time_ms()); |
| 890 |
} |
| 891 |
|
| 892 |
static void |
| 893 |
keymgr_acme_check(struct kore_domain *dom) |
| 894 |
{ |
| 895 |
FILE *fp; |
| 896 |
int days; |
| 897 |
X509 *x509; |
| 898 |
time_t expires, now; |
| 899 |
|
| 900 |
if (dom->acme == 0) |
| 901 |
return; |
| 902 |
|
| 903 |
if (access(dom->certfile, R_OK) == -1) { |
| 904 |
if (errno == ENOENT) { |
| 905 |
keymgr_acme_order_create(dom->domain); |
| 906 |
return; |
| 907 |
} |
| 908 |
kore_log(LOG_ERR, "access(%s): %s", dom->certfile, errno_s); |
| 909 |
return; |
| 910 |
} |
| 911 |
|
| 912 |
if ((fp = fopen(dom->certfile, "r")) == NULL) { |
| 913 |
kore_log(LOG_ERR, "fopen(%s): %s", dom->certfile, errno_s); |
| 914 |
return; |
| 915 |
} |
| 916 |
|
| 917 |
if ((x509 = PEM_read_X509(fp, NULL, NULL, NULL)) == NULL) { |
| 918 |
fclose(fp); |
| 919 |
kore_log(LOG_ERR, "PEM_read_X509: %s", ssl_errno_s); |
| 920 |
return; |
| 921 |
} |
| 922 |
|
| 923 |
fclose(fp); |
| 924 |
|
| 925 |
if (!keymgr_x509_not_after(x509, &expires)) { |
| 926 |
X509_free(x509); |
| 927 |
return; |
| 928 |
} |
| 929 |
|
| 930 |
time(&now); |
| 931 |
days = (expires - now) / 86400; |
| 932 |
|
| 933 |
kore_log(LOG_INFO, "%s certificate expires in %d days", |
| 934 |
dom->domain, days); |
| 935 |
|
| 936 |
if (days <= ACME_RENEWAL_THRESHOLD) { |
| 937 |
kore_log(LOG_INFO, "%s renewing certificate", dom->domain); |
| 938 |
keymgr_acme_order_create(dom->domain); |
| 939 |
} |
| 940 |
|
| 941 |
X509_free(x509); |
| 942 |
} |
| 943 |
|
| 944 |
static void |
| 945 |
keymgr_acme_renewal(void *udata, u_int64_t now) |
| 946 |
{ |
| 947 |
kore_domain_callback(keymgr_acme_check); |
| 948 |
} |
| 949 |
|
| 950 |
static void |
| 951 |
keymgr_acme_sign(struct kore_msg *msg, const void *data) |
| 952 |
{ |
| 953 |
u_int32_t id; |
| 954 |
struct kore_buf buf; |
| 955 |
const u_int8_t *ptr; |
| 956 |
u_int8_t *sig; |
| 957 |
EVP_MD_CTX *ctx; |
| 958 |
struct key *key; |
| 959 |
char *b64; |
| 960 |
unsigned int siglen; |
| 961 |
|
| 962 |
TAILQ_FOREACH(key, &keys, list) { |
| 963 |
if (key->dom == NULL) |
| 964 |
break; |
| 965 |
} |
| 966 |
|
| 967 |
if (key == NULL) |
| 968 |
fatalx("%s: missing key", __func__); |
| 969 |
|
| 970 |
if (msg->length < sizeof(id)) |
| 971 |
fatalx("%s: invalid length (%zu)", __func__, msg->length); |
| 972 |
|
| 973 |
ptr = data; |
| 974 |
memcpy(&id, ptr, sizeof(id)); |
| 975 |
|
| 976 |
ptr += sizeof(id); |
| 977 |
msg->length -= sizeof(id); |
| 978 |
|
| 979 |
sig = kore_calloc(1, EVP_PKEY_size(key->pkey)); |
| 980 |
|
| 981 |
if ((ctx = EVP_MD_CTX_create()) == NULL) |
| 982 |
fatalx("EVP_MD_CTX_create: %s", ssl_errno_s); |
| 983 |
|
| 984 |
if (!EVP_SignInit_ex(ctx, EVP_sha256(), NULL)) |
| 985 |
fatalx("EVP_SignInit_ex: %s", ssl_errno_s); |
| 986 |
|
| 987 |
if (!EVP_SignUpdate(ctx, ptr, msg->length)) |
| 988 |
fatalx("EVP_SignUpdate: %s", ssl_errno_s); |
| 989 |
|
| 990 |
if (!EVP_SignFinal(ctx, sig, &siglen, key->pkey)) |
| 991 |
fatalx("EVP_SignFinal: %s", ssl_errno_s); |
| 992 |
|
| 993 |
if (!kore_base64url_encode(sig, siglen, &b64, KORE_BASE64_RAW)) |
| 994 |
fatalx("%s: failed to b64url encode signed data", __func__); |
| 995 |
|
| 996 |
kore_buf_init(&buf, siglen + sizeof(id)); |
| 997 |
kore_buf_append(&buf, &id, sizeof(id)); |
| 998 |
kore_buf_append(&buf, b64, strlen(b64)); |
| 999 |
|
| 1000 |
kore_msg_send(KORE_WORKER_ACME, |
| 1001 |
KORE_ACME_SIGN_RESULT, buf.data, buf.offset); |
| 1002 |
|
| 1003 |
EVP_MD_CTX_destroy(ctx); |
| 1004 |
|
| 1005 |
kore_free(sig); |
| 1006 |
kore_free(b64); |
| 1007 |
kore_buf_cleanup(&buf); |
| 1008 |
} |
| 1009 |
|
| 1010 |
static void |
| 1011 |
keymgr_acme_install_cert(const void *data, size_t len, struct key *key) |
| 1012 |
{ |
| 1013 |
int fd; |
| 1014 |
ssize_t ret; |
| 1015 |
|
| 1016 |
fd = open(key->dom->certfile, O_CREAT | O_TRUNC | O_WRONLY, 0700); |
| 1017 |
if (fd == -1) |
| 1018 |
fatalx("open(%s): %s", key->dom->certfile, errno_s); |
| 1019 |
|
| 1020 |
kore_log(LOG_INFO, "writing %zu bytes of data", len); |
| 1021 |
|
| 1022 |
for (;;) { |
| 1023 |
ret = write(fd, data, len); |
| 1024 |
if (ret == -1) { |
| 1025 |
if (errno == EINTR) |
| 1026 |
continue; |
| 1027 |
fatalx("write(%s): %s", key->dom->certfile, errno_s); |
| 1028 |
} |
| 1029 |
|
| 1030 |
break; |
| 1031 |
} |
| 1032 |
|
| 1033 |
if ((size_t)ret != len) { |
| 1034 |
fatalx("incorrect write on %s (%zd/%zu)", |
| 1035 |
key->dom->certfile, ret, len); |
| 1036 |
} |
| 1037 |
|
| 1038 |
if (close(fd) == -1) { |
| 1039 |
kore_log(LOG_NOTICE, |
| 1040 |
"close error on '%s' (%s)", key->dom->certfile, errno_s); |
| 1041 |
} |
| 1042 |
|
| 1043 |
keymgr_submit_certificates(key->dom, KORE_MSG_WORKER_ALL); |
| 1044 |
|
| 1045 |
keymgr_x509_msg(key->dom->domain, NULL, 0, |
| 1046 |
KORE_MSG_WORKER_ALL, KORE_ACME_CHALLENGE_CLEAR_CERT); |
| 1047 |
} |
| 1048 |
|
| 1049 |
static void |
| 1050 |
keymgr_acme_order_failed(const void *data, size_t len, struct key *key) |
| 1051 |
{ |
| 1052 |
u_int32_t retry; |
| 1053 |
|
| 1054 |
if (len != sizeof(retry)) { |
| 1055 |
kore_log(LOG_ERR, "%s: invalid payload (%zu)", __func__, len); |
| 1056 |
return; |
| 1057 |
} |
| 1058 |
|
| 1059 |
memcpy(&retry, data, len); |
| 1060 |
|
| 1061 |
kore_timer_add(keymgr_acme_order_redo, retry, key->dom, |
| 1062 |
KORE_TIMER_ONESHOT); |
| 1063 |
} |
| 1064 |
|
| 1065 |
static void |
| 1066 |
keymgr_acme_challenge_cert(const void *data, size_t len, struct key *key) |
| 1067 |
{ |
| 1068 |
STACK_OF(X509_EXTENSION) *sk; |
| 1069 |
size_t idx; |
| 1070 |
time_t now; |
| 1071 |
X509_EXTENSION *ext; |
| 1072 |
X509_NAME *name; |
| 1073 |
X509 *x509; |
| 1074 |
const u_int8_t *digest; |
| 1075 |
int slen, i; |
| 1076 |
u_int8_t *cert, *uptr; |
| 1077 |
char hex[(SHA256_DIGEST_LENGTH * 2) + 1]; |
| 1078 |
|
| 1079 |
kore_log(LOG_INFO, "[%s] generating tls-alpn-01 challenge cert", |
| 1080 |
key->dom->domain); |
| 1081 |
|
| 1082 |
if (len != SHA256_DIGEST_LENGTH) |
| 1083 |
fatalx("invalid digest length of %zu bytes", len); |
| 1084 |
|
| 1085 |
digest = data; |
| 1086 |
|
| 1087 |
for (idx = 0; idx < SHA256_DIGEST_LENGTH; idx++) { |
| 1088 |
slen = snprintf(hex + (idx * 2), sizeof(hex) - (idx * 2), |
| 1089 |
"%02x", digest[idx]); |
| 1090 |
if (slen == -1 || (size_t)slen >= sizeof(hex)) |
| 1091 |
fatalx("failed to convert digest to hex"); |
| 1092 |
} |
| 1093 |
|
| 1094 |
if ((x509 = X509_new()) == NULL) |
| 1095 |
fatalx("X509_new(): %s", ssl_errno_s); |
| 1096 |
|
| 1097 |
if (!X509_set_version(x509, 2)) |
| 1098 |
fatalx("X509_set_version(): %s", ssl_errno_s); |
| 1099 |
|
| 1100 |
time(&now); |
| 1101 |
if (!ASN1_INTEGER_set(X509_get_serialNumber(x509), now)) |
| 1102 |
fatalx("ASN1_INTEGER_set(): %s", ssl_errno_s); |
| 1103 |
|
| 1104 |
if (!X509_gmtime_adj(X509_get_notBefore(x509), 0)) |
| 1105 |
fatalx("X509_gmtime_adj(): %s", ssl_errno_s); |
| 1106 |
|
| 1107 |
if (!X509_gmtime_adj(X509_get_notAfter(x509), ACME_X509_EXPIRATION)) |
| 1108 |
fatalx("X509_gmtime_adj(): %s", ssl_errno_s); |
| 1109 |
|
| 1110 |
if (!X509_set_pubkey(x509, key->pkey)) |
| 1111 |
fatalx("X509_set_pubkey(): %s", ssl_errno_s); |
| 1112 |
|
| 1113 |
if ((name = X509_get_subject_name(x509)) == NULL) |
| 1114 |
fatalx("X509_get_subject_name(): %s", ssl_errno_s); |
| 1115 |
|
| 1116 |
if (!X509_NAME_add_entry_by_txt(name, "CN", |
| 1117 |
MBSTRING_ASC, (const unsigned char *)key->dom->domain, -1, -1, 0)) |
| 1118 |
fatalx("X509_NAME_add_entry_by_txt(): CN %s", ssl_errno_s); |
| 1119 |
|
| 1120 |
if (!X509_set_issuer_name(x509, name)) |
| 1121 |
fatalx("X509_set_issuer_name(): %s", ssl_errno_s); |
| 1122 |
|
| 1123 |
sk = sk_X509_EXTENSION_new_null(); |
| 1124 |
keymgr_x509_ext(sk, acme_oid, "critical,%s", hex); |
| 1125 |
keymgr_x509_ext(sk, NID_subject_alt_name, "DNS:%s", key->dom->domain); |
| 1126 |
|
| 1127 |
for (i = 0; i < sk_X509_EXTENSION_num(sk); i++) { |
| 1128 |
ext = sk_X509_EXTENSION_value(sk, i); |
| 1129 |
if (!X509_add_ext(x509, ext, 0)) |
| 1130 |
fatalx("X509_add_ext(): %s", ssl_errno_s); |
| 1131 |
} |
| 1132 |
|
| 1133 |
if (!X509_sign(x509, key->pkey, EVP_sha256())) |
| 1134 |
fatalx("X509_sign(): %s", ssl_errno_s); |
| 1135 |
|
| 1136 |
if ((slen = i2d_X509(x509, NULL)) <= 0) |
| 1137 |
fatalx("i2d_X509: %s", ssl_errno_s); |
| 1138 |
|
| 1139 |
cert = kore_calloc(1, slen); |
| 1140 |
uptr = cert; |
| 1141 |
|
| 1142 |
if (i2d_X509(x509, &uptr) <= 0) |
| 1143 |
fatalx("i2d_X509: %s", ssl_errno_s); |
| 1144 |
|
| 1145 |
keymgr_x509_msg(key->dom->domain, cert, slen, |
| 1146 |
KORE_MSG_WORKER_ALL, KORE_ACME_CHALLENGE_SET_CERT); |
| 1147 |
|
| 1148 |
kore_free(cert); |
| 1149 |
X509_free(x509); |
| 1150 |
sk_X509_EXTENSION_pop_free(sk, X509_EXTENSION_free); |
| 1151 |
} |
| 1152 |
|
| 1153 |
static void |
| 1154 |
keymgr_acme_csr(const struct kore_keyreq *req, struct key *key) |
| 1155 |
{ |
| 1156 |
int len; |
| 1157 |
STACK_OF(X509_EXTENSION) *sk; |
| 1158 |
X509_REQ *csr; |
| 1159 |
X509_NAME *name; |
| 1160 |
u_int8_t *data, *uptr; |
| 1161 |
|
| 1162 |
kore_log(LOG_INFO, "[%s] creating CSR", req->domain); |
| 1163 |
|
| 1164 |
if ((csr = X509_REQ_new()) == NULL) |
| 1165 |
fatalx("X509_REQ_new: %s", ssl_errno_s); |
| 1166 |
|
| 1167 |
if (!X509_REQ_set_version(csr, 3)) |
| 1168 |
fatalx("X509_REQ_set_version(): %s", ssl_errno_s); |
| 1169 |
|
| 1170 |
if (!X509_REQ_set_pubkey(csr, key->pkey)) |
| 1171 |
fatalx("X509_REQ_set_pubkey(): %s", ssl_errno_s); |
| 1172 |
|
| 1173 |
if ((name = X509_REQ_get_subject_name(csr)) == NULL) |
| 1174 |
fatalx("X509_REQ_get_subject_name(): %s", ssl_errno_s); |
| 1175 |
|
| 1176 |
if (!X509_NAME_add_entry_by_txt(name, "CN", |
| 1177 |
MBSTRING_ASC, (const unsigned char *)key->dom->domain, -1, -1, 0)) |
| 1178 |
fatalx("X509_NAME_add_entry_by_txt(): %s", ssl_errno_s); |
| 1179 |
|
| 1180 |
sk = sk_X509_EXTENSION_new_null(); |
| 1181 |
keymgr_x509_ext(sk, NID_subject_alt_name, "DNS:%s", key->dom->domain); |
| 1182 |
|
| 1183 |
if (!X509_REQ_add_extensions(csr, sk)) |
| 1184 |
fatalx("X509_REQ_add_extensions(): %s", ssl_errno_s); |
| 1185 |
|
| 1186 |
if (!X509_REQ_sign(csr, key->pkey, EVP_sha256())) |
| 1187 |
fatalx("X509_REQ_sign(): %s", ssl_errno_s); |
| 1188 |
|
| 1189 |
if ((len = i2d_X509_REQ(csr, NULL)) <= 0) |
| 1190 |
fatalx("i2d_X509_REQ: %s", ssl_errno_s); |
| 1191 |
|
| 1192 |
data = kore_calloc(1, len); |
| 1193 |
uptr = data; |
| 1194 |
|
| 1195 |
if (i2d_X509_REQ(csr, &uptr) <= 0) |
| 1196 |
fatalx("i2d_X509_REQ: %s", ssl_errno_s); |
| 1197 |
|
| 1198 |
keymgr_x509_msg(key->dom->domain, data, len, |
| 1199 |
KORE_WORKER_ACME, KORE_ACME_CSR_RESPONSE); |
| 1200 |
|
| 1201 |
kore_free(data); |
| 1202 |
X509_REQ_free(csr); |
| 1203 |
|
| 1204 |
sk_X509_EXTENSION_pop_free(sk, X509_EXTENSION_free); |
| 1205 |
} |
| 1206 |
|
| 1207 |
static void |
| 1208 |
keymgr_x509_ext(STACK_OF(X509_EXTENSION) *sk, int extnid, const char *fmt, ...) |
| 1209 |
{ |
| 1210 |
int len; |
| 1211 |
va_list args; |
| 1212 |
X509_EXTENSION *ext; |
| 1213 |
char buf[1024]; |
| 1214 |
|
| 1215 |
va_start(args, fmt); |
| 1216 |
len = vsnprintf(buf, sizeof(buf), fmt, args); |
| 1217 |
va_end(args); |
| 1218 |
|
| 1219 |
if (len == -1 || (size_t)len >= sizeof(buf)) |
| 1220 |
fatalx("failed to create buffer for extension %d", extnid); |
| 1221 |
|
| 1222 |
if ((ext = X509V3_EXT_conf_nid(NULL, NULL, extnid, buf)) == NULL) { |
| 1223 |
fatalx("X509V3_EXT_conf_nid(%d, %s): %s", |
| 1224 |
extnid, buf, ssl_errno_s); |
| 1225 |
} |
| 1226 |
|
| 1227 |
sk_X509_EXTENSION_push(sk, ext); |
| 1228 |
} |
| 1229 |
|
| 1230 |
static char * |
| 1231 |
keymgr_bignum_base64(const BIGNUM *bn) |
| 1232 |
{ |
| 1233 |
int len; |
| 1234 |
void *buf; |
| 1235 |
char *encoded; |
| 1236 |
|
| 1237 |
len = BN_num_bytes(bn); |
| 1238 |
buf = kore_calloc(1, len); |
| 1239 |
|
| 1240 |
if (BN_bn2bin(bn, buf) != len) |
| 1241 |
fatalx("BN_bn2bin: %s", ssl_errno_s); |
| 1242 |
|
| 1243 |
if (!kore_base64url_encode(buf, len, &encoded, KORE_BASE64_RAW)) |
| 1244 |
fatalx("failed to base64 encode BIGNUM"); |
| 1245 |
|
| 1246 |
return (encoded); |
| 1247 |
} |
| 1248 |
|
| 1249 |
static int |
| 1250 |
keymgr_x509_not_after(X509 *x509, time_t *out) |
| 1251 |
{ |
| 1252 |
const ASN1_TIME *na; |
| 1253 |
int ret; |
| 1254 |
|
| 1255 |
ret = KORE_RESULT_ERROR; |
| 1256 |
|
| 1257 |
if ((na = X509_get_notAfter(x509)) == NULL) { |
| 1258 |
kore_log(LOG_ERR, "no notAfter date in x509"); |
| 1259 |
return (KORE_RESULT_ERROR); |
| 1260 |
} |
| 1261 |
|
| 1262 |
switch (na->type) { |
| 1263 |
case V_ASN1_UTCTIME: |
| 1264 |
ret = keymgr_asn1_convert_utctime(na, out); |
| 1265 |
break; |
| 1266 |
case V_ASN1_GENERALIZEDTIME: |
| 1267 |
ret = keymgr_asn1_convert_generalizedtime(na->data, |
| 1268 |
na->length, out); |
| 1269 |
break; |
| 1270 |
default: |
| 1271 |
kore_log(LOG_ERR, "invalid notAfter type (%d)", na->type); |
| 1272 |
break; |
| 1273 |
} |
| 1274 |
|
| 1275 |
return (ret); |
| 1276 |
} |
| 1277 |
|
| 1278 |
static int |
| 1279 |
keymgr_asn1_convert_utctime(const ASN1_TIME *na, time_t *out) |
| 1280 |
{ |
| 1281 |
int len, year; |
| 1282 |
char buf[ASN1_GENERALIZEDTIME_LEN + 1]; |
| 1283 |
|
| 1284 |
if (na->length != ASN1_UTCTIME_LEN) { |
| 1285 |
kore_log(LOG_ERR, "invalid UTCTIME: too short (%d)", |
| 1286 |
na->length); |
| 1287 |
return (KORE_RESULT_ERROR); |
| 1288 |
} |
| 1289 |
|
| 1290 |
if (!isdigit(na->data[0]) || !isdigit(na->data[1])) { |
| 1291 |
kore_log(LOG_ERR, "invalid UTCTIME: YY are not digits"); |
| 1292 |
return (KORE_RESULT_ERROR); |
| 1293 |
} |
| 1294 |
|
| 1295 |
year = (na->data[0] - '0') * 10 + (na->data[1] - '0'); |
| 1296 |
|
| 1297 |
/* RFC 5280 says years >= 50 are interpreted as 19YY */ |
| 1298 |
if (year >= 50) |
| 1299 |
year = 1900 + year; |
| 1300 |
else |
| 1301 |
year = 2000 + year; |
| 1302 |
|
| 1303 |
/* Convert it to GENERALIZEDTIME format and call that parser. */ |
| 1304 |
len = snprintf(buf, sizeof(buf), "%04d%.*s", year, |
| 1305 |
na->length - 2, (const char *)na->data+ 2); |
| 1306 |
if (len == -1 || (size_t)len >= sizeof(buf)) { |
| 1307 |
kore_log(LOG_ERR, "invalid UTCTIME: failed to convert"); |
| 1308 |
return (KORE_RESULT_ERROR); |
| 1309 |
} |
| 1310 |
|
| 1311 |
return (keymgr_asn1_convert_generalizedtime(buf, len, out)); |
| 1312 |
} |
| 1313 |
|
| 1314 |
static int |
| 1315 |
keymgr_asn1_convert_generalizedtime(const void *ptr, size_t len, time_t *out) |
| 1316 |
{ |
| 1317 |
size_t i; |
| 1318 |
struct tm tm; |
| 1319 |
const u_int8_t *buf; |
| 1320 |
|
| 1321 |
if (len != ASN1_GENERALIZEDTIME_LEN) { |
| 1322 |
kore_log(LOG_ERR, "invalid GENERALIZEDTIME: too short (%zu)", |
| 1323 |
len); |
| 1324 |
return (KORE_RESULT_ERROR); |
| 1325 |
} |
| 1326 |
|
| 1327 |
buf = ptr; |
| 1328 |
|
| 1329 |
for (i = 0; i < len - 1; i++) { |
| 1330 |
if (!isdigit(buf[i])) { |
| 1331 |
kore_log(LOG_ERR, |
| 1332 |
"invalid GENERALIZEDTIME: invalid bytes"); |
| 1333 |
return (KORE_RESULT_ERROR); |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
/* RFC 5280 states that Zulu time must be used (Z). */ |
| 1338 |
if (buf[i] != 'Z') { |
| 1339 |
kore_log(LOG_ERR, "invalid GENERALIZEDTIME: not Zulu time"); |
| 1340 |
return (KORE_RESULT_ERROR); |
| 1341 |
} |
| 1342 |
|
| 1343 |
memset(&tm, 0, sizeof(tm)); |
| 1344 |
|
| 1345 |
tm.tm_year = (buf[0] - '0') * 1000 + (buf[1] - '0') * 100 + |
| 1346 |
(buf[2] - '0') * 10 + (buf[3] - '0'); |
| 1347 |
|
| 1348 |
tm.tm_mon = (buf[4] - '0') * 10 + (buf[5] - '0'); |
| 1349 |
tm.tm_mday = (buf[6] - '0') * 10 + (buf[7] - '0'); |
| 1350 |
tm.tm_hour = (buf[8] - '0') * 10 + (buf[9] - '0'); |
| 1351 |
tm.tm_min = (buf[10] - '0') * 10 + (buf[11] - '0'); |
| 1352 |
tm.tm_sec = (buf[12] - '0') * 10 + (buf[13] - '0'); |
| 1353 |
|
| 1354 |
tm.tm_mon = tm.tm_mon - 1; |
| 1355 |
tm.tm_year = tm.tm_year - 1900; |
| 1356 |
|
| 1357 |
*out = mktime(&tm); |
| 1358 |
|
| 1359 |
return (KORE_RESULT_OK); |
| 1360 |
} |
| 1361 |
#endif |