diff options
author | doug <> | 2015-01-24 09:44:29 +0000 |
---|---|---|
committer | doug <> | 2015-01-24 09:44:29 +0000 |
commit | e7b94f3e0e803b7bea11d0c8a568dafe4a462f3b (patch) | |
tree | 13ec0b786cfc6fee228c8a71d6bf1867d520ae91 | |
parent | a05e3a02c8560eab00185e0de9b8a55cb4c6d736 (diff) | |
download | openbsd-e7b94f3e0e803b7bea11d0c8a568dafe4a462f3b.tar.gz openbsd-e7b94f3e0e803b7bea11d0c8a568dafe4a462f3b.tar.bz2 openbsd-e7b94f3e0e803b7bea11d0c8a568dafe4a462f3b.zip |
Convert openssl(1) sess_id to the new option handling.
input + ok jsing@
-rw-r--r-- | src/usr.bin/openssl/sess_id.c | 201 |
1 files changed, 113 insertions, 88 deletions
diff --git a/src/usr.bin/openssl/sess_id.c b/src/usr.bin/openssl/sess_id.c index 7c1fb771d1..51720204bf 100644 --- a/src/usr.bin/openssl/sess_id.c +++ b/src/usr.bin/openssl/sess_id.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: sess_id.c,v 1.2 2014/08/28 14:23:52 jsing Exp $ */ | 1 | /* $OpenBSD: sess_id.c,v 1.3 2015/01/24 09:44:29 doug Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -68,98 +68,119 @@ | |||
68 | #include <openssl/ssl.h> | 68 | #include <openssl/ssl.h> |
69 | #include <openssl/x509.h> | 69 | #include <openssl/x509.h> |
70 | 70 | ||
71 | static const char *sess_id_usage[] = { | 71 | static struct { |
72 | "usage: sess_id args\n", | 72 | int cert; |
73 | "\n", | 73 | char *context; |
74 | " -inform arg - input format - default PEM (DER or PEM)\n", | 74 | char *infile; |
75 | " -outform arg - output format - default PEM\n", | 75 | int informat; |
76 | " -in arg - input file - default stdin\n", | 76 | int noout; |
77 | " -out arg - output file - default stdout\n", | 77 | char *outfile; |
78 | " -text - print ssl session id details\n", | 78 | int outformat; |
79 | " -cert - output certificate \n", | 79 | int text; |
80 | " -noout - no output of encoded session info\n", | 80 | } sess_id_config; |
81 | " -context arg - set the session ID context\n", | 81 | |
82 | NULL | 82 | static struct option sess_id_options[] = { |
83 | { | ||
84 | .name = "cert", | ||
85 | .desc = "Output certificate if present in session", | ||
86 | .type = OPTION_FLAG, | ||
87 | .opt.flag = &sess_id_config.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 = &sess_id_config.context, | ||
95 | }, | ||
96 | { | ||
97 | .name = "in", | ||
98 | .argname = "file", | ||
99 | .desc = "Input file (default stdin)", | ||
100 | .type = OPTION_ARG, | ||
101 | .opt.arg = &sess_id_config.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 = &sess_id_config.informat, | ||
109 | }, | ||
110 | { | ||
111 | .name = "noout", | ||
112 | .desc = "Do not output the encoded session info", | ||
113 | .type = OPTION_FLAG, | ||
114 | .opt.flag = &sess_id_config.noout, | ||
115 | }, | ||
116 | { | ||
117 | .name = "out", | ||
118 | .argname = "file", | ||
119 | .desc = "Output file (default stdout)", | ||
120 | .type = OPTION_ARG, | ||
121 | .opt.arg = &sess_id_config.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 = &sess_id_config.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 = &sess_id_config.text, | ||
136 | }, | ||
137 | { NULL } | ||
83 | }; | 138 | }; |
84 | 139 | ||
85 | static SSL_SESSION *load_sess_id(char *file, int format); | 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 | } | ||
86 | 149 | ||
87 | int sess_id_main(int, char **); | 150 | static SSL_SESSION *load_sess_id(char *file, int format); |
88 | 151 | ||
89 | int | 152 | int |
90 | sess_id_main(int argc, char **argv) | 153 | sess_id_main(int argc, char **argv) |
91 | { | 154 | { |
92 | SSL_SESSION *x = NULL; | 155 | SSL_SESSION *x = NULL; |
93 | X509 *peer = NULL; | 156 | X509 *peer = NULL; |
94 | int ret = 1, i, num, badops = 0; | 157 | int ret = 1, i; |
95 | BIO *out = NULL; | 158 | BIO *out = NULL; |
96 | int informat, outformat; | ||
97 | char *infile = NULL, *outfile = NULL, *context = NULL; | ||
98 | int cert = 0, noout = 0, text = 0; | ||
99 | const char **pp; | ||
100 | 159 | ||
101 | informat = FORMAT_PEM; | 160 | memset(&sess_id_config, 0, sizeof(sess_id_config)); |
102 | outformat = FORMAT_PEM; | ||
103 | 161 | ||
104 | argc--; | 162 | sess_id_config.informat = FORMAT_PEM; |
105 | argv++; | 163 | sess_id_config.outformat = FORMAT_PEM; |
106 | num = 0; | ||
107 | while (argc >= 1) { | ||
108 | if (strcmp(*argv, "-inform") == 0) { | ||
109 | if (--argc < 1) | ||
110 | goto bad; | ||
111 | informat = str2fmt(*(++argv)); | ||
112 | } else if (strcmp(*argv, "-outform") == 0) { | ||
113 | if (--argc < 1) | ||
114 | goto bad; | ||
115 | outformat = str2fmt(*(++argv)); | ||
116 | } else if (strcmp(*argv, "-in") == 0) { | ||
117 | if (--argc < 1) | ||
118 | goto bad; | ||
119 | infile = *(++argv); | ||
120 | } else if (strcmp(*argv, "-out") == 0) { | ||
121 | if (--argc < 1) | ||
122 | goto bad; | ||
123 | outfile = *(++argv); | ||
124 | } else if (strcmp(*argv, "-text") == 0) | ||
125 | text = ++num; | ||
126 | else if (strcmp(*argv, "-cert") == 0) | ||
127 | cert = ++num; | ||
128 | else if (strcmp(*argv, "-noout") == 0) | ||
129 | noout = ++num; | ||
130 | else if (strcmp(*argv, "-context") == 0) { | ||
131 | if (--argc < 1) | ||
132 | goto bad; | ||
133 | context = *++argv; | ||
134 | } else { | ||
135 | BIO_printf(bio_err, "unknown option %s\n", *argv); | ||
136 | badops = 1; | ||
137 | break; | ||
138 | } | ||
139 | argc--; | ||
140 | argv++; | ||
141 | } | ||
142 | 164 | ||
143 | if (badops) { | 165 | if (options_parse(argc, argv, sess_id_options, NULL, NULL) != 0) { |
144 | bad: | 166 | sess_id_usage(); |
145 | for (pp = sess_id_usage; (*pp != NULL); pp++) | 167 | return (1); |
146 | BIO_printf(bio_err, "%s", *pp); | ||
147 | goto end; | ||
148 | } | 168 | } |
149 | 169 | ||
150 | x = load_sess_id(infile, informat); | 170 | x = load_sess_id(sess_id_config.infile, sess_id_config.informat); |
151 | if (x == NULL) { | 171 | if (x == NULL) { |
152 | goto end; | 172 | goto end; |
153 | } | 173 | } |
154 | peer = SSL_SESSION_get0_peer(x); | 174 | peer = SSL_SESSION_get0_peer(x); |
155 | 175 | ||
156 | if (context) { | 176 | if (sess_id_config.context) { |
157 | size_t ctx_len = strlen(context); | 177 | size_t ctx_len = strlen(sess_id_config.context); |
158 | if (ctx_len > SSL_MAX_SID_CTX_LENGTH) { | 178 | if (ctx_len > SSL_MAX_SID_CTX_LENGTH) { |
159 | BIO_printf(bio_err, "Context too long\n"); | 179 | BIO_printf(bio_err, "Context too long\n"); |
160 | goto end; | 180 | goto end; |
161 | } | 181 | } |
162 | SSL_SESSION_set1_id_context(x, (unsigned char *) context, ctx_len); | 182 | SSL_SESSION_set1_id_context(x, |
183 | (unsigned char *)sess_id_config.context, ctx_len); | ||
163 | } | 184 | } |
164 | #ifdef undef | 185 | #ifdef undef |
165 | /* just testing for memory leaks :-) */ | 186 | /* just testing for memory leaks :-) */ |
@@ -182,51 +203,55 @@ bad: | |||
182 | } | 203 | } |
183 | #endif | 204 | #endif |
184 | 205 | ||
185 | if (!noout || text) { | 206 | if (!sess_id_config.noout || sess_id_config.text) { |
186 | out = BIO_new(BIO_s_file()); | 207 | out = BIO_new(BIO_s_file()); |
187 | if (out == NULL) { | 208 | if (out == NULL) { |
188 | ERR_print_errors(bio_err); | 209 | ERR_print_errors(bio_err); |
189 | goto end; | 210 | goto end; |
190 | } | 211 | } |
191 | if (outfile == NULL) { | 212 | if (sess_id_config.outfile == NULL) { |
192 | BIO_set_fp(out, stdout, BIO_NOCLOSE); | 213 | BIO_set_fp(out, stdout, BIO_NOCLOSE); |
193 | } else { | 214 | } else { |
194 | if (BIO_write_filename(out, outfile) <= 0) { | 215 | if (BIO_write_filename(out, sess_id_config.outfile) |
195 | perror(outfile); | 216 | <= 0) { |
217 | perror(sess_id_config.outfile); | ||
196 | goto end; | 218 | goto end; |
197 | } | 219 | } |
198 | } | 220 | } |
199 | } | 221 | } |
200 | if (text) { | 222 | if (sess_id_config.text) { |
201 | SSL_SESSION_print(out, x); | 223 | SSL_SESSION_print(out, x); |
202 | 224 | ||
203 | if (cert) { | 225 | if (sess_id_config.cert) { |
204 | if (peer == NULL) | 226 | if (peer == NULL) |
205 | BIO_puts(out, "No certificate present\n"); | 227 | BIO_puts(out, "No certificate present\n"); |
206 | else | 228 | else |
207 | X509_print(out, peer); | 229 | X509_print(out, peer); |
208 | } | 230 | } |
209 | } | 231 | } |
210 | if (!noout && !cert) { | 232 | if (!sess_id_config.noout && !sess_id_config.cert) { |
211 | if (outformat == FORMAT_ASN1) | 233 | if (sess_id_config.outformat == FORMAT_ASN1) |
212 | i = i2d_SSL_SESSION_bio(out, x); | 234 | i = i2d_SSL_SESSION_bio(out, x); |
213 | else if (outformat == FORMAT_PEM) | 235 | else if (sess_id_config.outformat == FORMAT_PEM) |
214 | i = PEM_write_bio_SSL_SESSION(out, x); | 236 | i = PEM_write_bio_SSL_SESSION(out, x); |
215 | else { | 237 | else { |
216 | BIO_printf(bio_err, "bad output format specified for outfile\n"); | 238 | BIO_printf(bio_err, |
239 | "bad output format specified for outfile\n"); | ||
217 | goto end; | 240 | goto end; |
218 | } | 241 | } |
219 | if (!i) { | 242 | if (!i) { |
220 | BIO_printf(bio_err, "unable to write SSL_SESSION\n"); | 243 | BIO_printf(bio_err, "unable to write SSL_SESSION\n"); |
221 | goto end; | 244 | goto end; |
222 | } | 245 | } |
223 | } else if (!noout && (peer != NULL)) { /* just print the certificate */ | 246 | } else if (!sess_id_config.noout && (peer != NULL)) { |
224 | if (outformat == FORMAT_ASN1) | 247 | /* just print the certificate */ |
248 | if (sess_id_config.outformat == FORMAT_ASN1) | ||
225 | i = (int) i2d_X509_bio(out, peer); | 249 | i = (int) i2d_X509_bio(out, peer); |
226 | else if (outformat == FORMAT_PEM) | 250 | else if (sess_id_config.outformat == FORMAT_PEM) |
227 | i = PEM_write_bio_X509(out, peer); | 251 | i = PEM_write_bio_X509(out, peer); |
228 | else { | 252 | else { |
229 | BIO_printf(bio_err, "bad output format specified for outfile\n"); | 253 | BIO_printf(bio_err, |
254 | "bad output format specified for outfile\n"); | ||
230 | goto end; | 255 | goto end; |
231 | } | 256 | } |
232 | if (!i) { | 257 | if (!i) { |
@@ -235,11 +260,10 @@ bad: | |||
235 | } | 260 | } |
236 | } | 261 | } |
237 | ret = 0; | 262 | ret = 0; |
263 | |||
238 | end: | 264 | end: |
239 | if (out != NULL) | 265 | BIO_free_all(out); |
240 | BIO_free_all(out); | 266 | SSL_SESSION_free(x); |
241 | if (x != NULL) | ||
242 | SSL_SESSION_free(x); | ||
243 | 267 | ||
244 | return (ret); | 268 | return (ret); |
245 | } | 269 | } |
@@ -268,7 +292,8 @@ load_sess_id(char *infile, int format) | |||
268 | else if (format == FORMAT_PEM) | 292 | else if (format == FORMAT_PEM) |
269 | x = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL); | 293 | x = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL); |
270 | else { | 294 | else { |
271 | BIO_printf(bio_err, "bad input format specified for input crl\n"); | 295 | BIO_printf(bio_err, |
296 | "bad input format specified for input crl\n"); | ||
272 | goto end; | 297 | goto end; |
273 | } | 298 | } |
274 | if (x == NULL) { | 299 | if (x == NULL) { |