use of com.github.zhenwei.core.asn1.ASN1SequenceParser in project LinLong-Java by zhenwei1108.
the class AuthenticatedDataParser method getOriginatorInfo.
public OriginatorInfo getOriginatorInfo() throws IOException {
originatorInfoCalled = true;
if (nextObject == null) {
nextObject = seq.readObject();
}
if (nextObject instanceof ASN1TaggedObjectParser) {
ASN1TaggedObjectParser o = (ASN1TaggedObjectParser) nextObject;
if (o.hasContextTag(0)) {
ASN1SequenceParser originatorInfo = (ASN1SequenceParser) o.parseBaseUniversal(false, BERTags.SEQUENCE);
nextObject = null;
return OriginatorInfo.getInstance(originatorInfo.getLoadedObject());
}
}
return null;
}
use of com.github.zhenwei.core.asn1.ASN1SequenceParser in project LinLong-Java by zhenwei1108.
the class EnvelopedDataParser method getEncryptedContentInfo.
public EncryptedContentInfoParser getEncryptedContentInfo() throws IOException {
if (_nextObject == null) {
_nextObject = _seq.readObject();
}
if (_nextObject != null) {
ASN1SequenceParser o = (ASN1SequenceParser) _nextObject;
_nextObject = null;
return new EncryptedContentInfoParser(o);
}
return null;
}
use of com.github.zhenwei.core.asn1.ASN1SequenceParser in project LinLong-Java by zhenwei1108.
the class CMSSignedDataParser method replaceCertificatesAndCRLs.
/**
* Replace the certificate and CRL information associated with this CMSSignedData object with the
* new one passed in.
* <p>
* The output stream is returned unclosed.
* </p>
*
* @param original the signed data stream to be used as a base.
* @param certs new certificates to be used, if any.
* @param crls new CRLs to be used, if any.
* @param attrCerts new attribute certificates to be used, if any.
* @param out the stream to write the new signed data object to.
* @return out.
* @throws CMSException if there is an error processing the CertStore
*/
public static OutputStream replaceCertificatesAndCRLs(InputStream original, Store certs, Store crls, Store attrCerts, OutputStream out) throws CMSException, IOException {
ASN1StreamParser in = new ASN1StreamParser(original);
ContentInfoParser contentInfo = new ContentInfoParser((ASN1SequenceParser) in.readObject());
SignedDataParser signedData = SignedDataParser.getInstance(contentInfo.getContent(BERTags.SEQUENCE));
BERSequenceGenerator sGen = new BERSequenceGenerator(out);
sGen.addObject(CMSObjectIdentifiers.signedData);
BERSequenceGenerator sigGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);
// version number
sigGen.addObject(signedData.getVersion());
// digests
sigGen.getRawOutputStream().write(signedData.getDigestAlgorithms().toASN1Primitive().getEncoded());
// encap content info
ContentInfoParser encapContentInfo = signedData.getEncapContentInfo();
BERSequenceGenerator eiGen = new BERSequenceGenerator(sigGen.getRawOutputStream());
eiGen.addObject(encapContentInfo.getContentType());
pipeEncapsulatedOctetString(encapContentInfo, eiGen.getRawOutputStream());
eiGen.close();
//
// skip existing certs and CRLs
//
getASN1Set(signedData.getCertificates());
getASN1Set(signedData.getCrls());
//
if (certs != null || attrCerts != null) {
List certificates = new ArrayList();
if (certs != null) {
certificates.addAll(CMSUtils.getCertificatesFromStore(certs));
}
if (attrCerts != null) {
certificates.addAll(CMSUtils.getAttributeCertificatesFromStore(attrCerts));
}
ASN1Set asn1Certs = CMSUtils.createBerSetFromList(certificates);
if (asn1Certs.size() > 0) {
sigGen.getRawOutputStream().write(new DERTaggedObject(false, 0, asn1Certs).getEncoded());
}
}
if (crls != null) {
ASN1Set asn1Crls = CMSUtils.createBerSetFromList(CMSUtils.getCRLsFromStore(crls));
if (asn1Crls.size() > 0) {
sigGen.getRawOutputStream().write(new DERTaggedObject(false, 1, asn1Crls).getEncoded());
}
}
sigGen.getRawOutputStream().write(signedData.getSignerInfos().toASN1Primitive().getEncoded());
sigGen.close();
sGen.close();
return out;
}
use of com.github.zhenwei.core.asn1.ASN1SequenceParser in project LinLong-Java by zhenwei1108.
the class CMSSignedDataParser method replaceSigners.
/**
* Replace the signerinformation store associated with the passed in message contained in the
* stream original with the new one passed in. You would probably only want to do this if you
* wanted to change the unsigned attributes associated with a signer, or perhaps delete one.
* <p>
* The output stream is returned unclosed.
* </p>
*
* @param original the signed data stream to be used as a base.
* @param signerInformationStore the new signer information store to use.
* @param out the stream to write the new signed data object to.
* @return out.
*/
public static OutputStream replaceSigners(InputStream original, SignerInformationStore signerInformationStore, OutputStream out) throws CMSException, IOException {
ASN1StreamParser in = new ASN1StreamParser(original);
ContentInfoParser contentInfo = new ContentInfoParser((ASN1SequenceParser) in.readObject());
SignedDataParser signedData = SignedDataParser.getInstance(contentInfo.getContent(BERTags.SEQUENCE));
BERSequenceGenerator sGen = new BERSequenceGenerator(out);
sGen.addObject(CMSObjectIdentifiers.signedData);
BERSequenceGenerator sigGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);
// version number
sigGen.addObject(signedData.getVersion());
// digests
// skip old ones
signedData.getDigestAlgorithms().toASN1Primitive();
ASN1EncodableVector digestAlgs = new ASN1EncodableVector();
for (Iterator it = signerInformationStore.getSigners().iterator(); it.hasNext(); ) {
SignerInformation signer = (SignerInformation) it.next();
digestAlgs.add(CMSSignedHelper.INSTANCE.fixDigestAlgID(signer.getDigestAlgorithmID(), dgstAlgFinder));
}
sigGen.getRawOutputStream().write(new DERSet(digestAlgs).getEncoded());
// encap content info
ContentInfoParser encapContentInfo = signedData.getEncapContentInfo();
BERSequenceGenerator eiGen = new BERSequenceGenerator(sigGen.getRawOutputStream());
eiGen.addObject(encapContentInfo.getContentType());
pipeEncapsulatedOctetString(encapContentInfo, eiGen.getRawOutputStream());
eiGen.close();
writeSetToGeneratorTagged(sigGen, signedData.getCertificates(), 0);
writeSetToGeneratorTagged(sigGen, signedData.getCrls(), 1);
ASN1EncodableVector signerInfos = new ASN1EncodableVector();
for (Iterator it = signerInformationStore.getSigners().iterator(); it.hasNext(); ) {
SignerInformation signer = (SignerInformation) it.next();
signerInfos.add(signer.toASN1Structure());
}
sigGen.getRawOutputStream().write(new DERSet(signerInfos).getEncoded());
sigGen.close();
sGen.close();
return out;
}
use of com.github.zhenwei.core.asn1.ASN1SequenceParser in project LinLong-Java by zhenwei1108.
the class CMSEnvelopedDataParser method getUnprotectedAttributes.
/**
* return a table of the unprotected attributes indexed by the OID of the attribute.
*
* @throws IOException
*/
public AttributeTable getUnprotectedAttributes() throws IOException {
if (unprotectedAttributes == null && attrNotRead) {
ASN1SetParser set = envelopedData.getUnprotectedAttrs();
attrNotRead = false;
if (set != null) {
ASN1EncodableVector v = new ASN1EncodableVector();
ASN1Encodable o;
while ((o = set.readObject()) != null) {
ASN1SequenceParser seq = (ASN1SequenceParser) o;
v.add(seq.toASN1Primitive());
}
unprotectedAttributes = new AttributeTable(new DERSet(v));
}
}
return unprotectedAttributes;
}
Aggregations