KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > ca > certextensions > BasicCertificateExtension


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.core.model.ca.certextensions;
15
16 import java.math.BigInteger JavaDoc;
17
18 import org.bouncycastle.asn1.DERBitString;
19 import org.bouncycastle.asn1.DERBoolean;
20 import org.bouncycastle.asn1.DEREncodable;
21 import org.bouncycastle.asn1.DERIA5String;
22 import org.bouncycastle.asn1.DERInteger;
23 import org.bouncycastle.asn1.DERNull;
24 import org.bouncycastle.asn1.DEROctetString;
25 import org.bouncycastle.asn1.DERPrintableString;
26 import org.bouncycastle.asn1.DERUTF8String;
27 import org.bouncycastle.util.encoders.Hex;
28 import org.ejbca.core.model.InternalResources;
29 import org.ejbca.core.model.ca.caadmin.CA;
30 import org.ejbca.core.model.ca.certificateprofiles.CertificateProfile;
31 import org.ejbca.core.model.ra.UserDataVO;
32
33 /**
34  * The default basic certificate extension that has two property.
35  *
36  * 'value' : The value returned
37  * 'encoding' : How the value is encoded.
38  *
39  * See dokumentation for more information.
40  *
41  * @author Philip Vendil 2007 jan 5
42  *
43  * @version $Id: BasicCertificateExtension.java,v 1.1.2.2 2007/08/06 09:51:13 jeklund Exp $
44  */

