You are on page 1of 21

 

MÃ HÓA VÀ CHỨNG CHỈ SỐ SỬ DỤNG OPENSSL

1         Chuẩn bị thực hành


* Mở trình duyệt web, gõ địa chỉ URL: ftp://192.168.1.10
* Tải về file OpenSSL-Win32.rar và giải nén vào thư mục C:\OpenSSL-Win32
* Trong các lệnh, chú ý thay XYZ bằng tên người thực hành để phân biệt kết quả
>> Vào run, gõ lệnh: cmd để mở cửa sổ lệnh
>> Chuyển về thư mục C:\OpenSSL-Win32\bin
C:\Users\XYZ>cd C:\OpenSSL-Win32\bin

2         Thực hành sử dụng OpenSSL

2.1         Xem nội dung thư mục C:\OpenSSL-Win32\bin


C:\OpenSSL-Win32\bin> dir /w
 

2.2         Xem thông tin về OpenSSL


C:\OpenSSL-Win32\bin> openssl version

>>Xem thông tin chi tiết


C:\OpenSSL-Win32\bin> openssl version -a
 

>>  Xem tất cả các tham số của lệnh openssl


C:\OpenSSL-Win32\bin> openssl -h
>> Xem hướng dẫn sử dụng lệnh trong OpenSSL
C:\OpenSSL-Win32\bin> openssl dgst -h 
2.3         Xem các thuật toán mã hóa được cài đặt trong Open SSL
C:\OpenSSL-Win32\bin> openssl ciphers –v
 
>> Xem thông tin thuật toán mã hóa AES
C:\OpenSSL-Win32\bin> openssl ciphers -v
C:\OpenSSL-Win32\bin> openssl ciphers -v -aes
 
>> List only high encryption ciphers (keys larger than 128 bits)
C:\OpenSSL-Win32\bin> openssl cipher -v 'HIGH'
 
>> List only high encryption ciphers using the AES algorithm
C:\OpenSSL-Win32\bin> openssl cipher -v 'AES+HIGH'

2.4         Mã hóa tập tin baocao.doc thành baocaoXYZ01.doc.aes sử dụng thuật toán mã


hóa AES 256-bit ở chế độ CBC
C:\OpenSSL-Win32\bin> openssl enc -e -aes-256-cbc -in baocao.doc -
out baocaoXYZ01.doc.aes
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
 

>> Xem kết quả thực hiện lệnh


C:\OpenSSL-Win32\bin>dir /w
 Directory of C:\OpenSSL-Win32\bin
[.]              [..]             baocao.doc       baocaoXYZ01.doc.aes
...
 

>> Giải mã tập tin mã hóa AES baocaoXYZ.doc.aes thành tập tin baocaoXYZ01.doc
C:\OpenSSL-Win32\bin> openssl enc -d -aes-256-cbc -in
baocaoXYZ01.doc.aes -out baocaoXYZ01.doc

2.5         Mã hóa tập tin baocao.doc thành baocaoXYZ02.doc.aes sử dụng thuật toán mã


hóa AES 256-bit theo chuẩn mã hóa base64
C:\OpenSSL-Win32\bin> openssl enc -aes-256-cbc -a -salt -in
baocao.doc -out baocaoXYZ02.doc.aes
enter aes-256-cbc encryption password: 123456
Verifying - enter aes-256-cbc encryption password: 123456

>> Xem kết quả thực hiện lệnh


C:\OpenSSL-Win32\bin> dir /w
 Directory of C:\OpenSSL-Win32\bin
[.]              [..]             baocao.doc       baocaoXYZ02.doc.aes
...

>> Giải mã tập tin baocaoXYZ.doc.e  đã mã hóa. Chú ý tập tin này đã được tạo
từ lệnh trên
C:\OpenSSL-Win32\bin>  openssl enc -aes-256-cbc -d -a -in
baocaoXYZ02.doc.aes -out baocaoXYZ02.doc
enter aes-256-cbc decryption password: 123456

>> Xem kết quả thực hiện lệnh


