878 // Read the full stream before feeding to X509Factory, |
878 // Read the full stream before feeding to X509Factory, |
879 // otherwise, keytool -gencert | keytool -importcert |
879 // otherwise, keytool -gencert | keytool -importcert |
880 // might not work properly, since -gencert is slow |
880 // might not work properly, since -gencert is slow |
881 // and there's no data in the pipe at the beginning. |
881 // and there's no data in the pipe at the beginning. |
882 ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
882 ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
883 byte[] b = new byte[4096]; |
|
884 while (true) { |
|
885 int len = inStream.read(b); |
|
886 if (len < 0) break; |
|
887 bout.write(b, 0, len); |
|
888 } |
|
889 inStream = new ByteArrayInputStream(bout.toByteArray()); |
|
890 try { |
883 try { |
891 String importAlias = (alias!=null)?alias:keyAlias; |
884 byte[] b = new byte[4096]; |
892 if (keyStore.entryInstanceOf(importAlias, KeyStore.PrivateKeyEntry.class)) { |
885 while (true) { |
893 kssave = installReply(importAlias, inStream); |
886 int len = inStream.read(b); |
894 if (kssave) { |
887 if (len < 0) break; |
895 System.err.println(rb.getString |
888 bout.write(b, 0, len); |
896 ("Certificate reply was installed in keystore")); |
|
897 } else { |
|
898 System.err.println(rb.getString |
|
899 ("Certificate reply was not installed in keystore")); |
|
900 } |
|
901 } else if (!keyStore.containsAlias(importAlias) || |
|
902 keyStore.entryInstanceOf(importAlias, |
|
903 KeyStore.TrustedCertificateEntry.class)) { |
|
904 kssave = addTrustedCert(importAlias, inStream); |
|
905 if (kssave) { |
|
906 System.err.println(rb.getString |
|
907 ("Certificate was added to keystore")); |
|
908 } else { |
|
909 System.err.println(rb.getString |
|
910 ("Certificate was not added to keystore")); |
|
911 } |
|
912 } |
889 } |
913 } finally { |
890 } finally { |
914 if (inStream != System.in) { |
891 if (inStream != System.in) { |
915 inStream.close(); |
892 inStream.close(); |
|
893 } |
|
894 } |
|
895 inStream = new ByteArrayInputStream(bout.toByteArray()); |
|
896 String importAlias = (alias!=null)?alias:keyAlias; |
|
897 if (keyStore.entryInstanceOf(importAlias, KeyStore.PrivateKeyEntry.class)) { |
|
898 kssave = installReply(importAlias, inStream); |
|
899 if (kssave) { |
|
900 System.err.println(rb.getString |
|
901 ("Certificate reply was installed in keystore")); |
|
902 } else { |
|
903 System.err.println(rb.getString |
|
904 ("Certificate reply was not installed in keystore")); |
|
905 } |
|
906 } else if (!keyStore.containsAlias(importAlias) || |
|
907 keyStore.entryInstanceOf(importAlias, |
|
908 KeyStore.TrustedCertificateEntry.class)) { |
|
909 kssave = addTrustedCert(importAlias, inStream); |
|
910 if (kssave) { |
|
911 System.err.println(rb.getString |
|
912 ("Certificate was added to keystore")); |
|
913 } else { |
|
914 System.err.println(rb.getString |
|
915 ("Certificate was not added to keystore")); |
916 } |
916 } |
917 } |
917 } |
918 } else if (command == IMPORTKEYSTORE) { |
918 } else if (command == IMPORTKEYSTORE) { |
919 doImportKeyStore(); |
919 doImportKeyStore(); |
920 kssave = true; |
920 kssave = true; |