forked from zaproxy/zaproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SslCertificateServiceImpl.java
205 lines (181 loc) · 8.45 KB
/
SslCertificateServiceImpl.java
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
/*
* Zed Attack Proxy (ZAP) and its related class files.
*
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
*
* Copyright 2011 mawoki@ymail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.parosproxy.paros.security;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPrivateKey;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import org.bouncycastle.asn1.x500.X500NameBuilder;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.asn1.x509.BasicConstraints;
import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x509.GeneralNames;
import org.bouncycastle.asn1.x509.SubjectKeyIdentifier;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
/**
* This is a singleton class. Use {@link #getService()} method to
* obtain a service bean. This implementation is totally unbuffered and creates
* every time you call {@link SslCertificateService#createCertForHost(String, InetAddress)} a new certificate.
* If you want to have a cached solution, have a look at {@link CachedSslCertifificateServiceImpl}.
* This class is designed to be thread safe.
*
* A word about serial numbers ... There have to be different serial numbers
* generated, cause if multiple certificates with different finger prints
* do have the same serial from the same CA, the browser gets crazy.
* At least, Firefox v3.x does.
*
* @author MaWoKi
* @see CachedSslCertifificateServiceImpl for a cached SslCertificateService
*/
public final class SslCertificateServiceImpl implements SslCertificateService {
private X509Certificate caCert = null;
private PublicKey caPubKey = null;
private PrivateKey caPrivKey = null;
private final AtomicLong serial;
private static final SslCertificateService singleton = new SslCertificateServiceImpl();
private SslCertificateServiceImpl() {
Security.addProvider(new BouncyCastleProvider());
final Random rnd = new Random();
rnd.setSeed(System.currentTimeMillis());
// prevent browser certificate caches, cause of doubled serial numbers
// using 48bit random number
long sl = ((long)rnd.nextInt()) << 32 | (rnd.nextInt() & 0xFFFFFFFFL);
// let reserve of 16 bit for increasing, serials have to be positive
sl = sl & 0x0000FFFFFFFFFFFFL;
this.serial = new AtomicLong(sl);
}
@Override
public synchronized void initializeRootCA(KeyStore keystore) throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException {
if (keystore == null) {
this.caCert = null;
this.caPrivKey = null;
this.caPubKey = null;
} else {
this.caCert = (X509Certificate)keystore.getCertificate(ZAPROXY_JKS_ALIAS);
this.caPrivKey = (RSAPrivateKey) keystore.getKey(ZAPROXY_JKS_ALIAS, SslCertificateService.PASSPHRASE);
this.caPubKey = this.caCert.getPublicKey();
}
}
@Override
public KeyStore createCertForHost(String hostname, InetAddress listeningAddress) throws NoSuchAlgorithmException, InvalidKeyException, CertificateException, NoSuchProviderException, SignatureException, KeyStoreException, IOException, UnrecoverableKeyException {
if (this.caCert == null || this.caPrivKey == null || this.caPubKey == null) {
throw new MissingRootCertificateException(this.getClass() + " wasn't initialized! Got to options 'Dynamic SSL Certs' and create one.");
}
final KeyPair mykp = this.createKeyPair();
final PrivateKey privKey = mykp.getPrivate();
final PublicKey pubKey = mykp.getPublic();
X500NameBuilder namebld = new X500NameBuilder(BCStyle.INSTANCE);
if (hostname != null) {
namebld.addRDN(BCStyle.CN, hostname);
}
namebld.addRDN(BCStyle.OU, "Zed Attack Proxy Project");
namebld.addRDN(BCStyle.O, "OWASP");
namebld.addRDN(BCStyle.C, "xx");
namebld.addRDN(BCStyle.EmailAddress, "owasp-zed-attack-proxy@lists.owasp.org");
X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder (
new X509CertificateHolder(caCert.getEncoded()).getSubject(),
BigInteger.valueOf(serial.getAndIncrement()),
new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30),
new Date(System.currentTimeMillis() + 100*(1000L * 60 * 60 * 24 * 30)),
namebld.build(),
pubKey
);
certGen.addExtension(Extension.subjectKeyIdentifier, false, new SubjectKeyIdentifier(pubKey.getEncoded()));
certGen.addExtension(Extension.basicConstraints, false, new BasicConstraints(false));
ArrayList<GeneralName> subjectAlternativeNames = new ArrayList<>();
boolean subjectAlternativeNamesIsCritical = true;
if (hostname != null) {
subjectAlternativeNames.add(new GeneralName(GeneralName.dNSName, hostname));
subjectAlternativeNamesIsCritical = false;
}
if(!listeningAddress.isAnyLocalAddress()){
subjectAlternativeNames.add(new GeneralName(GeneralName.iPAddress, listeningAddress.getHostAddress()));
}
if (hostname == null && listeningAddress.isAnyLocalAddress()) {
throw new IllegalArgumentException("Error, 'hostname' is null and listeningAddress is a wildcard address!");
}
GeneralName[] subjectAlternativeNamesArray = new GeneralName[subjectAlternativeNames.size()];
certGen.addExtension(Extension.subjectAlternativeName, subjectAlternativeNamesIsCritical, new GeneralNames(subjectAlternativeNames.toArray(subjectAlternativeNamesArray)));
ContentSigner sigGen;
try {
sigGen = new JcaContentSignerBuilder("SHA256WithRSAEncryption").setProvider("BC").build(caPrivKey);
} catch (OperatorCreationException e) {
throw new CertificateException(e);
}
final X509Certificate cert = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certGen.build(sigGen));
cert.checkValidity(new Date());
cert.verify(caPubKey);
final KeyStore ks = KeyStore.getInstance("JKS");
ks.load(null, null);
final Certificate[] chain = new Certificate[2];
chain[1] = this.caCert;
chain[0] = cert;
ks.setKeyEntry(ZAPROXY_JKS_ALIAS, privKey, PASSPHRASE, chain);
return ks;
}
/**
* Generates a 2048 bit RSA key pair using SHA1PRNG.
*
* @return the key pair
* @throws NoSuchAlgorithmException if no provider supports the used algorithms.
*/
private KeyPair createKeyPair() throws NoSuchAlgorithmException {
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
final SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(Long.toString(System.currentTimeMillis()).getBytes());
keyGen.initialize(2048, random);
final KeyPair keypair = keyGen.generateKeyPair();
return keypair;
}
/**
* @return return the current {@link SslCertificateService}
*/
public static SslCertificateService getService() {
return singleton;
}
}