1 | /*
|
---|
2 | * Copyright 1999-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 "internal/cryptlib.h"
|
---|
12 | #include <openssl/asn1.h>
|
---|
13 | #include <openssl/conf.h>
|
---|
14 | #include <openssl/x509.h>
|
---|
15 | #include <openssl/x509v3.h>
|
---|
16 |
|
---|
17 | /* Test application to add extensions from a config file */
|
---|
18 |
|
---|
19 | int main(int argc, char **argv)
|
---|
20 | {
|
---|
21 | LHASH *conf;
|
---|
22 | X509 *cert;
|
---|
23 | FILE *inf;
|
---|
24 | char *conf_file;
|
---|
25 | int i;
|
---|
26 | int count;
|
---|
27 | X509_EXTENSION *ext;
|
---|
28 | X509V3_add_standard_extensions();
|
---|
29 | ERR_load_crypto_strings();
|
---|
30 | if (!argv[1]) {
|
---|
31 | fprintf(stderr, "Usage: v3conf cert.pem [file.cnf]\n");
|
---|
32 | exit(1);
|
---|
33 | }
|
---|
34 | conf_file = argv[2];
|
---|
35 | if (!conf_file)
|
---|
36 | conf_file = "test.cnf";
|
---|
37 | conf = CONF_load(NULL, "test.cnf", NULL);
|
---|
38 | if (!conf) {
|
---|
39 | fprintf(stderr, "Error opening Config file %s\n", conf_file);
|
---|
40 | ERR_print_errors_fp(stderr);
|
---|
41 | exit(1);
|
---|
42 | }
|
---|
43 |
|
---|
44 | inf = fopen(argv[1], "r");
|
---|
45 | if (!inf) {
|
---|
46 | fprintf(stderr, "Can't open certificate file %s\n", argv[1]);
|
---|
47 | exit(1);
|
---|
48 | }
|
---|
49 | cert = PEM_read_X509(inf, NULL, NULL);
|
---|
50 | if (!cert) {
|
---|
51 | fprintf(stderr, "Error reading certificate file %s\n", argv[1]);
|
---|
52 | exit(1);
|
---|
53 | }
|
---|
54 | fclose(inf);
|
---|
55 |
|
---|
56 | sk_pop_free(cert->cert_info->extensions, X509_EXTENSION_free);
|
---|
57 | cert->cert_info->extensions = NULL;
|
---|
58 |
|
---|
59 | if (!X509V3_EXT_add_conf(conf, NULL, "test_section", cert)) {
|
---|
60 | fprintf(stderr, "Error adding extensions\n");
|
---|
61 | ERR_print_errors_fp(stderr);
|
---|
62 | exit(1);
|
---|
63 | }
|
---|
64 |
|
---|
65 | count = X509_get_ext_count(cert);
|
---|
66 | printf("%d extensions\n", count);
|
---|
67 | for (i = 0; i < count; i++) {
|
---|
68 | ext = X509_get_ext(cert, i);
|
---|
69 | printf("%s", OBJ_nid2ln(OBJ_obj2nid(ext->object)));
|
---|
70 | if (ext->critical)
|
---|
71 | printf(",critical:\n");
|
---|
72 | else
|
---|
73 | printf(":\n");
|
---|
74 | X509V3_EXT_print_fp(stdout, ext, 0, 0);
|
---|
75 | printf("\n");
|
---|
76 |
|
---|
77 | }
|
---|
78 | return 0;
|
---|
79 | }
|
---|