public PKCS12SafeBagFactory(ContentInfo info, InputDecryptorProvider inputDecryptorProvider) throws PKCSException { if (info.getContentType().equals(PKCSObjectIdentifiers.encryptedData)) { CMSEncryptedData encData = new CMSEncryptedData(org.bouncycastle.asn1.cms.ContentInfo.getInstance(info)); try { this.safeBagSeq = ASN1Sequence.getInstance(encData.getContent(inputDecryptorProvider)); } catch (CMSException e) { throw new PKCSException("unable to extract data: " + e.getMessage(), e); } return; } throw new IllegalArgumentException("encryptedData requires constructor with decryptor."); }
/** * Add a SafeBag that is to be included as is. * * @param data the SafeBag to add. * @return this builder. * @throws IOException */ public PKCS12PfxPduBuilder addData(PKCS12SafeBag data) throws IOException { dataVector.add(new ContentInfo(PKCSObjectIdentifiers.data, new DEROctetString(new DLSequence(data.toASN1Structure()).getEncoded()))); return this; }
/** * Build the Pfx structure, protecting it with a MAC calculated against the passed in password. * * @param macCalcBuilder a builder for a PKCS12 mac calculator. * @param password the password to use. * @return a Pfx object. * @throws PKCSException on a encoding or processing error. */ public PKCS12PfxPdu build(PKCS12MacCalculatorBuilder macCalcBuilder, char[] password) throws PKCSException { AuthenticatedSafe auth = AuthenticatedSafe.getInstance(new DLSequence(dataVector)); byte[] encAuth; try { encAuth = auth.getEncoded(); } catch (IOException e) { throw new PKCSException("unable to encode AuthenticatedSafe: " + e.getMessage(), e); } ContentInfo mainInfo = new ContentInfo(PKCSObjectIdentifiers.data, new DEROctetString(encAuth)); MacData mData = null; if (macCalcBuilder != null) { MacDataGenerator mdGen = new MacDataGenerator(macCalcBuilder); mData = mdGen.build(password, encAuth); } // // output the Pfx // Pfx pfx = new Pfx(mainInfo, mData); return new PKCS12PfxPdu(pfx); }
/** * Return the content infos in the AuthenticatedSafe contained in this Pfx. * * @return an array of ContentInfo. */ public ContentInfo[] getContentInfos() { ASN1Sequence seq = ASN1Sequence.getInstance(ASN1OctetString.getInstance(this.pfx.getAuthSafe().getContent()).getOctets()); ContentInfo[] content = new ContentInfo[seq.size()]; for (int i = 0; i != seq.size(); i++) { content[i] = ContentInfo.getInstance(seq.getObjectAt(i)); } return content; }
public PKCS12SafeBagFactory(ContentInfo info) { if (info.getContentType().equals(PKCSObjectIdentifiers.encryptedData)) { throw new IllegalArgumentException("encryptedData requires constructor with decryptor."); } this.safeBagSeq = ASN1Sequence.getInstance(ASN1OctetString.getInstance(info.getContent()).getOctets()); }
@SuppressWarnings({ "unchecked", "resource" }) private List<DadesCertificat> obtenirDadesCertificatNoPdf( byte[] signatura) throws Exception { X509Certificate[] certificats = null; byte[] pkcs7Bytes = signatura; ASN1InputStream asn1is = new ASN1InputStream(new ByteArrayInputStream(pkcs7Bytes)); ContentInfo pkcs7Info = ContentInfo.getInstance(asn1is.readObject()); SignedData signedData = SignedData.getInstance(pkcs7Info.getContent()); ASN1Set signerInfos = signedData.getSignerInfos(); int numSignatures = signerInfos.size(); if (numSignatures > 0) { afegirProveidorBouncyCastle(); CMSSignedData cmsSignedData = new CMSSignedData(pkcs7Bytes); SignerInformationStore signers = cmsSignedData.getSignerInfos(); CertStore certStore = cmsSignedData.getCertificatesAndCRLs("Collection", "BC"); List<X509Certificate> certs = new ArrayList<X509Certificate>(); for (SignerInformation signer: (Collection<SignerInformation>)signers.getSigners()) { for (Certificate cert: certStore.getCertificates(signer.getSID())) { if (cert instanceof X509Certificate) certs.add((X509Certificate)cert); } } certificats = certs.toArray(new X509Certificate[certs.size()]); if (certificats.length != 1) throw new SignaturaPluginException("Aquesta signatura conté més d'un certificat"); //resposta.setInfoCertificat(getInfoCertificat(certificats[0])); List<DadesCertificat> dadesCertificats = new ArrayList<DadesCertificat>(); dadesCertificats.add(getDadesCertificat(certificats[0])); return dadesCertificats; } return null; }
/** * Convert a byte array to a PKCS7 SignedData object * @param bytes byte array * @return PKCS7 SignedData object */ public static SignedData bytesToPkcs7SignedData(byte[] bytes) { if(bytes == null) { throw new IllegalArgumentException("null bytes"); } ASN1InputStream ais = new ASN1InputStream(bytes); ASN1Object asn1 = null; try { asn1 = ais.readObject(); } catch(IOException ioe) { throw new IllegalArgumentException("not a pkcs7 signature"); } finally { try { ais.close(); } catch (IOException e) { // Ignore } } ContentInfo ci = ContentInfo.getInstance(asn1); ASN1ObjectIdentifier typeId = ci.getContentType(); if( ! typeId.equals(PKCSObjectIdentifiers.signedData)) { throw new IllegalArgumentException("not a pkcs7 signature"); } return SignedData.getInstance(ci.getContent()); }
private static PKCS12SafeBagFactory getSafeBagFactory(ContentInfo contentInfo) { return new PKCS12SafeBagFactory(contentInfo); }
public void testKeyBag() throws Exception { OutputEncryptor encOut = new BcPKCS12PBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC, new CBCBlockCipher(new DESedeEngine())).build(passwd); InputDecryptorProvider inputDecryptorProvider = new BcPKCS12PBEInputDecryptorProviderBuilder().build(passwd); KeyFactory fact = KeyFactory.getInstance("RSA", BC); PrivateKey privKey = fact.generatePrivate(privKeySpec); PKCS12SafeBagBuilder keyBagBuilder = new JcaPKCS12SafeBagBuilder(privKey); keyBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString("Eric's Key")); PKCS12PfxPduBuilder builder = new PKCS12PfxPduBuilder(); builder.addEncryptedData(encOut, keyBagBuilder.build()); PKCS12PfxPdu pfx = builder.build(new BcPKCS12MacCalculatorBuilder(), passwd); assertTrue(pfx.hasMac()); assertTrue(pfx.isMacValid(new BcPKCS12MacCalculatorBuilderProvider(BcDefaultDigestProvider.INSTANCE), passwd)); ContentInfo[] infos = pfx.getContentInfos(); for (int i = 0; i != infos.length; i++) { if (infos[i].getContentType().equals(PKCSObjectIdentifiers.encryptedData)) { PKCS12SafeBagFactory dataFact = new PKCS12SafeBagFactory(infos[i], inputDecryptorProvider); PKCS12SafeBag[] bags = dataFact.getSafeBags(); assertEquals(1, bags.length); assertEquals(PKCSObjectIdentifiers.keyBag, bags[0].getType()); assertTrue(Arrays.areEqual(privKey.getEncoded(), ((PrivateKeyInfo)bags[0].getBagValue()).getEncoded())); Attribute[] attributes = bags[0].getAttributes(); assertEquals(1, attributes.length); assertEquals(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, attributes[0].getAttrType()); ASN1Encodable[] attrValues = attributes[0].getAttributeValues(); assertEquals(1, attrValues.length); assertEquals(new DERBMPString("Eric's Key"), attrValues[0]); } else { fail("unknown bag encountered"); } } }