sun.security.pkcs10.PKCS10Attributes Java Examples

The following examples show how to use sun.security.pkcs10.PKCS10Attributes. 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: PKCS10AttrEncoding.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
 
Example #2
Source File: PKCS10AttributeReader.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Decode base64 encoded DER file
        byte[] pkcs10Bytes = Base64.getMimeDecoder().decode(ATTRIBS.getBytes());

        HashMap<ObjectIdentifier, Object> RequestStander = new HashMap() {
            {
                put(PKCS9Attribute.CHALLENGE_PASSWORD_OID, "GuessWhoAmI");
                put(PKCS9Attribute.SIGNING_TIME_OID, new Date(861720610000L));
                put(PKCS9Attribute.CONTENT_TYPE_OID,
                        new ObjectIdentifier("1.9.50.51.52"));
            }
        };

        int invalidNum = 0;
        PKCS10Attributes resp = new PKCS10Attributes(
                new DerInputStream(pkcs10Bytes));
        Enumeration eReq = resp.getElements();
        int numOfAttrs = 0;
        while (eReq.hasMoreElements()) {
            numOfAttrs++;
            PKCS10Attribute attr = (PKCS10Attribute) eReq.nextElement();
            if (RequestStander.containsKey(attr.getAttributeId())) {
                if (RequestStander.get(attr.getAttributeId())
                        .equals(attr.getAttributeValue())) {
                    System.out.println(attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                } else {
                    invalidNum++;
                    System.out.println("< " + attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                    System.out.println("< " + attr.getAttributeId() + " "
                            + RequestStander.get(attr.getAttributeId()));
                }
            } else {
                invalidNum++;
                System.out.println("No" + attr.getAttributeId()
                        + "in Certificate Request list");
            }
        }
        if (numOfAttrs != RequestStander.size()) {
            invalidNum++;
            System.out.println("Incorrect number of attributes.");
        }
        System.out.println();
        if (invalidNum > 0) {
            throw new RuntimeException(
                    "Attributes Compared with Stander :" + " Failed");
        }
        System.out.println("Attributes Compared with Stander: Pass");
    }
 
Example #3
Source File: PKCS10AttrEncoding.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
 
Example #4
Source File: PKCS10AttributeReader.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Decode base64 encoded DER file
        byte[] pkcs10Bytes = Base64.getMimeDecoder().decode(ATTRIBS.getBytes());

        HashMap<ObjectIdentifier, Object> RequestStander = new HashMap() {
            {
                put(PKCS9Attribute.CHALLENGE_PASSWORD_OID, "GuessWhoAmI");
                put(PKCS9Attribute.SIGNING_TIME_OID, new Date(861720610000L));
                put(PKCS9Attribute.CONTENT_TYPE_OID,
                        new ObjectIdentifier("1.9.50.51.52"));
            }
        };

        int invalidNum = 0;
        PKCS10Attributes resp = new PKCS10Attributes(
                new DerInputStream(pkcs10Bytes));
        Enumeration eReq = resp.getElements();
        int numOfAttrs = 0;
        while (eReq.hasMoreElements()) {
            numOfAttrs++;
            PKCS10Attribute attr = (PKCS10Attribute) eReq.nextElement();
            if (RequestStander.containsKey(attr.getAttributeId())) {
                if (RequestStander.get(attr.getAttributeId())
                        .equals(attr.getAttributeValue())) {
                    System.out.println(attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                } else {
                    invalidNum++;
                    System.out.println("< " + attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                    System.out.println("< " + attr.getAttributeId() + " "
                            + RequestStander.get(attr.getAttributeId()));
                }
            } else {
                invalidNum++;
                System.out.println("No" + attr.getAttributeId()
                        + "in Certificate Request list");
            }
        }
        if (numOfAttrs != RequestStander.size()) {
            invalidNum++;
            System.out.println("Incorrect number of attributes.");
        }
        System.out.println();
        if (invalidNum > 0) {
            throw new RuntimeException(
                    "Attributes Compared with Stander :" + " Failed");
        }
        System.out.println("Attributes Compared with Stander: Pass");
    }
 
Example #5
Source File: PKCS10AttrEncoding.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
 
Example #6
Source File: PKCS10AttributeReader.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Decode base64 encoded DER file
        byte[] pkcs10Bytes = Base64.getMimeDecoder().decode(ATTRIBS.getBytes());

        HashMap<ObjectIdentifier, Object> RequestStander = new HashMap() {
            {
                put(PKCS9Attribute.CHALLENGE_PASSWORD_OID, "GuessWhoAmI");
                put(PKCS9Attribute.SIGNING_TIME_OID, new Date(861720610000L));
                put(PKCS9Attribute.CONTENT_TYPE_OID,
                        new ObjectIdentifier("1.9.50.51.52"));
            }
        };

        int invalidNum = 0;
        PKCS10Attributes resp = new PKCS10Attributes(
                new DerInputStream(pkcs10Bytes));
        Enumeration eReq = resp.getElements();
        int numOfAttrs = 0;
        while (eReq.hasMoreElements()) {
            numOfAttrs++;
            PKCS10Attribute attr = (PKCS10Attribute) eReq.nextElement();
            if (RequestStander.containsKey(attr.getAttributeId())) {
                if (RequestStander.get(attr.getAttributeId())
                        .equals(attr.getAttributeValue())) {
                    System.out.println(attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                } else {
                    invalidNum++;
                    System.out.println("< " + attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                    System.out.println("< " + attr.getAttributeId() + " "
                            + RequestStander.get(attr.getAttributeId()));
                }
            } else {
                invalidNum++;
                System.out.println("No" + attr.getAttributeId()
                        + "in Certificate Request list");
            }
        }
        if (numOfAttrs != RequestStander.size()) {
            invalidNum++;
            System.out.println("Incorrect number of attributes.");
        }
        System.out.println();
        if (invalidNum > 0) {
            throw new RuntimeException(
                    "Attributes Compared with Stander :" + " Failed");
        }
        System.out.println("Attributes Compared with Stander: Pass");
    }
 
Example #7
Source File: PKCS10AttrEncoding.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
 
Example #8
Source File: PKCS10AttributeReader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Decode base64 encoded DER file
        byte[] pkcs10Bytes = Base64.getMimeDecoder().decode(ATTRIBS.getBytes());

        HashMap<ObjectIdentifier, Object> RequestStander = new HashMap() {
            {
                put(PKCS9Attribute.CHALLENGE_PASSWORD_OID, "GuessWhoAmI");
                put(PKCS9Attribute.SIGNING_TIME_OID, new Date(861720610000L));
                put(PKCS9Attribute.CONTENT_TYPE_OID,
                        new ObjectIdentifier("1.9.50.51.52"));
            }
        };

        int invalidNum = 0;
        PKCS10Attributes resp = new PKCS10Attributes(
                new DerInputStream(pkcs10Bytes));
        Enumeration eReq = resp.getElements();
        int numOfAttrs = 0;
        while (eReq.hasMoreElements()) {
            numOfAttrs++;
            PKCS10Attribute attr = (PKCS10Attribute) eReq.nextElement();
            if (RequestStander.containsKey(attr.getAttributeId())) {
                if (RequestStander.get(attr.getAttributeId())
                        .equals(attr.getAttributeValue())) {
                    System.out.println(attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                } else {
                    invalidNum++;
                    System.out.println("< " + attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                    System.out.println("< " + attr.getAttributeId() + " "
                            + RequestStander.get(attr.getAttributeId()));
                }
            } else {
                invalidNum++;
                System.out.println("No" + attr.getAttributeId()
                        + "in Certificate Request list");
            }
        }
        if (numOfAttrs != RequestStander.size()) {
            invalidNum++;
            System.out.println("Incorrect number of attributes.");
        }
        System.out.println();
        if (invalidNum > 0) {
            throw new RuntimeException(
                    "Attributes Compared with Stander :" + " Failed");
        }
        System.out.println("Attributes Compared with Stander: Pass");
    }
 
Example #9
Source File: PKCS10AttrEncoding.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
 
Example #10
Source File: PKCS10AttributeReader.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Decode base64 encoded DER file
        byte[] pkcs10Bytes = Base64.getMimeDecoder().decode(ATTRIBS.getBytes());

        HashMap<ObjectIdentifier, Object> RequestStander = new HashMap() {
            {
                put(PKCS9Attribute.CHALLENGE_PASSWORD_OID, "GuessWhoAmI");
                put(PKCS9Attribute.SIGNING_TIME_OID, new Date(861720610000L));
                put(PKCS9Attribute.CONTENT_TYPE_OID,
                        new ObjectIdentifier("1.9.50.51.52"));
            }
        };

        int invalidNum = 0;
        PKCS10Attributes resp = new PKCS10Attributes(
                new DerInputStream(pkcs10Bytes));
        Enumeration eReq = resp.getElements();
        int numOfAttrs = 0;
        while (eReq.hasMoreElements()) {
            numOfAttrs++;
            PKCS10Attribute attr = (PKCS10Attribute) eReq.nextElement();
            if (RequestStander.containsKey(attr.getAttributeId())) {
                if (RequestStander.get(attr.getAttributeId())
                        .equals(attr.getAttributeValue())) {
                    System.out.println(attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                } else {
                    invalidNum++;
                    System.out.println("< " + attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                    System.out.println("< " + attr.getAttributeId() + " "
                            + RequestStander.get(attr.getAttributeId()));
                }
            } else {
                invalidNum++;
                System.out.println("No" + attr.getAttributeId()
                        + "in Certificate Request list");
            }
        }
        if (numOfAttrs != RequestStander.size()) {
            invalidNum++;
            System.out.println("Incorrect number of attributes.");
        }
        System.out.println();
        if (invalidNum > 0) {
            throw new RuntimeException(
                    "Attributes Compared with Stander :" + " Failed");
        }
        System.out.println("Attributes Compared with Stander: Pass");
    }
 
Example #11
Source File: PKCS10AttrEncoding.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
 
Example #12
Source File: PKCS10AttributeReader.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Decode base64 encoded DER file
        byte[] pkcs10Bytes = Base64.getMimeDecoder().decode(ATTRIBS.getBytes());

        HashMap<ObjectIdentifier, Object> RequestStander = new HashMap() {
            {
                put(PKCS9Attribute.CHALLENGE_PASSWORD_OID, "GuessWhoAmI");
                put(PKCS9Attribute.SIGNING_TIME_OID, new Date(861720610000L));
                put(PKCS9Attribute.CONTENT_TYPE_OID,
                        new ObjectIdentifier("1.9.50.51.52"));
            }
        };

        int invalidNum = 0;
        PKCS10Attributes resp = new PKCS10Attributes(
                new DerInputStream(pkcs10Bytes));
        Enumeration eReq = resp.getElements();
        int numOfAttrs = 0;
        while (eReq.hasMoreElements()) {
            numOfAttrs++;
            PKCS10Attribute attr = (PKCS10Attribute) eReq.nextElement();
            if (RequestStander.containsKey(attr.getAttributeId())) {
                if (RequestStander.get(attr.getAttributeId())
                        .equals(attr.getAttributeValue())) {
                    System.out.println(attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                } else {
                    invalidNum++;
                    System.out.println("< " + attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                    System.out.println("< " + attr.getAttributeId() + " "
                            + RequestStander.get(attr.getAttributeId()));
                }
            } else {
                invalidNum++;
                System.out.println("No" + attr.getAttributeId()
                        + "in Certificate Request list");
            }
        }
        if (numOfAttrs != RequestStander.size()) {
            invalidNum++;
            System.out.println("Incorrect number of attributes.");
        }
        System.out.println();
        if (invalidNum > 0) {
            throw new RuntimeException(
                    "Attributes Compared with Stander :" + " Failed");
        }
        System.out.println("Attributes Compared with Stander: Pass");
    }
 
Example #13
Source File: PKCS10AttrEncoding.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
 
Example #14
Source File: PKCS10AttributeReader.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Decode base64 encoded DER file
        byte[] pkcs10Bytes = Base64.getMimeDecoder().decode(ATTRIBS.getBytes());

        HashMap<ObjectIdentifier, Object> RequestStander = new HashMap() {
            {
                put(PKCS9Attribute.CHALLENGE_PASSWORD_OID, "GuessWhoAmI");
                put(PKCS9Attribute.SIGNING_TIME_OID, new Date(861720610000L));
                put(PKCS9Attribute.CONTENT_TYPE_OID,
                        new ObjectIdentifier("1.9.50.51.52"));
            }
        };

        int invalidNum = 0;
        PKCS10Attributes resp = new PKCS10Attributes(
                new DerInputStream(pkcs10Bytes));
        Enumeration eReq = resp.getElements();
        int numOfAttrs = 0;
        while (eReq.hasMoreElements()) {
            numOfAttrs++;
            PKCS10Attribute attr = (PKCS10Attribute) eReq.nextElement();
            if (RequestStander.containsKey(attr.getAttributeId())) {
                if (RequestStander.get(attr.getAttributeId())
                        .equals(attr.getAttributeValue())) {
                    System.out.println(attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                } else {
                    invalidNum++;
                    System.out.println("< " + attr.getAttributeId() + " "
                            + attr.getAttributeValue());
                    System.out.println("< " + attr.getAttributeId() + " "
                            + RequestStander.get(attr.getAttributeId()));
                }
            } else {
                invalidNum++;
                System.out.println("No" + attr.getAttributeId()
                        + "in Certificate Request list");
            }
        }
        if (numOfAttrs != RequestStander.size()) {
            invalidNum++;
            System.out.println("Incorrect number of attributes.");
        }
        System.out.println();
        if (invalidNum > 0) {
            throw new RuntimeException(
                    "Attributes Compared with Stander :" + " Failed");
        }
        System.out.println("Attributes Compared with Stander: Pass");
    }