我有一组用于测试的RSA密钥。我正在为PKCS#12进行增强。现有的RSA密钥都在上面带有密码,例如“ mypass”。我想生成三种类型的PKCS#12密钥:
我在使用第一个选项时遇到了麻烦
这里,我将PEM格式的源密钥复制到我的工作区中,并创建一个以字母d
为前缀的解密版本。 $tkey
是PEM格式的输入键。
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:
我认为解密的密钥上没有密码,因为当我使用openssl签署某些数据时,openssl不会提示我输入密码。
我执行以下操作来生成我的PKCS#12密钥(其中$tcert
是输入密钥证书的路径。
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
然后,我检查每个密钥并执行以下操作(根据需要添加mypass)。当我显示d
密钥时,它要求我输入RSA密钥的密码。如果我输入“ mypass”,它将转储密钥。
openssl pkcs12 -info -in $dkey -passin pass:
由于上面编写了脚本,所以我让脚本回显了各行,以便可以确定它使用了正确的文件等。
我显然做错了,但我看不到。
我错了,需要输入旧密码。输入的任何四个字符都起作用。这使我看一下在默认PEM回调中发现硬编码4个字符要求的代码。
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);
}
如果我注意错误消息,我会看到“您必须输入4到1024个字符”。
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:
因此,该密钥不受密码保护,但是调用的回调函数需要输入四个字符。