Initial commit.
[BoarSSL] / XKeys / AlgorithmIdentifier.cs
1 /*
2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 using System;
26
27 using Asn1;
28
29 /*
30 * A wrapper class for an AlgorithmIdentifier (SEQUENCE of an OID
31 * then optional parameters).
32 */
33
34 class AlgorithmIdentifier {
35
36 /*
37 * Get the OID that identifies the algorithm.
38 */
39 internal string OID {
40 get {
41 return oid;
42 }
43 }
44
45 /*
46 * Get the algorithm parameters. This may be null if the
47 * structure did not contain parameters.
48 */
49 internal AsnElt Parameters {
50 get {
51 return parameters;
52 }
53 }
54
55 string oid;
56 AsnElt parameters;
57
58 /*
59 * Create an instance over the provided ASN.1 element. The
60 * outer tag will be checked to match the universal tag for
61 * SEQUENCE.
62 */
63 internal AlgorithmIdentifier(AsnElt ai) : this(ai, true)
64 {
65 }
66
67 /*
68 * Create an instance over the provided ASN.1 element. If
69 * 'checkTag' is true, then the outer tag will be checked to
70 * match the universal tag for SEQUENCE. Set 'checkTag' to
71 * false if the tag was already checked, or if it has been
72 * overwritten with an implicit tag.
73 */
74 internal AlgorithmIdentifier(AsnElt ai, bool checkTag)
75 {
76 if (checkTag) {
77 ai.CheckTag(AsnElt.SEQUENCE);
78 }
79 ai.CheckNumSubMin(1);
80 ai.CheckNumSubMax(2);
81 AsnElt ao = ai.GetSub(0);
82 ao.CheckTag(AsnElt.OBJECT_IDENTIFIER);
83 oid = ao.GetOID();
84 if (ai.Sub.Length >= 2) {
85 parameters = ai.GetSub(1);
86 } else {
87 parameters = null;
88 }
89 }
90
91 /*
92 * Create a new instance for a given OID, with no parameters.
93 */
94 internal AlgorithmIdentifier(string oid) : this(oid, null)
95 {
96 }
97
98 /*
99 * Create a new instance for a given OID, with the provided
100 * parameters (which may be null).
101 */
102 internal AlgorithmIdentifier(string oid, AsnElt parameters)
103 {
104 this.oid = oid;
105 this.parameters = parameters;
106 }
107
108 /*
109 * Encode this instance as a new ASN.1 object.
110 */
111 internal AsnElt ToAsn1()
112 {
113 AsnElt ao = AsnElt.MakeOID(oid);
114 if (parameters == null) {
115 return AsnElt.Make(AsnElt.SEQUENCE, ao);
116 } else {
117 return AsnElt.Make(AsnElt.SEQUENCE, ao, parameters);
118 }
119 }
120 }