| .. | .. |
|---|
| 19 | 19 | import net.curisit.securis.beans.LicenseBean; |
|---|
| 20 | 20 | |
|---|
| 21 | 21 | import org.apache.commons.io.FileUtils; |
|---|
| 22 | +import org.apache.commons.io.IOUtils; |
|---|
| 22 | 23 | import org.apache.commons.net.util.Base64; |
|---|
| 23 | 24 | import org.apache.logging.log4j.LogManager; |
|---|
| 24 | 25 | import org.apache.logging.log4j.Logger; |
|---|
| .. | .. |
|---|
| 79 | 80 | |
|---|
| 80 | 81 | } |
|---|
| 81 | 82 | |
|---|
| 83 | + private static final String DEFAULT_PUB_KEY_RESOURCE = "/securis_key.pub"; |
|---|
| 84 | + |
|---|
| 82 | 85 | 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); |
|---|
| 84 | 100 | } |
|---|
| 85 | 101 | |
|---|
| 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 | + } |
|---|
| 92 | 108 | KeyFactory keyFactory = KeyFactory.getInstance(DEFAULT_ALGORITHM); |
|---|
| 93 | 109 | X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(pubKeyBase64)); |
|---|
| 94 | 110 | PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); |
|---|
| 95 | | - log.debug("Public key read sucessfully from file: {}", publicKeyFile.getAbsolutePath()); |
|---|
| 96 | 111 | return publicKey; |
|---|
| 112 | + } |
|---|
| 113 | + |
|---|
| 114 | + private PublicKey generatePublicKey(File pubKeyFile) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { |
|---|
| 115 | + return generatePublicKey(FileUtils.readFileToString(pubKeyFile)); |
|---|
| 97 | 116 | } |
|---|
| 98 | 117 | |
|---|
| 99 | 118 | public PrivateKey generatePrivateKey(File privateKeyFile) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { |
|---|
| 100 | 119 | 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 | + } |
|---|
| 105 | 125 | KeyFactory keyFactory = KeyFactory.getInstance(DEFAULT_ALGORITHM); |
|---|
| 106 | 126 | PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privKeyBase64)); |
|---|
| 107 | 127 | PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); |
|---|
| .. | .. |
|---|
| 123 | 143 | { 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 }; |
|---|
| 124 | 144 | AUX = Base64.encodeBase64String(s); |
|---|
| 125 | 145 | } |
|---|
| 146 | + |
|---|
| 126 | 147 | } |
|---|