1 module hunt.security.x509.CertificateAlgorithmId;
2 
3 import hunt.security.x509.AlgorithmId;
4 import hunt.security.x509.CertAttrSet;
5 
6 import hunt.security.util.DerValue;
7 import hunt.security.util.DerInputStream;
8 import hunt.security.util.DerOutputStream;
9 
10 import hunt.Exceptions;
11 import hunt.text.Common;
12 
13 import hunt.stream.Common;
14 import hunt.collection;
15 
16 import std.conv;
17 
18 
19 /**
20  * This class defines the AlgorithmId for the Certificate.
21  *
22  * @author Amit Kapoor
23  * @author Hemma Prafullchandra
24  */
25 class CertificateAlgorithmId : CertAttrSet!(string, AlgorithmId) {
26     private AlgorithmId algId;
27 
28     /**
29      * Identifier for this attribute, to be used with the
30      * get, set, delete methods of Certificate, x509 type.
31      */
32     enum string IDENT = "x509.info.algorithmID";
33     /**
34      * Sub attributes name for this CertAttrSet.
35      */
36     enum string NAME = "algorithmID";
37 
38     /**
39      * Identifier to be used with get, set, and delete methods. When
40      * using this identifier the associated object being passed in or
41      * returned is an instance of AlgorithmId.
42      * @see sun.security.x509.AlgorithmId
43      */
44     enum string ALGORITHM = "algorithm";
45 
46     /**
47      * Default constructor for the certificate attribute.
48      *
49      * @param algId the Algorithm identifier
50      */
51     this(AlgorithmId algId) {
52         this.algId = algId;
53     }
54 
55     /**
56      * Create the object, decoding the values from the passed DER stream.
57      *
58      * @param stream the DerInputStream to read the serial number from.
59      * @exception IOException on decoding errors.
60      */
61     this(DerInputStream stream) {
62         DerValue val = stream.getDerValue();
63         algId = AlgorithmId.parse(val);
64     }
65 
66     /**
67      * Create the object, decoding the values from the passed stream.
68      *
69      * @param stream the InputStream to read the serial number from.
70      * @exception IOException on decoding errors.
71      */
72     this(InputStream stream) {
73         DerValue val = new DerValue(stream);
74         algId = AlgorithmId.parse(val);
75     }
76 
77     /**
78      * Return the algorithm identifier as user readable string.
79      */
80     override string toString() {
81         if (algId is null) return "";
82         return (algId.toString() ~
83                 ", OID = " ~ (algId.getOID()).toString() ~ "\n");
84     }
85 
86     /**
87      * Encode the algorithm identifier in DER form to the stream.
88      *
89      * @param stream the DerOutputStream to marshal the contents to.
90      * @exception IOException on errors.
91      */
92     void encode(OutputStream stream) {
93         DerOutputStream tmp = new DerOutputStream();
94         algId.encode(tmp);
95 
96         stream.write(tmp.toByteArray());
97     }
98 
99     /**
100      * Set the attribute value.
101      */
102     void set(string name, AlgorithmId obj) {
103         if (obj is null) {
104             throw new IOException("Attribute must be of type AlgorithmId.");
105         }
106         if (name.equalsIgnoreCase(ALGORITHM)) {
107             algId = cast(AlgorithmId)obj;
108         } else {
109             throw new IOException("Attribute name not recognized by " ~
110                               "CertAttrSet:CertificateAlgorithmId.");
111         }
112     }
113 
114     /**
115      * Get the attribute value.
116      */
117     AlgorithmId get(string name) {
118         if (name.equalsIgnoreCase(ALGORITHM)) {
119             return (algId);
120         } else {
121             throw new IOException("Attribute name not recognized by " ~
122                                "CertAttrSet:CertificateAlgorithmId.");
123         }
124     }
125 
126     /**
127      * Delete the attribute value.
128      */
129     void remove(string name) {
130         if (name.equalsIgnoreCase(ALGORITHM)) {
131             algId = null;
132         } else {
133             throw new IOException("Attribute name not recognized by " ~
134                                "CertAttrSet:CertificateAlgorithmId.");
135         }
136     }
137 
138     /**
139      * Return an enumeration of names of attributes existing within this
140      * attribute.
141      */
142     Enumeration!string getElements() {
143         // AttributeNameEnumeration elements = new AttributeNameEnumeration();
144         // elements.addElement(ALGORITHM);
145         // return (elements.elements());
146         implementationMissing();
147         return null;
148     }
149 
150    /**
151     * Return the name of this attribute.
152     */
153    string getName() {
154       return (NAME);
155    }
156 }