summary refs log tree commit diff
path: root/crypto/src/asn1/x509/AuthorityInformationAccess.cs
blob: 382513674dc48a5b9eab9ae89e379555bb6321f9 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Text;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.X509
{
    /**
     * The AuthorityInformationAccess object.
     * <pre>
     * id-pe-authorityInfoAccess OBJECT IDENTIFIER ::= { id-pe 1 }
     *
     * AuthorityInfoAccessSyntax  ::=
     *      Sequence SIZE (1..MAX) OF AccessDescription
     * AccessDescription  ::=  Sequence {
     *       accessMethod          OBJECT IDENTIFIER,
     *       accessLocation        GeneralName  }
     *
     * id-ad OBJECT IDENTIFIER ::= { id-pkix 48 }
     * id-ad-caIssuers OBJECT IDENTIFIER ::= { id-ad 2 }
     * id-ad-ocsp OBJECT IDENTIFIER ::= { id-ad 1 }
     * </pre>
     */
    public class AuthorityInformationAccess
        : Asn1Encodable
    {
        private static AccessDescription[] Copy(AccessDescription[] descriptions)
        {
            return (AccessDescription[])descriptions.Clone();
        }

        public static AuthorityInformationAccess GetInstance(object obj)
        {
            if (obj is AuthorityInformationAccess)
                return (AuthorityInformationAccess)obj;
            if (obj == null)
                return null;
            return new AuthorityInformationAccess(Asn1Sequence.GetInstance(obj));
        }

        public static AuthorityInformationAccess FromExtensions(X509Extensions extensions)
        {
            return GetInstance(X509Extensions.GetExtensionParsedValue(extensions, X509Extensions.AuthorityInfoAccess));
        }

        private readonly AccessDescription[] descriptions;

        private AuthorityInformationAccess(
            Asn1Sequence seq)
        {
            if (seq.Count < 1)
                throw new ArgumentException("sequence may not be empty");

            this.descriptions = new AccessDescription[seq.Count];

            for (int i = 0; i < seq.Count; ++i)
            {
                descriptions[i] = AccessDescription.GetInstance(seq[i]);
            }
        }

        public AuthorityInformationAccess(
            AccessDescription description)
        {
            this.descriptions = new AccessDescription[]{ description };
        }

        public AuthorityInformationAccess(
            AccessDescription[] descriptions)
        {
            this.descriptions = Copy(descriptions);
        }

        /**
         * create an AuthorityInformationAccess with the oid and location provided.
         */
        public AuthorityInformationAccess(DerObjectIdentifier oid, GeneralName location)
            : this(new AccessDescription(oid, location))
        {
        }

        public AccessDescription[] GetAccessDescriptions()
        {
            return Copy(descriptions);
        }

        public override Asn1Object ToAsn1Object()
        {
            return new DerSequence(descriptions);
        }

        public override string ToString()
        {
            //return "AuthorityInformationAccess: Oid(" + this.descriptions[0].AccessMethod.Id + ")";

            StringBuilder buf = new StringBuilder();
            buf.AppendLine("AuthorityInformationAccess:");
            foreach (AccessDescription description in descriptions)
            {
                buf.Append("    ")
                   .Append(description)
                   .AppendLine();
            }
            return buf.ToString();
        }
    }
}