2 /* cc -o ssdemo -I../include selfsign.c ../libcrypto.a */
7 #include <openssl/pem.h>
8 #include <openssl/conf.h>
9 #include <openssl/x509v3.h>
11 int mkit(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days);
19 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
21 bio_err=BIO_new_fp(stderr, BIO_NOCLOSE);
23 mkit(&x509,&pkey,512,0,365);
25 RSA_print_fp(stdout,pkey->pkey.rsa,0);
26 X509_print_fp(stdout,x509);
28 PEM_write_PrivateKey(stdout,pkey,NULL,NULL,0,NULL, NULL);
29 PEM_write_X509(stdout,x509);
35 /* Only needed if we add objects or custom extensions */
40 CRYPTO_mem_leaks(bio_err);
46 # define MS_CALLBACK _far _loadds
53 static void MS_CALLBACK callback(p, n, arg)
67 int mkit(x509p,pkeyp,bits,serial,days)
78 X509_NAME_ENTRY *ne=NULL;
79 X509_EXTENSION *ex=NULL;
82 if ((pkeyp == NULL) || (*pkeyp == NULL))
84 if ((pk=EVP_PKEY_new()) == NULL)
93 if ((x509p == NULL) || (*x509p == NULL))
95 if ((x=X509_new()) == NULL)
101 rsa=RSA_generate_key(bits,RSA_F4,callback,NULL);
102 if (!EVP_PKEY_assign_RSA(pk,rsa))
109 X509_set_version(x,3);
110 ASN1_INTEGER_set(X509_get_serialNumber(x),serial);
111 X509_gmtime_adj(X509_get_notBefore(x),0);
112 X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days);
113 X509_set_pubkey(x,pk);
115 name=X509_get_subject_name(x);
117 /* This function creates and adds the entry, working out the
118 * correct string type and performing checks on its length.
119 * Normally we'd check the return value for errors...
121 X509_NAME_add_entry_by_txt(name,"C",
122 MBSTRING_ASC, "UK", -1, -1, 0);
123 X509_NAME_add_entry_by_txt(name,"CN",
124 MBSTRING_ASC, "OpenSSL Group", -1, -1, 0);
126 X509_set_issuer_name(x,name);
128 /* Add extension using V3 code: we can set the config file as NULL
129 * because we wont reference any other sections. We can also set
130 * the context to NULL because none of these extensions below will need
134 ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_cert_type, "server");
135 X509_add_ext(x,ex,-1);
136 X509_EXTENSION_free(ex);
138 ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_comment,
139 "example comment extension");
140 X509_add_ext(x,ex,-1);
141 X509_EXTENSION_free(ex);
143 ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_ssl_server_name,
146 X509_add_ext(x,ex,-1);
147 X509_EXTENSION_free(ex);
150 /* might want something like this too.... */
151 ex = X509V3_EXT_conf_nid(NULL, NULL, NID_basic_constraints,
155 X509_add_ext(x,ex,-1);
156 X509_EXTENSION_free(ex);
160 /* Maybe even add our own extension based on existing */
163 nid = OBJ_create("1.2.3.4", "MyAlias", "My Test Alias Extension");
164 X509V3_EXT_add_alias(nid, NID_netscape_comment);
165 ex = X509V3_EXT_conf_nid(NULL, NULL, nid,
166 "example comment alias");
167 X509_add_ext(x,ex,-1);
168 X509_EXTENSION_free(ex);
172 if (!X509_sign(x,pk,EVP_md5()))