001: /*
002: * Title: Oyster Project
003: * Description: S/MIME email sending capabilities
004: * @Author Vladimir Radisic
005: * @Version 2.1.6
006: */
007:
008: package org.enhydra.oyster.cms;
009:
010: import org.enhydra.oyster.exception.SMIMEException;
011: import org.enhydra.oyster.exception.ErrorStorage;
012: import org.enhydra.oyster.der.DERSequencePr;
013: import org.enhydra.oyster.der.DERObjectIdentifier;
014: import org.enhydra.oyster.util.DERLengthSearcher;
015: import org.enhydra.oyster.util.ByteArrayComparator;
016: import java.security.cert.X509Certificate;
017:
018: /**
019: * IssuerName class is DER encoded object represented in ASN.1 notation
020: * according to RFC2630. It is used for representing information about issuer
021: * of particular certificates. Detail information about ASN.1 notation of
022: * this class can be found in description of ASN.1 notation of IssuerAndSerialNumber.
023: */
024: public class IssuerName extends DERSequencePr {
025:
026: /**
027: * Container for DN (set of distinguished names)
028: */
029: private byte[] dNames;
030:
031: /**
032: * Enables/Disables function for particular adding of Relative Distinguished Name
033: */
034: private int enable = 0;
035:
036: /**
037: * Construction with information got from specific X509Certificate or from .cer
038: * file information which is extracted into instance of X509Certificate class
039: * @param cert0 X509Certificate
040: * @exception SMIMEException caused by non SMIMEException which is:
041: * CertificateEncodingException. Also, it can be thrown by super class
042: * constructor.
043: */
044: public IssuerName(X509Certificate cert0) throws SMIMEException {
045: byte[] tbs = null;
046: try {
047: tbs = cert0.getTBSCertificate();
048: } catch (Exception e) {
049: throw new SMIMEException(e);
050: }
051: dNames = findDNfromTBS(tbs);
052: }
053:
054: /**
055: * Finds area with Distinguish Names from TBS Certificate part of X509
056: * certificate, represented as byte array
057: * @param tbs0 TBS Certificate represented as byte array
058: * @return Distinguish name as byte array
059: */
060: private byte[] findDNfromTBS(byte[] tbs0) {
061: int start = 0; // first SEQUENCE tag in TBSCertificate
062: byte[] temp;
063: DERLengthSearcher len = new DERLengthSearcher(start, tbs0);
064: start = start + len.getLengthtDERLengthPart() + 1; // [0]
065: len.newInitialization(start, tbs0);
066: start = start + len.getLengthtDERLengthPart()
067: + len.getLengthtDERContentPart() + 1; // CertificateSerialNumber
068: len.newInitialization(start, tbs0);
069: start = start + len.getLengthtDERLengthPart()
070: + len.getLengthtDERContentPart() + 1; // Algorythm identifier - SEQUENCE
071: len.newInitialization(start, tbs0);
072: start = start + len.getLengthtDERLengthPart()
073: + len.getLengthtDERContentPart() + 1; // Issuer Name - SEQUENCE
074: len.newInitialization(start, tbs0);
075: start = start + len.getLengthtDERLengthPart() + 1;
076: int stop = start + len.getLengthtDERContentPart() - 1;
077: temp = new byte[stop - start + 1];
078: for (int i = start; i <= stop; i++)
079: temp[i - start] = tbs0[i];
080: return temp;
081: }
082:
083: /**
084: * Adds all Relative Distinguish Names from certificate to IssuerName
085: * @exception SMIMEException thrown from super class addContent method.
086: */
087: public void addAllRelativeDN() throws SMIMEException {
088: super .addContent(dNames);
089: enable = 1;
090: }
091:
092: /**
093: * Adds particular Relative Distinguish Name from certificate to IssuerName.
094: * This method can be called many times, but never if method
095: * addAllRelativeDN was called first
096: * @param id_at_type0 object identifier name of desired Particular Distinguish
097: * Name
098: * @return Desired Particular Distinguish Name as byte array
099: * @exception SMIMEException if method addAllRelativeDN was already performed.
100: * Also it can be caused by non SMIMEException which is:
101: * UnsupportedEncodingException.
102: */
103: public int addParticularRelativeDN(String id_at_type0)
104: throws SMIMEException {
105: if (enable == 1)
106: throw new SMIMEException(1021);
107: byte[] temp = new DERObjectIdentifier(id_at_type0,
108: "NAME_STRING").getDEREncoded();
109: ByteArrayComparator bcomp = new ByteArrayComparator(temp,
110: dNames);
111: int positionFirst = bcomp.getMatchingIndex();
112: if (positionFirst != -1) // Matching is founded
113: {
114: positionFirst = positionFirst + temp.length;
115: DERLengthSearcher len = new DERLengthSearcher(
116: positionFirst, dNames);
117: positionFirst = positionFirst
118: + len.getLengthtDERLengthPart() + 1;
119: int positionLast = positionFirst
120: + len.getLengthtDERContentPart() - 1;
121: byte[] name = new byte[positionLast - positionFirst + 1];
122: for (int i = positionFirst; i <= positionLast; i++)
123: // Finding a text of particular distinguish name
124: name[i - positionFirst] = dNames[i];
125: RelativeDistinguishedName rdn = null;
126: try {
127: rdn = new RelativeDistinguishedName(id_at_type0,
128: "NAME_STRING", new String(name, "ISO-8859-1"));
129: } catch (Exception e) {
130: throw new SMIMEException(e);
131: }
132: super .addContent(rdn.getDEREncoded());
133: return 0; // success of operation
134: } else
135: return -1; // failure of operation
136: }
137: }
|