{"id":1598,"date":"2023-03-23T08:53:36","date_gmt":"2023-03-22T19:53:36","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=1598"},"modified":"2023-03-23T08:53:36","modified_gmt":"2023-03-22T19:53:36","slug":"retrieving-the-x509-certificate-information-in-java","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=1598","title":{"rendered":"Retrieving the X509 Certificate Information in Java"},"content":{"rendered":"<pre><code class=\"language-java\">\/\/This will hold the target leaf certificate.\nOptional&lt;Certificate&gt; cert;\n\ntry {\n    \/\/Must hold the target URL.\n    final var targetUrl = &quot;https:\/\/www.google.com&quot;;\n    final var httpsURL = new URL(targetUrl);\n    final var connection = (HttpsURLConnection) httpsURL.openConnection();\n    connection.connect();\n\n    \/\/Retrieve the first certificate. This is normally the leaf certificate.\n    cert = Arrays.stream(connection.getServerCertificates()).findFirst();\n} catch (IOException e) {\n    throw new RuntimeException(e);\n}\n\ncert.ifPresent(cer -&gt; {\n    \/\/Check if the instance of certificate is of type of X509Certificate.\n    if(cer instanceof final X509Certificate x509) {\n        System.out.println(&quot;Subject: &quot; + x509.getSubjectX500Principal().getName());\n        System.out.println(&quot;From: &quot; + x509.getNotBefore());\n        System.out.println(&quot;To: &quot; + x509.getNotAfter());\n        System.out.println(&quot;Duration: &quot; + ChronoUnit.DAYS.between(LocalDate.now(), LocalDate.ofInstant(x509.getNotAfter().toInstant(),ZoneId.systemDefault())));\n        System.out.println(&quot;\\n&quot;);\n    }\n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/This will hold the target leaf certificate. Optional&lt;Certificate&gt; cert; try { \/\/Must hold the target URL. final var targetUrl = &quot;https:\/\/www.google.com&quot;; final var httpsURL = new URL(targetUrl); final var connection = (HttpsURLConnection) httpsURL.openConnection(); connection.connect(); \/\/Retrieve the first certificate. This is normally the leaf certificate. cert = Arrays.stream(connection.getServerCertificates()).findFirst(); } catch (IOException e) { throw new RuntimeException(e); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[74],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1598"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1598"}],"version-history":[{"count":1,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1598\/revisions"}],"predecessor-version":[{"id":1599,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1598\/revisions\/1599"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}