c#-使用BouncyCastle在证书请求中指定证书模板
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-使用BouncyCastle在证书请求中指定证书模板,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2254字,纯文字阅读大概需要4分钟。
内容图文
![c#-使用BouncyCastle在证书请求中指定证书模板](/upload/InfoBanner/zyjiaocheng/678/8d71d3a9e7314e0ab731b2e38b0c8b63.jpg)
我正在使用BouncyCastle生成证书请求:
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Prng;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using System.IO;
class Program {
static void Main(string[] args) {
var keyGenerator = new RsaKeyPairGenerator();
keyGenerator.Init(
new KeyGenerationParameters(
new SecureRandom(new CryptoApiRandomGenerator()),
2048));
var keyPair = keyGenerator.GenerateKeyPair();
X509Name name = new X509Name("CN=test");
Pkcs10CertificationRequest csr = new Pkcs10CertificationRequest("SHA256WITHRSA", name, keyPair.Public, null, keyPair.Private);
using (FileStream fs = new FileStream(@"X:\tmp\tmp.csr", FileMode.Create)) {
var req = csr.GetDerEncoded();
fs.Write(req, 0, req.Length);
}
}
}
如何在请求中指定证书模板?
注意:使用certutil解码通过证书控制台创建的请求,看起来证书模板应该是请求的扩展;我尝试相应地创建扩展:
var extGen = new Org.BouncyCastle.Asn1.X509.X509ExtensionsGenerator();
extGen.AddExtension(
new DerObjectIdentifier("1.3.6.1.4.1.311.21.7"), // OID for certificate template extension
true,
new DerObjectIdentifier("1.3.6.1.4.1.311.21.8.the.OID.of.the.template"));
但是后来,我不明白如何将其附加到请求中.
解决方法:
经过一番挖掘后,此解决方案似乎有效:
const string TemplateOid = "1.3.6.1.4.1.311.21.8.etc.etc";
const int MajorVersion = 100;
const int MinorVersion = 4;
Dictionary<DerObjectIdentifier, X509Extension> extensionsDictionary = new Dictionary<DerObjectIdentifier,X509Extension>();
DerObjectIdentifier certificateTemplateExtensionOid = new DerObjectIdentifier("1.3.6.1.4.1.311.21.7");
DerSequence certificateTemplateExtension = new DerSequence(
new DerObjectIdentifier(TemplateOid),
new DerInteger(MajorVersion),
new DerInteger(MinorVersion));
extensionsDictionary[certificateTemplateExtensionOid] = new X509Extension(
false,
new DerOctetString(certificateTemplateExtension));
X509Extensions extensions = new X509Extensions(extensionsDictionary);
Attribute attribute = new Attribute(
PkcsObjectIdentifiers.Pkcs9AtExtensionRequest,
new DerSet(extensions));
DerSet attributes = new DerSet(attribute);
Pkcs10CertificationRequest csr = new Pkcs10CertificationRequest("SHA256WITHRSA", name, keyPair.Public, attributes, keyPair.Private);
内容总结
以上是互联网集市为您收集整理的c#-使用BouncyCastle在证书请求中指定证书模板全部内容,希望文章能够帮你解决c#-使用BouncyCastle在证书请求中指定证书模板所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。