45
46 public class BasicCertificateExtension extends CertificateExtension {
47
48     private static final InternalResources intres = InternalResources.getInstance();
49     
50     private static String JavaDoc ENCODING_DERBITSTRING = "DERBITSTRING";
51     private static String JavaDoc ENCODING_DERINTEGER = "DERINTEGER";
52     private static String JavaDoc ENCODING_DEROCTETSTRING = "DEROCTETSTRING";
53     private static String JavaDoc ENCODING_DERBOOLEAN = "DERBOOLEAN";
54     private static String JavaDoc ENCODING_DERPRINTABLESTRING = "DERPRINTABLESTRING";
55     private static String JavaDoc ENCODING_DERUTF8STRING = "DERUTF8STRING";
56     private static String JavaDoc ENCODING_DERIA5STRING = "DERIA5STRING";
57     private static String JavaDoc ENCODING_DERNULL = "DERNULL";
58     
59     // Defined Properties
60
private static String JavaDoc PROPERTY_VALUE = "value";
61     private static String JavaDoc PROPERTY_ENCODING = "encoding";
62     
63     private DEREncodable dEREncodable = null;
64     
65     /**
66      * Returns the defined property 'value' in the encoding
67      * specified in 'encoding'.
68      *
69      * @param userData not used
70      * @param ca not used
71      * @param certProfile not used
72      * @see org.ejbca.core.model.ca.certextensions.CertificateExtension#getValue(org.ejbca.core.model.ra.UserDataVO, org.ejbca.core.model.ca.caadmin.CA, org.ejbca.core.model.ca.certificateprofiles.CertificateProfile)
73      */

74     public DEREncodable getValue(UserDataVO userData, CA ca,
75             CertificateProfile certProfile)
76             throws CertificateExtentionConfigurationException {
77
78         if(dEREncodable == null){
79           String JavaDoc value = getProperties().getProperty(PROPERTY_VALUE);
80           String JavaDoc encoding = getProperties().getProperty(PROPERTY_ENCODING);
81           
82           if(!encoding.equalsIgnoreCase(ENCODING_DERNULL) && (value == null || value.trim().equals(""))){
83               throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.basic.incorrectvalue", new Integer JavaDoc(getId())));
84           }
85           
86           if(encoding.equalsIgnoreCase(ENCODING_DERBITSTRING)){
87               dEREncodable = parseDERBitString(value);
88           }else
89               if(encoding.equalsIgnoreCase(ENCODING_DERINTEGER)){
90                   dEREncodable = parseDERInteger(value);
91               }else
92                   if(encoding.equalsIgnoreCase(ENCODING_DEROCTETSTRING)){
93                       dEREncodable = parseDEROctetString(value);
94                   }else
95                       if(encoding.equalsIgnoreCase(ENCODING_DERBOOLEAN)){
96                           dEREncodable = parseDERBoolean(value);
97                       }else
98                           if(encoding.equalsIgnoreCase(ENCODING_DERPRINTABLESTRING)){
99                               dEREncodable = parseDERPrintableString(value);
100                           }else
101                               if(encoding.equalsIgnoreCase(ENCODING_DERUTF8STRING)){
102                                   dEREncodable = parseDERUTF8String(value);
103                               }else
104                                   if(encoding.equalsIgnoreCase(ENCODING_DERIA5STRING)){
105                                       dEREncodable = parseDERIA5String(value);
106                                   }else
107                                       if(encoding.equalsIgnoreCase(ENCODING_DERNULL)){
108                                           dEREncodable = new DERNull();
109                                       }else{
110                                           throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.basic.incorrectenc",new Integer JavaDoc(getId())));
111                                       }
112         }
113         
114         return dEREncodable;
115     }
116
117     private DEREncodable parseDERBitString(String JavaDoc value) throws CertificateExtentionConfigurationException {
118         DEREncodable retval = null;
119         try{
120             BigInteger JavaDoc bigInteger = new BigInteger JavaDoc(value,2);
121             int padBits = 8 - (value.length() % 8);
122             if(padBits == 8){
123                 padBits = 0;
124             }
125             byte[] byteArray = bigInteger.toByteArray();
126             if ( value.length() % 8 == 0 ) {
127                 // Remove empty extra byte
128
byte[] shorterByteArray = new byte[byteArray.length-1];
129                 for (int i=0; i<shorterByteArray.length; i++) {
130                     shorterByteArray[i] = byteArray[i+1];
131                 }
132                 byteArray = shorterByteArray;
133             }
134             retval = new DERBitString(byteArray, padBits);
135         }catch(NumberFormatException JavaDoc e){
136             throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.basic.illegalvalue",value,new Integer JavaDoc(getId())));
137         }
138         
139         return retval;
140     }
141     
142     private DEREncodable parseDERInteger(String JavaDoc value) throws CertificateExtentionConfigurationException {
143         DEREncodable retval = null;
144         try{
145             BigInteger JavaDoc intValue = new BigInteger JavaDoc(value,10);
146             retval = new DERInteger(intValue);
147         }catch(NumberFormatException JavaDoc e){
148             throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.basic.illegalvalue",value,new Integer JavaDoc(getId())));
149         }
150
151         return retval;
152     }
153     
154     private DEREncodable parseDEROctetString(String JavaDoc value) throws CertificateExtentionConfigurationException {
155         DEREncodable retval = null;
156         if(value.matches("^\\p{XDigit}*")){
157           byte[] bytes = Hex.decode(value);
158           retval = new DEROctetString(bytes);
159         }else{
160             throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.basic.illegalvalue",value,new Integer JavaDoc(getId())));
161         }
162         return retval;
163     }
164     
165     private DEREncodable parseDERBoolean(String JavaDoc value) throws CertificateExtentionConfigurationException {
166         DEREncodable retval = null;
167         if(value.equalsIgnoreCase("TRUE")){
168             retval = DERBoolean.TRUE;
169         }
170         
171         if(value.equalsIgnoreCase("FALSE")){
172             retval = DERBoolean.FALSE;
173         }
174         
175         if(retval == null){
176             throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.basic.illegalvalue",value,new Integer JavaDoc(getId())));
177         }
178
179         return retval;
180     }
181     
182     private DEREncodable parseDERPrintableString(String JavaDoc value) throws CertificateExtentionConfigurationException {
183         try{
184           return new DERPrintableString(value,true);
185         }catch(java.lang.IllegalArgumentException JavaDoc e){
186             throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.basic.illegalvalue",value,new Integer JavaDoc(getId())));
187         }
188     }
189     
190     private DEREncodable parseDERUTF8String(String JavaDoc value) {
191         return new DERUTF8String(value);
192     }
193     private DEREncodable parseDERIA5String(String JavaDoc value) {
194         return new DERIA5String(value, true);
195     }
196
197
198 }
199
Popular Tags