-
Notifications
You must be signed in to change notification settings - Fork 263
/
ElytronXmlParserTest.java
178 lines (152 loc) · 8.92 KB
/
ElytronXmlParserTest.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
/*
* JBoss, Home of Professional Open Source.
* Copyright 2019 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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.wildfly.security.auth.client;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.security.auth.x500.X500Principal;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.wildfly.client.config.ConfigXMLParseException;
import org.wildfly.security.SecurityFactory;
import org.wildfly.security.credential.PasswordCredential;
import org.wildfly.security.password.Password;
import org.wildfly.security.password.interfaces.ClearPassword;
import org.wildfly.security.x500.cert.SelfSignedX509CertificateAndSigningKey;
import org.wildfly.security.x500.cert.X509CertificateBuilder;
/**
* @author Tomas Hofman (thofman@redhat.com)
*/
public class ElytronXmlParserTest {
private static File KEYSTORE_DIR = new File("./target/keystore");
private static final String CLIENT_KEYSTORE_FILENAME = "/client.keystore";
private static final char[] PASSWORD = "password".toCharArray();
/**
* ELY-1428
*/
private static void createClientKeyStore(KeyStore clientKeyStore) throws Exception {
// Generate testclient2.example.com self signed certificate
X500Principal testClient2DN = new X500Principal("CN=testclient2.example.com, OU=JBoss, O=Red Hat, L=Raleigh, ST=North Carolina, C=US");
SelfSignedX509CertificateAndSigningKey testClient2SelfSignedX509CertificateAndSigningKey = SelfSignedX509CertificateAndSigningKey.builder()
.setKeyAlgorithmName("DSA")
.setSignatureAlgorithmName("SHA1withDSA")
.setDn(testClient2DN)
.setKeySize(1024)
.build();
X509Certificate testClient2Certificate = testClient2SelfSignedX509CertificateAndSigningKey.getSelfSignedCertificate();
clientKeyStore.setKeyEntry("dnsincnclient", testClient2SelfSignedX509CertificateAndSigningKey.getSigningKey(), PASSWORD, new X509Certificate[]{testClient2Certificate});
// Generate Test Authority self signed certificate
X500Principal testAuthorityDN = new X500Principal("CN=Test Authority, OU=JBoss, O=Red Hat, L=Raleigh, ST=North Carolina, C=US");
SelfSignedX509CertificateAndSigningKey testAuthoritySelfSignedX509CertificateAndSigningKey = SelfSignedX509CertificateAndSigningKey.builder()
.setDn(testAuthorityDN)
.setKeyAlgorithmName("RSA")
.setSignatureAlgorithmName("SHA1withRSA")
.build();
X509Certificate testAuthorityCertificate = testAuthoritySelfSignedX509CertificateAndSigningKey.getSelfSignedCertificate();
clientKeyStore.setKeyEntry("testauthority", testAuthoritySelfSignedX509CertificateAndSigningKey.getSigningKey(), PASSWORD, new X509Certificate[]{testAuthorityCertificate});
// Generate Test Client 1 self signed certificate
X500Principal testClient1DN = new X500Principal("CN=Test Client 1, OU=JBoss, O=Red Hat, L=Raleigh, ST=North Carolina, C=US");
SelfSignedX509CertificateAndSigningKey testClient1SelfSignedX509CertificateAndSigningKey = SelfSignedX509CertificateAndSigningKey.builder()
.setDn(testClient1DN)
.setKeyAlgorithmName("RSA")
.setSignatureAlgorithmName("SHA1withRSA")
.addExtension(false, "SubjectAlternativeName", "DNS:testclient1.example.com")
.build();
X509Certificate testClient1Certificate = testClient1SelfSignedX509CertificateAndSigningKey.getSelfSignedCertificate();
clientKeyStore.setKeyEntry("testclient1", testClient1SelfSignedX509CertificateAndSigningKey.getSigningKey(), PASSWORD, new X509Certificate[]{testClient1Certificate});
// Generate Signed Test Client certificate signed by Test Authority
X500Principal signedTestClientDN = new X500Principal("CN=Signed Test Client, OU=JBoss, O=Red Hat, ST=North Carolina, C=US");
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
KeyPair signedTestClientGeneratedKeys = keyPairGenerator.generateKeyPair();
PrivateKey signedTestClientSigningKey = signedTestClientGeneratedKeys.getPrivate();
PublicKey signedTestClientPublicKey = signedTestClientGeneratedKeys.getPublic();
X509Certificate signedTestClientCertificate = new X509CertificateBuilder()
.setIssuerDn(testAuthorityDN)
.setSubjectDn(signedTestClientDN)
.setSignatureAlgorithmName("SHA1withRSA")
.setSigningKey(testAuthoritySelfSignedX509CertificateAndSigningKey.getSigningKey())
.setPublicKey(signedTestClientPublicKey)
.build();
clientKeyStore.setKeyEntry("testclientsignedbyca", signedTestClientSigningKey, PASSWORD, new X509Certificate[]{signedTestClientCertificate, testAuthorityCertificate});
}
@Test
public void testKeyStoreClearPassword() throws ConfigXMLParseException, URISyntaxException {
URL config = getClass().getResource("test-wildfly-config-v1_4.xml");
SecurityFactory<AuthenticationContext> authContext = ElytronXmlParser.parseAuthenticationClientConfiguration(config.toURI());
Assert.assertNotNull(authContext);
}
@Test
public void testKeyStoreMaskedPassword() throws Exception {
URL config = getClass().getResource("test-wildfly-config-v1_4.xml");
SecurityFactory<AuthenticationContext> authContext = ElytronXmlParser.parseAuthenticationClientConfiguration(config.toURI());
Assert.assertNotNull(authContext);
RuleNode<SecurityFactory<SSLContext>> node = authContext.create().sslRuleMatching(new URI("http://masked/"), null, null);
Assert.assertNotNull(node);
Assert.assertNotNull(node.getConfiguration().create());
}
@Test
public void testClearCredential() throws Exception {
URL config = getClass().getResource("test-wildfly-config-v1_4.xml");
SecurityFactory<AuthenticationContext> authContext = ElytronXmlParser.parseAuthenticationClientConfiguration(config.toURI());
Assert.assertNotNull(authContext);
RuleNode<AuthenticationConfiguration> node = authContext.create().authRuleMatching(new URI("http://clear/"), null, null);
Assert.assertNotNull(node);
Password password = node.getConfiguration().getCredentialSource().getCredential(PasswordCredential.class).getPassword();
Assert.assertTrue(password instanceof ClearPassword);
Assert.assertEquals(new String(PASSWORD), new String(((ClearPassword)password).getPassword()));
}
@Test
public void testMaskedCredential() throws Exception {
URL config = getClass().getResource("test-wildfly-config-v1_4.xml");
SecurityFactory<AuthenticationContext> authContext = ElytronXmlParser.parseAuthenticationClientConfiguration(config.toURI());
Assert.assertNotNull(authContext);
RuleNode<AuthenticationConfiguration> node = authContext.create().authRuleMatching(new URI("http://masked/"), null, null);
Assert.assertNotNull(node);
Password password = node.getConfiguration().getCredentialSource().getCredential(PasswordCredential.class).getPassword();
Assert.assertEquals(new String(PASSWORD), new String(((ClearPassword)password).getPassword()));
}
@BeforeClass
public static void prepareKeyStores() throws Exception {
if (KEYSTORE_DIR.exists() == false) {
KEYSTORE_DIR.mkdirs();
}
KeyStore clientKeyStore = KeyStore.getInstance("JKS");
clientKeyStore.load(null, null);
createClientKeyStore(clientKeyStore);
File clientFile = new File(KEYSTORE_DIR, CLIENT_KEYSTORE_FILENAME);
try (FileOutputStream clientStream = new FileOutputStream(clientFile)){
clientKeyStore.store(clientStream, PASSWORD);
}
}
@AfterClass
public static void removeProvider() {
Assert.assertTrue("Keystore deleted", new File(KEYSTORE_DIR, CLIENT_KEYSTORE_FILENAME).delete());
Assert.assertTrue("Keystore directory deleted", KEYSTORE_DIR.delete());
}
}