Skip to content

Commit

Permalink
Import all certificates to single entry when adding to the keystore (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesk40 authored and havetisyan committed Dec 6, 2018
1 parent 771d2e0 commit 627a2a9
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 8 deletions.
28 changes: 21 additions & 7 deletions libs/java/cert_refresher/src/main/java/com/oath/auth/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.KeyManager;
Expand Down Expand Up @@ -194,7 +196,7 @@ public static SSLContext buildSSLContext(KeyManagerProxy keyManagerProxy,
*/
public static KeyStore createKeyStore(final String athenzPublicCert, final String athenzPrivateKey) throws Exception {

X509Certificate certificate;
List<? extends Certificate> certificates;
PrivateKey privateKey;
KeyStore keyStore;
File certFile;
Expand All @@ -214,8 +216,13 @@ public static KeyStore createKeyStore(final String athenzPublicCert, final Strin
Thread.sleep(1000);
}
} else {
certFile = new File(Utils.class.getClassLoader().getResource(athenzPublicCert).getFile());
keyFile = new File(Utils.class.getClassLoader().getResource(athenzPrivateKey).getFile());
URL certURL = Utils.class.getClassLoader().getResource(athenzPublicCert);
URL keyURL = Utils.class.getClassLoader().getResource(athenzPrivateKey);
if (null == certURL || null == keyURL) {
throw new IllegalArgumentException("Certificate or private key file is empty.");
}
certFile = new File(certURL.getFile());
keyFile = new File(keyURL.getFile());
}
} catch (Throwable t) {
throw new IllegalArgumentException(t);
Expand All @@ -237,12 +244,19 @@ public static KeyStore createKeyStore(final String athenzPublicCert, final Strin
} else {
throw new IllegalStateException("Unknown object type: " + key.getClass().getName());
}

certificate = (X509Certificate) cf.generateCertificate(publicCertStream);

certificates = (List<? extends Certificate>) cf.generateCertificates(publicCertStream);
if (certificates.isEmpty()) {
throw new IllegalArgumentException("Certificate file contains empty certificate or an invalid certificate.");
}
//We are going to assume that the first one is the main certificate which will be used for the alias
String alias = ((X509Certificate) certificates.get(0)).getSubjectX500Principal().getName();
if (LOG.isDebugEnabled()) {
LOG.debug("{} number of certificates found. Using {} alias to create the keystore", certificates.size(), alias);
}
keyStore = KeyStore.getInstance("JKS");
String alias = certificate.getSubjectX500Principal().getName();
keyStore.load(null);
keyStore.setKeyEntry(alias, privateKey, KEYSTORE_PASSWORD, new X509Certificate[]{certificate});
keyStore.setKeyEntry(alias, privateKey, KEYSTORE_PASSWORD, certificates.toArray(new X509Certificate[certificates.size()]));

} catch (IOException e) {
throw new IllegalArgumentException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
*/
package com.oath.auth;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.security.KeyStore;
import java.security.cert.X509Certificate;
import java.util.Enumeration;

import org.junit.Test;

public class KeyStoreTest {

static final String ALIAS_NAME = "cn=athenz.syncer,o=my test company,l=sunnyvale,st=ca,c=us";

@Test
public void testGetKeyStore() throws Exception {

Expand All @@ -37,10 +44,39 @@ public void testGetKeyStore() throws Exception {
} catch (Exception ignored) {
}
}



@Test
public void testCreateKeyStore() throws Exception {
KeyStore keyStore = Utils.createKeyStore("rsa_public_x509.cert", "rsa_private.key");
assertNotNull(keyStore);
String alias = null;
for (Enumeration<?> e = keyStore.aliases(); e.hasMoreElements(); ) {
alias = (String)e.nextElement();
assertEquals(ALIAS_NAME, alias);
}
X509Certificate[] chain = (X509Certificate[]) keyStore.getCertificateChain(alias);
assertNotNull(chain);
assertTrue(chain.length == 1);
}

@Test
public void testCreateKeyStoreChain() throws Exception {
KeyStore keyStore = Utils.createKeyStore("rsa_public_x510_w_intermediate.cert", "rsa_private.key");
assertNotNull(keyStore);
String alias = null;
for (Enumeration<?> e = keyStore.aliases(); e.hasMoreElements(); ) {
alias = (String)e.nextElement();
assertEquals(ALIAS_NAME, alias);
}

X509Certificate[] chain = (X509Certificate[]) keyStore.getCertificateChain(alias);
assertNotNull(chain);
assertTrue(chain.length == 2);
}

@Test (expected = IllegalArgumentException.class)
public void testCreateKeyStoreEmpty() throws Exception {
Utils.createKeyStore("rsa_public_x510_empty.cert", "rsa_private.key");
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-----BEGIN CERTIFICATE-----
MIICfzCCAimgAwIBAgIJAMIRugkwe7mwMA0GCSqGSIb3DQEBBQUAMGAxCzAJBgNV
BAYTAlVTMQswCQYDVQQIEwJDQTESMBAGA1UEBxMJU3Vubnl2YWxlMRgwFgYDVQQK
Ew9NeSBUZXN0IENvbXBhbnkxFjAUBgNVBAMTDWF0aGVuei5zeW5jZXIwHhcNMTYx
MjA5MjE1NDQ2WhcNMTcxMjA5MjE1NDQ2WjBgMQswCQYDVQQGEwJVUzELMAkGA1UE
CBMCQ0ExEjAQBgNVBAcTCVN1bm55dmFsZTEYMBYGA1UEChMPTXkgVGVzdCBDb21w
YW55MRYwFAYDVQQDEw1hdGhlbnouc3luY2VyMFwwDQYJKoZIhvcNAQEBBQADSwAw
SAJBANImoEeD7LU+x6x5dAnNRFtrbab2yxrDXFXdwKQxlIs2PWfLeo3Xq0e2bOU6
kQDgFz/IkMN3tEyDgQ/Nv0eAbckCAwEAAaOBxTCBwjAdBgNVHQ4EFgQUBi0msMNX
iC2XLpKjXPz1DMf9PoEwgZIGA1UdIwSBijCBh4AUBi0msMNXiC2XLpKjXPz1DMf9
PoGhZKRiMGAxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTESMBAGA1UEBxMJU3Vu
bnl2YWxlMRgwFgYDVQQKEw9NeSBUZXN0IENvbXBhbnkxFjAUBgNVBAMTDWF0aGVu
ei5zeW5jZXKCCQDCEboJMHu5sDAMBgNVHRM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