List of usage examples for org.bouncycastle.asn1 ASN1ApplicationSpecific getObject
public ASN1Primitive getObject(int derTagNo) throws IOException
From source file:ca.trustpoint.m2m.M2mCertificateFactory.java
License:Apache License
/** * Generates a certificate object and initializes it with the data read from the * {@link java.io.InputStream InputStream} {@code inStream}. * * <p>/*from ww w . jav a 2 s .c om*/ * The returned certificate object can be casted to the {@link M2mCertificate M2MCertificate} * class. * * <p> * The certificate provided in {@code inStream} must be DER-encoded and may be supplied in binary * or printable (Base64) encoding. If the certificate is provided in Base64 encoding, it must be * bounded at the beginning by -----BEGIN CERTIFICATE-----, and must be bounded at the end by * -----END CERTIFICATE-----. * * <p> * Note that if the given input stream does not support {@link java.io.InputStream#mark(int) mark} * and {@link java.io.InputStream#reset() reset}, this method will consume the entire input * stream. Otherwise, each call to this method consumes one certificate and the read position of * the input stream is positioned to the next available byte after the inherent end-of-certificate * marker. If the data in the input stream does not contain an inherent end-of-certificate marker * (other than EOF) and there is trailing data after the certificate is parsed, a * {@link java.security.cert.CertificateException CertificateException} is thrown. * * @param inStream an input stream with the certificate data. * * @return a certificate object initialized with the data from the input stream. * * @exception CertificateException on parsing errors. */ @Override public Certificate engineGenerateCertificate(InputStream inStream) throws CertificateException { if (inStream == null) { throw new IllegalArgumentException("input stream is null"); } try { ASN1InputStream aIn = new ASN1InputStream(inStream); ASN1ApplicationSpecific app = ASN1ApplicationSpecific.getInstance(aIn.readObject()); aIn.close(); int appTag = app.getApplicationTag(); if (appTag != M2mCertificate.APPLICATION_TAG_NUMBER) { throw new IOException("not M2M certificate application tag: " + appTag); } ASN1Sequence seq = (ASN1Sequence) app.getObject(BERTags.SEQUENCE); if (seq.size() != 2) { throw new IOException("sequence wrong size for a M2M certificate"); } // Construct M2M certificate M2mCertificate cert = new M2mCertificate(); for (int i = 0; i < seq.size(); i++) { ASN1TaggedObject obj = (ASN1TaggedObject) seq.getObjectAt(i); CertificateFields tag = CertificateFields.getInstance(obj.getTagNo()); switch (tag) { case TBS_CERTIFICATE: ASN1Sequence tbsCertificate = ASN1Sequence.getInstance(obj, false); parseTbsCertificate(tbsCertificate, cert); break; case CA_CALC_VALUE: ASN1OctetString cACalcValue = ASN1OctetString.getInstance(obj, false); cert.setCaCalcValue(cACalcValue.getOctets()); break; default: throw new IOException("unknown M2M data field number: " + tag.getTagNumber()); } } return cert; } catch (Exception e) { // Catch all exceptions and convert it to a CertificateException throw new CertificateException("exception on parsing certificate data", e); } }
From source file:pro.javacard.gp.GPData.java
License:Open Source License
public static List<GPKeySet.GPKey> get_key_template_list(byte[] data) throws GPException { List<GPKey> r = new ArrayList<>(); try (ASN1InputStream ais = new ASN1InputStream(data)) { while (ais.available() > 0) { ASN1ApplicationSpecific keys = (DERApplicationSpecific) ais.readObject(); // System.out.println(ASN1Dump.dumpAsString(keys, true)); ASN1Sequence seq = (ASN1Sequence) keys.getObject(BERTags.SEQUENCE); for (ASN1Encodable p : Lists.newArrayList(seq.iterator())) { ASN1ApplicationSpecific key = (DERApplicationSpecific) p.toASN1Primitive(); byte[] tmpl = key.getContents(); if (tmpl.length < 4) { throw new GPDataException("Key info template shorter than 4 bytes", tmpl); }/*ww w . ja va 2 s . co m*/ int id = tmpl[0] & 0xFF; int version = tmpl[1] & 0xFF; int type = tmpl[2] & 0xFF; int length = tmpl[3] & 0xFF; if (type == 0xFF) { throw new GPDataException("Extended key template not yet supported", tmpl); } r.add(new GPKey(version, id, length, type)); } } } catch (IOException | ClassCastException e) { throw new GPDataException("Could not parse key template: " + e.getMessage(), e); } return r; }