sun.security.provider.X509Factory Java Examples

The following examples show how to use sun.security.provider.X509Factory. 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: X509CertImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * read input stream as HEX-encoded DER-encoded bytes
 *
 * @param in InputStream to read
 * @returns DerValue corresponding to decoded HEX-encoded bytes
 * @throws IOException if stream can not be interpreted as RFC1421
 *                     encoded bytes
 */
private DerValue readRFC1421Cert(InputStream in) throws IOException {
    DerValue der = null;
    String line = null;
    BufferedReader certBufferedReader =
        new BufferedReader(new InputStreamReader(in, "ASCII"));
    try {
        line = certBufferedReader.readLine();
    } catch (IOException ioe1) {
        throw new IOException("Unable to read InputStream: " +
                              ioe1.getMessage());
    }
    if (line.equals(X509Factory.BEGIN_CERT)) {
        /* stream appears to be hex-encoded bytes */
        ByteArrayOutputStream decstream = new ByteArrayOutputStream();
        try {
            while ((line = certBufferedReader.readLine()) != null) {
                if (line.equals(X509Factory.END_CERT)) {
                    der = new DerValue(decstream.toByteArray());
                    break;
                } else {
                    decstream.write(Base64.getMimeDecoder().decode(line));
                }
            }
        } catch (IOException ioe2) {
            throw new IOException("Unable to read InputStream: "
                                  + ioe2.getMessage());
        }
    } else {
        throw new IOException("InputStream is not RFC1421 hex-encoded " +
                              "DER bytes");
    }
    return der;
}
 
Example #2
Source File: Main.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes an X.509 certificate in base64 or binary encoding to an output
 * stream.
 */
private void dumpCert(Certificate cert, PrintStream out)
    throws IOException, CertificateException
{
    if (rfc) {
        out.println(X509Factory.BEGIN_CERT);
        out.println(Base64.getMimeEncoder(64, CRLF).encodeToString(cert.getEncoded()));
        out.println(X509Factory.END_CERT);
    } else {
        out.write(cert.getEncoded()); // binary
    }
}
 
Example #3
Source File: X509CRLImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509CRL
 * to a X509CRLImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CRLImpl toImpl(X509CRL crl)
        throws CRLException {
    if (crl instanceof X509CRLImpl) {
        return (X509CRLImpl)crl;
    } else {
        return X509Factory.intern(crl);
    }
}
 
Example #4
Source File: X509CertImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509Certificate
 * to a X509CertImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CertImpl toImpl(X509Certificate cert)
        throws CertificateException {
    if (cert instanceof X509CertImpl) {
        return (X509CertImpl)cert;
    } else {
        return X509Factory.intern(cert);
    }
}
 
Example #5
Source File: Main.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes an X.509 certificate in base64 or binary encoding to an output
 * stream.
 */
private void dumpCert(Certificate cert, PrintStream out)
    throws IOException, CertificateException
{
    if (rfc) {
        out.println(X509Factory.BEGIN_CERT);
        out.println(Base64.getMimeEncoder().encodeToString(cert.getEncoded()));
        out.println(X509Factory.END_CERT);
    } else {
        out.write(cert.getEncoded()); // binary
    }
}
 
Example #6
Source File: X509CertImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * read input stream as HEX-encoded DER-encoded bytes
 *
 * @param in InputStream to read
 * @returns DerValue corresponding to decoded HEX-encoded bytes
 * @throws IOException if stream can not be interpreted as RFC1421
 *                     encoded bytes
 */
private DerValue readRFC1421Cert(InputStream in) throws IOException {
    DerValue der = null;
    String line = null;
    BufferedReader certBufferedReader =
        new BufferedReader(new InputStreamReader(in, "ASCII"));
    try {
        line = certBufferedReader.readLine();
    } catch (IOException ioe1) {
        throw new IOException("Unable to read InputStream: " +
                              ioe1.getMessage());
    }
    if (line.equals(X509Factory.BEGIN_CERT)) {
        /* stream appears to be hex-encoded bytes */
        ByteArrayOutputStream decstream = new ByteArrayOutputStream();
        try {
            while ((line = certBufferedReader.readLine()) != null) {
                if (line.equals(X509Factory.END_CERT)) {
                    der = new DerValue(decstream.toByteArray());
                    break;
                } else {
                    decstream.write(Base64.getMimeDecoder().decode(line));
                }
            }
        } catch (IOException ioe2) {
            throw new IOException("Unable to read InputStream: "
                                  + ioe2.getMessage());
        }
    } else {
        throw new IOException("InputStream is not RFC1421 hex-encoded " +
                              "DER bytes");
    }
    return der;
}
 