C:\OpenSSL-Win32\bin> dir /w
 Directory of C:\OpenSSL-Win32\bin
[.]                [..]               baocao.doc        
baocaoXYZ02.doc.aes     baocaoXYZ02.doc
...
 

2.6         Mã hóa tập tin baocao.doc thành baocaoXYZ03.doc.aes sử dụng thuật toán mã


hóa AES với mật khẩu nhập từ dòng lệnh
>> Mã hóa file theo thuật toán AES sử dụng mật khẩu nhập vào từ dòng lệnh
C:\OpenSSL-Win32\bin> openssl enc -aes-256-cbc -salt -in baocao.doc
-out baocaoXYZ03.doc.aes -pass pass:123456
 
>> Mã hóa file theo thuật toán AES sử dụng mật khẩu đã lưu trong file
password.txt
>> Tạo file C:\OpenSSL-Win32\bin\password.txt chứa mật khẩu mã hóa. Ví dụ:
123456
C:\OpenSSL-Win32\bin> openssl enc -aes-256-cbc -salt -in baocao.doc
-out baocaoXYZ04.doc.aes -pass file:password.txt

2.7         Mã hóa tập tin baocao.doc thành baocaoXYZ0.doc.des sử dụng thuật toán mã


hóa DES
C:\OpenSSL-Win32\bin> openssl des3 -salt -in baocao.doc -out
baocaoXYZ.doc.des
enter des-ede3-cbc encryption password: 123456
Verifying - enter des-ede3-cbc encryption password: 123456

>> Xem kết quả thực hiện lệnh


C:\OpenSSL-Win32\bin> dir /w
[.]                [..]               baocao.doc         
baocaoXYZ.doc.des   
...
 
C:\OpenSSL-Win32\bin> openssl des3 -d -salt -in baocaoXYZ.doc.des -out
baocaoXYZ05.doc -k 123456
 

>> Xem kết quả thực hiện lệnh


C:\OpenSSL-Win32\bin>  dir /w
[.]                [..]               baocao.doc         baocaoXYZ.doc.des
baocaoXYZ05.doc

...

2.8         Mã hóa tập tin baocao.doc thành baocaoXYZ0.doc.des sử dụng thuật toán mã


hóa Base64-encode
C:\OpenSSL-Win32\bin> openssl enc -base64 -in baocao.doc  -out
baocaoXYZ.doc.base64

2.9         Kiểm tra một số có phải là số nguyên tố không?


C:\OpenSSL-Win32\bin> openssl prime
1234567897123456789123456789123456789123456789123456789
CE3B5A24B00902B481BCA427710CFF98AF0B684045F15 is not prime

// Generate prime numbers of a specified length


C:\OpenSSL-Win32\bin> openssl prime -generate -bits 64
16148891040401035823

 
C:\OpenSSL-Win32\bin> openssl prime -generate -bits 64 -hex
E207F23B9AE52181

2.10     Tính giá trị băm (hash value) cho tập tin


// Calculate md5, sha1, sha256, sha384, sha512digests

 // Tính giá trị băm MD5


C:\OpenSSL-Win32\bin> openssl dgst -md5 baocao.doc
MD5(baocaoXYZ.doc)= 674cb8f80e2e7a7a72d7dcaed7ee45a2

 
C:\OpenSSL-Win32\bin> openssl dgst -md5 baocaoXYZ01.doc
MD5(baocaoXYZ01.doc)= 674cb8f80e2e7a7a72d7dcaed7ee45a2

 
C:\OpenSSL-Win32\bin> openssl dgst -md5 baocaoXYZ02.doc
MD5(baocaoXYZ02.doc)= 674cb8f80e2e7a7a72d7dcaed7ee45a2

 
C:\OpenSSL-Win32\bin> openssl dgst -md5 baocaoXYZ03.doc
MD5(baocaoXYZ02.doc)= 674cb8f80e2e7a7a72d7dcaed7ee45a2

 
C:\OpenSSL-Win32\bin> openssl dgst -md5 baocaoXYZ04.doc
MD5(baocaoXYZ02.doc)= 674cb8f80e2e7a7a72d7dcaed7ee45a2

  // Tính giá trị băm SHA1 digest


