| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "ask-password-api.h" |
| 5 |
#include "build.h" |
| 6 |
#include "crypto-util.h" |
| 7 |
#include "fd-util.h" |
| 8 |
#include "fileio.h" |
| 9 |
#include "format-table.h" |
| 10 |
#include "fs-util.h" |
| 11 |
#include "log.h" |
| 12 |
#include "main-func.h" |
| 13 |
#include "options.h" |
| 14 |
#include "parse-argument.h" |
| 15 |
#include "pretty-print.h" |
| 16 |
#include "string-util.h" |
| 17 |
#include "tmpfile-util.h" |
| 18 |
#include "verbs.h" |
| 19 |
|
| 20 |
static char *arg_private_key = NULL; |
| 21 |
static KeySourceType arg_private_key_source_type = OPENSSL_KEY_SOURCE_FILE; |
| 22 |
static char *arg_private_key_source = NULL; |
| 23 |
static char *arg_certificate = NULL; |
| 24 |
static char *arg_certificate_source = NULL; |
| 25 |
static CertificateSourceType arg_certificate_source_type = OPENSSL_CERTIFICATE_SOURCE_FILE; |
| 26 |
static char *arg_signature = NULL; |
| 27 |
static char *arg_content = NULL; |
| 28 |
static const char *arg_hash_algorithm = NULL; |
| 29 |
static char *arg_output = NULL; |
| 30 |
|
| 31 |
STATIC_DESTRUCTOR_REGISTER(arg_private_key, freep); |
| 32 |
STATIC_DESTRUCTOR_REGISTER(arg_private_key_source, freep); |
| 33 |
STATIC_DESTRUCTOR_REGISTER(arg_certificate, freep); |
| 34 |
STATIC_DESTRUCTOR_REGISTER(arg_certificate_source, freep); |
| 35 |
STATIC_DESTRUCTOR_REGISTER(arg_signature, freep); |
| 36 |
STATIC_DESTRUCTOR_REGISTER(arg_content, freep); |
| 37 |
STATIC_DESTRUCTOR_REGISTER(arg_output, freep); |
| 38 |
|
| 39 |
static int help(void) { |
| 40 |
_cleanup_free_ char *link = NULL; |
| 41 |
_cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL; |
| 42 |
int r; |
| 43 |
|
| 44 |
r = terminal_urlify_man("systemd-keyutil", "1", &link); |
| 45 |
if (r < 0) |
| 46 |
return log_oom(); |
| 47 |
|
| 48 |
r = verbs_get_help_table(&verbs); |
| 49 |
if (r < 0) |
| 50 |
return r; |
| 51 |
|
| 52 |
r = option_parser_get_help_table(&options); |
| 53 |
if (r < 0) |
| 54 |
return r; |
| 55 |
|
| 56 |
(void) table_sync_column_widths(0, verbs, options); |
| 57 |
|
| 58 |
printf("%s [OPTIONS...] COMMAND ...\n\n" |
| 59 |
"%sPerform various operations on private keys and certificates.%s\n" |
| 60 |
"\n%sCommands:%s\n", |
| 61 |
program_invocation_short_name, |
| 62 |
ansi_highlight(), |
| 63 |
ansi_normal(), |
| 64 |
ansi_underline(), |
| 65 |
ansi_normal()); |
| 66 |
|
| 67 |
r = table_print_or_warn(verbs); |
| 68 |
if (r < 0) |
| 69 |
return r; |
| 70 |
|
| 71 |
printf("\n%sOptions:%s\n", |
| 72 |
ansi_underline(), |
| 73 |
ansi_normal()); |
| 74 |
|
| 75 |
r = table_print_or_warn(options); |
| 76 |
if (r < 0) |
| 77 |
return r; |
| 78 |
|
| 79 |
printf("\nSee the %s for details.\n", link); |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
VERB_COMMON_HELP_HIDDEN(help); |
| 84 |
|
| 85 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 86 |
assert(argc >= 0); |
| 87 |
assert(argv); |
| 88 |
assert(ret_args); |
| 89 |
|
| 90 |
OptionParser opts = { argc, argv }; |
| 91 |
int r; |
| 92 |
|
| 93 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 94 |
switch (c) { |
| 95 |
|
| 96 |
OPTION_COMMON_HELP: |
| 97 |
return help(); |
| 98 |
|
| 99 |
OPTION_COMMON_VERSION: |
| 100 |
return version(); |
| 101 |
|
| 102 |
OPTION_COMMON_PRIVATE_KEY("Private key in PEM format"): |
| 103 |
r = free_and_strdup_warn(&arg_private_key, opts.arg); |
| 104 |
if (r < 0) |
| 105 |
return r; |
| 106 |
break; |
| 107 |
|
| 108 |
OPTION_COMMON_PRIVATE_KEY_SOURCE: |
| 109 |
r = parse_openssl_key_source_argument( |
| 110 |
opts.arg, |
| 111 |
&arg_private_key_source, |
| 112 |
&arg_private_key_source_type); |
| 113 |
if (r < 0) |
| 114 |
return r; |
| 115 |
break; |
| 116 |
|
| 117 |
OPTION_COMMON_CERTIFICATE("PEM certificate to use for signing"): |
| 118 |
r = free_and_strdup_warn(&arg_certificate, opts.arg); |
| 119 |
if (r < 0) |
| 120 |
return r; |
| 121 |
break; |
| 122 |
|
| 123 |
OPTION_COMMON_CERTIFICATE_SOURCE: |
| 124 |
r = parse_openssl_certificate_source_argument( |
| 125 |
opts.arg, |
| 126 |
&arg_certificate_source, |
| 127 |
&arg_certificate_source_type); |
| 128 |
if (r < 0) |
| 129 |
return r; |
| 130 |
break; |
| 131 |
|
| 132 |
OPTION_LONG("signature", "PATH", "PKCS#1 signature to embed in PKCS#7 signature"): |
| 133 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_signature); |
| 134 |
if (r < 0) |
| 135 |
return r; |
| 136 |
break; |
| 137 |
|
| 138 |
OPTION_LONG("content", "PATH", "Raw data content to embed in PKCS#7 signature"): |
| 139 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_content); |
| 140 |
if (r < 0) |
| 141 |
return r; |
| 142 |
break; |
| 143 |
|
| 144 |
OPTION_LONG("hash-algorithm", "ALGORITHM", |
| 145 |
"Hash algorithm used to create the PKCS#1 signature"): |
| 146 |
arg_hash_algorithm = opts.arg; |
| 147 |
break; |
| 148 |
|
| 149 |
OPTION_LONG("output", "PATH", "Where to write the PKCS#7 signature"): |
| 150 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_output); |
| 151 |
if (r < 0) |
| 152 |
return r; |
| 153 |
break; |
| 154 |
} |
| 155 |
|
| 156 |
if (arg_private_key_source && !arg_certificate) |
| 157 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "When using --private-key-source=, --certificate= must be specified."); |
| 158 |
|
| 159 |
*ret_args = option_parser_get_args(&opts); |
| 160 |
return 1; |
| 161 |
} |
| 162 |
|
| 163 |
VERB_NOARG(verb_validate, "validate", |
| 164 |
"Load and validate the given certificate and private key"); |
| 165 |
static int verb_validate(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 166 |
_cleanup_(X509_freep) X509 *certificate = NULL; |
| 167 |
_cleanup_(openssl_ask_password_ui_freep) OpenSSLAskPasswordUI *ui = NULL; |
| 168 |
_cleanup_(EVP_PKEY_freep) EVP_PKEY *private_key = NULL; |
| 169 |
int r; |
| 170 |
|
| 171 |
if (!arg_certificate) |
| 172 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 173 |
"No certificate specified, use --certificate="); |
| 174 |
|
| 175 |
if (!arg_private_key) |
| 176 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 177 |
"No private key specified, use --private-key=."); |
| 178 |
|
| 179 |
if (arg_certificate_source_type == OPENSSL_CERTIFICATE_SOURCE_FILE) { |
| 180 |
r = parse_path_argument(arg_certificate, /* suppress_root= */ false, &arg_certificate); |
| 181 |
if (r < 0) |
| 182 |
return r; |
| 183 |
} |
| 184 |
|
| 185 |
r = openssl_load_x509_certificate( |
| 186 |
arg_certificate_source_type, |
| 187 |
arg_certificate_source, |
| 188 |
arg_certificate, |
| 189 |
&certificate); |
| 190 |
if (r < 0) |
| 191 |
return log_error_errno(r, "Failed to load X.509 certificate from %s: %m", arg_certificate); |
| 192 |
|
| 193 |
if (arg_private_key_source_type == OPENSSL_KEY_SOURCE_FILE) { |
| 194 |
r = parse_path_argument(arg_private_key, /* suppress_root= */ false, &arg_private_key); |
| 195 |
if (r < 0) |
| 196 |
return r; |
| 197 |
} |
| 198 |
|
| 199 |
r = openssl_load_private_key( |
| 200 |
arg_private_key_source_type, |
| 201 |
arg_private_key_source, |
| 202 |
arg_private_key, |
| 203 |
&(AskPasswordRequest) { |
| 204 |
.tty_fd = -EBADF, |
| 205 |
.id = "keyutil-private-key-pin", |
| 206 |
.keyring = arg_private_key, |
| 207 |
.credential = "keyutil.private-key-pin", |
| 208 |
.until = USEC_INFINITY, |
| 209 |
.hup_fd = -EBADF, |
| 210 |
}, |
| 211 |
&private_key, |
| 212 |
&ui); |
| 213 |
if (r < 0) |
| 214 |
return log_error_errno(r, "Failed to load private key from %s: %m", arg_private_key); |
| 215 |
|
| 216 |
puts("OK"); |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
VERB_NOARG(verb_extract_public, "extract-public", |
| 221 |
"Extract a public key"); |
| 222 |
VERB_NOARG(verb_extract_public, "public", /* help= */ NULL); /* Deprecated but kept for backward compat. */ |
| 223 |
static int verb_extract_public(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 224 |
_cleanup_(EVP_PKEY_freep) EVP_PKEY *public_key = NULL; |
| 225 |
int r; |
| 226 |
|
| 227 |
if (arg_certificate) { |
| 228 |
_cleanup_(X509_freep) X509 *certificate = NULL; |
| 229 |
|
| 230 |
if (arg_certificate_source_type == OPENSSL_CERTIFICATE_SOURCE_FILE) { |
| 231 |
r = parse_path_argument(arg_certificate, /* suppress_root= */ false, &arg_certificate); |
| 232 |
if (r < 0) |
| 233 |
return r; |
| 234 |
} |
| 235 |
|
| 236 |
r = DLOPEN_LIBCRYPTO(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 237 |
if (r < 0) |
| 238 |
return r; |
| 239 |
|
| 240 |
r = openssl_load_x509_certificate( |
| 241 |
arg_certificate_source_type, |
| 242 |
arg_certificate_source, |
| 243 |
arg_certificate, |
| 244 |
&certificate); |
| 245 |
if (r < 0) |
| 246 |
return log_error_errno(r, "Failed to load X.509 certificate from %s: %m", arg_certificate); |
| 247 |
|
| 248 |
public_key = sym_X509_get_pubkey(certificate); |
| 249 |
if (!public_key) |
| 250 |
return log_error_errno( |
| 251 |
SYNTHETIC_ERRNO(EIO), |
| 252 |
"Failed to extract public key from certificate %s.", |
| 253 |
arg_certificate); |
| 254 |
|
| 255 |
} else if (arg_private_key) { |
| 256 |
_cleanup_(openssl_ask_password_ui_freep) OpenSSLAskPasswordUI *ui = NULL; |
| 257 |
_cleanup_(EVP_PKEY_freep) EVP_PKEY *private_key = NULL; |
| 258 |
|
| 259 |
if (arg_private_key_source_type == OPENSSL_KEY_SOURCE_FILE) { |
| 260 |
r = parse_path_argument(arg_private_key, /* suppress_root= */ false, &arg_private_key); |
| 261 |
if (r < 0) |
| 262 |
return r; |
| 263 |
} |
| 264 |
|
| 265 |
r = openssl_load_private_key( |
| 266 |
arg_private_key_source_type, |
| 267 |
arg_private_key_source, |
| 268 |
arg_private_key, |
| 269 |
&(AskPasswordRequest) { |
| 270 |
.tty_fd = -EBADF, |
| 271 |
.id = "keyutil-private-key-pin", |
| 272 |
.keyring = arg_private_key, |
| 273 |
.credential = "keyutil.private-key-pin", |
| 274 |
.until = USEC_INFINITY, |
| 275 |
.hup_fd = -EBADF, |
| 276 |
}, |
| 277 |
&private_key, |
| 278 |
&ui); |
| 279 |
if (r < 0) |
| 280 |
return log_error_errno(r, "Failed to load private key from %s: %m", arg_private_key); |
| 281 |
|
| 282 |
r = openssl_extract_public_key(private_key, &public_key); |
| 283 |
if (r < 0) |
| 284 |
return log_error_errno(r, "Failed to extract public key from private key file '%s': %m", arg_private_key); |
| 285 |
} else |
| 286 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "One of --certificate=, or --private-key= must be specified"); |
| 287 |
|
| 288 |
if (sym_PEM_write_PUBKEY(stdout, public_key) == 0) |
| 289 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write public key to stdout"); |
| 290 |
|
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
VERB_NOARG(verb_extract_certificate, "extract-certificate", |
| 295 |
"Extract a certificate"); |
| 296 |
static int verb_extract_certificate(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 297 |
_cleanup_(X509_freep) X509 *certificate = NULL; |
| 298 |
int r; |
| 299 |
|
| 300 |
if (!arg_certificate) |
| 301 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--certificate= must be specified."); |
| 302 |
|
| 303 |
if (arg_certificate_source_type == OPENSSL_CERTIFICATE_SOURCE_FILE) { |
| 304 |
r = parse_path_argument(arg_certificate, /* suppress_root= */ false, &arg_certificate); |
| 305 |
if (r < 0) |
| 306 |
return r; |
| 307 |
} |
| 308 |
|
| 309 |
r = openssl_load_x509_certificate( |
| 310 |
arg_certificate_source_type, |
| 311 |
arg_certificate_source, |
| 312 |
arg_certificate, |
| 313 |
&certificate); |
| 314 |
if (r < 0) |
| 315 |
return log_error_errno(r, "Failed to load X.509 certificate from %s: %m", arg_certificate); |
| 316 |
|
| 317 |
if (sym_PEM_write_X509(stdout, certificate) == 0) |
| 318 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write certificate to stdout."); |
| 319 |
|
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
VERB(verb_pkcs7, "pkcs7", NULL, VERB_ANY, VERB_ANY, 0, |
| 324 |
"Generate a PKCS#7 signature"); |
| 325 |
static int verb_pkcs7(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 326 |
_cleanup_(X509_freep) X509 *certificate = NULL; |
| 327 |
_cleanup_free_ char *pkcs1 = NULL; |
| 328 |
size_t pkcs1_len = 0; |
| 329 |
int r; |
| 330 |
|
| 331 |
if (!arg_certificate) |
| 332 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--certificate= must be specified"); |
| 333 |
|
| 334 |
if (!arg_signature) |
| 335 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--signature= must be specified"); |
| 336 |
|
| 337 |
if (!arg_output) |
| 338 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--output= must be specified"); |
| 339 |
|
| 340 |
if (arg_certificate_source_type == OPENSSL_CERTIFICATE_SOURCE_FILE) { |
| 341 |
r = parse_path_argument(arg_certificate, /* suppress_root= */ false, &arg_certificate); |
| 342 |
if (r < 0) |
| 343 |
return r; |
| 344 |
} |
| 345 |
|
| 346 |
r = openssl_load_x509_certificate( |
| 347 |
arg_certificate_source_type, |
| 348 |
arg_certificate_source, |
| 349 |
arg_certificate, |
| 350 |
&certificate); |
| 351 |
if (r < 0) |
| 352 |
return log_error_errno(r, "Failed to load X.509 certificate from %s: %m", arg_certificate); |
| 353 |
|
| 354 |
r = read_full_file(arg_signature, &pkcs1, &pkcs1_len); |
| 355 |
if (r < 0) |
| 356 |
return log_error_errno(r, "Failed to read PKCS#1 file %s: %m", arg_signature); |
| 357 |
if (pkcs1_len == 0) |
| 358 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "PKCS#1 file %s is empty", arg_signature); |
| 359 |
|
| 360 |
_cleanup_(PKCS7_freep) PKCS7 *pkcs7 = NULL; |
| 361 |
PKCS7_SIGNER_INFO *signer_info; |
| 362 |
r = pkcs7_new(certificate, /* private_key= */ NULL, arg_hash_algorithm, &pkcs7, &signer_info); |
| 363 |
if (r < 0) |
| 364 |
return log_error_errno(r, "Failed to allocate PKCS#7 context: %m"); |
| 365 |
|
| 366 |
if (arg_content) { |
| 367 |
_cleanup_free_ char *content = NULL; |
| 368 |
size_t content_len = 0; |
| 369 |
|
| 370 |
r = read_full_file(arg_content, &content, &content_len); |
| 371 |
if (r < 0) |
| 372 |
return log_error_errno(r, "Failed to read content file %s: %m", arg_content); |
| 373 |
if (content_len == 0) |
| 374 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Content file %s is empty", arg_content); |
| 375 |
|
| 376 |
if (!sym_PKCS7_content_new(pkcs7, NID_pkcs7_data)) |
| 377 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Error creating new PKCS7 content field"); |
| 378 |
|
| 379 |
sym_ASN1_STRING_set0(pkcs7->d.sign->contents->d.data, TAKE_PTR(content), content_len); |
| 380 |
} else |
| 381 |
if (sym_PKCS7_set_detached(pkcs7, true) == 0) |
| 382 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), |
| 383 |
"Failed to set PKCS#7 detached attribute: %s", |
| 384 |
sym_ERR_error_string(sym_ERR_get_error(), NULL)); |
| 385 |
|
| 386 |
/* Add PKCS1 signature to PKCS7_SIGNER_INFO */ |
| 387 |
sym_ASN1_STRING_set0(signer_info->enc_digest, TAKE_PTR(pkcs1), pkcs1_len); |
| 388 |
|
| 389 |
_cleanup_fclose_ FILE *output = NULL; |
| 390 |
_cleanup_(unlink_and_freep) char *tmp = NULL; |
| 391 |
r = fopen_tmpfile_linkable(arg_output, O_WRONLY|O_CLOEXEC, &tmp, &output); |
| 392 |
if (r < 0) |
| 393 |
return log_error_errno(r, "Failed to open temporary file: %m"); |
| 394 |
|
| 395 |
if (!sym_i2d_PKCS7_fp(output, pkcs7)) |
| 396 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write PKCS#7 file: %s", |
| 397 |
sym_ERR_error_string(sym_ERR_get_error(), NULL)); |
| 398 |
|
| 399 |
r = flink_tmpfile(output, tmp, arg_output, LINK_TMPFILE_REPLACE|LINK_TMPFILE_SYNC); |
| 400 |
if (r < 0) |
| 401 |
return log_error_errno(r, "Failed to link temporary file to %s: %m", arg_output); |
| 402 |
|
| 403 |
tmp = mfree(tmp); |
| 404 |
|
| 405 |
return 0; |
| 406 |
} |
| 407 |
|
| 408 |
static int run(int argc, char *argv[]) { |
| 409 |
int r; |
| 410 |
|
| 411 |
log_setup(); |
| 412 |
|
| 413 |
char **args = NULL; |
| 414 |
r = parse_argv(argc, argv, &args); |
| 415 |
if (r <= 0) |
| 416 |
return r; |
| 417 |
|
| 418 |
return dispatch_verb(args, NULL); |
| 419 |
} |
| 420 |
|
| 421 |
DEFINE_MAIN_FUNCTION(run); |