1 /**
2 * Algorithm Identifier
3 * 
4 * Copyright:
5 * (C) 1999-2007 Jack Lloyd
6 * (C) 2014-2015 Etienne Cimon
7 *
8 * License:
9 * Botan is released under the Simplified BSD License (see LICENSE.md)
10 */
11 module botan.asn1.alg_id;
12 
13 import botan.constants;
14 import botan.utils.types;
15 import botan.asn1.asn1_obj;
16 import botan.asn1.asn1_oid;
17 import botan.asn1.der_enc;
18 import botan.asn1.ber_dec;
19 import botan.asn1.oids;
20 
21 alias AlgorithmIdentifier = RefCounted!AlgorithmIdentifierImpl;
22 
23 /**
24 * Algorithm Identifier
25 */
26 final class AlgorithmIdentifierImpl : ASN1Object
27 {
28 public:
29     alias EncodingOption = bool;
30     enum : EncodingOption { USE_NULL_PARAM }
31 
32     /*
33     * DER encode an AlgorithmIdentifier
34     */
35     override void encodeInto(ref DEREncoder codec) const
36     {
37         //logTrace("encoding OID: ", m_oid.toString());
38         codec.startCons(ASN1Tag.SEQUENCE)
39                 .encode(m_oid)
40                 .rawBytes(m_parameters)
41                 .endCons();
42     }
43 
44     /*
45     * Decode a BER encoded AlgorithmIdentifier
46     */
47     override void decodeFrom(ref BERDecoder codec)
48     {
49         codec.startCons(ASN1Tag.SEQUENCE)
50                 .decode(m_oid)
51                 .rawBytes(m_parameters)
52                 .endCons();
53     }
54 
55     this() { m_oid = OID(); }
56 
57     /*
58     * Create an AlgorithmIdentifier
59     */
60     this(OID alg_id, EncodingOption option) {
61         __gshared immutable ubyte[2] DER_NULL = [ 0x05, 0x00 ];
62         
63         m_oid = alg_id;
64         
65         if (option == USE_NULL_PARAM)
66             m_parameters ~= DER_NULL.ptr[0 .. 2];
67     }
68 
69     /*
70     * Create an AlgorithmIdentifier
71     */
72     this(string alg_id, EncodingOption option) {
73         __gshared immutable ubyte[2] DER_NULL = [ 0x05, 0x00 ];
74         
75         m_oid = OIDS.lookup(alg_id);
76         
77         if (option == USE_NULL_PARAM)
78             m_parameters ~= DER_NULL.ptr[0 .. 2];
79     }
80     
81     /*
82     * Create an AlgorithmIdentifier
83     */
84     this(OID alg_id, ref Vector!ubyte param)
85     {
86         m_oid = alg_id;
87         m_parameters = Vector!ubyte(param[]);
88     }
89 
90     /*
91     * Create an AlgorithmIdentifier
92     */
93     this(in string alg_id, ref Vector!ubyte param) {
94         m_oid = OIDS.lookup(alg_id);
95         m_parameters = Vector!ubyte(param[]);
96     }
97 
98     /*
99      * Make a copy of another AlgorithmIdentifier
100      */
101     this(const ref AlgorithmIdentifier other) {
102         m_oid = OID(other.m_oid);
103         m_parameters = Vector!ubyte(other.m_parameters[]);
104     }
105 
106     /*
107     * Compare two AlgorithmIdentifiers
108     */
109     bool opEquals(in AlgorithmIdentifier a2) const
110     {
111         if (m_oid != a2.m_oid)
112             return false;
113         if (m_parameters != a2.m_parameters)
114             return false;
115         return true;
116     }
117 
118     /*
119     * Compare two AlgorithmIdentifiers
120     */
121     int opCmp(in AlgorithmIdentifier a2) const
122     {
123         if (this == a2) return 0;
124         else return -1;
125     }
126 
127     @property const(OID) oid() const {
128         return m_oid;
129     }
130 
131     @property Vector!ubyte parameters() const {
132         return m_parameters.dup;
133     }
134 
135     @property void oid(OID oid) {
136         m_oid = oid;
137     }
138 
139     @property void parameters()(auto ref Vector!ubyte param) {
140         m_parameters = param.dup;
141     }
142 
143     override string toString() const {
144         return m_oid.toString() ~ " & param length: " ~ m_parameters.length.to!string;
145     }
146 
147 private:
148     OID m_oid;
149     Vector!ubyte m_parameters;
150 }