C:\OpenSSL-Win32\bin> openssl dgst -sha1 baocao.doc

  // Tính giá trị băm SHA256 digest


C:\OpenSSL-Win32\bin> openssl dgst -sha256 baocao.doc

>> Generate a crypt-style password hash


C:\OpenSSL-Win32\bin> openssl passwd 123456

# If you know an existing password’s “salt,” you can duplicate the hash.
C:\OpenSSL-Win32\bin> openssl passwd -salt 8E 123456
 
#Add a password and a salt value. For a password of "123456" and salt of "mm", you should
get a hashed value of mmYnPHkgeSFA6
C:\OpenSSL-Win32\bin> openssl passwd -crypt -salt mm 123456
mmYnPHkgeSFA6
 
C:\OpenSSL-Win32\bin> openssl passwd -crypt -salt yourname 123456
yoqvbKm3bGwMU
 
C:\OpenSSL-Win32\bin> openssl passwd -1 -salt yourname 123456
$1$yourname$FPamUwOOYj5SPuxBaV4MO.
 
C:\OpenSSL-Win32\bin> openssl passwd -apr1 -salt yourname 123456
$apr1$yourname$qPivXannLx6tYtj21wH2t/
 
2.11     Mã hóa sử dụng thuật toán mã hóa công khai RSA
>> Chú ý thay XYZ bằng tên của người dùng

2.11.1   Tạo khóa riêng XYZpri.key  theo thuật toán mã hóa RSA


 
C:\OpenSSL-Win32\bin> openssl genrsa -out XYZpri.key 1024
Loading 'screen' into random state - done
Generating RSA private key, 1024 bit long modulus
...........++++++
...................................++++++
e is 65537 (0x10001)
 
>> Xem kết quả thực hiện lệnh
C:\OpenSSL-Win32\bin> dir /w
 Directory of C:\OpenSSL-Win32\bin
[.]                [..]               XYZpri.key

>> Xem nội dung khóa riêng trong file XYZpri.key


C:\OpenSSL-Win32\bin> type XYZpri.key
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDMFF0hy+4YLe0zIUtPp6L/s0fRfqVkDiNiO0Dk8XT6cYy9I5GL
VPZ34oN4gSoBfgNBdQAXwrxyfIYmlvqJIh1Y4Pf03Y3T5XpXn3EkJyK/Wbea+ufH
...
/a6eSLB0NjYxmA3GDdCnAkA5nu48xJlX64x98XN2jVuG9YqQleYgUrBsDFzqw0hC
xFN5S3GK/s3jBHksr3CLA+bVpwzwIbLtPCCNQlYSX0PN
-----END RSA PRIVATE KEY-----
 

>> Tạo khóa công khai XYZpub.key từ khóa riêng XYZpri.key:


C:\OpenSSL-Win32\bin> openssl rsa -in XYZpri.key -pubout -out
XYZpub.key
writing RSA key

>> Xem nội dung khóa công khai trong file XYZpub.key
C:\OpenSSL-Win32\bin> type XYZpub.key
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDB2UG+5qGpqoCnuWni6/41YAOF
...
OhsddJw6ffSg+B/zPCUzwWo4SOr8mpu5e3nH1kznk4I3IrE2y9CbT18IDsTMbmhj
itUSfBwKp1L8tZtD4wIDAQAB
-----END PUBLIC KEY-----

>> Mã hóa và giải mã sử dụng các khóa (XYZpub.key, XYZpri.key) vừa tạo
C:\OpenSSL-Win32\bin> openssl rsautl -encrypt -pubin -inkey
XYZpub.key -in baocao.txt -out baocaoXYZ.txt.rsa

>> Xem kết quả thực hiện lệnh


C:\OpenSSL-Win32\bin> dir /w
 Directory of C:\OpenSSL-Win32\bin
