Roberto Sánchez
2014-07-16 2db2bc932897adde84437498680c43e4e4e4e3bd
src/main/java/net/curisit/securis/utils/SignatureHelper.java
....@@ -19,6 +19,7 @@
1919 import net.curisit.securis.beans.LicenseBean;
2020
2121 import org.apache.commons.io.FileUtils;
22
+import org.apache.commons.io.IOUtils;
2223 import org.apache.commons.net.util.Base64;
2324 import org.apache.logging.log4j.LogManager;
2425 import org.apache.logging.log4j.Logger;
....@@ -79,29 +80,48 @@
7980
8081 }
8182
83
+ private static final String DEFAULT_PUB_KEY_RESOURCE = "/securis_key.pub";
84
+
8285 private PublicKey generatePublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
83
- return generatePublicKey(new File(Params.get(Params.KEYS.PUBLIC_KEY_FILE, System.getenv("SECURIS_PUBLIC_KEY_FILE"))));
86
+ String pubKeyFilePath = System.getenv("SECURIS_PUBLIC_KEY_FILE");
87
+ if (pubKeyFilePath == null)
88
+ pubKeyFilePath = Params.get(Params.KEYS.PUBLIC_KEY_FILE);
89
+ String pubKeyBase64 = null;
90
+ if (pubKeyFilePath != null) {
91
+ File pubKeyFile = new File(pubKeyFilePath);
92
+ if (pubKeyFile.exists()) {
93
+ pubKeyBase64 = FileUtils.readFileToString(pubKeyFile);
94
+ }
95
+ }
96
+ if (pubKeyBase64 == null) {
97
+ pubKeyBase64 = IOUtils.toString(this.getClass().getResourceAsStream(DEFAULT_PUB_KEY_RESOURCE), "utf-8");
98
+ }
99
+ return generatePublicKey(pubKeyBase64);
84100 }
85101
86
- private PublicKey generatePublicKey(File publicKeyFile) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
87
- String pubKeyBase64 = FileUtils.readFileToString(publicKeyFile);
88
- int from = pubKeyBase64.indexOf('\n');
89
- int to = pubKeyBase64.indexOf("-----END", from);
90
- pubKeyBase64 = pubKeyBase64.substring(from, to);
91
-
102
+ private PublicKey generatePublicKey(String pubKeyBase64) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
103
+ if (pubKeyBase64.startsWith("-----BEGIN")) {
104
+ int from = pubKeyBase64.indexOf('\n');
105
+ int to = pubKeyBase64.indexOf("-----END", from);
106
+ pubKeyBase64 = pubKeyBase64.substring(from, to);
107
+ }
92108 KeyFactory keyFactory = KeyFactory.getInstance(DEFAULT_ALGORITHM);
93109 X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(pubKeyBase64));
94110 PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
95
- log.debug("Public key read sucessfully from file: {}", publicKeyFile.getAbsolutePath());
96111 return publicKey;
112
+ }
113
+
114
+ private PublicKey generatePublicKey(File pubKeyFile) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
115
+ return generatePublicKey(FileUtils.readFileToString(pubKeyFile));
97116 }
98117
99118 public PrivateKey generatePrivateKey(File privateKeyFile) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
100119 String privKeyBase64 = FileUtils.readFileToString(privateKeyFile);
101
- int from = privKeyBase64.indexOf('\n');
102
- int to = privKeyBase64.indexOf("-----END", from);
103
- privKeyBase64 = privKeyBase64.substring(from, to);
104
-
120
+ if (privKeyBase64.startsWith("-----BEGIN")) {
121
+ int from = privKeyBase64.indexOf('\n');
122
+ int to = privKeyBase64.indexOf("-----END", from);
123
+ privKeyBase64 = privKeyBase64.substring(from, to);
124
+ }
105125 KeyFactory keyFactory = KeyFactory.getInstance(DEFAULT_ALGORITHM);
106126 PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privKeyBase64));
107127 PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
....@@ -123,4 +143,5 @@
123143 { 64, -31, -81, 36, 99, -77, 100, 17, 16, -119, 31, 72, 123, -88, -32, 51, 39, -96, -35, 116, -65, 8, 41, -119, -108, -34, 41, 19, 26, -102, -16, -120, -96, 1, -5, -26, -13, 61, -121, 94, 59, 54, 110, 110, -55, 127, -106 };
124144 AUX = Base64.encodeBase64String(s);
125145 }
146
+
126147 }