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

rsa.c

Go to the documentation of this file.
00001 /* apps/rsa.c */
00002 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
00003  * All rights reserved.
00004  *
00005  * This package is an SSL implementation written
00006  * by Eric Young (eay@cryptsoft.com).
00007  * The implementation was written so as to conform with Netscapes SSL.
00008  * 
00009  * This library is free for commercial and non-commercial use as long as
00010  * the following conditions are aheared to.  The following conditions
00011  * apply to all code found in this distribution, be it the RC4, RSA,
00012  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
00013  * included with this distribution is covered by the same copyright terms
00014  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
00015  * 
00016  * Copyright remains Eric Young's, and as such any Copyright notices in
00017  * the code are not to be removed.
00018  * If this package is used in a product, Eric Young should be given attribution
00019  * as the author of the parts of the library used.
00020  * This can be in the form of a textual message at program startup or
00021  * in documentation (online or textual) provided with the package.
00022  * 
00023  * Redistribution and use in source and binary forms, with or without
00024  * modification, are permitted provided that the following conditions
00025  * are met:
00026  * 1. Redistributions of source code must retain the copyright
00027  *    notice, this list of conditions and the following disclaimer.
00028  * 2. Redistributions in binary form must reproduce the above copyright
00029  *    notice, this list of conditions and the following disclaimer in the
00030  *    documentation and/or other materials provided with the distribution.
00031  * 3. All advertising materials mentioning features or use of this software
00032  *    must display the following acknowledgement:
00033  *    "This product includes cryptographic software written by
00034  *     Eric Young (eay@cryptsoft.com)"
00035  *    The word 'cryptographic' can be left out if the rouines from the library
00036  *    being used are not cryptographic related :-).
00037  * 4. If you include any Windows specific code (or a derivative thereof) from 
00038  *    the apps directory (application code) you must include an acknowledgement:
00039  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
00040  * 
00041  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
00042  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00043  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00044  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00045  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00046  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00047  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00048  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00049  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00050  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00051  * SUCH DAMAGE.
00052  * 
00053  * The licence and distribution terms for any publically available version or
00054  * derivative of this code cannot be changed.  i.e. this code cannot simply be
00055  * copied and put under another distribution licence
00056  * [including the GNU Public Licence.]
00057  */
00058 
00059 #include <openssl/opensslconf.h>
00060 #ifndef OPENSSL_NO_RSA
00061 #include <stdio.h>
00062 #include <stdlib.h>
00063 #include <string.h>
00064 #include <time.h>
00065 #include "apps.h"
00066 #include <openssl/bio.h>
00067 #include <openssl/err.h>
00068 #include <openssl/rsa.h>
00069 #include <openssl/evp.h>
00070 #include <openssl/x509.h>
00071 #include <openssl/pem.h>
00072 #include <openssl/bn.h>
00073 
00074 #undef PROG
00075 #define PROG    rsa_main
00076 
00077 /* -inform arg  - input format - default PEM (one of DER, NET or PEM)
00078  * -outform arg - output format - default PEM
00079  * -in arg      - input file - default stdin
00080  * -out arg     - output file - default stdout
00081  * -des         - encrypt output if PEM format with DES in cbc mode
00082  * -des3        - encrypt output if PEM format
00083  * -idea        - encrypt output if PEM format
00084  * -aes128      - encrypt output if PEM format
00085  * -aes192      - encrypt output if PEM format
00086  * -aes256      - encrypt output if PEM format
00087  * -text        - print a text version
00088  * -modulus     - print the RSA key modulus
00089  * -check       - verify key consistency
00090  * -pubin       - Expect a public key in input file.
00091  * -pubout      - Output a public key.
00092  */
00093 
00094 int MAIN(int, char **);
00095 
00096 int MAIN(int argc, char **argv)
00097         {
00098         ENGINE *e = NULL;
00099         int ret=1;
00100         RSA *rsa=NULL;
00101         int i,badops=0, sgckey=0;
00102         const EVP_CIPHER *enc=NULL;
00103         BIO *out=NULL;
00104         int informat,outformat,text=0,check=0,noout=0;
00105         int pubin = 0, pubout = 0;
00106         char *infile,*outfile,*prog;
00107         char *passargin = NULL, *passargout = NULL;
00108         char *passin = NULL, *passout = NULL;
00109 #ifndef OPENSSL_NO_ENGINE
00110         char *engine=NULL;
00111 #endif
00112         int modulus=0;
00113 
00114         apps_startup();
00115 
00116         if (bio_err == NULL)
00117                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
00118                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
00119 
00120         if (!load_config(bio_err, NULL))
00121                 goto end;
00122 
00123         infile=NULL;
00124         outfile=NULL;
00125         informat=FORMAT_PEM;
00126         outformat=FORMAT_PEM;
00127 
00128         prog=argv[0];
00129         argc--;
00130         argv++;
00131         while (argc >= 1)
00132                 {
00133                 if      (strcmp(*argv,"-inform") == 0)
00134                         {
00135                         if (--argc < 1) goto bad;
00136                         informat=str2fmt(*(++argv));
00137                         }
00138                 else if (strcmp(*argv,"-outform") == 0)
00139                         {
00140                         if (--argc < 1) goto bad;
00141                         outformat=str2fmt(*(++argv));
00142                         }
00143                 else if (strcmp(*argv,"-in") == 0)
00144                         {
00145                         if (--argc < 1) goto bad;
00146                         infile= *(++argv);
00147                         }
00148                 else if (strcmp(*argv,"-out") == 0)
00149                         {
00150                         if (--argc < 1) goto bad;
00151                         outfile= *(++argv);
00152                         }
00153                 else if (strcmp(*argv,"-passin") == 0)
00154                         {
00155                         if (--argc < 1) goto bad;
00156                         passargin= *(++argv);
00157                         }
00158                 else if (strcmp(*argv,"-passout") == 0)
00159                         {
00160                         if (--argc < 1) goto bad;
00161                         passargout= *(++argv);
00162                         }
00163 #ifndef OPENSSL_NO_ENGINE
00164                 else if (strcmp(*argv,"-engine") == 0)
00165                         {
00166                         if (--argc < 1) goto bad;
00167                         engine= *(++argv);
00168                         }
00169 #endif
00170                 else if (strcmp(*argv,"-sgckey") == 0)
00171                         sgckey=1;
00172                 else if (strcmp(*argv,"-pubin") == 0)
00173                         pubin=1;
00174                 else if (strcmp(*argv,"-pubout") == 0)
00175                         pubout=1;
00176                 else if (strcmp(*argv,"-noout") == 0)
00177                         noout=1;
00178                 else if (strcmp(*argv,"-text") == 0)
00179                         text=1;
00180                 else if (strcmp(*argv,"-modulus") == 0)
00181                         modulus=1;
00182                 else if (strcmp(*argv,"-check") == 0)
00183                         check=1;
00184                 else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL)
00185                         {
00186                         BIO_printf(bio_err,"unknown option %s\n",*argv);
00187                         badops=1;
00188                         break;
00189                         }
00190                 argc--;
00191                 argv++;
00192                 }
00193 
00194         if (badops)
00195                 {
00196 bad:
00197                 BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
00198                 BIO_printf(bio_err,"where options are\n");
00199                 BIO_printf(bio_err," -inform arg     input format - one of DER NET PEM\n");
00200                 BIO_printf(bio_err," -outform arg    output format - one of DER NET PEM\n");
00201                 BIO_printf(bio_err," -in arg         input file\n");
00202                 BIO_printf(bio_err," -sgckey         Use IIS SGC key format\n");
00203                 BIO_printf(bio_err," -passin arg     input file pass phrase source\n");
00204                 BIO_printf(bio_err," -out arg        output file\n");
00205                 BIO_printf(bio_err," -passout arg    output file pass phrase source\n");
00206                 BIO_printf(bio_err," -des            encrypt PEM output with cbc des\n");
00207                 BIO_printf(bio_err," -des3           encrypt PEM output with ede cbc des using 168 bit key\n");
00208 #ifndef OPENSSL_NO_IDEA
00209                 BIO_printf(bio_err," -idea           encrypt PEM output with cbc idea\n");
00210 #endif
00211 #ifndef OPENSSL_NO_AES
00212                 BIO_printf(bio_err," -aes128, -aes192, -aes256\n");
00213                 BIO_printf(bio_err,"                 encrypt PEM output with cbc aes\n");
00214 #endif
00215                 BIO_printf(bio_err," -text           print the key in text\n");
00216                 BIO_printf(bio_err," -noout          don't print key out\n");
00217                 BIO_printf(bio_err," -modulus        print the RSA key modulus\n");
00218                 BIO_printf(bio_err," -check          verify key consistency\n");
00219                 BIO_printf(bio_err," -pubin          expect a public key in input file\n");
00220                 BIO_printf(bio_err," -pubout         output a public key\n");
00221 #ifndef OPENSSL_NO_ENGINE
00222                 BIO_printf(bio_err," -engine e       use engine e, possibly a hardware device.\n");
00223 #endif
00224                 goto end;
00225                 }
00226 
00227         ERR_load_crypto_strings();
00228 
00229 #ifndef OPENSSL_NO_ENGINE
00230         e = setup_engine(bio_err, engine, 0);
00231 #endif
00232 
00233         if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
00234                 BIO_printf(bio_err, "Error getting passwords\n");
00235                 goto end;
00236         }
00237 
00238         if(check && pubin) {
00239                 BIO_printf(bio_err, "Only private keys can be checked\n");
00240                 goto end;
00241         }
00242 
00243         out=BIO_new(BIO_s_file());
00244 
00245         {
00246                 EVP_PKEY        *pkey;
00247 
00248                 if (pubin)
00249                         pkey = load_pubkey(bio_err, infile,
00250                                 (informat == FORMAT_NETSCAPE && sgckey ?
00251                                         FORMAT_IISSGC : informat), 1,
00252                                 passin, e, "Public Key");
00253                 else
00254                         pkey = load_key(bio_err, infile,
00255                                 (informat == FORMAT_NETSCAPE && sgckey ?
00256                                         FORMAT_IISSGC : informat), 1,
00257                                 passin, e, "Private Key");
00258 
00259                 if (pkey != NULL)
00260                 rsa = pkey == NULL ? NULL : EVP_PKEY_get1_RSA(pkey);
00261                 EVP_PKEY_free(pkey);
00262         }
00263 
00264         if (rsa == NULL)
00265                 {
00266                 ERR_print_errors(bio_err);
00267                 goto end;
00268                 }
00269 
00270         if (outfile == NULL)
00271                 {
00272                 BIO_set_fp(out,stdout,BIO_NOCLOSE);
00273 #ifdef OPENSSL_SYS_VMS
00274                 {
00275                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
00276                 out = BIO_push(tmpbio, out);
00277                 }
00278 #endif
00279                 }
00280         else
00281                 {
00282                 if (BIO_write_filename(out,outfile) <= 0)
00283                         {
00284                         perror(outfile);
00285                         goto end;
00286                         }
00287                 }
00288 
00289         if (text) 
00290                 if (!RSA_print(out,rsa,0))
00291                         {
00292                         perror(outfile);
00293                         ERR_print_errors(bio_err);
00294                         goto end;
00295                         }
00296 
00297         if (modulus)
00298                 {
00299                 BIO_printf(out,"Modulus=");
00300                 BN_print(out,rsa->n);
00301                 BIO_printf(out,"\n");
00302                 }
00303 
00304         if (check)
00305                 {
00306                 int r = RSA_check_key(rsa);
00307 
00308                 if (r == 1)
00309                         BIO_printf(out,"RSA key ok\n");
00310                 else if (r == 0)
00311                         {
00312                         unsigned long err;
00313 
00314                         while ((err = ERR_peek_error()) != 0 &&
00315                                 ERR_GET_LIB(err) == ERR_LIB_RSA &&
00316                                 ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
00317                                 ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE)
00318                                 {
00319                                 BIO_printf(out, "RSA key error: %s\n", ERR_reason_error_string(err));
00320                                 ERR_get_error(); /* remove e from error stack */
00321                                 }
00322                         }
00323                 
00324                 if (r == -1 || ERR_peek_error() != 0) /* should happen only if r == -1 */
00325                         {
00326                         ERR_print_errors(bio_err);
00327                         goto end;
00328                         }
00329                 }
00330                 
00331         if (noout)
00332                 {
00333                 ret = 0;
00334                 goto end;
00335                 }
00336         BIO_printf(bio_err,"writing RSA key\n");
00337         if      (outformat == FORMAT_ASN1) {
00338                 if(pubout || pubin) i=i2d_RSA_PUBKEY_bio(out,rsa);
00339                 else i=i2d_RSAPrivateKey_bio(out,rsa);
00340         }
00341 #ifndef OPENSSL_NO_RC4
00342         else if (outformat == FORMAT_NETSCAPE)
00343                 {
00344                 unsigned char *p,*pp;
00345                 int size;
00346 
00347                 i=1;
00348                 size=i2d_RSA_NET(rsa,NULL,NULL, sgckey);
00349                 if ((p=(unsigned char *)OPENSSL_malloc(size)) == NULL)
00350                         {
00351                         BIO_printf(bio_err,"Memory allocation failure\n");
00352                         goto end;
00353                         }
00354                 pp=p;
00355                 i2d_RSA_NET(rsa,&p,NULL, sgckey);
00356                 BIO_write(out,(char *)pp,size);
00357                 OPENSSL_free(pp);
00358                 }
00359 #endif
00360         else if (outformat == FORMAT_PEM) {
00361                 if(pubout || pubin)
00362                     i=PEM_write_bio_RSA_PUBKEY(out,rsa);
00363                 else i=PEM_write_bio_RSAPrivateKey(out,rsa,
00364                                                 enc,NULL,0,NULL,passout);
00365         } else  {
00366                 BIO_printf(bio_err,"bad output format specified for outfile\n");
00367                 goto end;
00368                 }
00369         if (!i)
00370                 {
00371                 BIO_printf(bio_err,"unable to write key\n");
00372                 ERR_print_errors(bio_err);
00373                 }
00374         else
00375                 ret=0;
00376 end:
00377         if(out != NULL) BIO_free_all(out);
00378         if(rsa != NULL) RSA_free(rsa);
00379         if(passin) OPENSSL_free(passin);
00380         if(passout) OPENSSL_free(passout);
00381         apps_shutdown();
00382         OPENSSL_EXIT(ret);
00383         }
00384 #else /* !OPENSSL_NO_RSA */
00385 
00386 # if PEDANTIC
00387 static void *dummy=&dummy;
00388 # endif
00389 
00390 #endif

© sourcejam.com 2005-2008