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 X509V3_add_standard_extensions();
23 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
24 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE);
26 mkit(&x509,&pkey,512,0,365);
28 RSA_print_fp(stdout,pkey->pkey.rsa,0);
29 X509_print_fp(stdout,x509);
31 PEM_write_RSAPrivateKey(stdout,pkey->pkey.rsa,NULL,NULL,0,NULL);
32 PEM_write_X509(stdout,x509);
40 CRYPTO_mem_leaks(bio_err);
45 # define MS_CALLBACK _far _loadds
52 static void MS_CALLBACK callback(p, n, arg)
66 int mkit(x509p,pkeyp,bits,serial,days)
77 X509_NAME_ENTRY *ne=NULL;
78 X509_EXTENSION *ex=NULL;
81 if ((pkeyp == NULL) || (*pkeyp == NULL))
83 if ((pk=EVP_PKEY_new()) == NULL)
92 if ((x509p == NULL) || (*x509p == NULL))
94 if ((x=X509_new()) == NULL)
100 rsa=RSA_generate_key(bits,RSA_F4,callback,NULL);
101 if (!EVP_PKEY_assign_RSA(pk,rsa))
108 X509_set_version(x,3);
109 ASN1_INTEGER_set(X509_get_serialNumber(x),serial);
110 X509_gmtime_adj(X509_get_notBefore(x),0);
111 X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days);
112 X509_set_pubkey(x,pk);
114 name=X509_NAME_new();
116 ne=X509_NAME_ENTRY_create_by_NID(NULL,NID_countryName,
117 V_ASN1_APP_CHOOSE,"AU",-1);
118 X509_NAME_add_entry(name,ne,0,0);
120 X509_NAME_ENTRY_create_by_NID(&ne,NID_commonName,
121 V_ASN1_APP_CHOOSE,"Eric Young",-1);
122 X509_NAME_add_entry(name,ne,1,0);
124 /* finished with structure */
125 X509_NAME_ENTRY_free(ne);
127 X509_set_subject_name(x,name);
128 X509_set_issuer_name(x,name);
130 /* finished with structure */
131 X509_NAME_free(name);
133 /* Add extension using V3 code: we can set the config file as NULL
134 * because we wont reference any other sections. We can also set
135 * the context to NULL because none of these extensions below will need
139 ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_cert_type, "server");
140 X509_add_ext(x,ex,-1);
142 ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_comment,
143 "example comment extension");
144 X509_add_ext(x,ex,-1);
146 ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_ssl_server_name,
149 X509_add_ext(x,ex,-1);
152 /* might want something like this too.... */
153 ex = X509V3_EXT_conf_nid(NULL, NULL, NID_basic_constraints,
157 X509_add_ext(x,ex,-1);
160 if (!X509_sign(x,pk,EVP_md5()))