use of com.github.zhenwei.core.asn1.pkcs.ContentInfo in project LinLong-Java by zhenwei1108.
the class CMSCompressedData method getContentStream.
public CMSTypedStream getContentStream(InputExpanderProvider expanderProvider) {
ContentInfo content = comData.getEncapContentInfo();
ASN1OctetString bytes = (ASN1OctetString) content.getContent();
InputExpander expander = expanderProvider.get(comData.getCompressionAlgorithmIdentifier());
InputStream zIn = expander.getInputStream(bytes.getOctetStream());
return new CMSTypedStream(content.getContentType(), zIn);
}
use of com.github.zhenwei.core.asn1.pkcs.ContentInfo in project LinLong-Java by zhenwei1108.
the class CMSEncryptedDataGenerator method doGenerate.
private CMSEncryptedData doGenerate(CMSTypedData content, OutputEncryptor contentEncryptor) throws CMSException {
AlgorithmIdentifier encAlgId;
ASN1OctetString encContent;
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
try {
OutputStream cOut = contentEncryptor.getOutputStream(bOut);
content.write(cOut);
cOut.close();
} catch (IOException e) {
throw new CMSException("");
}
byte[] encryptedContent = bOut.toByteArray();
encAlgId = contentEncryptor.getAlgorithmIdentifier();
encContent = new BEROctetString(encryptedContent);
EncryptedContentInfo eci = new EncryptedContentInfo(content.getContentType(), encAlgId, encContent);
ASN1Set unprotectedAttrSet = null;
if (unprotectedAttributeGenerator != null) {
AttributeTable attrTable = unprotectedAttributeGenerator.getAttributes(Collections.EMPTY_MAP);
unprotectedAttrSet = new BERSet(attrTable.toASN1EncodableVector());
}
ContentInfo contentInfo = new ContentInfo(CMSObjectIdentifiers.encryptedData, new EncryptedData(eci, unprotectedAttrSet));
return new CMSEncryptedData(contentInfo);
}
use of com.github.zhenwei.core.asn1.pkcs.ContentInfo in project LinLong-Java by zhenwei1108.
the class CMSCompressedDataStreamGenerator method open.
/**
* Open a compressing output stream.
*
* @param contentOID the content type OID.
* @param out the stream to encode to.
* @param compressor the type of compressor to use.
* @return an output stream to write the data be compressed to.
* @throws IOException
*/
public OutputStream open(ASN1ObjectIdentifier contentOID, OutputStream out, OutputCompressor compressor) throws IOException {
BERSequenceGenerator sGen = new BERSequenceGenerator(out);
sGen.addObject(CMSObjectIdentifiers.compressedData);
//
// Compressed Data
//
BERSequenceGenerator cGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);
cGen.addObject(new ASN1Integer(0));
//
// AlgorithmIdentifier
//
cGen.addObject(compressor.getAlgorithmIdentifier());
//
// Encapsulated ContentInfo
//
BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen.getRawOutputStream());
eiGen.addObject(contentOID);
OutputStream octetStream = CMSUtils.createBEROctetOutputStream(eiGen.getRawOutputStream(), 0, true, _bufferSize);
return new CmsCompressedOutputStream(compressor.getOutputStream(octetStream), sGen, cGen, eiGen);
}
use of com.github.zhenwei.core.asn1.pkcs.ContentInfo in project LinLong-Java by zhenwei1108.
the class CMSEnvelopedDataGenerator method doGenerate.
private CMSEnvelopedData doGenerate(CMSTypedData content, OutputEncryptor contentEncryptor) throws CMSException {
ASN1EncodableVector recipientInfos = new ASN1EncodableVector();
AlgorithmIdentifier encAlgId;
ASN1OctetString encContent;
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
try {
OutputStream cOut = contentEncryptor.getOutputStream(bOut);
content.write(cOut);
cOut.close();
if (contentEncryptor instanceof OutputAEADEncryptor) {
byte[] mac = ((OutputAEADEncryptor) contentEncryptor).getMAC();
bOut.write(mac, 0, mac.length);
}
} catch (IOException e) {
throw new CMSException("");
}
byte[] encryptedContent = bOut.toByteArray();
encAlgId = contentEncryptor.getAlgorithmIdentifier();
encContent = new BEROctetString(encryptedContent);
GenericKey encKey = contentEncryptor.getKey();
for (Iterator it = recipientInfoGenerators.iterator(); it.hasNext(); ) {
RecipientInfoGenerator recipient = (RecipientInfoGenerator) it.next();
recipientInfos.add(recipient.generate(encKey));
}
EncryptedContentInfo eci = new EncryptedContentInfo(content.getContentType(), encAlgId, encContent);
ASN1Set unprotectedAttrSet = null;
if (unprotectedAttributeGenerator != null) {
AttributeTable attrTable = unprotectedAttributeGenerator.getAttributes(Collections.EMPTY_MAP);
unprotectedAttrSet = new BERSet(attrTable.toASN1EncodableVector());
}
ContentInfo contentInfo = new ContentInfo(CMSObjectIdentifiers.envelopedData, new EnvelopedData(originatorInfo, new DERSet(recipientInfos), eci, unprotectedAttrSet));
return new CMSEnvelopedData(contentInfo);
}
use of com.github.zhenwei.core.asn1.pkcs.ContentInfo in project LinLong-Java by zhenwei1108.
the class CMSEnvelopedDataStreamGenerator method open.
protected OutputStream open(OutputStream out, ASN1EncodableVector recipientInfos, OutputEncryptor encryptor) throws CMSException {
try {
//
// ContentInfo
//
BERSequenceGenerator cGen = new BERSequenceGenerator(out);
cGen.addObject(CMSObjectIdentifiers.envelopedData);
//
// Encrypted Data
//
BERSequenceGenerator envGen = new BERSequenceGenerator(cGen.getRawOutputStream(), 0, true);
ASN1Set recipients;
if (_berEncodeRecipientSet) {
recipients = new BERSet(recipientInfos);
} else {
recipients = new DERSet(recipientInfos);
}
envGen.addObject(getVersion(recipientInfos));
if (originatorInfo != null) {
envGen.addObject(new DERTaggedObject(false, 0, originatorInfo));
}
envGen.getRawOutputStream().write(recipients.getEncoded());
BERSequenceGenerator eiGen = new BERSequenceGenerator(envGen.getRawOutputStream());
eiGen.addObject(CMSObjectIdentifiers.data);
AlgorithmIdentifier encAlgId = encryptor.getAlgorithmIdentifier();
eiGen.getRawOutputStream().write(encAlgId.getEncoded());
OutputStream octetStream = CMSUtils.createBEROctetOutputStream(eiGen.getRawOutputStream(), 0, false, _bufferSize);
return new CmsEnvelopedDataOutputStream(encryptor, octetStream, cGen, envGen, eiGen);
} catch (IOException e) {
throw new CMSException("exception decoding algorithm parameters.", e);
}
}
Aggregations