1 13 14 package org.ejbca.core.model.ca.certextensions; 15 16 import java.math.BigInteger ; 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 45 46 public class BasicCertificateExtension extends CertificateExtension { 47 48 private static final InternalResources intres = InternalResources.getInstance(); 49 50 private static String ENCODING_DERBITSTRING = "DERBITSTRING"; 51 private static String ENCODING_DERINTEGER = "DERINTEGER"; 52 private static String ENCODING_DEROCTETSTRING = "DEROCTETSTRING"; 53 private static String ENCODING_DERBOOLEAN = "DERBOOLEAN"; 54 private static String ENCODING_DERPRINTABLESTRING = "DERPRINTABLESTRING"; 55 private static String ENCODING_DERUTF8STRING = "DERUTF8STRING"; 56 private static String ENCODING_DERIA5STRING = "DERIA5STRING"; 57 private static String ENCODING_DERNULL = "DERNULL"; 58 59 private static String PROPERTY_VALUE = "value"; 61 private static String PROPERTY_ENCODING = "encoding"; 62 63 private DEREncodable dEREncodable = null; 64 65 74 public DEREncodable getValue(UserDataVO userData, CA ca, 75 CertificateProfile certProfile) 76 throws CertificateExtentionConfigurationException { 77 78 if(dEREncodable == null){ 79 String value = getProperties().getProperty(PROPERTY_VALUE); 80 String 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 (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 (getId()))); 111 } 112 } 113 114 return dEREncodable; 115 } 116 117 private DEREncodable parseDERBitString(String value) throws CertificateExtentionConfigurationException { 118 DEREncodable retval = null; 119 try{ 120 BigInteger bigInteger = new BigInteger (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 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 e){ 136 throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.basic.illegalvalue",value,new Integer (getId()))); 137 } 138 139 return retval; 140 } 141 142 private DEREncodable parseDERInteger(String value) throws CertificateExtentionConfigurationException { 143 DEREncodable retval = null; 144 try{ 145 BigInteger intValue = new BigInteger (value,10); 146 retval = new DERInteger(intValue); 147 }catch(NumberFormatException e){ 148 throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.basic.illegalvalue",value,new Integer (getId()))); 149 } 150 151 return retval; 152 } 153 154 private DEREncodable parseDEROctetString(String 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 (getId()))); 161 } 162 return retval; 163 } 164 165 private DEREncodable parseDERBoolean(String 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 (getId()))); 177 } 178 179 return retval; 180 } 181 182 private DEREncodable parseDERPrintableString(String value) throws CertificateExtentionConfigurationException { 183 try{ 184 return new DERPrintableString(value,true); 185 }catch(java.lang.IllegalArgumentException e){ 186 throw new CertificateExtentionConfigurationException(intres.getLocalizedMessage("certext.basic.illegalvalue",value,new Integer (getId()))); 187 } 188 } 189 190 private DEREncodable parseDERUTF8String(String value) { 191 return new DERUTF8String(value); 192 } 193 private DEREncodable parseDERIA5String(String value) { 194 return new DERIA5String(value, true); 195 } 196 197 198 } 199 | Popular Tags |