Search in sources :

Example 86 with GeneralName

use of org.apache.harmony.security.x509.GeneralName in project ddf by codice.

the class CertificateSigningRequestTest method testNewCertificateBuilderWithSan.

@Test
public void testNewCertificateBuilderWithSan() throws Exception {
    final DateTime start = DateTime.now().minusDays(1);
    final DateTime end = start.plusYears(100);
    final KeyPair kp = makeKeyPair();
    csr.setSerialNumber(1);
    csr.setNotBefore(start);
    csr.setNotAfter(end);
    csr.setCommonName("A");
    csr.setSubjectKeyPair(kp);
    csr.addSubjectAlternativeNames("IP:1.2.3.4", "DNS:A");
    final X509Certificate issuerCert = mock(X509Certificate.class);
    doReturn(new X500Principal("CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US")).when(issuerCert).getSubjectX500Principal();
    final JcaX509v3CertificateBuilder builder = csr.newCertificateBuilder(issuerCert);
    final X509CertificateHolder holder = builder.build(new DemoCertificateAuthority().getContentSigner());
    assertThat(holder.getSerialNumber(), equalTo(BigInteger.ONE));
    assertThat(holder.getNotBefore(), equalTo(new Time(start.toDate()).getDate()));
    assertThat(holder.getNotAfter(), equalTo(new Time(end.toDate()).getDate()));
    assertThat(holder.getSubject().toString(), equalTo("cn=A"));
    assertThat("Unable to validate public key", holder.getSubjectPublicKeyInfo(), equalTo(SubjectPublicKeyInfo.getInstance(kp.getPublic().getEncoded())));
    final org.bouncycastle.asn1.x509.Extension csn = holder.getExtension(org.bouncycastle.asn1.x509.Extension.subjectAlternativeName);
    assertThat(csn.getParsedValue().toASN1Primitive().getEncoded(ASN1Encoding.DER), equalTo(new GeneralNamesBuilder().addName(new GeneralName(GeneralName.iPAddress, "1.2.3.4")).addName(new GeneralName(GeneralName.dNSName, "A")).build().getEncoded(ASN1Encoding.DER)));
}
Also used : KeyPair(java.security.KeyPair) Time(org.bouncycastle.asn1.x509.Time) DateTime(org.joda.time.DateTime) DateTime(org.joda.time.DateTime) X509Certificate(java.security.cert.X509Certificate) GeneralNamesBuilder(org.bouncycastle.asn1.x509.GeneralNamesBuilder) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) X500Principal(javax.security.auth.x500.X500Principal) GeneralName(org.bouncycastle.asn1.x509.GeneralName) Test(org.junit.Test)

Example 87 with GeneralName

use of org.apache.harmony.security.x509.GeneralName in project ddf by codice.

the class PkiToolsTest method testMakeGeneralNameForEmail.

@Test
public void testMakeGeneralNameForEmail() {
    final String value = "a@host.com";
    final GeneralName gname = PkiTools.makeGeneralName("email:" + value);
    assertThat(gname.getTagNo(), equalTo(GeneralName.rfc822Name));
    assertThat(gname.getName().toString(), equalTo(value));
}
Also used : GeneralName(org.bouncycastle.asn1.x509.GeneralName) Test(org.junit.Test)

Example 88 with GeneralName

use of org.apache.harmony.security.x509.GeneralName in project ddf by codice.

the class PkiToolsTest method testMakeGeneralNameForRID.

@Test
public void testMakeGeneralNameForRID() {
    final String value = "0.2.1.4";
    final GeneralName gname = PkiTools.makeGeneralName("RID:" + value);
    assertThat(gname.getTagNo(), equalTo(GeneralName.registeredID));
    assertThat(gname.getName().toString(), equalTo(value));
}
Also used : GeneralName(org.bouncycastle.asn1.x509.GeneralName) Test(org.junit.Test)

Example 89 with GeneralName

use of org.apache.harmony.security.x509.GeneralName in project ddf by codice.

