2 * Certificate request creation. Demonstrates some request related
9 #include <openssl/pem.h>
10 #include <openssl/conf.h>
11 #include <openssl/x509.h>
12 #include <openssl/x509v3.h>
13 #ifndef OPENSSL_NO_ENGINE
14 # include <openssl/engine.h>
17 int mkreq(X509_REQ
**x509p
, EVP_PKEY
**pkeyp
, int bits
, int serial
, int days
);
18 int add_ext(STACK_OF(X509_EXTENSION
) *sk
, int nid
, char *value
);
20 int main(int argc
, char **argv
)
24 EVP_PKEY
*pkey
= NULL
;
26 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
28 bio_err
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
30 mkreq(&req
, &pkey
, 512, 0, 365);
32 RSA_print_fp(stdout
, pkey
->pkey
.rsa
, 0);
33 X509_REQ_print_fp(stdout
, req
);
35 PEM_write_X509_REQ(stdout
, req
);
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
)
65 int mkreq(X509_REQ
**req
, EVP_PKEY
**pkeyp
, int bits
, int serial
, int days
)
70 X509_NAME
*name
= NULL
;
71 STACK_OF(X509_EXTENSION
) *exts
= NULL
;
73 if ((pk
= EVP_PKEY_new()) == NULL
)
76 if ((x
= X509_REQ_new()) == NULL
)
79 rsa
= RSA_generate_key(bits
, RSA_F4
, callback
, NULL
);
80 if (!EVP_PKEY_assign_RSA(pk
, rsa
))
85 X509_REQ_set_pubkey(x
, pk
);
87 name
= X509_REQ_get_subject_name(x
);
90 * This function creates and adds the entry, working out the correct
91 * string type and performing checks on its length. Normally we'd check
92 * the return value for errors...
94 X509_NAME_add_entry_by_txt(name
, "C", MBSTRING_ASC
, "UK", -1, -1, 0);
95 X509_NAME_add_entry_by_txt(name
, "CN",
96 MBSTRING_ASC
, "OpenSSL Group", -1, -1, 0);
98 #ifdef REQUEST_EXTENSIONS
100 * Certificate requests can contain extensions, which can be used to
101 * indicate the extensions the requestor would like added to their
102 * certificate. CAs might ignore them however or even choke if they are
107 * For request extensions they are all packed in a single attribute. We
108 * save them in a STACK and add them all at once later...
111 exts
= sk_X509_EXTENSION_new_null();
112 /* Standard extenions */
114 add_ext(exts
, NID_key_usage
, "critical,digitalSignature,keyEncipherment");
117 * This is a typical use for request extensions: requesting a value for
118 * subject alternative name.
121 add_ext(exts
, NID_subject_alt_name
, "email:steve@openssl.org");
123 /* Some Netscape specific extensions */
124 add_ext(exts
, NID_netscape_cert_type
, "client,email");
127 /* Maybe even add our own extension based on existing */
130 nid
= OBJ_create("1.2.3.4", "MyAlias", "My Test Alias Extension");
131 X509V3_EXT_add_alias(nid
, NID_netscape_comment
);
132 add_ext(x
, nid
, "example comment alias");
136 /* Now we've created the extensions we add them to the request */
138 X509_REQ_add_extensions(x
, exts
);
140 sk_X509_EXTENSION_pop_free(exts
, X509_EXTENSION_free
);
144 if (!X509_REQ_sign(x
, pk
, EVP_sha1()))
155 * Add extension using V3 code: we can set the config file as NULL because we
156 * wont reference any other sections.
159 int add_ext(STACK_OF(X509_EXTENSION
) *sk
, int nid
, char *value
)
162 ex
= X509V3_EXT_conf_nid(NULL
, NULL
, nid
, value
);
165 sk_X509_EXTENSION_push(sk
, ex
);