Search in sources :

Example 1 with X509Key

use of org.mozilla.jss.netscape.security.x509.X509Key in project jss by dogtagpki.

the class X509CertTest method convertPublicKeyToX509Key.

public static X509Key convertPublicKeyToX509Key(PublicKey pubk) throws Exception {
    X509Key xKey = null;
    if (pubk instanceof RSAPublicKey) {
        RSAPublicKey rsaKey = (RSAPublicKey) pubk;
        xKey = new org.mozilla.jss.netscape.security.provider.RSAPublicKey(new BigInt(rsaKey.getModulus()), new BigInt(rsaKey.getPublicExponent()));
    } else if (pubk instanceof PK11ECPublicKey) {
        byte[] encoded = pubk.getEncoded();
        xKey = X509Key.parse(new DerValue(encoded));
    }
    return xKey;
}
Also used : PK11ECPublicKey(org.mozilla.jss.pkcs11.PK11ECPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) DerValue(org.mozilla.jss.netscape.security.util.DerValue) BigInt(org.mozilla.jss.netscape.security.util.BigInt) X509Key(org.mozilla.jss.netscape.security.x509.X509Key) CertificateX509Key(org.mozilla.jss.netscape.security.x509.CertificateX509Key)

Example 2 with X509Key

use of org.mozilla.jss.netscape.security.x509.X509Key in project Bytecoder by mirkosertic.

the class ECUtil method x509EncodeECPublicKey.

public static byte[] x509EncodeECPublicKey(ECPoint w, ECParameterSpec params) throws InvalidKeySpecException {
    KeyFactory keyFactory = getKeyFactory();
    ECPublicKeySpec keySpec = new ECPublicKeySpec(w, params);
    X509Key key = (X509Key) keyFactory.generatePublic(keySpec);
    return key.getEncoded();
}
Also used : X509Key(sun.security.x509.X509Key)

Example 3 with X509Key

use of org.mozilla.jss.netscape.security.x509.X509Key in project jdk8u_jdk by JetBrains.

the class P11ECUtil method x509EncodeECPublicKey.

static byte[] x509EncodeECPublicKey(ECPoint w, ECParameterSpec params) throws InvalidKeySpecException {
    ECPublicKeySpec keySpec = new ECPublicKeySpec(w, params);
    X509Key key = (X509Key) ECGeneratePublic(keySpec);
    return key.getEncoded();
}
Also used : X509Key(sun.security.x509.X509Key)

Example 4 with X509Key

use of org.mozilla.jss.netscape.security.x509.X509Key in project jdk8u_jdk by JetBrains.

the class PKCS10AttrEncoding method main.

public static void main(String[] args) throws Exception {
    // initializations
    int len = ids.length;
    Object[] values = { new ObjectIdentifier("1.2.3.4"), new GregorianCalendar(1970, 1, 25, 8, 56, 7).getTime(), "challenging" };
    for (int j = 0; j < len; j++) {
        constructedMap.put(ids[j], values[j]);
    }
    X500Name subject = new X500Name("cn=Test");
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    String sigAlg = "DSA";
    keyGen.initialize(512);
    KeyPair pair = keyGen.generateKeyPair();
    X509Key publicKey = (X509Key) pair.getPublic();
    PrivateKey privateKey = pair.getPrivate();
    Signature signature = Signature.getInstance(sigAlg);
    signature.initSign(privateKey);
    // Create the PKCS10 request
    PKCS10Attribute[] attrs = new PKCS10Attribute[len];
    for (int j = 0; j < len; j++) {
        attrs[j] = new PKCS10Attribute(ids[j], values[j]);
    }
    PKCS10 req = new PKCS10(publicKey, new PKCS10Attributes(attrs));
    System.out.println("List of attributes in constructed PKCS10 " + "request: ");
    checkAttributes(req.getAttributes().getElements());
    // Encode the PKCS10 request and generate another PKCS10 request from
    // the encoded byte array
    req.encodeAndSign(subject, signature);
    PKCS10 resp = new PKCS10(req.getEncoded());
    System.out.println("List of attributes in DER encoded PKCS10 Request:");
    checkAttributes(resp.getAttributes().getElements());
    if (failedCount > 0) {
        throw new RuntimeException("Attributes Compared : Failed");
    }
    System.out.println("Attributes Compared : Pass");
}
Also used : PKCS10Attribute(sun.security.pkcs10.PKCS10Attribute) KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PKCS10Attributes(sun.security.pkcs10.PKCS10Attributes) GregorianCalendar(java.util.GregorianCalendar) X500Name(sun.security.x509.X500Name) KeyPairGenerator(java.security.KeyPairGenerator) X509Key(sun.security.x509.X509Key) PKCS10(sun.security.pkcs10.PKCS10) Signature(java.security.Signature) ObjectIdentifier(sun.security.util.ObjectIdentifier)

