jdk/test/sun/security/pkcs12/StoreTrustedCertTest.java
changeset 15532 859facd70580
parent 15298 06867bfe82ac
equal deleted inserted replaced
15531:071efc9f31ad 15532:859facd70580
    47 
    47 
    48     public static void main(String[] args) throws Exception {
    48     public static void main(String[] args) throws Exception {
    49 
    49 
    50         new File(KEYSTORE).delete();
    50         new File(KEYSTORE).delete();
    51 
    51 
    52         try {
    52         KeyStore keystore = KeyStore.getInstance("PKCS12");
    53             KeyStore keystore = KeyStore.getInstance("PKCS12");
    53         keystore.load(null, null);
    54             keystore.load(null, null);
       
    55 
    54 
    56             Certificate cert = loadCertificate(CERT);
    55         Certificate cert = loadCertificate(CERT);
    57             Set<KeyStore.Entry.Attribute> attributes = new HashSet<>();
    56         Set<KeyStore.Entry.Attribute> attributes = new HashSet<>();
    58             attributes.add(new PKCS12Attribute("1.3.5.7.9", "that's odd"));
    57         attributes.add(new PKCS12Attribute("1.3.5.7.9", "that's odd"));
    59             attributes.add(new PKCS12Attribute("2.4.6.8.10", "that's even"));
    58         attributes.add(new PKCS12Attribute("2.4.6.8.10", "that's even"));
    60 
    59 
    61             // Set trusted certificate entry
    60         // Set trusted certificate entry
    62             keystore.setEntry(ALIAS,
    61         keystore.setEntry(ALIAS,
    63                 new KeyStore.TrustedCertificateEntry(cert), null);
    62             new KeyStore.TrustedCertificateEntry(cert), null);
    64 
    63 
    65             // Set trusted certificate entry with attributes
    64         // Set trusted certificate entry with attributes
    66             keystore.setEntry(ALIAS2,
    65         keystore.setEntry(ALIAS2,
    67                 new KeyStore.TrustedCertificateEntry(cert, attributes), null);
    66             new KeyStore.TrustedCertificateEntry(cert, attributes), null);
    68 
    67 
       
    68         try (FileOutputStream outStream = new FileOutputStream(KEYSTORE)) {
    69             System.out.println("Storing keystore to: " + KEYSTORE);
    69             System.out.println("Storing keystore to: " + KEYSTORE);
    70             keystore.store(new FileOutputStream(KEYSTORE), PASSWORD);
    70             keystore.store(outStream, PASSWORD);
       
    71         }
    71 
    72 
       
    73         try (FileInputStream inStream = new FileInputStream(KEYSTORE)) {
    72             System.out.println("Loading keystore from: " + KEYSTORE);
    74             System.out.println("Loading keystore from: " + KEYSTORE);
    73             keystore.load(new FileInputStream(KEYSTORE), PASSWORD);
    75             keystore.load(inStream, PASSWORD);
    74             System.out.println("Loaded keystore with " + keystore.size() +
    76             System.out.println("Loaded keystore with " + keystore.size() +
    75                 " entries");
    77                 " entries");
       
    78         }
    76 
    79 
    77             KeyStore.Entry entry = keystore.getEntry(ALIAS, null);
    80         KeyStore.Entry entry = keystore.getEntry(ALIAS, null);
    78             if (entry instanceof KeyStore.TrustedCertificateEntry) {
    81         if (entry instanceof KeyStore.TrustedCertificateEntry) {
    79                 System.out.println("Retrieved trusted certificate entry: " +
    82             System.out.println("Retrieved trusted certificate entry: " + entry);
    80                     entry);
    83         } else {
       
    84             throw new Exception("Not a trusted certificate entry");
       
    85         }
       
    86         System.out.println();
       
    87 
       
    88         entry = keystore.getEntry(ALIAS2, null);
       
    89         if (entry instanceof KeyStore.TrustedCertificateEntry) {
       
    90             KeyStore.TrustedCertificateEntry trustedEntry =
       
    91                 (KeyStore.TrustedCertificateEntry) entry;
       
    92             Set<KeyStore.Entry.Attribute> entryAttributes =
       
    93                 trustedEntry.getAttributes();
       
    94 
       
    95             if (entryAttributes.containsAll(attributes)) {
       
    96                 System.out.println("Retrieved trusted certificate entry " +
       
    97                     "with attributes: " + entry);
    81             } else {
    98             } else {
    82                 throw new Exception("Not a trusted certificate entry");
    99                 throw new Exception("Failed to retrieve entry attributes");
    83             }
   100             }
    84             System.out.println();
   101         } else {
    85 
   102             throw new Exception("Not a trusted certificate entry");
    86             entry = keystore.getEntry(ALIAS2, null);
       
    87             if (entry instanceof KeyStore.TrustedCertificateEntry) {
       
    88                 KeyStore.TrustedCertificateEntry trustedEntry =
       
    89                     (KeyStore.TrustedCertificateEntry) entry;
       
    90                 Set<KeyStore.Entry.Attribute> entryAttributes =
       
    91                     trustedEntry.getAttributes();
       
    92 
       
    93                 if (entryAttributes.containsAll(attributes)) {
       
    94                     System.out.println("Retrieved trusted certificate entry " +
       
    95                         "with attributes: " + entry);
       
    96                 } else {
       
    97                     throw new Exception("Failed to retrieve entry attributes");
       
    98                 }
       
    99             } else {
       
   100                 throw new Exception("Not a trusted certificate entry");
       
   101             }
       
   102 
       
   103         } finally {
       
   104             new File(KEYSTORE).delete();
       
   105         }
   103         }
   106     }
   104     }
   107 
   105 
   108     private static Certificate loadCertificate(String certFile)
   106     private static Certificate loadCertificate(String certFile)
   109         throws Exception {
   107         throws Exception {