Java Code Examples for javax.security.auth.x500.X500Principal#getName()

The following examples show how to use javax.security.auth.x500.X500Principal#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: X500PrincipalTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Inits X500Principal with byte array
 * gets Name in RFC1779 format
 * compares with expected value of name
 */
public void testStreamGetName_RFC1779() throws Exception {
    byte[] mess = { 0x30, (byte) 0x81, (byte) 0x9A, 0x31, 0x0A, 0x30, 0x08,
            0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x5A, 0x31, 0x0A,
            0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01, 0x45,
            0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
            0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04,
            0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
            0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30, 0x08,
            0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30, 0x09,
            0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41, 0x31,
            0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01,
            0x45, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06,
            0x13, 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
            0x04, 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06,
            0x03, 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30,
            0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30,
            0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41 };
    ByteArrayInputStream is = new ByteArrayInputStream(mess);
    X500Principal principal = new X500Principal(is);

    String s = principal.getName(X500Principal.RFC1779);
    assertEquals(
            "CN=A + ST=CA, O=B, L=C, C=D, OU=E, CN=A + ST=CA, O=B, L=C, C=D, OU=E, CN=Z",
            s);
}
 
Example 2
Source File: EscapedChars.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        String dn="CN=\\#user";
        X500Principal xp = new X500Principal(dn);

        System.out.println("RFC2253 DN is " +
            xp.getName(X500Principal.RFC2253));
        System.out.println("CANONICAL DN is is " +
            xp.getName(X500Principal.CANONICAL));

        String dn1 = xp.getName(X500Principal.CANONICAL);
        if (!(dn1.substring(3,5).equals("\\#")))
            throw new Exception("Leading # not escaped");

        X500Principal xp1 = new X500Principal(dn1);
        System.out.println("CANONICAL DN is " +
            xp1.getName(X500Principal.CANONICAL));
    }
 
Example 3
Source File: EscapedChars.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        String dn="CN=\\#user";
        X500Principal xp = new X500Principal(dn);

        System.out.println("RFC2253 DN is " +
            xp.getName(X500Principal.RFC2253));
        System.out.println("CANONICAL DN is is " +
            xp.getName(X500Principal.CANONICAL));

        String dn1 = xp.getName(X500Principal.CANONICAL);
        if (!(dn1.substring(3,5).equals("\\#")))
            throw new Exception("Leading # not escaped");

        X500Principal xp1 = new X500Principal(dn1);
        System.out.println("CANONICAL DN is " +
            xp1.getName(X500Principal.CANONICAL));
    }
 
Example 4
Source File: CertificateUtilTest.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCNException(@Mocked X500Principal aX500Principal,
    @Mocked MyX509Certificate myX509Certificate) {
  new Expectations() {
    {
      aX500Principal.getName();
      result = "NOCN=Test1234";
      myX509Certificate.getSubjectX500Principal();
      result = aX500Principal;
    }
  };

  MyX509Certificate xxmyX509Certificate = new MyX509Certificate();

  try {
    Set<String> strExpect = CertificateUtil.getCN(xxmyX509Certificate);
    Assert.assertEquals(strExpect.size(), 0);
  } catch (IllegalArgumentException e) {
    Assert.assertNotNull(null);
  }
}
 
Example 5
Source File: EscapedChars.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        String dn="CN=\\#user";
        X500Principal xp = new X500Principal(dn);

        System.out.println("RFC2253 DN is " +
            xp.getName(X500Principal.RFC2253));
        System.out.println("CANONICAL DN is is " +
            xp.getName(X500Principal.CANONICAL));

        String dn1 = xp.getName(X500Principal.CANONICAL);
        if (!(dn1.substring(3,5).equals("\\#")))
            throw new Exception("Leading # not escaped");

        X500Principal xp1 = new X500Principal(dn1);
        System.out.println("CANONICAL DN is " +
            xp1.getName(X500Principal.CANONICAL));
    }
 
Example 6
Source File: NameFormat.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void testName(String in, String outFormat,
                            String expect, int n)
    throws Exception {

    X500Principal p = new X500Principal(in);
    if (outFormat.equalsIgnoreCase("toString")) {
        if (p.toString().equals(expect)) {
            System.out.println("test " + n + " succeeded");
        } else {
            throw new SecurityException("test " + n + " failed:\n" +
                    "expected '" + expect + "'\n" +
                    "got '" + p.toString() + "'");
        }
    } else {
        if (p.getName(outFormat).equals(expect)) {
            System.out.println("test " + n + " succeeded");
        } else {
            throw new SecurityException("test " + n + " failed:\n" +
                    "expected '" + expect + "'\n" +
                    "got '" + p.getName(outFormat) + "'");
        }
    }
}
 
Example 7
Source File: X500PrincipalTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Inits X500Principal with byte array, where there are special characters
 * gets Name in CANONICAL format
 * compares with expected value of name, checks if the characters are escaped
 */
