【OpenSSL】Memory BIO

Test code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

# include <openssl/e_os2.h>
# include <openssl/bio.h>
# include <openssl/err.h>
# include <openssl/bn.h>
# include <openssl/rsa.h>
# include <openssl/evp.h>
# include <openssl/ssl.h>
# include <openssl/x509.h>
# include <openssl/pem.h>
# include <openssl/rand.h>

static 
void 
loadRsa_bio(BIO * bp, const char * passphrase);

static 
void 
loadRsaPrivateKey(const char * file, const char * passphrase);

static 
void 
genRsa_bio(BIO * bp, const char * passphrase);

static 
void 
genRsaPrivateKey(const char * file, const char * passphrase);

int 
main(int argc, char * argv[])
{
    // initialize openssl library
    // Notice, if not initialized, some decrypt routine will fail.
    // but, if links in all algos, prog will grow large.
    OpenSSL_add_all_algorithms();
    SSL_load_error_strings();
    ERR_load_crypto_strings();

    //Test: Gen RSA and sink to file.
    //genRsaPrivateKey("bobkey.pem", "passphrase");
    //loadRsaPrivateKey("bobkey.pem","passphrase");

    BIO* mbio = BIO_new(BIO_s_mem());// sink to memory.
    genRsa_bio(mbio, "passphrase");
    loadRsa_bio(mbio,"passphrase");
    BIO_free(mbio);

    // cleanup library
    EVP_cleanup();

    return 0;
}

static 
void 
loadRsa_bio(BIO * bp, const char * passphrase)
{
    RSA * x = PEM_read_bio_RSAPrivateKey(bp
                                    , NULL /*or, &x */
                                    , NULL /*void callback*/
                                    , (void *)passphrase);

    if (!x) return ;

    BIO * bio_err = NULL;
    if ((bio_err = BIO_new(BIO_s_file())) != NULL)
       BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);

    //RSA_print_fp(stdout, x, 0);
    RSA_print(bio_err, x, 0);

    if (x) RSA_free(x);
    else{
        /*Error*/
        ERR_print_errors_fp(stdout);
    }
    x = NULL;
}

static 
void 
loadRsaPrivateKey(const char * file, const char * passphrase)
{
    BIO * in = BIO_new(BIO_s_file());
    if (!in) return ;

    if (BIO_read_filename(in, (char*)file) <= 0)
        return ;

    loadRsa_bio(in, passphrase);

    if (in) BIO_free(in);
    in = NULL;

    return ;
}

static 
void 
genRsa_bio(BIO * bp, const char * passphrase)
{
    BIGNUM *bn = BN_new();
    BN_set_word(bn, RSA_F4);

    RSA *rsa = RSA_new();
    RSA_generate_key_ex(rsa, 2048 /*key bits*/, bn, NULL);

    PEM_write_bio_RSAPrivateKey(bp
                                , rsa
                                , EVP_des_ede3_cbc()
                                , NULL, 0
                                , NULL, (void*)passphrase);

    if (rsa) RSA_free(rsa);

    if (bn)  BN_free(bn);
}

static 
void 
genRsaPrivateKey(const char * file, const char * passphrase)
{

    BIO *out = BIO_new(BIO_s_file());
    BIO_write_filename(out, (char*)file);

    genRsa_bio(out, passphrase);

    if (out) BIO_free(out);
}

Dump memory BIO

#include <openssl/buffer.h>
int 
main(int argc, char * argv[])
{
    // initialize openssl library
    // Notice, if not initialized, 
    // some decryption routine will fail.
    // but, if links in all algos, prog will grow large.
    OpenSSL_add_all_algorithms();
    SSL_load_error_strings();
    ERR_load_crypto_strings();

    do {
        //Test: Gen RSA and sink to file.
        //genRsaPrivateKey("bobkey.pem", "passphrase");
        //loadRsaPrivateKey("bobkey.pem","passphrase");
    }while(0);

    do {
        BIO* mbio = BIO_new(BIO_s_mem());// sink to memory.
        genRsa_bio(mbio, "passphrase");

        // dump the raw file to stdout.
        BUF_MEM * bufptr = NULL;
        BIO_get_mem_ptr(mbio, &bufptr);
        for (int i = 0; i < bufptr->length; i++) 
            putchar(*(char *)&bufptr->data[i]);

        loadRsa_bio(mbio,"passphrase");
        BIO_free(mbio);
    } while(0);

    // cleanup library
    EVP_cleanup();

    return 0;
}

Construct memory BIO from string

Notice
each line should be tail-ed with CRLF

