1 2 package com.ca.commons.security.cert.extensions; 3 4 import java.util.*; 5 6 import com.ca.commons.security.asn1.*; 7 8 36 public class IssuerAltName implements V3Extension 37 { 38 String value = null; 39 40 public void init(ASN1Object asn1object) throws Exception 41 { 42 if (!asn1object.isASN1Type(ASN1Type.SEQUENCE)) 43 throw new Exception ("Wrong ASN.1 type for *AltName"); 44 45 for (int i = 0; i < asn1object.size(); i++) 46 { 47 ASN1Object nextName = (ASN1Object) asn1object.getComponent(i); 48 49 String nextEntry = getGNameString(nextName); 50 51 if (value == null) 52 value = nextEntry; 53 else 54 value = value + "\n" + nextEntry; 55 } 56 } 57 58 public static String cleanName(String dn) 60 { 61 StringTokenizer tok = new StringTokenizer(dn, ","); 62 Vector toks = new Vector(); 63 while (tok.hasMoreTokens()) 64 { 65 String nextToken = tok.nextToken(); 66 if (!nextToken.endsWith(" = null")) 67 toks.addElement(nextToken); 68 } 69 70 StringBuffer buff = new StringBuffer (); 71 for (int i = 0; i < toks.size(); i ++) 72 { 73 if (buff.toString().length() == 0) 74 buff.append((String ) toks.elementAt(i)); 75 else 76 buff.append(", " + toks.elementAt(i)); 77 } 78 79 return buff.toString(); 80 } 81 82 private static String getIP(String ip) 84 { 85 StringBuffer buff = new StringBuffer (); 86 for (int i = 0; i < ip.length(); i++) 87 { 88 buff.append((int) ip.charAt(i) + "."); 89 } 90 if (buff.toString().endsWith(".")) 91 buff.deleteCharAt(buff.length() - 1); 92 93 return buff.toString(); 94 } 95 96 public static String getGNameString(ASN1Object nextName) 97 { 98 GeneralName gname = new GeneralName(nextName); 99 int type = gname.getType(); 100 Object valuee = gname.getValue(type); 101 102 String nextEntry = null; 103 if (type >= 0 && valuee != null) 104 { 105 String value = null; 106 if (type == 7) 107 { value = getIP(valuee.toString()); 109 } 110 else 111 { 112 value = cleanName(valuee.toString()); 113 } 114 nextEntry = GeneralName.lookUpName(type) + ": " + value; 115 } 116 else 117 { 118 nextEntry = "Unrecognised GeneralName type: " + type; 119 } 120 121 return nextEntry; 122 } 123 124 public String toString() 125 { 126 return value; 127 } 128 } 129 130 | Popular Tags |