锘??xml version="1.0" encoding="utf-8" standalone="yes"?> This tutorial will walk you through how to configure SSL (https://localhost:8443 access) on Tomcat in 5 minutes. For this tutorial you will need: The set up consists in 3 basic steps: Fisrt, open the terminal on your computer and type: Windows: Linux or Mac OS: The $JAVA_HOME on Mac is located on “/System/Library/Frameworks/JavaVM.framework/Versions/{your java version}/Home/” You will change the current directory to the directory Java is installed on your computer. Inside the Java Home directory, cd to the bin folder. Inside the bin folder there is a file named keytool. This guy is responsible for generating the keystore file for us. Next, type on the terminal: When you type the command above, it will ask you some questions. First, it will ask you to create a password (My password is “password“): It will create a .keystore file on your user home directory. On Windows, it will be on: C:\Documents and Settings\[username]; on Mac it will be on /Users/[username] and on Linux will be on /home/[username]. Open your Tomcat installation directory and open the conf folder. Inside this folder, you will find the server.xml file. Open it. Find the following declaration: Uncomment it and modify it to look like the following: Note we add the keystoreFile, keystorePass and changed the protocol declarations. Start tomcat service and try to access https://localhost:8443. You will see Tomcat’s local home page. Note if you try to access the default 8080 port it will be working too: http://localhost:8080 To force your web application to work with SSL, you simply need to add the following code to your web.xml file (before web-app tag ends): The url pattern is set to /* so any page/resource from your application is secure (it can be only accessed with https). The transport-guarantee tag is set to CONFIDENTIAL to make sure your app will work on SSL. If you want to turn off the SSL, you don’t need to delete the code above from web.xml, simply changeCONFIDENTIAL to NONE. Reference: http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html (this tutorial is a little confusing, that is why I decided to write another one my own). Happy Coding!1 – Creating a Keystore file using Java
cd %JAVA_HOME%/bin
cd $JAVA_HOME/bin
keytool -genkey -alias tomcat -keyalg RSA
loiane:bin loiane$ keytool -genkey -alias tomcat -keyalg RSA Enter keystore password: password Re-enter new password: password What is your first and last name? [Unknown]: Loiane Groner What is the name of your organizational unit? [Unknown]: home What is the name of your organization? [Unknown]: home What is the name of your City or Locality? [Unknown]: Sao Paulo What is the name of your State or Province? [Unknown]: SP What is the two-letter country code for this unit? [Unknown]: BR Is CN=Loiane Groner, OU=home, O=home, L=Sao Paulo, ST=SP, C=BR correct? [no]: yes Enter key password for (RETURN if same as keystore password): password Re-enter new password: password
2 – Configuring Tomcat for using the keystore file – SSL config
<!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> -->
Connector SSLEnabled="true" acceptCount="100" clientAuth="false" disableUploadTimeout="true" enableLookups="false" maxThreads="25" port="8443" keystoreFile="/Users/loiane/.keystore" keystorePass="password" protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https" secure="true" sslProtocol="TLS" />
3 – Let’s test it!
4 – BONUS - Configuring your app to work with SSL (access through https://localhost:8443/yourApp)
<security-constraint> <web-resource-collection> <web-resource-name>securedapp</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint>
]]>
涓嬮潰鐨凜LIENT绔彲浠ヤ笌鍓嶆枃鎻愬埌鐨凧AVA鏈嶅姟绔氫俊錛?br />#use聽strict;
use聽IO::Socket::SSL(debug4);
my聽($v_mode,聽$sock,聽$buf);
if($ARGV[0]聽eq聽"DEBUG")聽{聽$IO::Socket::SSL::DEBUG聽=聽1;聽}
#聽Check聽to聽make聽sure聽that聽we聽were聽not聽accidentally聽run聽in聽the聽wrong
#聽directory:
unless聽(-d聽"certs")聽{
聽聽聽聽if聽(-d聽"../certs")聽{
聽聽聽聽chdir聽"..";
聽聽聽聽}聽else聽{
#聽聽聽聽die聽"Please聽run聽this聽example聽from聽the聽IO::Socket::SSL聽distribution聽directory!\n";
聽聽聽聽}
}
if(!($sock聽=聽IO::Socket::SSL->new(聽PeerAddr聽=>聽'172.19.149.52',
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽PeerPort聽=>聽'5555',
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Proto聽聽聽聽=>聽'tcp',
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽SSL_verify_mode聽=>聽0x01,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽SSL_ca_file聽=>聽'mycerts/cacert.pem',
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽)))聽{
聽聽聽聽warn聽"unable聽to聽create聽socket:聽",聽&IO::Socket::SSL::errstr,聽"\n";
聽聽聽聽exit(0);
}聽else聽{
聽聽聽聽warn聽"connect聽($sock).\n"聽if聽($IO::Socket::SSL::DEBUG);
}
#聽check聽server聽cert.
my聽($subject_name,聽$issuer_name,聽$cipher);
if(聽ref($sock)聽eq聽"IO::Socket::SSL")聽{
聽聽聽聽$subject_name聽=聽$sock->peer_certificate("subject");
聽聽聽聽$issuer_name聽=聽$sock->peer_certificate("issuer");
聽聽聽聽$cipher聽=聽$sock->get_cipher();
}
warn聽"cipher:聽$cipher.\n",聽"server聽cert:\n",聽
聽聽聽聽"\t聽'$subject_name'聽\n\t聽'$issuer_name'.\n\n";
print聽$sock聽"Knock,聽knock.\n";
my聽($buf)聽=聽$sock->getlines;
$sock->close();
print聽"read:聽'$buf'.\n";
鍙﹀錛屼篃緇欏嚭涓涓狿ERL鐨凷VR绔ず渚嬶細#use聽strict;
use聽IO::Socket::SSL(debug4);
my聽($sock,聽$s,聽$v_mode);
if($ARGV[0]聽eq聽"DEBUG")聽{聽$IO::Socket::SSL::DEBUG聽=聽1;聽}
#聽Check聽to聽make聽sure聽that聽we聽were聽not聽accidentally聽run聽in聽the聽wrong
#聽directory:
unless聽(-d聽"certs")聽{
聽聽聽聽if聽(-d聽"../certs")聽{
聽聽聽聽chdir聽"..";
聽聽聽聽}聽else聽{
#聽聽聽聽die聽"Please聽run聽this聽example聽from聽the聽IO::Socket::SSL聽distribution聽directory!\n";
聽聽聽聽}
}
if(!($sock聽=聽IO::Socket::SSL->new(聽Listen聽=>聽5,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽LocalAddr聽=>聽'10.56.28.35',
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽LocalPort聽=>聽9000,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Proto聽聽聽聽聽=>聽'tcp',
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Reuse聽聽聽聽聽=>聽1,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽SSL_use_cert聽=>聽1,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽SSL_verify_mode聽=>聽0x00,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽SSL_cert_file聽=>聽'mycerts/cert.pem',
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽SSL_key_file聽=>聽'mycerts/key.pem'聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽))聽)聽{
聽聽聽聽warn聽"unable聽to聽create聽socket:聽",聽&IO::Socket::SSL::errstr,聽"\n";
聽聽聽聽exit(0);
}
warn聽"socket聽created:聽$sock.\n";
while聽(1)聽{
聽聽warn聽"waiting聽for聽next聽connection.\n";
聽聽
聽聽while(($s聽=聽$sock->accept()))聽{
聽聽聽聽聽聽my聽($peer_cert,聽$subject_name,聽$issuer_name,聽$date,聽$str);
聽聽聽聽聽聽
聽聽聽聽聽聽if(聽!聽$s聽)聽{
聽聽聽聽聽聽warn聽"error:聽",聽$sock->errstr,聽"\n";
聽聽聽聽聽聽next;
聽聽聽聽聽聽}
聽聽聽聽聽聽
聽聽聽聽聽聽warn聽"connection聽opened聽($s).\n";
聽聽聽聽聽聽
聽聽聽聽聽聽if(聽ref($sock)聽eq聽"IO::Socket::SSL")聽{
聽聽聽聽聽聽$subject_name聽=聽$s->peer_certificate("subject");
聽聽聽聽聽聽$issuer_name聽=聽$s->peer_certificate("issuer");
聽聽聽聽聽聽}
聽聽聽聽聽聽
聽聽聽聽聽聽warn聽"\t聽subject:聽'$subject_name'.\n";
聽聽聽聽聽聽warn聽"\t聽issuer:聽'$issuer_name'.\n";
聽聽
聽聽聽聽聽聽my聽$date聽=聽localtime();
聽聽聽聽聽聽print聽$s聽"my聽date聽command聽says聽it's:聽'$date'";
聽聽聽聽聽聽close($s);
聽聽聽聽聽聽warn聽"\t聽connection聽closed.\n";
聽聽}
}
$sock->close();
warn聽"loop聽exited.\n";
鍦≒ERL涓啓SSL鐨凷OCKET錛岃娉ㄦ剰錛?br />SVR绔腑錛?br />聽聽聽聽聽聽 SSL_use_cert => 1,
聽聽聽聽聽聽 SSL_verify_mode => 0x00,
聽聽聽聽聽聽 SSL_cert_file => 'mycerts/cert.pem',
聽聽聽聽聽聽 SSL_key_file => 'mycerts/key.pem'
CLI绔槸錛?br />聽聽聽聽聽聽 SSL_verify_mode => 0x01,
聽聽聽聽聽聽 SSL_ca_file => 'mycerts/cacert.pem',
mode鏄?琛ㄧず錛屼笉璁よ瘉瀵圭錛屾槸1琛ㄧず瑕佽璇佸鏂廣?img src ="http://www.aygfsteel.com/alwayscy/aggbug/85368.html" width = "1" height = "1" />
]]>
]]>
B->A: 鐢╒2鍔犲瘑榪囩殑P1錛堝嵆鐢ㄦ埛璇佷功錛孉灝辯敤P2瑙e瘑鍑篜1錛?br />
A->B: ok
B->A: 鐢╒1鍔犲瘑鐨勪竴孌典俊鎭?br />
A->B: 鐢≒1鍔犲瘑涓涓嚜鍔ㄧ敓鎴愮殑K錛堢敤涔嬪墠鐨凱1瑙e瘑鎴愬姛榪欐淇℃伅鍒欒涓築鏄彲淇$殑浜嗭級
B->A: 鐢↘鍔犲瘑鐨勬暟鎹紙涔嬪悗涓ゅ瀵嗛挜鍔熻兘緇撴潫錛岀敱K鏉ュ姞瑙e瘑鏁版嵁錛?br />
榪欓噷錛孭2灝辨槸絎?鏂圭殑CA璇佷功錛岀敱浜庨潪瀵圭О鍔犲瘑寰堟參錛屾墍浠ュ叕縐侀挜鍙槸鐢ㄦ潵淇濊瘉K鐨勪紶閫佸畨鍏紝涔嬪悗閫氫俊鏄敤K鐨勫縐板姞瀵嗙畻娉曟潵淇濊瘉銆?br />
涓轟粈涔堥氳繃浠ヤ笂榪囩▼A灝辮兘澶熺‘瀹氳偗瀹氭槸B錛岃屼笉鏄煇涓狢鍦ㄥ亣瑁匓浜嗗憿錛熷洜涓鴻繖涓繃紼嬩腑錛孊鐢╒1鍔犲瘑榪囦竴孌典俊鎭彂緇橝錛孉涔熸垚鍔熻В寮浜嗐傛垜浠紑澶磋皥鍒板叕閽ワ紙P1錛夊彧鍙互鍞竴瑙e瘑縐侀挜錛圴1錛夊姞瀵嗚繃鐨勪俊鎭紝榪欐牱A灝卞彲浠ュ畬鍏ㄧ浉淇鏄嫢鏈塚1鐨勶紝鑰孷1鏄弗鏍間繚瀵嗭紝鍙鏈嶅姟鎻愪緵鍏徃鎷ユ湁錛屾墍浠ヤ繚璇佷簡閫氫俊鐨勬湇鍔℃柟姝g‘鎬с?br />
榪欓噷(P2,V2)灝辨槸certificate authority (CA)鐢ㄦ潵緇欏鎴風鍚嶇敤鐨勫叕縐侀挜銆?br />
(P1,V1)鏄鎴瘋嚜宸辯殑鍏閽ワ紝鎻愪氦緇機A錛孋A鎵鍋氱殑浜嬫儏灝辨槸鐢?P2,V2)鏉ョ粰瀹㈡埛鐨?P1,V1)絳懼悕錛岀畝鍗曞惂錛?br />
V2鏄疌A鍏徃瑕佷繚瀵嗙殑錛岃孭2灝辨槸鍏敤CA璇佷功銆傜敤V2鍔犲瘑榪囷紙絳懼悕榪囷級鐨凱1錛岀О涓虹敤鎴瘋瘉涔︼紝涓鑸瀹夎鍦ㄦ湇鍔″櫒绔?br />
涓嬮潰鎴戜滑OpenSSL鏉ュ仛榪欎竴鏁翠歡浜嬫儏銆?br />
鍏堢敓鎴怌A鐨勫叕縐侀挜(Root Certificate )
鍑嗗宸ヤ綔
mkdir CA
cd CA
mkdir newcerts private
echo '01' > serial
touch index.txt
鐢熸垚閰嶇疆鏂囦歡銆傜敱浜巓penssl鍛戒護琛屽弬鏁板お澶氾紝鎵浠ュ氨鐢ㄦ枃浠舵潵緇勭粐鍚勭閫夐」銆?br />
鍏朵腑,req_distinguished_name 鑺傝〃紺洪渶瑕佹彁紺虹敤鎴瘋緭鍏ョ殑淇℃伅銆?br />
v3_ca鏄湁鍏矯A鍏閽ョ敓鎴愮殑錛寁3_req鏄湁鍏崇敤鎴瘋瘉涔︾敓鎴愮殑銆?br />
ca_default鏄敤CA鍏閽ョ鍚嶇殑鏃跺欙紝鐢ㄦ埛璇佷功鐨勯粯璁や俊鎭?br />
vi ./openssl.cnf
dir = .
[ req ]
default_bits = 1024 # Size of keys
default_keyfile = key.pem # name of generated keys
default_md = md5 # message digest algorithm
string_mask = nombstr # permitted characters
distinguished_name = req_distinguished_name
req_extensions = v3_req
[ req_distinguished_name ]
# Variable name Prompt string
#---------------------- ----------------------------------
0.organizationName = Organization Name (company)
organizationalUnitName = Organizational Unit Name (department, division)
emailAddress = Email Address
emailAddress_max = 40
localityName = Locality Name (city, district)
stateOrProvinceName = State or Province Name (full name)
countryName = Country Name (2 letter code)
countryName_min = 2
countryName_max = 2
commonName = Common Name (hostname, IP, or your name)
commonName_max = 64
# Default values for the above, for consistency and less typing.
# Variable name Value
#------------------------------ ------------------------------
0.organizationName_default = EB Company
localityName_default = Shen Zhen
stateOrProvinceName_default = Guan Dong
countryName_default = CN
[ v3_ca ]
basicConstraints = CA:TRUE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer:always
[ v3_req ]
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
[ ca ]
default_ca = CA_default
[ CA_default ]
serial = $dir/serial
database = $dir/index.txt
new_certs_dir = $dir/newcerts
certificate = $dir/cacert.pem
private_key = $dir/private/cakey.pem
default_days = 365
default_md = md5
preserve = no
email_in_dn = no
nameopt = default_ca
certopt = default_ca
policy = policy_match
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
鐢熸垚CA鍏閽ワ細
openssl req -new -x509 -extensions v3_ca -keyout private/cakey.pem -out cacert.pem -days 3650 -config ./openssl.cnf
浼氭彁紺鴻緭鍏ュ瘑鐮侊紝褰撶敤瀹冪粰鐢ㄦ埛璇佷功絳懼悕鏃墮渶瑕佽緭鍏ワ紝浠ラ伩鍏嶅叾瀹冧漢鐢ㄥ畠闅忔剰浜х敓鐢ㄦ埛璇佷功銆?br />
-days琛ㄧず鏈夋晥鏈燂紝鍥犱負瀹冩槸鏍硅瘉涔︼紝鎵浠ユ椂闂翠竴瀹氳寰堥暱錛屽惁鍒欑敱瀹冪敓鎴愮殑鐢ㄦ埛璇佷功瀹規槗榪囨湡銆?br />
榪欐椂灝辯敓鎴愪簡錛?br />
P1
cacert.pem
V1
private/cakey.pem
鏌ョ湅淇℃伅鐢細
openssl x509 -in cacert.pem -noout -text
鐢熸垚P2,V2錛屽嵆Certificate Signing Request (CSR)
鎵ц錛?br />
openssl req -new -nodes -out req.pem -config ./openssl.cnf
榪欐牱灝辯敓鎴愪簡錛?br />
P2
req.pem
V2
key.pem
鐢ㄦ鍛戒護鏌ョ湅錛?br />
openssl req -in req.pem -text -verify -noout
鐢–A鐨勭閽1涓篜2絳懼悕錛屽嵆鐢熸垚鐢ㄦ埛璇佷功
鎵ц錛?br />
openssl ca -out cert.pem -config ./openssl.cnf -infiles req.pem
鐢熸垚鐢ㄦ埛璇佷功錛?br />
cert.pem
姝ゆ椂錛屼細鎷瘋礉涓浠藉埌newcerts鐩綍涓嬨傚茍浼氭洿鏂版暟鎹簱鏂囦歡錛歩ndex.txt浠ュ強serail鏂囦歡
鐢ㄥ懡浠ゆ煡鐪嬶細
openssl x509 -in cert.pem -noout -text -purpose | more
濡傛灉瑕佸幓闄ゅ彲璇諱俊鎭儴鍒嗭紝鎵ц錛?br />
mv cert.pem tmp.pem
openssl x509 -in tmp.pem -out cert.pem
瀹夎璇佷功
key.pem(V2)鍜宑ert.pem(鐢╒1鍔犲瘑榪囩殑P2錛夊畨瑁呭埌鏈嶅姟绔?br />
鏈夌殑鏈嶅姟鍣ㄩ渶瑕佹妸榪欎袱涓枃浠惰繛涓轟竴涓紝鍙互鎵ц錛?br />
cat key.pem cert.pem >key-cert.pem
cacert.pem瀹夎鍒板鎴風
Apache鐨勯厤緗細
File Comment
/home/httpd/html Apache DocumentRoot
/home/httpd/ssl SSL-related files
/home/httpd/ssl/cert.pem Site certificate
/home/httpd/ssl/key.pem Site private key
Stunnel鐨勯厤緗?br />
stunnel -p /etc/ssl/certs/key-cert.pem
緙栬緫浜?8.4.26錛屽彟鏈変袱涓緥瀛愶細
鐢∣penSSL涓嶫AVA(JSSE)閫氫俊
Perl涓嶫ava鐨凷SL閫氫俊紺轟緥
]]>
SDK WIN SVR 2003 SP1
MASM 8.0
榪涘叆鎵撳紑sdk鐨?000緙栬瘧鍛戒護琛岋紝鍐嶈繍琛岋細
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat"" x86
鍘誨埌瑙e帇鐩綍錛?br />cd /d "E:\Prj2\ForMe\RefExe\perl+ssl\openssl-0.9.8d"
鍐嶇紪璇戯細
perl Configure VC-WIN32 --prefix=dist
ms\do_ms
nmake -f ms\ntdll.mak
nmake -f ms\ntdll.mak test
nmake -f ms\ntdll.mak install
瀹屾垚鍚庯紝dist鐩綍灝辨槸瀹夎濂界殑涓滆タ錛屽彲浠ユ嫹璐濆埌鍒浣跨敤