Example #7
Source File: X509CertImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509Certificate
 * to a X509CertImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CertImpl toImpl(X509Certificate cert)
        throws CertificateException {
    if (cert instanceof X509CertImpl) {
        return (X509CertImpl)cert;
    } else {
        return X509Factory.intern(cert);
    }
}
 
Example #8
Source File: X509CRLImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509CRL
 * to a X509CRLImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CRLImpl toImpl(X509CRL crl)
        throws CRLException {
    if (crl instanceof X509CRLImpl) {
        return (X509CRLImpl)crl;
    } else {
        return X509Factory.intern(crl);
    }
}
 
Example #9
Source File: Main.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes an X.509 certificate in base64 or binary encoding to an output
 * stream.
 */
private void dumpCert(Certificate cert, PrintStream out)
    throws IOException, CertificateException
{
    if (rfc) {
        out.println(X509Factory.BEGIN_CERT);
        out.println(Base64.getMimeEncoder().encodeToString(cert.getEncoded()));
        out.println(X509Factory.END_CERT);
    } else {
        out.write(cert.getEncoded()); // binary
    }
}
 
Example #10
Source File: X509CertImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * read input stream as HEX-encoded DER-encoded bytes
 *
 * @param in InputStream to read
 * @returns DerValue corresponding to decoded HEX-encoded bytes
 * @throws IOException if stream can not be interpreted as RFC1421
 *                     encoded bytes
 */
private DerValue readRFC1421Cert(InputStream in) throws IOException {
    DerValue der = null;
    String line = null;
    BufferedReader certBufferedReader =
        new BufferedReader(new InputStreamReader(in, "ASCII"));
    try {
        line = certBufferedReader.readLine();
    } catch (IOException ioe1) {
        throw new IOException("Unable to read InputStream: " +
                              ioe1.getMessage());
    }
    if (line.equals(X509Factory.BEGIN_CERT)) {
        /* stream appears to be hex-encoded bytes */
        BASE64Decoder         decoder   = new BASE64Decoder();
        ByteArrayOutputStream decstream = new ByteArrayOutputStream();
        try {
            while ((line = certBufferedReader.readLine()) != null) {
                if (line.equals(X509Factory.END_CERT)) {
                    der = new DerValue(decstream.toByteArray());
                    break;
                } else {
                    decstream.write(decoder.decodeBuffer(line));
                }
            }
        } catch (IOException ioe2) {
            throw new IOException("Unable to read InputStream: "
                                  + ioe2.getMessage());
        }
    } else {
        throw new IOException("InputStream is not RFC1421 hex-encoded " +
                              "DER bytes");
    }
    return der;
}
 
Example #11
Source File: X509CertImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509Certificate
 * to a X509CertImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CertImpl toImpl(X509Certificate cert)
        throws CertificateException {
    if (cert instanceof X509CertImpl) {
        return (X509CertImpl)cert;
    } else {
        return X509Factory.intern(cert);
    }
}
 
Example #12
Source File: X509CRLImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509CRL
 * to a X509CRLImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CRLImpl toImpl(X509CRL crl)
        throws CRLException {
    if (crl instanceof X509CRLImpl) {
        return (X509CRLImpl)crl;
    } else {
        return X509Factory.intern(crl);
    }
}
 
Example #13
Source File: X509CRLImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509CRL
 * to a X509CRLImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CRLImpl toImpl(X509CRL crl)
        throws CRLException {
    if (crl instanceof X509CRLImpl) {
        return (X509CRLImpl)crl;
    } else {
        return X509Factory.intern(crl);
    }
}
 
Example #14
Source File: X509CRLImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509CRL
 * to a X509CRLImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CRLImpl toImpl(X509CRL crl)
        throws CRLException {
    if (crl instanceof X509CRLImpl) {
        return (X509CRLImpl)crl;
    } else {
        return X509Factory.intern(crl);
    }
}
 
Example #15
Source File: X509CertImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509Certificate
 * to a X509CertImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CertImpl toImpl(X509Certificate cert)
        throws CertificateException {
    if (cert instanceof X509CertImpl) {
        return (X509CertImpl)cert;
    } else {
        return X509Factory.intern(cert);
    }
}
 
Example #16
Source File: Main.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes an X.509 certificate in base64 or binary encoding to an output
 * stream.
 */
private void dumpCert(Certificate cert, PrintStream out)
    throws IOException, CertificateException
{
    if (rfc) {
        out.println(X509Factory.BEGIN_CERT);
        out.println(Base64.getMimeEncoder().encodeToString(cert.getEncoded()));
        out.println(X509Factory.END_CERT);
    } else {
        out.write(cert.getEncoded()); // binary
    }
}
 
