1 /* Certificate request creation. Demonstrates some request related
8 #include <openssl/pem.h>
9 #include <openssl/conf.h>
10 #include <openssl/x509.h>
11 #include <openssl/x509v3.h>
12 #ifndef OPENSSL_NO_ENGINE
13 #include <openssl/engine.h>
16 int mkreq(X509_REQ **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days);
17 int add_ext(STACK_OF(X509_EXTENSION) *sk, 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 mkreq(&req,&pkey,512,0,365);
31 RSA_print_fp(stdout,pkey->pkey.rsa,0);
32 X509_REQ_print_fp(stdout,req);
34 PEM_write_X509_REQ(stdout,req);
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)
60 int mkreq(X509_REQ **req, EVP_PKEY **pkeyp, int bits, int serial, int days)
66 STACK_OF(X509_EXTENSION) *exts = NULL;
68 if ((pk=EVP_PKEY_new()) == NULL)
71 if ((x=X509_REQ_new()) == NULL)
74 rsa=RSA_generate_key(bits,RSA_F4,callback,NULL);
75 if (!EVP_PKEY_assign_RSA(pk,rsa))
80 X509_REQ_set_pubkey(x,pk);
82 name=X509_REQ_get_subject_name(x);
84 /* This function creates and adds the entry, working out the
85 * correct string type and performing checks on its length.
86 * Normally we'd check the return value for errors...
88 X509_NAME_add_entry_by_txt(name,"C",
89 MBSTRING_ASC, "UK", -1, -1, 0);
90 X509_NAME_add_entry_by_txt(name,"CN",
91 MBSTRING_ASC, "OpenSSL Group", -1, -1, 0);
93 #ifdef REQUEST_EXTENSIONS
94 /* Certificate requests can contain extensions, which can be used
95 * to indicate the extensions the requestor would like added to
96 * their certificate. CAs might ignore them however or even choke
97 * if they are present.
100 /* For request extensions they are all packed in a single attribute.
101 * We save them in a STACK and add them all at once later...
104 exts = sk_X509_EXTENSION_new_null();
105 /* Standard extenions */
107 add_ext(exts, NID_key_usage, "critical,digitalSignature,keyEncipherment");
109 /* This is a typical use for request extensions: requesting a value for
110 * subject alternative name.
113 add_ext(exts, NID_subject_alt_name, "email:steve@openssl.org");
115 /* Some Netscape specific extensions */
116 add_ext(exts, NID_netscape_cert_type, "client,email");
121 /* Maybe even add our own extension based on existing */
124 nid = OBJ_create("1.2.3.4", "MyAlias", "My Test Alias Extension");
125 X509V3_EXT_add_alias(nid, NID_netscape_comment);
126 add_ext(x, nid, "example comment alias");
130 /* Now we've created the extensions we add them to the request */
132 X509_REQ_add_extensions(x, exts);
134 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
138 if (!X509_REQ_sign(x,pk,EVP_sha1()))
148 /* Add extension using V3 code: we can set the config file as NULL
149 * because we wont reference any other sections.
152 int add_ext(STACK_OF(X509_EXTENSION) *sk, int nid, char *value)
155 ex = X509V3_EXT_conf_nid(NULL, NULL, nid, value);
158 sk_X509_EXTENSION_push(sk, ex);