summary refs log tree commit diff
path: root/crypto/src/asn1/crmf/EncKeyWithID.cs
blob: 6de56fa0b67d798f622bd3d991ae7673c1df8cb7 (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
using System;

using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Crmf
{
    public class EncKeyWithID
        : Asn1Encodable
    {
        private readonly PrivateKeyInfo privKeyInfo;
        private readonly Asn1Encodable identifier;

        public static EncKeyWithID GetInstance(object obj)
        {
            if (obj is EncKeyWithID)
                return (EncKeyWithID)obj;

            if (obj != null)
                return new EncKeyWithID(Asn1Sequence.GetInstance(obj));

            return null;
        }

        private EncKeyWithID(Asn1Sequence seq)
        {
            this.privKeyInfo = PrivateKeyInfo.GetInstance(seq[0]);

            if (seq.Count > 1)
            {
                if (!(seq[1] is DerUtf8String))
                {
                    this.identifier = GeneralName.GetInstance(seq[1]);
                }
                else
                {
                    this.identifier = (Asn1Encodable)seq[1];
                }
            }
            else
            {
                this.identifier = null;
            }
        }

        public EncKeyWithID(PrivateKeyInfo privKeyInfo)
        {
            this.privKeyInfo = privKeyInfo;
            this.identifier = null;
        }

        public EncKeyWithID(PrivateKeyInfo privKeyInfo, DerUtf8String str)
        {
            this.privKeyInfo = privKeyInfo;
            this.identifier = str;
        }

        public EncKeyWithID(PrivateKeyInfo privKeyInfo, GeneralName generalName)
        {
            this.privKeyInfo = privKeyInfo;
            this.identifier = generalName;
        }

        public virtual PrivateKeyInfo PrivateKey
        {
            get { return privKeyInfo; }
        }

        public virtual bool HasIdentifier
        {
            get { return identifier != null; }
        }

        public virtual bool IsIdentifierUtf8String
        {
            get { return identifier is DerUtf8String; }
        }

        public virtual Asn1Encodable Identifier
        {
            get { return identifier; }
        }

        /**
         * <pre>
         * EncKeyWithID ::= SEQUENCE {
         *      privateKey           PrivateKeyInfo,
         *      identifier CHOICE {
         *         string               UTF8String,
         *         generalName          GeneralName
         *     } OPTIONAL
         * }
         * </pre>
         * @return
         */
        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(privKeyInfo);
            v.AddOptional(identifier);
            return new DerSequence(v);
        }
    }
}