(KeyPair keyPair,
X509Certificate cert, String alias, File file, char[] pwd)
| 1121 | } |
| 1122 | |
| 1123 | public static final void writeKeyPairToFile(KeyPair keyPair, |
| 1124 | X509Certificate cert, String alias, File file, char[] pwd) { |
| 1125 | FileInputStream input = null; |
| 1126 | FileOutputStream output = null; |
| 1127 | try { |
| 1128 | KeyStore keyStore = KeyStore.getInstance("PKCS12"); |
| 1129 | if (file.exists()) { |
| 1130 | input = new FileInputStream(file); |
| 1131 | keyStore.load(new FileInputStream(file), pwd); |
| 1132 | input.close(); |
| 1133 | } else { |
| 1134 | keyStore.load(null); // weird but required |
| 1135 | } |
| 1136 | |
| 1137 | // save my private key |
| 1138 | keyStore.setKeyEntry(alias, keyPair.getPrivate(), pwd, |
| 1139 | new X509Certificate[] { cert }); |
| 1140 | |
| 1141 | // store away the keystore |
| 1142 | output = new java.io.FileOutputStream(file); |
| 1143 | keyStore.store(output, pwd); |
| 1144 | output.flush(); |
| 1145 | } catch (Exception e) { |
| 1146 | log.error("Error while storing key: " + e.getMessage(), e); |
| 1147 | } finally { |
| 1148 | if (input != null) { |
| 1149 | try { |
| 1150 | input.close(); |
| 1151 | } catch (IOException e) { |
| 1152 | // ignore while closing |
| 1153 | log.trace("Error while closing: " + e.getMessage(), e); |
| 1154 | } |
| 1155 | } |
| 1156 | if (output != null) { |
| 1157 | try { |
| 1158 | output.close(); |
| 1159 | } catch (IOException e) { |
| 1160 | // ignore while closing |
| 1161 | log.trace("Error while closing: " + e.getMessage(), e); |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | private static final X509Certificate createCertificate(KeyPair keyPair, |
| 1168 | String algorithm) { |
no test coverage detected