【问题标题】:Create and sign certificate创建并签署证书
【发布时间】:2016-05-04 05:57:16
【问题描述】:

我正在开发一个 API 服务器,它将接收用户的输入以获取以下信息

"CN=ABC123,O=DEF,L=XYZ,S=CA,C=US,E=info@abc.com"

并使用我们的根证书创建签名的开发者证书。为了使用makecert.exe 制作证书,我遵循了Creating self signed certificates with makecert.exe for development 教程并使用以下命令创建了根证书

makecert.exe ^
-n "CN=CARoot" ^
-r ^
-pe ^
-a sha512 ^
-len 4096 ^
-cy authority ^
-sv CARoot.pvk ^
CARoot.cer

pvk2pfx.exe ^
-pvk CARoot.pvk ^
-spc CARoot.cer ^
-pfx CARoot.pfx ^
-po Test123

在命令提示符中,它会在提示弹出窗口中询问证书和 private.key 密码,这很好,因为这是一次性过程,我已经手动完成了

我用过的开发者证书在哪里

Process.Start("makecert.exe",certCmd);

certCmd 中的以下内容为string

makecert.exe ^
-n "CN=%1" ^
-iv CARoot.pvk ^
-ic CARoot.cer ^
-pe ^
-a sha512 ^
-len 4096 ^
-b 01/01/2014 ^
-e 01/01/2016 ^
-sky exchange ^
-eku 1.3.6.1.5.5.7.3.2 ^
-sv %1.pvk ^
%1.cer

pvk2pfx.exe ^
-pvk %1.pvk ^
-spc %1.cer ^
-pfx %1.pfx ^
-po Test123

现在根据documentation,上面的命令中没有给出-po参数,因此它在提示符中要求输入密码这是一个问题

由于这是一个 API,无法在私钥提示中输入密码

另一种选择是使用 X509Certificate2bouncyCastle 但不确定如何使用它们,任何帮助都会很明显

我想让它尽可能简单,这就是我选择makecert.exeProcess.Start()的原因

【问题讨论】:

  • 为什么不能在stdinmakecert.exe 中发送一个换行符?

标签: c# windows ssl x509certificate2 makecert


【解决方案1】:

如果您想制作自己的证书... 我过去曾使用此代码即时生成证书。您将需要根据您的解决方案对其进行调整,但它应该为您提供所需的一切。它使用充气城堡。

public static X509Certificate GenerateRootCertificate(AsymmetricCipherKeyPair key, X509Name subjectAndIssuer, int serialNumber, int yearsValid)
    {
        X509V3CertificateGenerator gen = new X509V3CertificateGenerator();
        Asn1SignatureFactory sig = new Asn1SignatureFactory("SHA256withRSA", key.Private, new SecureRandom());
        DateTime notBefore = DateTime.Now;
        DateTime notAfter = DateTime.Now.AddYears(yearsValid);

        gen.SetSerialNumber(BigInteger.ValueOf(serialNumber));
        gen.SetSubjectDN(subjectAndIssuer);
        gen.SetIssuerDN(subjectAndIssuer);
        gen.SetPublicKey(key.Public);

        gen.AddExtension(X509Extensions.BasicConstraints, true, new BasicConstraints(true));
        gen.AddExtension(X509Extensions.KeyUsage, true, new KeyUsage(KeyUsage.DigitalSignature | KeyUsage.CrlSign | KeyUsage.KeyCertSign));
        gen.AddExtension(X509Extensions.SubjectKeyIdentifier, false, new SubjectKeyIdentifierStructure(key.Public));
        gen.AddExtension(X509Extensions.AuthorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure(key.Public));

        gen.SetNotBefore(notBefore);
        gen.SetNotAfter(notAfter);

        return gen.Generate(sig);
    }

这可能是一个更好的方法,最终让你有更多的控制权。

编辑: 您可以使用此方法检索您的公钥和私钥,因为它是 PEM 格式:

public static object ReadObjectFromPEMFile(string fileName)
        {
            PemReader reader = new PemReader(new StreamReader(File.Open(fileName, FileMode.Open)));
            object r = reader.ReadObject();
            reader.Reader.Close();
            return r;
        }

不要忘记将结果转换为AsymmetricCipherKeyPair

【讨论】:

  • 谢谢Luke,我知道这个功能我以前在google 时看到过,问题是我不知道如何将我的根证书转换为AsymmetricCipherKeyPair
  • 这很奇怪,是我写的。你的根私钥是什么格式的?查看我的编辑。
  • 卢克我有 .cer 证书文件,我需要用它来签署客户/开发者证书
  • 我会尝试一下你的建议并回复你
  • @riksof-zeeshan 您确定您的 .cer 文件包含 私钥吗?通常,唯一包含私钥的证书是 PKCS12 文件。 .crt 和 .cer 文件往往只包含公钥。
【解决方案2】:

我同意 @LukeParks 的回答,您应该在自己的代码中完成所有这些操作,但如果您仍想按照当前的方式进行操作,方法如下:

您可以重定向 makecert 进程的 StandardInput 以将输入直接传递给程序。

ProcessStartInfo procInf = new ProcessStartInfo("makecert.exe",certCmd)
{
    RedirectStandardOutput = true,
    RedirectStandardInput = true,
    RedirectStandardError = true,
    UseShellExecute = false,
    CreateNoWindow = true
};

Process makeCertProcess = Process.Start(procInf);

//Here is the "manual" input (timing doesn't matter)
pscp.StandardInput.WriteLine("password or whatever");

pscp.WaitForExit(TimeOut);

string errors = pscp.StandardError.ReadToEnd();
string output = pscp.StandardOutput.ReadToEnd();
int errorcode = pscp.ExitCode;

【讨论】:

  • with pscp 你的意思是makeCertProcess ??
  • 我完全按照您上面的建议添加了processMakeCert.StandardInput.WriteLine(clientCertPass);,但它仍然在提示弹出窗口中要求输入密码
【解决方案3】:

前段时间我回答了一个非常相似的问题。 那么问题来了

即时生成自签名证书

但后来问题被修改为使用生成的 CA 证书颁发最终实体证书。嗯,看来我改完后没有重命名方法GenerateSelfSignedCertificate。它应该命名为GenerateEndEntityCertificate,因为它就是这样做的。

试试看我的回答here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多