2 * Certificate creation. Demonstrates some certificate related operations.
8 #include <openssl/pem.h>
9 #include <openssl/conf.h>
10 #include <openssl/x509v3.h>
11 #ifndef OPENSSL_NO_ENGINE
12 # include <openssl/engine.h>
15 int mkcert(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days);
16 int add_ext(X509 *cert, int nid, char *value);
18 int main(int argc, char **argv)
22 EVP_PKEY *pkey = NULL;
24 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
26 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
28 mkcert(&x509, &pkey, 512, 0, 365);
30 RSA_print_fp(stdout, pkey->pkey.rsa, 0);
31 X509_print_fp(stdout, x509);
33 PEM_write_PrivateKey(stdout, pkey, NULL, NULL, 0, NULL, NULL);
34 PEM_write_X509(stdout, x509);
39 #ifndef OPENSSL_NO_ENGINE
42 CRYPTO_cleanup_all_ex_data();
44 CRYPTO_mem_leaks(bio_err);
49 static void callback(int p, int n, void *arg)
64 int mkcert(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days)
69 X509_NAME *name = NULL;
71 if ((pkeyp == NULL) || (*pkeyp == NULL)) {
72 if ((pk = EVP_PKEY_new()) == NULL) {
79 if ((x509p == NULL) || (*x509p == NULL)) {
80 if ((x = X509_new()) == NULL)
85 rsa = RSA_generate_key(bits, RSA_F4, callback, NULL);
86 if (!EVP_PKEY_assign_RSA(pk, rsa)) {
92 X509_set_version(x, 2);
93 ASN1_INTEGER_set(X509_get_serialNumber(x), serial);
94 X509_gmtime_adj(X509_get_notBefore(x), 0);
95 X509_gmtime_adj(X509_get_notAfter(x), (long)60 * 60 * 24 * days);
96 X509_set_pubkey(x, pk);
98 name = X509_get_subject_name(x);
101 * This function creates and adds the entry, working out the correct
102 * string type and performing checks on its length. Normally we'd check
103 * the return value for errors...
105 X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, "UK", -1, -1, 0);
106 X509_NAME_add_entry_by_txt(name, "CN",
107 MBSTRING_ASC, "OpenSSL Group", -1, -1, 0);
110 * Its self signed so set the issuer name to be the same as the subject.
112 X509_set_issuer_name(x, name);
114 /* Add various extensions: standard extensions */
115 add_ext(x, NID_basic_constraints, "critical,CA:TRUE");
116 add_ext(x, NID_key_usage, "critical,keyCertSign,cRLSign");
118 add_ext(x, NID_subject_key_identifier, "hash");
120 /* Some Netscape specific extensions */
121 add_ext(x, NID_netscape_cert_type, "sslCA");
123 add_ext(x, NID_netscape_comment, "example comment extension");
126 /* Maybe even add our own extension based on existing */
129 nid = OBJ_create("1.2.3.4", "MyAlias", "My Test Alias Extension");
130 X509V3_EXT_add_alias(nid, NID_netscape_comment);
131 add_ext(x, nid, "example comment alias");
135 if (!X509_sign(x, pk, EVP_sha1()))
146 * Add extension using V3 code: we can set the config file as NULL because we
147 * wont reference any other sections.
150 int add_ext(X509 *cert, int nid, char *value)
154 /* This sets the 'context' of the extensions. */
155 /* No configuration database */
156 X509V3_set_ctx_nodb(&ctx);
158 * Issuer and subject certs: both the target since it is self signed, no
161 X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
162 ex = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
166 X509_add_ext(cert, ex, -1);
167 X509_EXTENSION_free(ex);