]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/sess_id.c
test/asn1_time_test.c: fix pre-C90 warning.
[thirdparty/openssl.git] / apps / sess_id.c
CommitLineData
846e33c7 1/*
2234212c 2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
846e33c7
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
d02b48c6
RE
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include "apps.h"
ec577822
BM
14#include <openssl/bio.h>
15#include <openssl/err.h>
16#include <openssl/x509.h>
17#include <openssl/pem.h>
18#include <openssl/ssl.h>
d02b48c6 19
7e1b7485
RS
20typedef enum OPTION_choice {
21 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
22 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
23 OPT_TEXT, OPT_CERT, OPT_NOOUT, OPT_CONTEXT
24} OPTION_CHOICE;
25
44c83ebd 26const OPTIONS sess_id_options[] = {
7e1b7485
RS
27 {"help", OPT_HELP, '-', "Display this summary"},
28 {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"},
1446f72b 29 {"outform", OPT_OUTFORM, 'f',
7e1b7485
RS
30 "Output format - default PEM (PEM, DER or NSS)"},
31 {"in", OPT_IN, 's', "Input file - default stdin"},
32 {"out", OPT_OUT, 's', "Output file - default stdout"},
33 {"text", OPT_TEXT, '-', "Print ssl session id details"},
34 {"cert", OPT_CERT, '-', "Output certificate "},
35 {"noout", OPT_NOOUT, '-', "Don't output the encoded session info"},
36 {"context", OPT_CONTEXT, 's', "Set the session ID context"},
37 {NULL}
d02b48c6
RE
38};
39
d02b48c6 40static SSL_SESSION *load_sess_id(char *file, int format);
667ac4ec 41
7e1b7485 42int sess_id_main(int argc, char **argv)
0f113f3e
MC
43{
44 SSL_SESSION *x = NULL;
45 X509 *peer = NULL;
0f113f3e 46 BIO *out = NULL;
7e1b7485
RS
47 char *infile = NULL, *outfile = NULL, *context = NULL, *prog;
48 int informat = FORMAT_PEM, outformat = FORMAT_PEM;
49 int cert = 0, noout = 0, text = 0, ret = 1, i, num = 0;
50 OPTION_CHOICE o;
51
52 prog = opt_init(argc, argv, sess_id_options);
53 while ((o = opt_next()) != OPT_EOF) {
54 switch (o) {
55 case OPT_EOF:
56 case OPT_ERR:
57 opthelp:
58 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
59 goto end;
60 case OPT_HELP:
61 opt_help(sess_id_options);
62 ret = 0;
63 goto end;
64 case OPT_INFORM:
65 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
66 goto opthelp;
67 break;
68 case OPT_OUTFORM:
1446f72b
MC
69 if (!opt_format(opt_arg(), OPT_FMT_PEMDER | OPT_FMT_NSS,
70 &outformat))
7e1b7485
RS
71 goto opthelp;
72 break;
73 case OPT_IN:
74 infile = opt_arg();
75 break;
76 case OPT_OUT:
77 outfile = opt_arg();
78 break;
79 case OPT_TEXT:
0f113f3e 80 text = ++num;
7e1b7485
RS
81 break;
82 case OPT_CERT:
0f113f3e 83 cert = ++num;
7e1b7485
RS
84 break;
85 case OPT_NOOUT:
0f113f3e 86 noout = ++num;
7e1b7485
RS
87 break;
88 case OPT_CONTEXT:
89 context = opt_arg();
0f113f3e
MC
90 break;
91 }
0f113f3e 92 }
7e1b7485 93 argc = opt_num_rest();
03358517
KR
94 if (argc != 0)
95 goto opthelp;
d02b48c6 96
0f113f3e
MC
97 x = load_sess_id(infile, informat);
98 if (x == NULL) {
99 goto end;
100 }
101 peer = SSL_SESSION_get0_peer(x);
b4cadc6e 102
2234212c 103 if (context != NULL) {
0f113f3e
MC
104 size_t ctx_len = strlen(context);
105 if (ctx_len > SSL_MAX_SID_CTX_LENGTH) {
106 BIO_printf(bio_err, "Context too long\n");
107 goto end;
108 }
7e1b7485 109 if (!SSL_SESSION_set1_id_context(x, (unsigned char *)context,
2234212c 110 ctx_len)) {
ac59d705
MC
111 BIO_printf(bio_err, "Error setting id context\n");
112 goto end;
113 }
0f113f3e 114 }
d02b48c6 115
0f113f3e 116 if (!noout || text) {
bdd58d98 117 out = bio_open_default(outfile, 'w', outformat);
7e1b7485 118 if (out == NULL)
0f113f3e 119 goto end;
0f113f3e 120 }
d02b48c6 121
0f113f3e
MC
122 if (text) {
123 SSL_SESSION_print(out, x);
d02b48c6 124
0f113f3e
MC
125 if (cert) {
126 if (peer == NULL)
127 BIO_puts(out, "No certificate present\n");
128 else
129 X509_print(out, peer);
130 }
131 }
d02b48c6 132
0f113f3e 133 if (!noout && !cert) {
2234212c 134 if (outformat == FORMAT_ASN1) {
0f113f3e 135 i = i2d_SSL_SESSION_bio(out, x);
2234212c 136 } else if (outformat == FORMAT_PEM) {
0f113f3e 137 i = PEM_write_bio_SSL_SESSION(out, x);
2234212c 138 } else if (outformat == FORMAT_NSS) {
0f113f3e 139 i = SSL_SESSION_print_keylog(out, x);
2234212c 140 } else {
0f113f3e
MC
141 BIO_printf(bio_err, "bad output format specified for outfile\n");
142 goto end;
143 }
144 if (!i) {
145 BIO_printf(bio_err, "unable to write SSL_SESSION\n");
146 goto end;
147 }
148 } else if (!noout && (peer != NULL)) { /* just print the certificate */
2234212c 149 if (outformat == FORMAT_ASN1) {
0f113f3e 150 i = (int)i2d_X509_bio(out, peer);
2234212c 151 } else if (outformat == FORMAT_PEM) {
0f113f3e 152 i = PEM_write_bio_X509(out, peer);
2234212c 153 } else {
0f113f3e
MC
154 BIO_printf(bio_err, "bad output format specified for outfile\n");
155 goto end;
156 }
157 if (!i) {
158 BIO_printf(bio_err, "unable to write X509\n");
159 goto end;
160 }
161 }
162 ret = 0;
163 end:
ca3a82c3 164 BIO_free_all(out);
25aaa98a 165 SSL_SESSION_free(x);
26a7d938 166 return ret;
0f113f3e 167}
d02b48c6 168
6b691a5c 169static SSL_SESSION *load_sess_id(char *infile, int format)
0f113f3e
MC
170{
171 SSL_SESSION *x = NULL;
172 BIO *in = NULL;
d02b48c6 173
bdd58d98 174 in = bio_open_default(infile, 'r', format);
7e1b7485 175 if (in == NULL)
0f113f3e 176 goto end;
0f113f3e
MC
177 if (format == FORMAT_ASN1)
178 x = d2i_SSL_SESSION_bio(in, NULL);
7e1b7485 179 else
0f113f3e 180 x = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL);
0f113f3e
MC
181 if (x == NULL) {
182 BIO_printf(bio_err, "unable to load SSL_SESSION\n");
183 ERR_print_errors(bio_err);
184 goto end;
185 }
d02b48c6 186
0f113f3e 187 end:
ca3a82c3 188 BIO_free(in);
26a7d938 189 return x;
0f113f3e 190}