> In message <[EMAIL PROTECTED]> on Fri, 11 Oct 2002 
>15:54:39 +0200, [EMAIL PROTECTED] said:
> 
> dom>   Attached is a new applet for /usr/bin/openssl that we developped.
> 
> I think you forgot to actually attach it...

  Why of course :-(

-- 
Dominique QUATRAVAUX                           Ing�nieur d�veloppeur senior
01 44 42 00 35                                 IDEALX

/* oracle.c : decode nonstandard Oracle private key exports */

#include <stdio.h>
#include <string.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pkcs12.h>

#include "apps.h"
#define PROG oracle_main

int MAIN(int, char **);

int MAIN(int argc, char **argv)

{
        ENGINE *e = NULL;
        char **args, *infile = NULL, *outfile = NULL;
        char *passargin = NULL, *passargout = NULL;
        BIO *in = NULL, *out = NULL;
        int informat, outformat;
        X509_SIG *p8;
        RSA *rsa=NULL;
        EVP_PKEY *pkey=NULL;
        char *passin = NULL, *passout = NULL;
        int badarg = 0;
        char *engine=NULL;

        if (bio_err == NULL) bio_err = BIO_new_fp (stderr, BIO_NOCLOSE);

        informat=FORMAT_PEM;
        outformat=FORMAT_PEM;

        ERR_load_crypto_strings();
        OpenSSL_add_all_algorithms();
        args = argv + 1;
        while (!badarg && *args && *args[0] == '-') {
                if (!strcmp(*args,"-inform")) {
                        if (args[1]) {
                                args++;
                                informat=str2fmt(*args);
                        } else badarg = 1;
                } else if (!strcmp(*args,"-outform")) {
                        if (args[1]) {
                                args++;
                                outformat=str2fmt(*args);
                        } else badarg = 1;
                } else if (!strcmp(*args,"-passin"))
                        {
                        if (!args[1]) goto bad;
                        passargin= *(++args);
                        }
                else if (!strcmp(*args,"-passout"))
                        {
                        if (!args[1]) goto bad;
                        passargout= *(++args);
                        }
                else if (strcmp(*args,"-engine") == 0)
                        {
                        if (!args[1]) goto bad;
                        engine= *(++args);
                        }
                else if (!strcmp (*args, "-in")) {
                        if (args[1]) {
                                args++;
                                infile = *args;
                        } else badarg = 1;
                } else if (!strcmp (*args, "-out")) {
                        if (args[1]) {
                                args++;
                                outfile = *args;
                        } else badarg = 1;
                } else badarg = 1;
                args++;
        }

        // TODO: forcer la saisie d'un mot de passe.

        if (badarg) {
                bad:
                BIO_printf(bio_err, "Usage : oracle [options]\n");
                BIO_printf(bio_err, "where options are\n");
                BIO_printf(bio_err, "-in file        input file\n");
                BIO_printf(bio_err, "-inform X       input format (DER or PEM)\n");
                BIO_printf(bio_err, "-passin arg     input file pass phrase source\n");
                BIO_printf(bio_err, "-outform X      output format (DER or PEM)\n");
                BIO_printf(bio_err, "-out file       output file\n");
                BIO_printf(bio_err, "-passout arg    output file pass phrase 
source\n");
                BIO_printf(bio_err," -engine e       use engine e, possibly a hardware 
device.\n");
                return (1);
        }


        e = setup_engine(bio_err, engine, 0);
        if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
                BIO_printf(bio_err, "Error getting passwords\n");
                return (1);
        }

        /* To the best of our knowledge, Oracle key files are always 
password-protected. */
        if (! passin) {
          passin=malloc(51);
          EVP_read_pw_string(passin, 50, "Enter Encryption Password:", 0);
        }


        if (infile) {
                if (!(in = BIO_new_file(infile, "rb"))) {
                        BIO_printf(bio_err,
                                 "Can't open input file %s\n", infile);
                        return (1);
                }
        } else in = BIO_new_fp (stdin, BIO_NOCLOSE);

        if (outfile) {
                if (!(out = BIO_new_file (outfile, "wb"))) {
                        BIO_printf(bio_err,
                                 "Can't open output file %s\n", outfile);
                        return (1);
                }
        } else {
                out = BIO_new_fp (stdout, BIO_NOCLOSE);
#ifdef OPENSSL_SYS_VMS
                {
                        BIO *tmpbio = BIO_new(BIO_f_linebuffer());
                        out = BIO_push(tmpbio, out);
                }
#endif
        }

        
        if(informat == FORMAT_PEM) 
          p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
        else if(informat == FORMAT_ASN1)
          p8 = d2i_PKCS8_bio(in, NULL);
        else {
          BIO_printf(bio_err, "Bad format specified for key\n");
          return (1);
        }

        if (!p8) {
          BIO_printf (bio_err, "Error reading Oracle key\n", outfile);
          ERR_print_errors(bio_err);
          return (1);
        }

        /* OK this is not PKCS8 actually, it only looks like so. Inside the envelope
           there is a plain RSA key */

        rsa=PKCS12_item_decrypt_d2i(p8->algor, ASN1_ITEM_rptr(RSAPrivateKey), passin,
                                        strlen(passin), p8->digest, 1);

        X509_SIG_free(p8);

        if (!rsa) {
                BIO_printf(bio_err, "Error decrypting key\n", outfile);
                ERR_print_errors(bio_err);
                return (1);
        }

        pkey=EVP_PKEY_new();
        if (!EVP_PKEY_assign_RSA(pkey,rsa)) {
                BIO_printf(bio_err, "Error converting key\n", outfile);
                ERR_print_errors(bio_err);
                return (1);
        }
        // RSA_free(rsa);

        if(outformat == FORMAT_PEM) 
                PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
        else if(outformat == FORMAT_ASN1)
                i2d_PrivateKey_bio(out, pkey);
        else {
                BIO_printf(bio_err, "Bad format specified for key\n");
                        return (1);
        }

        EVP_PKEY_free(pkey);
        BIO_free_all(out);
        BIO_free(in);
        if(passin) OPENSSL_free(passin);
        if(passout) OPENSSL_free(passout);

        return (0);
}

Reply via email to