Test code


    do {// Create memory bio from string.
        std::string content = 
        "-----BEGIN RSA PRIVATE KEY-----\r\n"
        "Proc-Type: 4,ENCRYPTED\r\n"
        "DEK-Info: DES-EDE3-CBC,327FA353E96D4B7E\r\n"
        "\r\n"
        "oHaiNdoeh1M4qWrnag82QZKFRPmsNShAU+yG5Es7BtVpzDwnH+ge4bdFWyFt1C91\r\n"
        "WbHVemIgOQUHOCpoRm0XflyPOQbMseF5S8i5luH/6trKUzX7B7+el9A/o5YtYMVw\r\n"
        "8GK+5zeNJi+1sJjGrdhWZ9+FB3RVGbcfCKzKDBawx0XQerHYAhH2WIJOovt10ZKb\r\n"
        "5RUBPkrdKrbgigxg6QPs46g4NWXjxCEKqP8IqxnhH6xTVMVIwZGKcrowH8pJ4qiz\r\n"
        "Z2TYz0LA8gkeGwwrt+Es35DbeW+xddYBVQD6aKrr2SkgaVQkJLDy91noagZWlYiL\r\n"
        "vzOgODmVX7sYX0sruDvsKntCJ1sLJTCyusX4d7WZ46paR13lvoPsm+Vw/7P6jdYm\r\n"
        "EKkSb/hQpxBksSWlULCSI5n84SsCY/wSdihtdWyxgW+tKQqEhgFpI7c2xhTKyh5V\r\n"
        "W/FaBstKwm0Q7azGH5g2sjGuzCHJBKLq1WpI8X21Q85BI9diBzvjINESBmri6wq0\r\n"
        "WeQacaH5mDNPwDD1JnVcMxT+v7nNb1evPM18kDH+hHQUn14yHqoagWG90BNUNHqi\r\n"
        "VLTM93ErLLQf41FR3ikMc+h2K0dUsRpb/LebJVOBbwwm83XkAVqo02eit9kgq041\r\n"
        "PJ3GoxviyWAzTw7xrJ3eIp9TTzlPikiTVJky5Omz0eDjQpnQCQwVeqqCtCWEqn8b\r\n"
        "CkLO6Dfyctfwq8E3qjWRBVnrol8cvlFYRG9pbZ/WrcHtLFV9mWgbMxy8b8ZmT0O4\r\n"
        "dgUnwQty7tZtpOpA32gArmHlImBZ88mwRjTr2y2yWbVRIjptrHP9eXB+cVx9WzB8\r\n"
        "0tvBV0uLc48ZbrWH6uuUkPcS/KK42uA2onzqVSh6p4QwTE/CQDpAtWYDqjs9EuKg\r\n"
        "VSrjX7E6v4KRMMhYkv51abNkL3KEC/VhCi+DKnHACx53xCcLaBRuZzHGkjGQfR4W\r\n"
        "g70VlsKtvheclbIYhHqZYeYpbFnNQ0kAfFD45zWoFNYESWZEwlU8N5aI784Lklja\r\n"
        "mglt6eXCNSbyLwoGoMpmbfHGval8uVK+RQY3PwHwIY84g5UOYCmYG71hjqZbg8aM\r\n"
        "hH5ak/8/ywg6qe1540KCrmjEjG0rcGl8jsC0wj8nVbWAMtSOG/11i1XsOTASxStb\r\n"
        "2sBpOFZO3CHgcKPpoeFlA0Bv/+Y9J9Pthn9wFHqgNNao0uooeFCx1JlPO0Ed51Gl\r\n"
        "b2LiJds3k9I1TbXaU9p3+Ee3C5rz9IPE4RFLmANBAaX8iOntfUaWiqzNRfUz/2fz\r\n"
        "2ZtdpSQF6pGvMotA47ANvdpyyryahkCnKVDiHJ4Ab8aSlXwNxA9AhBn7n7rHf8f/\r\n"
        "hqcE5hoh9zpPzFKPgfpZRBY72TCys0AXde73XbRGGAWunlDlnHyXkuXuVSBq2Mz6\r\n"
        "YZckpHN/4vr1j1q4v3R8MF3VXKahk5FGRhQDUR2zJtjS6PeDnomGdYKtDV7hvURw\r\n"
        "2ZohnKhXH6tQ9gZ39rjGisXKPuP4yX43XigPBQiNlrbDWagGxYUjSLI6kRnpNvPc\r\n"
        "xrzjPjSSEqXbyGYIhiBQwnlH0FO79EPSB1YkBed3fbG5ThK4pf5l2aVAl9RDkVtF\r\n"
        "-----END RSA PRIVATE KEY-----\r\n";

        BIO * mbio = BIO_new_mem_buf(
                            (void*)content.c_str()
                            , content.length());

        loadRsa_bio(mbio, "passphrase");

        BIO_free(mbio);
    }while(0);
發佈了200 篇原創文章 · 獲贊 34 · 訪問量 55萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章