1 7 8 9 package org.enhydra.oyster.activation; 10 11 import org.enhydra.oyster.crypto.SymmetricEncryption; 12 import org.enhydra.oyster.cms.*; 13 import org.enhydra.oyster.util.MimeAssist; 14 import org.enhydra.oyster.util.MimeAssist; 15 import org.enhydra.oyster.exception.SMIMEException; 16 import org.enhydra.oyster.exception.SMIMEIOException; 17 import javax.mail.internet.MimeMessage ; 18 import java.security.cert.X509Certificate ; 19 import javax.activation.DataSource ; 20 import java.io.*; 21 22 29 public class CMSEnvelopedDataSource implements DataSource { 30 31 34 private EncryptedContentInfo encContInf; 35 36 39 private RecipientInfos recInf; 40 41 53 public CMSEnvelopedDataSource (byte[] message0, String algType0, int keyLength0) throws SMIMEException 54 { 55 encContInf = new EncryptedContentInfo(); 56 SymmetricEncryption symEnc = new SymmetricEncryption(algType0, keyLength0); symEnc.encrypt(message0); Content encryptedContent = new Content(symEnc.getEncryptedValue(), false); AlgorithmIdentifier contentEncryptAlgID; 60 if (algType0.equalsIgnoreCase("RC2_CBC")) { 62 contentEncryptAlgID = new AlgorithmIdentifier("RC2_CBC", "NAME_STRING"); 63 RC2CBCParameter par = new RC2CBCParameter(symEnc.getKeyLength(), symEnc.getIV()); 64 contentEncryptAlgID.addParamToAlgorithmId(par.getDEREncoded()); } 66 else if (algType0.equalsIgnoreCase("DES_EDE3_CBC")) { 68 contentEncryptAlgID = new AlgorithmIdentifier("DES_EDE3_CBC", "NAME_STRING"); 69 DESede3CBCParameter par = new DESede3CBCParameter(symEnc.getIV()); 70 contentEncryptAlgID.addParamToAlgorithmId(par.getDEREncoded()); } 72 else if (algType0.equalsIgnoreCase("DES")) { 74 contentEncryptAlgID = new AlgorithmIdentifier("DES", "NAME_STRING"); 75 DESede3CBCParameter par = new DESede3CBCParameter(symEnc.getIV()); contentEncryptAlgID.addParamToAlgorithmId(par.getDEREncoded()); } 78 else 79 throw new SMIMEException(this, 1013); 80 ContentTypeIdentifier contentData = new ContentTypeIdentifier("ID_DATA", "NAME_STRING"); encContInf.addContentType(contentData.getDEREncoded()); 82 encContInf.addEncryptAlgorithmID(contentEncryptAlgID.getDEREncoded()); 83 encContInf.addEncryptContent(encryptedContent.getDEREncoded()); 84 recInf = new RecipientInfos(symEnc.getSymmetricKey()); } 86 87 101 public CMSEnvelopedDataSource (MimeMessage message0, String algType0, int keyLength0) throws SMIMEException 102 { 103 this(MimeAssist.messageConvertor(message0), algType0, keyLength0); 104 } 105 106 112 public void addRecipient (X509Certificate cert0) throws SMIMEException { 113 recInf.addRecipient(cert0); 114 } 115 116 123 public byte[] getCMSEnvelopedObject () throws SMIMEException { 124 ContentTypeIdentifier contentTypeEnvelopDataId = new ContentTypeIdentifier("ID_ENVELOPEDDATA", "NAME_STRING"); EnvelopedData envData = new EnvelopedData(); envData.addCMSVersion(new CMSVersion(0).getDEREncoded()); 127 envData.addRecipientInfos(recInf.getDEREncoded()); 128 envData.addEncryptContentInfo(encContInf.getDEREncoded()); 129 Content cont = new Content(envData.getDEREncoded(), true); ContentInfo cmsObjectEnvelopedData = new ContentInfo(); 131 cmsObjectEnvelopedData.addContentType(contentTypeEnvelopDataId.getDEREncoded()); 132 cmsObjectEnvelopedData.addContent(cont.getDEREncoded()); 133 return cmsObjectEnvelopedData.getDEREncoded(); 134 } 135 136 146 public byte[] getBASE64CMSEnvelopedObject () throws SMIMEException { 147 return MimeAssist.getBASE64WithBreakOn76(this.getCMSEnvelopedObject()); 148 } 149 150 154 public String getContentType () { 155 return "application/x-pkcs7-mime; smime-type=enveloped-data; name=\"smime.p7m\""; 157 } 158 159 164 public InputStream getInputStream () throws SMIMEIOException { 165 try { 166 return new ByteArrayInputStream(getCMSEnvelopedObject()); 167 } catch (SMIMEException e) { 168 throw new SMIMEIOException(e); 169 } 170 } 171 172 176 public String getName () { 177 return "EnvelopedDataContentInfo"; 178 } 179 180 186 public OutputStream getOutputStream () throws IOException { 187 throw new IOException("EnvelopedDataContentInfo does not support getOutputStream()"); 188 } 189 } 190 191 192 193 | Popular Tags |