Answers:
可以通过各种openssl
调用来实现。
首先,提取证书:
$ openssl pkcs12 -clcerts -nokeys -in "YourPKCSFile" \
-out certificate.crt -password pass:PASSWORD -passin pass:PASSWORD
二,CA密钥:
$ openssl pkcs12 -cacerts -nokeys -in "YourPKCSFile" \
-out ca-cert.ca -password pass:PASSWORD -passin pass:PASSWORD
现在,私钥:
$ openssl pkcs12 -nocerts -in "YourPKCSFile" \
-out private.key -password pass:PASSWORD -passin pass:PASSWORD \
-passout pass:TemporaryPassword
现在删除密码:
$ openssl rsa -in private.key -out "NewKeyFile.key" \
-passin pass:TemporaryPassword
将新的PKCS文件放在一起:
$ cat "NewKeyFile.key" \
"certificate.crt" \
"ca-cert.ca" > PEM.pem
并创建新文件:
$ openssl pkcs12 -export -nodes -CAfile ca-cert.ca \
-in PEM.pem -out "NewPKCSWithoutPassphraseFile"
现在,您有了一个新的PKCS12密钥文件,在私钥部分上没有密码短语。
ca-cert.ca
什么?
-nodes
在-export
使用时被忽略,在这种情况下没有记录(请参见openssl手册页,-nodes
仅在从PKCS#12导出到PEM时列出)。您的最后通话仍然提示我输入导出密码。如果我只是按回车键,则会得到一个PKCS#12文件,其密码是一个空字符串,而不是没有密码的文件。当我这样做时,openssl pkcs12 -in "NewPKCSWithoutPassphraseFile"
它仍然提示我输入密码。我只要按回车键就可以,但是如果没有密码,它甚至不会提示。
我找到的最简单的解决方案是
openssl pkcs12 -in protected.p12 -nodes -out temp.pem
# -> Enter password
openssl pkcs12 -export -in temp.pem -out unprotected.p12
# -> Just press [return] twice for no password
rm temp.pem
keytool -v -list -storetype pkcs12 -keystore unprotected.p12
将发出警告,并且不会列出证书。因此它可能适用于OpenVPN,但不适用于其他功能。
现在,私钥:
openssl pkcs12 -nocerts -in "YourPKCSFile" -out private.key -password pass:PASSWORD -passin pass:PASSWORD -passout pass:TemporaryPassword
立即删除密码:
openssl rsa -in private.key -out "NewKeyFile.key" -passin pass:TemporaryPassword
这两个步骤可以替换为
openssl pkcs12 -nocerts -in "YourPKCSFile" -out private.key -nodes
这些都不对我有用。最后,我恢复了第一次工作的dotNet代码。
class Script
{
static public void Main(string[] args)
{
if (args.Length < 3 || args.Contains("/?"))
{
MainHelp(args);
return;
}
string _infile = args[0],
_outfile = args[2];
string _password = args[1], _outpassword = (args.Length > 3) ? args[3] : "";
Console.WriteLine(String.Format("{0} -> {1} with ({2} -> {3})", _infile, _outfile, _password, _outpassword));
System.Security.Cryptography.X509Certificates.X509Certificate2 cert = null;
Console.WriteLine(String.Format("Load {0} with {2}", _infile, _outfile, _password, _outpassword));
cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(_infile, _password, X509KeyStorageFlags.Exportable);
Console.WriteLine(String.Format("Export {1} with {3}", _infile, _outfile, _password, _outpassword));
System.IO.File.WriteAllBytes(_outfile, cert.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Pfx, _outpassword));
Console.WriteLine(String.Format("Export complete", _infile, _outfile, _password, _outpassword));
}
static public void MainHelp(string[] args)
{
Console.WriteLine("Usage pfxremovepwd [inpfx] [inpwd] [outpfx] [optional outpwd]");
return;
}
}
./remove_pass_from_cert.sh YourCertName YourCertPass