Example #17
Source File: CreateCA.java    From signer with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws IOException, OperatorCreationException, NoSuchAlgorithmException {

		// ---------------------- CA Creation ----------------------
		// System.out.println("Generating Keys");
		KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
		rsa.initialize(1024);
		KeyPair kp = rsa.generateKeyPair();

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.YEAR, 100);

		// System.out.println("Getting data");
		byte[] pk = kp.getPublic().getEncoded();
		SubjectPublicKeyInfo bcPk = SubjectPublicKeyInfo.getInstance(pk);

		// System.out.println("Creating cert");
		X509v1CertificateBuilder certGen = new X509v1CertificateBuilder(new X500Name("CN=CA Cert"), BigInteger.ONE,
				new Date(), cal.getTime(), new X500Name("CN=CA Cert"), bcPk);

		X509CertificateHolder certHolder = certGen
				.build(new JcaContentSignerBuilder("SHA1withRSA").build(kp.getPrivate()));

		StringBuffer s = new StringBuffer();

		s.append(X509Factory.BEGIN_CERT + "\n");
		s.append(Base64Utils.base64Encode(certHolder.getEncoded()) + "\n");
		s.append(X509Factory.END_CERT);

		saveFile(s.toString().getBytes());

		// ---------------------- ISSUER Creation ----------------------

	}
 
Example #18
Source File: Main.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes an X.509 certificate in base64 or binary encoding to an output
 * stream.
 */
private void dumpCert(Certificate cert, PrintStream out)
    throws IOException, CertificateException
{
    if (rfc) {
        out.println(X509Factory.BEGIN_CERT);
        out.println(Base64.getMimeEncoder().encodeToString(cert.getEncoded()));
        out.println(X509Factory.END_CERT);
    } else {
        out.write(cert.getEncoded()); // binary
    }
}
 
Example #19
Source File: X509CRLImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509CRL
 * to a X509CRLImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CRLImpl toImpl(X509CRL crl)
        throws CRLException {
    if (crl instanceof X509CRLImpl) {
        return (X509CRLImpl)crl;
    } else {
        return X509Factory.intern(crl);
    }
}
 
Example #20
Source File: X509CertImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509Certificate
 * to a X509CertImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CertImpl toImpl(X509Certificate cert)
        throws CertificateException {
    if (cert instanceof X509CertImpl) {
        return (X509CertImpl)cert;
    } else {
        return X509Factory.intern(cert);
    }
}
 
Example #21
Source File: X509CertImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * read input stream as HEX-encoded DER-encoded bytes
 *
 * @param in InputStream to read
 * @returns DerValue corresponding to decoded HEX-encoded bytes
 * @throws IOException if stream can not be interpreted as RFC1421
 *                     encoded bytes
 */
private DerValue readRFC1421Cert(InputStream in) throws IOException {
    DerValue der = null;
    String line = null;
    BufferedReader certBufferedReader =
        new BufferedReader(new InputStreamReader(in, "ASCII"));
    try {
        line = certBufferedReader.readLine();
    } catch (IOException ioe1) {
        throw new IOException("Unable to read InputStream: " +
                              ioe1.getMessage());
    }
    if (line.equals(X509Factory.BEGIN_CERT)) {
        /* stream appears to be hex-encoded bytes */
        ByteArrayOutputStream decstream = new ByteArrayOutputStream();
        try {
            while ((line = certBufferedReader.readLine()) != null) {
                if (line.equals(X509Factory.END_CERT)) {
                    der = new DerValue(decstream.toByteArray());
                    break;
                } else {
                    decstream.write(Base64.getMimeDecoder().decode(line));
                }
            }
        } catch (IOException ioe2) {
            throw new IOException("Unable to read InputStream: "
                                  + ioe2.getMessage());
        }
    } else {
        throw new IOException("InputStream is not RFC1421 hex-encoded " +
                              "DER bytes");
    }
    return der;
}
 
Example #22
Source File: BadPem.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    String ks = System.getProperty("test.src", ".")
            + "/../../ssl/etc/keystore";
    String pass = "passphrase";
    String alias = "dummy";

    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(new FileInputStream(ks), pass.toCharArray());
    byte[] cert = keyStore.getCertificate(alias).getEncoded();

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    PrintStream pout = new PrintStream(bout);
    byte[] CRLF = new byte[] {'\r', '\n'};
    pout.println(X509Factory.BEGIN_CERT);
    for (int i=0; i<cert.length; i += 48) {
        int blockLen = (cert.length > i + 48) ? 48 : (cert.length - i);
        pout.println("!" + Base64.getEncoder()
                .encodeToString(Arrays.copyOfRange(cert, i, i + blockLen)));
    }
    pout.println(X509Factory.END_CERT);

    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    try {
        cf.generateCertificate(new ByteArrayInputStream(bout.toByteArray()));
        throw new Exception("Should fail");
    } catch (CertificateException e) {
        // Good
    }
}
 
Example #23
Source File: Main.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes an X.509 certificate in base64 or binary encoding to an output
 * stream.
 */
