summaryrefslogtreecommitdiffstats
path: root/pki/base/util/src/netscape/security/x509/BasicConstraintsExtension.java
blob: ed48d2d4d5e6f215562bf34ed2dc89d47902966a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package netscape.security.x509;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.util.Enumeration;

import netscape.security.util.BigInt;
import netscape.security.util.DerOutputStream;
import netscape.security.util.DerValue;

/**
 * This class represents the Basic Constraints Extension.
 *
 * <p>The basic constraints extension identifies whether the subject of the
 * certificate is a CA and how deep a certification path may exist
 * through that CA.
 *
 * <pre>
 * The ASN.1 syntax for this extension is:
 * BasicConstraints ::= SEQUENCE {
 *     cA                BOOLEAN DEFAULT FALSE,
 *     pathLenConstraint INTEGER (0..MAX) OPTIONAL
 * }
 * </pre>
 * @author Amit Kapoor
 * @author Hemma Prafullchandra
 * @version 1.7
 * @see CertAttrSet
 * @see Extension
 */
public class BasicConstraintsExtension extends Extension
implements CertAttrSet {
    /**
     * Identifier for this attribute, to be used with the
     * get, set, delete methods of Certificate, x509 type.
     */  
    public static final String IDENT = "x509.info.extensions.BasicConstraints";
    /**
     * Attribute names.
     */
    public static final String NAME = "BasicConstraints";
    public static final String IS_CA = "is_ca";
    public static final String PATH_LEN = "path_len";

    // Private data members
    private boolean	ca = false;
    private int	pathLen = -1;

    // Encode this extension value
    private void encodeThis() throws IOException {
        DerOutputStream out = new DerOutputStream();
        DerOutputStream tmp = new DerOutputStream();

        if (ca) {
            tmp.putBoolean(ca);
        }
        if (pathLen >= 0) {
            tmp.putInteger(new BigInt(pathLen));
        }
        out.write(DerValue.tag_Sequence, tmp);
        this.extensionValue = out.toByteArray();
    }

    /**
     * Default constructor for this object.
     *
     * @param ca true, if the subject of the Certificate is a CA.
     * @param len specifies the depth of the certification path.
     */
    public BasicConstraintsExtension(boolean ca, int len) throws IOException {
        this.ca = ca;
        this.pathLen = len;
        this.extensionId = PKIXExtensions.BasicConstraints_Id;
        if (ca) {
            critical = true;
        } else {
            critical = false;
        }
        encodeThis();
    }

    /**
     * Default constructor for this object.
     *
     * @param ca true, if the subject of the Certificate is a CA.
     * @param len specifies the depth of the certification path.
     */
    public BasicConstraintsExtension(boolean ca, boolean critical, int len) throws IOException {
        this.ca = ca;
        this.pathLen = len;
        this.extensionId = PKIXExtensions.BasicConstraints_Id;
        this.critical = critical;
        encodeThis();
    }

    /**
     * Create the extension from the passed DER encoded value of the same.
     *
     * @param extension the DER encoded value of the extension.
     * @exception IOException on error.
     */
     public BasicConstraintsExtension(Boolean critical, Object value)
     throws IOException {
         this.extensionId = PKIXExtensions.BasicConstraints_Id;
	 this.critical = critical.booleanValue();

         if (value instanceof byte[]) {
         int len = Array.getLength(value);
         byte[] extValue = new byte[len];
             System.arraycopy(value, 0, extValue, 0, len);

         this.extensionValue = extValue;
         DerValue val = new DerValue(extValue);
         if (val.tag != DerValue.tag_Sequence) {
                 throw new IOException("Invalid encoding of BasicConstraints");
         }

             // non-CA cert with no limit to certification path length
             if (val.data == null || val.data.available() < 1) {
                 this.ca = false;
                 this.pathLen = -1;
                 return;
             }
         DerValue opt = val.data.getDerValue();
         if (opt.tag != DerValue.tag_Boolean) {
                 this.ca = false;
         } else {
                 this.ca = true;
             if (val.data.available() != 0) {
	         opt = val.data.getDerValue();
             } else {
                     this.pathLen = -1;
	         return;
             }
         }
         if (opt.tag != DerValue.tag_Integer) {
                 throw new IOException("Invalid encoding of BasicConstraints");
         }
             this.pathLen = (opt.getInteger()).toInt();
             /*
              * Activate this check once again after PKIX profiling
              * is a standard and this check no longer imposes an
              * interoperability barrier.
              * if (ca) {
              *   if (!this.critical) {
              *   throw new IOException("Criticality cannot be false for CA.");
              *   }
              * }
              */
          } else
              throw new IOException("Invalid argument type");
     }

     /**
      * Return user readable form of extension.
      */
     public String toString() {
         String s = super.toString() + "BasicConstraints:[\n";

         s += ((ca) ? ("CA:true") : ("CA:false")) + "\n";
         if (pathLen >= 0) {
             s += "PathLen:" + pathLen + "\n";
         } else {
             s += "PathLen: undefined\n";
         }
         return (s + "]\n");
     }

    /**
     * Decode the extension from the InputStream.
     *
     * @param in the InputStream to unmarshal the contents from.
     * @exception IOException on decoding or validity errors.
     */
    public void decode(InputStream in) throws IOException {
        throw new IOException("Method not to be called directly.");
    }

     /**
      * Encode this extension value to the output stream.
      *
      * @param out the DerOutputStream to encode the extension to.
      */
     public void encode(OutputStream out) throws IOException {
         DerOutputStream tmp = new DerOutputStream();
         if (extensionValue == null) {
             this.extensionId = PKIXExtensions.BasicConstraints_Id;
/* #57286 - so that profile can set critiality */
/*
             if (ca) {
	         critical = true;
             } else {
	         critical = false;
             }
*/
             encodeThis();
         }
         super.encode(tmp);

	 out.write(tmp.toByteArray());
     }

    /**
     * Set the attribute value.
     */
    public void set(String name, Object obj) throws IOException {
        clearValue();
	if (name.equalsIgnoreCase(IS_CA)) {
	    if (!(obj instanceof Boolean)) {
	      throw new IOException("Attribute value should be of type Boolean.");
	    }
	    ca = ((Boolean)obj).booleanValue();
	} else if (name.equalsIgnoreCase(PATH_LEN)) {
	    if (!(obj instanceof Integer)) {
	      throw new IOException("Attribute value should be of type Integer.");
	    }
	    pathLen = ((Integer)obj).intValue();
	} else {
	  throw new IOException("Attribute name not recognized by " + 
				"CertAttrSet:BasicConstraints.");
	}
    }

    /**
     * Get the attribute value.
     */
    public Object get(String name) throws IOException {
	if (name.equalsIgnoreCase(IS_CA)) {
	    return (new Boolean(ca));
	} else if (name.equalsIgnoreCase(PATH_LEN)) {
	    return (Integer.valueOf(pathLen));
	} else {
	  throw new IOException("Attribute name not recognized by " + 
				"CertAttrSet:BasicConstraints.");
	}
    }

    /**
     * Delete the attribute value.
     */
    public void delete(String name) throws IOException {
	if (name.equalsIgnoreCase(IS_CA)) {
	    ca = false;
	} else if (name.equalsIgnoreCase(PATH_LEN)) {
	    pathLen = -1;
	} else {
	  throw new IOException("Attribute name not recognized by " + 
				"CertAttrSet:BasicConstraints.");
	}
    }

    /**
     * Return an enumeration of names of attributes existing within this
     * attribute.
     */
    public Enumeration getElements() {
        AttributeNameEnumeration elements = new AttributeNameEnumeration();
        elements.addElement(IS_CA);
        elements.addElement(PATH_LEN);

	return (elements.elements());
    }

    /**
     * Return the name of this attribute.
     */
    public String getName() {
        return (NAME);
    }
}