[.]                [..]               baocaoXYZ.txt.rsa

 
C:\OpenSSL-Win32\bin> openssl rsautl -decrypt -inkey XYZpri.key -out
baocaoXYZ01.txt -in baocaoXYZ.txt.rsa

>> Xem kết quả thực hiện lệnh


C:\OpenSSL-Win32\bin> dir /w
 Directory of C:\OpenSSL-Win32\bin
[.]                [..]               baocaoXYZ01.txt

2.11.2   Tạo file chứng chỉ số XYZ.crt từ khóa riêng XYZpri.key


 
>> Tạo file chứng nhận XYZ.crt từ tập tin XYZpri.key
C:\OpenSSL-Win32\bin> openssl req -new -x509 -days 1826 -key
XYZpri.key -out XYZ.crt -config openssl.cnf
Loading 'screen' into random state - done
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:VN
State or Province Name (full name) [Some-State]:Hai Chau
Locality Name (eg, city) []:Da Nang
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Dai hoc Da Nang
Organizational Unit Name (eg, section) []:Dai hoc Bach Khoa
Common Name (eg, YOUR name) []:XYZ
Email Address []:XYZ@gmail.com
 

>> Xem kết quả thực hiện lệnh


C:\OpenSSL-Win32\bin> dir /w
 Directory of C:\OpenSSL-Win32\bin
[.]                [..]               XYZpri.key         XYZ.crt

 
>> Xem nội dung giấy chứng nhận XYZ.crt (CRT Certificate Signing Request)
C:\OpenSSL-Win32\bin> type XYZ.crt
-----BEGIN CERTIFICATE-----
MIIDpTCCAw6gAwIBAgIJAIqNct4PcYcZMA0GCSqGSIb3DQEBBQUAMIGUMQswCQYD
VQQGEwJWTjERMA8GA1UECBMISGFpIENoYXUxEDAOBgNVBAcTB0RhIE5hbmcxGDAW
BgNVBAoTD0RhaSBob2MgRGEgTmFuZzEaMBgGA1UECxMRRGFpIGhvYyBCYWNoIEto
...
lsHzUGHCC1ZFdGzibdzAxryUOYVd4psYRN05Zc9n3corO27TQPGAzWcsOQYn+mmk
kE3LProXGTb/voVEHq/+B86uu3x035FZHLaJZ6lF4/JjGwJJ+D2v6SMwx7Ipx7H0
UlqbCRhh52wMneRwEF18CPjZu9dNfNeIMQ==
-----END CERTIFICATE-----
 
C:\OpenSSL-Win32\bin>

2.11.3   Tạo file chứng chỉ số XYZ.pfx từ khóa riêng XYZpri.key


>> Từ khóa riêng XYZpri.key và chứng chỉ XYZ.crt, ta tạo ra một tập tin
XYZ.pfx :
File .pfx là tập tin định dạng chứng thư số SSL chuẩn PKCS12. File này chứa
nhiều thông tin: chứng chỉ, chuỗi chứng chỉ, chứng chỉ gốc và khóa riêng và được bảo
vệ bằng mật khẩu để giữ khóa riêng, tính toàn vẹn của chứng chỉ gốc.
C:\OpenSSL-Win32\bin> openssl pkcs12 -export -out XYZ.pfx -inkey
XYZpri.key -in XYZ.crt
Loading 'screen' into random state - done
Enter Export Password:
Verifying - Enter Export Password:

 >> Xem kết quả thực hiện lệnh


C:\OpenSSL-Win32\bin> dir /w
 Directory of C:\OpenSSL-Win32\bin
[.]                [..]               XYZ.crt            XYZ.pfx    
XYZpri.key
...

 >> Từ cửa sổ Windows, chạy tập tin XYZ.pfx để cài đặt chữ kí điện tử vừa tạo. Xuất
