155714Skris/* v3conf.c */
2280304Sjkim/*
3280304Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4280304Sjkim * 1999.
555714Skris */
655714Skris/* ====================================================================
755714Skris * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
855714Skris *
955714Skris * Redistribution and use in source and binary forms, with or without
1055714Skris * modification, are permitted provided that the following conditions
1155714Skris * are met:
1255714Skris *
1355714Skris * 1. Redistributions of source code must retain the above copyright
14280304Sjkim *    notice, this list of conditions and the following disclaimer.
1555714Skris *
1655714Skris * 2. Redistributions in binary form must reproduce the above copyright
1755714Skris *    notice, this list of conditions and the following disclaimer in
1855714Skris *    the documentation and/or other materials provided with the
1955714Skris *    distribution.
2055714Skris *
2155714Skris * 3. All advertising materials mentioning features or use of this
2255714Skris *    software must display the following acknowledgment:
2355714Skris *    "This product includes software developed by the OpenSSL Project
2455714Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2555714Skris *
2655714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2755714Skris *    endorse or promote products derived from this software without
2855714Skris *    prior written permission. For written permission, please contact
2955714Skris *    licensing@OpenSSL.org.
3055714Skris *
3155714Skris * 5. Products derived from this software may not be called "OpenSSL"
3255714Skris *    nor may "OpenSSL" appear in their names without prior written
3355714Skris *    permission of the OpenSSL Project.
3455714Skris *
3555714Skris * 6. Redistributions of any form whatsoever must retain the following
3655714Skris *    acknowledgment:
3755714Skris *    "This product includes software developed by the OpenSSL Project
3855714Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3955714Skris *
4055714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4155714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4255714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4355714Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4455714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4555714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4655714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4755714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4955714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5055714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5155714Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
5255714Skris * ====================================================================
5355714Skris *
5455714Skris * This product includes cryptographic software written by Eric Young
5555714Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5655714Skris * Hudson (tjh@cryptsoft.com).
5755714Skris *
5855714Skris */
5955714Skris
6055714Skris#include <stdio.h>
6155714Skris#include "cryptlib.h"
6255714Skris#include <openssl/asn1.h>
6355714Skris#include <openssl/conf.h>
6455714Skris#include <openssl/x509.h>
6555714Skris#include <openssl/x509v3.h>
6655714Skris
6755714Skris/* Test application to add extensions from a config file */
6855714Skris
6955714Skrisint main(int argc, char **argv)
7055714Skris{
71280304Sjkim    LHASH *conf;
72280304Sjkim    X509 *cert;
73280304Sjkim    FILE *inf;
74280304Sjkim    char *conf_file;
75280304Sjkim    int i;
76280304Sjkim    int count;
77280304Sjkim    X509_EXTENSION *ext;
78280304Sjkim    X509V3_add_standard_extensions();
79280304Sjkim    ERR_load_crypto_strings();
80280304Sjkim    if (!argv[1]) {
81280304Sjkim        fprintf(stderr, "Usage: v3conf cert.pem [file.cnf]\n");
82280304Sjkim        exit(1);
83280304Sjkim    }
84280304Sjkim    conf_file = argv[2];
85280304Sjkim    if (!conf_file)
86280304Sjkim        conf_file = "test.cnf";
87280304Sjkim    conf = CONF_load(NULL, "test.cnf", NULL);
88280304Sjkim    if (!conf) {
89280304Sjkim        fprintf(stderr, "Error opening Config file %s\n", conf_file);
90280304Sjkim        ERR_print_errors_fp(stderr);
91280304Sjkim        exit(1);
92280304Sjkim    }
9355714Skris
94280304Sjkim    inf = fopen(argv[1], "r");
95280304Sjkim    if (!inf) {
96280304Sjkim        fprintf(stderr, "Can't open certificate file %s\n", argv[1]);
97280304Sjkim        exit(1);
98280304Sjkim    }
99280304Sjkim    cert = PEM_read_X509(inf, NULL, NULL);
100280304Sjkim    if (!cert) {
101280304Sjkim        fprintf(stderr, "Error reading certificate file %s\n", argv[1]);
102280304Sjkim        exit(1);
103280304Sjkim    }
104280304Sjkim    fclose(inf);
10555714Skris
106280304Sjkim    sk_pop_free(cert->cert_info->extensions, X509_EXTENSION_free);
107280304Sjkim    cert->cert_info->extensions = NULL;
10855714Skris
109280304Sjkim    if (!X509V3_EXT_add_conf(conf, NULL, "test_section", cert)) {
110280304Sjkim        fprintf(stderr, "Error adding extensions\n");
111280304Sjkim        ERR_print_errors_fp(stderr);
112280304Sjkim        exit(1);
113280304Sjkim    }
11455714Skris
115280304Sjkim    count = X509_get_ext_count(cert);
116280304Sjkim    printf("%d extensions\n", count);
117280304Sjkim    for (i = 0; i < count; i++) {
118280304Sjkim        ext = X509_get_ext(cert, i);
119280304Sjkim        printf("%s", OBJ_nid2ln(OBJ_obj2nid(ext->object)));
120280304Sjkim        if (ext->critical)
121280304Sjkim            printf(",critical:\n");
122280304Sjkim        else
123280304Sjkim            printf(":\n");
124280304Sjkim        X509V3_EXT_print_fp(stdout, ext, 0, 0);
125280304Sjkim        printf("\n");
126280304Sjkim
127280304Sjkim    }
128280304Sjkim    return 0;
12955714Skris}
130