|
| 1 | +package iot.technology.mqtt.server.ssl; |
| 2 | + |
| 3 | +import iot.technology.mqtt.server.utils.ResourceUtils; |
| 4 | +import lombok.Data; |
| 5 | +import lombok.EqualsAndHashCode; |
| 6 | +import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; |
| 7 | +import org.bouncycastle.cert.X509CertificateHolder; |
| 8 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 9 | +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; |
| 10 | +import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; |
| 11 | +import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder; |
| 12 | +import org.bouncycastle.openssl.PEMParser; |
| 13 | +import org.bouncycastle.openssl.PEMKeyPair; |
| 14 | +import org.bouncycastle.openssl.PEMDecryptorProvider; |
| 15 | +import org.bouncycastle.openssl.PEMEncryptedKeyPair; |
| 16 | +import org.springframework.util.StringUtils; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.io.InputStreamReader; |
| 21 | +import java.security.GeneralSecurityException; |
| 22 | +import java.security.KeyStore; |
| 23 | +import java.security.PrivateKey; |
| 24 | +import java.security.Security; |
| 25 | +import java.security.cert.CertPath; |
| 26 | +import java.security.cert.Certificate; |
| 27 | +import java.security.cert.CertificateFactory; |
| 28 | +import java.security.cert.X509Certificate; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import java.util.stream.Collectors; |
| 32 | + |
| 33 | +@Data |
| 34 | +@EqualsAndHashCode(callSuper = false) |
| 35 | +public class PemSslCredentials extends AbstractSslCredentials { |
| 36 | + |
| 37 | + public static final String DEFAULT_KEY_ALIAS = "server"; |
| 38 | + |
| 39 | + private String certFile; |
| 40 | + private String keyFile; |
| 41 | + private String keyPassword; |
| 42 | + |
| 43 | + @Override |
| 44 | + protected boolean canUse() { |
| 45 | + return ResourceUtils.resourceExists(this, this.certFile); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected KeyStore loadKeyStore(boolean trustsOnly, char[] keyPasswordArray) throws IOException, GeneralSecurityException { |
| 50 | + if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) { |
| 51 | + Security.addProvider(new BouncyCastleProvider()); |
| 52 | + } |
| 53 | + List<X509Certificate> certificates = new ArrayList<>(); |
| 54 | + PrivateKey privateKey = null; |
| 55 | + JcaX509CertificateConverter certConverter = new JcaX509CertificateConverter(); |
| 56 | + JcaPEMKeyConverter keyConverter = new JcaPEMKeyConverter(); |
| 57 | + try (InputStream inStream = ResourceUtils.getInputStream(this, this.certFile)) { |
| 58 | + try (PEMParser pemParser = new PEMParser(new InputStreamReader(inStream))) { |
| 59 | + Object object; |
| 60 | + while ((object = pemParser.readObject()) != null) { |
| 61 | + if (object instanceof X509CertificateHolder) { |
| 62 | + X509Certificate x509Cert = certConverter.getCertificate((X509CertificateHolder) object); |
| 63 | + certificates.add(x509Cert); |
| 64 | + } else if (object instanceof PEMEncryptedKeyPair) { |
| 65 | + PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(keyPasswordArray); |
| 66 | + privateKey = keyConverter.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(decProv)).getPrivate(); |
| 67 | + } else if (object instanceof PEMKeyPair) { |
| 68 | + privateKey = keyConverter.getKeyPair((PEMKeyPair) object).getPrivate(); |
| 69 | + } else if (object instanceof PrivateKeyInfo) { |
| 70 | + privateKey = keyConverter.getPrivateKey((PrivateKeyInfo) object); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + if (privateKey == null && !StringUtils.isEmpty(this.keyFile)) { |
| 76 | + if (ResourceUtils.resourceExists(this, this.keyFile)) { |
| 77 | + try (InputStream inStream = ResourceUtils.getInputStream(this, this.keyFile)) { |
| 78 | + try (PEMParser pemParser = new PEMParser(new InputStreamReader(inStream))) { |
| 79 | + Object object; |
| 80 | + while ((object = pemParser.readObject()) != null) { |
| 81 | + if (object instanceof PEMEncryptedKeyPair) { |
| 82 | + PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(keyPasswordArray); |
| 83 | + privateKey = keyConverter.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(decProv)).getPrivate(); |
| 84 | + break; |
| 85 | + } else if (object instanceof PEMKeyPair) { |
| 86 | + privateKey = keyConverter.getKeyPair((PEMKeyPair) object).getPrivate(); |
| 87 | + break; |
| 88 | + } else if (object instanceof PrivateKeyInfo) { |
| 89 | + privateKey = keyConverter.getPrivateKey((PrivateKeyInfo) object); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + if (certificates.isEmpty()) { |
| 97 | + throw new IllegalArgumentException("No certificates found in certFile: " + this.certFile); |
| 98 | + } |
| 99 | + if (privateKey == null && !trustsOnly) { |
| 100 | + throw new IllegalArgumentException("Unable to load private key neither from certFile: " + this.certFile |
| 101 | + + " nor form keyFile: " + this.keyFile); |
| 102 | + } |
| 103 | + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 104 | + keyStore.load(null); |
| 105 | + if (trustsOnly) { |
| 106 | + List<Certificate> unique = certificates.stream().distinct().collect(Collectors.toList()); |
| 107 | + for (int i = 0; i < unique.size(); i++) { |
| 108 | + keyStore.setCertificateEntry("root-" + i, unique.get(i)); |
| 109 | + } |
| 110 | + } |
| 111 | + if (privateKey != null) { |
| 112 | + CertificateFactory factory = CertificateFactory.getInstance("X.509"); |
| 113 | + CertPath certPath = factory.generateCertPath(certificates); |
| 114 | + List<? extends Certificate> path = certPath.getCertificates(); |
| 115 | + Certificate[] x509Certificates = path.toArray(new Certificate[0]); |
| 116 | + keyStore.setKeyEntry(DEFAULT_KEY_ALIAS, privateKey, keyPasswordArray, x509Certificates); |
| 117 | + } |
| 118 | + return keyStore; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + protected void updateKeyAlias(String keyAlias) { |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String getKeyAlias() { |
| 127 | + return DEFAULT_KEY_ALIAS; |
| 128 | + } |
| 129 | +} |
0 commit comments