summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/certsrv/authentication/AuthToken.java
blob: 827278711aff430373ca36c014c2e0550766e68b (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// --- 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 com.netscape.certsrv.authentication;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;

import netscape.security.util.DerInputStream;
import netscape.security.util.DerOutputStream;
import netscape.security.util.DerValue;
import netscape.security.x509.CertificateExtensions;
import netscape.security.x509.X509CertImpl;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.usrgrp.Certificates;

/**
 * Authentication token returned by Authentication Managers.
 * Upon return, it contains authentication/identification information
 * as well as information retrieved from the database where the
 * authentication was done against. Each authentication manager has
 * its own list of such information. See individual authenticaiton
 * manager for more details.
 * <p>
 *
 * @version $Revision$, $Date$
 */
public class AuthToken implements IAuthToken {
    protected Hashtable<String, Object> mAttrs = null;

    /* Subject name of the certificate in the authenticating entry */
    public static final String TOKEN_CERT_SUBJECT = "tokenCertSubject";

    /* NotBefore value of the certificate in the authenticating entry */
    public static final String TOKEN_CERT_NOTBEFORE = "tokenCertNotBefore";

    /* NotAfter value of the certificate in the authenticating entry */
    public static final String TOKEN_CERT_NOTAFTER = "tokenCertNotAfter";

    /* Cert Extentions value of the certificate in the authenticating entry */
    public static final String TOKEN_CERT_EXTENSIONS = "tokenCertExts";

    /* Serial number of the certificate in the authenticating entry */
    public static final String TOKEN_CERT_SERIALNUM = "certSerial";

    /**
     * Certificate to be renewed
     */
    public static final String TOKEN_CERT = "tokenCert";

    /* Certificate to be revoked */
    public static final String TOKEN_CERT_TO_REVOKE = "tokenCertToRevoke";

    /**
     * Plugin name of the authentication manager that created the
     * AuthToken as a string.
     */
    public static final String TOKEN_AUTHMGR_IMPL_NAME = "authMgrImplName";

    /**
     * Name of the authentication manager that created the AuthToken
     * as a string.
     */
    public static final String TOKEN_AUTHMGR_INST_NAME = "authMgrInstName";

    /**
     * Time of authentication as a java.util.Date
     */
    public static final String TOKEN_AUTHTIME = "authTime";

    /**
     * Constructs an instance of a authentication token.
     * The token by default contains the following attributes: <br>
     *
     * <pre>
     * 	"authMgrInstName" - The authentication manager instance name.
     * 	"authMgrImplName" - The authentication manager plugin name.
     * 	"authTime" - The - The time of authentication.
     * </pre>
     *
     * @param authMgr The authentication manager that created this Token.
     */
    public AuthToken(IAuthManager authMgr) {
        mAttrs = new Hashtable<String, Object>();
        if (authMgr != null) {
            set(TOKEN_AUTHMGR_INST_NAME, authMgr.getName());
            set(TOKEN_AUTHMGR_IMPL_NAME, authMgr.getImplName());
        }
        set(TOKEN_AUTHTIME, new Date());
    }

    public Object get(String attrName) {
        return mAttrs.get(attrName);
    }

    public String getInString(String attrName) {
        return (String) mAttrs.get(attrName);
    }

    public boolean set(String attrName, String value) {
        if (value == null) {
            return false;
        }
        mAttrs.put(attrName, value);
        return true;
    }

    /**
     * Removes an attribute in the AuthToken
     *
     * @param attrName The name of the attribute to remove.
     */
    public void delete(String attrName) {
        mAttrs.remove(attrName);
    }

    /**
     * Enumerate all attribute names in the AuthToken.
     *
     * @return Enumeration of all attribute names in this AuthToken.
     */
    public Enumeration<String> getElements() {
        return (mAttrs.keys());
    }

    public byte[] getInByteArray(String name) {
        String value = getInString(name);
        if (value == null) {
            return null;
        }
        return CMS.AtoB(value);
    }

    public boolean set(String name, byte[] value) {
        if (value == null) {
            return false;
        }
        return set(name, CMS.BtoA(value));
    }

    public Integer getInInteger(String name) {
        String strVal = getInString(name);
        if (strVal == null) {
            return null;
        }
        try {
            return Integer.valueOf(strVal);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public boolean set(String name, Integer value) {
        if (value == null) {
            return false;
        }
        return set(name, value.toString());
    }

    public BigInteger[] getInBigIntegerArray(String name) {
        String value = getInString(name);
        if (value == null) {
            return null;
        }
        String[] values = value.split(",");
        if (values.length == 0) {
            return null;
        }
        BigInteger[] result = new BigInteger[values.length];
        for (int i = 0; i < values.length; i++) {
            try {
                result[i] = new BigInteger(values[i]);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return result;
    }

    public boolean set(String name, BigInteger[] value) {
        if (value == null) {
            return false;
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < value.length; i++) {
            if (i != 0) {
                buffer.append(",");
            }
            buffer.append(value[i].toString());
        }
        return set(name, buffer.toString());
    }

    public Date getInDate(String name) {
        String value = getInString(name);
        if (value == null) {
            return null;
        }
        try {
            return new Date(Long.parseLong(value));
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public boolean set(String name, Date value) {
        if (value == null) {
            return false;
        }
        return set(name, String.valueOf(value.getTime()));
    }

    public String[] getInStringArray(String name) {
        String[] stringValues;

        byte[] byteValue = getInByteArray(name);
        if (byteValue == null) {
            return null;
        }
        try {
            DerInputStream in = new DerInputStream(byteValue);
            DerValue[] derValues = in.getSequence(5);
            stringValues = new String[derValues.length];
            for (int i = 0; i < derValues.length; i++) {
                stringValues[i] = derValues[i].getAsString();
            }
        } catch (IOException e) {
            return null;
        }
        return stringValues;
    }

    public boolean set(String name, String[] value) {
        if (value == null) {
            return false;
        }
        DerOutputStream out = new DerOutputStream();
        DerValue[] derValues = new DerValue[value.length];
        try {
            for (int i = 0; i < value.length; i++) {
                derValues[i] = new DerValue(value[i]);
            }
            out.putSequence(derValues);
            return set(name, out.toByteArray());
        } catch (IOException e) {
            return false;
        }
    }

    public X509CertImpl getInCert(String name) {
        byte[] data = getInByteArray(name);
        if (data == null) {
            return null;
        }
        try {
            return new X509CertImpl(data);
        } catch (CertificateException e) {
            return null;
        }
    }

    public boolean set(String name, X509CertImpl value) {
        if (value == null) {
            return false;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            value.encode(out);
        } catch (CertificateEncodingException e) {
            return false;
        }
        return set(name, out.toByteArray());
    }

    public CertificateExtensions getInCertExts(String name) throws IOException {
        CertificateExtensions exts = null;
        byte[] data = getInByteArray(name);
        if (data != null) {
            exts = new CertificateExtensions();
            // exts.decode() doesn't work for empty CertExts
            exts.decodeEx(new ByteArrayInputStream(data));
        }
        return exts;
    }

    public boolean set(String name, CertificateExtensions value) {
        if (value == null) {
            return false;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            value.encode(out);
        } catch (IOException e) {
            return false;
        } catch (CertificateException e) {
            return false;
        }
        return set(name, out.toByteArray());
    }

    public Certificates getInCertificates(String name) throws IOException, CertificateException {
        X509CertImpl[] certArray;

        byte[] byteValue = getInByteArray(name);
        if (byteValue == null) {
            return null;
        }

        DerInputStream in = new DerInputStream(byteValue);
        DerValue[] derValues = in.getSequence(5);
        certArray = new X509CertImpl[derValues.length];
        for (int i = 0; i < derValues.length; i++) {
            byte[] certData = derValues[i].toByteArray();
            certArray[i] = new X509CertImpl(certData);
        }
        return new Certificates(certArray);
    }

    public boolean set(String name, Certificates value) {
        if (value == null) {
            return false;
        }
        DerOutputStream derStream = new DerOutputStream();
        X509Certificate[] certArray = value.getCertificates();
        DerValue[] derValues = new DerValue[certArray.length];
        try {
            for (int i = 0; i < certArray.length; i++) {
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                try {
                    X509CertImpl certImpl = (X509CertImpl) certArray[i];
                    certImpl.encode(byteStream);
                    derValues[i] = new DerValue(byteStream.toByteArray());
                } catch (CertificateEncodingException e) {
                    return false;
                } catch (ClassCastException e) {
                    return false;
                }
            }
            derStream.putSequence(derValues);
            return set(name, derStream.toByteArray());
        } catch (IOException e) {
            return false;
        }
    }

    public byte[][] getInByteArrayArray(String name) throws IOException {
        byte[][] retval;

        byte[] byteValue = getInByteArray(name);
        if (byteValue == null) {
            return null;
        }
        DerInputStream in = new DerInputStream(byteValue);
        DerValue[] derValues = in.getSequence(5);
        retval = new byte[derValues.length][];
        for (int i = 0; i < derValues.length; i++) {
            retval[i] = derValues[i].getOctetString();
        }
        return retval;
    }

    public boolean set(String name, byte[][] value) {
        if (value == null) {
            return false;
        }
        DerOutputStream out = new DerOutputStream();
        DerValue[] derValues = new DerValue[value.length];
        try {
            for (int i = 0; i < value.length; i++) {
                derValues[i] = new DerValue(DerValue.tag_OctetString, value[i]);
            }
            out.putSequence(derValues);
            return set(name, out.toByteArray());
        } catch (IOException e) {
            return false;
        }
    }

    /**
     * Enumerate all attribute values in the AuthToken.
     *
     * @return Enumeration of all attribute names in this AuthToken.
     */
    public Enumeration<Object> getVals() {
        return (mAttrs.elements());
    }

    /**
     * Gets the name of the authentication manager instance that created
     * this token.
     *
     * @return The name of the authentication manager instance that created
     *         this token.
     */
    public String getAuthManagerInstName() {
        return ((String) mAttrs.get(TOKEN_AUTHMGR_INST_NAME));
    }

    /**
     * Gets the plugin name of the authentication manager that created this
     * token.
     *
     * @return The plugin name of the authentication manager that created this
     *         token.
     */
    public String getAuthManagerImplName() {
        return ((String) mAttrs.get(TOKEN_AUTHMGR_IMPL_NAME));
    }

    /**
     * Gets the time of authentication.
     *
     * @return The time of authentication
     */
    public Date getAuthTime() {
        return ((Date) mAttrs.get(TOKEN_AUTHTIME));
    }
}