Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

按照文档设置tls双向认证不成功 #137

Open
larychen opened this issue Feb 22, 2024 · 1 comment
Open

按照文档设置tls双向认证不成功 #137

larychen opened this issue Feb 22, 2024 · 1 comment

Comments

@larychen
Copy link

按照文档: 生成了服务端和客户端证书
在控制台配置了服务端证书和根证书开启了客户端验证
image
使用mqttx工具配置客户端证书和根证书可以连接成功
image
使用java paho客户端按照列子 设置不成功

` Security.addProvider(new BouncyCastleProvider());

    // Load CA certificates
    KeyStore caKs = loadCAKeyStore(caCrtFile);

    // Load client certificate chain and key
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null, null);

    // Load the entire client certificate chain
    Certificate[] chain;
    try (FileInputStream fis = new FileInputStream(crtFile)) {
        CertificateFactory cf = CertificateFactory.getInstance("X509");
        Collection<? extends Certificate> certs = cf.generateCertificates(fis);
        chain = certs.toArray(new Certificate[0]);
    }

    // Load client private key
    try (PEMParser pemParser = new PEMParser(new FileReader(keyFile))) {
        Object object = pemParser.readObject();
        JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
        PrivateKey key = converter.getPrivateKey((PrivateKeyInfo) object);
        ks.setKeyEntry("private-key", key, password.toCharArray(), chain);
    }

    // Set up key managers and trust managers
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
    tmf.init(caKs);
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(ks, password.toCharArray());

    // finally, create SSL socket factory
    SSLContext context = SSLContext.getInstance("TLSv1.2");
    context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

    return context.getSocketFactory();`

反复检查了证书路径没有问题
请问下这个有什么方法处理吗

@Kiki513H
Copy link

Kiki513H commented Jul 3, 2024

最后怎么解决的 这种方式连接不成功 就得通过Ng 不通过Ng 是没办法成功的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants