1ebfedea0SLionel Sambuc /* apps/sess_id.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc
59ebfedea0SLionel Sambuc #include <stdio.h>
60ebfedea0SLionel Sambuc #include <stdlib.h>
61ebfedea0SLionel Sambuc #include <string.h>
62ebfedea0SLionel Sambuc #include "apps.h"
63ebfedea0SLionel Sambuc #include <openssl/bio.h>
64ebfedea0SLionel Sambuc #include <openssl/err.h>
65ebfedea0SLionel Sambuc #include <openssl/x509.h>
66ebfedea0SLionel Sambuc #include <openssl/pem.h>
67ebfedea0SLionel Sambuc #include <openssl/ssl.h>
68ebfedea0SLionel Sambuc
69ebfedea0SLionel Sambuc #undef PROG
70ebfedea0SLionel Sambuc #define PROG sess_id_main
71ebfedea0SLionel Sambuc
72ebfedea0SLionel Sambuc static const char *sess_id_usage[] = {
73ebfedea0SLionel Sambuc "usage: sess_id args\n",
74ebfedea0SLionel Sambuc "\n",
75ebfedea0SLionel Sambuc " -inform arg - input format - default PEM (DER or PEM)\n",
76ebfedea0SLionel Sambuc " -outform arg - output format - default PEM\n",
77ebfedea0SLionel Sambuc " -in arg - input file - default stdin\n",
78ebfedea0SLionel Sambuc " -out arg - output file - default stdout\n",
79ebfedea0SLionel Sambuc " -text - print ssl session id details\n",
80ebfedea0SLionel Sambuc " -cert - output certificate \n",
81ebfedea0SLionel Sambuc " -noout - no CRL output\n",
82ebfedea0SLionel Sambuc " -context arg - set the session ID context\n",
83ebfedea0SLionel Sambuc NULL
84ebfedea0SLionel Sambuc };
85ebfedea0SLionel Sambuc
86ebfedea0SLionel Sambuc static SSL_SESSION *load_sess_id(char *file, int format);
87ebfedea0SLionel Sambuc
88ebfedea0SLionel Sambuc int MAIN(int, char **);
89ebfedea0SLionel Sambuc
MAIN(int argc,char ** argv)90ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
91ebfedea0SLionel Sambuc {
92ebfedea0SLionel Sambuc SSL_SESSION *x = NULL;
93ebfedea0SLionel Sambuc X509 *peer = NULL;
94ebfedea0SLionel Sambuc int ret = 1, i, num, badops = 0;
95ebfedea0SLionel Sambuc BIO *out = NULL;
96ebfedea0SLionel Sambuc int informat, outformat;
97ebfedea0SLionel Sambuc char *infile = NULL, *outfile = NULL, *context = NULL;
98ebfedea0SLionel Sambuc int cert = 0, noout = 0, text = 0;
99ebfedea0SLionel Sambuc const char **pp;
100ebfedea0SLionel Sambuc
101ebfedea0SLionel Sambuc apps_startup();
102ebfedea0SLionel Sambuc
103ebfedea0SLionel Sambuc if (bio_err == NULL)
104ebfedea0SLionel Sambuc if ((bio_err = BIO_new(BIO_s_file())) != NULL)
105ebfedea0SLionel Sambuc BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
106ebfedea0SLionel Sambuc
107ebfedea0SLionel Sambuc informat = FORMAT_PEM;
108ebfedea0SLionel Sambuc outformat = FORMAT_PEM;
109ebfedea0SLionel Sambuc
110ebfedea0SLionel Sambuc argc--;
111ebfedea0SLionel Sambuc argv++;
112ebfedea0SLionel Sambuc num = 0;
113*0a6a1f1dSLionel Sambuc while (argc >= 1) {
114*0a6a1f1dSLionel Sambuc if (strcmp(*argv, "-inform") == 0) {
115*0a6a1f1dSLionel Sambuc if (--argc < 1)
116*0a6a1f1dSLionel Sambuc goto bad;
117ebfedea0SLionel Sambuc informat = str2fmt(*(++argv));
118*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-outform") == 0) {
119*0a6a1f1dSLionel Sambuc if (--argc < 1)
120*0a6a1f1dSLionel Sambuc goto bad;
121ebfedea0SLionel Sambuc outformat = str2fmt(*(++argv));
122*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-in") == 0) {
123*0a6a1f1dSLionel Sambuc if (--argc < 1)
124*0a6a1f1dSLionel Sambuc goto bad;
125ebfedea0SLionel Sambuc infile = *(++argv);
126*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-out") == 0) {
127*0a6a1f1dSLionel Sambuc if (--argc < 1)
128*0a6a1f1dSLionel Sambuc goto bad;
129ebfedea0SLionel Sambuc outfile = *(++argv);
130*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-text") == 0)
131ebfedea0SLionel Sambuc text = ++num;
132ebfedea0SLionel Sambuc else if (strcmp(*argv, "-cert") == 0)
133ebfedea0SLionel Sambuc cert = ++num;
134ebfedea0SLionel Sambuc else if (strcmp(*argv, "-noout") == 0)
135ebfedea0SLionel Sambuc noout = ++num;
136*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-context") == 0) {
137*0a6a1f1dSLionel Sambuc if (--argc < 1)
138*0a6a1f1dSLionel Sambuc goto bad;
139ebfedea0SLionel Sambuc context = *++argv;
140*0a6a1f1dSLionel Sambuc } else {
141ebfedea0SLionel Sambuc BIO_printf(bio_err, "unknown option %s\n", *argv);
142ebfedea0SLionel Sambuc badops = 1;
143ebfedea0SLionel Sambuc break;
144ebfedea0SLionel Sambuc }
145ebfedea0SLionel Sambuc argc--;
146ebfedea0SLionel Sambuc argv++;
147ebfedea0SLionel Sambuc }
148ebfedea0SLionel Sambuc
149*0a6a1f1dSLionel Sambuc if (badops) {
150ebfedea0SLionel Sambuc bad:
151ebfedea0SLionel Sambuc for (pp = sess_id_usage; (*pp != NULL); pp++)
152ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s", *pp);
153ebfedea0SLionel Sambuc goto end;
154ebfedea0SLionel Sambuc }
155ebfedea0SLionel Sambuc
156ebfedea0SLionel Sambuc ERR_load_crypto_strings();
157ebfedea0SLionel Sambuc x = load_sess_id(infile, informat);
158*0a6a1f1dSLionel Sambuc if (x == NULL) {
159*0a6a1f1dSLionel Sambuc goto end;
160*0a6a1f1dSLionel Sambuc }
161ebfedea0SLionel Sambuc peer = SSL_SESSION_get0_peer(x);
162ebfedea0SLionel Sambuc
163*0a6a1f1dSLionel Sambuc if (context) {
164ebfedea0SLionel Sambuc size_t ctx_len = strlen(context);
165*0a6a1f1dSLionel Sambuc if (ctx_len > SSL_MAX_SID_CTX_LENGTH) {
166ebfedea0SLionel Sambuc BIO_printf(bio_err, "Context too long\n");
167ebfedea0SLionel Sambuc goto end;
168ebfedea0SLionel Sambuc }
169ebfedea0SLionel Sambuc SSL_SESSION_set1_id_context(x, (unsigned char *)context, ctx_len);
170ebfedea0SLionel Sambuc }
171ebfedea0SLionel Sambuc #ifdef undef
172ebfedea0SLionel Sambuc /* just testing for memory leaks :-) */
173ebfedea0SLionel Sambuc {
174ebfedea0SLionel Sambuc SSL_SESSION *s;
175ebfedea0SLionel Sambuc char buf[1024 * 10], *p;
176ebfedea0SLionel Sambuc int i;
177ebfedea0SLionel Sambuc
178ebfedea0SLionel Sambuc s = SSL_SESSION_new();
179ebfedea0SLionel Sambuc
180ebfedea0SLionel Sambuc p = &buf;
181ebfedea0SLionel Sambuc i = i2d_SSL_SESSION(x, &p);
182ebfedea0SLionel Sambuc p = &buf;
183ebfedea0SLionel Sambuc d2i_SSL_SESSION(&s, &p, (long)i);
184ebfedea0SLionel Sambuc p = &buf;
185ebfedea0SLionel Sambuc d2i_SSL_SESSION(&s, &p, (long)i);
186ebfedea0SLionel Sambuc p = &buf;
187ebfedea0SLionel Sambuc d2i_SSL_SESSION(&s, &p, (long)i);
188ebfedea0SLionel Sambuc SSL_SESSION_free(s);
189ebfedea0SLionel Sambuc }
190ebfedea0SLionel Sambuc #endif
191ebfedea0SLionel Sambuc
192*0a6a1f1dSLionel Sambuc if (!noout || text) {
193ebfedea0SLionel Sambuc out = BIO_new(BIO_s_file());
194*0a6a1f1dSLionel Sambuc if (out == NULL) {
195ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
196ebfedea0SLionel Sambuc goto end;
197ebfedea0SLionel Sambuc }
198ebfedea0SLionel Sambuc
199*0a6a1f1dSLionel Sambuc if (outfile == NULL) {
200ebfedea0SLionel Sambuc BIO_set_fp(out, stdout, BIO_NOCLOSE);
201ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
202ebfedea0SLionel Sambuc {
203ebfedea0SLionel Sambuc BIO *tmpbio = BIO_new(BIO_f_linebuffer());
204ebfedea0SLionel Sambuc out = BIO_push(tmpbio, out);
205ebfedea0SLionel Sambuc }
206ebfedea0SLionel Sambuc #endif
207*0a6a1f1dSLionel Sambuc } else {
208*0a6a1f1dSLionel Sambuc if (BIO_write_filename(out, outfile) <= 0) {
209ebfedea0SLionel Sambuc perror(outfile);
210ebfedea0SLionel Sambuc goto end;
211ebfedea0SLionel Sambuc }
212ebfedea0SLionel Sambuc }
213ebfedea0SLionel Sambuc }
214ebfedea0SLionel Sambuc
215*0a6a1f1dSLionel Sambuc if (text) {
216ebfedea0SLionel Sambuc SSL_SESSION_print(out, x);
217ebfedea0SLionel Sambuc
218*0a6a1f1dSLionel Sambuc if (cert) {
219ebfedea0SLionel Sambuc if (peer == NULL)
220ebfedea0SLionel Sambuc BIO_puts(out, "No certificate present\n");
221ebfedea0SLionel Sambuc else
222ebfedea0SLionel Sambuc X509_print(out, peer);
223ebfedea0SLionel Sambuc }
224ebfedea0SLionel Sambuc }
225ebfedea0SLionel Sambuc
226*0a6a1f1dSLionel Sambuc if (!noout && !cert) {
227ebfedea0SLionel Sambuc if (outformat == FORMAT_ASN1)
228ebfedea0SLionel Sambuc i = i2d_SSL_SESSION_bio(out, x);
229ebfedea0SLionel Sambuc else if (outformat == FORMAT_PEM)
230ebfedea0SLionel Sambuc i = PEM_write_bio_SSL_SESSION(out, x);
231ebfedea0SLionel Sambuc else {
232ebfedea0SLionel Sambuc BIO_printf(bio_err, "bad output format specified for outfile\n");
233ebfedea0SLionel Sambuc goto end;
234ebfedea0SLionel Sambuc }
235ebfedea0SLionel Sambuc if (!i) {
236ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to write SSL_SESSION\n");
237ebfedea0SLionel Sambuc goto end;
238ebfedea0SLionel Sambuc }
239*0a6a1f1dSLionel Sambuc } else if (!noout && (peer != NULL)) { /* just print the certificate */
240ebfedea0SLionel Sambuc if (outformat == FORMAT_ASN1)
241ebfedea0SLionel Sambuc i = (int)i2d_X509_bio(out, peer);
242ebfedea0SLionel Sambuc else if (outformat == FORMAT_PEM)
243ebfedea0SLionel Sambuc i = PEM_write_bio_X509(out, peer);
244ebfedea0SLionel Sambuc else {
245ebfedea0SLionel Sambuc BIO_printf(bio_err, "bad output format specified for outfile\n");
246ebfedea0SLionel Sambuc goto end;
247ebfedea0SLionel Sambuc }
248ebfedea0SLionel Sambuc if (!i) {
249ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to write X509\n");
250ebfedea0SLionel Sambuc goto end;
251ebfedea0SLionel Sambuc }
252ebfedea0SLionel Sambuc }
253ebfedea0SLionel Sambuc ret = 0;
254ebfedea0SLionel Sambuc end:
255*0a6a1f1dSLionel Sambuc if (out != NULL)
256*0a6a1f1dSLionel Sambuc BIO_free_all(out);
257*0a6a1f1dSLionel Sambuc if (x != NULL)
258*0a6a1f1dSLionel Sambuc SSL_SESSION_free(x);
259ebfedea0SLionel Sambuc apps_shutdown();
260ebfedea0SLionel Sambuc OPENSSL_EXIT(ret);
261ebfedea0SLionel Sambuc }
262ebfedea0SLionel Sambuc
load_sess_id(char * infile,int format)263ebfedea0SLionel Sambuc static SSL_SESSION *load_sess_id(char *infile, int format)
264ebfedea0SLionel Sambuc {
265ebfedea0SLionel Sambuc SSL_SESSION *x = NULL;
266ebfedea0SLionel Sambuc BIO *in = NULL;
267ebfedea0SLionel Sambuc
268ebfedea0SLionel Sambuc in = BIO_new(BIO_s_file());
269*0a6a1f1dSLionel Sambuc if (in == NULL) {
270ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
271ebfedea0SLionel Sambuc goto end;
272ebfedea0SLionel Sambuc }
273ebfedea0SLionel Sambuc
274ebfedea0SLionel Sambuc if (infile == NULL)
275ebfedea0SLionel Sambuc BIO_set_fp(in, stdin, BIO_NOCLOSE);
276*0a6a1f1dSLionel Sambuc else {
277*0a6a1f1dSLionel Sambuc if (BIO_read_filename(in, infile) <= 0) {
278ebfedea0SLionel Sambuc perror(infile);
279ebfedea0SLionel Sambuc goto end;
280ebfedea0SLionel Sambuc }
281ebfedea0SLionel Sambuc }
282ebfedea0SLionel Sambuc if (format == FORMAT_ASN1)
283ebfedea0SLionel Sambuc x = d2i_SSL_SESSION_bio(in, NULL);
284ebfedea0SLionel Sambuc else if (format == FORMAT_PEM)
285ebfedea0SLionel Sambuc x = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL);
286ebfedea0SLionel Sambuc else {
287ebfedea0SLionel Sambuc BIO_printf(bio_err, "bad input format specified for input crl\n");
288ebfedea0SLionel Sambuc goto end;
289ebfedea0SLionel Sambuc }
290*0a6a1f1dSLionel Sambuc if (x == NULL) {
291ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to load SSL_SESSION\n");
292ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
293ebfedea0SLionel Sambuc goto end;
294ebfedea0SLionel Sambuc }
295ebfedea0SLionel Sambuc
296ebfedea0SLionel Sambuc end:
297*0a6a1f1dSLionel Sambuc if (in != NULL)
298*0a6a1f1dSLionel Sambuc BIO_free(in);
299ebfedea0SLionel Sambuc return (x);
300ebfedea0SLionel Sambuc }
301