Survive pedanticism.
[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/buffer.h>
8 #include <openssl/crypto.h>
9 #include <openssl/objects.h>
10 #include <openssl/asn1.h>
11 #include <openssl/evp.h>
12 #include <openssl/x509.h>
13 #include <openssl/pem.h>
14
15 int mkit(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days);
16
17 int main()
18         {
19         BIO *bio_err;
20         X509 *x509=NULL;
21         EVP_PKEY *pkey=NULL;
22
23         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
24
25         X509v3_add_netscape_extensions();
26
27         if ((bio_err=BIO_new(BIO_s_file())) != NULL)
28                 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE);
29
30         mkit(&x509,&pkey,512,0,365);
31
32         RSA_print_fp(stdout,pkey->pkey.rsa,0);
33         X509_print_fp(stdout,x509);
34
35         PEM_write_RSAPrivateKey(stdout,pkey->pkey.rsa,NULL,NULL,0,NULL);
36         PEM_write_X509(stdout,x509);
37
38         X509_free(x509);
39         EVP_PKEY_free(pkey);
40         BIO_free(bio_err);
41
42         X509_cleanup_extensions();
43
44         CRYPTO_mem_leaks(bio_err);
45         return(0);
46         }
47
48 #ifdef WIN16
49 #  define MS_CALLBACK   _far _loadds
50 #  define MS_FAR        _far
51 #else
52 #  define MS_CALLBACK
53 #  define MS_FAR
54 #endif
55
56 static void MS_CALLBACK callback(p, n)
57 int p;
58 int n;
59         {
60         char c='B';
61
62         if (p == 0) c='.';
63         if (p == 1) c='+';
64         if (p == 2) c='*';
65         if (p == 3) c='\n';
66         fputc(c,stderr);
67         }
68
69 int mkit(x509p,pkeyp,bits,serial,days)
70 X509 **x509p;
71 EVP_PKEY **pkeyp;
72 int bits;
73 int serial;
74 int days;
75         {
76         X509 *x;
77         EVP_PKEY *pk;
78         RSA *rsa;
79         char *s;
80         X509_NAME *name=NULL;
81         X509_NAME_ENTRY *ne=NULL;
82         X509_EXTENSION *ex=NULL;
83         ASN1_OCTET_STRING *data=NULL;
84
85         
86         if ((pkeyp == NULL) || (*pkeyp == NULL))
87                 {
88                 if ((pk=EVP_PKEY_new()) == NULL)
89                         {
90                         abort(); 
91                         return(0);
92                         }
93                 }
94         else
95                 pk= *pkeyp;
96
97         if ((x509p == NULL) || (*x509p == NULL))
98                 {
99                 if ((x=X509_new()) == NULL)
100                         goto err;
101                 }
102         else
103                 x= *x509p;
104
105         rsa=RSA_generate_key(bits,RSA_F4,callback,NULL);
106         if (!EVP_PKEY_assign_RSA(pk,rsa))
107                 {
108                 abort();
109                 goto err;
110                 }
111         rsa=NULL;
112
113         X509_set_version(x,3);
114         ASN1_INTEGER_set(X509_get_serialNumber(x),serial);
115         X509_gmtime_adj(X509_get_notBefore(x),0);
116         X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days);
117         X509_set_pubkey(x,pk);
118
119         name=X509_NAME_new();
120
121         ne=X509_NAME_ENTRY_create_by_NID(NULL,NID_countryName,
122                 V_ASN1_APP_CHOOSE,"AU",-1);
123         X509_NAME_add_entry(name,ne,0,0);
124
125         X509_NAME_ENTRY_create_by_NID(&ne,NID_commonName,
126                 V_ASN1_APP_CHOOSE,"Eric Young",-1);
127         X509_NAME_add_entry(name,ne,1,0);
128
129         /* finished with structure */
130         X509_NAME_ENTRY_free(ne);
131
132         X509_set_subject_name(x,name);
133         X509_set_issuer_name(x,name);
134
135         /* finished with structure */
136         X509_NAME_free(name);
137
138         data=X509v3_pack_string(NULL,V_ASN1_BIT_STRING,
139                 "\001",1);
140         ex=X509_EXTENSION_create_by_NID(NULL,NID_netscape_cert_type,0,data);
141         X509_add_ext(x,ex,-1);
142
143         X509v3_pack_string(&data,V_ASN1_IA5STRING,
144                 "example comment extension",-1);
145         X509_EXTENSION_create_by_NID(&ex,NID_netscape_comment,0,data);
146         X509_add_ext(x,ex,-1);
147
148         X509v3_pack_string(&data,V_ASN1_BIT_STRING,
149                 "www.cryptsoft.com",-1);
150         X509_EXTENSION_create_by_NID(&ex,NID_netscape_ssl_server_name,0,data);
151         X509_add_ext(x,ex,-1);
152         
153         X509_EXTENSION_free(ex);
154         ASN1_OCTET_STRING_free(data);
155
156         if (!X509_sign(x,pk,EVP_md5()))
157                 goto err;
158
159         *x509p=x;
160         *pkeyp=pk;
161         return(1);
162 err:
163         return(0);
164         }
165                          
166
167
168