List of usage examples for org.bouncycastle.asn1.pkcs SignedData getCertificates
public ASN1Set getCertificates()
From source file:ca.trustpoint.m2m.M2mCertificateFactory.java
License:Apache License
/** * Generates a {@link java.security.cert.CertPath CertPath} object and initializes it with the * data read from the {@link java.io.InputStream InputStream} inStream. The data is assumed to be * in the specified encoding./*from ww w .j ava 2 s . c om*/ * * <p> * The returned certificate path object can be typecast to the {@link M2mCertPath} class. * * @param inStream an {@link java.io.InputStream InputStream} containing the data * @param encoding the encoding used for the data * @return a {@link java.security.cert.CertPath CertPath} initialized with the data from the * {@link java.io.InputStream InputStream} * @exception CertificateException if an exception occurs while decoding or the encoding requested * is not supported */ @Override public CertPath engineGenerateCertPath(InputStream inStream, String encoding) throws CertificateException { if (inStream == null) { throw new CertificateException("input stream is null"); } try { ASN1InputStream aIn = new ASN1InputStream(inStream); ASN1Sequence seq = ASN1Sequence.getInstance(aIn.readObject()); aIn.close(); ASN1Encodable[] objs; List<M2mCertificate> certList; InputStream is; M2mCertificate cert; if (encoding.equals(SupportedEncodings.PKIPATH.getId())) { objs = seq.toArray(); certList = new ArrayList<M2mCertificate>(objs.length); // certificates in PKIPATH encoding is from root to signer but M2MCerPath stores // certificates from signer to root so do it in reverse order. for (int i = objs.length - 1; i >= 0; i--) { is = new ByteArrayInputStream(objs[i].toASN1Primitive().getEncoded()); cert = (M2mCertificate) engineGenerateCertificate(is); certList.add(cert); } } else if (encoding.equals(SupportedEncodings.PKCS7.getId())) { ContentInfo ci = ContentInfo.getInstance(seq); SignedData sd = SignedData.getInstance(ci.getContent()); objs = sd.getCertificates().toArray(); certList = new ArrayList<M2mCertificate>(objs.length); // certificates in PKCS7 encoding is from signer to root, the same order as in M2mCertPath for (int i = 0; i < objs.length; i++) { is = new ByteArrayInputStream(objs[i].toASN1Primitive().getEncoded()); cert = (M2mCertificate) engineGenerateCertificate(is); certList.add(cert); } } else { throw new CertificateException("unknown encoding path: " + encoding); } return new M2mCertPath(certList); } catch (IOException e) { throw new CertificateException("IOException parsing PkiPath data: " + e, e); } }
From source file:fi.laverca.Pkcs7.java
License:Apache License
/** * Read all certificates from a SignedData * @param sd data/* w w w.j a v a2 s.c o m*/ * @return all X509 certificates or null */ public static List<X509Certificate> readCerts(final SignedData sd) { if (sd == null) { return null; } List<X509Certificate> certs = new ArrayList<X509Certificate>(); ASN1Set certSet = sd.getCertificates(); Enumeration<?> en = certSet.getObjects(); while (en.hasMoreElements()) { Object o = en.nextElement(); try { byte[] certDer = ((DERSequence) o).getEncoded(); X509Certificate cert = X509Util.DERtoX509Certificate(certDer); certs.add(cert); } catch (IOException e) { log.debug("Failed to read cert", e); } } return certs; }
From source file:se.inera.intyg.webcert.web.service.signatur.asn1.ASN1UtilImpl.java
License:Open Source License
@Override public String getValue(String identifier, InputStream asn1Signature) { ByteArrayInputStream bais = null; ASN1InputStream asn1InputStream = null; try {/*from w w w . ja va2s . co m*/ bais = convertStream(asn1Signature); asn1InputStream = new ASN1InputStream(bais); DERObject obj = asn1InputStream.readObject(); ContentInfo contentInfo = ContentInfo.getInstance(obj); // Extract certificates SignedData signedData = SignedData.getInstance(contentInfo.getContent()); return findInCertificate(identifier, (DERObject) signedData.getCertificates().getObjectAt(0)); } catch (IOException e) { LOG.error("Error parsing signature: {}", e.getMessage()); throw new IllegalStateException(e); } finally { IOUtils.closeQuietly(bais); IOUtils.closeQuietly(asn1InputStream); } }