private void dumpCert(Certificate cert, PrintStream out)
    throws IOException, CertificateException
{
    if (rfc) {
        out.println(X509Factory.BEGIN_CERT);
        out.println(Base64.getMimeEncoder(64, CRLF).encodeToString(cert.getEncoded()));
        out.println(X509Factory.END_CERT);
    } else {
        out.write(cert.getEncoded()); // binary
    }
}
 
Example #24
Source File: X509CRLImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509CRL
 * to a X509CRLImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CRLImpl toImpl(X509CRL crl)
        throws CRLException {
    if (crl instanceof X509CRLImpl) {
        return (X509CRLImpl)crl;
    } else {
        return X509Factory.intern(crl);
    }
}
 
Example #25
Source File: X509CertImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509Certificate
 * to a X509CertImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CertImpl toImpl(X509Certificate cert)
        throws CertificateException {
    if (cert instanceof X509CertImpl) {
        return (X509CertImpl)cert;
    } else {
        return X509Factory.intern(cert);
    }
}
 
Example #26
Source File: X509CertImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * read input stream as HEX-encoded DER-encoded bytes
 *
 * @param in InputStream to read
 * @returns DerValue corresponding to decoded HEX-encoded bytes
 * @throws IOException if stream can not be interpreted as RFC1421
 *                     encoded bytes
 */
private DerValue readRFC1421Cert(InputStream in) throws IOException {
    DerValue der = null;
    String line = null;
    BufferedReader certBufferedReader =
        new BufferedReader(new InputStreamReader(in, "ASCII"));
    try {
        line = certBufferedReader.readLine();
    } catch (IOException ioe1) {
        throw new IOException("Unable to read InputStream: " +
                              ioe1.getMessage());
    }
    if (line.equals(X509Factory.BEGIN_CERT)) {
        /* stream appears to be hex-encoded bytes */
        ByteArrayOutputStream decstream = new ByteArrayOutputStream();
        try {
            while ((line = certBufferedReader.readLine()) != null) {
                if (line.equals(X509Factory.END_CERT)) {
                    der = new DerValue(decstream.toByteArray());
                    break;
                } else {
                    decstream.write(Pem.decode(line));
                }
            }
        } catch (IOException ioe2) {
            throw new IOException("Unable to read InputStream: "
                                  + ioe2.getMessage());
        }
    } else {
        throw new IOException("InputStream is not RFC1421 hex-encoded " +
                              "DER bytes");
    }
    return der;
}
 
Example #27
Source File: BadPem.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    String ks = System.getProperty("test.src", ".")
            + "/../../ssl/etc/keystore";
    String pass = "passphrase";
    String alias = "dummy";

    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(new FileInputStream(ks), pass.toCharArray());
    byte[] cert = keyStore.getCertificate(alias).getEncoded();

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    PrintStream pout = new PrintStream(bout);
    byte[] CRLF = new byte[] {'\r', '\n'};
    pout.println(X509Factory.BEGIN_CERT);
    for (int i=0; i<cert.length; i += 48) {
        int blockLen = (cert.length > i + 48) ? 48 : (cert.length - i);
        pout.println("!" + Base64.getEncoder()
                .encodeToString(Arrays.copyOfRange(cert, i, i + blockLen)));
    }
    pout.println(X509Factory.END_CERT);

    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    try {
        cf.generateCertificate(new ByteArrayInputStream(bout.toByteArray()));
        throw new Exception("Should fail");
    } catch (CertificateException e) {
        // Good
    }
}
 
Example #28
Source File: Main.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes an X.509 certificate in base64 or binary encoding to an output
 * stream.
 */
private void dumpCert(Certificate cert, PrintStream out)
    throws IOException, CertificateException
{
    if (rfc) {
        out.println(X509Factory.BEGIN_CERT);
        out.println(Base64.getMimeEncoder(64, CRLF).encodeToString(cert.getEncoded()));
        out.println(X509Factory.END_CERT);
    } else {
        out.write(cert.getEncoded()); // binary
    }
}
 
Example #29
Source File: X509CRLImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509CRL
 * to a X509CRLImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CRLImpl toImpl(X509CRL crl)
        throws CRLException {
    if (crl instanceof X509CRLImpl) {
        return (X509CRLImpl)crl;
    } else {
        return X509Factory.intern(crl);
    }
}
 
Example #30
Source File: X509CertImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to convert an arbitrary instance of X509Certificate
 * to a X509CertImpl. Does a cast if possible, otherwise reparses
 * the encoding.
 */
public static X509CertImpl toImpl(X509Certificate cert)
        throws CertificateException {
    if (cert instanceof X509CertImpl) {
        return (X509CertImpl)cert;
    } else {
        return X509Factory.intern(cert);
    }
}