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