use of com.github.zhenwei.pkix.util.asn1.cms.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.pkix.util.asn1.cms.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.pkix.util.asn1.cms.ContentInfo in project gdmatrix by gdmatrix.
the class P7MDocument method getSignatures.
public List<P7MSignature> getSignatures() throws Exception {
ArrayList<P7MSignature> signatures = new ArrayList();
// CertStore certStore = cms.getCertificatesAndCRLs("Collection", "BC");
Store certStore = cms.getCertificates();
SignerInformationStore siStore = cms.getSignerInfos();
Collection signers = siStore.getSigners();
for (Object elem : signers) {
SignerInformation signer = (SignerInformation) elem;
P7MSignature signature = new P7MSignature();
signatures.add(signature);
Collection certCollection = certStore.getMatches(signer.getSID());
// Collection certCollection = certStore.getCertificates(certSelector);
X509CertificateHolder certificateHolder = (X509CertificateHolder) certCollection.iterator().next();
X509Certificate certificate = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certificateHolder);
signature.setCertificate(certificate);
signature.loadProperties();
signature.setSignature(Base64.getMimeEncoder().encodeToString(signer.getSignature()).toUpperCase());
// **** signed attributes ****
AttributeTable table = signer.getSignedAttributes();
Hashtable attributes = table.toHashtable();
// signingTime
Attribute attrib = (Attribute) attributes.get(new ASN1ObjectIdentifier("1.2.840.113549.1.9.5"));
if (attrib != null) {
ASN1UTCTime time = (ASN1UTCTime) attrib.getAttrValues().getObjectAt(0);
String timeString = time.getAdjustedTime();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss'GMT+'00:00");
signature.setSigningDate(df.parse(timeString));
}
// filename
DEROctetString octet;
attrib = (Attribute) attributes.get(new ASN1ObjectIdentifier("1.3.6.1.4.1.311.88.2.1"));
if (attrib != null) {
octet = (DEROctetString) attrib.getAttrValues().getObjectAt(0);
if (octet != null) {
signature.setFilename(new String(octet.getOctets(), "UTF-16LE"));
}
}
// decretNumber
attrib = (Attribute) attributes.get(new ASN1ObjectIdentifier("1.3.6.1.4.1.311.88.2.2"));
if (attrib != null) {
octet = (DEROctetString) attrib.getAttrValues().getObjectAt(0);
if (octet != null) {
signature.setDecretNumber(new String(octet.getOctets(), "UTF-16LE"));
}
}
// **** unsigned attributes ****
table = signer.getUnsignedAttributes();
if (table != null) {
attributes = table.toHashtable();
// timeStampToken
attrib = (Attribute) attributes.get(new ASN1ObjectIdentifier("1.2.840.113549.1.9.16.2.14"));
if (attrib != null) {
ASN1Sequence seq = (ASN1Sequence) attrib.getAttrValues().getObjectAt(0);
ContentInfo timeStampToken = ContentInfo.getInstance(seq);
SignedData sd = SignedData.getInstance(timeStampToken.getContent());
ASN1Set certificates = sd.getCertificates();
ASN1Primitive derCert = certificates.getObjectAt(0).toASN1Primitive();
byte[] certBytes = derCert.getEncoded();
CertificateFactory certFactory = CertificateFactory.getInstance("X509");
X509Certificate tsCertificate = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(certBytes));
signature.setTimeStampCertificate(tsCertificate);
ASN1Encodable content = sd.getEncapContentInfo().getContent();
// TSTInfo tstInfo = new TSTInfo((ASN1Sequence)
// new ASN1InputStream(((ASN1OctetString)content).getOctets()).readObject());
TSTInfo tstInfo = TSTInfo.getInstance(((ASN1OctetString) content).getOctets());
signature.setTimeStampDate(tstInfo.getGenTime().getDate());
}
}
// signature validation
signature.setValid(signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider("BC").build(signature.getCertificate())));
}
Collections.sort(signatures);
return signatures;
}
use of com.github.zhenwei.pkix.util.asn1.cms.ContentInfo in project gdmatrix by gdmatrix.
the class P7MUtils method addTimeStamp.
public static CMSSignedData addTimeStamp(String serviceURI, CMSSignedData signedData) throws Exception {
CMSSignedData newSignedData;
SignerInformationStore sigStore = signedData.getSignerInfos();
ArrayList siList = new ArrayList();
for (Object o : sigStore.getSigners()) {
// CertStore certStore =
// signedData.getCertificatesAndCRLs("Collection", "BC");
Store store = signedData.getCertificates();
SignerInformation si = (SignerInformation) o;
SignerId sigId = si.getSID();
JcaX509CertSelectorConverter converter = new JcaX509CertSelectorConverter();
CertSelector certSelector = converter.getCertSelector(sigId);
Collection certCollection = store.getMatches((Selector) certSelector);
// Collection certCollection = certStore.getCertificates(sigId);
X509Certificate certificate = (X509Certificate) certCollection.iterator().next();
System.out.println(certificate.getSubjectDN().getName());
// get signature
byte[] signature = si.getSignature();
// signed attributes
System.out.println("SignedAttributes:");
AttributeTable signedAttributes = si.getSignedAttributes();
printAttributeTable(signedAttributes);
// unsigned attributes
System.out.println("UnsignedAttributes:");
AttributeTable unsignedAttributes = si.getUnsignedAttributes();
printAttributeTable(unsignedAttributes);
ASN1ObjectIdentifier tsId = new ASN1ObjectIdentifier("1.2.840.113549.1.9.16.2.14");
Attribute att = unsignedAttributes == null ? null : unsignedAttributes.get(tsId);
if (att == null) {
System.out.println("creating timeStamp...");
ASN1EncodableVector tsVector = new ASN1EncodableVector();
ContentInfo timeStampToken = createTimeStamp(serviceURI, signature);
tsVector.add(timeStampToken);
DERSet attributeValues = new DERSet(tsVector);
att = new Attribute(tsId, attributeValues);
Hashtable attrMap = new Hashtable();
attrMap.put(tsId, att);
AttributeTable table = new AttributeTable(attrMap);
SignerInformation newSi = SignerInformation.replaceUnsignedAttributes(si, table);
siList.add(newSi);
} else {
System.out.println("timeStamp present");
}
}
if (// replace signers
!siList.isEmpty()) {
newSignedData = CMSSignedData.replaceSigners(signedData, new SignerInformationStore(siList));
newSignedData = new CMSSignedData(newSignedData.getEncoded());
} else
newSignedData = signedData;
return newSignedData;
}
use of com.github.zhenwei.pkix.util.asn1.cms.ContentInfo in project gdmatrix by gdmatrix.
the class P7MUtils method sendData.
private static TimeStampResp sendData(InputStream dataToBeSent, String serviceURI) throws Exception {
URL url = new URL(serviceURI);
URLConnection conn = url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
// post request data
OutputStream os = conn.getOutputStream();
byte[] buffer = new byte[4096];
int numRead = dataToBeSent.read(buffer);
while (numRead > 0) {
os.write(buffer, 0, numRead);
numRead = dataToBeSent.read(buffer);
}
os.flush();
// read response
InputStream response = conn.getInputStream();
ASN1InputStream asn1Is = new ASN1InputStream(response);
Enumeration e = ((ASN1Sequence) asn1Is.readObject()).getObjects();
PKIStatusInfo pkiStatusInfo = PKIStatusInfo.getInstance(e.nextElement());
ContentInfo timeStampToken = null;
if (e.hasMoreElements()) {
timeStampToken = ContentInfo.getInstance(e.nextElement());
}
TimeStampResp tspResp = new TimeStampResp(pkiStatusInfo, timeStampToken);
return tspResp;
}
Aggregations