public void testNameSpecialCharsFromEncoding_CANONICAL() throws Exception {
    byte[] mess = { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
            0x04, 0x03, 0x0C, 0x02, 0x3B, 0x2C };
    X500Principal principal = new X500Principal(mess);
    String s = principal.getName(X500Principal.CANONICAL);
    assertEquals("cn=\\;\\,", s);

}
 
Example 8
Source File: InternalX500DNHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public String getName(X500Principal principal, String format) {
    if (principal == null) {
        throw new NullPointerException("X500Principal may not be null");
    }
    return principal.getName(format);
}
 
Example 9
Source File: X500PrincipalTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Inits X500Principal with a string, there are multiple AVAs and Oid which does not fall into any keyword
 * Gets encoding
 * Inits other X500Principal with the encoding
 * gets string in RFC1779 format
 * compares with expected value paying attention on sorting order of AVAs
 */
public void testGetName_EncodingWithWrongOidButGoodName_MultAVA_RFC1779()
        throws Exception {
    String dn = "OID.2.16.4.3=B + CN=A";
    X500Principal principal = new X500Principal(dn);
    byte[] enc = principal.getEncoded();
    X500Principal principal2 = new X500Principal(enc);
    String s = principal2.getName(X500Principal.RFC1779);
    assertTrue("OID.2.16.4.3=B + CN=A".equals(s) ||
        "CN=A + OID.2.16.4.3=B".equals(s));

}
 
Example 10
Source File: PolicyFile.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private String getDN(String alias, KeyStore keystore) {
    Certificate cert = null;
    try {
        cert = keystore.getCertificate(alias);
    } catch (Exception e) {
        if (debug != null) {
            debug.println("  Error retrieving certificate for '" +
                            alias +
                            "': " +
                            e.toString());
        }
        return null;
    }

    if (cert == null || !(cert instanceof X509Certificate)) {
        if (debug != null) {
            debug.println("  -- No certificate for '" +
                            alias +
                            "' - ignoring entry");
        }
        return null;
    } else {
        X509Certificate x509Cert = (X509Certificate)cert;

        // 4702543:  X500 names with an EmailAddress
        // were encoded incorrectly.  create new
        // X500Principal name with correct encoding

        X500Principal p = new X500Principal
            (x509Cert.getSubjectX500Principal().toString());
        return p.getName();
    }
}
 
Example 11
Source File: PolicyFile.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private String getDN(String alias, KeyStore keystore) {
    Certificate cert = null;
    try {
        cert = keystore.getCertificate(alias);
    } catch (Exception e) {
        if (debug != null) {
            debug.println("  Error retrieving certificate for '" +
                            alias +
                            "': " +
                            e.toString());
        }
        return null;
    }

    if (cert == null || !(cert instanceof X509Certificate)) {
        if (debug != null) {
            debug.println("  -- No certificate for '" +
                            alias +
                            "' - ignoring entry");
        }
        return null;
    } else {
        X509Certificate x509Cert = (X509Certificate)cert;

        // 4702543:  X500 names with an EmailAddress
        // were encoded incorrectly.  create new
        // X500Principal name with correct encoding

        X500Principal p = new X500Principal
            (x509Cert.getSubjectX500Principal().toString());
        return p.getName();
    }
}
 
Example 12
Source File: X500PrincipalTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Inits X500Principal with byte array, where there are leading and tailing spaces
 * gets Name in RFC2253 format
 * compares with expected value of name
 */
public void testNameSpaceFromEncoding_RFC2253() throws Exception {
    byte[] mess = { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55,
            0x04, 0x03, 0x13, 0x03, 0x20, 0x41, 0x20, };
    X500Principal principal = new X500Principal(mess);
    String s = principal.getName(X500Principal.RFC2253);
    assertEquals("CN=\\ A\\ ", s);

}
 
Example 13
Source File: DistinguishedNameParser.java    From crosswalk-cordova-android with Apache License 2.0 5 votes vote down vote up
public DistinguishedNameParser(X500Principal principal) {
  // RFC2253 is used to ensure we get attributes in the reverse
  // order of the underlying ASN.1 encoding, so that the most
  // significant values of repeated attributes occur first.
  this.dn = principal.getName(X500Principal.RFC2253);
  this.length = this.dn.length();
}
 
Example 14
Source File: X500PrincipalTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Inits X500Principal with a string, where OID does not fall into any keyword
 * gets encoded form
 * inits new X500Principal with the encoding
 * gets string in RFC1779 format
 * compares with expected value
 */
public void testGetName_EncodingWithWrongOidButGoodName_SeveralRDNs_RFC1779()
        throws Exception {
    String dn = "OID.2.16.4.3=B; CN=A";
    X500Principal principal = new X500Principal(dn);
    byte[] enc = principal.getEncoded();
    X500Principal principal2 = new X500Principal(enc);
    String s = principal2.getName(X500Principal.RFC1779);
    assertEquals("OID.2.16.4.3=B, CN=A", s);

}
 
