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