List of usage examples for org.bouncycastle.cms SignerInfoGenerator generate
public SignerInfo generate(ASN1ObjectIdentifier contentType) throws CMSException
From source file:net.jsign.asn1.authenticode.AuthenticodeSignedDataGenerator.java
License:Apache License
public CMSSignedData generate(ASN1ObjectIdentifier contentTypeOID, ASN1Encodable content) throws CMSException, IOException { digests.clear();//from w w w . j a va2s . co m SignerInfo signerInfo; if (!_signers.isEmpty()) { signerInfo = ((SignerInformation) _signers.get(0)).toASN1Structure(); } else { SignerInfoGenerator signerInfoGenerator = (SignerInfoGenerator) signerGens.get(0); byte[] signedContent = content.toASN1Primitive().getEncoded("DER"); OutputStream out = signerInfoGenerator.getCalculatingOutputStream(); out.write(signedContent, 2, signedContent.length - 2); // skip the first 2 bytes as specified out.flush(); out.close(); signerInfo = signerInfoGenerator.generate(contentTypeOID); byte[] calculatedDigest = signerInfoGenerator.getCalculatedDigest(); digests.put(signerInfoGenerator.getDigestAlgorithm().getAlgorithm().getId(), calculatedDigest); } ContentInfo encInfo = new ContentInfo(contentTypeOID, content); ASN1Set certificates = new DERSet((ASN1Encodable[]) certs.toArray(new ASN1Encodable[0])); ASN1Encodable signedData = new AuthenticodeSignedData(signerInfo.getDigestAlgorithm(), encInfo, certificates, signerInfo); ContentInfo contentInfo = new ContentInfo(CMSObjectIdentifiers.signedData, signedData); return new CMSSignedData( new CMSProcessableByteArray(contentTypeOID, content.toASN1Primitive().getEncoded("DER")), contentInfo); }
From source file:org.votingsystem.signature.util.PDFContentSigner.java
License:Open Source License
public CMSSignedData genSignedData(byte[] signatureHash, CMSAttributeTableGenerator unsAttr) throws Exception { CMSProcessable content = new CMSProcessableByteArray(signatureHash); ByteArrayOutputStream out = null; if (content != null) { out = new ByteArrayOutputStream(); content.write(out);//from w w w.j av a 2 s .c o m out.close(); } ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray()); MessageDigest softwareDigestEngine = MessageDigest.getInstance(signatureDigestAlg); int bytesRead; byte[] dataBuffer = new byte[4096]; while ((bytesRead = bais.read(dataBuffer)) >= 0) { softwareDigestEngine.update(dataBuffer, 0, bytesRead); } byte[] hash = softwareDigestEngine.digest(); CertStore certsAndCRLs = CertStore.getInstance(CERT_STORE_TYPE, new CollectionCertStoreParameters(Arrays.asList(signerCertChain)), ContextVS.PROVIDER); addCertificatesAndCRLs(certsAndCRLs); CMSAttributeTableGenerator sAttr = new DefaultSignedAttributeTableGenerator(); ASN1ObjectIdentifier contentTypeOID = new ASN1ObjectIdentifier(CMSSignedGenerator.DATA); Map parameters = getBaseParameters(contentTypeOID, new AlgorithmIdentifier(new DERObjectIdentifier(pdfDigestObjectIdentifier), new DERNull()), hash); AttributeTable attributeTable = sAttr.getAttributes(Collections.unmodifiableMap(parameters)); //String signatureHashStr = new String(Base64.encode(signatureHash)); JcaSimpleSignerInfoGeneratorBuilder jcaSignerInfoGeneratorBuilder = new JcaSimpleSignerInfoGeneratorBuilder(); jcaSignerInfoGeneratorBuilder = jcaSignerInfoGeneratorBuilder.setProvider(ContextVS.PROVIDER); jcaSignerInfoGeneratorBuilder.setSignedAttributeGenerator(attributeTable); jcaSignerInfoGeneratorBuilder.setUnsignedAttributeGenerator(unsAttr); SignerInfoGenerator signerInfoGenerator = jcaSignerInfoGeneratorBuilder.build(signatureMechanism, privateKey, userCert); SignerInfo signerInfo = signerInfoGenerator.generate(contentTypeOID); List<SignerInfo> signerInfoList = new ArrayList<SignerInfo>(); signerInfoList.add(signerInfo); log.info(" -- userCert: " + userCert.getSubjectDN().getName()); CMSSignedData signedData = getCMSSignedData(CMSSignedGenerator.DATA, content, true, CMSUtils.getProvider("BC"), true, signerInfoList); return signedData; }