How to read in a Private key from a PEM file using buffer BIO? - Openssl

This is a discussion on How to read in a Private key from a PEM file using buffer BIO? - Openssl ; Hello, we want to read in this private key using buffer BIO (no other BIO like the fp version!): -----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,ABC593E89A1C77DC VRFgc0wejrAxwsGZud6I7IMVV30ZAHGu2Xl5BASjuBwjw4LB22 UVAvNuCJRHRlOu 6UI774NZamj6Tme1UgdyP6S3jtISEFhcJKQ5ldudBRfEKsW5hu/DGR7ZRz9hT365 ISmbudSl35Eq+GYqT666Vng9ELyYwlNI3G02F2pmLwahNVMTbG tJxHZ/c2pqJzel flPww/4AVm4aRi4PBwFt8+Tf2xTGKeIb+b795Aq6pfoiQnnLf1sLB2Jj X7L6OUsO VuDFb0dt/h82/T71+/5Oc2g+51sD2w+UjIlo8mXYTxX3d6Dsw7a6sSKRHg+C4h4o /s4rKH+e1YV9AtFl9n5EOGTjov6oOCwuEAmphR97k4puR1aQaiC 05zz3nsRxDf6q ddWgfv+I4uPB7qA1VwNClwTr93cDnAAyBphMFygAuP5Dob3uJ2 FerBmyMVxSKjZC CDPhY9qLWvQwMeAZPhEJCfw3X49hPlA/svFqPThqMKKozqOsTtf4VL4fI67lTLag ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: How to read in a Private key from a PEM file using buffer BIO?

  1. How to read in a Private key from a PEM file using buffer BIO?

    Hello,

    we want to read in this private key using buffer BIO (no other BIO like
    the fp version!):

    -----BEGIN RSA PRIVATE KEY-----
    Proc-Type: 4,ENCRYPTED
    DEK-Info: DES-EDE3-CBC,ABC593E89A1C77DC

    VRFgc0wejrAxwsGZud6I7IMVV30ZAHGu2Xl5BASjuBwjw4LB22 UVAvNuCJRHRlOu
    6UI774NZamj6Tme1UgdyP6S3jtISEFhcJKQ5ldudBRfEKsW5hu/DGR7ZRz9hT365
    ISmbudSl35Eq+GYqT666Vng9ELyYwlNI3G02F2pmLwahNVMTbG tJxHZ/c2pqJzel
    flPww/4AVm4aRi4PBwFt8+Tf2xTGKeIb+b795Aq6pfoiQnnLf1sLB2Jj X7L6OUsO
    VuDFb0dt/h82/T71+/5Oc2g+51sD2w+UjIlo8mXYTxX3d6Dsw7a6sSKRHg+C4h4o
    /s4rKH+e1YV9AtFl9n5EOGTjov6oOCwuEAmphR97k4puR1aQaiC 05zz3nsRxDf6q
    ddWgfv+I4uPB7qA1VwNClwTr93cDnAAyBphMFygAuP5Dob3uJ2 FerBmyMVxSKjZC
    CDPhY9qLWvQwMeAZPhEJCfw3X49hPlA/svFqPThqMKKozqOsTtf4VL4fI67lTLag
    ApEnmuLpR1PLfhlyYeJEgTB3sWB8wnmflnlHYK1c7PicFm2wfY qCWmLZ+iE7cX9v
    i/aMJA1XYngCJnh2gaThRJIs5Wwd0pCBHN8Y5GTV5Q4Yx3q0uwob 2w==
    -----END RSA PRIVATE KEY-----

    The above key has been create using this command:

    "openssl genrsa -des3 -passout pass:fints -f4 -out privateKey.pem 768"

    Therefore we need to provide the passphrase "fints" when reading the BIO in.

    We read in the file contents and created a buffer BIO:

    // Callback to disable console passphrase input
    int disable_passphrase_prompt(
    char *buf,
    int size,
    int rwflag,
    void *u)
    {
    return 0;
    }

    // Extracted from out code that reads in the from the buffer BIO
    // ...
    void * u = (void*) "fints";
    BIO * bio = BIO_new_mem_buf(fileContents, sizeOfContents);
    RSA * rsa = PEM_read_bio_RSAPrivateKey(
    bio,
    0,
    u==0 ? disable_passphrase_prompt : 0,
    u);

    if (rsa == 0)
    {
    char buffer[120];
    ERR_error_string(ERR_get_error(), buffer);
    fprintf(stderr, "OpenSSL error: %s", buffer);
    }
    // ...

    The error output goes like this:

    "error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag"

    What are we doing wrong?

    Thanks in advance
    Konrad
    __________________________________________________ ____________________
    OpenSSL Project http://www.openssl.org
    User Support Mailing List openssl-users@openssl.org
    Automated List Manager majordomo@openssl.org


  2. Re: How to read in a Private key from a PEM file using buffer BIO?

    Sorry, our fault, everything works fine as described.

    The only reason it didn't before was, that we created a class that shall
    handle the SSL initialization and DEinitialization. And instead of
    creating an object of this class we only called it's constructor:

    OurApplication(argc, argv);

    instead of

    OurApplication app(argc, argv);

    Konrad Kleine schrieb:
    > Hello,
    >
    > we want to read in this private key using buffer BIO (no other BIO like
    > the fp version!):
    >
    > -----BEGIN RSA PRIVATE KEY-----
    > Proc-Type: 4,ENCRYPTED
    > DEK-Info: DES-EDE3-CBC,ABC593E89A1C77DC
    >
    > VRFgc0wejrAxwsGZud6I7IMVV30ZAHGu2Xl5BASjuBwjw4LB22 UVAvNuCJRHRlOu
    > 6UI774NZamj6Tme1UgdyP6S3jtISEFhcJKQ5ldudBRfEKsW5hu/DGR7ZRz9hT365
    > ISmbudSl35Eq+GYqT666Vng9ELyYwlNI3G02F2pmLwahNVMTbG tJxHZ/c2pqJzel
    > flPww/4AVm4aRi4PBwFt8+Tf2xTGKeIb+b795Aq6pfoiQnnLf1sLB2Jj X7L6OUsO
    > VuDFb0dt/h82/T71+/5Oc2g+51sD2w+UjIlo8mXYTxX3d6Dsw7a6sSKRHg+C4h4o
    > /s4rKH+e1YV9AtFl9n5EOGTjov6oOCwuEAmphR97k4puR1aQaiC 05zz3nsRxDf6q
    > ddWgfv+I4uPB7qA1VwNClwTr93cDnAAyBphMFygAuP5Dob3uJ2 FerBmyMVxSKjZC
    > CDPhY9qLWvQwMeAZPhEJCfw3X49hPlA/svFqPThqMKKozqOsTtf4VL4fI67lTLag
    > ApEnmuLpR1PLfhlyYeJEgTB3sWB8wnmflnlHYK1c7PicFm2wfY qCWmLZ+iE7cX9v
    > i/aMJA1XYngCJnh2gaThRJIs5Wwd0pCBHN8Y5GTV5Q4Yx3q0uwob 2w==
    > -----END RSA PRIVATE KEY-----
    >
    > The above key has been create using this command:
    >
    > "openssl genrsa -des3 -passout pass:fints -f4 -out privateKey.pem 768"
    >
    > Therefore we need to provide the passphrase "fints" when reading the BIO in.
    >
    > We read in the file contents and created a buffer BIO:
    >
    > // Callback to disable console passphrase input
    > int disable_passphrase_prompt(
    > char *buf,
    > int size,
    > int rwflag,
    > void *u)
    > {
    > return 0;
    > }
    >
    > // Extracted from out code that reads in the from the buffer BIO
    > // ...
    > void * u = (void*) "fints";
    > BIO * bio = BIO_new_mem_buf(fileContents, sizeOfContents);
    > RSA * rsa = PEM_read_bio_RSAPrivateKey(
    > bio,
    > 0,
    > u==0 ? disable_passphrase_prompt : 0,
    > u);
    >
    > if (rsa == 0)
    > {
    > char buffer[120];
    > ERR_error_string(ERR_get_error(), buffer);
    > fprintf(stderr, "OpenSSL error: %s", buffer);
    > }
    > // ...
    >
    > The error output goes like this:
    >
    > "error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag"
    >
    > What are we doing wrong?
    >
    > Thanks in advance
    > Konrad
    > __________________________________________________ ____________________
    > OpenSSL Project http://www.openssl.org
    > User Support Mailing List openssl-users@openssl.org
    > Automated List Manager majordomo@openssl.org
    >

    __________________________________________________ ____________________
    OpenSSL Project http://www.openssl.org
    User Support Mailing List openssl-users@openssl.org
    Automated List Manager majordomo@openssl.org


+ Reply to Thread