]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/spkac.c
Fix a bundle of trailing spaces in several files
[thirdparty/openssl.git] / apps / spkac.c
CommitLineData
0f113f3e 1/*
846e33c7 2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
8ce97163 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
8ce97163 8 */
846e33c7 9
8ce97163
DSH
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <time.h>
14#include "apps.h"
15#include <openssl/bio.h>
de83c122 16#include <openssl/conf.h>
8ce97163 17#include <openssl/err.h>
8ce97163 18#include <openssl/evp.h>
37634c8b 19#include <openssl/lhash.h>
8ce97163
DSH
20#include <openssl/x509.h>
21#include <openssl/pem.h>
22
7e1b7485
RS
23typedef enum OPTION_choice {
24 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
25 OPT_NOOUT, OPT_PUBKEY, OPT_VERIFY, OPT_IN, OPT_OUT,
26 OPT_ENGINE, OPT_KEY, OPT_CHALLENGE, OPT_PASSIN, OPT_SPKAC,
27 OPT_SPKSECT
28} OPTION_CHOICE;
29
44c83ebd 30const OPTIONS spkac_options[] = {
7e1b7485
RS
31 {"help", OPT_HELP, '-', "Display this summary"},
32 {"in", OPT_IN, '<', "Input file"},
33 {"out", OPT_OUT, '>', "Output file"},
34 {"key", OPT_KEY, '<', "Create SPKAC using private key"},
35 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
36 {"challenge", OPT_CHALLENGE, 's', "Challenge string"},
37 {"spkac", OPT_SPKAC, 's', "Alternative SPKAC name"},
38 {"noout", OPT_NOOUT, '-', "Don't print SPKAC"},
39 {"pubkey", OPT_PUBKEY, '-', "Output public key"},
40 {"verify", OPT_VERIFY, '-', "Verify SPKAC signature"},
12d56b29
F
41 {"spksect", OPT_SPKSECT, 's',
42 "Specify the name of an SPKAC-dedicated section of configuration"},
7e1b7485
RS
43#ifndef OPENSSL_NO_ENGINE
44 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
45#endif
46 {NULL}
47};
667ac4ec 48
7e1b7485 49int spkac_main(int argc, char **argv)
0f113f3e 50{
cc01d217 51 BIO *out = NULL;
0f113f3e 52 CONF *conf = NULL;
7e1b7485 53 ENGINE *e = NULL;
0f113f3e 54 EVP_PKEY *pkey = NULL;
7e1b7485 55 NETSCAPE_SPKI *spki = NULL;
333b070e 56 char *challenge = NULL, *keyfile = NULL;
7e1b7485
RS
57 char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL;
58 char *spkstr = NULL, *prog;
59 const char *spkac = "SPKAC", *spksect = "default";
60 int i, ret = 1, verify = 0, noout = 0, pubkey = 0;
61 OPTION_CHOICE o;
62
63 prog = opt_init(argc, argv, spkac_options);
64 while ((o = opt_next()) != OPT_EOF) {
65 switch (o) {
66 case OPT_EOF:
67 case OPT_ERR:
03358517 68 opthelp:
7e1b7485
RS
69 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
70 goto end;
71 case OPT_HELP:
72 opt_help(spkac_options);
73 ret = 0;
74 goto end;
75 case OPT_IN:
76 infile = opt_arg();
77 break;
78 case OPT_OUT:
79 outfile = opt_arg();
80 break;
81 case OPT_NOOUT:
0f113f3e 82 noout = 1;
7e1b7485
RS
83 break;
84 case OPT_PUBKEY:
0f113f3e 85 pubkey = 1;
7e1b7485
RS
86 break;
87 case OPT_VERIFY:
0f113f3e 88 verify = 1;
7e1b7485
RS
89 break;
90 case OPT_PASSIN:
91 passinarg = opt_arg();
92 break;
93 case OPT_KEY:
94 keyfile = opt_arg();
95 break;
96 case OPT_CHALLENGE:
97 challenge = opt_arg();
98 break;
99 case OPT_SPKAC:
100 spkac = opt_arg();
101 break;
102 case OPT_SPKSECT:
103 spksect = opt_arg();
104 break;
105 case OPT_ENGINE:
333b070e 106 e = setup_engine(opt_arg(), 0);
7e1b7485 107 break;
7e1b7485 108 }
0f113f3e 109 }
7e1b7485 110 argc = opt_num_rest();
03358517
KR
111 if (argc != 0)
112 goto opthelp;
8ce97163 113
7e1b7485 114 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
0f113f3e
MC
115 BIO_printf(bio_err, "Error getting password\n");
116 goto end;
117 }
5270e702 118
0f113f3e 119 if (keyfile) {
7e1b7485 120 pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL,
0f113f3e
MC
121 FORMAT_PEM, 1, passin, e, "private key");
122 if (!pkey) {
123 goto end;
124 }
125 spki = NETSCAPE_SPKI_new();
126 if (challenge)
127 ASN1_STRING_set(spki->spkac->challenge,
128 challenge, (int)strlen(challenge));
129 NETSCAPE_SPKI_set_pubkey(spki, pkey);
130 NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
131 spkstr = NETSCAPE_SPKI_b64_encode(spki);
8ce97163 132
bdd58d98 133 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
08f6ae5b
MC
134 if (out == NULL) {
135 OPENSSL_free(spkstr);
0f113f3e 136 goto end;
08f6ae5b 137 }
0f113f3e
MC
138 BIO_printf(out, "SPKAC=%s\n", spkstr);
139 OPENSSL_free(spkstr);
140 ret = 0;
141 goto end;
142 }
8ce97163 143
cc01d217 144 if ((conf = app_load_config(infile)) == NULL)
0f113f3e 145 goto end;
8ce97163 146
0f113f3e 147 spkstr = NCONF_get_string(conf, spksect, spkac);
8ce97163 148
96487cdd 149 if (spkstr == NULL) {
0f113f3e
MC
150 BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
151 ERR_print_errors(bio_err);
152 goto end;
153 }
8ce97163 154
0f113f3e 155 spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
8ce97163 156
0f113f3e
MC
157 if (!spki) {
158 BIO_printf(bio_err, "Error loading SPKAC\n");
159 ERR_print_errors(bio_err);
160 goto end;
161 }
8ce97163 162
bdd58d98 163 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
7e1b7485 164 if (out == NULL)
0f113f3e 165 goto end;
8ce97163 166
0f113f3e
MC
167 if (!noout)
168 NETSCAPE_SPKI_print(out, spki);
169 pkey = NETSCAPE_SPKI_get_pubkey(spki);
170 if (verify) {
171 i = NETSCAPE_SPKI_verify(spki, pkey);
172 if (i > 0)
173 BIO_printf(bio_err, "Signature OK\n");
174 else {
175 BIO_printf(bio_err, "Signature Failure\n");
176 ERR_print_errors(bio_err);
177 goto end;
178 }
179 }
180 if (pubkey)
181 PEM_write_bio_PUBKEY(out, pkey);
8ce97163 182
0f113f3e 183 ret = 0;
8ce97163 184
0f113f3e
MC
185 end:
186 NCONF_free(conf);
187 NETSCAPE_SPKI_free(spki);
0f113f3e
MC
188 BIO_free_all(out);
189 EVP_PKEY_free(pkey);
dd1abd44 190 release_engine(e);
b548a1f1 191 OPENSSL_free(passin);
7e1b7485 192 return (ret);
0f113f3e 193}