hiện cửa sổ Certificate Import Wizard, chọn nút Next...
2.11.4   Tạo file chứng chỉ số XYZ.pem từ XYZ.pfx
>> Tạo tập tin XYZ.pem từ tập tin XYZ.pfx
File .pem là tập tin định dạng chứng nhận Base64-encoded, như chứng nhận PEM-
encoded X509; sử dụng để chứng thực bảo mật cho một trang web, thông thường
được nhập từ một Apache Web server dựa trên nền Unix, Linux và tương thích với
các ứng dụng OpenSSL.
C:\OpenSSL-Win32\bin> openssl pkcs12 -in XYZ.pfx -out XYZ.pem -nodes
Enter Import Password:
MAC verified OK

 >> Xem kết quả thực hiện lệnh


C:\OpenSSL-Win32\bin> dir /w
[.]                [..]               XYZ.pem            XYZ.pfx            
XYZpri.key
 

  >> Ký vào tập tin baocaoXYZ.doc sử dụng file XYZ.pem


C:\OpenSSL-Win32\bin> openssl dgst -sha1 -sign XYZ.pem -out
baocaoXYZ.doc.sigrsa baocao.doc
C:\OpenSSL-Win32\bin> dir /w
[.]                [..]               baocaoXYZ.doc.sigrsa   
baocao.doc         XYZ.pem 

2.11.5   Tạo file chứng chỉ số XYZ.cer từ XYZ.pem


>> Tạo file chứng nhận XYZ.cer từ tập tin XYZ.pem
C:\OpenSSL-Win32\bin>openssl x509 -pubkey -outform der -in XYZ.pem -
out XYZ.cer
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDohRzc8dk28ZkDJNxDshXvZQAJ
eODHC8tJVfsKlnBnrJ8Z3j8KPjclNBz7BM8qKGE6ptBZk/Y0qQcWwHHEluabY107
BCBL9THZ7X5AVUbrah7iWra2OyeNpAxWeqoTFHofV64y8CRhb5LV/Cc0gLYJNdyP
8j0ars49rjINiEMA1QIDAQAB
-----END PUBLIC KEY-----

2.12     Tạo khóa riêng XYZpri.pem có sử dụng password để bảo vệ - Mã hóa sử dụng


AES 256 bits
>> Generate a public and private RSA key pair:
C:\OpenSSL-Win32\bin> openssl genrsa -out XYZpri.pem -aes256 1024
Loading 'screen' into random state - done
Generating RSA private key, 1024 bit long modulus
.........................++++++
..........................++++++
e is 65537 (0x10001)
Enter pass phrase for XYZpri.pem:
Verifying - Enter pass phrase for XYZpri.pem:

>> Xem kết quả thực hiện lệnh


C:\OpenSSL-Win32\bin> dir /w
 Directory of C:\OpenSSL-Win32\bin
 
[.]              [..]             XYZpri.pem     

 
C:\OpenSSL-Win32\bin> type XYZpri.pem
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,80C970AE0B2838796CCA2532B88796F5
 
kLBRwN90W6VvrTz18yvqdU7497Esp/Cv1yQk81PEBmCkP3Q24VGkFg/coprFt21T
p79CTgZiMtS1q58E0FqdVxnTyjz5UaCOM1nYBvhD5x7UoU3j0HfHKf6/7+fRxJGk
10YMjZEQ6LhNfojbrwBlo7Mk3faRabWPUiGp8qKldMZTT51b2TcxDc5hnwBaGHD+
...
-----END RSA PRIVATE KEY----- 

>> Tạo khóa công khai từ tập tin XYZpri.pem


C:\OpenSSL-Win32\bin> openssl rsa -in XYZpri.pem -outform PEM -
pubout -out XYZpub.pem
Enter pass phrase for XYZpri.pem:
writing RSA key

>> Xem kết quả thực hiện lệnh


C:\OpenSSL-Win32\bin> dir /w
 Directory of C:\OpenSSL-Win32\bin
 
[.]              [..]             XYZpri.pem       XYZpub.pem
... 

>> Xem nội dung khóa công khai trong tập tin XYZpub.pem
C:\OpenSSL-Win32\bin> type XYZpub.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCoXV2n8jZLOs68a9VjXMU1tIYB
10YMjZEQ6LhNfojbrwBlo7Mk3faRabWPUiGp8qKldMZTT51b2TcxDc5hnwBaGHD+
...
-----END PUBLIC KEY----- 

