|
1 | 1 | package com.snowflake.kafka.connector.internal; |
2 | 2 |
|
3 | 3 | import com.snowflake.kafka.connector.Utils; |
| 4 | +import net.snowflake.client.jdbc.internal.apache.commons.codec.binary.Base64; |
| 5 | +import net.snowflake.client.jdbc.internal.org.bouncycastle.jce.provider.BouncyCastleProvider; |
4 | 6 | import net.snowflake.ingest.connection.IngestStatus; |
5 | | -import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; |
6 | | -import org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider; |
7 | | -import org.bouncycastle.openssl.PEMKeyPair; |
8 | | -import org.bouncycastle.openssl.PEMParser; |
9 | | -import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; |
10 | | -import org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder; |
11 | | -import org.bouncycastle.operator.InputDecryptorProvider; |
12 | | -import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; |
13 | 7 | import org.slf4j.Logger; |
14 | 8 | import org.slf4j.LoggerFactory; |
15 | 9 |
|
16 | | -import java.io.StringReader; |
17 | 10 | import java.security.KeyFactory; |
18 | 11 | import java.security.PrivateKey; |
19 | | -import java.security.Security; |
20 | 12 | import java.security.spec.PKCS8EncodedKeySpec; |
21 | 13 | import java.sql.ResultSet; |
22 | 14 | import java.sql.SQLException; |
@@ -82,70 +74,19 @@ static void assertNotEmpty(String name, Object value) |
82 | 74 | } |
83 | 75 | } |
84 | 76 |
|
85 | | - static PrivateKey parseEncryptedPrivateKey(String key, String passphrase) |
86 | | - { |
87 | | - //remove header, footer, and line breaks |
88 | | - key = key.replaceAll("-+[A-Za-z ]+-+", ""); |
89 | | - key = key.replaceAll("\\s", ""); |
90 | | - |
91 | | - StringBuilder builder = new StringBuilder(); |
92 | | - builder.append("-----BEGIN ENCRYPTED PRIVATE KEY-----"); |
93 | | - for (int i = 0; i < key.length(); i++) |
94 | | - { |
95 | | - if (i % 64 == 0) |
96 | | - { |
97 | | - builder.append("\n"); |
98 | | - } |
99 | | - builder.append(key.charAt(i)); |
100 | | - } |
101 | | - builder.append("\n-----END ENCRYPTED PRIVATE KEY-----"); |
102 | | - key = builder.toString(); |
103 | | - Security.addProvider(new BouncyCastleFipsProvider()); |
104 | | - try |
105 | | - { |
106 | | - PEMParser pemParser = new PEMParser(new StringReader(key)); |
107 | | - PKCS8EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = |
108 | | - (PKCS8EncryptedPrivateKeyInfo) pemParser.readObject(); |
109 | | - pemParser.close(); |
110 | | - InputDecryptorProvider pkcs8Prov = |
111 | | - new JceOpenSSLPKCS8DecryptorProviderBuilder().build(passphrase.toCharArray()); |
112 | | - JcaPEMKeyConverter converter = |
113 | | - new JcaPEMKeyConverter().setProvider(BouncyCastleFipsProvider.PROVIDER_NAME); |
114 | | - PrivateKeyInfo decryptedPrivateKeyInfo = |
115 | | - encryptedPrivateKeyInfo.decryptPrivateKeyInfo(pkcs8Prov); |
116 | | - return converter.getPrivateKey(decryptedPrivateKeyInfo); |
117 | | - } catch (Exception e) |
118 | | - { |
119 | | - throw SnowflakeErrors.ERROR_0018.getException(e); |
120 | | - } |
121 | | - } |
122 | | - |
123 | 77 | static PrivateKey parsePrivateKey(String key) |
124 | 78 | { |
125 | 79 | //remove header, footer, and line breaks |
126 | 80 | key = key.replaceAll("-+[A-Za-z ]+-+", ""); |
127 | 81 | key = key.replaceAll("\\s", ""); |
128 | 82 |
|
129 | | - StringBuilder builder = new StringBuilder(); |
130 | | - builder.append("-----BEGIN RSA PRIVATE KEY-----"); |
131 | | - for (int i = 0; i < key.length(); i++) |
132 | | - { |
133 | | - if (i % 64 == 0) |
134 | | - { |
135 | | - builder.append("\n"); |
136 | | - } |
137 | | - builder.append(key.charAt(i)); |
138 | | - } |
139 | | - builder.append("\n-----END RSA PRIVATE KEY-----"); |
140 | | - key = builder.toString(); |
| 83 | + java.security.Security.addProvider(new BouncyCastleProvider()); |
| 84 | + byte[] encoded = Base64.decodeBase64(key); |
141 | 85 | try |
142 | 86 | { |
143 | | - PEMParser pemParser = new PEMParser(new StringReader(key)); |
144 | | - PEMKeyPair pemKeyPair = (PEMKeyPair) pemParser.readObject(); |
145 | | - PKCS8EncodedKeySpec keySpec = |
146 | | - new PKCS8EncodedKeySpec(pemKeyPair.getPrivateKeyInfo().getEncoded()); |
147 | | - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
148 | | - return keyFactory.generatePrivate(keySpec); |
| 87 | + KeyFactory kf = KeyFactory.getInstance("RSA"); |
| 88 | + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded); |
| 89 | + return kf.generatePrivate(keySpec); |
149 | 90 | } catch (Exception e) |
150 | 91 | { |
151 | 92 | throw SnowflakeErrors.ERROR_0002.getException(e); |
@@ -217,8 +158,9 @@ static Properties createProperties(Map<String, String> conf) |
217 | 158 |
|
218 | 159 | if (!privateKeyPassphrase.isEmpty()) |
219 | 160 | { |
220 | | - properties.put(JDBC_PRIVATE_KEY, parseEncryptedPrivateKey(privateKey, |
221 | | - privateKeyPassphrase)); |
| 161 | + properties.put(JDBC_PRIVATE_KEY, |
| 162 | + EncryptionUtils.parseEncryptedPrivateKey(privateKey, |
| 163 | + privateKeyPassphrase)); |
222 | 164 | } |
223 | 165 | else if (!privateKey.isEmpty()) |
224 | 166 | { |
|
0 commit comments