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