Example 5 with X509Key

use of org.mozilla.jss.netscape.security.x509.X509Key in project jss by dogtagpki.

the class CertPrettyPrint method X509toString.

public String X509toString(Locale clientLocale) {
    // get I18N resources
    ResourceBundle resource = ResourceBundle.getBundle(PrettyPrintResources.class.getName());
    DateFormat dateFormater = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, clientLocale);
    // get timezone and timezone ID
    String tz = " ";
    String tzid = " ";
    StringBuffer sb = new StringBuffer();
    try {
        X509CertInfo info = (X509CertInfo) mX509Cert.get(X509CertImpl.NAME + "." + X509CertImpl.INFO);
        String serial2 = mX509Cert.getSerialNumber().toString(16).toUpperCase();
        // get correct instance of key
        PublicKey pKey = mX509Cert.getPublicKey();
        X509Key key = null;
        if (pKey instanceof CertificateX509Key) {
            CertificateX509Key certKey = (CertificateX509Key) pKey;
            key = (X509Key) certKey.get(CertificateX509Key.KEY);
        }
        if (pKey instanceof X509Key) {
            key = (X509Key) pKey;
        }
        // take care of spki
        sb.append(pp.indent(4) + resource.getString(PrettyPrintResources.TOKEN_CERTIFICATE) + "\n");
        sb.append(pp.indent(8) + resource.getString(PrettyPrintResources.TOKEN_DATA) + "\n");
        sb.append(pp.indent(12) + resource.getString(PrettyPrintResources.TOKEN_VERSION) + " v");
        sb.append((mX509Cert.getVersion() + 1) + "\n");
        sb.append(pp.indent(12) + resource.getString(PrettyPrintResources.TOKEN_SERIAL) + "0x" + serial2 + "\n");
        // XXX I18N Algorithm Name ?
        sb.append(pp.indent(12) + resource.getString(PrettyPrintResources.TOKEN_SIGALG) + mX509Cert.getSigAlgName() + " - " + mX509Cert.getSigAlgOID() + "\n");
        // XXX I18N IssuerDN ?
        sb.append(pp.indent(12) + resource.getString(PrettyPrintResources.TOKEN_ISSUER) + mX509Cert.getIssuerX500Principal() + "\n");
        sb.append(pp.indent(12) + resource.getString(PrettyPrintResources.TOKEN_VALIDITY) + "\n");
        String notBefore = dateFormater.format(mX509Cert.getNotBefore());
        String notAfter = dateFormater.format(mX509Cert.getNotAfter());
        // get timezone and timezone ID
        if (TimeZone.getDefault() != null) {
            tz = TimeZone.getDefault().getDisplayName(TimeZone.getDefault().inDaylightTime(mX509Cert.getNotBefore()), TimeZone.SHORT, clientLocale);
            tzid = TimeZone.getDefault().getID();
        }
        // Specify notBefore
        if (tz.equals(tzid) || tzid.equals(CUSTOM_LOCALE)) {
            // Do NOT append timezone ID
            sb.append(pp.indent(16) + resource.getString(PrettyPrintResources.TOKEN_NOT_BEFORE) + notBefore + "\n");
        } else {
            // Append timezone ID
            sb.append(pp.indent(16) + resource.getString(PrettyPrintResources.TOKEN_NOT_BEFORE) + notBefore + " " + tzid + "\n");
        }
        // re-get timezone (just in case it is different . . .)
        if (TimeZone.getDefault() != null) {
            tz = TimeZone.getDefault().getDisplayName(TimeZone.getDefault().inDaylightTime(mX509Cert.getNotAfter()), TimeZone.SHORT, clientLocale);
        }
        // Specify notAfter
        if (tz.equals(tzid) || tzid.equals(CUSTOM_LOCALE)) {
            // Do NOT append timezone ID
            sb.append(pp.indent(16) + resource.getString(PrettyPrintResources.TOKEN_NOT_AFTER) + notAfter + "\n");
        } else {
            // Append timezone ID
            sb.append(pp.indent(16) + resource.getString(PrettyPrintResources.TOKEN_NOT_AFTER) + notAfter + " " + tzid + "\n");
        }
        // XXX I18N SubjectDN ?
        sb.append(pp.indent(12) + resource.getString(PrettyPrintResources.TOKEN_SUBJECT) + mX509Cert.getSubjectX500Principal() + "\n");
        sb.append(pp.indent(12) + resource.getString(PrettyPrintResources.TOKEN_SPKI) + "\n");
        PubKeyPrettyPrint pkpp = new PubKeyPrettyPrint(key);
        sb.append(pkpp.toString(clientLocale, 16, 16));
        // take care of extensions
        CertificateExtensions extensions = (CertificateExtensions) info.get(X509CertInfo.EXTENSIONS);
        sb.append(pp.indent(12) + resource.getString(PrettyPrintResources.TOKEN_EXTENSIONS) + "\n");
        if (extensions != null)
            for (int i = 0; i < extensions.size(); i++) {
                Extension ext = extensions.elementAt(i);
                ExtPrettyPrint extpp = new ExtPrettyPrint(ext, 16);
                sb.append(extpp.toString());
            }
        // take care of signature
        sb.append(pp.indent(8) + resource.getString(PrettyPrintResources.TOKEN_SIGNATURE) + "\n");
        // XXX I18N Algorithm Name ?
        sb.append(pp.indent(12) + resource.getString(PrettyPrintResources.TOKEN_ALGORITHM) + mX509Cert.getSigAlgName() + " - " + mX509Cert.getSigAlgOID() + "\n");
        sb.append(pp.indent(12) + resource.getString(PrettyPrintResources.TOKEN_SIGNATURE) + "\n");
        sb.append(pp.toHexString(mX509Cert.getSignature(), 16, 16));
        // fingerprints
        String[] hashes = new String[] { "MD2", "MD5", "SHA-1", "SHA-256", "SHA-512" };
        StringBuffer certFingerprints = new StringBuffer();
        sb.append(pp.indent(8) + "FingerPrint\n");
        for (int i = 0; i < hashes.length; i++) {
            MessageDigest md = MessageDigest.getInstance(hashes[i]);
            md.update(mX509Cert.getEncoded());
            certFingerprints.append(pp.indent(12) + hashes[i] + ":\n" + pp.toHexString(md.digest(), 16, 16));
        }
        sb.append(certFingerprints.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}
Also used : X509CertInfo(org.mozilla.jss.netscape.security.x509.X509CertInfo) PublicKey(java.security.PublicKey) CertificateExtensions(org.mozilla.jss.netscape.security.x509.CertificateExtensions) X509Key(org.mozilla.jss.netscape.security.x509.X509Key) CertificateX509Key(org.mozilla.jss.netscape.security.x509.CertificateX509Key) CertificateX509Key(org.mozilla.jss.netscape.security.x509.CertificateX509Key) Extension(org.mozilla.jss.netscape.security.x509.Extension) DateFormat(java.text.DateFormat) ResourceBundle(java.util.ResourceBundle) MessageDigest(java.security.MessageDigest)

Aggregations

CertificateX509Key (org.mozilla.jss.netscape.security.x509.CertificateX509Key)3 X509Key (sun.security.x509.X509Key)3 CertificateExtensions (org.mozilla.jss.netscape.security.x509.CertificateExtensions)2 X509CertInfo (org.mozilla.jss.netscape.security.x509.X509CertInfo)2 X509Key (org.mozilla.jss.netscape.security.x509.X509Key)2 KeyPair (java.security.KeyPair)1 KeyPairGenerator (java.security.KeyPairGenerator)1 MessageDigest (java.security.MessageDigest)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 Signature (java.security.Signature)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 DateFormat (java.text.DateFormat)1 GregorianCalendar (java.util.GregorianCalendar)1 ResourceBundle (java.util.ResourceBundle)1 BigInt (org.mozilla.jss.netscape.security.util.BigInt)1 DerValue (org.mozilla.jss.netscape.security.util.DerValue)1 CertificateAlgorithmId (org.mozilla.jss.netscape.security.x509.CertificateAlgorithmId)1 CertificateSerialNumber (org.mozilla.jss.netscape.security.x509.CertificateSerialNumber)1 CertificateSubjectName (org.mozilla.jss.netscape.security.x509.CertificateSubjectName)1