{"id":629,"date":"2015-05-19T23:57:12","date_gmt":"2015-05-19T23:57:12","guid":{"rendered":"http:\/\/192.168.1.102\/?p=629"},"modified":"2015-11-04T00:37:02","modified_gmt":"2015-11-04T00:37:02","slug":"java-8-tomcat-8-ssl-setup-from-pfx-using-443","status":"publish","type":"post","link":"https:\/\/digitalsos.net\/?p=629","title":{"rendered":"Java 8, tomcat 8, SSL setup from pfx, using 443"},"content":{"rendered":"<p>This took me a day to setup on a new CentOS Amazon image. To be honest I&#8217;d never configured SSL for tomcat before, and this was the first time that I&#8217;d used tomcat8. So I just want to go over the steps I had to do so I&#8217;ll remember all of the tweeks needed.<\/p>\n<p>Configuring SSL was more painful than I expected. First issue was that I had to break up the Microsoft IIS formated certificate I had. Fortunatly that I&#8217;ve done before. From <a href=\"http:\/\/www.novell.com\/support\/kb\/doc.php?id=7004039\" target=\"_blank\">novell<\/a><\/p>\n<p>First create a new folder for all of this.<br \/>\nType: mkdir cert Type: cd cert<br \/>\nNow get the Intermediate and root certificates from your CA place them in the folder.<br \/>\nGet the .pfx certificate and put it in the folder.<\/p>\n<p>To export the private key without a passphrase or password.<br \/>\nType: openssl pkcs12 -in filename.pfx -nocerts -nodes -out key.pem<\/p>\n<p>To Generate a public version of the private RSAkey<br \/>\nType: openssl rsa -in key.pem -out server.key<\/p>\n<p>To export the Certificate<br \/>\nType: openssl pkcs12 -in filename.pfx -clcerts -nokeys -out cert.pem<\/p>\n<p>The directory will now have a file cert.pem and a key.pem<\/p>\n<p>Now from <a href=\"http:\/\/mail-archives.apache.org\/mod_mbox\/tomcat-users\/200409.mbox\/%3C4150C297.8030802@ddai.net%3E\" target=\"_blank\">apache.org<\/a><br \/>\nAssuming:<\/p>\n<p>* key.pem &#8211; your certificate&#8217;s private key<br \/>\n* cert.pem &#8211; your certificate<br \/>\n* domainIntermediate.crt &#8211; Organization Validation intermediate<br \/>\n* inter.crt &#8211; the intermediate CA that signed your certificate<br \/>\n* root.crt &#8211; the root CA that signed the intermediate CA<\/p>\n<p>First, concatenate the CA certs, make sure the intermediate CA goes first:<br \/>\n<strong>Note on this &#8211; For the chain ON A NORMAL LOAD BALANCER, it&#8217;s intermediate first then domain Intermediate then the root, BUT if you want a unified cert like we are doing here the order is different, it would be domain Intermediate, then CA Intermediate, then the CA Root.\u00a0 Makes no sense to me but for <a href=\"https:\/\/community.centminmod.com\/threads\/positive-ssl-certificate-chain-order.1293\/\" target=\"_blank\">Comodo it is so<\/a>.<\/strong><\/p>\n<p>$ cat domainIntermediate.crt inter.crt root.crt &gt; chain.crt<\/p>\n<p>Next, export the pkcs12 file:<\/p>\n<p>$ openssl pkcs12 -export -chain -inkey key.pem -in cert.pem\\<br \/>\n-name &#8220;server&#8221; -CAfile chain.crt -out server.p12<\/p>\n<p>When prompt for export password, enter something and don&#8217;t leave it empty.<\/p>\n<p>Now, use keytool to verify:<\/p>\n<p>$ keytool -list -v -storetype pkcs12 -keystore server.p12<\/p>\n<p>Enter the export password for the keystore password. Then you should see<br \/>\na line like this from the output:<\/p>\n<p>&#8230;.<br \/>\nCertificate chain length: 3<br \/>\n&#8230;.<\/p>\n<p>Tomcat8 should now be able to use that server.p12 file as it&#8217;s keystore.<br \/>\nMove the server.p12 to the tomcat home directory which is \/usr\/share\/tomcat8\/<br \/>\nMake sure tomcat is the owner, Type: chmod tomcat:tomcat server.p12<br \/>\nThis server needs to use 443 instead of 8443. To do that we need to tweek java permissions.<br \/>\nI used the guide at <a href=\"https:\/\/confluence.atlassian.com\/display\/CONFKB\/Permission+Denied+Error+when+Binding+a+Port\" target=\"_blank\">confluence<\/a> but used the 5th option:<\/p>\n<p>If using Linux 2.6.24 or later, you can set up a file capability on the java executable, to give elevated privileges to allow opening privileged ports only, and no other superuser privileges:<br \/>\n# setcap cap_net_bind_service+ep \/path\/to\/bin\/java<br \/>\nAfter setting this you may notice errors when starting Java like this, for example:<br \/>\n$ java -version<br \/>\n\/path\/to\/bin\/java: error while loading shared libraries: libjli.so: cannot open shared object file: No such file or directory<br \/>\nThis means that the library is being imported from a dynamic path, and not in the trusted ld.so path. See http:\/\/bugs.sun.com\/view_bug.do?bug_id=7157699 for details. To fix this, you need to locate the library, and add its path to the ld.so configuration. Note that the below is an example, and this may differ depending on Linux distribution. Replace JAVA_HOME with the correct location:<br \/>\n$ find JAVA_HOME -name &#8216;libjli.so&#8217;<br \/>\nJAVA_HOME\/lib\/amd64\/jli\/libjli.so<\/p>\n<p># echo &#8220;JAVA_HOME\/lib\/amd64\/jli&#8221; &gt; \/etc\/ld.so.conf.d\/java-libjli.conf<br \/>\n# ldconfig -v<br \/>\nAfter setting this all up, you need to make sure that Confluence only starts java with the direct binary path, and not via a symbolic link, otherwise the capability will not be picked up.<br \/>\nSetting this up means that any user can open privileged ports using Java, which may or may not be acceptable for you<\/p>\n<p>At this point I usually switch user to tomcat. to do that edit \/etc\/passwd and change tomcat user to use \/bin\/bash<br \/>\nthen as root su tomcat<\/p>\n<p>We need to edit \/etc\/tomcat8\/server.xml<br \/>\nAdd a new connector like this:<br \/>\n&lt;Connector<br \/>\nprotocol=&#8221;org.apache.coyote.http11.Http11NioProtocol&#8221;<br \/>\nport=&#8221;443&#8243; maxThreads=&#8221;200&#8243;<br \/>\nscheme=&#8221;https&#8221; secure=&#8221;true&#8221; SSLEnabled=&#8221;true&#8221;<br \/>\nkeystoreFile=&#8221;${user.home}\/server.p12&#8243; keystoreType=&#8221;PKCS12&#8243; keystorePass=&#8221;changeit&#8221;<br \/>\nclientAuth=&#8221;false&#8221; sslProtocol=&#8221;TLS&#8221;\/&gt;<\/p>\n<p>Also in my case the application was to live on the root so to do that find the host section and add Context like so:<\/p>\n<p>&lt;Host appBase=&#8221;webapps&#8221; autoDeploy=&#8221;true&#8221; name=&#8221;localhost&#8221; unpackWARs=&#8221;true&#8221;&gt;<br \/>\n&lt;Context docBase=&#8221;\/var\/lib\/tomcat8\/webapps\/YourAppName&#8221; path=&#8221;&#8221; reloadable=&#8221;true&#8221; \/&gt;<\/p>\n<p>exit out of the tomcat account change it back to nologin then restart tomcat. Easy right?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This took me a day to setup on a new CentOS Amazon image. To be honest I&#8217;d never configured SSL for tomcat before, and this was the first time that I&#8217;d used tomcat8. So I just want to go over the steps I had to do so I&#8217;ll remember all of the tweeks needed. Configuring<\/p>\n<div class=\"read-more-wrapper\"><a class=\"read-more\" href=\"https:\/\/digitalsos.net\/?p=629\" title=\"Read More\"> <span class=\"button \">Read More<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[19],"tags":[25],"class_list":["post-629","post","type-post","status-publish","format-standard","hentry","category-linux","tag-tomcat-java-openssl-keystore"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pgxmGV-a9","_links":{"self":[{"href":"https:\/\/digitalsos.net\/index.php?rest_route=\/wp\/v2\/posts\/629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digitalsos.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digitalsos.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digitalsos.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/digitalsos.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=629"}],"version-history":[{"count":5,"href":"https:\/\/digitalsos.net\/index.php?rest_route=\/wp\/v2\/posts\/629\/revisions"}],"predecessor-version":[{"id":664,"href":"https:\/\/digitalsos.net\/index.php?rest_route=\/wp\/v2\/posts\/629\/revisions\/664"}],"wp:attachment":[{"href":"https:\/\/digitalsos.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digitalsos.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digitalsos.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}