Which is the correct way to use javax.ws.rs.client.Client for SSL connections with .p12 file?

Our application should make use of javax.ws.rs.client.Client (whose implementation is provided by resteasy-jaxrs-3.0.19.Final.jar) to perform an HTTP POST request to an external service whose URL starts with https.
The developers of the external service provided us a .p12 file to be used as certificate. First of all we run the command

openssl pkcs12 -info -in file.p12 -out filecertificate.crt -nodes

to generate the .cert file and tried to connect to the HTTPS service by this command, according to the specifications received from the providers of the service:

curl --cert /home/ec2-user/filecertificate.crt  --location 'https://veryverylongurl?client_id=effYyJAMjw49TdOg' --header 'X-Gravitee-Api-Key: x-y-z-k' --header 'Content-Type: application/json' --header 'Authorization: Basic Base64encodeddata' --data 'json object here'

This connections succeedes and now we must replicate the same thing inside our product in Java via the library indicated before. That’s our code:

public DtoDiscoveryServiceToken getTokenByCredential(String baseURI, String clientId, String apiKey, String certificatePassword, byte[] signature, String user, String password) {
        final String credentials = user+":"+password;
        final DtoDiscoveryServiceTokenInput dtoDiscoveryServiceTokenInput = new DtoDiscoveryServiceTokenInput(true);
        
        Client newClient = null;
        try {
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            keyStore.load(new ByteArrayInputStream(signature), certificatePassword.toCharArray());

            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keyStore, certificatePassword.toCharArray());
            
            SSLContext sslContext = SSLContext.getInstance("SSL");
        
            sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
            
            newClient = ClientBuilder.newBuilder()
                    .sslContext(sslContext)
                    .build();
            
        } catch (KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | UnrecoverableKeyException e) {
            e.printStackTrace();
        }
        
        
        Response response = newClient
                .target(baseURI)
                .path("veryverylongurl?client_id={clientId}")
                .resolveTemplate("clientId", clientId)
                .request(MediaType.APPLICATION_JSON)
                .header("X-Gravitee-Api-Key", apiKey)
                .header(HttpHeaders.AUTHORIZATION, "Basic "+Base64.getEncoder().encodeToString(credentials.getBytes()))
                .accept(MediaType.APPLICATION_JSON)
                .post(Entity.json(dtoDiscoveryServiceTokenInput));
        
        try {
            if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
                throw new WebApplicationException(response.getStatusInfo().getReasonPhrase(), response.getStatus());
            }
            
            boolean hasEntity = response.hasEntity();
            
            if(hasEntity) {
                return response.readEntity(DtoDiscoveryServiceToken.class);
            }
            else {
                return null;
            }

        } finally {
            response.close();
        }
    }

where:

  • user and password are the credentials of the standard user which must authenticate to the service
  • certificatePassword is the password of the p12 file provided by the service developers
  • signature is the p12 file loaded from database as array of bytes
  • apiKey is the header X-Gravitee-Api-Key: x-y-z-k you can see in the curl command
  • clientId is the identifier to be injected in the URL (the one of previous curl command)
  • baseURI is the HTTPS uri of the server hosting the service.
  • DtoDiscoveryServiceTokenInput is an object to be serialized as JSON to pass as data (always see the curl command)

The code does not work and we get simply a 400 Bad Request without further details. So, how shall we implement the previous code? Shall we use the .p12 file or the .cert one? Shall we perform some transformations on the files before proceeding with the call to the URL?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật