KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > cms > IssuerName


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email sending capabilities
4  * @Author Vladimir Radisic
5  * @Version 2.1.5
6  */

7
8
9 package org.enhydra.oyster.cms;
10
11 import org.enhydra.oyster.exception.SMIMEException;
12 import org.enhydra.oyster.exception.ErrorStorage;
13 import org.enhydra.oyster.der.DERSequencePr;
14 import org.enhydra.oyster.der.DERObjectIdentifier;
15 import org.enhydra.oyster.util.DERLengthSearcher;
16 import org.enhydra.oyster.util.ByteArrayComparator;
17 import java.security.cert.X509Certificate JavaDoc;
18
19
20 /**
21  * IssuerName class is DER encoded object represented in ASN.1 notation
22  * according to RFC2630. It is used for representing information about issuer
23  * of particular certificates. Detail information about ASN.1 notation of
24  * this class can be found in description of ASN.1 notation of IssuerAndSerialNumber.
25  */

26 public class IssuerName extends DERSequencePr {
27
28 /**
29  * Container for DN (set of distinguished names)
30  */

31   private byte[] dNames;
32
33 /**
34  * Enables/Disables function for particular adding of Relative Distinguished Name
35  */

36   private int enable = 0;
37
38 /**
39  * Construction with information got from specific X509Certificate or from .cer
40  * file information which is extracted into instance of X509Certificate class
41  * @param cert0 X509Certificate
42  * @exception SMIMEException caused by non SMIMEException which is:
43  * CertificateEncodingException. Also, it can be thrown by super class
44  * constructor.
45  */

46   public IssuerName (X509Certificate JavaDoc cert0) throws SMIMEException
47   {
48     byte[] tbs = null;
49     try {
50       tbs = cert0.getTBSCertificate();
51     }
52     catch(Exception JavaDoc e) {
53       throw SMIMEException.getInstance(this, e, "constructor" );
54     }
55     dNames = findDNfromTBS(tbs);
56   }
57
58 /**
59  * Finds area with Distinguish Names from TBS Certificate part of X509
60  * certificate, represented as byte array
61  * @param tbs0 TBS Certificate represented as byte array
62  * @return Distinguish name as byte array
63  */

64   private byte[] findDNfromTBS (byte[] tbs0) {
65     int start = 0; // first SEQUENCE tag in TBSCertificate
66
byte[] temp;
67     DERLengthSearcher len = new DERLengthSearcher(start, tbs0);
68     start = start + len.getLengthtDERLengthPart() + 1; // [0]
69
len.newInitialization(start, tbs0);
70     start = start + len.getLengthtDERLengthPart() + len.getLengthtDERContentPart() + 1; // CertificateSerialNumber
71
len.newInitialization(start, tbs0);
72     start = start + len.getLengthtDERLengthPart() + len.getLengthtDERContentPart() + 1; // Algorythm identifier - SEQUENCE
73
len.newInitialization(start, tbs0);
74     start = start + len.getLengthtDERLengthPart() + len.getLengthtDERContentPart() + 1; // Issuer Name - SEQUENCE
75
len.newInitialization(start, tbs0);
76     start = start + len.getLengthtDERLengthPart() + 1;
77     int stop = start + len.getLengthtDERContentPart() - 1;
78     temp = new byte[stop - start + 1];
79     for (int i = start; i <= stop; i++)
80       temp[i - start] = tbs0[i];
81     return temp;
82   }
83
84 /**
85  * Adds all Relative Distinguish Names from certificate to IssuerName
86  * @exception SMIMEException thrown from super class addContent method.
87  */

88   public void addAllRelativeDN () throws SMIMEException {
89     super.addContent(dNames);
90     enable = 1;
91   }
92
93 /**
94  * Adds particular Relative Distinguish Name from certificate to IssuerName.
95  * This method can be called many times, but never if method
96  * addAllRelativeDN was called first
97  * @param id_at_type0 object identifier name of desired Particular Distinguish
98  * Name
99  * @return Desired Particular Distinguish Name as byte array
100  * @exception SMIMEException if method addAllRelativeDN was already performed.
101  * Also it can be caused by non SMIMEException which is:
102  * UnsupportedEncodingException.
103  */

104   public int addParticularRelativeDN (String JavaDoc id_at_type0) throws SMIMEException {
105     if (enable == 1)
106       throw new SMIMEException(this, 1021);
107     byte[] temp = new DERObjectIdentifier(id_at_type0, "NAME_STRING").getDEREncoded();
108     ByteArrayComparator bcomp = new ByteArrayComparator(temp, dNames);
109     int positionFirst = bcomp.getMatchingIndex();
110     if (positionFirst != -1) // Matching is founded
111
{
112       positionFirst = positionFirst + temp.length;
113       DERLengthSearcher len = new DERLengthSearcher(positionFirst, dNames);
114       positionFirst = positionFirst + len.getLengthtDERLengthPart() + 1;
115       int positionLast = positionFirst + len.getLengthtDERContentPart() - 1;
116       byte[] name = new byte[positionLast - positionFirst + 1];
117       for (int i = positionFirst; i <= positionLast; i++) // Finding a text of particular distinguish name
118
name[i - positionFirst] = dNames[i];
119       RelativeDistinguishedName rdn = null;
120       try {
121         rdn = new RelativeDistinguishedName(id_at_type0, "NAME_STRING", new String JavaDoc(name, "ISO-8859-1"));
122       }
123       catch(Exception JavaDoc e) {
124         throw SMIMEException.getInstance(this, e, "addParticularRelativeDN" );
125       }
126       super.addContent(rdn.getDEREncoded());
127       return 0; // success of operation
128
}
129     else
130       return -1; // failure of operation
131
}
132 }
133
134
135
136
Popular Tags