2.13      Tạo mới một email baocaoXYZ.msg sử dụng chữ ký điện tử XYZ.pem từ nội


dung tập tin baocao.txt
C:\OpenSSL-Win32\bin> openssl smime -sign -signer XYZ.pem -in
baocao.txt -out baocaoXYZ.msg -from youremail@gmail.com -
to yourfriend'semail@gmail.com -subject "My signed message"
Loading 'screen' into random state - done

 
C:\OpenSSL-Win32\bin> dir /w
[.]                [..]       baocao.txt   baocaoXYZ.msg         XYZ.pem

 
C:\OpenSSL-Win32\bin> type baocaoXYZ.msg
To: yyy@gmail.com
From: XYZ@gmail.com
Subject: My signed message
MIME-Version: 1.0
Content-Type: multipart/signed; protocol="application/x-pkcs7-signature";
micalg=sha1; boundary="----506DCA19FAE915BEA60D41A3A5AA37C0"
 
This is an S/MIME signed message
 
------506DCA19FAE915BEA60D41A3A5AA37C0
NOI DUNG TUYET MAT
menh mong muon mau mot mau mua
moi mat mien man mai mit mu
------506DCA19FAE915BEA60D41A3A5AA37C0
Content-Type: application/x-pkcs7-signature; name="smime.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="smime.p7s"
 
MIIF3gYJKoZIhvcNAQcCoIIFzzCCBcsCAQExCzAJBgUrDgMCGgUAMAsGCSqGSIb3
DQEHAaCCA6kwggOlMIIDDqADAgECAgkAio1y3g9xhxkwDQYJKoZIhvcNAQEFBQAw
gZQxCzAJBgNVBAYTAlZOMREwDwYDVQQIEwhIYWkgQ2hhdTEQMA4GA1UEBxMHRGEg
...
 
------506DCA19FAE915BEA60D41A3A5AA37C0--

Kết quả nhận được tập tin baocaoXYZ.msg đã được mã hóa và có chữ kí điện tử.
Dùng WordPad để xem nội dung tập tin.

Hình 1.2.9. Kết quả ký vào tập tin thư điện tử  baocaoXYZ.msg
2.14     Sử dụng OpenSSL như một chương trình kết nối đến một Web server và hiển
thị chuỗi certificate chain.
C:\OpenSSL-Win32\bin> openssl s_client -connect www.facebook.com:443
-showcerts

>> Tạo khóa cho mã hóa Elliptic (?)


C:\OpenSSL-Win32\bin> openssl ecparam -out key.pem -name prime256v1
-genkey
>> OpenSSL can provide full list of EC parameter names suitable for passing to
the -name option above:
C:\OpenSSL-Win32\bin> openssl ecparam -list_curves

2.15     Benchmarking
The OpenSSL developers have built a benchmarking suite directly into the openssl
binary. It’s accessible via the speed option. It tests how many operations it can
perform in a given time, rather than how long it takes to perform a given number of
operations. There are two sets of results. The first reports how many bytes per second
can be processed for each algorithm, the second the times needed for sign/verify
cycles.
C:\OpenSSL-Win32\bin> openssl speed
 
You can run any of the algorithm-specific subtests directly.
# test rsa speeds
openssl speed rsa
 

3         THAM KHẢO THÊM

3.1         Create X.509 certificates


Create self-signed certificate and new private key from scratch:
openssl req -nodes -newkey rsa:2048 -keyout example.key -out
example.crt -x509 -days 365
Create a self signed certificate using existing CSR and private key:
openssl x509 -req -in example.csr -signkey example.key -out
example.crt -days 365
Sign child certificate using your own “CA” certificate and it’s private key. If you
were a CA company, this shows a very naive example of how you could issue new
certificates.
openssl x509 -req -in child.csr -days 365 -CA ca.crt -CAkey ca.key -
set_serial 01 -out child.crt
Print textual representation of the certificate
openssl x509 -in example.crt -text -noout
Print certificate’s fingerprint as md5, sha1, sha256 digest:
openssl x509 -in cert.pem -fingerprint -sha256 -noout

