diff options
Diffstat (limited to 'src/usr.bin/openssl/pkcs8.c')
| -rw-r--r-- | src/usr.bin/openssl/pkcs8.c | 92 |
1 files changed, 46 insertions, 46 deletions
diff --git a/src/usr.bin/openssl/pkcs8.c b/src/usr.bin/openssl/pkcs8.c index ea12230006..d78202e03f 100644 --- a/src/usr.bin/openssl/pkcs8.c +++ b/src/usr.bin/openssl/pkcs8.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: pkcs8.c,v 1.15 2022/11/11 17:07:39 joshua Exp $ */ | 1 | /* $OpenBSD: pkcs8.c,v 1.16 2023/03/06 14:32:06 tb Exp $ */ |
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
| 3 | * project 1999-2004. | 3 | * project 1999-2004. |
| 4 | */ | 4 | */ |
| @@ -79,12 +79,12 @@ static struct { | |||
| 79 | char *passargout; | 79 | char *passargout; |
| 80 | int pbe_nid; | 80 | int pbe_nid; |
| 81 | int topk8; | 81 | int topk8; |
| 82 | } pkcs8_config; | 82 | } cfg; |
| 83 | 83 | ||
| 84 | static int | 84 | static int |
| 85 | pkcs8_opt_v1(char *arg) | 85 | pkcs8_opt_v1(char *arg) |
| 86 | { | 86 | { |
| 87 | if ((pkcs8_config.pbe_nid = OBJ_txt2nid(arg)) == NID_undef) { | 87 | if ((cfg.pbe_nid = OBJ_txt2nid(arg)) == NID_undef) { |
| 88 | fprintf(stderr, "Unknown PBE algorithm '%s'\n", arg); | 88 | fprintf(stderr, "Unknown PBE algorithm '%s'\n", arg); |
| 89 | return (1); | 89 | return (1); |
| 90 | } | 90 | } |
| @@ -95,7 +95,7 @@ pkcs8_opt_v1(char *arg) | |||
| 95 | static int | 95 | static int |
| 96 | pkcs8_opt_v2(char *arg) | 96 | pkcs8_opt_v2(char *arg) |
| 97 | { | 97 | { |
| 98 | if ((pkcs8_config.cipher = EVP_get_cipherbyname(arg)) == NULL) { | 98 | if ((cfg.cipher = EVP_get_cipherbyname(arg)) == NULL) { |
| 99 | fprintf(stderr, "Unknown cipher '%s'\n", arg); | 99 | fprintf(stderr, "Unknown cipher '%s'\n", arg); |
| 100 | return (1); | 100 | return (1); |
| 101 | } | 101 | } |
| @@ -109,62 +109,62 @@ static const struct option pkcs8_options[] = { | |||
| 109 | .argname = "file", | 109 | .argname = "file", |
| 110 | .desc = "Input file (default stdin)", | 110 | .desc = "Input file (default stdin)", |
| 111 | .type = OPTION_ARG, | 111 | .type = OPTION_ARG, |
| 112 | .opt.arg = &pkcs8_config.infile, | 112 | .opt.arg = &cfg.infile, |
| 113 | }, | 113 | }, |
| 114 | { | 114 | { |
| 115 | .name = "inform", | 115 | .name = "inform", |
| 116 | .argname = "der | pem", | 116 | .argname = "der | pem", |
| 117 | .desc = "Input format (default PEM)", | 117 | .desc = "Input format (default PEM)", |
| 118 | .type = OPTION_ARG_FORMAT, | 118 | .type = OPTION_ARG_FORMAT, |
| 119 | .opt.value = &pkcs8_config.informat, | 119 | .opt.value = &cfg.informat, |
| 120 | }, | 120 | }, |
| 121 | { | 121 | { |
| 122 | .name = "nocrypt", | 122 | .name = "nocrypt", |
| 123 | .desc = "Use or expect unencrypted private key", | 123 | .desc = "Use or expect unencrypted private key", |
| 124 | .type = OPTION_FLAG, | 124 | .type = OPTION_FLAG, |
| 125 | .opt.flag = &pkcs8_config.nocrypt, | 125 | .opt.flag = &cfg.nocrypt, |
| 126 | }, | 126 | }, |
| 127 | { | 127 | { |
| 128 | .name = "noiter", | 128 | .name = "noiter", |
| 129 | .desc = "Use 1 as iteration count", | 129 | .desc = "Use 1 as iteration count", |
| 130 | .type = OPTION_VALUE, | 130 | .type = OPTION_VALUE, |
| 131 | .value = 1, | 131 | .value = 1, |
| 132 | .opt.value = &pkcs8_config.iter, | 132 | .opt.value = &cfg.iter, |
| 133 | }, | 133 | }, |
| 134 | { | 134 | { |
| 135 | .name = "out", | 135 | .name = "out", |
| 136 | .argname = "file", | 136 | .argname = "file", |
| 137 | .desc = "Output file (default stdout)", | 137 | .desc = "Output file (default stdout)", |
| 138 | .type = OPTION_ARG, | 138 | .type = OPTION_ARG, |
| 139 | .opt.arg = &pkcs8_config.outfile, | 139 | .opt.arg = &cfg.outfile, |
| 140 | }, | 140 | }, |
| 141 | { | 141 | { |
| 142 | .name = "outform", | 142 | .name = "outform", |
| 143 | .argname = "der | pem", | 143 | .argname = "der | pem", |
| 144 | .desc = "Output format (default PEM)", | 144 | .desc = "Output format (default PEM)", |
| 145 | .type = OPTION_ARG_FORMAT, | 145 | .type = OPTION_ARG_FORMAT, |
| 146 | .opt.value = &pkcs8_config.outformat, | 146 | .opt.value = &cfg.outformat, |
| 147 | }, | 147 | }, |
| 148 | { | 148 | { |
| 149 | .name = "passin", | 149 | .name = "passin", |
| 150 | .argname = "source", | 150 | .argname = "source", |
| 151 | .desc = "Input file passphrase source", | 151 | .desc = "Input file passphrase source", |
| 152 | .type = OPTION_ARG, | 152 | .type = OPTION_ARG, |
| 153 | .opt.arg = &pkcs8_config.passargin, | 153 | .opt.arg = &cfg.passargin, |
| 154 | }, | 154 | }, |
| 155 | { | 155 | { |
| 156 | .name = "passout", | 156 | .name = "passout", |
| 157 | .argname = "source", | 157 | .argname = "source", |
| 158 | .desc = "Output file passphrase source", | 158 | .desc = "Output file passphrase source", |
| 159 | .type = OPTION_ARG, | 159 | .type = OPTION_ARG, |
| 160 | .opt.arg = &pkcs8_config.passargout, | 160 | .opt.arg = &cfg.passargout, |
| 161 | }, | 161 | }, |
| 162 | { | 162 | { |
| 163 | .name = "topk8", | 163 | .name = "topk8", |
| 164 | .desc = "Read traditional format key and write PKCS#8 format" | 164 | .desc = "Read traditional format key and write PKCS#8 format" |
| 165 | " key", | 165 | " key", |
| 166 | .type = OPTION_FLAG, | 166 | .type = OPTION_FLAG, |
| 167 | .opt.flag = &pkcs8_config.topk8, | 167 | .opt.flag = &cfg.topk8, |
| 168 | }, | 168 | }, |
| 169 | { | 169 | { |
| 170 | .name = "v1", | 170 | .name = "v1", |
| @@ -208,48 +208,48 @@ pkcs8_main(int argc, char **argv) | |||
| 208 | exit(1); | 208 | exit(1); |
| 209 | } | 209 | } |
| 210 | 210 | ||
| 211 | memset(&pkcs8_config, 0, sizeof(pkcs8_config)); | 211 | memset(&cfg, 0, sizeof(cfg)); |
| 212 | 212 | ||
| 213 | pkcs8_config.iter = PKCS12_DEFAULT_ITER; | 213 | cfg.iter = PKCS12_DEFAULT_ITER; |
| 214 | pkcs8_config.informat = FORMAT_PEM; | 214 | cfg.informat = FORMAT_PEM; |
| 215 | pkcs8_config.outformat = FORMAT_PEM; | 215 | cfg.outformat = FORMAT_PEM; |
| 216 | pkcs8_config.pbe_nid = -1; | 216 | cfg.pbe_nid = -1; |
| 217 | 217 | ||
| 218 | if (options_parse(argc, argv, pkcs8_options, NULL, NULL) != 0) { | 218 | if (options_parse(argc, argv, pkcs8_options, NULL, NULL) != 0) { |
| 219 | pkcs8_usage(); | 219 | pkcs8_usage(); |
| 220 | return (1); | 220 | return (1); |
| 221 | } | 221 | } |
| 222 | 222 | ||
| 223 | if (!app_passwd(bio_err, pkcs8_config.passargin, | 223 | if (!app_passwd(bio_err, cfg.passargin, |
| 224 | pkcs8_config.passargout, &passin, &passout)) { | 224 | cfg.passargout, &passin, &passout)) { |
| 225 | BIO_printf(bio_err, "Error getting passwords\n"); | 225 | BIO_printf(bio_err, "Error getting passwords\n"); |
| 226 | goto end; | 226 | goto end; |
| 227 | } | 227 | } |
| 228 | if ((pkcs8_config.pbe_nid == -1) && !pkcs8_config.cipher) | 228 | if ((cfg.pbe_nid == -1) && !cfg.cipher) |
| 229 | pkcs8_config.pbe_nid = NID_pbeWithMD5AndDES_CBC; | 229 | cfg.pbe_nid = NID_pbeWithMD5AndDES_CBC; |
| 230 | 230 | ||
| 231 | if (pkcs8_config.infile) { | 231 | if (cfg.infile) { |
| 232 | if (!(in = BIO_new_file(pkcs8_config.infile, "rb"))) { | 232 | if (!(in = BIO_new_file(cfg.infile, "rb"))) { |
| 233 | BIO_printf(bio_err, | 233 | BIO_printf(bio_err, |
| 234 | "Can't open input file '%s'\n", | 234 | "Can't open input file '%s'\n", |
| 235 | pkcs8_config.infile); | 235 | cfg.infile); |
| 236 | goto end; | 236 | goto end; |
| 237 | } | 237 | } |
| 238 | } else | 238 | } else |
| 239 | in = BIO_new_fp(stdin, BIO_NOCLOSE); | 239 | in = BIO_new_fp(stdin, BIO_NOCLOSE); |
| 240 | 240 | ||
| 241 | if (pkcs8_config.outfile) { | 241 | if (cfg.outfile) { |
| 242 | if (!(out = BIO_new_file(pkcs8_config.outfile, "wb"))) { | 242 | if (!(out = BIO_new_file(cfg.outfile, "wb"))) { |
| 243 | BIO_printf(bio_err, "Can't open output file '%s'\n", | 243 | BIO_printf(bio_err, "Can't open output file '%s'\n", |
| 244 | pkcs8_config.outfile); | 244 | cfg.outfile); |
| 245 | goto end; | 245 | goto end; |
| 246 | } | 246 | } |
| 247 | } else { | 247 | } else { |
| 248 | out = BIO_new_fp(stdout, BIO_NOCLOSE); | 248 | out = BIO_new_fp(stdout, BIO_NOCLOSE); |
| 249 | } | 249 | } |
| 250 | if (pkcs8_config.topk8) { | 250 | if (cfg.topk8) { |
| 251 | pkey = load_key(bio_err, pkcs8_config.infile, | 251 | pkey = load_key(bio_err, cfg.infile, |
| 252 | pkcs8_config.informat, 1, passin, "key"); | 252 | cfg.informat, 1, passin, "key"); |
| 253 | if (!pkey) | 253 | if (!pkey) |
| 254 | goto end; | 254 | goto end; |
| 255 | if (!(p8inf = EVP_PKEY2PKCS8(pkey))) { | 255 | if (!(p8inf = EVP_PKEY2PKCS8(pkey))) { |
| @@ -257,10 +257,10 @@ pkcs8_main(int argc, char **argv) | |||
| 257 | ERR_print_errors(bio_err); | 257 | ERR_print_errors(bio_err); |
| 258 | goto end; | 258 | goto end; |
| 259 | } | 259 | } |
| 260 | if (pkcs8_config.nocrypt) { | 260 | if (cfg.nocrypt) { |
| 261 | if (pkcs8_config.outformat == FORMAT_PEM) | 261 | if (cfg.outformat == FORMAT_PEM) |
| 262 | PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf); | 262 | PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf); |
| 263 | else if (pkcs8_config.outformat == FORMAT_ASN1) | 263 | else if (cfg.outformat == FORMAT_ASN1) |
| 264 | i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf); | 264 | i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf); |
| 265 | else { | 265 | else { |
| 266 | BIO_printf(bio_err, | 266 | BIO_printf(bio_err, |
| @@ -276,16 +276,16 @@ pkcs8_main(int argc, char **argv) | |||
| 276 | "Enter Encryption Password:", 1)) | 276 | "Enter Encryption Password:", 1)) |
| 277 | goto end; | 277 | goto end; |
| 278 | } | 278 | } |
| 279 | if (!(p8 = PKCS8_encrypt(pkcs8_config.pbe_nid, | 279 | if (!(p8 = PKCS8_encrypt(cfg.pbe_nid, |
| 280 | pkcs8_config.cipher, p8pass, strlen(p8pass), | 280 | cfg.cipher, p8pass, strlen(p8pass), |
| 281 | NULL, 0, pkcs8_config.iter, p8inf))) { | 281 | NULL, 0, cfg.iter, p8inf))) { |
| 282 | BIO_printf(bio_err, "Error encrypting key\n"); | 282 | BIO_printf(bio_err, "Error encrypting key\n"); |
| 283 | ERR_print_errors(bio_err); | 283 | ERR_print_errors(bio_err); |
| 284 | goto end; | 284 | goto end; |
| 285 | } | 285 | } |
| 286 | if (pkcs8_config.outformat == FORMAT_PEM) | 286 | if (cfg.outformat == FORMAT_PEM) |
| 287 | PEM_write_bio_PKCS8(out, p8); | 287 | PEM_write_bio_PKCS8(out, p8); |
| 288 | else if (pkcs8_config.outformat == FORMAT_ASN1) | 288 | else if (cfg.outformat == FORMAT_ASN1) |
| 289 | i2d_PKCS8_bio(out, p8); | 289 | i2d_PKCS8_bio(out, p8); |
| 290 | else { | 290 | else { |
| 291 | BIO_printf(bio_err, | 291 | BIO_printf(bio_err, |
| @@ -297,20 +297,20 @@ pkcs8_main(int argc, char **argv) | |||
| 297 | ret = 0; | 297 | ret = 0; |
| 298 | goto end; | 298 | goto end; |
| 299 | } | 299 | } |
| 300 | if (pkcs8_config.nocrypt) { | 300 | if (cfg.nocrypt) { |
| 301 | if (pkcs8_config.informat == FORMAT_PEM) | 301 | if (cfg.informat == FORMAT_PEM) |
| 302 | p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, | 302 | p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, |
| 303 | NULL, NULL); | 303 | NULL, NULL); |
| 304 | else if (pkcs8_config.informat == FORMAT_ASN1) | 304 | else if (cfg.informat == FORMAT_ASN1) |
| 305 | p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL); | 305 | p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL); |
| 306 | else { | 306 | else { |
| 307 | BIO_printf(bio_err, "Bad format specified for key\n"); | 307 | BIO_printf(bio_err, "Bad format specified for key\n"); |
| 308 | goto end; | 308 | goto end; |
| 309 | } | 309 | } |
| 310 | } else { | 310 | } else { |
| 311 | if (pkcs8_config.informat == FORMAT_PEM) | 311 | if (cfg.informat == FORMAT_PEM) |
| 312 | p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL); | 312 | p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL); |
| 313 | else if (pkcs8_config.informat == FORMAT_ASN1) | 313 | else if (cfg.informat == FORMAT_ASN1) |
| 314 | p8 = d2i_PKCS8_bio(in, NULL); | 314 | p8 = d2i_PKCS8_bio(in, NULL); |
| 315 | else { | 315 | else { |
| 316 | BIO_printf(bio_err, "Bad format specified for key\n"); | 316 | BIO_printf(bio_err, "Bad format specified for key\n"); |
| @@ -342,10 +342,10 @@ pkcs8_main(int argc, char **argv) | |||
| 342 | ERR_print_errors(bio_err); | 342 | ERR_print_errors(bio_err); |
| 343 | goto end; | 343 | goto end; |
| 344 | } | 344 | } |
| 345 | if (pkcs8_config.outformat == FORMAT_PEM) | 345 | if (cfg.outformat == FORMAT_PEM) |
| 346 | PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, | 346 | PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, |
| 347 | passout); | 347 | passout); |
| 348 | else if (pkcs8_config.outformat == FORMAT_ASN1) | 348 | else if (cfg.outformat == FORMAT_ASN1) |
| 349 | i2d_PrivateKey_bio(out, pkey); | 349 | i2d_PrivateKey_bio(out, pkey); |
| 350 | else { | 350 | else { |
| 351 | BIO_printf(bio_err, "Bad format specified for key\n"); | 351 | BIO_printf(bio_err, "Bad format specified for key\n"); |
