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

The following examples show how to use javax.security.auth.x500.X500Principal#getEncoded() . 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: HostnameChecker.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return the subject of a certificate as X500Name, by reparsing if
 * necessary. X500Name should only be used if access to name components
 * is required, in other cases X500Principal is to be preferred.
 *
 * This method is currently used from within JSSE, do not remove.
 */
public static X500Name getSubjectX500Name(X509Certificate cert)
        throws CertificateParsingException {
    try {
        Principal subjectDN = cert.getSubjectDN();
        if (subjectDN instanceof X500Name) {
            return (X500Name)subjectDN;
        } else {
            X500Principal subjectX500 = cert.getSubjectX500Principal();
            return new X500Name(subjectX500.getEncoded());
        }
    } catch (IOException e) {
        throw(CertificateParsingException)
            new CertificateParsingException().initCause(e);
    }
}
 
Example 2
Source File: HostnameChecker.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return the subject of a certificate as X500Name, by reparsing if
 * necessary. X500Name should only be used if access to name components
 * is required, in other cases X500Principal is to be preferred.
 *
 * This method is currently used from within JSSE, do not remove.
 */
public static X500Name getSubjectX500Name(X509Certificate cert)
        throws CertificateParsingException {
    try {
        Principal subjectDN = cert.getSubjectDN();
        if (subjectDN instanceof X500Name) {
            return (X500Name)subjectDN;
        } else {
            X500Principal subjectX500 = cert.getSubjectX500Principal();
            return new X500Name(subjectX500.getEncoded());
        }
    } catch (IOException e) {
        throw(CertificateParsingException)
            new CertificateParsingException().initCause(e);
    }
}
 
Example 3
Source File: X509Util.java    From BiglyBT with GNU General Public License v2.0 5 votes vote down vote up
static X509Principal convertPrincipal(
    X500Principal principal)
{
    try
    {
        return new X509Principal(principal.getEncoded());
    }
    catch (IOException e)
    {
        throw new IllegalArgumentException("cannot convert principal");
    }
}
 
Example 4
Source File: X500PrincipalTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Inits X500Principal with the string with special characters - ABC"DEF"
 * gets encoding
 * compares with expected encoding
 */
public void testNameWithQuotation() throws Exception {
    String dn = "CN=\"ABCDEF\"";

    X500Principal principal = new X500Principal(dn);
    byte[] enc = principal.getEncoded();
    assertTrue(Arrays.equals(new byte[] { 0x30, 0x11, 0x31, 0x0F, 0x30,
            0x0D, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x06, 0x41, 0x42,
            0x43, 0x44, 0x45, 0x46 }, enc));

}
 
Example 5
Source File: X509Util.java    From ripple-lib-java with ISC License 5 votes vote down vote up
static X509Principal convertPrincipal(
    X500Principal principal)
{
    try
    {
        return new X509Principal(principal.getEncoded());
    }
    catch (IOException e)
    {
        throw new IllegalArgumentException("cannot convert principal");
    }
}
 
Example 6
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 RFC2253 format
 * compares with expected value
 */
public void testGetName_EncodingWithWrongOidButGoodName_SeveralRDNs_RFC2253()
        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.RFC2253);
    assertEquals("2.16.4.3=#130142,CN=A", s);

}
 
Example 7
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 CANONICAL format
 * compares with expected value paying attention on sorting order of AVAs
 */
public void testGetName_EncodingWithWrongOidButGoodName_MultAVA_CANONICAL()
        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.CANONICAL);
    assertEquals("cn=a+2.16.4.3=#130142", s);

}
 
Example 8
Source File: DomainComponentEncoding.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private static void testDN(String dn) throws Exception {
    X500Principal p = new X500Principal(dn);
    byte[] encoded = p.getEncoded();

    // name is a sequence of RDN's
    DerInputStream dis = new DerInputStream(encoded);
    DerValue[] nameseq = dis.getSequence(3);

    boolean passed = false;
    for (int i = 0; i < nameseq.length; i++) {

        // each RDN is a set of AttributeTypeAndValue
        DerInputStream is = new DerInputStream(nameseq[i].toByteArray());
        DerValue[] ava = is.getSet(3);

        for (int j = 0; j < ava.length; j++) {

            ObjectIdentifier oid = ava[j].data.getOID();

            if (oid.equals(X500Name.DOMAIN_COMPONENT_OID)) {
                DerValue value = ava[j].data.getDerValue();
                if (value.getTag() == DerValue.tag_IA5String) {
                    passed = true;
                    break;
                } else {
                    throw new SecurityException
                            ("Test failed, expected DOMAIN_COMPONENT tag '" +
                            DerValue.tag_IA5String +
                            "', got '" +
                            value.getTag() + "'");
                }
            }
        }

        if (passed) {
            break;
        }
    }

    if (passed) {
        System.out.println("Test passed");
    } else {
        throw new SecurityException("Test failed");
    }
}
 
