Skip to content

Commit cbc7ad0

Browse files
committed
fix: using io.github.hakky54:ayza-for-pem to load KeyManager for PEM
1 parent 936c45b commit cbc7ad0

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@
167167
<artifactId>okhttp-brotli</artifactId>
168168
<version>4.12.0</version>
169169
</dependency>
170+
171+
<dependency>
172+
<groupId>io.github.hakky54</groupId>
173+
<artifactId>ayza-for-pem</artifactId>
174+
<version>10.0.1</version>
175+
</dependency>
170176
</dependencies>
171177
<build>
172178
<plugins>

src/main/java/com/laker/postman/service/http/ssl/ClientCertificateLoader.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package com.laker.postman.service.http.ssl;
22

3+
import cn.hutool.core.io.FileUtil;
4+
import cn.hutool.core.util.StrUtil;
35
import com.laker.postman.model.ClientCertificate;
46
import lombok.extern.slf4j.Slf4j;
7+
import nl.altindag.ssl.pem.util.PemUtils;
58

69
import javax.net.ssl.KeyManager;
710
import javax.net.ssl.KeyManagerFactory;
11+
import javax.net.ssl.X509ExtendedKeyManager;
812
import javax.net.ssl.X509KeyManager;
13+
import java.io.BufferedInputStream;
914
import java.io.FileInputStream;
1015
import java.io.IOException;
1116
import java.net.Socket;
@@ -70,28 +75,16 @@ private static KeyManager[] createKeyManagersFromPFX(ClientCertificate cert) thr
7075
* 从 PEM 文件创建 KeyManager
7176
*/
7277
private static KeyManager[] createKeyManagersFromPEM(ClientCertificate cert) throws Exception {
73-
// 加载证书
74-
X509Certificate certificate = loadCertificateFromPEM(cert.getCertPath());
78+
log.debug("Loaded PEM certificate from: {} and key from: {}", cert.getCertPath(), cert.getKeyPath());
79+
try (BufferedInputStream certInputStream = FileUtil.getInputStream(cert.getCertPath());
80+
BufferedInputStream keyInputStream = FileUtil.getInputStream(cert.getKeyPath())) {
7581

76-
// 加载私钥
77-
PrivateKey privateKey = loadPrivateKeyFromPEM(cert.getKeyPath(), cert.getKeyPassword());
82+
X509ExtendedKeyManager keyManager = StrUtil.isNotBlank(cert.getKeyPassword())
83+
? PemUtils.loadIdentityMaterial(certInputStream, keyInputStream, cert.getKeyPassword().toCharArray())
84+
: PemUtils.loadIdentityMaterial(certInputStream, keyInputStream);
7885

79-
// 创建 KeyStore 并添加证书和私钥
80-
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
81-
keyStore.load(null, null);
82-
83-
Certificate[] certChain = new Certificate[]{certificate};
84-
char[] keyPassword = cert.getKeyPassword() != null ?
85-
cert.getKeyPassword().toCharArray() : new char[0];
86-
87-
keyStore.setKeyEntry("client-cert", privateKey, keyPassword, certChain);
88-
89-
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
90-
kmf.init(keyStore, keyPassword);
91-
92-
log.debug("Loaded PEM certificate from: {} and key from: {}",
93-
cert.getCertPath(), cert.getKeyPath());
94-
return kmf.getKeyManagers();
86+
return new KeyManager[]{keyManager};
87+
}
9588
}
9689

9790
/**

0 commit comments

Comments
 (0)