private TimeStampedData(ASN1Sequence seq) { this.version = ASN1Integer.getInstance(seq.getObjectAt(0)); int index = 1; if (seq.getObjectAt(index) instanceof DERIA5String) { this.dataUri = DERIA5String.getInstance(seq.getObjectAt(index++)); } if (seq.getObjectAt(index) instanceof MetaData || seq.getObjectAt(index) instanceof ASN1Sequence) { this.metaData = MetaData.getInstance(seq.getObjectAt(index++)); } if (seq.getObjectAt(index) instanceof ASN1OctetString) { this.content = ASN1OctetString.getInstance(seq.getObjectAt(index++)); } this.temporalEvidence = Evidence.getInstance(seq.getObjectAt(index)); }
protected Vector getCRLDistUrls(CRLDistPoint crlDistPoints) { Vector urls = new Vector(); if (crlDistPoints != null) { DistributionPoint[] distPoints = crlDistPoints.getDistributionPoints(); for (int i = 0; i < distPoints.length; i++) { DistributionPointName dp_name = distPoints[i].getDistributionPoint(); if (dp_name.getType() == DistributionPointName.FULL_NAME) { GeneralName[] generalNames = GeneralNames.getInstance(dp_name.getName()).getNames(); for (int j = 0; j < generalNames.length; j++) { if (generalNames[j].getTagNo() == GeneralName.uniformResourceIdentifier) { String url = ((DERIA5String) generalNames[j].getName()).getString(); urls.add(url); } } } } } return urls; }
protected Vector getOCSPUrls(AuthorityInformationAccess authInfoAccess) { Vector urls = new Vector(); if (authInfoAccess != null) { AccessDescription[] ads = authInfoAccess.getAccessDescriptions(); for (int i = 0; i < ads.length; i++) { if (ads[i].getAccessMethod().equals(AccessDescription.id_ad_ocsp)) { GeneralName name = ads[i].getAccessLocation(); if (name.getTagNo() == GeneralName.uniformResourceIdentifier) { String url = ((DERIA5String) name.getName()).getString(); urls.add(url); } } } } return urls; }
public NetscapeCertRequest( String challenge, AlgorithmIdentifier signing_alg, PublicKey pub_key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException { this.challenge = challenge; sigAlg = signing_alg; pubkey = pub_key; ASN1EncodableVector content_der = new ASN1EncodableVector(); content_der.add(getKeySpec()); //content_der.add(new SubjectPublicKeyInfo(sigAlg, new RSAPublicKeyStructure(pubkey.getModulus(), pubkey.getPublicExponent()).getDERObject())); content_der.add(new DERIA5String(challenge)); try { content = new DERBitString(new DERSequence(content_der)); } catch (IOException e) { throw new InvalidKeySpecException("exception encoding key: " + e.toString()); } }
public ASN1Primitive toASN1Primitive() { ASN1EncodableVector spkac = new ASN1EncodableVector(); ASN1EncodableVector pkac = new ASN1EncodableVector(); try { pkac.add(getKeySpec()); } catch (Exception e) { //ignore } pkac.add(new DERIA5String(challenge)); spkac.add(new DERSequence(pkac)); spkac.add(sigAlg); spkac.add(new DERBitString(sigBits)); return new DERSequence(spkac); }
public String toString() { StringBuffer buf = new StringBuffer(); buf.append(tag); buf.append(": "); switch (tag) { case rfc822Name: case dNSName: case uniformResourceIdentifier: buf.append(DERIA5String.getInstance(obj).getString()); break; case directoryName: buf.append(X500Name.getInstance(obj).toString()); break; default: buf.append(obj.toString()); } return buf.toString(); }
/** * 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 TimeStampedDataParser(ASN1SequenceParser parser) throws IOException { this.parser = parser; this.version = ASN1Integer.getInstance(parser.readObject()); ASN1Encodable obj = parser.readObject(); if (obj instanceof DERIA5String) { this.dataUri = DERIA5String.getInstance(obj); obj = parser.readObject(); } if (obj instanceof MetaData || obj instanceof ASN1SequenceParser) { this.metaData = MetaData.getInstance(obj.toASN1Primitive()); obj = parser.readObject(); } if (obj instanceof ASN1OctetStringParser) { this.content = (ASN1OctetStringParser)obj; } }
private MetaData(ASN1Sequence seq) { this.hashProtected = ASN1Boolean.getInstance(seq.getObjectAt(0)); int index = 1; if (index < seq.size() && seq.getObjectAt(index) instanceof DERUTF8String) { this.fileName = DERUTF8String.getInstance(seq.getObjectAt(index++)); } if (index < seq.size() && seq.getObjectAt(index) instanceof DERIA5String) { this.mediaType = DERIA5String.getInstance(seq.getObjectAt(index++)); } if (index < seq.size()) { this.otherMetaData = Attributes.getInstance(seq.getObjectAt(index++)); } }
private CrlID( ASN1Sequence seq) { Enumeration e = seq.getObjects(); while (e.hasMoreElements()) { ASN1TaggedObject o = (ASN1TaggedObject)e.nextElement(); switch (o.getTagNo()) { case 0: crlUrl = DERIA5String.getInstance(o, true); break; case 1: crlNum = ASN1Integer.getInstance(o, true); break; case 2: crlTime = DERGeneralizedTime.getInstance(o, true); break; default: throw new IllegalArgumentException( "unknown tag number: " + o.getTagNo()); } } }
private static Pair<String, String> parseOtherName(byte[] otherName) { try { ASN1Primitive asn1Primitive = ASN1Primitive.fromByteArray(otherName); if (asn1Primitive instanceof DERTaggedObject) { ASN1Primitive inner = ((DERTaggedObject) asn1Primitive).getObject(); if (inner instanceof DLSequence) { DLSequence sequence = (DLSequence) inner; if (sequence.size() >= 2 && sequence.getObjectAt(1) instanceof DERTaggedObject) { String oid = sequence.getObjectAt(0).toString(); ASN1Primitive value = ((DERTaggedObject) sequence.getObjectAt(1)).getObject(); if (value instanceof DERUTF8String) { return new Pair<>(oid, ((DERUTF8String) value).getString()); } else if (value instanceof DERIA5String) { return new Pair<>(oid, ((DERIA5String) value).getString()); } } } } return null; } catch (IOException e) { return null; } }
/** * Check if the given GeneralName is contained in the excluded set. * * @param name The GeneralName. * @throws NameConstraintValidatorException If the <code>name</code> is * excluded. */ public void checkExcluded(GeneralName name) throws NameConstraintValidatorException { switch (name.getTagNo()) { case GeneralName.rfc822Name: checkExcludedEmail(excludedSubtreesEmail, extractNameAsString(name)); break; case GeneralName.dNSName: checkExcludedDNS(excludedSubtreesDNS, DERIA5String.getInstance( name.getName()).getString()); break; case GeneralName.directoryName: checkExcludedDN(X500Name.getInstance(name.getName())); break; case GeneralName.uniformResourceIdentifier: checkExcludedURI(excludedSubtreesURI, DERIA5String.getInstance( name.getName()).getString()); break; case GeneralName.iPAddress: byte[] ip = ASN1OctetString.getInstance(name.getName()).getOctets(); checkExcludedIP(excludedSubtreesIP, ip); } }
private BiometricData(ASN1Sequence seq) { Enumeration e = seq.getObjects(); // typeOfBiometricData typeOfBiometricData = TypeOfBiometricData.getInstance(e.nextElement()); // hashAlgorithm hashAlgorithm = AlgorithmIdentifier.getInstance(e.nextElement()); // biometricDataHash biometricDataHash = ASN1OctetString.getInstance(e.nextElement()); // sourceDataUri if (e.hasMoreElements()) { sourceDataUri = DERIA5String.getInstance(e.nextElement()); } }
/** * Produce an object suitable for an ASN1OutputStream. * <p> * Returns: * <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); }
protected ASN1Encodable encodeStringValue(ASN1ObjectIdentifier oid, String value) { if (oid.equals(EmailAddress) || oid.equals(DC)) { return new DERIA5String(value); } else if (oid.equals(DATE_OF_BIRTH)) // accept time string as well as # (for compatibility) { return new ASN1GeneralizedTime(value); } else if (oid.equals(C) || oid.equals(SN) || oid.equals(DN_QUALIFIER) || oid.equals(TELEPHONE_NUMBER)) { return new DERPrintableString(value); } return super.encodeStringValue(oid, value); }
private CrlID( ASN1Sequence seq) { Enumeration e = seq.getObjects(); while (e.hasMoreElements()) { ASN1TaggedObject o = (ASN1TaggedObject)e.nextElement(); switch (o.getTagNo()) { case 0: crlUrl = DERIA5String.getInstance(o, true); break; case 1: crlNum = ASN1Integer.getInstance(o, true); break; case 2: crlTime = ASN1GeneralizedTime.getInstance(o, true); break; default: throw new IllegalArgumentException( "unknown tag number: " + o.getTagNo()); } } }
/** * Returns the AuthorityInfoAccess extension value on list format.<br> * Otherwise, returns <b>list empty</b>.<br> * @return List Authority info access list */ public List<String> getAuthorityInfoAccess() { List<String> address = new ArrayList<String>(); try { byte[] authorityInfoAccess = certificate.getExtensionValue(Extension.authorityInfoAccess.getId()); if (authorityInfoAccess != null && authorityInfoAccess.length > 0) { AuthorityInformationAccess infoAccess = AuthorityInformationAccess.getInstance(X509ExtensionUtil .fromExtensionValue(authorityInfoAccess)); for (AccessDescription desc : infoAccess.getAccessDescriptions()) if (desc.getAccessLocation().getTagNo() == GeneralName.uniformResourceIdentifier) address.add(((DERIA5String) desc.getAccessLocation().getName()).getString()); } return address; } catch (IOException error) { logger.info(error.getMessage()); return address; } }
private static OtherName parseOtherName(byte[] otherName) { try { ASN1Primitive asn1Primitive = ASN1Primitive.fromByteArray(otherName); if (asn1Primitive instanceof DERTaggedObject) { ASN1Primitive inner = ((DERTaggedObject) asn1Primitive).getObject(); if (inner instanceof DLSequence) { DLSequence sequence = (DLSequence) inner; if (sequence.size() >= 2 && sequence.getObjectAt(1) instanceof DERTaggedObject) { String oid = sequence.getObjectAt(0).toString(); ASN1Primitive value = ((DERTaggedObject) sequence.getObjectAt(1)).getObject(); if (value instanceof DERUTF8String) { return new OtherName(oid, ((DERUTF8String) value).getString()); } else if (value instanceof DERIA5String) { return new OtherName(oid, ((DERIA5String) value).getString()); } } } } return null; } catch (IOException e) { return null; } }
public static List<String> extractX509CSRDnsNames(PKCS10CertificationRequest certReq) { List<String> dnsNames = new ArrayList<>(); Attribute[] attributes = certReq.getAttributes(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest); for (Attribute attribute : attributes) { for (ASN1Encodable value : attribute.getAttributeValues()) { Extensions extensions = Extensions.getInstance(value); GeneralNames gns = GeneralNames.fromExtensions(extensions, Extension.subjectAlternativeName); for (GeneralName name : gns.getNames()) { if (name.getTagNo() == GeneralName.dNSName) { dnsNames.add(((DERIA5String) name.getName()).getString()); } } } } return dnsNames; }
@Test(dataProvider = "x500Principal") public void testX509CSRrequest(String x500Principal, boolean badRequest) throws Exception{ PublicKey publicKey = Crypto.loadPublicKey(rsaPublicKey); PrivateKey privateKey = Crypto.loadPrivateKey(rsaPrivateKey); String certRequest = null; GeneralName otherName1 = new GeneralName(GeneralName.otherName, new DERIA5String("role1")); GeneralName otherName2 = new GeneralName(GeneralName.otherName, new DERIA5String("role2")); GeneralName[] sanArray = new GeneralName[]{otherName1, otherName2}; try { certRequest = Crypto.generateX509CSR(privateKey, publicKey, x500Principal, sanArray); } catch (Exception e){ if (!badRequest){ fail("Should not have failed to create csr"); } } if (!badRequest){ //Now validate the csr Crypto.getPKCS10CertRequest(certRequest); } }
@Test(dataProvider = "x500Principal") public void testX509CSRrequestWithPrivateKeyOnly(String x500Principal, boolean badRequest) throws Exception { PrivateKey privateKey = Crypto.loadPrivateKey(rsaPrivateKey); String certRequest = null; GeneralName otherName1 = new GeneralName(GeneralName.otherName, new DERIA5String("role1")); GeneralName otherName2 = new GeneralName(GeneralName.otherName, new DERIA5String("role2")); GeneralName[] sanArray = new GeneralName[]{otherName1, otherName2}; try { certRequest = Crypto.generateX509CSR(privateKey, x500Principal, sanArray); } catch (Exception e){ if (!badRequest){ fail("Should not have failed to create csr"); } } if (!badRequest){ //Now validate the csr Crypto.getPKCS10CertRequest(certRequest); } }
public void sign(PrivateKey priv_key, SecureRandom rand) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, NoSuchProviderException, InvalidKeySpecException { Signature sig = Signature.getInstance(sigAlg.getAlgorithm().getId(), "BC"); if (rand != null) { sig.initSign(priv_key, rand); } else { sig.initSign(priv_key); } ASN1EncodableVector pkac = new ASN1EncodableVector(); pkac.add(getKeySpec()); pkac.add(new DERIA5String(challenge)); try { sig.update(new DERSequence(pkac).getEncoded(ASN1Encoding.DER)); } catch (IOException ioe) { throw new SignatureException(ioe.getMessage()); } sigBits = sig.sign(); }