【问题标题】:Using custom Oid in Subject Alternative Name with Bouncycastle在带有 Bouncycastle 的主题备用名称中使用自定义 Oid
【发布时间】:2016-12-17 01:59:49
【问题描述】:

我正在使用 BouncyCastle 生成证书。一切正常,直到我尝试使用 GeneralName.OtherName 添加一个主题备用名称扩展名和自定义 Oid=1.3.6.1.4.1.311.20.2.3 (它代表用户主体名称(UPN))。所以结果应该是这样的: 主题备用名称部分 -> OtherName -> 用户主体名称=user@domain

我是这样做的:

Asn1EncodableVector vector = new Asn1EncodableVector
{
  new GeneralName(GeneralName.OtherName, 
                  new KeySpecificInfo(new DerObjectIdentifier("1.3.6.1.4.1.311.20.2.3"), new DerOctetString(GetBytes("user@domain"))))
}
DerSequence seq = new DerSequence(vector);
GeneralNames subjectAltName = GeneralNames.GetInstance(seq);
// Adding extension to X509V3CertificateGenerator
certGen.AddExtension(X509Extensions.SubjectAlternativeName, false, subjectAltName);

之后,我在创建的证书的 SubjectAlternativeName 部分收到一个奇怪的序列化结果。很明显,添加 OtherName 部分是错误的,但我找不到任何有关为 BouncyCastle 添加自定义 oid 的信息。 有什么想法吗?

【问题讨论】:

  • Java 还是 C#?您使用的是哪个版本的库?
  • C# 但我也尝试过 Java(它们之间存在一些差异)。

标签: certificate x509certificate bouncycastle


【解决方案1】:

找到答案here。它是 Java,但 C# 的代码几乎相同。

这是我的 C# 版本。

Asn1EncodableVector otherName = new Asn1EncodableVector();
otherName.Add(new DerObjectIdentifier("1.3.6.1.4.1.311.20.2.3"));
otherName.Add(new DerTaggedObject(true, GeneralName.OtherName, new DerUtf8String(siteName)));
Asn1Object upn = new DerTaggedObject(false, 0, new DerSequence(otherName));
Asn1EncodableVector generalNames = new Asn1EncodableVector();
generalNames.Add(upn);

// Adding extension to X509V3CertificateGenerator
certificateGenerator.AddExtension(X509Extensions.SubjectAlternativeName, false, new DerSequence(generalNames));

【讨论】:

    【解决方案2】:

    这也适用于 C# bouncycastle 证书:

    //Subject Alternative Name
    if (! (String.IsNullOrEmpty(_subjectAlternativeName))) {
        //Here we signify ip address instead of DNS SAN. This could be condition upon further development.
        GeneralNames subjectAltName = new GeneralNames(new GeneralName(GeneralName.IPAddress, _subjectAlternativeName));
        certificateGenerator.AddExtension(X509Extensions.SubjectAlternativeName, false, subjectAltName);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-09
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 2021-06-24
      • 1970-01-01
      • 2017-10-08
      相关资源
      最近更新 更多