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