Example 9
Source File: DerIsConstructor.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {

        try {

            // create 2 different X500Principals
            X500Principal p = new X500Principal("o=sun, cn=duke");
            X500Principal p2 = new X500Principal("o=sun, cn=dukette");

            // get the encoded bytes for the 2 principals
            byte[] encoded = p.getEncoded();
            byte[] encoded2 = p2.getEncoded();

            // create a ByteArrayInputStream with the
            // encodings from the 2 principals
            byte[] all = new byte[encoded.length + encoded2.length];
            System.arraycopy(encoded, 0, all, 0, encoded.length);
            System.arraycopy(encoded2, 0, all, encoded.length, encoded2.length);
            ByteArrayInputStream bais = new ByteArrayInputStream(all);

            // create 2 new X500Principals from the ByteArrayInputStream
            X500Principal pp = new X500Principal(bais);
            X500Principal pp2 = new X500Principal(bais);

            // sanity check the 2 new principals
            if (p.equals(pp) && p2.equals(pp2) && !pp.equals(pp2)) {
                System.out.println("Test 1 passed");
            } else {
                throw new SecurityException("Test 1 failed");
            }

            // corrupt the ByteArrayInputStream and see if the
            // mark/reset worked
            byte[] all2 = new byte[all.length];
            System.arraycopy(all, 0, all2, 0, all.length);
            all2[encoded.length + 2] = (byte)-1;
            bais = new ByteArrayInputStream(all2);

            // this should work
            X500Principal ppp = new X500Principal(bais);

            // this should throw an IOException due to stream corruption
            int origAvailable = bais.available();
            try {
                X500Principal ppp2 = new X500Principal(bais);
                throw new SecurityException("Test 2 (part a) failed");
            } catch (IllegalArgumentException iae) {
                if (bais.available() == origAvailable) {
                    System.out.println("Test 2 passed");
                } else {
                    throw new SecurityException("Test 2 (part b) failed");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SecurityException(e.getMessage());
        }
    }
 
Example 10
Source File: DomainComponentEncoding.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void testDN(String dn) throws Exception {
    X500Principal p = new X500Principal(dn);
    byte[] encoded = p.getEncoded();

    // name is a sequence of RDN's
    DerInputStream dis = new DerInputStream(encoded);
    DerValue[] nameseq = dis.getSequence(3);

    boolean passed = false;
    for (int i = 0; i < nameseq.length; i++) {

        // each RDN is a set of AttributeTypeAndValue
        DerInputStream is = new DerInputStream(nameseq[i].toByteArray());
        DerValue[] ava = is.getSet(3);

        for (int j = 0; j < ava.length; j++) {

            ObjectIdentifier oid = ava[j].data.getOID();

            if (oid.equals(X500Name.DOMAIN_COMPONENT_OID)) {
                DerValue value = ava[j].data.getDerValue();
                if (value.getTag() == DerValue.tag_IA5String) {
                    passed = true;
                    break;
                } else {
                    throw new SecurityException
                            ("Test failed, expected DOMAIN_COMPONENT tag '" +
                            DerValue.tag_IA5String +
                            "', got '" +
                            value.getTag() + "'");
                }
            }
        }

        if (passed) {
            break;
        }
    }

    if (passed) {
        System.out.println("Test passed");
    } else {
        throw new SecurityException("Test failed");
    }
}
 
Example 11
Source File: HandshakeMessage.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
DistinguishedName(X500Principal dn) {
    name = dn.getEncoded();
}
 
Example 12
Source File: DerIsConstructor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {

        try {

            // create 2 different X500Principals
            X500Principal p = new X500Principal("o=sun, cn=duke");
            X500Principal p2 = new X500Principal("o=sun, cn=dukette");

            // get the encoded bytes for the 2 principals
            byte[] encoded = p.getEncoded();
            byte[] encoded2 = p2.getEncoded();

            // create a ByteArrayInputStream with the
            // encodings from the 2 principals
            byte[] all = new byte[encoded.length + encoded2.length];
            System.arraycopy(encoded, 0, all, 0, encoded.length);
            System.arraycopy(encoded2, 0, all, encoded.length, encoded2.length);
            ByteArrayInputStream bais = new ByteArrayInputStream(all);

            // create 2 new X500Principals from the ByteArrayInputStream
            X500Principal pp = new X500Principal(bais);
            X500Principal pp2 = new X500Principal(bais);

            // sanity check the 2 new principals
            if (p.equals(pp) && p2.equals(pp2) && !pp.equals(pp2)) {
                System.out.println("Test 1 passed");
            } else {
                throw new SecurityException("Test 1 failed");
            }

            // corrupt the ByteArrayInputStream and see if the
            // mark/reset worked
            byte[] all2 = new byte[all.length];
            System.arraycopy(all, 0, all2, 0, all.length);
            all2[encoded.length + 2] = (byte)-1;
            bais = new ByteArrayInputStream(all2);

            // this should work
            X500Principal ppp = new X500Principal(bais);

            // this should throw an IOException due to stream corruption
            int origAvailable = bais.available();
            try {
                X500Principal ppp2 = new X500Principal(bais);
                throw new SecurityException("Test 2 (part a) failed");
            } catch (IllegalArgumentException iae) {
                if (bais.available() == origAvailable) {
                    System.out.println("Test 2 passed");
                } else {
                    throw new SecurityException("Test 2 (part b) failed");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SecurityException(e.getMessage());
        }
    }
 
Example 13
Source File: HandshakeMessage.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
DistinguishedName(X500Principal dn) {
    name = dn.getEncoded();
}
 
Example 14
Source File: HandshakeMessage.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
DistinguishedName(X500Principal dn) {
    name = dn.getEncoded();
}
 
Example 15
Source File: HandshakeMessage.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
DistinguishedName(X500Principal dn) {
    name = dn.getEncoded();
}
 
Example 16
Source File: HandshakeMessage.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
DistinguishedName(X500Principal dn) {
    name = dn.getEncoded();
}
 
Example 17
Source File: HandshakeMessage.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
DistinguishedName(X500Principal dn) {
    name = dn.getEncoded();
}
 
Example 18
Source File: DerIsConstructor.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {

        try {

            // create 2 different X500Principals
            X500Principal p = new X500Principal("o=sun, cn=duke");
            X500Principal p2 = new X500Principal("o=sun, cn=dukette");

            // get the encoded bytes for the 2 principals
            byte[] encoded = p.getEncoded();
            byte[] encoded2 = p2.getEncoded();

            // create a ByteArrayInputStream with the
            // encodings from the 2 principals
            byte[] all = new byte[encoded.length + encoded2.length];
            System.arraycopy(encoded, 0, all, 0, encoded.length);
            System.arraycopy(encoded2, 0, all, encoded.length, encoded2.length);
            ByteArrayInputStream bais = new ByteArrayInputStream(all);

            // create 2 new X500Principals from the ByteArrayInputStream
            X500Principal pp = new X500Principal(bais);
            X500Principal pp2 = new X500Principal(bais);

            // sanity check the 2 new principals
            if (p.equals(pp) && p2.equals(pp2) && !pp.equals(pp2)) {
                System.out.println("Test 1 passed");
            } else {
                throw new SecurityException("Test 1 failed");
            }

            // corrupt the ByteArrayInputStream and see if the
            // mark/reset worked
            byte[] all2 = new byte[all.length];
            System.arraycopy(all, 0, all2, 0, all.length);
            all2[encoded.length + 2] = (byte)-1;
            bais = new ByteArrayInputStream(all2);

            // this should work
            X500Principal ppp = new X500Principal(bais);

            // this should throw an IOException due to stream corruption
            int origAvailable = bais.available();
            try {
                X500Principal ppp2 = new X500Principal(bais);
                throw new SecurityException("Test 2 (part a) failed");
            } catch (IllegalArgumentException iae) {
                if (bais.available() == origAvailable) {
                    System.out.println("Test 2 passed");
                } else {
                    throw new SecurityException("Test 2 (part b) failed");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SecurityException(e.getMessage());
        }
    }
 
Example 19
Source File: DerIsConstructor.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {

        try {

            // create 2 different X500Principals
            X500Principal p = new X500Principal("o=sun, cn=duke");
            X500Principal p2 = new X500Principal("o=sun, cn=dukette");

            // get the encoded bytes for the 2 principals
            byte[] encoded = p.getEncoded();
            byte[] encoded2 = p2.getEncoded();

            // create a ByteArrayInputStream with the
            // encodings from the 2 principals
            byte[] all = new byte[encoded.length + encoded2.length];
            System.arraycopy(encoded, 0, all, 0, encoded.length);
            System.arraycopy(encoded2, 0, all, encoded.length, encoded2.length);
            ByteArrayInputStream bais = new ByteArrayInputStream(all);

            // create 2 new X500Principals from the ByteArrayInputStream
            X500Principal pp = new X500Principal(bais);
            X500Principal pp2 = new X500Principal(bais);

            // sanity check the 2 new principals
            if (p.equals(pp) && p2.equals(pp2) && !pp.equals(pp2)) {
                System.out.println("Test 1 passed");
            } else {
                throw new SecurityException("Test 1 failed");
            }

            // corrupt the ByteArrayInputStream and see if the
            // mark/reset worked
            byte[] all2 = new byte[all.length];
            System.arraycopy(all, 0, all2, 0, all.length);
            all2[encoded.length + 2] = (byte)-1;
            bais = new ByteArrayInputStream(all2);

            // this should work
            X500Principal ppp = new X500Principal(bais);

            // this should throw an IOException due to stream corruption
            int origAvailable = bais.available();
            try {
                X500Principal ppp2 = new X500Principal(bais);
                throw new SecurityException("Test 2 (part a) failed");
            } catch (IllegalArgumentException iae) {
                if (bais.available() == origAvailable) {
                    System.out.println("Test 2 passed");
                } else {
                    throw new SecurityException("Test 2 (part b) failed");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SecurityException(e.getMessage());
        }
    }
 
Example 20
Source File: DSSASN1Utils.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static String getUtf8String(final X500Principal x500Principal) {

		final byte[] encoded = x500Principal.getEncoded();
		final ASN1Sequence asn1Sequence = ASN1Sequence.getInstance(encoded);
		final ASN1Encodable[] asn1Encodables = asn1Sequence.toArray();
		final StringBuilder stringBuilder = new StringBuilder();
		/**
		 * RFC 4514 LDAP: Distinguished Names
		 * 2.1. Converting the RDNSequence
		 *
		 * If the RDNSequence is an empty sequence, the result is the empty or
		 * zero-length string.
		 *
		 * Otherwise, the output consists of the string encodings of each
		 * RelativeDistinguishedName in the RDNSequence (according to Section
		 * 2.2), starting with the last element of the sequence and moving
		 * backwards toward the first.
		 * ...
		 */
		for (int ii = asn1Encodables.length - 1; ii >= 0; ii--) {

			final ASN1Encodable asn1Encodable = asn1Encodables[ii];

			final DLSet dlSet = (DLSet) asn1Encodable;
			for (int jj = 0; jj < dlSet.size(); jj++) {

				final DLSequence dlSequence = (DLSequence) dlSet.getObjectAt(jj);
				if (dlSequence.size() != 2) {

					throw new DSSException("The DLSequence must contains exactly 2 elements.");
				}
				final ASN1Encodable attributeType = dlSequence.getObjectAt(0);
				final ASN1Encodable attributeValue = dlSequence.getObjectAt(1);
				String string = getString(attributeValue);

				/**
				 * RFC 4514 LDAP: Distinguished Names
				 * ...
				 * Other characters may be escaped.
				 *
				 * Each octet of the character to be escaped is replaced by a backslash
				 * and two hex digits, which form a single octet in the code of the
				 * character. Alternatively, if and only if the character to be escaped
				 * is one of
				 *
				 * ' ', '"', '#', '+', ',', ';', '<', '=', '>', or '\'
				 * (U+0020, U+0022, U+0023, U+002B, U+002C, U+003B,
				 * U+003C, U+003D, U+003E, U+005C, respectively)
				 *
				 * it can be prefixed by a backslash ('\' U+005C).
				 */
				string = Rdn.escapeValue(string);
				if (stringBuilder.length() != 0) {
					stringBuilder.append(',');
				}
				stringBuilder.append(attributeType).append('=').append(string);
			}
		}
		return stringBuilder.toString();
	}