00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
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
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
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();
00321 }
00322 }
00323
00324 if (r == -1 || ERR_peek_error() != 0)
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
00385
00386 # if PEDANTIC
00387 static void *dummy=&dummy;
00388 # endif
00389
00390 #endif