/** * return the issuer of the given cert as an X509PrincipalObject. */ public static X509Principal getIssuerX509Principal( X509Certificate cert) throws CertificateEncodingException { try { TBSCertificateStructure tbsCert = TBSCertificateStructure.getInstance( ASN1Primitive.fromByteArray(cert.getTBSCertificate())); return new X509Principal(X509Name.getInstance(tbsCert.getIssuer())); } catch (IOException e) { throw new CertificateEncodingException(e.toString()); } }
/** * Produce an object suitable for an ASN1OutputStream. * <pre> * BasicConstraints := SEQUENCE { * cA BOOLEAN DEFAULT FALSE, * pathLenConstraint INTEGER (0..MAX) OPTIONAL * } * </pre> */ public ASN1Primitive toASN1Primitive() { ASN1EncodableVector v = new ASN1EncodableVector(); if (cA != null) { v.add(cA); } if (pathLenConstraint != null) // yes some people actually do this when cA is false... { v.add(pathLenConstraint); } return new DERSequence(v); }
private void parseDG1(byte[] DG1) { try { ASN1InputStream bIn = new ASN1InputStream(DG1); org.bouncycastle.asn1.DERApplicationSpecific app = (DERApplicationSpecific) bIn.readObject(); ASN1Sequence seq = (ASN1Sequence) app.getObject(BERTags.SEQUENCE); Enumeration secEnum = seq.getObjects(); while (secEnum.hasMoreElements()) { ASN1Primitive seqObj = (ASN1Primitive) secEnum.nextElement(); byte[] data = seqObj.getEncoded(); if (data[0]== 0x41) { Log.d("type approval number", ByteUtils.bytesToHex(data)); this.set5F01(data); } else if (data[0] == 0x42) { byte[] input = Arrays.copyOfRange(data, 3, data.length); parse5F02(input); } else if (data[0] == 0x7F) { parse7F63(data); } } bIn.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Produce an object suitable for an ASN1OutputStream. * <p/> * Returns: * <p/> * <pre> * ProcurationSyntax ::= SEQUENCE { * country [1] EXPLICIT PrintableString(SIZE(2)) OPTIONAL, * typeOfSubstitution [2] EXPLICIT DirectoryString (SIZE(1..128)) OPTIONAL, * signingFor [3] EXPLICIT SigningFor * } * <p/> * SigningFor ::= CHOICE * { * thirdPerson GeneralName, * certRef IssuerSerial * } * </pre> * * @return a DERObject */ public ASN1Primitive toASN1Primitive() { ASN1EncodableVector vec = new ASN1EncodableVector(); if (country != null) { vec.add(new DERTaggedObject(true, 1, new DERPrintableString(country, true))); } if (typeOfSubstitution != null) { vec.add(new DERTaggedObject(true, 2, typeOfSubstitution)); } if (thirdPerson != null) { vec.add(new DERTaggedObject(true, 3, thirdPerson)); } else { vec.add(new DERTaggedObject(true, 3, certRef)); } return new DERSequence(vec); }
public ASN1Primitive toASN1Primitive() { ASN1EncodableVector v = new ASN1EncodableVector(); if (permitted != null) { v.add(new DERTaggedObject(false, 0, new DERSequence(permitted))); } if (excluded != null) { v.add(new DERTaggedObject(false, 1, new DERSequence(excluded))); } return new DERSequence(v); }
private void parseDG1(byte[] DG1) { try (ASN1InputStream bIn = new ASN1InputStream(DG1)) { org.bouncycastle.asn1.DERApplicationSpecific app = (DERApplicationSpecific) bIn.readObject(); ASN1Sequence seq = (ASN1Sequence) app.getObject(BERTags.SEQUENCE); Enumeration secEnum = seq.getObjects(); while (secEnum.hasMoreElements()) { ASN1Primitive seqObj = (ASN1Primitive) secEnum.nextElement(); byte[] data = seqObj.getEncoded(); if (data[0]== 0x41) { Log.d("type approval number", ByteUtils.bytesToHex(data)); this.set5F01(Bytes.allButFirst(data, 1)); } else if (data[0] == 0x42) { byte[] input = Arrays.copyOfRange(data, 3, data.length); parse5F02(input); } else if (data[0] == 0x7F) { parse7F63(data); } } } catch (IOException e) { Log.e(getClass().getName(), e.getMessage(), e); } }
private void parse7F63(byte[] input) { Log.d("input", ByteUtils.bytesToHex(input)); try (ASN1InputStream bIn = new ASN1InputStream(input)) { ASN1Primitive obj = bIn.readObject(); DERApplicationSpecific app = (DERApplicationSpecific) obj; ASN1Sequence seq = (ASN1Sequence) app.getObject(BERTags.SEQUENCE); Enumeration secEnum = seq.getObjects(); List<byte[]> categories = new ArrayList<>(); while (secEnum.hasMoreElements()) { ASN1Primitive seqObj = (ASN1Primitive) secEnum.nextElement(); byte[] data = seqObj.getEncoded(); Log.d("5F02data", ByteUtils.bytesToHex(data)); switch (data[0]) { case 0x02: Log.d("#CATEGORY","number of categories:" + data[data.length-1]); break; case (byte) 0x87: categories.add(Arrays.copyOfRange(data, 2, data.length)); break; } } this.set7F63(categories); } catch (IOException e) { Log.e(getClass().getName(), e.getMessage(), e); } }
/** * Produce an object suitable for an ASN1OutputStream. * <pre> * KEKIdentifier ::= SEQUENCE { * keyIdentifier OCTET STRING, * date GeneralizedTime OPTIONAL, * other OtherKeyAttribute OPTIONAL * } * </pre> */ public ASN1Primitive toASN1Primitive() { ASN1EncodableVector v = new ASN1EncodableVector(); v.add(keyIdentifier); if (date != null) { v.add(date); } if (other != null) { v.add(other); } return new DERSequence(v); }
/** * <pre> * PollRepContent ::= SEQUENCE OF SEQUENCE { * certReqId INTEGER, * checkAfter INTEGER, -- time in seconds * reason PKIFreeText OPTIONAL * } * </pre> * @return a basic ASN.1 object representation. */ public ASN1Primitive toASN1Primitive() { ASN1EncodableVector outer = new ASN1EncodableVector(); for (int i = 0; i != certReqId.length; i++) { ASN1EncodableVector v = new ASN1EncodableVector(); v.add(certReqId[i]); v.add(checkAfter[i]); if (reason[i] != null) { v.add(reason[i]); } outer.add(new DERSequence(v)); } return new DERSequence(outer); }
public static Asn1CreateSecretKeyParams getInstance(Object obj) throws BadAsn1ObjectException { if (obj == null || obj instanceof Asn1CreateSecretKeyParams) { return (Asn1CreateSecretKeyParams) obj; } try { if (obj instanceof ASN1Sequence) { return new Asn1CreateSecretKeyParams((ASN1Sequence) obj); } else if (obj instanceof byte[]) { return getInstance(ASN1Primitive.fromByteArray((byte[]) obj)); } else { throw new BadAsn1ObjectException("unknown object: " + obj.getClass().getName()); } } catch (IOException | IllegalArgumentException ex) { throw new BadAsn1ObjectException("unable to parse encoded object: " + ex.getMessage(), ex); } }
public static String canonicalize(String s) { String value = Strings.toLowerCase(s.trim()); if (value.length() > 0 && value.charAt(0) == '#') { ASN1Primitive obj = decodeObject(value); if (obj instanceof ASN1String) { value = Strings.toLowerCase(((ASN1String)obj).getString().trim()); } } value = stripInternalSpaces(value); return value; }
public static Asn1P11Params getInstance(Object obj) throws BadAsn1ObjectException { if (obj == null || obj instanceof Asn1P11Params) { return (Asn1P11Params) obj; } try { if (obj instanceof ASN1TaggedObject) { return new Asn1P11Params((ASN1TaggedObject) obj); } else if (obj instanceof byte[]) { return getInstance(ASN1Primitive.fromByteArray((byte[]) obj)); } else { throw new BadAsn1ObjectException("unknown object: " + obj.getClass().getName()); } } catch (IOException | IllegalArgumentException ex) { throw new BadAsn1ObjectException("unable to parse encoded object: " + ex.getMessage(), ex); } }
/** * Produce an object suitable for an ASN1OutputStream. * <pre> * Curve ::= SEQUENCE { * a FieldElement, * b FieldElement, * seed BIT STRING OPTIONAL * } * </pre> */ public ASN1Primitive toASN1Primitive() { ASN1EncodableVector v = new ASN1EncodableVector(); if (fieldIdentifier.equals(prime_field)) { v.add(new X9FieldElement(curve.getA()).toASN1Primitive()); v.add(new X9FieldElement(curve.getB()).toASN1Primitive()); } else if (fieldIdentifier.equals(characteristic_two_field)) { v.add(new X9FieldElement(curve.getA()).toASN1Primitive()); v.add(new X9FieldElement(curve.getB()).toASN1Primitive()); } if (seed != null) { v.add(new DERBitString(seed)); } return new DERSequence(v); }
private OtherRevRefs(ASN1Sequence seq) { if (seq.size() != 2) { throw new IllegalArgumentException("Bad sequence size: " + seq.size()); } this.otherRevRefType = new ASN1ObjectIdentifier(((ASN1ObjectIdentifier)seq.getObjectAt(0)).getId()); try { this.otherRevRefs = ASN1Primitive.fromByteArray(seq.getObjectAt(1) .toASN1Primitive().getEncoded(ASN1Encoding.DER)); } catch (IOException e) { throw new IllegalStateException(); } }
/** * Return the certificates stored in the underlying OriginatorInfo object. * * @return a Store of X509CertificateHolder objects. */ public Store getCertificates() { ASN1Set certSet = originatorInfo.getCertificates(); if (certSet != null) { List certList = new ArrayList(certSet.size()); for (Enumeration en = certSet.getObjects(); en.hasMoreElements();) { ASN1Primitive obj = ((ASN1Encodable)en.nextElement()).toASN1Primitive(); if (obj instanceof ASN1Sequence) { certList.add(new X509CertificateHolder(Certificate.getInstance(obj))); } } return new CollectionStore(certList); } return new CollectionStore(new ArrayList()); }
public Object parseObject(PemObject obj) throws IOException { try { PrivateKeyInfo info = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(obj.getContent())); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(obj.getContent()); KeyFactory keyFact = KeyFactory.getInstance(info.getPrivateKeyAlgorithm().getAlgorithm().getId(), provider); return keyFact.generatePrivate(keySpec); } catch (Exception e) { throw new PEMException("problem parsing PRIVATE KEY: " + e.toString(), e); } }
/** * @deprecated use KeyPurposeId[] constructor. */ public ExtendedKeyUsage( Vector usages) { ASN1EncodableVector v = new ASN1EncodableVector(); Enumeration e = usages.elements(); while (e.hasMoreElements()) { ASN1Primitive o = (ASN1Primitive)e.nextElement(); v.add(o); this.usageTable.put(o, o); } this.seq = new DERSequence(v); }
/** * return the subject of the given cert as an X509PrincipalObject. */ public static X509Principal getSubjectX509Principal( X509Certificate cert) throws CertificateEncodingException { try { TBSCertificateStructure tbsCert = TBSCertificateStructure.getInstance( ASN1Primitive.fromByteArray(cert.getTBSCertificate())); return new X509Principal(X509Name.getInstance(tbsCert.getSubject())); } catch (IOException e) { throw new CertificateEncodingException(e.toString()); } }
public static Asn1ServerCaps getInstance(Object obj) throws BadAsn1ObjectException { if (obj == null || obj instanceof Asn1ServerCaps) { return (Asn1ServerCaps) obj; } try { if (obj instanceof ASN1Sequence) { return new Asn1ServerCaps((ASN1Sequence) obj); } else if (obj instanceof byte[]) { return getInstance(ASN1Primitive.fromByteArray((byte[]) obj)); } else { throw new BadAsn1ObjectException("unknown object: " + obj.getClass().getName()); } } catch (IOException | IllegalArgumentException ex) { throw new BadAsn1ObjectException("unable to parse encoded object: " + ex.getMessage(), ex); } }
/** * Produce an object suitable for an ASN1OutputStream. * <pre> * OriginatorInfo ::= SEQUENCE { * certs [0] IMPLICIT CertificateSet OPTIONAL, * crls [1] IMPLICIT CertificateRevocationLists OPTIONAL * } * </pre> */ public ASN1Primitive toASN1Primitive() { ASN1EncodableVector v = new ASN1EncodableVector(); if (certs != null) { v.add(new DERTaggedObject(false, 0, certs)); } if (crls != null) { v.add(new DERTaggedObject(false, 1, crls)); } return new DERSequence(v); }
public ASN1Primitive toASN1Primitive() { ASN1EncodableVector seqOfPSh = new ASN1EncodableVector(); ASN1EncodableVector seqOfPSw = new ASN1EncodableVector(); ASN1EncodableVector seqOfPSK = new ASN1EncodableVector(); for (int i = 0; i < h.length; i++) { seqOfPSh.add(new ASN1Integer(h[i])); seqOfPSw.add(new ASN1Integer(w[i])); seqOfPSK.add(new ASN1Integer(k[i])); } ASN1EncodableVector v = new ASN1EncodableVector(); v.add(new ASN1Integer(t)); v.add(new DERSequence(seqOfPSh)); v.add(new DERSequence(seqOfPSw)); v.add(new DERSequence(seqOfPSK)); return new DERSequence(v); }
private void setPrivateData(ASN1InputStream cha) throws IOException { ASN1Primitive obj; obj = cha.readObject(); if (obj instanceof ASN1ObjectIdentifier) { this.oid = (ASN1ObjectIdentifier)obj; } else { throw new IllegalArgumentException("no Oid in CerticateHolderAuthorization"); } obj = cha.readObject(); if (obj instanceof DERApplicationSpecific) { this.accessRights = (DERApplicationSpecific)obj; } else { throw new IllegalArgumentException("No access rights in CerticateHolderAuthorization"); } }
public static Asn1RemoveObjectsParams getInstance(Object obj) throws BadAsn1ObjectException { if (obj == null || obj instanceof Asn1RemoveObjectsParams) { return (Asn1RemoveObjectsParams) obj; } try { if (obj instanceof ASN1Sequence) { return new Asn1RemoveObjectsParams((ASN1Sequence) obj); } else if (obj instanceof byte[]) { return getInstance(ASN1Primitive.fromByteArray((byte[]) obj)); } else { throw new BadAsn1ObjectException("unknown object: " + obj.getClass().getName()); } } catch (IOException | IllegalArgumentException ex) { throw new BadAsn1ObjectException("unable to parse encoded object: " + ex.getMessage(), ex); } }
/** * @see org.bouncycastle.asn1.ASN1Object#toASN1Primitive() */ public ASN1Primitive toASN1Primitive() { ASN1EncodableVector v = new ASN1EncodableVector(); if (valid != (signValid | bodyValid)) { return null; } v.add(certificateBody); try { v.add(new DERApplicationSpecific(false, EACTags.STATIC_INTERNAL_AUTHENTIFICATION_ONE_STEP, new DEROctetString(signature))); } catch (IOException e) { throw new IllegalStateException("unable to convert signature!"); } return new DERApplicationSpecific(EACTags.CARDHOLDER_CERTIFICATE, v); }
private ASN1Primitive getObjectInTag(int tagNo) { Enumeration e = seq.getObjects(); while (e.hasMoreElements()) { ASN1Encodable obj = (ASN1Encodable)e.nextElement(); if (obj instanceof ASN1TaggedObject) { ASN1TaggedObject tag = (ASN1TaggedObject)obj; if (tag.getTagNo() == tagNo) { return (ASN1Primitive)((ASN1Encodable)tag.getObject()).toASN1Primitive(); } } } return null; }
public ASN1Primitive toASN1Primitive() { ASN1EncodableVector v = new ASN1EncodableVector(); if (namedCurve != null) { v.add(namedCurve); } else { v.add(ecbinary); } if (!org.bouncycastle.util.Arrays.areEqual(dke, DEFAULT_DKE)) { v.add(new DEROctetString(dke)); } return new DERSequence(v); }
public ASN1Primitive toASN1Primitive() { ASN1EncodableVector v = new ASN1EncodableVector(); v.add(algId); v.add(new DEROctetString(digest)); return new DERSequence(v); }
AlgorithmIdentifier getAlgorithmIdentifier(ASN1ObjectIdentifier encryptionOID, AlgorithmParameters params) throws CMSException { ASN1Encodable asn1Params; if (params != null) { try { asn1Params = ASN1Primitive.fromByteArray(params.getEncoded("ASN.1")); } catch (IOException e) { throw new CMSException("cannot encode parameters: " + e.getMessage(), e); } } else { asn1Params = DERNull.INSTANCE; } return new AlgorithmIdentifier( encryptionOID, asn1Params); }
/** * Produce an object suitable for an ASN1OutputStream. * <p/> * Returns: * <p/> * <pre> * NamingAuthority ::= SEQUENCE * { * namingAuthorityId OBJECT IDENTIFIER OPTIONAL, * namingAuthorityUrl IA5String OPTIONAL, * namingAuthorityText DirectoryString(SIZE(1..128)) OPTIONAL * } * </pre> * * @return a DERObject */ public ASN1Primitive toASN1Primitive() { ASN1EncodableVector vec = new ASN1EncodableVector(); if (namingAuthorityId != null) { vec.add(namingAuthorityId); } if (namingAuthorityUrl != null) { vec.add(new DERIA5String(namingAuthorityUrl, true)); } if (namingAuthorityText != null) { vec.add(namingAuthorityText); } return new DERSequence(vec); }
private void initFrom(ASN1InputStream aIS) throws IOException { ASN1Primitive obj; while ((obj = aIS.readObject()) != null) { if (obj instanceof DERApplicationSpecific) { setPrivateData((DERApplicationSpecific)obj); } else { throw new IOException("Invalid Input Stream for creating an Iso7816CertificateStructure"); } } }
/** * <pre> * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension * * Extension ::= SEQUENCE { * extnId EXTENSION.&id ({ExtensionSet}), * critical BOOLEAN DEFAULT FALSE, * extnValue OCTET STRING } * </pre> */ public ASN1Primitive toASN1Primitive() { ASN1EncodableVector vec = new ASN1EncodableVector(); Enumeration e = ordering.elements(); while (e.hasMoreElements()) { ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)e.nextElement(); Extension ext = (Extension)extensions.get(oid); vec.add(ext); } return new DERSequence(vec); }
/** * Apply default coversion for the given value depending on the oid * and the character range of the value. * * @param oid the object identifier for the DN entry * @param value the value associated with it * @return the ASN.1 equivalent for the string value. */ public ASN1Primitive getConvertedValue( ASN1ObjectIdentifier oid, String value) { if (value.length() != 0 && value.charAt(0) == '#') { try { return convertHexEncoded(value, 1); } catch (IOException e) { throw new RuntimeException("can't recode value for oid " + oid.getId()); } } else { if (value.length() != 0 && value.charAt(0) == '\\') { value = value.substring(1); } if (oid.equals(X509Name.EmailAddress) || oid.equals(X509Name.DC)) { return new DERIA5String(value); } else if (oid.equals(X509Name.DATE_OF_BIRTH)) // accept time string as well as # (for compatibility) { return new DERGeneralizedTime(value); } else if (oid.equals(X509Name.C) || oid.equals(X509Name.SN) || oid.equals(X509Name.DN_QUALIFIER) || oid.equals(X509Name.TELEPHONE_NUMBER)) { return new DERPrintableString(value); } } return new DERUTF8String(value); }
private void parseDG15(byte[] DG15) { try (ASN1InputStream bIn = new ASN1InputStream(DG15)) { DERApplicationSpecific app = (DERApplicationSpecific) bIn.readObject(); ASN1Sequence seq = (ASN1Sequence) app.getObject(BERTags.SEQUENCE); byte[] data = ((ASN1Primitive)seq.getObjects().nextElement()).getEncoded(); Log.d(getClass().getName(), "Data = "+ Bytes.hexString(data)); try (ASN1InputStream in = new ASN1InputStream(data)) { Enumeration seq1 = ((DLSequence) in.readObject()).getObjects(); while (seq1.hasMoreElements()) { ASN1Primitive obj = (ASN1Primitive)seq1.nextElement(); byte[] data1 = obj.getEncoded(); Log.d(getClass().getName(), "data1 = "+ Bytes.hexString(data1)); if (data1[0] == (byte) 0x01) { this.set18(data1[2] == 0x01); } else if (data1[0] == (byte) 0x02) { this.setAge(Bytes.toInt(data1[2])); } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override public ASN1Primitive toASN1Primitive() { ASN1EncodableVector vecVersions = new ASN1EncodableVector(); for (Short version : versions) { vecVersions.add(new ASN1Integer(BigInteger.valueOf(version))); } ASN1EncodableVector vec = new ASN1EncodableVector(); vec.add(ASN1Boolean.getInstance(readOnly)); vec.add(new DERSequence(vecVersions)); return new DERSequence(vec); }
private DERBitString getPublicKeyDetails(BCECGOST3410PublicKey pub) { try { SubjectPublicKeyInfo info = SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(pub.getEncoded())); return info.getPublicKeyData(); } catch (IOException e) { // should never happen return null; } }
/** * Produce an object suitable for an ASN1OutputStream. * <pre> * ContentInfo ::= SEQUENCE { * contentType ContentType, * content * [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL } * </pre> */ public ASN1Primitive toASN1Primitive() { ASN1EncodableVector v = new ASN1EncodableVector(); v.add(contentType); if (content != null) { v.add(new BERTaggedObject(0, content)); } return new BERSequence(v); }
public ASN1Primitive toASN1Primitive() { ASN1EncodableVector v = new ASN1EncodableVector(); v.add(version); v.add(subject); v.add(subjectPKInfo); if (attributes != null) { v.add(new DERTaggedObject(false, 0, attributes)); } return new DERSequence(v); }
/** * Produce an object suitable for an ASN1OutputStream. * <pre> * ResponderID ::= CHOICE { * byName [1] Name, * byKey [2] KeyHash } * </pre> */ public ASN1Primitive toASN1Primitive() { if (value instanceof ASN1OctetString) { return new DERTaggedObject(true, 2, value); } return new DERTaggedObject(true, 1, value); }
private ASN1Primitive getSingleValuedSignedAttribute( ASN1ObjectIdentifier attrOID, String printableName) throws CMSException { AttributeTable unsignedAttrTable = this.getUnsignedAttributes(); if (unsignedAttrTable != null && unsignedAttrTable.getAll(attrOID).size() > 0) { throw new CMSException("The " + printableName + " attribute MUST NOT be an unsigned attribute"); } AttributeTable signedAttrTable = this.getSignedAttributes(); if (signedAttrTable == null) { return null; } ASN1EncodableVector v = signedAttrTable.getAll(attrOID); switch (v.size()) { case 0: return null; case 1: { Attribute t = (Attribute)v.get(0); ASN1Set attrValues = t.getAttrValues(); if (attrValues.size() != 1) { throw new CMSException("A " + printableName + " attribute MUST have a single attribute value"); } return attrValues.getObjectAt(0).toASN1Primitive(); } default: throw new CMSException("The SignedAttributes in a signerInfo MUST NOT include multiple instances of the " + printableName + " attribute"); } }
private SubjectKeyIdentifier createSubjectKeyId( PublicKey pubKey) { try { SubjectPublicKeyInfo info = new SubjectPublicKeyInfo( (ASN1Sequence)ASN1Primitive.fromByteArray(pubKey.getEncoded())); return new SubjectKeyIdentifier(info); } catch (Exception e) { throw new RuntimeException("error creating key"); } }