KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > security > cert > extensions > IssuerAltName


1
2 package com.ca.commons.security.cert.extensions;
3
4 import java.util.*;
5
6 import com.ca.commons.security.asn1.*;
7
8 /**
9  * <pre>
10  * IssuerAltName ::= GeneralNames
11  *
12  * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
13  *
14  * GeneralName ::= CHOICE {
15  * otherName [0] OtherName,
16  * rfc822Name [1] IA5String,
17  * dNSName [2] IA5String,
18  * x400Address [3] ORAddress,
19  * directoryName [4] Name,
20  * ediPartyName [5] EDIPartyName,
21  * uniformResourceIdentifier [6] IA5String,
22  * iPAddress [7] OCTET STRING,
23  * registeredID [8] OBJECT IDENTIFIER}
24  *
25  * OtherName ::= SEQUENCE {
26  * type-id OBJECT IDENTIFIER,
27  * value [0] EXPLICIT ANY DEFINED BY type-id }
28  *
29  * EDIPartyName ::= SEQUENCE {
30  * nameAssigner [0] DirectoryString OPTIONAL,
31  * partyName [1] DirectoryString }
32  * </pre>
33  *
34  * @author vbui
35  */

36 public class IssuerAltName implements V3Extension
37 {
38     String JavaDoc value = null;
39
40     public void init(ASN1Object asn1object) throws Exception JavaDoc
41     {
42         if (!asn1object.isASN1Type(ASN1Type.SEQUENCE))
43             throw new Exception JavaDoc("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 JavaDoc nextEntry = getGNameString(nextName);
50
51             if (value == null)
52                 value = nextEntry;
53             else
54                 value = value + "\n" + nextEntry;
55         }
56     }
57
58     // clean up a DN
59
public static String JavaDoc cleanName(String JavaDoc dn)
60     {
61         StringTokenizer tok = new StringTokenizer(dn, ",");
62         Vector toks = new Vector();
63         while (tok.hasMoreTokens())
64         {
65             String JavaDoc nextToken = tok.nextToken();
66             if (!nextToken.endsWith(" = null"))
67                 toks.addElement(nextToken);
68         }
69
70         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
71         for (int i = 0; i < toks.size(); i ++)
72         {
73             if (buff.toString().length() == 0)
74                 buff.append((String JavaDoc) toks.elementAt(i));
75             else
76                 buff.append(", " + toks.elementAt(i));
77         }
78
79         return buff.toString();
80     }
81
82     // convert a 4 bytes IP address to a more presentable format
83
private static String JavaDoc getIP(String JavaDoc ip)
84     {
85         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
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 JavaDoc getGNameString(ASN1Object nextName)
97     {
98         GeneralName gname = new GeneralName(nextName);
99         int type = gname.getType();
100         Object JavaDoc valuee = gname.getValue(type);
101
102         String JavaDoc nextEntry = null;
103         if (type >= 0 && valuee != null)
104         {
105             String JavaDoc value = null;
106             if (type == 7)
107             { // ipAddress
108
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 JavaDoc toString()
125     {
126         return value;
127     }
128 }
129
130
Popular Tags