2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
30 * A wrapper class for an AlgorithmIdentifier (SEQUENCE of an OID
31 * then optional parameters).
34 class AlgorithmIdentifier {
37 * Get the OID that identifies the algorithm.
46 * Get the algorithm parameters. This may be null if the
47 * structure did not contain parameters.
49 internal AsnElt Parameters {
59 * Create an instance over the provided ASN.1 element. The
60 * outer tag will be checked to match the universal tag for
63 internal AlgorithmIdentifier(AsnElt ai) : this(ai, true)
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.
74 internal AlgorithmIdentifier(AsnElt ai, bool checkTag)
77 ai.CheckTag(AsnElt.SEQUENCE);
81 AsnElt ao = ai.GetSub(0);
82 ao.CheckTag(AsnElt.OBJECT_IDENTIFIER);
84 if (ai.Sub.Length >= 2) {
85 parameters = ai.GetSub(1);
92 * Create a new instance for a given OID, with no parameters.
94 internal AlgorithmIdentifier(string oid) : this(oid, null)
99 * Create a new instance for a given OID, with the provided
100 * parameters (which may be null).
102 internal AlgorithmIdentifier(string oid, AsnElt parameters)
105 this.parameters = parameters;
109 * Encode this instance as a new ASN.1 object.
111 internal AsnElt ToAsn1()
113 AsnElt ao = AsnElt.MakeOID(oid);
114 if (parameters == null) {
115 return AsnElt.Make(AsnElt.SEQUENCE, ao);
117 return AsnElt.Make(AsnElt.SEQUENCE, ao, parameters);