yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <openssl/pem.h> |
| 13 | #include <openssl/err.h> |
| 14 | #include <openssl/pkcs12.h> |
| 15 | |
| 16 | /* Simple PKCS#12 file creator */ |
| 17 | |
| 18 | int main(int argc, char **argv) |
| 19 | { |
| 20 | FILE *fp; |
| 21 | EVP_PKEY *pkey; |
| 22 | X509 *cert; |
| 23 | PKCS12 *p12; |
| 24 | if (argc != 5) { |
| 25 | fprintf(stderr, "Usage: pkwrite infile password name p12file\n"); |
| 26 | exit(1); |
| 27 | } |
| 28 | OpenSSL_add_all_algorithms(); |
| 29 | ERR_load_crypto_strings(); |
| 30 | if ((fp = fopen(argv[1], "r")) == NULL) { |
| 31 | fprintf(stderr, "Error opening file %s\n", argv[1]); |
| 32 | exit(1); |
| 33 | } |
| 34 | cert = PEM_read_X509(fp, NULL, NULL, NULL); |
| 35 | rewind(fp); |
| 36 | pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL); |
| 37 | fclose(fp); |
| 38 | p12 = PKCS12_create(argv[2], argv[3], pkey, cert, NULL, 0, 0, 0, 0, 0); |
| 39 | if (!p12) { |
| 40 | fprintf(stderr, "Error creating PKCS#12 structure\n"); |
| 41 | ERR_print_errors_fp(stderr); |
| 42 | exit(1); |
| 43 | } |
| 44 | if ((fp = fopen(argv[4], "wb")) == NULL) { |
| 45 | fprintf(stderr, "Error opening file %s\n", argv[1]); |
| 46 | ERR_print_errors_fp(stderr); |
| 47 | exit(1); |
| 48 | } |
| 49 | i2d_PKCS12_fp(fp, p12); |
| 50 | PKCS12_free(p12); |
| 51 | fclose(fp); |
| 52 | return 0; |
| 53 | } |