1 /* Certificate creation. Demonstrates some certificate related
9 #include <openssl/pem.h>
10 #include <openssl/conf.h>
11 #include <openssl/x509v3.h>
12 #ifndef OPENSSL_NO_ENGINE
13 #include <openssl/engine.h>
16 int mkcert(X509
**x509p
, EVP_PKEY
**pkeyp
, int bits
, int serial
, int days
);
17 int add_ext(X509
*cert
, int nid
, char *value
);
19 int main(int argc
, char **argv
)
25 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
27 bio_err
=BIO_new_fp(stderr
, BIO_NOCLOSE
);
29 mkcert(&x509
,&pkey
,512,0,365);
31 RSA_print_fp(stdout
,pkey
->pkey
.rsa
,0);
32 X509_print_fp(stdout
,x509
);
34 PEM_write_PrivateKey(stdout
,pkey
,NULL
,NULL
,0,NULL
, NULL
);
35 PEM_write_X509(stdout
,x509
);
40 #ifndef OPENSSL_NO_ENGINE
43 CRYPTO_cleanup_all_ex_data();
45 CRYPTO_mem_leaks(bio_err
);
50 static void callback(int p
, int n
, void *arg
)
61 int mkcert(X509
**x509p
, EVP_PKEY
**pkeyp
, int bits
, int serial
, int days
)
68 if ((pkeyp
== NULL
) || (*pkeyp
== NULL
))
70 if ((pk
=EVP_PKEY_new()) == NULL
)
79 if ((x509p
== NULL
) || (*x509p
== NULL
))
81 if ((x
=X509_new()) == NULL
)
87 rsa
=RSA_generate_key(bits
,RSA_F4
,callback
,NULL
);
88 if (!EVP_PKEY_assign_RSA(pk
,rsa
))
95 X509_set_version(x
,2);
96 ASN1_INTEGER_set(X509_get_serialNumber(x
),serial
);
97 X509_gmtime_adj(X509_get_notBefore(x
),0);
98 X509_gmtime_adj(X509_get_notAfter(x
),(long)60*60*24*days
);
99 X509_set_pubkey(x
,pk
);
101 name
=X509_get_subject_name(x
);
103 /* This function creates and adds the entry, working out the
104 * correct string type and performing checks on its length.
105 * Normally we'd check the return value for errors...
107 X509_NAME_add_entry_by_txt(name
,"C",
108 MBSTRING_ASC
, "UK", -1, -1, 0);
109 X509_NAME_add_entry_by_txt(name
,"CN",
110 MBSTRING_ASC
, "OpenSSL Group", -1, -1, 0);
112 /* Its self signed so set the issuer name to be the same as the
115 X509_set_issuer_name(x
,name
);
117 /* Add various extensions: standard extensions */
118 add_ext(x
, NID_basic_constraints
, "critical,CA:TRUE");
119 add_ext(x
, NID_key_usage
, "critical,keyCertSign,cRLSign");
121 add_ext(x
, NID_subject_key_identifier
, "hash");
123 /* Some Netscape specific extensions */
124 add_ext(x
, NID_netscape_cert_type
, "sslCA");
126 add_ext(x
, NID_netscape_comment
, "example comment extension");
130 /* Maybe even add our own extension based on existing */
133 nid
= OBJ_create("1.2.3.4", "MyAlias", "My Test Alias Extension");
134 X509V3_EXT_add_alias(nid
, NID_netscape_comment
);
135 add_ext(x
, nid
, "example comment alias");
139 if (!X509_sign(x
,pk
,EVP_md5()))
149 /* Add extension using V3 code: we can set the config file as NULL
150 * because we wont reference any other sections.
153 int add_ext(X509
*cert
, int nid
, char *value
)
157 /* This sets the 'context' of the extensions. */
158 /* No configuration database */
159 X509V3_set_ctx_nodb(&ctx
);
160 /* Issuer and subject certs: both the target since it is self signed,
161 * no request and no CRL
163 X509V3_set_ctx(&ctx
, cert
, cert
, NULL
, NULL
, 0);
164 ex
= X509V3_EXT_conf_nid(NULL
, &ctx
, nid
, value
);
168 X509_add_ext(cert
,ex
,-1);
169 X509_EXTENSION_free(ex
);