@Test public void testChooseKeyMaterial() throws Exception { OpenSslKeyMaterialProvider provider = newMaterialProvider(newKeyManagerFactory(), PASSWORD); OpenSslKeyMaterial nonExistingMaterial = provider.chooseKeyMaterial( UnpooledByteBufAllocator.DEFAULT, NON_EXISTING_ALIAS); assertNull(nonExistingMaterial); OpenSslKeyMaterial material = provider.chooseKeyMaterial(UnpooledByteBufAllocator.DEFAULT, EXISTING_ALIAS); assertNotNull(material); assertNotEquals(0, material.certificateChainAddress()); assertNotEquals(0, material.privateKeyAddress()); assertRelease(material); provider.destroy(); }
OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) throws Exception { X509Certificate[] certificates = keyManager.getCertificateChain(alias); if (certificates == null || certificates.length == 0) { return null; } PrivateKey key = keyManager.getPrivateKey(alias); PemEncoded encoded = PemX509Certificate.toPEM(allocator, true, certificates); long chainBio = 0; long pkeyBio = 0; long chain = 0; long pkey = 0; try { chainBio = toBIO(allocator, encoded.retain()); chain = SSL.parseX509Chain(chainBio); OpenSslKeyMaterial keyMaterial; if (key instanceof OpenSslPrivateKey) { keyMaterial = ((OpenSslPrivateKey) key).toKeyMaterial(chain); } else { pkeyBio = toBIO(allocator, key); pkey = key == null ? 0 : SSL.parsePrivateKey(pkeyBio, password); keyMaterial = new DefaultOpenSslKeyMaterial(chain, pkey); } chain = 0; pkey = 0; return keyMaterial; } finally { SSL.freeBIO(chainBio); SSL.freeBIO(pkeyBio); if (chain != 0) { SSL.freeX509Chain(chain); } if (pkey != 0) { SSL.freePrivateKey(pkey); } encoded.release(); } }
OpenSslKeyMaterialProvider { OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) throws Exception { X509Certificate[] certificates = keyManager.getCertificateChain(alias); if (certificates == null || certificates.length == 0) { return null; } PrivateKey key = keyManager.getPrivateKey(alias); PemEncoded encoded = PemX509Certificate.toPEM(allocator, true, certificates); long chainBio = 0; long pkeyBio = 0; long chain = 0; long pkey = 0; try { chainBio = toBIO(allocator, encoded.retain()); chain = SSL.parseX509Chain(chainBio); OpenSslKeyMaterial keyMaterial; if (key instanceof OpenSslPrivateKey) { keyMaterial = ((OpenSslPrivateKey) key).toKeyMaterial(chain); } else { pkeyBio = toBIO(allocator, key); pkey = key == null ? 0 : SSL.parsePrivateKey(pkeyBio, password); keyMaterial = new DefaultOpenSslKeyMaterial(chain, pkey); } chain = 0; pkey = 0; return keyMaterial; } finally { SSL.freeBIO(chainBio); SSL.freeBIO(pkeyBio); if (chain != 0) { SSL.freeX509Chain(chain); } if (pkey != 0) { SSL.freePrivateKey(pkey); } encoded.release(); } } }
OpenSslKeyMaterialProvider { OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) throws Exception { X509Certificate[] certificates = keyManager.getCertificateChain(alias); if (certificates == null || certificates.length == 0) { return null; } PrivateKey key = keyManager.getPrivateKey(alias); PemEncoded encoded = PemX509Certificate.toPEM(allocator, true, certificates); long chainBio = 0; long pkeyBio = 0; long chain = 0; long pkey = 0; try { chainBio = toBIO(allocator, encoded.retain()); chain = SSL.parseX509Chain(chainBio); OpenSslKeyMaterial keyMaterial; if (key instanceof OpenSslPrivateKey) { keyMaterial = ((OpenSslPrivateKey) key).toKeyMaterial(chain); } else { pkeyBio = toBIO(allocator, key); pkey = key == null ? 0 : SSL.parsePrivateKey(pkeyBio, password); keyMaterial = new DefaultOpenSslKeyMaterial(chain, pkey); } chain = 0; pkey = 0; return keyMaterial; } finally { SSL.freeBIO(chainBio); SSL.freeBIO(pkeyBio); if (chain != 0) { SSL.freeX509Chain(chain); } if (pkey != 0) { SSL.freePrivateKey(pkey); } encoded.release(); } } OpenSslKeyMaterialProvider(X509KeyManager keyManager, String password); }
OpenSslKeyMaterialProvider { OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) throws Exception { X509Certificate[] certificates = keyManager.getCertificateChain(alias); if (certificates == null || certificates.length == 0) { return null; } PrivateKey key = keyManager.getPrivateKey(alias); PemEncoded encoded = PemX509Certificate.toPEM(allocator, true, certificates); long chainBio = 0; long pkeyBio = 0; long chain = 0; long pkey = 0; try { chainBio = toBIO(allocator, encoded.retain()); chain = SSL.parseX509Chain(chainBio); OpenSslKeyMaterial keyMaterial; if (key instanceof OpenSslPrivateKey) { keyMaterial = ((OpenSslPrivateKey) key).toKeyMaterial(chain); } else { pkeyBio = toBIO(allocator, key); pkey = key == null ? 0 : SSL.parsePrivateKey(pkeyBio, password); keyMaterial = new DefaultOpenSslKeyMaterial(chain, pkey); } chain = 0; pkey = 0; return keyMaterial; } finally { SSL.freeBIO(chainBio); SSL.freeBIO(pkeyBio); if (chain != 0) { SSL.freeX509Chain(chain); } if (pkey != 0) { SSL.freePrivateKey(pkey); } encoded.release(); } } OpenSslKeyMaterialProvider(X509KeyManager keyManager, String password);  }
OpenSslKeyMaterialProvider { OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) throws Exception { X509Certificate[] certificates = keyManager.getCertificateChain(alias); if (certificates == null || certificates.length == 0) { return null; } PrivateKey key = keyManager.getPrivateKey(alias); PemEncoded encoded = PemX509Certificate.toPEM(allocator, true, certificates); long chainBio = 0; long pkeyBio = 0; long chain = 0; long pkey = 0; try { chainBio = toBIO(allocator, encoded.retain()); chain = SSL.parseX509Chain(chainBio); OpenSslKeyMaterial keyMaterial; if (key instanceof OpenSslPrivateKey) { keyMaterial = ((OpenSslPrivateKey) key).toKeyMaterial(chain); } else { pkeyBio = toBIO(allocator, key); pkey = key == null ? 0 : SSL.parsePrivateKey(pkeyBio, password); keyMaterial = new DefaultOpenSslKeyMaterial(chain, pkey); } chain = 0; pkey = 0; return keyMaterial; } finally { SSL.freeBIO(chainBio); SSL.freeBIO(pkeyBio); if (chain != 0) { SSL.freeX509Chain(chain); } if (pkey != 0) { SSL.freePrivateKey(pkey); } encoded.release(); } } OpenSslKeyMaterialProvider(X509KeyManager keyManager, String password);   }