顯示具有 SSL 標籤的文章。 顯示所有文章
顯示具有 SSL 標籤的文章。 顯示所有文章

2017年12月29日 星期五

centos6.2 , tomcat6 , LetsEncrypt


wget https://dl.eff.org/certbot-auto

chmod a+x certbot-auto

certbot-auto certonly --webroot -w /var/lib/tomcat6/webapps -d {DomainName} -m {E-Mail}


cd /etc/letsencrypt/live/{DomainName}

--openssl pkcs12 -export -out bundle.pfx -inkey privkey.pem -in cert.pem -certfile chain.pem -password pass:{PASSWORD}


openssl pkcs12 -export -out /etc/letsencrypt/live/www.vghtc.gov.tw/fullchain_and_key.p12     -in /etc/letsencrypt/live/www.vghtc.gov.tw/fullchain.pem     -inkey /etc/letsencrypt/live/www.vghtc.gov.tw/privkey.pem     -name tomcat


最後真的有效的
openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out keystore.p12 -name tomcat -CAfile chain.pem -caname root  //看看能不能上password

keytool -importkeystore -deststorepass cc3fvghtc -destkeypass cc3fvghtc -srckeystore keystore.p12 -destkeystore www.vghtc.gov.tw.jks -srcstoretype pkcs12 -srcstorepass cc3fvghtc -alias tomcat



1202  cp /etc/letsencrypt/live/www.vghtc.gov.tw/www.vghtc.gov.tw.jks /var/lib/tomcat6/


 1232  iptables -I PREROUTING -t nat -p tcp --dport 443 -j REDIRECT --to-port 8443
 1233  iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
 1234  iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 8443 -j ACCEPT
 1242  iptables-save



-------------------------------------
強制改走 https

http://adalf0722.blogspot.tw/2010/12/tomcathttp-80https-443.html

0.先備份web.xml與servel.xml

1.編輯Tomcat安裝目錄conf下的web.xml
  在</welcome-file-list>與</web-app>之間,加入如下的code,然後存檔

<security-constraint>
 <web-resource-collection>
 <web-resource-name>Protected Context</web-resource-name>
 <url-pattern>/*</url-pattern>
 </web-resource-collection>
 <user-data-constraint>
 <transport-guarantee>CONFIDENTIAL</transport-guarantee>
 </user-data-constraint>
 </security-constraint>
2.同樣編輯目錄下的server.xml,確認一下redirectPort是不是已經是443
    <Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="443" />
3.完成,重啟Tomcat一下,就可以試試看囉


https://www.journaldev.com/160/steps-to-configure-ssl-on-tomcat-and-setup-auto-redirect-from-http-to-https


Tomcat Redirect HTTP to HTTPS

So we can access any web application on both HTTP and HTTPS ports. We can set up tomcat to redirect all HTTP request to HTTPS port with some configurations.
  1. In ~TomcatInstallation/conf/server.xml
    For HTTP Connector, set the redirect port to the HTTPS connector port. It will look somewhat like this:
    <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
        <Connector port="8090" maxHttpHeaderSize="8192"
                   maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
                   enableLookups="false" redirectPort="8443" acceptCount="100"
                   connectionTimeout="20000" disableUploadTimeout="true" />
  2. In ~TomcatInstallation/conf/web.xml
    Add below configuration but make sure to add it after all the servlet mapping tags.
    <!-- added by Pankaj for automatic redirect from HTTP to HTTPS -->
    <security-constraint>
    <web-resource-collection>
    <web-resource-name>Entire Application</web-resource-name>
    <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
    <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
    </security-constraint>
Restart the tomcat now and all the HTTP requests will automatically be redirected to HTTPS i.e http://localhost:8080/axis2 will be automatically redirected to https://localhost:8443/axis2
Note: If you don’t want to provide ports in the URLs, then use 80 for HTTP and 443 for HTTPS and in this case you can skip the first step to automatic redirect HTTP requests to HTTPS because it will automatically pick the default port 443.
-------------------------------------
Failed to load keystore type JKS with path jks due to (Permission denied)

把 keystore 搬到 tomcat 家目錄底下(好像在/etc下會看不到 可能是沒有從 root 開始開 read 的關係)

-------------------------------------
https://teddysun.com/153.html

安装完运行yum报错:
Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
解决办法:
vi /etc/yum.repos.d/epel.repo
编辑[epel]下的baseurl前的#号去掉,mirrorlist前添加#号。正确配置如下:
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

-------------------------------------
http://acman.bluenest.net/wordpress/archives/96

2017年8月11日 星期五

apache httpd + SSL

引自 https://letsencrypt.tw/




最後更新日期:2017-03-16。

介紹

Let's Encrypt 是由許多大公司以及各大非營利團體為了推廣 HTTPS 而贊助的一家免費發佈 SSL certificate 的 Certiciate Authority。
這篇文章主要是介紹怎麼使用 Let's Encrypt 所提供的服務,會需要知道如何操作 command line interface。

限制

Let's Encrypt 目前已經正式開放,但有發行數量限制,一般人可能會遇到的:
  • Names/Certificate:單一 certificate 限制 100 個 hostname。
  • Certificates/Domain:每個 domain 每個禮拜最多 20 個 certificate,但 renew 不計算在 quota 內 (需要憑證內的 hostname 與之前完全一樣)。
  • Certificates/FQDNset:相同 hostname 的憑證每個禮拜最多發出五個。。
最新的資訊以及完整的細節請參考「Rate Limits - Let's Encrypt - Free SSL/TLS Certificates」這邊的說明。

安裝過程

我們用到的程式需要 curl,所以先安裝 curl:
# echo "for Debian/Ubuntu"
# apt-get install curl
# echo "for CentOS/RedHat"
# yum install curl
下載最新 release 的 dehydrated 並且解開,目前是 0.4.0:
$ # refer: https://github.com/lukas2511/dehydrated/releases
$ curl -LO https://github.com/lukas2511/dehydrated/archive/v0.4.0.tar.gz
$ tar -zxv -f v0.4.0.tar.gz
$ cd dehydrated-0.4.0/
或是透過 Git 下載最新版本:
$ cd ~; git clone https://github.com/lukas2511/dehydrated.git
$ cd dehydrated/
除了用 Git 下載外,也可以直接只抓執行檔:
$ curl -LO https://raw.githubusercontent.com/lukas2511/dehydrated/master/dehydrated
把程式安裝到 /etc/dehydrated/ 下:
# mkdir /etc/dehydrated/
# cp ~/dehydrated/dehydrated /etc/dehydrated/
# chmod a+x /etc/dehydrated/dehydrated
建立 SSL certificate 證驗證過程時所需要的目錄:
# mkdir -p /var/www/dehydrated/
設定 Apache 或是 nginx,在要認證的 virtual host 裡加上:
# for Apache
Alias /.well-known/acme-challenge/ /var/www/dehydrated/
# for nginx
location /.well-known/acme-challenge/ {
    alias /var/www/dehydrated/;
}
第一次需要先同意 Let's Encrypt 的條款:
# /etc/dehydrated/dehydrated --register --accept-terms
第一次產生 SSL certificate,黃色的部份請代換成網域名稱:
# /etc/dehydrated/dehydrated -c -d letsencrypt.tw
成功的話會有類似的輸出:
# INFO: Using main config file /etc/dehydrated/config
Processing letsencrypt.tw
 + Signing domains...
 + Generating private key...
 + Generating signing request...
 + Requesting challenge for letsencrypt.tw...
 + Responding to challenge for letsencrypt.tw...
 + Challenge is valid!
 + Requesting certificate...
 + Checking certificate...
 + Done!
 + Creating fullchain.pem...
 + Done!
成功後產生的檔案都在 /etc/dehydrated/certs/letsencrypt.tw/ 裡:
drwx------ 2 root root 4096 Feb 24 02:25 .
drwx------ 3 root root 4096 Feb 24 02:23 ..
-rw------- 1 root root 1651 Feb 24 02:25 cert-1456280700.csr
-rw------- 1 root root 2143 Feb 24 02:25 cert-1456280700.pem
lrwxrwxrwx 1 root root   19 Feb 24 02:25 cert.csr -> cert-1456280700.csr
lrwxrwxrwx 1 root root   19 Feb 24 02:25 cert.pem -> cert-1456280700.pem
-rw------- 1 root root 1675 Feb 24 02:25 chain-1456280700.pem
lrwxrwxrwx 1 root root   20 Feb 24 02:25 chain.pem -> chain-1456280700.pem
-rw------- 1 root root 3818 Feb 24 02:25 fullchain-1456280700.pem
lrwxrwxrwx 1 root root   24 Feb 24 02:25 fullchain.pem -> fullchain-1456280700.pem
-rw------- 1 root root 3243 Feb 24 02:25 privkey-1456280700.pem
lrwxrwxrwx 1 root root   22 Feb 24 02:25 privkey.pem -> privkey-1456280700.pem
接著就可以修改 Apache 或是 nginx 的 SSL 設定:
# for Apache
SSLCertificateFile /etc/dehydrated/certs/letsencrypt.tw/cert.pem
SSLCertificateChainFile /etc/dehydrated/certs/letsencrypt.tw/chain.pem
SSLCertificateKeyFile /etc/dehydrated/certs/letsencrypt.tw/privkey.pem
# for nginx
ssl_certificate /etc/dehydrated/certs/letsencrypt.tw/fullchain.pem;
ssl_certificate_key /etc/dehydrated/certs/letsencrypt.tw/privkey.pem;
然後重新載入 Apache 或是 nginx 的設定檔 (或是直接重新啟動):
# echo "for Apache"
# service apache2 reload
# echo "for nginx"
# service nginx reload
接下來設定 /etc/cron.d/dehydrated-letsencrypt_tw (因為 /etc/cron.d/ 裡面的檔名不能有 . 這個符號,用 _ 取代),讓 cron 每天自動檢查並更新:
# for Apache
0 0 * * * root sleep $(expr $(printf "\%d" "0x$(hostname | md5sum | cut -c 1-8)") \% 86400); ( /etc/dehydrated/dehydrated -c -d letsencrypt.tw; /usr/sbin/service apache2 reload ) > /tmp/dehydrated-letsencrypt.tw.log 2>&1
# for nginx
0 0 * * * root sleep $(expr $(printf "\%d" "0x$(hostname | md5sum | cut -c 1-8)") \% 86400); ( /etc/dehydrated/dehydrated -c -d letsencrypt.tw; /usr/sbin/service nginx reload ) > /tmp/dehydrated-letsencrypt.tw.log 2>&1

規劃

這段在說明上面為什麼這樣規劃,對於自動化 (像是 Puppet、Chef) 會很有幫助:
  • 選用 dehydrated 而非官方的 certbot 是因為 dehydrated 的需求相當低,只需要有 curl 與 openssl 就可以執行,相較於官方版本需要 Python 會比較簡單。
  • 放到 /etc/dehydrated/ 下的目的是避免之後各作業系統有提供 dehydrated 的套件而衝突到 (套件通常都會把可執行檔放到 /usr/bin 或是 /usr/sbin 下),另外一方面 dehydrated 會吃同一個目錄下的 config,這對於設定上可以少一些功夫。
  • 在 cron job 裡面每天執行是因為 dehydrated 會自己檢查憑證有效期限,如果還有一個月以上的時間有效就不會 renew,所以不需要擔心每天執行會造成 Let's Encrypt 的伺服器產生負擔。
  • 在 cron job 中的 sleep $(expr $(printf "\%d" "0x$(hostname | md5sum | cut -c 1-8)") \% 86400) 設計是利用機器名稱產生出十六進位 hash 值,抓一部分轉成十進位後除以一天的秒數,得到餘數後先停這個秒數再跑 dehydrated,這樣可以避免同時間有太多機器到 Let's Encrypt 的伺服器,造成類似 DDoS 的攻擊。

參考資料

Copyright

CC0 
To the extent possible under law, Gea-Suan Lin has waived all copyright and related or neighboring rights tohttps://creativecommons.org/choose/zero/waiver. This work is published from: 台灣.
 



JK部分引自





最需要注意的是JkWorkersFile與JkMount,JkWorkersFile是workers.properties的路徑,而JkMount是負責設定URL的對應。以我的例子而言,在Tomcat Server上我有LearningEnglish與LogoutArmyCD兩個應用程式,因此我針對LearningEnglish與LogoutArmyCD兩個名稱做filter,當然你也可以針對附檔名去處理。這兩個AP分別對應到le與lacd,這標記是記錄在workers.properties中。


JkWorkersFile conf/workers.properties
JkLogFile  /var/log/jk.log
JkShmFile  /var/log/jk-runtime-status
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
 
JkMount /LearningEnglish le
JkMount /LearningEnglish/* le
 
JkMount /LogoutArmyCD lacd
JkMount /LogoutArmyCD/* lacd

ssl.conf的部分只需要加入Filter對應即可。當User透過https訪問到LearningEnglish或LogoutArmyCD時,會自動導向至le與lacd所設定的Tomcat Server上。
<VirtualHost *:443>
...
JkMount /LearningEnglish le
JkMount /LearningEnglish/* le
 
JkMount /LogoutArmyCD lacd
JkMount /LogoutArmyCD/* lacd
</VirtualHost>


轉成 tomcat 用的 keystone

You do need to add the privkey.pem to the JKS, but I couldn't do so directly with keytool or Portecle and used a workaround instead.
With the help of openSSL you can create a PKCS12 keystore with both your certificate and the private key like so (no password for privkey.pem required):
openssl pkcs12 -export -in cert.pem -inkey privkey.pem -out cert_and_key.p12 -name tomcat -CAfile chain.pem -caname root
I than converted this PKCS12 to a JKS:
keytool -importkeystore -deststorepass <changeit> -destkeypass <changeit> -destkeystore MyDSKeyStore.jks -srckeystore cert_and_key.p12 -srcstoretype PKCS12 -srcstorepass <thePasswordUsedInTheCommandAbove> -alias tomcat
After that I added the chain.pem (although this might have been avoided with the use of -chain in the openSSL command, I guess):
keytool -import -trustcacerts -alias root -file chain.pem -keystore MyDSKeyStore.jks
The resulting JKS can be used in a Tomcat Connector configuration like so:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="/path/to/MyDSKeyStore.jks" keystorePass="<changeit>"
keyAlias="tomcat" keyPass="<changeit>"/>
So this allows you to use your LE certificate with Tomcat =)
Hope this helps.