use of org.bouncycastle.cms.CMSSignedData in project walle by Meituan-Dianping.
the class V1SchemeSigner method generateSignatureBlock.
private static byte[] generateSignatureBlock(SignerConfig signerConfig, byte[] signatureFileBytes) throws InvalidKeyException, CertificateEncodingException, SignatureException {
JcaCertStore certs = new JcaCertStore(signerConfig.certificates);
X509Certificate signerCert = signerConfig.certificates.get(0);
String jcaSignatureAlgorithm = getJcaSignatureAlgorithm(signerCert.getPublicKey(), signerConfig.signatureDigestAlgorithm);
try {
ContentSigner signer = new JcaContentSignerBuilder(jcaSignatureAlgorithm).build(signerConfig.privateKey);
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
gen.addSignerInfoGenerator(new SignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().build(), SignerInfoSignatureAlgorithmFinder.INSTANCE).setDirectSignature(true).build(signer, new JcaX509CertificateHolder(signerCert)));
gen.addCertificates(certs);
CMSSignedData sigData = gen.generate(new CMSProcessableByteArray(signatureFileBytes), false);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded())) {
DEROutputStream dos = new DEROutputStream(out);
dos.writeObject(asn1.readObject());
}
return out.toByteArray();
} catch (OperatorCreationException | CMSException | IOException e) {
throw new SignatureException("Failed to generate signature", e);
}
}
use of org.bouncycastle.cms.CMSSignedData in project serverless by bluenimble.
the class SignDocument method main.
public static void main(String[] args) throws IOException, CertificateException, UnrecoverableKeyException, KeyStoreException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, CertStoreException, CMSException, OperatorCreationException {
File toBeSigned = new File("ToBeSigned.txt");
byte[] buffer = new byte[(int) toBeSigned.length()];
DataInputStream in = new DataInputStream(new FileInputStream(toBeSigned));
in.readFully(buffer);
in.close();
// Chargement des certificats qui seront stockes dans le fichier .p7
// Ici, seulement le certificat personnal_nyal.cer sera associe.
// Par contre, la cha�ne des certificats non.
X509Certificate cert = ReadX509.read(new FileInputStream("msp.cer"));
// "2[$0wUOS";
String password = "msp_pass";
// "thawte freemail member's thawte consulting (pty) ltd. id";
String alias = "msp";
KeyInformation keyInfo = ReadPKCS12.read(new FileInputStream("msp.p12"), password, alias);
// List<X509Certificate> certList = new ArrayList<X509Certificate> (); Wrong check below
// certList.add (cert);
List<X509CertificateHolder> certList = new ArrayList<X509CertificateHolder>();
certList.add(new X509CertificateHolder(cert.getEncoded()));
// CertStore certs = CertStore.getInstance ("Collection", new CollectionCertStoreParameters (certList), "BC"); Wrong check below
JcaCertStore jcaCertStore = new JcaCertStore(certList);
CMSSignedDataGenerator signGen = new CMSSignedDataGenerator();
ContentSigner contentSigner = new JcaContentSignerBuilder("SHA1withRSA").setProvider("BC").build(keyInfo.getPrivateKey());
DigestCalculatorProvider digestCalcProv = new JcaDigestCalculatorProviderBuilder().setProvider("BC").build();
SignerInfoGenerator signInfoGeneratorBuilder = new JcaSignerInfoGeneratorBuilder(digestCalcProv).build(contentSigner, cert);
signGen.addSignerInfoGenerator(signInfoGeneratorBuilder);
// privatekey correspond a notre cle privee recuperee du fichier PKCS#12
// cert correspond au certificat publique personnal_nyal.cer
// Le dernier argument est l'algorithme de hachage qui sera utilise
// signGen.addSigner (keyInfo.getPrivateKey (), cert, CMSSignedDataGenerator.DIGEST_SHA1);
signGen.addCertificates(jcaCertStore);
// Wrong signGen.addCertificatesAndCRLs (certs);
CMSProcessableByteArray content = new CMSProcessableByteArray(buffer);
// Generation du fichier CMS/PKCS#7
// L'argument deux permet de signifier si le document doit etre attache avec la signature
// Valeur true: le fichier est attache (c'est le cas ici)
// Valeur false: le fichier est detache
// CMSSignedData signedData = signGen.generate (content, true, "BC");
CMSSignedData signedData = signGen.generate(content, true);
byte[] signeddata = signedData.getEncoded();
// Ecriture du buffer dans un fichier.
FileOutputStream envfos = new FileOutputStream("Signed.pk7");
envfos.write(signeddata);
envfos.close();
}
use of org.bouncycastle.cms.CMSSignedData in project serverless by bluenimble.
the class DefaultSigner method signWithCerts.
// Updated
private void signWithCerts(SecureDocument doc, PrivateKey key, X509Certificate[] certs) throws SignerException {
if (certs == null || certs.length == 0) {
throw new SignerException("A valid X509 Certificate is required");
}
String signAlg = "DSA".equals(key.getAlgorithm()) ? CMSSignedDataGenerator.DIGEST_SHA1 : CMSSignedDataGenerator.DIGEST_MD5;
CMSSignedDataGenerator signGen = new CMSSignedDataGenerator();
List<X509CertificateHolder> certList = new ArrayList<X509CertificateHolder>();
try {
ContentSigner contentSigner = new JcaContentSignerBuilder(signAlg).setProvider("BC").build(key);
DigestCalculatorProvider digestCalcProv = new JcaDigestCalculatorProviderBuilder().setProvider("BC").build();
for (X509Certificate cert : certs) {
X509CertificateHolder certHolder = new X509CertificateHolder(cert.getEncoded());
certList.add(certHolder);
SignerInfoGenerator signInfoGeneratorBuilder = new JcaSignerInfoGeneratorBuilder(digestCalcProv).build(contentSigner, cert);
signGen.addSignerInfoGenerator(signInfoGeneratorBuilder);
}
JcaCertStore jcaCertStore = new JcaCertStore(certList);
signGen.addCertificates(jcaCertStore);
// signGen.addCRLs (jcaCertStore); TODO : not sure
CMSProcessableByteArray content = new CMSProcessableByteArray(doc.getBytes());
CMSSignedData signedData = signGen.generate(content, true);
doc.setBytes(signedData.getEncoded());
} catch (Throwable th) {
throw new SignerException(th, th.getMessage());
}
}
use of org.bouncycastle.cms.CMSSignedData in project fdroidclient by f-droid.
the class SignatureBlockGenerator method generate.
/**
* Sign the given content using the private and public keys from the keySet, and return the encoded CMS (PKCS#7) data.
* Use of direct signature and DER encoding produces a block that is verifiable by Android recovery programs.
*/
public static byte[] generate(KeySet keySet, byte[] content) {
try {
List certList = new ArrayList();
CMSTypedData msg = new CMSProcessableByteArray(content);
certList.add(keySet.getPublicKey());
Store certs = new JcaCertStore(certList);
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
JcaContentSignerBuilder jcaContentSignerBuilder = new JcaContentSignerBuilder(keySet.getSignatureAlgorithm()).setProvider("BC");
ContentSigner sha1Signer = jcaContentSignerBuilder.build(keySet.getPrivateKey());
JcaDigestCalculatorProviderBuilder jcaDigestCalculatorProviderBuilder = new JcaDigestCalculatorProviderBuilder().setProvider("BC");
DigestCalculatorProvider digestCalculatorProvider = jcaDigestCalculatorProviderBuilder.build();
JcaSignerInfoGeneratorBuilder jcaSignerInfoGeneratorBuilder = new JcaSignerInfoGeneratorBuilder(digestCalculatorProvider);
jcaSignerInfoGeneratorBuilder.setDirectSignature(true);
SignerInfoGenerator signerInfoGenerator = jcaSignerInfoGeneratorBuilder.build(sha1Signer, keySet.getPublicKey());
gen.addSignerInfoGenerator(signerInfoGenerator);
gen.addCertificates(certs);
CMSSignedData sigData = gen.generate(msg, false);
return sigData.toASN1Structure().getEncoded("DER");
} catch (Exception x) {
throw new RuntimeException(x.getMessage(), x);
}
}
Aggregations