Java Code Examples for org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder#setSignedAttributeGenerator()
The following examples show how to use
org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder#setSignedAttributeGenerator() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CmsSignatureBuilder.java From freehealth-connector with GNU Affero General Public License v3.0 | 5 votes |
public byte[] sign(Credential signatureCredential, byte[] byteToSign, Map<String, Object> options) throws TechnicalConnectorException { byte[] contentToSign = ArrayUtils.clone(byteToSign); Map<String, Object> optionMap = new HashMap(); if (options != null) { optionMap.putAll(options); } this.validateInput(signatureCredential, contentToSign); try { CMSTypedData content = new CMSProcessableByteArray(contentToSign); CMSSignedDataGenerator generator = new CMSSignedDataGenerator(); String signatureAlgorithm = (String)SignatureUtils.getOption("signatureAlgorithm", optionMap, "Sha1WithRSA"); JcaSignerInfoGeneratorBuilder signerInfoGeneratorBuilder = new JcaSignerInfoGeneratorBuilder((new JcaDigestCalculatorProviderBuilder()).build()); ContentSigner contentSigner = (new JcaContentSignerBuilder(signatureAlgorithm)).build(signatureCredential.getPrivateKey()); CMSAttributeTableGenerator cmsAttributeTableGenerator = (CMSAttributeTableGenerator)SignatureUtils.getOption("signedAttributeGenerator", optionMap, new DefaultSignedAttributeTableGenerator()); signerInfoGeneratorBuilder.setSignedAttributeGenerator(cmsAttributeTableGenerator); generator.addSignerInfoGenerator(signerInfoGeneratorBuilder.build(contentSigner, signatureCredential.getCertificate())); Certificate[] certificateChain = signatureCredential.getCertificateChain(); if (certificateChain != null && certificateChain.length > 0) { generator.addCertificates(new JcaCertStore(Arrays.asList(certificateChain))); } boolean encapsulate = (Boolean)SignatureUtils.getOption("encapsulate", optionMap, Boolean.FALSE); return generator.generate(content, encapsulate).getEncoded(); } catch (Exception var14) { LOG.error(var14.getMessage(), var14); throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_SIGNATURE, var14, new Object[]{var14.getClass().getSimpleName() + " : " + var14.getMessage()}); } }
Example 2
Source File: CmsSignatureBuilder.java From freehealth-connector with GNU Affero General Public License v3.0 | 5 votes |
public byte[] sign(Credential signatureCredential, byte[] byteToSign, Map<String, Object> options) throws TechnicalConnectorException { byte[] contentToSign = ArrayUtils.clone(byteToSign); Map<String, Object> optionMap = new HashMap(); if (options != null) { optionMap.putAll(options); } this.validateInput(signatureCredential, contentToSign); try { CMSTypedData content = new CMSProcessableByteArray(contentToSign); CMSSignedDataGenerator generator = new CMSSignedDataGenerator(); String signatureAlgorithm = (String)SignatureUtils.getOption("signatureAlgorithm", optionMap, "Sha1WithRSA"); JcaSignerInfoGeneratorBuilder signerInfoGeneratorBuilder = new JcaSignerInfoGeneratorBuilder((new JcaDigestCalculatorProviderBuilder()).build()); ContentSigner contentSigner = (new JcaContentSignerBuilder(signatureAlgorithm)).build(signatureCredential.getPrivateKey()); CMSAttributeTableGenerator cmsAttributeTableGenerator = (CMSAttributeTableGenerator)SignatureUtils.getOption("signedAttributeGenerator", optionMap, new DefaultSignedAttributeTableGenerator()); signerInfoGeneratorBuilder.setSignedAttributeGenerator(cmsAttributeTableGenerator); generator.addSignerInfoGenerator(signerInfoGeneratorBuilder.build(contentSigner, signatureCredential.getCertificate())); Certificate[] certificateChain = signatureCredential.getCertificateChain(); if (certificateChain != null && certificateChain.length > 0) { generator.addCertificates(new JcaCertStore(Arrays.asList(certificateChain))); } boolean encapsulate = (Boolean)SignatureUtils.getOption("encapsulate", optionMap, Boolean.FALSE); return generator.generate(content, encapsulate).getEncoded(); } catch (Exception var14) { LOG.error(var14.getMessage(), var14); throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_SIGNATURE, var14, new Object[]{var14.getClass().getSimpleName() + " : " + var14.getMessage()}); } }
Example 3
Source File: CmsSignatureBuilder.java From freehealth-connector with GNU Affero General Public License v3.0 | 5 votes |
public byte[] sign(Credential signatureCredential, byte[] byteToSign, Map<String, Object> options) throws TechnicalConnectorException { byte[] contentToSign = ArrayUtils.clone(byteToSign); Map<String, Object> optionMap = new HashMap(); if (options != null) { optionMap.putAll(options); } this.validateInput(signatureCredential, contentToSign); try { CMSTypedData content = new CMSProcessableByteArray(contentToSign); CMSSignedDataGenerator generator = new CMSSignedDataGenerator(); String signatureAlgorithm = (String)SignatureUtils.getOption("signatureAlgorithm", optionMap, "Sha1WithRSA"); JcaSignerInfoGeneratorBuilder signerInfoGeneratorBuilder = new JcaSignerInfoGeneratorBuilder((new JcaDigestCalculatorProviderBuilder()).build()); ContentSigner contentSigner = (new JcaContentSignerBuilder(signatureAlgorithm)).build(signatureCredential.getPrivateKey()); CMSAttributeTableGenerator cmsAttributeTableGenerator = (CMSAttributeTableGenerator)SignatureUtils.getOption("signedAttributeGenerator", optionMap, new DefaultSignedAttributeTableGenerator()); signerInfoGeneratorBuilder.setSignedAttributeGenerator(cmsAttributeTableGenerator); generator.addSignerInfoGenerator(signerInfoGeneratorBuilder.build(contentSigner, signatureCredential.getCertificate())); Certificate[] certificateChain = signatureCredential.getCertificateChain(); if (certificateChain != null && certificateChain.length > 0) { generator.addCertificates(new JcaCertStore(Arrays.asList(certificateChain))); } boolean encapsulate = ((Boolean)SignatureUtils.getOption("encapsulate", optionMap, Boolean.FALSE)); return generator.generate(content, encapsulate).getEncoded(); } catch (Exception var14) { LOG.error(var14.getMessage(), var14); throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_SIGNATURE, var14, new Object[]{var14.getClass().getSimpleName() + " : " + var14.getMessage()}); } }
Example 4
Source File: CmsSignatureBuilder.java From freehealth-connector with GNU Affero General Public License v3.0 | 5 votes |
public byte[] sign(Credential signatureCredential, byte[] byteToSign, Map<String, Object> options) throws TechnicalConnectorException { byte[] contentToSign = ArrayUtils.clone(byteToSign); Map<String, Object> optionMap = new HashMap(); if (options != null) { optionMap.putAll(options); } this.validateInput(signatureCredential, contentToSign); try { CMSTypedData content = new CMSProcessableByteArray(contentToSign); CMSSignedDataGenerator generator = new CMSSignedDataGenerator(); String signatureAlgorithm = (String)SignatureUtils.getOption("signatureAlgorithm", optionMap, "Sha1WithRSA"); JcaSignerInfoGeneratorBuilder signerInfoGeneratorBuilder = new JcaSignerInfoGeneratorBuilder((new JcaDigestCalculatorProviderBuilder()).build()); ContentSigner contentSigner = (new JcaContentSignerBuilder(signatureAlgorithm)).build(signatureCredential.getPrivateKey()); CMSAttributeTableGenerator cmsAttributeTableGenerator = (CMSAttributeTableGenerator)SignatureUtils.getOption("signedAttributeGenerator", optionMap, new DefaultSignedAttributeTableGenerator()); signerInfoGeneratorBuilder.setSignedAttributeGenerator(cmsAttributeTableGenerator); generator.addSignerInfoGenerator(signerInfoGeneratorBuilder.build(contentSigner, signatureCredential.getCertificate())); Certificate[] certificateChain = signatureCredential.getCertificateChain(); if (certificateChain != null && certificateChain.length > 0) { generator.addCertificates(new JcaCertStore(Arrays.asList(certificateChain))); } boolean encapsulate = ((Boolean)SignatureUtils.getOption("encapsulate", optionMap, Boolean.FALSE)).booleanValue(); return generator.generate(content, encapsulate).getEncoded(); } catch (Exception var14) { LOG.error(var14.getMessage(), var14); throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_SIGNATURE, var14, new Object[]{var14.getClass().getSimpleName() + " : " + var14.getMessage()}); } }
Example 5
Source File: CmsSignatureBuilder.java From freehealth-connector with GNU Affero General Public License v3.0 | 5 votes |
public byte[] sign(Credential signatureCredential, byte[] byteToSign, Map<String, Object> options) throws TechnicalConnectorException { byte[] contentToSign = ArrayUtils.clone(byteToSign); Map<String, Object> optionMap = new HashMap(); if (options != null) { optionMap.putAll(options); } this.validateInput(signatureCredential, contentToSign); try { CMSTypedData content = new CMSProcessableByteArray(contentToSign); CMSSignedDataGenerator generator = new CMSSignedDataGenerator(); String signatureAlgorithm = (String)SignatureUtils.getOption("signatureAlgorithm", optionMap, "Sha1WithRSA"); JcaSignerInfoGeneratorBuilder signerInfoGeneratorBuilder = new JcaSignerInfoGeneratorBuilder((new JcaDigestCalculatorProviderBuilder()).build()); ContentSigner contentSigner = (new JcaContentSignerBuilder(signatureAlgorithm)).build(signatureCredential.getPrivateKey()); CMSAttributeTableGenerator cmsAttributeTableGenerator = (CMSAttributeTableGenerator)SignatureUtils.getOption("signedAttributeGenerator", optionMap, new DefaultSignedAttributeTableGenerator()); signerInfoGeneratorBuilder.setSignedAttributeGenerator(cmsAttributeTableGenerator); generator.addSignerInfoGenerator(signerInfoGeneratorBuilder.build(contentSigner, signatureCredential.getCertificate())); Certificate[] certificateChain = signatureCredential.getCertificateChain(); if (certificateChain != null && certificateChain.length > 0) { generator.addCertificates(new JcaCertStore(Arrays.asList(certificateChain))); } boolean encapsulate = (Boolean) SignatureUtils.getOption("encapsulate", optionMap, Boolean.FALSE); return generator.generate(content, encapsulate).getEncoded(); } catch (Exception var14) { LOG.error(var14.getMessage(), var14); throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_SIGNATURE, var14, new Object[]{var14.getClass().getSimpleName() + " : " + var14.getMessage()}); } }
Example 6
Source File: NextCaMessage.java From xipki with Apache License 2.0 | 4 votes |
public ContentInfo encode(PrivateKey signingKey, X509Cert signerCert, X509Cert[] cmsCertSet) throws MessageEncodingException { Args.notNull(signingKey, "signingKey"); Args.notNull(signerCert, "signerCert"); try { CMSSignedDataGenerator degenerateSignedData = new CMSSignedDataGenerator(); degenerateSignedData.addCertificate(caCert.toBcCert()); if (CollectionUtil.isNotEmpty(raCerts)) { for (X509Cert m : raCerts) { degenerateSignedData.addCertificate(m.toBcCert()); } } byte[] degenratedSignedDataBytes = degenerateSignedData.generate( new CMSAbsentContent()).getEncoded(); CMSSignedDataGenerator generator = new CMSSignedDataGenerator(); // I don't known which hash algorithm is supported by the client, use SHA-1 String signatureAlgo = getSignatureAlgorithm(signingKey, HashAlgo.SHA1); ContentSigner signer = new JcaContentSignerBuilder(signatureAlgo).build(signingKey); // signerInfo JcaSignerInfoGeneratorBuilder signerInfoBuilder = new JcaSignerInfoGeneratorBuilder( new BcDigestCalculatorProvider()); signerInfoBuilder.setSignedAttributeGenerator(new DefaultSignedAttributeTableGenerator()); SignerInfoGenerator signerInfo = signerInfoBuilder.build(signer, signerCert.toBcCert()); generator.addSignerInfoGenerator(signerInfo); CMSTypedData cmsContent = new CMSProcessableByteArray(CMSObjectIdentifiers.signedData, degenratedSignedDataBytes); // certificateSet ScepUtil.addCmsCertSet(generator, cmsCertSet); return generator.generate(cmsContent, true).toASN1Structure(); } catch (CMSException | CertificateEncodingException | IOException | OperatorCreationException ex) { throw new MessageEncodingException(ex); } }