blob: b751658e17b4e1c5a018fc114708db5f811d0ef4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
using System;
using System.Collections.Generic;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Security.Certificates;
namespace Org.BouncyCastle.X509.Extension
{
public class X509ExtensionUtilities
{
public static Asn1Object FromExtensionValue(
Asn1OctetString extensionValue)
{
return Asn1Object.FromByteArray(extensionValue.GetOctets());
}
public static IList<IList<object>> GetIssuerAlternativeNames(X509Certificate cert)
{
Asn1OctetString extVal = cert.GetExtensionValue(X509Extensions.IssuerAlternativeName);
return GetAlternativeName(extVal);
}
public static IList<IList<object>> GetSubjectAlternativeNames(X509Certificate cert)
{
Asn1OctetString extVal = cert.GetExtensionValue(X509Extensions.SubjectAlternativeName);
return GetAlternativeName(extVal);
}
private static IList<IList<object>> GetAlternativeName(
Asn1OctetString extVal)
{
var result = new List<IList<object>>();
if (extVal != null)
{
try
{
Asn1Sequence seq = Asn1Sequence.GetInstance(FromExtensionValue(extVal));
foreach (Asn1Encodable primName in seq)
{
GeneralName genName = GeneralName.GetInstance(primName);
var list = new List<object>(2);
list.Add(genName.TagNo);
switch (genName.TagNo)
{
case GeneralName.EdiPartyName:
case GeneralName.X400Address:
case GeneralName.OtherName:
list.Add(genName.Name.ToAsn1Object());
break;
case GeneralName.DirectoryName:
list.Add(X509Name.GetInstance(genName.Name).ToString());
break;
case GeneralName.DnsName:
case GeneralName.Rfc822Name:
case GeneralName.UniformResourceIdentifier:
list.Add(((IAsn1String)genName.Name).GetString());
break;
case GeneralName.RegisteredID:
list.Add(DerObjectIdentifier.GetInstance(genName.Name).Id);
break;
case GeneralName.IPAddress:
list.Add(Asn1OctetString.GetInstance(genName.Name).GetOctets());
break;
default:
throw new IOException("Bad tag number: " + genName.TagNo);
}
result.Add(list);
}
}
catch (Exception e)
{
throw new CertificateParsingException(e.Message);
}
}
return result;
}
}
}
|