繁体   English   中英

如何在 C# 中使用 Ed25519 创建自签名证书

[英]How to create self-signed certificate using Ed25519 in C#

我必须使用 Ed25519 生成 X509 证书。 我知道我应该使用System.Security.Cryptography.X509Certificates命名空间中的RequestCertificate class,但它似乎不支持 ed25519。

那是我的场景:我有私有 ed25519 密钥,并基于它我需要生成能够在相互 TLS 中使用的自签名 X509 证书。

我不知道如何在使用 ed25519 时执行此操作,因为似乎不支持此曲线。 我怎样才能做到这一点?

创建OpenSSL的配置文件,eg openssl-25519.cnf:

[req]
distinguished_name = req_distinguished_name
req_extensions = v3_req
prompt = no
[req_distinguished_name]
C = DE
CN = www.example.com
[v3_req]
keyUsage = keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = www.example.com
DNS.2 = example.com

您可以将 File.WriteAllText 用于临时文件以在证书签名期间使用它,而 openSsl25519Configuration 是上面配置的字符串,您可以在其中插入动态值。

string tempCnfName = Path.GetTempFileName();

File.WriteAllText(tempCnfName, openSsl25519Configuration);

然后使用您的私钥(example.com.key) 使用 OpenSSL 请求证书签名请求文件。

openssl req -new -out example.com.csr -key example.com.key -config openssl-25519.cnf

如果你已经有现成的私钥,参考arguments进程中.key文件的文件路径:

string tempCsrName = Path.GetTempFileName();

Process process = new Process() {
    StartInfo = {
        FileName = "openssl.exe",
        Arguments = $"req -new -out {tempCsrName} -key example.com.key -config {tempCnfName}"
    }
};

process.Start();
process.WaitForExit();

现在您可以再次使用 OpenSSL 自签名example.com.csr

openssl x509 -req -days 700 -in example.com.csr -signkey example.com.key -out example.com.crt
string tempCrtName = Path.GetTempFileName();

Process process = new Process() {
    StartInfo = {
        FileName = "openssl.exe",
        Arguments = $"req x509 -req -days 700 -in {tempCsrName} -signkey example.com.key -out {tempCrtName}"
    }
};

process.Start();
process.WaitForExit();

现在您有了一个自签名的 ED25519 证书,您可以根据需要通过 tempCrtName 移动或读取该证书。

如果您还没有私钥,您可以生成一个:

openssl genpkey -algorithm ED25519 > example.com.key

资料来源: https://blog.pinterjann.is/ed25519-certificates.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM