s_client and s_server now have their own man pages.
[openssl.git] / demos / selfsign.c
1 /* NOCW */
2 /* cc -o ssdemo -I../include selfsign.c ../libcrypto.a */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include <openssl/pem.h>
8 #include <openssl/conf.h>
9 #include <openssl/x509v3.h>
10
11 int mkit(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days);
12
13 int main()
14         {
15         BIO *bio_err;
16         X509 *x509=NULL;
17         EVP_PKEY *pkey=NULL;
18
19         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
20
21         X509V3_add_standard_extensions();
22
23         if ((bio_err=BIO_new(BIO_s_file())) != NULL)
24                 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE);
25
26         mkit(&x509,&pkey,512,0,365);
27
28         RSA_print_fp(stdout,pkey->pkey.rsa,0);
29         X509_print_fp(stdout,x509);
30
31         PEM_write_RSAPrivateKey(stdout,pkey->pkey.rsa,NULL,NULL,0,NULL);
32         PEM_write_X509(stdout,x509);
33
34         X509_free(x509);
35         EVP_PKEY_free(pkey);
36         BIO_free(bio_err);
37
38         X509V3_EXT_cleanup();
39
40         CRYPTO_mem_leaks(bio_err);
41         return(0);
42         }
43
44 #ifdef WIN16
45 #  define MS_CALLBACK   _far _loadds
46 #  define MS_FAR        _far
47 #else
48 #  define MS_CALLBACK
49 #  define MS_FAR
50 #endif
51
52 static void MS_CALLBACK callback(p, n, arg)
53 int p;
54 int n;
55 void *arg;
56         {
57         char c='B';
58
59         if (p == 0) c='.';
60         if (p == 1) c='+';
61         if (p == 2) c='*';
62         if (p == 3) c='\n';
63         fputc(c,stderr);
64         }
65
66 int mkit(x509p,pkeyp,bits,serial,days)
67 X509 **x509p;
68 EVP_PKEY **pkeyp;
69 int bits;
70 int serial;
71 int days;
72         {
73         X509 *x;
74         EVP_PKEY *pk;
75         RSA *rsa;
76         X509_NAME *name=NULL;
77         X509_NAME_ENTRY *ne=NULL;
78         X509_EXTENSION *ex=NULL;
79
80         
81         if ((pkeyp == NULL) || (*pkeyp == NULL))
82                 {
83                 if ((pk=EVP_PKEY_new()) == NULL)
84                         {
85                         abort(); 
86                         return(0);
87                         }
88                 }
89         else
90                 pk= *pkeyp;
91
92         if ((x509p == NULL) || (*x509p == NULL))
93                 {
94                 if ((x=X509_new()) == NULL)
95                         goto err;
96                 }
97         else
98                 x= *x509p;
99
100         rsa=RSA_generate_key(bits,RSA_F4,callback,NULL);
101         if (!EVP_PKEY_assign_RSA(pk,rsa))
102                 {
103                 abort();
104                 goto err;
105                 }
106         rsa=NULL;
107
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);
113
114         name=X509_NAME_new();
115
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);
119
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);
123
124         /* finished with structure */
125         X509_NAME_ENTRY_free(ne);
126
127         X509_set_subject_name(x,name);
128         X509_set_issuer_name(x,name);
129
130         /* finished with structure */
131         X509_NAME_free(name);
132
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
136          * to access it.
137          */
138
139         ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_cert_type, "server");
140         X509_add_ext(x,ex,-1);
141
142         ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_comment,
143                                                 "example comment extension");
144         X509_add_ext(x,ex,-1);
145
146         ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_ssl_server_name,
147                                                         "www.openssl.org");
148
149         X509_add_ext(x,ex,-1);
150
151 #if 0
152         /* might want something like this too.... */
153         ex = X509V3_EXT_conf_nid(NULL, NULL, NID_basic_constraints,
154                                                         "critical,CA:TRUE");
155
156
157         X509_add_ext(x,ex,-1);
158 #endif
159         
160         if (!X509_sign(x,pk,EVP_md5()))
161                 goto err;
162
163         *x509p=x;
164         *pkeyp=pk;
165         return(1);
166 err:
167         return(0);
168         }