Main Page | Class List | Directories | File List | Class Members | File Members

ec.c

Go to the documentation of this file.
00001 /* apps/ec.c */
00002 /*
00003  * Written by Nils Larsch for the OpenSSL project.
00004  */
00005 /* ====================================================================
00006  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  *
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer. 
00014  *
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in
00017  *    the documentation and/or other materials provided with the
00018  *    distribution.
00019  *
00020  * 3. All advertising materials mentioning features or use of this
00021  *    software must display the following acknowledgment:
00022  *    "This product includes software developed by the OpenSSL Project
00023  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
00024  *
00025  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
00026  *    endorse or promote products derived from this software without
00027  *    prior written permission. For written permission, please contact
00028  *    openssl-core@openssl.org.
00029  *
00030  * 5. Products derived from this software may not be called "OpenSSL"
00031  *    nor may "OpenSSL" appear in their names without prior written
00032  *    permission of the OpenSSL Project.
00033  *
00034  * 6. Redistributions of any form whatsoever must retain the following
00035  *    acknowledgment:
00036  *    "This product includes software developed by the OpenSSL Project
00037  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
00038  *
00039  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
00040  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00041  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00042  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
00043  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00044  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00045  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00046  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00047  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00048  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00049  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00050  * OF THE POSSIBILITY OF SUCH DAMAGE.
00051  * ====================================================================
00052  *
00053  * This product includes cryptographic software written by Eric Young
00054  * (eay@cryptsoft.com).  This product includes software written by Tim
00055  * Hudson (tjh@cryptsoft.com).
00056  *
00057  */
00058 
00059 #include <openssl/opensslconf.h>
00060 #ifndef OPENSSL_NO_EC
00061 #include <stdio.h>
00062 #include <stdlib.h>
00063 #include <string.h>
00064 #include "apps.h"
00065 #include <openssl/bio.h>
00066 #include <openssl/err.h>
00067 #include <openssl/evp.h>
00068 #include <openssl/pem.h>
00069 
00070 #undef PROG
00071 #define PROG    ec_main
00072 
00073 /* -inform arg    - input format - default PEM (one of DER, NET or PEM)
00074  * -outform arg   - output format - default PEM
00075  * -in arg        - input file - default stdin
00076  * -out arg       - output file - default stdout
00077  * -des           - encrypt output if PEM format with DES in cbc mode
00078  * -text          - print a text version
00079  * -param_out     - print the elliptic curve parameters
00080  * -conv_form arg - specifies the point encoding form
00081  * -param_enc arg - specifies the parameter encoding
00082  */
00083 
00084 int MAIN(int, char **);
00085 
00086 int MAIN(int argc, char **argv)
00087 {
00088 #ifndef OPENSSL_NO_ENGINE
00089         ENGINE  *e = NULL;
00090 #endif
00091         int     ret = 1;
00092         EC_KEY  *eckey = NULL;
00093         const EC_GROUP *group;
00094         int     i, badops = 0;
00095         const EVP_CIPHER *enc = NULL;
00096         BIO     *in = NULL, *out = NULL;
00097         int     informat, outformat, text=0, noout=0;
00098         int     pubin = 0, pubout = 0, param_out = 0;
00099         char    *infile, *outfile, *prog, *engine;
00100         char    *passargin = NULL, *passargout = NULL;
00101         char    *passin = NULL, *passout = NULL;
00102         point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
00103         int     new_form = 0;
00104         int     asn1_flag = OPENSSL_EC_NAMED_CURVE;
00105         int     new_asn1_flag = 0;
00106 
00107         apps_startup();
00108 
00109         if (bio_err == NULL)
00110                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
00111                         BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
00112 
00113         if (!load_config(bio_err, NULL))
00114                 goto end;
00115 
00116         engine = NULL;
00117         infile = NULL;
00118         outfile = NULL;
00119         informat = FORMAT_PEM;
00120         outformat = FORMAT_PEM;
00121 
00122         prog = argv[0];
00123         argc--;
00124         argv++;
00125         while (argc >= 1)
00126                 {
00127                 if (strcmp(*argv,"-inform") == 0)
00128                         {
00129                         if (--argc < 1) goto bad;
00130                         informat=str2fmt(*(++argv));
00131                         }
00132                 else if (strcmp(*argv,"-outform") == 0)
00133                         {
00134                         if (--argc < 1) goto bad;
00135                         outformat=str2fmt(*(++argv));
00136                         }
00137                 else if (strcmp(*argv,"-in") == 0)
00138                         {
00139                         if (--argc < 1) goto bad;
00140                         infile= *(++argv);
00141                         }
00142                 else if (strcmp(*argv,"-out") == 0)
00143                         {
00144                         if (--argc < 1) goto bad;
00145                         outfile= *(++argv);
00146                         }
00147                 else if (strcmp(*argv,"-passin") == 0)
00148                         {
00149                         if (--argc < 1) goto bad;
00150                         passargin= *(++argv);
00151                         }
00152                 else if (strcmp(*argv,"-passout") == 0)
00153                         {
00154                         if (--argc < 1) goto bad;
00155                         passargout= *(++argv);
00156                         }
00157                 else if (strcmp(*argv, "-engine") == 0)
00158                         {
00159                         if (--argc < 1) goto bad;
00160                         engine= *(++argv);
00161                         }
00162                 else if (strcmp(*argv, "-noout") == 0)
00163                         noout = 1;
00164                 else if (strcmp(*argv, "-text") == 0)
00165                         text = 1;
00166                 else if (strcmp(*argv, "-conv_form") == 0)
00167                         {
00168                         if (--argc < 1)
00169                                 goto bad;
00170                         ++argv;
00171                         new_form = 1;
00172                         if (strcmp(*argv, "compressed") == 0)
00173                                 form = POINT_CONVERSION_COMPRESSED;
00174                         else if (strcmp(*argv, "uncompressed") == 0)
00175                                 form = POINT_CONVERSION_UNCOMPRESSED;
00176                         else if (strcmp(*argv, "hybrid") == 0)
00177                                 form = POINT_CONVERSION_HYBRID;
00178                         else
00179                                 goto bad;
00180                         }
00181                 else if (strcmp(*argv, "-param_enc") == 0)
00182                         {
00183                         if (--argc < 1)
00184                                 goto bad;
00185                         ++argv;
00186                         new_asn1_flag = 1;
00187                         if (strcmp(*argv, "named_curve") == 0)
00188                                 asn1_flag = OPENSSL_EC_NAMED_CURVE;
00189                         else if (strcmp(*argv, "explicit") == 0)
00190                                 asn1_flag = 0;
00191                         else
00192                                 goto bad;
00193                         }
00194                 else if (strcmp(*argv, "-param_out") == 0)
00195                         param_out = 1;
00196                 else if (strcmp(*argv, "-pubin") == 0)
00197                         pubin=1;
00198                 else if (strcmp(*argv, "-pubout") == 0)
00199                         pubout=1;
00200                 else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL)
00201                         {
00202                         BIO_printf(bio_err, "unknown option %s\n", *argv);
00203                         badops=1;
00204                         break;
00205                         }
00206                 argc--;
00207                 argv++;
00208                 }
00209 
00210         if (badops)
00211                 {
00212 bad:
00213                 BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
00214                 BIO_printf(bio_err, "where options are\n");
00215                 BIO_printf(bio_err, " -inform arg     input format - "
00216                                 "DER or PEM\n");
00217                 BIO_printf(bio_err, " -outform arg    output format - "
00218                                 "DER or PEM\n");
00219                 BIO_printf(bio_err, " -in arg         input file\n");
00220                 BIO_printf(bio_err, " -passin arg     input file pass "
00221                                 "phrase source\n");
00222                 BIO_printf(bio_err, " -out arg        output file\n");
00223                 BIO_printf(bio_err, " -passout arg    output file pass "
00224                                 "phrase source\n");
00225                 BIO_printf(bio_err, " -engine e       use engine e, "
00226                                 "possibly a hardware device.\n");
00227                 BIO_printf(bio_err, " -des            encrypt PEM output, "
00228                                 "instead of 'des' every other \n"
00229                                 "                 cipher "
00230                                 "supported by OpenSSL can be used\n");
00231                 BIO_printf(bio_err, " -text           print the key\n");
00232                 BIO_printf(bio_err, " -noout          don't print key out\n");
00233                 BIO_printf(bio_err, " -param_out      print the elliptic "
00234                                 "curve parameters\n");
00235                 BIO_printf(bio_err, " -conv_form arg  specifies the "
00236                                 "point conversion form \n");
00237                 BIO_printf(bio_err, "                 possible values:"
00238                                 " compressed\n");
00239                 BIO_printf(bio_err, "                                 "
00240                                 " uncompressed (default)\n");
00241                 BIO_printf(bio_err, "                                  "
00242                                 " hybrid\n");
00243                 BIO_printf(bio_err, " -param_enc arg  specifies the way"
00244                                 " the ec parameters are encoded\n");
00245                 BIO_printf(bio_err, "                 in the asn1 der "
00246                                 "encoding\n");
00247                 BIO_printf(bio_err, "                 possilbe values:"
00248                                 " named_curve (default)\n");
00249                 BIO_printf(bio_err,"                                  "
00250                                 "explicit\n");
00251                 goto end;
00252                 }
00253 
00254         ERR_load_crypto_strings();
00255 
00256 #ifndef OPENSSL_NO_ENGINE
00257         e = setup_engine(bio_err, engine, 0);
00258 #endif
00259 
00260         if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) 
00261                 {
00262                 BIO_printf(bio_err, "Error getting passwords\n");
00263                 goto end;
00264                 }
00265 
00266         in = BIO_new(BIO_s_file());
00267         out = BIO_new(BIO_s_file());
00268         if ((in == NULL) || (out == NULL))
00269                 {
00270                 ERR_print_errors(bio_err);
00271                 goto end;
00272                 }
00273 
00274         if (infile == NULL)
00275                 BIO_set_fp(in, stdin, BIO_NOCLOSE);
00276         else
00277                 {
00278                 if (BIO_read_filename(in, infile) <= 0)
00279                         {
00280                         perror(infile);
00281                         goto end;
00282                         }
00283                 }
00284 
00285         BIO_printf(bio_err, "read EC key\n");
00286         if (informat == FORMAT_ASN1) 
00287                 {
00288                 if (pubin) 
00289                         eckey = d2i_EC_PUBKEY_bio(in, NULL);
00290                 else 
00291                         eckey = d2i_ECPrivateKey_bio(in, NULL);
00292                 } 
00293         else if (informat == FORMAT_PEM) 
00294                 {
00295                 if (pubin) 
00296                         eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, 
00297                                 NULL);
00298                 else 
00299                         eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL,
00300                                 passin);
00301                 } 
00302         else
00303                 {
00304                 BIO_printf(bio_err, "bad input format specified for key\n");
00305                 goto end;
00306                 }
00307         if (eckey == NULL)
00308                 {
00309                 BIO_printf(bio_err,"unable to load Key\n");
00310                 ERR_print_errors(bio_err);
00311                 goto end;
00312                 }
00313 
00314         if (outfile == NULL)
00315                 {
00316                 BIO_set_fp(out, stdout, BIO_NOCLOSE);
00317 #ifdef OPENSSL_SYS_VMS
00318                         {
00319                         BIO *tmpbio = BIO_new(BIO_f_linebuffer());
00320                         out = BIO_push(tmpbio, out);
00321                         }
00322 #endif
00323                 }
00324         else
00325                 {
00326                 if (BIO_write_filename(out, outfile) <= 0)
00327                         {
00328                         perror(outfile);
00329                         goto end;
00330                         }
00331                 }
00332 
00333         group = EC_KEY_get0_group(eckey);
00334 
00335         if (new_form)
00336                 EC_KEY_set_conv_form(eckey, form);
00337 
00338         if (new_asn1_flag)
00339                 EC_KEY_set_asn1_flag(eckey, asn1_flag);
00340 
00341         if (text) 
00342                 if (!EC_KEY_print(out, eckey, 0))
00343                         {
00344                         perror(outfile);
00345                         ERR_print_errors(bio_err);
00346                         goto end;
00347                         }
00348 
00349         if (noout) 
00350                 goto end;
00351 
00352         BIO_printf(bio_err, "writing EC key\n");
00353         if (outformat == FORMAT_ASN1) 
00354                 {
00355                 if (param_out)
00356                         i = i2d_ECPKParameters_bio(out, group);
00357                 else if (pubin || pubout) 
00358                         i = i2d_EC_PUBKEY_bio(out, eckey);
00359                 else 
00360                         i = i2d_ECPrivateKey_bio(out, eckey);
00361                 } 
00362         else if (outformat == FORMAT_PEM) 
00363                 {
00364                 if (param_out)
00365                         i = PEM_write_bio_ECPKParameters(out, group);
00366                 else if (pubin || pubout)
00367                         i = PEM_write_bio_EC_PUBKEY(out, eckey);
00368                 else 
00369                         i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
00370                                                 NULL, 0, NULL, passout);
00371                 } 
00372         else 
00373                 {
00374                 BIO_printf(bio_err, "bad output format specified for "
00375                         "outfile\n");
00376                 goto end;
00377                 }
00378 
00379         if (!i)
00380                 {
00381                 BIO_printf(bio_err, "unable to write private key\n");
00382                 ERR_print_errors(bio_err);
00383                 }
00384         else
00385                 ret=0;
00386 end:
00387         if (in)
00388                 BIO_free(in);
00389         if (out)
00390                 BIO_free_all(out);
00391         if (eckey)
00392                 EC_KEY_free(eckey);
00393         if (passin)
00394                 OPENSSL_free(passin);
00395         if (passout)
00396                 OPENSSL_free(passout);
00397         apps_shutdown();
00398         OPENSSL_EXIT(ret);
00399 }
00400 #endif

© sourcejam.com 2005-2008