Example 15
Source File: PolicyFile.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private String getDN(String alias, KeyStore keystore) {
    Certificate cert = null;
    try {
        cert = keystore.getCertificate(alias);
    } catch (Exception e) {
        if (debug != null) {
            debug.println("  Error retrieving certificate for '" +
                            alias +
                            "': " +
                            e.toString());
        }
        return null;
    }

    if (cert == null || !(cert instanceof X509Certificate)) {
        if (debug != null) {
            debug.println("  -- No certificate for '" +
                            alias +
                            "' - ignoring entry");
        }
        return null;
    } else {
        X509Certificate x509Cert = (X509Certificate)cert;

        // 4702543:  X500 names with an EmailAddress
        // were encoded incorrectly.  create new
        // X500Principal name with correct encoding

        X500Principal p = new X500Principal
            (x509Cert.getSubjectX500Principal().toString());
        return p.getName();
    }
}
 
Example 16
Source File: RFC4514.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void parse(String dnString) throws Exception {

        System.out.println("Parsing " + dnString);
        X500Principal dn = new X500Principal(dnString);
        String dnString2 = dn.getName();
        X500Principal dn2 = new X500Principal(dnString2);
        if (dn.equals(dn2)) {
            System.out.println("PASSED");
        } else {
            System.out.println("FAILED");
            failed++;
        }
    }
 
Example 17
Source File: X500PrincipalTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testStreamPosition_2() throws Exception {
    byte[] mess = { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
            0x04, 0x03, 0x13, 0x01, 0x41, 2 };
    ByteArrayInputStream is = new ByteArrayInputStream(mess);
    X500Principal principal = new X500Principal(is);
    String s = principal.getName(X500Principal.RFC1779);
    assertEquals("CN=A", s);
    assertEquals(1, is.available());
    assertEquals(2, is.read());
}
 
Example 18
Source File: TenantClientImpl.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public final Future<TenantObject> get(final X500Principal subjectDn, final SpanContext parent) {

    Objects.requireNonNull(subjectDn);

    final String subjectDnRfc2253 = subjectDn.getName(X500Principal.RFC2253);
    final TriTuple<TenantAction, X500Principal, Object> key = TriTuple.of(TenantAction.get, subjectDn, null);
    final Span span = newChildSpan(parent, "get Tenant by subject DN");
    TAG_SUBJECT_DN.set(span, subjectDnRfc2253);
    return get(
            key,
            () -> new JsonObject().put(TenantConstants.FIELD_PAYLOAD_SUBJECT_DN, subjectDnRfc2253),
            span);
}
 
Example 19
Source File: TrustAnchor.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates an instance of {@code TrustAnchor} where the
 * most-trusted CA is specified as an X500Principal and public key.
 * Name constraints are an optional parameter, and are intended to be used
 * as additional constraints when validating an X.509 certification path.
 * <p>
 * The name constraints are specified as a byte array. This byte array
 * contains the DER encoded form of the name constraints, as they
 * would appear in the NameConstraints structure defined in RFC 3280
 * and X.509. The ASN.1 notation for this structure is supplied in the
 * documentation for
 * {@link #TrustAnchor(X509Certificate, byte[])
 * TrustAnchor(X509Certificate trustedCert, byte[] nameConstraints) }.
 * <p>
 * Note that the name constraints byte array supplied here is cloned to
 * protect against subsequent modifications.
 *
 * @param caPrincipal the name of the most-trusted CA as X500Principal
 * @param pubKey the public key of the most-trusted CA
 * @param nameConstraints a byte array containing the ASN.1 DER encoding of
 * a NameConstraints extension to be used for checking name constraints.
 * Only the value of the extension is included, not the OID or criticality
 * flag. Specify {@code null} to omit the parameter.
 * @throws NullPointerException if the specified {@code caPrincipal} or
 * {@code pubKey} parameter is {@code null}
 * @since 1.5
 */
public TrustAnchor(X500Principal caPrincipal, PublicKey pubKey,
        byte[] nameConstraints) {
    if ((caPrincipal == null) || (pubKey == null)) {
        throw new NullPointerException();
    }
    this.trustedCert = null;
    this.caPrincipal = caPrincipal;
    this.caName = caPrincipal.getName();
    this.pubKey = pubKey;
    setNameConstraints(nameConstraints);
}
 
Example 20
Source File: X500PrincipalTest.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Inits X500Principal with the string with special characters - X#YZ
 * gets Name in RFC2253 format
 * compares with expected value of name - X\#YZ
 */
public void testNameSpecialChars_RFC2253_05() {
    String dn = "CN=X#YZ";
    X500Principal principal = new X500Principal(dn);

    String s = principal.getName(X500Principal.RFC2253);

    assertEquals("CN=X\\#YZ", s);

}