1 package org.ejbca.util.cert; 2 3 import java.io.IOException ; 4 5 import org.bouncycastle.asn1.DERBMPString; 6 import org.bouncycastle.asn1.DERIA5String; 7 import org.bouncycastle.asn1.DERObject; 8 import org.bouncycastle.asn1.DERObjectIdentifier; 9 import org.bouncycastle.asn1.DERPrintableString; 10 import org.bouncycastle.asn1.DERUTF8String; 11 import org.bouncycastle.asn1.x509.X509Name; 12 import org.bouncycastle.asn1.x509.X509NameEntryConverter; 13 14 18 public class PrintableStringEntryConverter 19 extends X509NameEntryConverter 20 { 21 22 26 private boolean canBeUTF8( 27 String str) 28 { 29 for (int i = str.length() - 1; i >= 0; i--) 30 { 31 if (str.charAt(i) > 0x00ff) 32 { 33 return false; 34 } 35 } 36 37 return true; 38 } 39 40 48 public DERObject getConvertedValue( 49 DERObjectIdentifier oid, 50 String value) 51 { 52 if (value.length() != 0 && value.charAt(0) == '#') 53 { 54 try 55 { 56 return convertHexEncoded(value, 1); 57 } 58 catch (IOException e) 59 { 60 throw new RuntimeException ("can't recode value for oid " + oid.getId()); 61 } 62 } 63 else if (oid.equals(X509Name.EmailAddress) || oid.equals(X509Name.DC)) 64 { 65 return new DERIA5String(value); 66 } 67 else if (canBePrintable(value)) 68 { 69 return new DERPrintableString(value); 70 } 71 else if (canBeUTF8(value)) 72 { 73 return new DERUTF8String(value); 74 } 75 76 return new DERBMPString(value); 77 } 78 } 79 | Popular Tags |