1 13 14 package org.ejbca.util.cert; 15 16 import java.io.IOException ; 17 import java.math.BigInteger ; 18 import java.security.cert.X509Certificate ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 22 import org.apache.log4j.Logger; 23 import org.bouncycastle.asn1.ASN1Encodable; 24 import org.bouncycastle.asn1.ASN1Sequence; 25 import org.bouncycastle.asn1.DERObject; 26 import org.bouncycastle.asn1.DERObjectIdentifier; 27 import org.bouncycastle.asn1.x509.GeneralName; 28 import org.bouncycastle.asn1.x509.qualified.ETSIQCObjectIdentifiers; 29 import org.bouncycastle.asn1.x509.qualified.MonetaryValue; 30 import org.bouncycastle.asn1.x509.qualified.QCStatement; 31 import org.bouncycastle.asn1.x509.qualified.RFC3739QCObjectIdentifiers; 32 import org.bouncycastle.asn1.x509.qualified.SemanticsInformation; 33 import org.ejbca.util.CertTools; 34 35 41 public class QCStatementExtension extends CertTools { 42 43 private static Logger log = Logger.getLogger(SubjectDirAttrExtension.class); 44 45 48 private QCStatementExtension() { 49 } 50 51 57 public static boolean hasQcStatement(X509Certificate cert) throws IOException { 58 DERObject obj = getExtensionValue(cert, QCSTATEMENTS_OBJECTID); 59 if (obj == null) { 60 return false; 61 } 62 return true; 63 } 64 70 public static Collection getQcStatementIds(X509Certificate cert) throws IOException { 71 ArrayList ret = new ArrayList (); 72 DERObject obj = getExtensionValue(cert, QCSTATEMENTS_OBJECTID); 73 if (obj == null) { 74 return ret; 75 } 76 ASN1Sequence seq = (ASN1Sequence)obj; 77 for (int i = 0; i < seq.size(); i++) { 78 QCStatement qc = QCStatement.getInstance(seq.getObjectAt(i)); 79 DERObjectIdentifier oid = qc.getStatementId(); 80 if (oid != null) { 81 ret.add(oid.getId()); 82 } 83 } 84 return ret; 85 } 86 92 public static String getQcStatementValueLimit(X509Certificate cert) throws IOException { 93 String ret = null; 94 DERObject obj = getExtensionValue(cert, QCSTATEMENTS_OBJECTID); 95 if (obj == null) { 96 return null; 97 } 98 ASN1Sequence seq = (ASN1Sequence)obj; 99 MonetaryValue mv = null; 100 for (int i = 0; i < seq.size(); i++) { 102 QCStatement qc = QCStatement.getInstance(seq.getObjectAt(i)); 103 DERObjectIdentifier oid = qc.getStatementId(); 104 if (oid != null) { 105 if (oid.equals(ETSIQCObjectIdentifiers.id_etsi_qcs_LimiteValue)) { 106 ASN1Encodable enc = qc.getStatementInfo(); 108 if (enc != null) { 109 mv = MonetaryValue.getInstance(enc); 110 break; 112 } 113 } 114 } 115 } 116 if (mv != null) { 117 BigInteger amount = mv.getAmount(); 118 BigInteger exp = mv.getExponent(); 119 BigInteger ten = BigInteger.valueOf(10); 120 long value = amount.longValue() * (ten.pow(exp.intValue())).longValue(); 122 if (value < 0) { 123 log.error("ETSI LimitValue amount is < 0."); 124 } 125 String curr = mv.getCurrency().getAlphabetic(); 126 if (curr == null) { 127 log.error("ETSI LimitValue currency is null"); 128 } 129 if ( (value >= 0) && (curr != null) ) { 130 ret = value + " "+curr; 131 } 132 } 133 return ret; 134 135 } 136 142 public static String getQcStatementAuthorities(X509Certificate cert) throws IOException { 143 String ret = null; 144 DERObject obj = getExtensionValue(cert, QCSTATEMENTS_OBJECTID); 145 if (obj == null) { 146 return null; 147 } 148 ASN1Sequence seq = (ASN1Sequence)obj; 149 SemanticsInformation si = null; 150 for (int i = 0; i < seq.size(); i++) { 152 QCStatement qc = QCStatement.getInstance(seq.getObjectAt(i)); 153 DERObjectIdentifier oid = qc.getStatementId(); 154 if (oid != null) { 155 if (oid.equals(RFC3739QCObjectIdentifiers.id_qcs_pkixQCSyntax_v1) || oid.equals(RFC3739QCObjectIdentifiers.id_qcs_pkixQCSyntax_v2)) { 156 ASN1Encodable enc = qc.getStatementInfo(); 158 if (enc != null) { 159 si = SemanticsInformation.getInstance(enc); 160 break; 162 } 163 } 164 } 165 } 166 if (si != null) { 167 GeneralName[] gns = si.getNameRegistrationAuthorities(); 168 if (gns == null) { 169 return null; 170 } 171 StringBuffer strBuf = new StringBuffer (); 172 for (int i = 0; i < gns.length; i++) { 173 GeneralName gn = gns[i]; 174 if (strBuf.length() != 0) { 175 strBuf.append(", "); 177 } 178 String str = getGeneralNameString(gn.getTagNo(), gn.getName()); 179 if (str != null) { 180 strBuf.append(str); 181 } 182 } 183 if (strBuf.length() > 0) { 184 ret = strBuf.toString(); 185 } 186 } 187 return ret; 188 } 189 190 } 191 | Popular Tags |