On Fri, 2005-04-01 at 07:31 +0530, Denis wrote:
> Don't you need to allocate some memory to be able to store the RSA key 
> in r_rsa?

I assumed that would happen automatically due to the 'pointer-to-
pointer' parameter. 

Best regards,
  jules


BTW: Doing the following solved my problem:

RSA *read_pub_key(const char *pubkey_file)
{
        RSA *key = NULL;
        BIO *bp;

        ERR_load_crypto_strings();

        bp = BIO_new(BIO_s_file());
        if (BIO_read_filename(bp, pubkey_file) <= 0) {
                perror(pubkey_file);
                goto exit;;
        }

        key = PEM_read_bio_RSAPublicKey(bp, NULL, NULL, NULL);
        if (!key) {
                ERR_print_errors_fp(stderr);
                goto exit;
        }

exit:
        BIO_free(bp);

        return key;
}

RSA *read_priv_key(const char *privkey_file)
{
        RSA *key = NULL;
        BIO *bp;

        SSLeay_add_all_algorithms();
        ERR_load_PEM_strings();

        bp = BIO_new(BIO_s_file());
        if (BIO_read_filename(bp, privkey_file) <= 0) {
                perror(privkey_file);
                goto exit;
        }

        key = PEM_read_bio_RSAPrivateKey(bp, NULL, NULL, NULL);
        if (!key) {
                ERR_print_errors_fp(stderr);
                goto exit;
        }

exit:
        BIO_free(bp);

        return key;
}

void create_rsa_files(const char *pubkey_file,
                      const char *privkey_file)
{
        RSA *rsa;
        RSA *pub_rsa;
        RSA *priv_rsa;
        int len;
        unsigned char buf[RSA_KEY_LENGTH * 2];
        unsigned char *p;

        HCRYPTPROV cx = NULL;
        if (!CryptAcquireContextA(&cx, NULL, NULL, PROV_RSA_FULL, 
CRYPT_VERIFYCONTEXT)) {
                DWORD err = GetLastError();
                printf("Could not acquire a cryptographic context - %X\n", err);
        }


        unsigned char seed[64];
        if (!CryptGenRandom(cx, 64, seed))
                printf("Could not get random seed\n");

        RAND_seed(seed, 64);
        if (!CryptReleaseContext(cx, 0)) {
                DWORD err = GetLastError();
                printf("Could not release the cryptographic context - %X\n", 
err);
        }

        rsa = RSA_generate_key(RSA_KEY_LENGTH, RSA_F4, NULL, (char *)stdout);
        RSA_blinding_on(rsa, NULL);

        p = buf;

        /* Save the public key into buffer, we know it will be big enough
         * but we should really check how much space we need by calling the
         * i2d functions with a NULL second parameter */
        len = i2d_RSAPublicKey(rsa, &p);
        len += i2d_RSAPrivateKey(rsa, &p);

        printf("The public and private key are now both in a char array\n");
        printf("and are taking up %d bytes\n", len);

        printf("%s\n", buf);

        RSA_free(rsa);

        p = buf;
        pub_rsa = d2i_RSAPublicKey(NULL, (const unsigned char**) &p, (long)len);
        len -= (p-buf);
        priv_rsa = d2i_RSAPrivateKey(NULL, (const unsigned char**)&p, 
(long)len);

        if ((pub_rsa == NULL) || (priv_rsa == NULL))
                ERR_print_errors_fp(stderr);

        if (!RSA_check_key(priv_rsa))
                printf("Private RSA not validated\n");
        else
                printf("Private RSA OK\n");

        // store public key
        BIO *pub = BIO_new_file(pubkey_file, "w");
        PEM_write_bio_RSAPublicKey(pub, pub_rsa);
        BIO_free(pub);
        RSA_free(pub_rsa);

        // store private key
        BIO *priv = BIO_new_file(privkey_file, "w");
        PEM_write_bio_RSAPrivateKey(priv, priv_rsa, NULL, NULL, 0, NULL, NULL);
        BIO_free(priv);
        RSA_free(priv_rsa);
}




______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to