OpenSSL ์์ฃผ ์ฐ๋ ๋ช ๋ น์ด(command) ๋ฐ ์ฌ์ฉ๋ฒ, tip ์ ๋ฆฌ
HTTPS ์ค์ , ๋ฐ์ดํ ์๋ณตํธ๋ฑ OpenSSL ์ ํ์ฉํ ์ผ์ด ๋ง์ผ๋ฏ๋ก ์ฌ๋ก๋ณ๋ก ์์ฃผ ์ฌ์ฉํ๋ ๋ช ๋ น์ด๋ฅผ ์ ๋ฆฌํ์ต๋๋ค.
์ํธ์ ๋ํ ๋๋ต์ ์ธ ์๊ฐ๋ slideshare ์ ๊ณต๊ฐํ "์ํธํ ์ด๊ฒ๋ง ์๋ฉด ๋๋ค" ๋ฅผ ์ฐธ๊ณ ํ์ธ์
์ค์น
RHEL/CentOS Linux ๋ ๊ธฐ๋ณธ ํจํค์ง์ ํฌํจ๋์ด ์์ผ๋ฏ๋ก ๋ณ๋ ์ค์น๋ฅผ ์ํด๋ ๋ฉ๋๋ค. Windows ๋ ๊ธฐํ Unix ์์ ์ค์น๋ OpenSSL ์ปดํ์ผ(compile) & ๋น๋(build) ์ฐธ๊ณ ํ์ธ์
์ค์น๋ openssl ์ version ์ ๋ค์ ๋ช ๋ น์ด๋ก ํ์ธํ ์ ์์ต๋๋ค.
$ openssl version OpenSSL 1.0.2o 27 Mar 2018
์ธ์ฆ์ ์ ๋ณด ๋ณด๊ธฐ
openssl ๋ก x509 ์ธ์ฆ์ ํ์ฑ( certificate parsing )ํ๊ธฐ ์ฐธ๊ณ
๊ฐ์ธํค(PrivateKey)
RSA 2048 ํค ์์ฑ ๋ฐ ๊ฐ์ธํค๋ฅผ AES256 ์ผ๋ก ์ํธํ
- ์ํธ( pass phrase)๋ asdfasdf ์ด๋ฉฐ ์ ๋ ฅ์ฐฝ์ ๋์ฐ์ง ์๊ณ ์ปค๋งจ๋์์ ๋ฐ๋ก ์ค์ ( -passout ์ต์ )
openssl genrsa -aes256 -passout pass:asdfasdf -out aes-pri.pem 2048
์์์ ์์ฑํ ๊ฐ์ธํค ๋ณตํธํํ์ฌ RSA Private Key ์ถ์ถ
openssl rsa -outform der -in aes-pri.pem -passin pass:asdfasdf -out aes-pri.key
pass phrase ์ ์ํธํ ์๊ณ ๋ฆฌ์ฆ ๋ณ๊ฒฝ
- ์๊ณ ๋ฆฌ์ฆ: Triple DES → AES256
- Pass phrase : asdfasdf -> new-password
openssl rsa -aes256 -in aes-pri.pem -passin pass:asdfasdf -passout pass:new-password -out aes-pri.key
๊ฐ์ธํค(PrivateKey) pass phrase ํด๋
OpenSSL ๋ก ๊ฐ์ธ ํค(Private Key) ๋น๋ฐ ๊ตฌ์ (Pass Phrase) ํด๋ ๋ฐ ์ํธํ ์ฐธ๊ณ ํ์ธ์.
๊ฐ์ธํค(PrivateKey) pass phrase ์ค์
OpenSSL ๋ก ๊ฐ์ธ ํค(Private Key) ๋น๋ฐ ๊ตฌ์ (Pass Phrase) ํด๋ ๋ฐ ์ํธํ ์ฐธ๊ณ ํ์ธ์.
pkcs#8 ๋ฐฉ์์ ๊ฐ์ธํค ํด๋
Private-Key Information Syntax Specification ๋ฐฉ์์ผ๋ก ์ํธํ๋ RSA PrivateKey ๋ฅผ ํด๋ ํ๋ ค๋ฉด ์๋ ๋ช ๋ น์ ์ฌ์ฉํฉ๋๋ค.
openssl pkcs8 -inform der -in pkcs8-pri.key -out rsa-pri.key
PKCS#8 ํ์ผ์ binary ํ์(DER) ๊ณผ text ํ์(PEM) ์ด ์์ ์ ์์ผ๋ฉฐ ์๋ํฐ๋ก ์ด์์ ๋ -----BEGIN ENCRYPTED PRIVATE KEY----- ๋ก ์์ํ๋ ๊ฒฝ์ฐ PEM ์ด๋ฉฐ ๊นจ์ง๋ ๋ฌธ์๊ฐ ์์ ๊ฒฝ์ฐ DER ์ ๋๋ค.
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFHzBJBgkqhkiG9w0BBQ0wPDAbBgkqhkiG9w0BBQwwDgQITJWLw/UHoM0CAggA
PEM ํ์์ผ ๊ฒฝ์ฐ -inform der ๊ตฌ๋ฌธ๋์ -inform pem ์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
pkcs#8 ๋ก ๋ณํ
๊ฐ์ธํค์ pass phrase ๋ฅผ PKCS#8 ํ์์ผ๋ก ๋ณํ
openssl pkcs8 -topk8 -v2 aes128 -in aes-pri.pem -out aes-strong.key -outform der -passout pass:asdfasdf
- -topk8 : output PKCS8 file
- -v2 aes128 : PKCS#5 Ver 2.0 ์ฌ์ฉ ๋ฐ aes128 ์ฌ์ฉ
HTTPS ์ฐ๊ฒฐ์ ์ธ์ฆ์ ๋๋ฒ๊น
HTTPS ๋๋ฒ๊น (httpd ์ SSLCertificateChainFile, SSLCACertificateFile ์ ์ ์ค์ ์ฌ๋ถ ํ์ธ๋ฑ)์ด๋ curl ๋ฑ์ ca bundle ์ ๋ฑ๋กํ๊ธฐ ์ํ ๋ชฉ์ ์ผ๋ก ์๋ฒ๊ฐ ์ฌ์ฉํ๋ SSL ์ธ์ฆ์๋ฅผ ์ถ์ถํ ๊ฒฝ์ฐ ์๋ ๋ช ๋ น์ด ์ฌ์ฉ
openssl s_client -debug -connect ssl.example.com:443
- http://steveliles.github.io/android_ssl_certificate_not_trusted.html
- Java ์์ ValidatorException ๋ฑ ์ธ์ฆ์ ๊ด๋ จ ์๋ฌ ํด๊ฒฐ - keystore์ SSL/TLS ์ธ์ฆ์๋ฅผ import ํ๊ธฐ#AlphaSSL์ผ๋
CMS (PKCS#7, S/MIME)
cms(Cryptographic Message Syntax) ๋ช ๋ น์ด๋ S/MIME v3.1 mail ์ด๋ PKCS#7 ํ์์ ๋ฐ์ดํ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ช ๋ น์ด๋ก ์ฃผ์ ์ต์ ์ ๋ค์๊ณผ ๊ฐ๋ค.
- -verify : ์ ์์๋ช ๊ฒ์ฆ ์ํ
- -in : ๊ฒ์ฆํ ์ ์์๋ช ๋ฐ์ดํ ํ์ผ
- -certfile : ๊ฒ์ฆ์ ์ฌ์ฉํ ์ธ์ฆ์ ํ์ผ(์ ์ ์๋ช ๋ฐ์ดํ๋ด์ ์ธ์ฆ์๊ฐ ์์ ๊ฒฝ์ฐ ํ์ - ์์ฑ์ -nocerts ์ผ๋ก ์์ฑํ์ ๊ฒฝ์ฐ)
- -out : ๊ฒ์ฆํ ์๋ณธ์ ์ ์ฅํ ํ์ผ๋ช
- -content : ๊ฒ์ฆ์ ์ฌ์ฉํ ์๋ณธ ํ์ผ
- -CAfile : ์ธ์ฆ์ ๋ฐ๊ธ ์ฒด์ธ(CA ์ธ์ฆ์ ๋ฌถ์. PEM ํ์์ผ๋ก ์ฐ์ ํด์ ์์ฑํด ์ฃผ๋ฉด ๋๋ฉฐ ์์ ๋ curl ์ ํฌํจ๋ ca์ธ์ฆ์ ๋ฒ๋ค ํ์ผ ์ฐธ๊ณ - /etc/pki/tls/certs/ca-bundle.crt)
signeddata ๊ฒ์ฆ
DER ๋ก ์ธ์ฝ๋ฉ๋ cms signed-data ํ์์ธ inputfile ์ ๊ฒ์ฆํ๊ณ ์๋ณธ์ content ๋ผ๋ ํ์ผ๋ก ์ ์ฅ.
openssl cms -verify -in signedData.ber -inform DER -out content
์ธ์ฆ์ ์ฒด์ธ ์ง์
signed-data ์์ ์ธ์ฆ์ ์ฒด์ธ์ด ์์ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
certificate verify error:cms_smime.c:304:Verify error:unable to get local issuer certificate
CA ์ธ์ฆ์๋ฅผ PEM ํ์์ ํ์ผ(Ex: ca-file) ์ผ๋ก ๋ง๋ ํ์ -CAfile file ์ต์ ์ ์ถ๊ฐํ๋ฉด ๊ฒ์ฆ์ ์ฌ์ฉํ CA ์ธ์ฆ์๋ฅผ ์ง์ ํด ์ค ์ ์๋ค.
openssl cms -verify -in signedData.ber -inform DER -out content -CAfile ca-file
detached signeddata ๊ฒ์ฆ
์๋ช ์ ์ฌ์ฉ๋ ์ปจํ ์ธ ๊ฐ CMS Signed Data ๋ด์ ์๊ฑฐ๋ ๋๋ ์์ด๋ ๊ฐ์ ๋ก ์ธ๋ถ ํ์ผ์ ์ฌ์ฉํ ๊ฒฝ์ฐ -content file ์ต์ ์ผ๋ก ํ์ผ์ ์ง์ ํ๋ฉด ๋๋ค.
openssl cms -verify -in signedData.ber -inform DER -out content -CAfile ca-file -content origFile
signeddata ์์ฑ
PEM ํ์์ผ๋ก ๋ ์ธ์ฆ์์ ๊ฐ์ธํค๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ ์๋ช ๋ฐ์ดํ ์์ฑ
openssl cms -sign -in contents.pdf -aes128 -nosmimecap -signer sign-cert.pem -inkey sign-key.pem -outform DER -nodetach -out signed-data.ber
- -sign : ์ ์ ์๋ช ๋ฐ์ดํ ์์ฑ
- -in : ์ ์์๋ช ํ ์๋ณธ ๋ฐ์ดํ
- -nodetach: ์ ์์๋ช ๋ฐ์ดํ์ ์๋ณธ ์ฒจ๋ถ
- -nosmimecap:
- -noattr: ์ ์์๋ช ๋ฐ์ดํ์ ์ด๋ค signed attributes ๋ ํฌํจํ์ง ์์.
envelop data ์์ฑ
CMS envelopedData data ์์ฑ(๋์นญํค๋ฅผ ์์ฑํ ์๋ณธ์ ์ํธํํ ํ์ ์๋๋ฐฉ ์ธ์ฆ์์ ๊ณต๊ฐํค๋ก ๋์นญํค๋ฅผ ์ํธํ ๋ฐ์ดํ ํ์)
openssl cms -encrypt -in contents.pdf -aes256 -recip sign-cert.pem -outform DER -out enveloped-data.ber
-encrypt: encrypt ๋ฐ์ดํ ์์ฑ
-in : ์ํธํํ ์๋ณธ ๋ฐ์ดํ
- -aes256 : AES256 ์ผ๋ก ์ํธํ(-aes128, -seed, -camellia128 ๋ฑ์ ์๊ณ ๋ฆฌ์ฆ ์ฌ์ฉ ๊ฐ๋ฅ)
-recip: ๋ฐ์ดํ๋ฅผ ์์ ํ ์๋๋ฐฉ์ ์ธ์ฆ์(์ด ์์ ์๋ ๊ณต๊ฐํค๋ก ์ํธํํ๋ฏ๋ก ์๋๋ฐฉ ์ธ์ฆ์๋ฅผ ์ ํํ ๋ฃ์ด์ฃผ์ด์ผ ํจ)
envelop data ํด๋
openssl cms -decrypt -in enveloped-data.ber -inform der -inkey kmpri.pem
- -decrypt : 1123
- -in : ํด๋ ํ enveloped data ํ์ผ
- -inform : ํ์ผ์ ํฌ๋งท. ๊ธฐ๋ณธ๊ฐ์ PEM ์ด๋ฉฐ der ์ธ์ฝ๋ฉ๋์์ ๊ฒฝ์ฐ der ์ถ๊ฐ
- -inkey: ๋ณตํธํํ ๊ฐ์ธํค
-decrypt ์ -out ์ต์ ์ด ํตํ์ง ์์ผ๋ฏ๋ก > ๋ก ์๋ณธ ํ์ผ์ ์ ์ฅํด์ผ ํจ
openssl cms -decrypt -in enveloped-data.ber -inform der -inkey kmpri.pem > contents
PKCS#12
Check a Certificate Signing Request (CSR) - PKCS#10
openssl req -text -noout -verify -in CSR.csr
pkcs12 ์์ฑ
p12 ํ์ผ ์์ฑ
openssl pkcs12 -export -in cert.pem -inkey pri-key.pem -out file.p12 -name "My Certificate"
- -export : PKCS#12 ํ์ผ ์์ฑ
- -in : p12 ์ ๋ค์ด๊ฐ ์ธ์ฆ์
- -inkey: ํฌํจ์ํฌ ๊ฐ์ธํค
- -out : ์์ฑ๋ p12 ํ์ผ๋ช
- -name: ์ฒจ๋ถ ๊ทธ๋ฆผ์ฒ๋ผ friendlyName ์ ๋ค์ด๊ฐ ์ด๋ฆ์ด๋ฉฐ Java ์์ KeyStore ๋ก ์ ๊ทผ์ alias ํญ๋ชฉ์ด ๋๋ฏ๋ก ํ์๋ก ์
๋ ฅํด์ผ ํ๋ค. openssl ์ ์
๋ ฅ๋์ง ์์์ ๊ฒฝ์ฐ ์ธ์ฆ์์ ํด์๊ฐ์ ์ค์ ํ๋ ๊ฒ ๊ฐ๋ค.
- -descert : p12 ๋ด ์ธ์ฆ์ ํญ๋ชฉ์ Triple DES ๋ก ์ํธํ(๊ธฐ๋ณธ๊ฐ RC2-40) - ์ธ์ฆ์๋ ๊ณต๊ฐํ๋ ์ฉ๋์ด๋ฏ๋ก ํฌ๊ฒ ์๋ฏธ ์๋ ์ต์
- -des3 : encrypt private keys with triple DES (default)
- -aes128 : ๊ฐ์ธํค๋ฅผ AES128 ๋ก ์ํธํ(๊ถ์ฅ)
- -keypbe alg: specify private key PBE algorithm (default 3DES)
๊ธฐํ ์ธ์ฆ์๋ฅผ ํฌํจํ์ฌ p12 ์์ฑ
openssl pkcs12 -export -in cert.pem -inkey pri-key.pem -out file.p12 -name "My Certificate" \ -certfile othercerts.pem
-certfile : ํฌํจ์ํฌ ์ถ๊ฐ ์ธ์ฆ์
Check a PKCS#12 file (.pfx or .p12)
PKCS#12 ์ ๋ณด ์ถ๋ ฅ
openssl pkcs12 -info -in keyStore.p12
PKCS#12 ๋ด ์ธ์ฆ์๋ฅผ ํ์ผ๋ก ์ ์ฅ(-clcerts -nokeys)
openssl pkcs12 -in file.p12 -clcerts -nokeys -out file.crt
PKCS#12 ๋ด ๊ฐ์ธํค๋ฅผ ํ์ผ๋ก ์ ์ฅ
openssl pkcs12 -in file.p12 -nocerts -out file.key
PKCS#12 ๋ด ๊ฐ์ธํค์ pass phrase ๋ฅผ ์ ์ฉํ์ง ์๊ณ ํ์ผ๋ก ์ ์ฅ
openssl pkcs12 -in file.p12 -out file.pem -nodes
OCSP
์ธ์ฆ์๋ PEM ํ์์ด์ด์ผ ํจ.
OCSPRequest ์์ฑ
lesstif.cer ์ธ์ฆ์๋ฅผ ๊ฒ์ฆํ๊ธฐ ์ํ OCSPRequest ๋ฅผ ์์ฑํ์ฌ ํ์ผ(ocsp-req.ber)๋ก ์ ์ฅ. -issuer ์ต์ ์๋ ์ธ์ฆ๊ธฐ๊ด ์ธ์ฆ์๋ฅผ ์ ๋ ฅ
openssl ocsp -issuer myca.cer -cert lesstif.cer -reqout ocsp-req.ber
ocsp ๋ก ์ธ์ฆ์ ๊ฒ์ฆ
์์์ ์์ฑํ OCSPRequest ๋ฅผ ์ฝ์ด์ -url ๋ก ์ง์ ๋ OCSP ์๋ฒ์์ ์ธ์ฆ์ ๊ฒ์ฆ ์์ฒญ
openssl ocsp -reqin ocsp-req.ber -text -url http://myocsp.server.com:8080/ocsp
๊ฒ์ฆํ ์ธ์ฆ์๋ฅผ ์ฝ์ด์ ๊ฒ์ฆ ์์ฒญ
openssl ocsp -issuer myca.cer -cert lesstif.cer -text -url http://myocsp.server.com:8080/ocsp
ocsp asn ํ์ฑ
-reqin ์ผ๋ก ์ง์ ๋ ํ์ผ๋ก๋ถํฐ OCSPRequest ํ์์ ๋ฐ์ดํ๋ฅผ ์ฝ์ด์ ์ถ๋ ฅ
$ openssl ocsp -reqin ocsp-req.ber -text OCSP Request Data: Version: 1 (0x0) Requestor List: Certificate ID: Hash Algorithm: sha1 Issuer Name Hash: D530654290FA7C42771A7566518BB1420AB04CE0 Issuer Key Hash: 4D5D560A0703DF83CAF3D56D8F19FC12AC90A28A Serial Number: 598E19F6 Request Extensions: OCSP Nonce: 0410D8F5A2A55605873CBEBB043FCA79022A
TSA(Time Stamp Authority)
ts ์์ฑ
openssl ts -query -data mydata.txt -no_nonce -sha1 -out design1.tsq
openssl ts -query -in design1.tsq -text
ASN1Parse
- https://www.openssl.org/docs/manmaster/apps/asn1parse.html
- https://www.openssl.org/docs/manmaster/crypto/ASN1_generate_v3.html
UTF8String ์์ฑ
UTF8String ์ ์์ฑํด์ utf8string.der ํ์ผ๋ก ์ ์ฅ
openssl asn1parse -genstr "UTF8:ํฌ๋ก World" -out utf8string.der
UTF8String file ๋ก ๋ถํฐ ํ์ฑ
์์ฑ๋ ASN1 ํ์ผ๋ก ๋ถํฐ ํ์ฑ
openssl asn1parse -inform DER -in utf8string.der
UTCTime ์์ฑ
openssl asn1parse -genstr "UTCTIME:970909034126Z" -out utctime.der
UTCTime ํ์ฑ
openssl asn1parse -inform DER -in utctime.der
OctetString ์์ฑ
ํ์ธ ํ์
"Hello World" ๋ผ๋ ๋ฌธ์์ด์ Octet string ์ผ๋ก ์์ฑํด์ octetstring.der ๋ก ์ ์ฅ
openssl asn1parse -genstr "OCTETSTRING:Hello World" -out octetstring.der
contents ๋ผ๋ ํ์ผ์ octet string ์ผ๋ก ์์ฑํ์ฌ octetstring.der ๋ก ์ ์ฅ
openssl asn1parse -genstr "OCTETSTRING" -in contents -out octetstring.der
์๊ณ ๋ฆฌ์ฆ ์๋ ์ธก์
openssl speed ๋ช ๋ น์ด๋ก ์ธก์ ๊ฐ๋ฅ
$ openssl speed -h
๋ค์์ aes-128-cbc ์ rsa 2014 ๋ฅผ ๋น๊ตํ๋ ๋ช ๋ น
$ openssl speed aes-128-cbc rsa1024 Doing aes-128 cbc for 3s on 16 size blocks: 22920078 aes-128 cbc's in 3.00s Doing aes-128 cbc for 3s on 64 size blocks: 6343026 aes-128 cbc's in 3.00s Doing aes-128 cbc for 3s on 256 size blocks: 1621301 aes-128 cbc's in 3.00s Doing aes-128 cbc for 3s on 1024 size blocks: 408313 aes-128 cbc's in 3.00s Doing aes-128 cbc for 3s on 8192 size blocks: 51219 aes-128 cbc's in 3.00s Doing 1024 bit private rsa's for 10s: 52898 1024 bit private RSA's in 10.00s Doing 1024 bit public rsa's for 10s: 907416 1024 bit public RSA's in 9.99s OpenSSL 1.0.1e-fips 11 Feb 2013
๊ฐ์ด ๋ณด๊ธฐ
- OpenSSL ๋ก ROOT CA ์์ฑ ๋ฐ SSL ์ธ์ฆ์ ๋ฐ๊ธ
- apache httpd ์ SSL/HTTPS ์ ์ฉ ๋ฐ VirtualHost ์ SNI(Server Name Indication) ๋ฌธ์ ํด๊ฒฐ
- OpenSSL ์ปดํ์ผ(compile) & ๋น๋(build)
Ref
- http://wiki.openssl.org/index.php/Command_Line_Utilities
- http://techglimpse.com/linux-openss-toolkit-example-commands/
- https://www.madboa.com/geek/openssl/
- https://www.mkssoftware.com/docs/man1/openssl_pkcs8.1.asp