Yet more comments
[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         bio_err=BIO_new_fp(stderr, BIO_NOCLOSE);
22
23         mkit(&x509,&pkey,512,0,365);
24
25         RSA_print_fp(stdout,pkey->pkey.rsa,0);
26         X509_print_fp(stdout,x509);
27
28         PEM_write_PrivateKey(stdout,pkey,NULL,NULL,0,NULL, NULL);
29         PEM_write_X509(stdout,x509);
30
31         X509_free(x509);
32         EVP_PKEY_free(pkey);
33
34 #ifdef CUSTOM_EXT
35         /* Only needed if we add objects or custom extensions */
36         X509V3_EXT_cleanup();
37         OBJ_cleanup();
38 #endif
39
40         CRYPTO_mem_leaks(bio_err);
41         BIO_free(bio_err);
42         return(0);
43         }
44
45 static void callback(p, n, arg)
46 int p;
47 int n;
48 void *arg;
49         {
50         char c='B';
51
52         if (p == 0) c='.';
53         if (p == 1) c='+';
54         if (p == 2) c='*';
55         if (p == 3) c='\n';
56         fputc(c,stderr);
57         }
58
59 int mkit(x509p,pkeyp,bits,serial,days)
60 X509 **x509p;
61 EVP_PKEY **pkeyp;
62 int bits;
63 int serial;
64 int days;
65         {
66         X509 *x;
67         EVP_PKEY *pk;
68         RSA *rsa;
69         X509_NAME *name=NULL;
70         X509_NAME_ENTRY *ne=NULL;
71         X509_EXTENSION *ex=NULL;
72
73         
74         if ((pkeyp == NULL) || (*pkeyp == NULL))
75                 {
76                 if ((pk=EVP_PKEY_new()) == NULL)
77                         {
78                         abort(); 
79                         return(0);
80                         }
81                 }
82         else
83                 pk= *pkeyp;
84
85         if ((x509p == NULL) || (*x509p == NULL))
86                 {
87                 if ((x=X509_new()) == NULL)
88                         goto err;
89                 }
90         else
91                 x= *x509p;
92
93         rsa=RSA_generate_key(bits,RSA_F4,callback,NULL);
94         if (!EVP_PKEY_assign_RSA(pk,rsa))
95                 {
96                 abort();
97                 goto err;
98                 }
99         rsa=NULL;
100
101         X509_set_version(x,3);
102         ASN1_INTEGER_set(X509_get_serialNumber(x),serial);
103         X509_gmtime_adj(X509_get_notBefore(x),0);
104         X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days);
105         X509_set_pubkey(x,pk);
106
107         name=X509_get_subject_name(x);
108
109         /* This function creates and adds the entry, working out the
110          * correct string type and performing checks on its length.
111          * Normally we'd check the return value for errors...
112          */
113         X509_NAME_add_entry_by_txt(name,"C",
114                                 MBSTRING_ASC, "UK", -1, -1, 0);
115         X509_NAME_add_entry_by_txt(name,"CN",
116                                 MBSTRING_ASC, "OpenSSL Group", -1, -1, 0);
117
118         X509_set_issuer_name(x,name);
119
120         /* Add extension using V3 code: we can set the config file as NULL
121          * because we wont reference any other sections. We can also set
122          * the context to NULL because none of these extensions below will need
123          * to access it.
124          */
125
126         ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_cert_type, "server");
127         X509_add_ext(x,ex,-1);
128         X509_EXTENSION_free(ex);
129
130         ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_comment,
131                                                 "example comment extension");
132         X509_add_ext(x,ex,-1);
133         X509_EXTENSION_free(ex);
134
135         ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_ssl_server_name,
136                                                         "www.openssl.org");
137
138         X509_add_ext(x,ex,-1);
139         X509_EXTENSION_free(ex);
140
141 #if 0
142         /* might want something like this too.... */
143         ex = X509V3_EXT_conf_nid(NULL, NULL, NID_basic_constraints,
144                                                         "critical,CA:TRUE");
145
146
147         X509_add_ext(x,ex,-1);
148         X509_EXTENSION_free(ex);
149 #endif
150
151 #ifdef CUSTOM_EXT
152         /* Maybe even add our own extension based on existing */
153         {
154                 int nid;
155                 nid = OBJ_create("1.2.3.4", "MyAlias", "My Test Alias Extension");
156                 X509V3_EXT_add_alias(nid, NID_netscape_comment);
157                 ex = X509V3_EXT_conf_nid(NULL, NULL, nid,
158                                                 "example comment alias");
159                 X509_add_ext(x,ex,-1);
160                 X509_EXTENSION_free(ex);
161         }
162 #endif
163         
164         if (!X509_sign(x,pk,EVP_md5()))
165                 goto err;
166
167         *x509p=x;
168         *pkeyp=pk;
169         return(1);
170 err:
171         return(0);
172         }