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