Bodo Möller | d4a8f90 | 2002-08-26 11:20:50 +0000 | [diff] [blame] | 1 | /* apps/ec.c */ |
| 2 | /* |
| 3 | * Written by Nils Larsch for the OpenSSL project. |
| 4 | */ |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 5 | /* ==================================================================== |
| 6 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * |
| 20 | * 3. All advertising materials mentioning features or use of this |
| 21 | * software must display the following acknowledgment: |
| 22 | * "This product includes software developed by the OpenSSL Project |
| 23 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 24 | * |
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 26 | * endorse or promote products derived from this software without |
| 27 | * prior written permission. For written permission, please contact |
| 28 | * openssl-core@openssl.org. |
| 29 | * |
| 30 | * 5. Products derived from this software may not be called "OpenSSL" |
| 31 | * nor may "OpenSSL" appear in their names without prior written |
| 32 | * permission of the OpenSSL Project. |
| 33 | * |
| 34 | * 6. Redistributions of any form whatsoever must retain the following |
| 35 | * acknowledgment: |
| 36 | * "This product includes software developed by the OpenSSL Project |
| 37 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 38 | * |
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 51 | * ==================================================================== |
| 52 | * |
| 53 | * This product includes cryptographic software written by Eric Young |
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 55 | * Hudson (tjh@cryptsoft.com). |
| 56 | * |
| 57 | */ |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 58 | |
Bodo Möller | d4a8f90 | 2002-08-26 11:20:50 +0000 | [diff] [blame] | 59 | #ifndef OPENSSL_NO_EC |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 60 | #include <stdio.h> |
| 61 | #include <stdlib.h> |
| 62 | #include <string.h> |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 63 | #include "apps.h" |
| 64 | #include <openssl/bio.h> |
| 65 | #include <openssl/err.h> |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 66 | #include <openssl/evp.h> |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 67 | #include <openssl/pem.h> |
| 68 | |
| 69 | #undef PROG |
Bodo Möller | d4a8f90 | 2002-08-26 11:20:50 +0000 | [diff] [blame] | 70 | #define PROG ec_main |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 71 | |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 72 | /* -inform arg - input format - default PEM (one of DER, NET or PEM) |
| 73 | * -outform arg - output format - default PEM |
| 74 | * -in arg - input file - default stdin |
| 75 | * -out arg - output file - default stdout |
| 76 | * -des - encrypt output if PEM format with DES in cbc mode |
| 77 | * -text - print a text version |
| 78 | * -param_out - print the elliptic curve parameters |
| 79 | * -conv_form arg - specifies the point encoding form |
| 80 | * -param_enc arg - specifies the parameter encoding |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 81 | */ |
| 82 | |
| 83 | int MAIN(int, char **); |
| 84 | |
| 85 | int MAIN(int argc, char **argv) |
| 86 | { |
Geoff Thorpe | 58ae65c | 2004-10-21 00:06:14 +0000 | [diff] [blame] | 87 | #ifndef OPENSSL_NO_ENGINE |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 88 | ENGINE *e = NULL; |
Geoff Thorpe | 58ae65c | 2004-10-21 00:06:14 +0000 | [diff] [blame] | 89 | #endif |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 90 | int ret = 1; |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 91 | EC_KEY *eckey = NULL; |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 92 | int i, badops = 0; |
| 93 | const EVP_CIPHER *enc = NULL; |
| 94 | BIO *in = NULL, *out = NULL; |
| 95 | int informat, outformat, text=0, noout=0; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 96 | int pubin = 0, pubout = 0, param_out = 0; |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 97 | char *infile, *outfile, *prog, *engine; |
| 98 | char *passargin = NULL, *passargout = NULL; |
| 99 | char *passin = NULL, *passout = NULL; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 100 | point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED; |
| 101 | int new_form = 0; |
| 102 | int asn1_flag = OPENSSL_EC_NAMED_CURVE; |
| 103 | int new_asn1_flag = 0; |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 104 | |
| 105 | apps_startup(); |
| 106 | |
| 107 | if (bio_err == NULL) |
| 108 | if ((bio_err=BIO_new(BIO_s_file())) != NULL) |
| 109 | BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT); |
| 110 | |
Dr. Stephen Henson | 3647bee | 2002-02-22 14:01:21 +0000 | [diff] [blame] | 111 | if (!load_config(bio_err, NULL)) |
| 112 | goto end; |
| 113 | |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 114 | engine = NULL; |
| 115 | infile = NULL; |
| 116 | outfile = NULL; |
| 117 | informat = FORMAT_PEM; |
| 118 | outformat = FORMAT_PEM; |
| 119 | |
| 120 | prog = argv[0]; |
| 121 | argc--; |
| 122 | argv++; |
| 123 | while (argc >= 1) |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 124 | { |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 125 | if (strcmp(*argv,"-inform") == 0) |
| 126 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 127 | if (--argc < 1) goto bad; |
| 128 | informat=str2fmt(*(++argv)); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 129 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 130 | else if (strcmp(*argv,"-outform") == 0) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 131 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 132 | if (--argc < 1) goto bad; |
| 133 | outformat=str2fmt(*(++argv)); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 134 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 135 | else if (strcmp(*argv,"-in") == 0) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 136 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 137 | if (--argc < 1) goto bad; |
| 138 | infile= *(++argv); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 139 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 140 | else if (strcmp(*argv,"-out") == 0) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 141 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 142 | if (--argc < 1) goto bad; |
| 143 | outfile= *(++argv); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 144 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 145 | else if (strcmp(*argv,"-passin") == 0) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 146 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 147 | if (--argc < 1) goto bad; |
| 148 | passargin= *(++argv); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 149 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 150 | else if (strcmp(*argv,"-passout") == 0) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 151 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 152 | if (--argc < 1) goto bad; |
| 153 | passargout= *(++argv); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 154 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 155 | else if (strcmp(*argv, "-engine") == 0) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 156 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 157 | if (--argc < 1) goto bad; |
| 158 | engine= *(++argv); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 159 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 160 | else if (strcmp(*argv, "-noout") == 0) |
| 161 | noout = 1; |
| 162 | else if (strcmp(*argv, "-text") == 0) |
| 163 | text = 1; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 164 | else if (strcmp(*argv, "-conv_form") == 0) |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 165 | { |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 166 | if (--argc < 1) |
| 167 | goto bad; |
| 168 | ++argv; |
| 169 | new_form = 1; |
| 170 | if (strcmp(*argv, "compressed") == 0) |
| 171 | form = POINT_CONVERSION_COMPRESSED; |
| 172 | else if (strcmp(*argv, "uncompressed") == 0) |
| 173 | form = POINT_CONVERSION_UNCOMPRESSED; |
| 174 | else if (strcmp(*argv, "hybrid") == 0) |
| 175 | form = POINT_CONVERSION_HYBRID; |
| 176 | else |
| 177 | goto bad; |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 178 | } |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 179 | else if (strcmp(*argv, "-param_enc") == 0) |
| 180 | { |
| 181 | if (--argc < 1) |
| 182 | goto bad; |
| 183 | ++argv; |
| 184 | new_asn1_flag = 1; |
| 185 | if (strcmp(*argv, "named_curve") == 0) |
| 186 | asn1_flag = OPENSSL_EC_NAMED_CURVE; |
| 187 | else if (strcmp(*argv, "explicit") == 0) |
| 188 | asn1_flag = 0; |
| 189 | else |
| 190 | goto bad; |
| 191 | } |
| 192 | else if (strcmp(*argv, "-param_out") == 0) |
| 193 | param_out = 1; |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 194 | else if (strcmp(*argv, "-pubin") == 0) |
| 195 | pubin=1; |
| 196 | else if (strcmp(*argv, "-pubout") == 0) |
| 197 | pubout=1; |
| 198 | else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 199 | { |
| 200 | BIO_printf(bio_err, "unknown option %s\n", *argv); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 201 | badops=1; |
| 202 | break; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 203 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 204 | argc--; |
| 205 | argv++; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 206 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 207 | |
| 208 | if (badops) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 209 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 210 | bad: |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 211 | BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 212 | BIO_printf(bio_err, "where options are\n"); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 213 | BIO_printf(bio_err, " -inform arg input format - " |
| 214 | "DER or PEM\n"); |
| 215 | BIO_printf(bio_err, " -outform arg output format - " |
| 216 | "DER or PEM\n"); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 217 | BIO_printf(bio_err, " -in arg input file\n"); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 218 | BIO_printf(bio_err, " -passin arg input file pass " |
| 219 | "phrase source\n"); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 220 | BIO_printf(bio_err, " -out arg output file\n"); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 221 | BIO_printf(bio_err, " -passout arg output file pass " |
| 222 | "phrase source\n"); |
| 223 | BIO_printf(bio_err, " -engine e use engine e, " |
| 224 | "possibly a hardware device.\n"); |
| 225 | BIO_printf(bio_err, " -des encrypt PEM output, " |
| 226 | "instead of 'des' every other \n" |
| 227 | " cipher " |
| 228 | "supported by OpenSSL can be used\n"); |
| 229 | BIO_printf(bio_err, " -text print the key\n"); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 230 | BIO_printf(bio_err, " -noout don't print key out\n"); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 231 | BIO_printf(bio_err, " -param_out print the elliptic " |
| 232 | "curve parameters\n"); |
| 233 | BIO_printf(bio_err, " -conv_form arg specifies the " |
| 234 | "point conversion form \n"); |
Bodo Möller | c96f0fd | 2002-08-26 14:50:52 +0000 | [diff] [blame] | 235 | BIO_printf(bio_err, " possible values:" |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 236 | " compressed\n"); |
Bodo Möller | c96f0fd | 2002-08-26 14:50:52 +0000 | [diff] [blame] | 237 | BIO_printf(bio_err, " " |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 238 | " uncompressed (default)\n"); |
| 239 | BIO_printf(bio_err, " " |
| 240 | " hybrid\n"); |
| 241 | BIO_printf(bio_err, " -param_enc arg specifies the way" |
| 242 | " the ec parameters are encoded\n"); |
| 243 | BIO_printf(bio_err, " in the asn1 der " |
| 244 | "encoding\n"); |
Bodo Möller | c96f0fd | 2002-08-26 14:50:52 +0000 | [diff] [blame] | 245 | BIO_printf(bio_err, " possilbe values:" |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 246 | " named_curve (default)\n"); |
Bodo Möller | c96f0fd | 2002-08-26 14:50:52 +0000 | [diff] [blame] | 247 | BIO_printf(bio_err," " |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 248 | "explicit\n"); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 249 | goto end; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 250 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 251 | |
| 252 | ERR_load_crypto_strings(); |
| 253 | |
Geoff Thorpe | 58ae65c | 2004-10-21 00:06:14 +0000 | [diff] [blame] | 254 | #ifndef OPENSSL_NO_ENGINE |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 255 | e = setup_engine(bio_err, engine, 0); |
Geoff Thorpe | 58ae65c | 2004-10-21 00:06:14 +0000 | [diff] [blame] | 256 | #endif |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 257 | |
| 258 | if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 259 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 260 | BIO_printf(bio_err, "Error getting passwords\n"); |
| 261 | goto end; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 262 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 263 | |
| 264 | in = BIO_new(BIO_s_file()); |
| 265 | out = BIO_new(BIO_s_file()); |
| 266 | if ((in == NULL) || (out == NULL)) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 267 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 268 | ERR_print_errors(bio_err); |
| 269 | goto end; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 270 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 271 | |
| 272 | if (infile == NULL) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 273 | BIO_set_fp(in, stdin, BIO_NOCLOSE); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 274 | else |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 275 | { |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 276 | if (BIO_read_filename(in, infile) <= 0) |
| 277 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 278 | perror(infile); |
| 279 | goto end; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 280 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 281 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 282 | |
Bodo Möller | d4a8f90 | 2002-08-26 11:20:50 +0000 | [diff] [blame] | 283 | BIO_printf(bio_err, "read EC key\n"); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 284 | if (informat == FORMAT_ASN1) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 285 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 286 | if (pubin) |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 287 | eckey = d2i_EC_PUBKEY_bio(in, NULL); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 288 | else |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 289 | eckey = d2i_ECPrivateKey_bio(in, NULL); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 290 | } |
| 291 | else if (informat == FORMAT_PEM) |
| 292 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 293 | if (pubin) |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 294 | eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 295 | NULL); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 296 | else |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 297 | eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 298 | passin); |
| 299 | } |
| 300 | else |
| 301 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 302 | BIO_printf(bio_err, "bad input format specified for key\n"); |
| 303 | goto end; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 304 | } |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 305 | if (eckey == NULL) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 306 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 307 | BIO_printf(bio_err,"unable to load Key\n"); |
| 308 | ERR_print_errors(bio_err); |
| 309 | goto end; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 310 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 311 | |
| 312 | if (outfile == NULL) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 313 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 314 | BIO_set_fp(out, stdout, BIO_NOCLOSE); |
| 315 | #ifdef OPENSSL_SYS_VMS |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 316 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 317 | BIO *tmpbio = BIO_new(BIO_f_linebuffer()); |
| 318 | out = BIO_push(tmpbio, out); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 319 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 320 | #endif |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 321 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 322 | else |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 323 | { |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 324 | if (BIO_write_filename(out, outfile) <= 0) |
| 325 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 326 | perror(outfile); |
| 327 | goto end; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 328 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 329 | } |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 330 | |
| 331 | if (new_form) |
| 332 | { |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 333 | EC_GROUP_set_point_conversion_form(eckey->group, form); |
| 334 | eckey->conv_form = form; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | if (new_asn1_flag) |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 338 | EC_GROUP_set_asn1_flag(eckey->group, asn1_flag); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 339 | |
| 340 | if (text) |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 341 | if (!EC_KEY_print(out, eckey, 0)) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 342 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 343 | perror(outfile); |
| 344 | ERR_print_errors(bio_err); |
| 345 | goto end; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 346 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 347 | |
| 348 | if (noout) |
| 349 | goto end; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 350 | |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 351 | BIO_printf(bio_err, "writing EC key\n"); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 352 | if (outformat == FORMAT_ASN1) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 353 | { |
| 354 | if (param_out) |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 355 | i = i2d_ECPKParameters_bio(out, eckey->group); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 356 | else if (pubin || pubout) |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 357 | i = i2d_EC_PUBKEY_bio(out, eckey); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 358 | else |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 359 | i = i2d_ECPrivateKey_bio(out, eckey); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 360 | } |
| 361 | else if (outformat == FORMAT_PEM) |
| 362 | { |
| 363 | if (param_out) |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 364 | i = PEM_write_bio_ECPKParameters(out, eckey->group); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 365 | else if (pubin || pubout) |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 366 | i = PEM_write_bio_EC_PUBKEY(out, eckey); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 367 | else |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 368 | i = PEM_write_bio_ECPrivateKey(out, eckey, enc, |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 369 | NULL, 0, NULL, passout); |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | BIO_printf(bio_err, "bad output format specified for " |
| 374 | "outfile\n"); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 375 | goto end; |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 378 | if (!i) |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 379 | { |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 380 | BIO_printf(bio_err, "unable to write private key\n"); |
| 381 | ERR_print_errors(bio_err); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 382 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 383 | else |
| 384 | ret=0; |
| 385 | end: |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 386 | if (in) |
| 387 | BIO_free(in); |
| 388 | if (out) |
| 389 | BIO_free_all(out); |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 390 | if (eckey) |
| 391 | EC_KEY_free(eckey); |
Bodo Möller | eefa6e4 | 2002-07-23 09:51:57 +0000 | [diff] [blame] | 392 | if (passin) |
| 393 | OPENSSL_free(passin); |
| 394 | if (passout) |
| 395 | OPENSSL_free(passout); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 396 | apps_shutdown(); |
Richard Levitte | 1c3e4a3 | 2002-12-03 16:33:03 +0000 | [diff] [blame] | 397 | OPENSSL_EXIT(ret); |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 398 | } |
| 399 | #endif |