tuskentower
tuskentower

Reputation: 21

Problem with generating a PKCS#12 key from an RSA key

I have a set of RSA keys which are used for testing. I am working on an enhancement for PKCS#12. The existing RSA keys all have a password on them, let's say "mypass". I want to generate three types of PKCS#12 keys:

I'm having trouble with the first option category

Here, I copy the PEM formatted source key into my workspace and create a decrypted version prefixed by the letter d. $tkey is the PEM formatted input key.

echo RSA keys PEM formatted
key=${name}.priv.rsa.pem
dkey=d${name}.priv.rsa.pem
pubkey=${name}.pub.rsa.pem
cp $tkey ./${key}
openssl rsa -in $key -passin pass:mypass -out $dkey -outform pem -passout pass:

I think that the decrypted key doesn't have a password on it, because openssl does not prompt me for a password when I use it to sign some data.

I do the following to generate my PKCS#12 keys (where $tcert is the path to input key's certificate).

tcert=`dirname $tkey`/${name}.crt
key=${name}.priv.p12
echo PKCS#12 envelope without password and unencrypted RSA key
openssl pkcs12 -export -inkey $dkey -in $tcert -out d$key -name d$name  -passout pass:
echo PKCS#12 envelope with password and unencrypted RSA key
openssl pkcs12 -export -inkey $dkey -in $tcert -out x$key -name  $name  -passout pass:mypass
echo PKCS#12 envelope with password and password protected RSA key
openssl pkcs12 -export -inkey $tkey -in $tcert -out  $key -name  $name  -passout pass:mypass -passin pass:mypass

I then inspect each key doing the following (adding mypass as needed). When I go to display the d key, it asks me for a password for the RSA key. If I input "mypass", it dumps the key.

openssl pkcs12 -info -in $dkey -passin pass:

Since the above is scripted, I had the script echo the lines so that I could be sure that it was using the correct files and such.

I'm obviously doing something wrong, but I don't see it.

Upvotes: 1

Views: 3542

Answers (1)

tuskentower
tuskentower

Reputation: 21

I was wrong about needing to input my old password. Any four characters of input worked. Which lead me to look at the code where I found a hard coded 4 character requirement on the default PEM callback.

crypto/pem/pem_lib.c
#define MIN_LENGTH      4

int PEM_def_callback(char *buf, int num, int rwflag, void *userdata)
{
    int i, min_len;
    const char *prompt;

    /* We assume that the user passes a default password as userdata */
    if (userdata) {
        i = strlen(userdata);
        i = (i > num) ? num : i;
        memcpy(buf, userdata, i); 
        return i;
    }   

    prompt = EVP_get_pw_prompt();
    if (prompt == NULL)
        prompt = "Enter PEM pass phrase:";

    /*  
     * rwflag == 0 means decryption
     * rwflag == 1 means encryption
     *
     * We assume that for encryption, we want a minimum length, while for
     * decryption, we cannot know any minimum length, so we assume zero.
     */
    min_len = rwflag ? MIN_LENGTH : 0;

    i = EVP_read_pw_string_min(buf, min_len, num, prompt, rwflag);
    if (i != 0) {
        PEMerr(PEM_F_PEM_DEF_CALLBACK, PEM_R_PROBLEMS_GETTING_PASSWORD);
        memset(buf, 0, (unsigned int)num);
        return -1; 
    }   
    return strlen(buf);
}

Had I paid attention to the error messages, I would have seen "You must type in 4 to 1024 characters".

Enter PEM pass phrase:
Error outputting keys and certificates
139783395758528:error:28078065:UI routines:UI_set_result_ex:result too small:../crypto/ui/ui_lib.c:903:You must type in 4 to 1024 characters
139783395758528:error:2807106B:UI routines:UI_process:processing error:../crypto/ui/ui_lib.c:543:while reading strings
139783395758528:error:0906406D:PEM routines:PEM_def_callback:problems getting password:../crypto/pem/pem_lib.c:59:
139783395758528:error:0907E06F:PEM routines:do_pk8pkey:read key:../crypto/pem/pem_pk8.c:83:

So, the key is not password protected, but the callback function that was called requires four characters of input.

Upvotes: 1

Related Questions