1 /* Certificate request creation. Demonstrates some request related
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 mkreq(X509_REQ
**x509p
, EVP_PKEY
**pkeyp
, int bits
, int serial
, int days
);
16 int add_ext(STACK_OF(X509_REQUEST
) *sk
, int nid
, char *value
);
18 int main(int argc
, char **argv
)
24 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
26 bio_err
=BIO_new_fp(stderr
, BIO_NOCLOSE
);
28 mkreq(&req
,&pkey
,512,0,365);
30 RSA_print_fp(stdout
,pkey
->pkey
.rsa
,0);
31 X509_REQ_print_fp(stdout
,req
);
33 PEM_write_X509_REQ(stdout
,req
);
38 #ifndef OPENSSL_NO_ENGINE
41 CRYPTO_cleanup_all_ex_data();
43 CRYPTO_mem_leaks(bio_err
);
48 static void callback(int p
, int n
, void *arg
)
59 int mkreq(X509_REQ
**req
, EVP_PKEY
**pkeyp
, int bits
, int serial
, int days
)
65 STACK_OF(X509_EXTENSION
) *exts
= NULL
;
67 if ((pk
=EVP_PKEY_new()) == NULL
)
70 if ((x
=X509_REQ_new()) == NULL
)
73 rsa
=RSA_generate_key(bits
,RSA_F4
,callback
,NULL
);
74 if (!EVP_PKEY_assign_RSA(pk
,rsa
))
79 X509_REQ_set_pubkey(x
,pk
);
81 name
=X509_REQ_get_subject_name(x
);
83 /* This function creates and adds the entry, working out the
84 * correct string type and performing checks on its length.
85 * Normally we'd check the return value for errors...
87 X509_NAME_add_entry_by_txt(name
,"C",
88 MBSTRING_ASC
, "UK", -1, -1, 0);
89 X509_NAME_add_entry_by_txt(name
,"CN",
90 MBSTRING_ASC
, "OpenSSL Group", -1, -1, 0);
92 #ifdef REQUEST_EXTENSIONS
93 /* Certificate requests can contain extensions, which can be used
94 * to indicate the extensions the requestor would like added to
95 * their certificate. CAs might ignore them however or even choke
96 * if they are present.
99 /* For request extensions they are all packed in a single attribute.
100 * We save them in a STACK and add them all at once later...
103 exts
= sk_X509_EXTENSION_new_null();
104 /* Standard extenions */
106 add_ext(exts
, NID_key_usage
, "critical,digitalSignature,keyEncipherment");
108 /* This is a typical use for request extensions: requesting a value for
109 * subject alternative name.
112 add_ext(exts
, NID_subject_alt_name
, "email:steve@openssl.org");
114 /* Some Netscape specific extensions */
115 add_ext(exts
, NID_netscape_cert_type
, "client,email");
120 /* Maybe even add our own extension based on existing */
123 nid
= OBJ_create("1.2.3.4", "MyAlias", "My Test Alias Extension");
124 X509V3_EXT_add_alias(nid
, NID_netscape_comment
);
125 add_ext(x
, nid
, "example comment alias");
129 /* Now we've created the extensions we add them to the request */
131 X509_REQ_add_extensions(x
, exts
);
133 sk_X509_EXTENSION_pop_free(exts
, X509_EXTENSION_free
);
137 if (!X509_REQ_sign(x
,pk
,EVP_sha1()))
147 /* Add extension using V3 code: we can set the config file as NULL
148 * because we wont reference any other sections.
151 int add_ext(STACK_OF(X509_REQUEST
) *sk
, int nid
, char *value
)
154 ex
= X509V3_EXT_conf_nid(NULL
, NULL
, nid
, value
);
157 sk_X509_EXTENSION_push(sk
, ex
);