3.2         Verify CSRs or certificates


Verify a CSR signature:
openssl req -in example.csr -verify
Verify that private key matches a certificate and CSR:
openssl rsa -noout -modulus -in example.key | openssl sha256
openssl x509 -noout -modulus -in example.crt | openssl sha256
openssl req -noout -modulus -in example.csr | openssl sha256
Verify certificate, provided that you have root and any intemediate certificates
configured as trusted on your machine:
openssl verify example.crt
Verify certificate, when you have intermediate certificate chain. Root certificate is
not a part of bundle, and should be configured as a trusted on your machine.
openssl verify -untrusted intermediate-ca-chain.pem example.crt
Verify certificate, when you have intermediate certificate chain and root
certificate, that is not configured as a trusted one.
openssl verify -CAFile root.crt -untrusted intermediate-ca-chain.pem
child.crt
Verify that certificate served by a remote server covers given host name. Useful to
check your mutlidomain certificate properly covers all the host names.
openssl s_client -verify_hostname www.example.com -connect
example.com:443

3.3         Calculate message digests and base64 encoding


Calculate md5, sha1, sha256, sha384, sha512digests:
openssl dgst -[hash_function] <input.file
cat input.file | openssl [hash_function]
Base64 encoding and decoding:
cat /dev/urandom | head -c 50 | openssl base64 | openssl base64 -d

3.4         TLS client to connect to a remote server


Connect to a server supporting TLS:
openssl s_client -connect example.com:443
openssl s_client -host example.com -port 443
Connect to a server and show full certificate chain:
openssl s_client -showcerts -host example.com -port 443 </dev/null
Extract the certificate:
openssl s_client -connect example.com:443 2>&1 < /dev/null | sed -n
'/-----BEGIN/,/-----END/p' > certificate.pem
Override SNI (Server Name Indication) extension with another server name.
Useful for testing when multiple secure sites are hosted on same IP address:
openssl s_client -servername www.example.com -host example.com -port 443
Test TLS connection by forcibly using specific cipher suite, e.g. ECDHE-RSA-
AES128-GCM-SHA256. Useful to check if a server can properly talk via different
configured cipher suites, not one it prefers.
openssl s_client -host example.com -port 443 -cipher ECDHE-RSA-
AES128-GCM-SHA256 2>&1 </dev/null

3.5         Measure TLS connection and handshake time


Measure SSL connection time without/with session reuse:
openssl s_time -connect example.com:443 -new
openssl s_time -connect example.com:443 -reuse
Roughly examine TCP and SSL handshake times using curl:
curl -kso /dev/null -w "tcp:%{time_connect}, ssldone:%
{time_appconnect}\n" https://example.com
Measure speed of various security algorithms:
openssl speed rsa2048
openssl speed ecdsap256

3.6         Convert between encoding and container formats


Convert certificate between DER and PEM formats:
openssl x509 -in example.pem -outform der -out example.der
openssl x509 -in example.der -inform der -out example.pem
Combine several certificates in PKCS7 (P7B) file:
openssl crl2pkcs7 -nocrl -certfile child.crt -certfile ca.crt -out
example.p7b
Convert from PKCS7 back to PEM. If PKCS7 file has multiple certificates, the
PEM file will contain all of the items in it.
openssl pkcs7 -in example.p7b -print_certs -out example.crt
Combine a PEM certificate file and a private key to PKCS#12 (.pfx .p12). Also,
you can add a chain of certificates to PKCS12 file.
openssl pkcs12 -export -out certificate.pfx -inkey privkey.pem -in
certificate.pem -certfile ca-chain.pem
Convert a PKCS#12 file (.pfx .p12) containing a private key and certificates back
to PEM:
openssl pkcs12 -in keystore.pfx -out keystore.pem -nodes
 
------------------------------------------------------------------------------------------------------------

You might also like