the class PkiToolsTest method testMakeGeneralNameForDirName.

@Test
public void testMakeGeneralNameForDirName() {
    final String value = "C=UK+CN=My Name+OU=My Unit+O=My Organization";
    final GeneralName gname = PkiTools.makeGeneralName("dirName:" + value);
    assertThat(gname.getTagNo(), equalTo(GeneralName.directoryName));
    assertThat(gname.getName().toString(), equalTo(value));
}
Also used : GeneralName(org.bouncycastle.asn1.x509.GeneralName) Test(org.junit.Test)

Example 90 with GeneralName

use of org.apache.harmony.security.x509.GeneralName in project robovm by robovm.

the class AuthorityKeyIdentifierStructure method fromCertificate.

private static ASN1Sequence fromCertificate(X509Certificate certificate) throws CertificateParsingException {
    try {
        if (certificate.getVersion() != 3) {
            GeneralName genName = new GeneralName(PrincipalUtil.getIssuerX509Principal(certificate));
            SubjectPublicKeyInfo info = new SubjectPublicKeyInfo((ASN1Sequence) new ASN1InputStream(certificate.getPublicKey().getEncoded()).readObject());
            return (ASN1Sequence) new AuthorityKeyIdentifier(info, new GeneralNames(genName), certificate.getSerialNumber()).toASN1Object();
        } else {
            GeneralName genName = new GeneralName(PrincipalUtil.getIssuerX509Principal(certificate));
            byte[] ext = certificate.getExtensionValue(X509Extensions.SubjectKeyIdentifier.getId());
            if (ext != null) {
                ASN1OctetString str = (ASN1OctetString) X509ExtensionUtil.fromExtensionValue(ext);
                return (ASN1Sequence) new AuthorityKeyIdentifier(str.getOctets(), new GeneralNames(genName), certificate.getSerialNumber()).toASN1Object();
            } else {
                SubjectPublicKeyInfo info = new SubjectPublicKeyInfo((ASN1Sequence) new ASN1InputStream(certificate.getPublicKey().getEncoded()).readObject());
                return (ASN1Sequence) new AuthorityKeyIdentifier(info, new GeneralNames(genName), certificate.getSerialNumber()).toASN1Object();
            }
        }
    } catch (Exception e) {
        throw new CertificateParsingException("Exception extracting certificate details: " + e.toString());
    }
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) CertificateParsingException(java.security.cert.CertificateParsingException) GeneralNames(org.bouncycastle.asn1.x509.GeneralNames) AuthorityKeyIdentifier(org.bouncycastle.asn1.x509.AuthorityKeyIdentifier) GeneralName(org.bouncycastle.asn1.x509.GeneralName) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) CertificateParsingException(java.security.cert.CertificateParsingException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

GeneralName (org.bouncycastle.asn1.x509.GeneralName)144 GeneralNames (org.bouncycastle.asn1.x509.GeneralNames)67 IOException (java.io.IOException)54 ArrayList (java.util.ArrayList)40 DERIA5String (org.bouncycastle.asn1.DERIA5String)39 X500Name (org.bouncycastle.asn1.x500.X500Name)34 CRLDistPoint (org.bouncycastle.asn1.x509.CRLDistPoint)34 DistributionPoint (org.bouncycastle.asn1.x509.DistributionPoint)33 X509Certificate (java.security.cert.X509Certificate)32 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)28 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)27 DEROctetString (org.bouncycastle.asn1.DEROctetString)25 List (java.util.List)21 BigInteger (java.math.BigInteger)20 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)20 GeneralName (org.apache.harmony.security.x509.GeneralName)18 DERSequence (org.bouncycastle.asn1.DERSequence)18 DirectoryString (org.bouncycastle.asn1.x500.DirectoryString)18 BasicConstraints (org.bouncycastle.asn1.x509.BasicConstraints)18 JcaX509CertificateConverter (org.bouncycastle.cert.jcajce.JcaX509CertificateConverter)18