src/util/mkcert/mkcert.c
changeset 3321 52e8eec3014c
equal deleted inserted replaced
3320:f727edff50bd 3321:52e8eec3014c
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 /*
       
    27  * Certificate creation. Demonstrates some certificate related
       
    28  * operations.
       
    29  */
       
    30 
       
    31 
       
    32 #include <stdio.h>
       
    33 #include <stdlib.h>
       
    34 #include <string.h>
       
    35 
       
    36 #include <openssl/pem.h>
       
    37 #include <openssl/conf.h>
       
    38 #include <openssl/x509v3.h>
       
    39 #ifndef OPENSSL_NO_ENGINE
       
    40 #include <openssl/engine.h>
       
    41 #endif
       
    42 
       
    43 int mkcert(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days);
       
    44 int add_ext(X509 *cert, int nid, char *value);
       
    45 
       
    46 int
       
    47 main(int argc, char **argv)
       
    48 {
       
    49 	BIO *bio_err;
       
    50 	X509 *x509 = NULL;
       
    51 	EVP_PKEY *pkey = NULL;
       
    52 	FILE *fp = NULL;
       
    53 
       
    54 	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
       
    55 
       
    56 	bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
       
    57 
       
    58 	mkcert(&x509, &pkey, 1024, 0, 365);
       
    59 
       
    60 	RSA_print_fp(stdout, pkey->pkey.rsa, 0);
       
    61 	X509_print_fp(stdout, x509);
       
    62 
       
    63 	fp = fopen("cust_key.pem", "w");
       
    64 	PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
       
    65 	fp = fopen("cust_cert.pem", "w");
       
    66 	PEM_write_X509(fp, x509);
       
    67 
       
    68 	X509_free(x509);
       
    69 	EVP_PKEY_free(pkey);
       
    70 
       
    71 #ifndef OPENSSL_NO_ENGINE
       
    72 	ENGINE_cleanup();
       
    73 #endif
       
    74 	CRYPTO_cleanup_all_ex_data();
       
    75 
       
    76 	CRYPTO_mem_leaks(bio_err);
       
    77 	BIO_free(bio_err);
       
    78 	return (0);
       
    79 }
       
    80 
       
    81 static void callback(int p, int n, void *arg)
       
    82 {
       
    83 	char c = 'B';
       
    84 
       
    85 	if (p == 0) c = '.';
       
    86 	if (p == 1) c = '+';
       
    87 	if (p == 2) c = '*';
       
    88 	if (p == 3) c = '\n';
       
    89 	fputc(c, stderr);
       
    90 }
       
    91 
       
    92 int
       
    93 mkcert(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days)
       
    94 {
       
    95 	X509 *x;
       
    96 	EVP_PKEY *pk;
       
    97 	RSA *rsa;
       
    98 	X509_NAME *name = NULL;
       
    99 
       
   100 	if ((pkeyp == NULL) || (*pkeyp == NULL)) {
       
   101 		if ((pk = EVP_PKEY_new()) == NULL) {
       
   102 			abort();
       
   103 		}
       
   104 	}
       
   105 	else
       
   106 		pk = *pkeyp;
       
   107 
       
   108 	if ((x509p == NULL) || (*x509p == NULL)) {
       
   109 		if ((x = X509_new()) == NULL)
       
   110 			goto err;
       
   111 	}
       
   112 	else
       
   113 		x = *x509p;
       
   114 
       
   115 	rsa = RSA_generate_key(bits, RSA_F4, callback, NULL);
       
   116 	if (!EVP_PKEY_assign_RSA(pk, rsa)) {
       
   117 		abort();
       
   118 	}
       
   119 	rsa = NULL;
       
   120 
       
   121 	X509_set_version(x, 2);
       
   122 	ASN1_INTEGER_set(X509_get_serialNumber(x), serial);
       
   123 	X509_gmtime_adj(X509_get_notBefore(x), 0);
       
   124 	X509_gmtime_adj(X509_get_notAfter(x), (long)60*60*24*days);
       
   125 	X509_set_pubkey(x, pk);
       
   126 
       
   127 	name = X509_get_subject_name(x);
       
   128 
       
   129 	/*
       
   130 	 * This function creates and adds the entry, working out the
       
   131 	 * correct string type and performing checks on its length.
       
   132 	 * Normally we'd check the return value for errors...
       
   133 	 */
       
   134 	X509_NAME_add_entry_by_txt(name, "C",
       
   135 	    MBSTRING_ASC, (unsigned char *)"US", -1, -1, 0);
       
   136 	X509_NAME_add_entry_by_txt(name, "ST",
       
   137 	    MBSTRING_ASC, (unsigned char *)"California", -1, -1, 0);
       
   138 	X509_NAME_add_entry_by_txt(name, "L",
       
   139 	    MBSTRING_ASC, (unsigned char *)"Santa Clara", -1, -1, 0);
       
   140 	X509_NAME_add_entry_by_txt(name, "O",
       
   141 	    MBSTRING_ASC, (unsigned char *)"pkg5", -1, -1, 0);
       
   142 	X509_NAME_add_entry_by_txt(name, "CN",
       
   143 	    MBSTRING_ASC, (unsigned char *)"OpenSSL Group", -1, -1, 0);
       
   144 
       
   145 	/*
       
   146 	 * Its self signed so set the issuer name to be the same as the
       
   147 	 * subject.
       
   148 	 */
       
   149 	X509_set_issuer_name(x, name);
       
   150 
       
   151 
       
   152 #ifdef CUSTOM_EXT
       
   153 	/* Maybe even add our own extension based on existing */
       
   154 	{
       
   155 		int nid;
       
   156 		nid = OBJ_create("1.2.3.4", "MyAlias",
       
   157 		    "My Test Alias Extension");
       
   158 		X509V3_EXT_add_alias(nid, NID_netscape_comment);
       
   159 		add_ext(x, nid, "critical,example comment alias");
       
   160 	}
       
   161 #endif
       
   162 
       
   163 	if (!X509_sign(x, pk, EVP_sha256()))
       
   164 		goto err;
       
   165 
       
   166 	*x509p = x;
       
   167 	*pkeyp = pk;
       
   168 	return (1);
       
   169 err:
       
   170 	return (0);
       
   171 }
       
   172 
       
   173 /*
       
   174  * Add extension using V3 code: we can set the config file as NULL
       
   175  * because we wont reference any other sections.
       
   176  */
       
   177 
       
   178 int
       
   179 add_ext(X509 *cert, int nid, char *value)
       
   180 {
       
   181 	X509_EXTENSION *ex;
       
   182 	X509V3_CTX ctx;
       
   183 	/* This sets the 'context' of the extensions. */
       
   184 	/* No configuration database */
       
   185 	X509V3_set_ctx_nodb(&ctx);
       
   186 	/*
       
   187 	 * Issuer and subject certs: both the target since it is self signed,
       
   188 	 * no request and no CRL
       
   189 	 */
       
   190 	X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
       
   191 	ex = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
       
   192 	if (!ex)
       
   193 		return (0);
       
   194 
       
   195 	X509_add_ext(cert, ex, -1);
       
   196 	X509_EXTENSION_free(ex);
       
   197 	return (1);
       
   198 }