SSLHelper.java
8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.codec.binary.Base64
* org.apache.commons.io.FileUtils
* org.apache.commons.io.IOUtils
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.bedrock.internal;
import com.adobe.aemds.bedrock.CoreConfigService;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SSLHelper {
private static final byte[] NEW_LINE = System.getProperty("line.separator", "\n").getBytes();
private Logger log = LoggerFactory.getLogger(SSLHelper.class);
private KeyStore keyStore;
private CoreConfigService coreConfigService;
private char[] password;
public void initialize() throws IOException, GeneralSecurityException {
this.keyStore = this.getKeyStore();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void createCertificateForOpenSSL() throws Exception {
FileOutputStream fosKey = null;
try {
int idx;
Certificate cert = this.getCertificate();
byte[] bCert = cert.getEncoded();
File fCert = new File(this.getServerNativeDir(), "SSLCert.pem");
FileOutputStream fosCert = new FileOutputStream(fCert);
fosCert.write("-----BEGIN CERTIFICATE-----".getBytes());
fosCert.write(NEW_LINE);
byte[] baCert = Base64.encodeBase64((byte[])bCert);
fosCert.write(baCert);
fosCert.write(NEW_LINE);
fosCert.write("-----END CERTIFICATE-----".getBytes());
fosCert.write(NEW_LINE);
fosCert.close();
File fKey = new File(this.getServerNativeDir(), "SSLKey.pem");
fosKey = new FileOutputStream(fKey);
PrivateKey pk = this.getPrivateKey();
byte[] rawkey = pk.getEncoded();
byte[] key = new byte[rawkey.length - 26];
System.arraycopy(rawkey, 26, key, 0, rawkey.length - 26);
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecureRandom rand = new SecureRandom();
byte[] seed = new byte[8];
rand.nextBytes(seed);
IvParameterSpec iv = new IvParameterSpec(seed);
byte[] abyte0 = new byte[24];
byte[] pwd = new String(this.getPassword()).getBytes();
MessageDigest messagedigest = MessageDigest.getInstance("MD5");
messagedigest.update(pwd);
messagedigest.update(iv.getIV());
byte[] abyte2 = messagedigest.digest();
System.arraycopy(abyte2, 0, abyte0, 0, 16);
messagedigest.update(abyte2);
messagedigest.update(pwd);
messagedigest.update(iv.getIV());
abyte2 = messagedigest.digest();
System.arraycopy(abyte2, 0, abyte0, 16, 8);
SecretKeySpec sKey = new SecretKeySpec(abyte0, "DESede");
cipher.init(1, (Key)sKey, iv);
byte[] baKey = cipher.doFinal(key);
fosKey.write("-----BEGIN RSA PRIVATE KEY-----".getBytes());
fosKey.write(NEW_LINE);
fosKey.write("Proc-Type: 4,ENCRYPTED".getBytes());
fosKey.write(NEW_LINE);
fosKey.write("DEK-Info: DES-EDE3-CBC,".getBytes());
fosKey.write(SSLHelper.toHex(iv.getIV()).getBytes());
fosKey.write(NEW_LINE);
fosKey.write(NEW_LINE);
byte[] pemKey = SSLHelper.base64(baKey);
for (idx = 0; idx < pemKey.length - 64; idx += 64) {
fosKey.write(pemKey, idx, 64);
fosKey.write(NEW_LINE);
}
fosKey.write(pemKey, idx, pemKey.length - idx);
fosKey.write(NEW_LINE);
fosKey.write("-----END RSA PRIVATE KEY-----".getBytes());
fosKey.write(NEW_LINE);
fosKey.write("-----BEGIN CERTIFICATE-----".getBytes());
fosKey.write(NEW_LINE);
fosKey.write(baCert);
fosKey.write(NEW_LINE);
fosKey.write("-----END CERTIFICATE-----".getBytes());
fosKey.write(NEW_LINE);
Object var24_23 = null;
}
catch (Throwable var23_25) {
Object var24_24 = null;
IOUtils.closeQuietly((OutputStream)fosKey);
throw var23_25;
}
IOUtils.closeQuietly((OutputStream)fosKey);
{
}
}
private PrivateKey getPrivateKey() throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
return (PrivateKey)this.keyStore.getKey("ads-credentials", this.getPassword());
}
private Certificate getCertificate() throws KeyStoreException {
return this.keyStore.getCertificate("ads-credentials");
}
private KeyStore getKeyStore() throws IOException, GeneralSecurityException {
File svcFile = new File(this.getServerNativeDir());
File keyStoreFile = new File(svcFile, "ads-ssl.jks");
KeyStore keyStore = KeyStore.getInstance("JKS");
FileInputStream fis = FileUtils.openInputStream((File)keyStoreFile);
keyStore.load(fis, this.getPassword());
return keyStore;
}
private char[] getPassword() {
return this.password;
}
private String getServerNativeDir() {
return this.coreConfigService.getServerNativeDir();
}
private static String toHex(byte[] b) {
char[] hex = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char[] buf = new char[b.length * 2];
int j = 0;
for (int i = 0; i < b.length; ++i) {
byte k = b[i];
buf[j++] = hex[k >>> 4 & 15];
buf[j++] = hex[k & 15];
}
return new String(buf);
}
private static byte[] base64(byte[] dataIn) {
int idxIn;
if (dataIn == null) {
return null;
}
byte[] dataOut = new byte[(dataIn.length + 2) / 3 * 4];
int idxOut = 0;
for (idxIn = 0; idxIn < dataIn.length - 2; idxIn += 3) {
dataOut[idxOut++] = (byte)(dataIn[idxIn] >>> 2 & 63);
dataOut[idxOut++] = (byte)(dataIn[idxIn + 1] >>> 4 & 15 | dataIn[idxIn] << 4 & 63);
dataOut[idxOut++] = (byte)(dataIn[idxIn + 2] >>> 6 & 3 | dataIn[idxIn + 1] << 2 & 63);
dataOut[idxOut++] = (byte)(dataIn[idxIn + 2] & 63);
}
if (idxIn < dataIn.length) {
dataOut[idxOut++] = (byte)(dataIn[idxIn] >>> 2 & 63);
if (idxIn < dataIn.length - 1) {
dataOut[idxOut++] = (byte)(dataIn[idxIn + 1] >>> 4 & 15 | dataIn[idxIn] << 4 & 63);
dataOut[idxOut++] = (byte)(dataIn[idxIn + 1] << 2 & 63);
} else {
dataOut[idxOut++] = (byte)(dataIn[idxIn] << 4 & 63);
}
}
for (idxIn = 0; idxIn < idxOut; ++idxIn) {
dataOut[idxIn] = dataOut[idxIn] < 26 ? (byte)(dataOut[idxIn] + 65) : (dataOut[idxIn] < 52 ? (byte)(dataOut[idxIn] + 97 - 26) : (dataOut[idxIn] < 62 ? (byte)(dataOut[idxIn] + 48 - 52) : (dataOut[idxIn] < 63 ? 43 : 47)));
}
while (idxIn < dataOut.length) {
dataOut[idxIn] = 61;
++idxIn;
}
return dataOut;
}
public void setCoreConfigService(CoreConfigService coreConfigService) {
this.coreConfigService = coreConfigService;
}
public void setPassword(char[] password) {
this.password = password;
}
}