diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/usr.bin/openssl/dgst.c | 315 |
1 files changed, 216 insertions, 99 deletions
diff --git a/src/usr.bin/openssl/dgst.c b/src/usr.bin/openssl/dgst.c index 3ec19cc04e..b8253a8fb8 100644 --- a/src/usr.bin/openssl/dgst.c +++ b/src/usr.bin/openssl/dgst.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: dgst.c,v 1.14 2019/07/29 10:06:55 inoguchi Exp $ */ | 1 | /* $OpenBSD: dgst.c,v 1.15 2019/08/30 11:19:00 inoguchi 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 | * |
| @@ -98,6 +98,199 @@ static struct { | |||
| 98 | int want_pub; | 98 | int want_pub; |
| 99 | } dgst_config; | 99 | } dgst_config; |
| 100 | 100 | ||
| 101 | static int | ||
| 102 | dgst_opt_macopt(char *arg) | ||
| 103 | { | ||
| 104 | if (arg == NULL) | ||
| 105 | return (1); | ||
| 106 | |||
| 107 | if (dgst_config.macopts == NULL && | ||
| 108 | (dgst_config.macopts = sk_OPENSSL_STRING_new_null()) == NULL) | ||
| 109 | return (1); | ||
| 110 | |||
| 111 | if (!sk_OPENSSL_STRING_push(dgst_config.macopts, arg)) | ||
| 112 | return (1); | ||
| 113 | |||
| 114 | return (0); | ||
| 115 | } | ||
| 116 | |||
| 117 | static int | ||
| 118 | dgst_opt_md(int argc, char **argv, int *argsused) | ||
| 119 | { | ||
| 120 | char *name = argv[0]; | ||
| 121 | |||
| 122 | if (*name++ != '-') | ||
| 123 | return (1); | ||
| 124 | |||
| 125 | if ((dgst_config.m = EVP_get_digestbyname(name)) != NULL) | ||
| 126 | dgst_config.md = dgst_config.m; | ||
| 127 | else | ||
| 128 | return (1); | ||
| 129 | |||
| 130 | *argsused = 1; | ||
| 131 | return (0); | ||
| 132 | } | ||
| 133 | |||
| 134 | static int | ||
| 135 | dgst_opt_prverify(char *arg) | ||
| 136 | { | ||
| 137 | if (arg == NULL) | ||
| 138 | return (1); | ||
| 139 | |||
| 140 | dgst_config.keyfile = arg; | ||
| 141 | dgst_config.do_verify = 1; | ||
| 142 | return (0); | ||
| 143 | } | ||
| 144 | |||
| 145 | static int | ||
| 146 | dgst_opt_sigopt(char *arg) | ||
| 147 | { | ||
| 148 | if (arg == NULL) | ||
| 149 | return (1); | ||
| 150 | |||
| 151 | if (dgst_config.sigopts == NULL && | ||
| 152 | (dgst_config.sigopts = sk_OPENSSL_STRING_new_null()) == NULL) | ||
| 153 | return (1); | ||
| 154 | |||
| 155 | if (!sk_OPENSSL_STRING_push(dgst_config.sigopts, arg)) | ||
| 156 | return (1); | ||
| 157 | |||
| 158 | return (0); | ||
| 159 | } | ||
| 160 | |||
| 161 | static int | ||
| 162 | dgst_opt_verify(char *arg) | ||
| 163 | { | ||
| 164 | if (arg == NULL) | ||
| 165 | return (1); | ||
| 166 | |||
| 167 | dgst_config.keyfile = arg; | ||
| 168 | dgst_config.want_pub = 1; | ||
| 169 | dgst_config.do_verify = 1; | ||
| 170 | return (0); | ||
| 171 | } | ||
| 172 | |||
| 173 | static const struct option dgst_options[] = { | ||
| 174 | { | ||
| 175 | .name = "binary", | ||
| 176 | .desc = "Output in binary form", | ||
| 177 | .type = OPTION_VALUE, | ||
| 178 | .opt.value = &dgst_config.out_bin, | ||
| 179 | .value = 1, | ||
| 180 | }, | ||
| 181 | { | ||
| 182 | .name = "c", | ||
| 183 | .desc = "To output the digest with separating colons", | ||
| 184 | .type = OPTION_VALUE, | ||
| 185 | .opt.value = &dgst_config.separator, | ||
| 186 | .value = 1, | ||
| 187 | }, | ||
| 188 | { | ||
| 189 | .name = "d", | ||
| 190 | .desc = "To output debug info", | ||
| 191 | .type = OPTION_FLAG, | ||
| 192 | .opt.flag = &dgst_config.debug, | ||
| 193 | }, | ||
| 194 | { | ||
| 195 | .name = "hex", | ||
| 196 | .desc = "Output as hex dump", | ||
| 197 | .type = OPTION_VALUE, | ||
| 198 | .opt.value = &dgst_config.out_bin, | ||
| 199 | .value = 0, | ||
| 200 | }, | ||
| 201 | { | ||
| 202 | .name = "hmac", | ||
| 203 | .argname = "key", | ||
| 204 | .desc = "Create hashed MAC with key", | ||
| 205 | .type = OPTION_ARG, | ||
| 206 | .opt.arg = &dgst_config.hmac_key, | ||
| 207 | }, | ||
| 208 | { | ||
| 209 | .name = "keyform", | ||
| 210 | .argname = "format", | ||
| 211 | .desc = "Key file format (PEM)", | ||
| 212 | .type = OPTION_ARG_FORMAT, | ||
| 213 | .opt.value = &dgst_config.keyform, | ||
| 214 | }, | ||
| 215 | { | ||
| 216 | .name = "mac", | ||
| 217 | .argname = "algorithm", | ||
| 218 | .desc = "Create MAC (not necessarily HMAC)", | ||
| 219 | .type = OPTION_ARG, | ||
| 220 | .opt.arg = &dgst_config.mac_name, | ||
| 221 | }, | ||
| 222 | { | ||
| 223 | .name = "macopt", | ||
| 224 | .argname = "nm:v", | ||
| 225 | .desc = "MAC algorithm parameters or key", | ||
| 226 | .type = OPTION_ARG_FUNC, | ||
| 227 | .opt.argfunc = dgst_opt_macopt, | ||
| 228 | }, | ||
| 229 | { | ||
| 230 | .name = "out", | ||
| 231 | .argname = "file", | ||
| 232 | .desc = "Output to file rather than stdout", | ||
| 233 | .type = OPTION_ARG, | ||
| 234 | .opt.arg = &dgst_config.outfile, | ||
| 235 | }, | ||
| 236 | { | ||
| 237 | .name = "passin", | ||
| 238 | .argname = "arg", | ||
| 239 | .desc = "Input file passphrase source", | ||
| 240 | .type = OPTION_ARG, | ||
| 241 | .opt.arg = &dgst_config.passargin, | ||
| 242 | }, | ||
| 243 | { | ||
| 244 | .name = "prverify", | ||
| 245 | .argname = "file", | ||
| 246 | .desc = "Verify a signature using private key in file", | ||
| 247 | .type = OPTION_ARG_FUNC, | ||
| 248 | .opt.argfunc = dgst_opt_prverify, | ||
| 249 | }, | ||
| 250 | { | ||
| 251 | .name = "r", | ||
| 252 | .desc = "To output the digest in coreutils format", | ||
| 253 | .type = OPTION_VALUE, | ||
| 254 | .opt.value = &dgst_config.separator, | ||
| 255 | .value = 2, | ||
| 256 | }, | ||
| 257 | { | ||
| 258 | .name = "sign", | ||
| 259 | .argname = "file", | ||
| 260 | .desc = "Sign digest using private key in file", | ||
| 261 | .type = OPTION_ARG, | ||
| 262 | .opt.arg = &dgst_config.keyfile, | ||
| 263 | }, | ||
| 264 | { | ||
| 265 | .name = "signature", | ||
| 266 | .argname = "file", | ||
| 267 | .desc = "Signature to verify", | ||
| 268 | .type = OPTION_ARG, | ||
| 269 | .opt.arg = &dgst_config.sigfile, | ||
| 270 | }, | ||
| 271 | { | ||
| 272 | .name = "sigopt", | ||
| 273 | .argname = "nm:v", | ||
| 274 | .desc = "Signature parameter", | ||
| 275 | .type = OPTION_ARG_FUNC, | ||
| 276 | .opt.argfunc = dgst_opt_sigopt, | ||
| 277 | }, | ||
| 278 | { | ||
| 279 | .name = "verify", | ||
| 280 | .argname = "file", | ||
| 281 | .desc = "Verify a signature using public key in file", | ||
| 282 | .type = OPTION_ARG_FUNC, | ||
| 283 | .opt.argfunc = dgst_opt_verify, | ||
| 284 | }, | ||
| 285 | { | ||
| 286 | .name = NULL, | ||
| 287 | .desc = "", | ||
| 288 | .type = OPTION_ARGV_FUNC, | ||
| 289 | .opt.argvfunc = dgst_opt_md, | ||
| 290 | }, | ||
| 291 | { NULL }, | ||
| 292 | }; | ||
| 293 | |||
| 101 | static void | 294 | static void |
| 102 | list_md_fn(const EVP_MD * m, const char *from, const char *to, void *arg) | 295 | list_md_fn(const EVP_MD * m, const char *from, const char *to, void *arg) |
| 103 | { | 296 | { |
| @@ -114,10 +307,25 @@ list_md_fn(const EVP_MD * m, const char *from, const char *to, void *arg) | |||
| 114 | return; | 307 | return; |
| 115 | if (strchr(mname, ' ')) | 308 | if (strchr(mname, ' ')) |
| 116 | mname = EVP_MD_name(m); | 309 | mname = EVP_MD_name(m); |
| 117 | BIO_printf(arg, "-%-14s to use the %s message digest algorithm\n", | 310 | BIO_printf(arg, " -%-17s To use the %s message digest algorithm\n", |
| 118 | mname, mname); | 311 | mname, mname); |
| 119 | } | 312 | } |
| 120 | 313 | ||
| 314 | static void | ||
| 315 | dgst_usage(void) | ||
| 316 | { | ||
| 317 | fprintf(stderr, "usage: dgst [-cdr] [-binary] [-digest] [-hex]"); | ||
| 318 | fprintf(stderr, " [-hmac key] [-keyform fmt]\n"); | ||
| 319 | fprintf(stderr, " [-mac algorithm] [-macopt nm:v] [-out file]"); | ||
| 320 | fprintf(stderr, " [-passin arg]\n"); | ||
| 321 | fprintf(stderr, " [-prverify file] [-sign file]"); | ||
| 322 | fprintf(stderr, " [-signature file]\n"); | ||
| 323 | fprintf(stderr, " [-sigopt nm:v] [-verify file] [file ...]\n\n"); | ||
| 324 | options_usage(dgst_options); | ||
| 325 | EVP_MD_do_all_sorted(list_md_fn, bio_err); | ||
| 326 | fprintf(stderr, "\n"); | ||
| 327 | } | ||
| 328 | |||
| 121 | int | 329 | int |
| 122 | dgst_main(int argc, char **argv) | 330 | dgst_main(int argc, char **argv) |
| 123 | { | 331 | { |
| @@ -154,109 +362,18 @@ dgst_main(int argc, char **argv) | |||
| 154 | 362 | ||
| 155 | dgst_config.md = EVP_get_digestbyname(pname); | 363 | dgst_config.md = EVP_get_digestbyname(pname); |
| 156 | 364 | ||
| 157 | argc--; | 365 | if (options_parse(argc, argv, dgst_options, NULL, |
| 158 | argv++; | 366 | &dgst_config.argsused) != 0) { |
| 159 | while (argc > 0) { | 367 | dgst_usage(); |
| 160 | if ((*argv)[0] != '-') | 368 | goto end; |
| 161 | break; | ||
| 162 | if (strcmp(*argv, "-c") == 0) | ||
| 163 | dgst_config.separator = 1; | ||
| 164 | else if (strcmp(*argv, "-r") == 0) | ||
| 165 | dgst_config.separator = 2; | ||
| 166 | else if (strcmp(*argv, "-out") == 0) { | ||
| 167 | if (--argc < 1) | ||
| 168 | break; | ||
| 169 | dgst_config.outfile = *(++argv); | ||
| 170 | } else if (strcmp(*argv, "-sign") == 0) { | ||
| 171 | if (--argc < 1) | ||
| 172 | break; | ||
| 173 | dgst_config.keyfile = *(++argv); | ||
| 174 | } else if (!strcmp(*argv, "-passin")) { | ||
| 175 | if (--argc < 1) | ||
| 176 | break; | ||
| 177 | dgst_config.passargin = *++argv; | ||
| 178 | } else if (strcmp(*argv, "-verify") == 0) { | ||
| 179 | if (--argc < 1) | ||
| 180 | break; | ||
| 181 | dgst_config.keyfile = *(++argv); | ||
| 182 | dgst_config.want_pub = 1; | ||
| 183 | dgst_config.do_verify = 1; | ||
| 184 | } else if (strcmp(*argv, "-prverify") == 0) { | ||
| 185 | if (--argc < 1) | ||
| 186 | break; | ||
| 187 | dgst_config.keyfile = *(++argv); | ||
| 188 | dgst_config.do_verify = 1; | ||
| 189 | } else if (strcmp(*argv, "-signature") == 0) { | ||
| 190 | if (--argc < 1) | ||
| 191 | break; | ||
| 192 | dgst_config.sigfile = *(++argv); | ||
| 193 | } else if (strcmp(*argv, "-keyform") == 0) { | ||
| 194 | if (--argc < 1) | ||
| 195 | break; | ||
| 196 | dgst_config.keyform = str2fmt(*(++argv)); | ||
| 197 | } | ||
| 198 | else if (strcmp(*argv, "-hex") == 0) | ||
| 199 | dgst_config.out_bin = 0; | ||
| 200 | else if (strcmp(*argv, "-binary") == 0) | ||
| 201 | dgst_config.out_bin = 1; | ||
| 202 | else if (strcmp(*argv, "-d") == 0) | ||
| 203 | dgst_config.debug = 1; | ||
| 204 | else if (!strcmp(*argv, "-hmac")) { | ||
| 205 | if (--argc < 1) | ||
| 206 | break; | ||
| 207 | dgst_config.hmac_key = *++argv; | ||
| 208 | } else if (!strcmp(*argv, "-mac")) { | ||
| 209 | if (--argc < 1) | ||
| 210 | break; | ||
| 211 | dgst_config.mac_name = *++argv; | ||
| 212 | } else if (strcmp(*argv, "-sigopt") == 0) { | ||
| 213 | if (--argc < 1) | ||
| 214 | break; | ||
| 215 | if (!dgst_config.sigopts) | ||
| 216 | dgst_config.sigopts = sk_OPENSSL_STRING_new_null(); | ||
| 217 | if (!dgst_config.sigopts || !sk_OPENSSL_STRING_push(dgst_config.sigopts, *(++argv))) | ||
| 218 | break; | ||
| 219 | } else if (strcmp(*argv, "-macopt") == 0) { | ||
| 220 | if (--argc < 1) | ||
| 221 | break; | ||
| 222 | if (!dgst_config.macopts) | ||
| 223 | dgst_config.macopts = sk_OPENSSL_STRING_new_null(); | ||
| 224 | if (!dgst_config.macopts || !sk_OPENSSL_STRING_push(dgst_config.macopts, *(++argv))) | ||
| 225 | break; | ||
| 226 | } else if ((dgst_config.m = EVP_get_digestbyname(&((*argv)[1]))) != NULL) | ||
| 227 | dgst_config.md = dgst_config.m; | ||
| 228 | else | ||
| 229 | break; | ||
| 230 | argc--; | ||
| 231 | argv++; | ||
| 232 | } | 369 | } |
| 370 | argc -= dgst_config.argsused; | ||
| 371 | argv += dgst_config.argsused; | ||
| 233 | 372 | ||
| 234 | if (dgst_config.do_verify && !dgst_config.sigfile) { | 373 | if (dgst_config.do_verify && !dgst_config.sigfile) { |
| 235 | BIO_printf(bio_err, "No signature to verify: use the -signature option\n"); | 374 | BIO_printf(bio_err, "No signature to verify: use the -signature option\n"); |
| 236 | goto end; | 375 | goto end; |
| 237 | } | 376 | } |
| 238 | if ((argc > 0) && (argv[0][0] == '-')) { /* bad option */ | ||
| 239 | BIO_printf(bio_err, "unknown option '%s'\n", *argv); | ||
| 240 | BIO_printf(bio_err, "options are\n"); | ||
| 241 | BIO_printf(bio_err, "-c to output the digest with separating colons\n"); | ||
| 242 | BIO_printf(bio_err, "-r to output the digest in coreutils format\n"); | ||
| 243 | BIO_printf(bio_err, "-d to output debug info\n"); | ||
| 244 | BIO_printf(bio_err, "-hex output as hex dump\n"); | ||
| 245 | BIO_printf(bio_err, "-binary output in binary form\n"); | ||
| 246 | BIO_printf(bio_err, "-sign file sign digest using private key in file\n"); | ||
| 247 | BIO_printf(bio_err, "-verify file verify a signature using public key in file\n"); | ||
| 248 | BIO_printf(bio_err, "-prverify file verify a signature using private key in file\n"); | ||
| 249 | BIO_printf(bio_err, "-keyform arg key file format (PEM)\n"); | ||
| 250 | BIO_printf(bio_err, "-out filename output to filename rather than stdout\n"); | ||
| 251 | BIO_printf(bio_err, "-signature file signature to verify\n"); | ||
| 252 | BIO_printf(bio_err, "-sigopt nm:v signature parameter\n"); | ||
| 253 | BIO_printf(bio_err, "-hmac key create hashed MAC with key\n"); | ||
| 254 | BIO_printf(bio_err, "-mac algorithm create MAC (not neccessarily HMAC)\n"); | ||
| 255 | BIO_printf(bio_err, "-macopt nm:v MAC algorithm parameters or key\n"); | ||
| 256 | |||
| 257 | EVP_MD_do_all_sorted(list_md_fn, bio_err); | ||
| 258 | goto end; | ||
| 259 | } | ||
| 260 | 377 | ||
| 261 | in = BIO_new(BIO_s_file()); | 378 | in = BIO_new(BIO_s_file()); |
| 262 | bmd = BIO_new(BIO_f_md()); | 379 | bmd = BIO_new(BIO_f_md()); |
