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