開始學習openssl命令,目前處於啥也不懂的狀態。因為不是所有命令都能找到詳盡的使用方法(部分可能因為版本問題,甚至找不到對應功能)。僅為我那可憐兮兮的7條命令做個學習記錄。
一、在linux環境下下載openssl(用的wsl)
使用命令:
sudo apt-get install openssl sudo apt-get install libssl-dev
二、命令集合
1、prime
1.用途
生成素數/判斷輸入值是否為素數(當前的眾多加密技術高度依賴素數的產生和判斷)
2.支持功能
-generate 生成一個素數(無法單獨使用。需要設定輸出的位數)
-bits +int 設定生成數的位數( -hex 返回值為16進制)
-safe 和-generate搭配使用,生成安全素數
-checks +int 需要檢查的數
直接使用prime 判斷輸入值是否為素數
2、rand命令
1.用途
用來產生偽隨機字節
語法:
openssl rand [-out file] [-rand file(s)] [-base64] [-hex] num
2.支持功能
3.具體應用舉例
-base64:輸出結果為BASE64編碼數據。
-hex:輸出結果為16進制數據。
-out file:結果輸出到file中。
num:隨機數長度。
三、req
1.用途
生成證書請求、驗證證書請求文件和創建根CA
openssl req [-inform PEM|DER] [-outform PEM|DER] [-in filename] [-passin arg] [-out filename] [-passout arg] [-text] [-pubkey] [-noout] [-verify] [-modulus] [-new] [-rand file(s)] [-newkey rsa:bits][-newkey alg:file] [-nodes] [-key filename] [-keyform PEM|DER] [-keyout filename] [-keygen_engine id] [-[digest]] [-config filename] [-subj arg] [-multivalue-rdn] [-x509] [-days n] [-set_serial n][-asn1-kludge] [-no-asn1-kludge] [-newhdr] [-extensions section] [-reqexts section] [-utf8] [-nameopt] [-reqopt] [-subject] [-subj arg] [-batch] [-verbose] [-engine id]
2.實現功能
-new:說明生成證書請求文件 -x509:說明生成自簽名證書 -key:指定已有的秘鑰文件生成秘鑰請求,只與生成證書請求選項-new配合。 -newkey :-newkey是與-key互斥的,-newkey是指在生成證書請求或者自簽名證書的時候自動生成密鑰,然后生成的密鑰名稱由-keyout參數指定。當指定newkey選項時,后面指定rsa:bits說明產生rsa密鑰,位數由bits指定。 如果沒有指定選項-key和-newkey,默認自動生成秘鑰。 -out :-out 指定生成的證書請求或者自簽名證書名稱 -config:默認參數在ubuntu上為 /etc/ssl/openssl.cnf, 可以使用-config指定特殊路徑的配置文件 -nodes :如果指定-newkey自動生成秘鑰,那么-nodes選項說明生成的秘鑰不需要加密,即不需要輸入passphase. -batch :指定非交互模式,直接讀取config文件配置參數,或者使用默認參數值
3.具體應用舉例
1、根據私鑰pri_key.pem生成一個新的證書請求文件。其中"-new"表示新生成一個新的證書請求文件,"-key"指定私鑰文件,"-out"指定輸出文件,此處輸出文件即為證書請求文件
openssl genrsa -out pri_key.pem openssl req -new -key pri_key.pem -out req1.csr
2、查看證書請求文件內容
openssl req -in req1.csr或cat req1.csr或openssl req -in req1.csr -text
3、指定證書請求文件中的簽名算法
openssl req -new -key pri_key.pem -out req2.csr -md5
4、驗證請求文件的數字簽名,這樣可以驗證出證書請求文件是否被篡改過
openssl req -verify -in req2.csr
5、自簽署證書,可用於自建根CA時
openssl req -new -out req3.csr openssl req -new -out req3.csr -nodes -keyout myprivkey.pem
6、使用"-newkey"選項
openssl req -newkey rsa:2048 -out req3.csr -nodes -keyout myprivkey.pem
7、生成自簽名證書,證書名client.crt,采用自動生成秘鑰的方式,指定生成秘鑰長度為1024,加密,秘鑰文件client.key.
四、rsa
1.用途
用於處理RSA密鑰、格式轉換和打印信息
語法:openssl rsa [-inform PEM|NET|DER] [-outform PEM|NET|DER] [-in filename] [-passin arg] [-out filename] [-passout arg] [-sgckey] [-text] [-noout] [-modulus] [-check] [-pubin] [-pubout] [-engine id] [-des] [-des3] [-idea]
2.實現功能
3.具體應用舉例
1、 生成從私鑰導出公鑰(生成pub_key.pem)
2、 將PEM格式的私鑰導出到文本格式:
//查看rsa.txt
3、rsa添加和去除密鑰的保護口令
//生成不加密的RSA密鑰
//為RSA密鑰增加口令保護*//*為RSA密鑰去除口令保護
4、修改密鑰的保護口令和算法
//生成RSA密鑰
//修改加密算法為aes128,口令是123456
5、查看密鑰對中的各個參數
6、提取密鑰中的公鑰並打印模數值
//提取公鑰,用pubout參數指定輸出為公鑰
//打印公鑰中模數值
7、轉換密鑰的格式
//把pem格式轉化成der格式,使用outform指定der格式
//把der格式轉化成pem格式,使用inform指定der格式
五、rsautl
1.用途
能夠使用RSA算法簽名,驗證身份,加密/解密數據
語法:openssl rsautl [-in file] [-out file] [-inkey file] [-passin arg] [-keyform PEM|DER|NET] [-pubin] [-certin][-asn1parse] [-hexdump] [-raw] [-oaep] [-ssl] [-pkcs] [-x931] [-sign] [-verify][-encrypt] [-decrypt] [-rev] [-engine e]
2.可實現功能
3.具體應用舉例
1、使用rsautl進行加密和解密操作
//先生成一個plain.txt內容為“sunnyday”,之后我們將對其進行加密解密操作
//生成RSA密鑰
//提取公鑰,查看目錄,生成了pub.pem
//使用RSA作為密鑰進行加密,實際上使用其中的公鑰進行加密,此時查看enc.txt,輸出結果為加密后結果
//使用RSA作為密鑰進行解密,實際上使用其中的私鑰進行解密,這時查看replain.txt,輸出結果為“sunnyday”
//比較原始文件和解密后文件,無差別,無輸出
在進行相同步驟操作時,可能得出加密結果與上文顯示不同,這是因為rsa公鑰加密的時候根據填充模式填充隨機數,導致每次加密結果不同。
2、使用rsautl進行簽名和驗證操作
//先生成一個plain.txt內容為“sunnyday”,之后我們將對其進行加密解密操作
//提取PCKS8格式的私鑰
//使用RSA密鑰進行簽名,實際上使用私鑰進行加密
//使用RSA密鑰進行驗證,實際上使用公鑰進行解密
//對比原始文件和簽名解密后的文件
要注意這里的簽名和驗證過程其本質上是加解密操作,不是標准意義上的簽名和驗證。標准意義上簽名和驗證是需要增加摘要操作的。
六、c_slient
1.用途
一個SSL/TLS客戶端程序,與s_server對應,它不僅能與s_server進行通信,也能與任何使用ssl協議的其他服務程序進行通信
openssl s_client [-host host] [-port port] [-connect host:port] [-verify depth] [-cert filename] [-certform DER|PEM] [-key filename] [-keyform DER|PEM] [-pass arg] [-CApath directory] [-CAfile filename] [-reconnect][-pause] [-showcerts] [-debug] [-msg] [-state] [-nbio_test] [-nbio][-crlf] [-ign_eof] [-no_ign_eof] [-quiet] [-ssl2] [-ssl3] [-tls1_1] [-tls1_2] [-tls1] [-dtls1] [-no_ssl2][-no_ssl3] [-no_tls1] [-no_tls1_1] [-no_tls1_2] [-bugs] [-cipher cipherlist] [-starttls protocol] [-engine id] [-tlsextdebug] [-no_ticket] [-sess_out filename] [-sess_in filename] [-rand file(s)]
2.支持功能:
zhangcr@LAPTOP-2GRHFMSQ:/mnt/d/yifan/openssl/yao$ openssl s_client -help Usage: s_client [options] Valid options are: -help Display this summary -host val Use -connect instead -port +int Use -connect instead -connect val TCP/IP where to connect (default is :4433) -bind val bind local address for connection -proxy val Connect to via specified proxy to the real server -unix val Connect over the specified Unix-domain socket -4 Use IPv4 onl y -6 Use IPv6 only -verify +int Turn on peer certificate verification -cert infile Certificate file to use, PEM format assumed -certform PEM|DER Certificate format (PEM or DER) PEM default -nameopt val Various certificate name options -key val Private key file to use, if not in -cert file -keyform PEM|DER|ENGINE Key format (PEM, DER or engine) PEM default -pass val Private key file pass phrase source -CApath dir PEM format directory of CA's -CAfile infile PEM format file of CA's -no-CAfile Do not load the default certificates file -no-CApath Do not load certificates from the default certificates directory -requestCAfile infile PEM format file of CA names to send to the server -dane_tlsa_domain val DANE TLSA base domain -dane_tlsa_rrdata val DANE TLSA rrdata presentation form -dane_ee_no_namechecks Disable name checks when matching DANE-EE(3) TLSA records -reconnect Drop and re-make the connection with the same Session-ID -showcerts Show all certificates sent by the server -debug Extra output -msg Show protocol messages -msgfile outfile File to send output of -msg or -trace, instead of stdout -nbio_test More ssl protocol testing -state Print the ssl states -crlf Convert LF from terminal into CRLF -quiet No s_client output -ign_eof Ignore input eof (default when -quiet) -no_ign_eof Don't ignore input eof -starttls val Use the appropriate STARTTLS command before starting TLS -xmpphost val Alias of -name option for "-starttls xmpp[-server]" -rand val Load the file(s) into the random number generator -writerand outfile Write random data to the specified file -sess_out outfile File to write SSL session to -sess_in infile File to read SSL session from -use_srtp val Offer SRTP key management with a colon-separated profile list -keymatexport val Export keying material using label -keymatexportlen +int Export len bytes of keying material (default 20) -maxfraglen +int Enable Maximum Fragment Length Negotiation (len values: 512, 1024, 2048 and 4096) -fallback_scsv Send the fallback SCSV -name val Hostname to use for "-starttls lmtp", "-starttls smtp" or "-starttls xmpp[-server]" -CRL infile CRL file to use -crl_download Download CRL from distribution points -CRLform PEM|DER CRL format (PEM or DER) PEM is default -verify_return_error Close connection on verification error -verify_quiet Restrict verify output to errors -brief Restrict output to brief summary of connection parameters -prexit Print session information when the program exits -security_debug Enable security debug messages -security_debug_verbose Output more security debug output -cert_chain infile Certificate chain file (in PEM format) -chainCApath dir Use dir as certificate store path to build CA certificate chain -verifyCApath dir Use dir as certificate store path to verify CA certificate -build_chain Build certificate chain -chainCAfile infile CA file for certificate chain (PEM format) -verifyCAfile infile CA file for certificate verification (PEM format) -nocommands Do not use interactive command letters -servername val Set TLS extension servername (SNI) in ClientHello (default) -noservername Do not send the server name (SNI) extension in the ClientHello -tlsextdebug Hex dump of all TLS extensions received -status Request certificate status from server -serverinfo val types Send empty ClientHello extensions (comma-separated numbers) -alpn val Enable ALPN extension, considering named protocols supported (comma-separated list) -async Support asynchronous operation -ssl_config val Use specified configuration file -max_send_frag +int Maximum Size of send frames -split_send_frag +int Size used to split data for encrypt pipelines -max_pipelines +int Maximum number of encrypt/decrypt pipelines to be used -read_buf +int Default read buffer size to be used for connections -no_ssl3 Just disable SSLv3 -no_tls1 Just disable TLSv1 -no_tls1_1 Just disable TLSv1.1 -no_tls1_2 Just disable TLSv1.2 -no_tls1_3 Just disable TLSv1.3 -bugs Turn on SSL bug compatibility -no_comp Disable SSL/TLS compression (default) -comp Use SSL/TLS-level compression -no_ticket Disable use of TLS session tickets -serverpref Use server's cipher preferences -legacy_renegotiation Enable use of legacy renegotiation (dangerous) -no_renegotiation Disable all renegotiation. -legacy_server_connect Allow initial connection to servers that don't support RI -no_resumption_on_reneg Disallow session resumption on renegotiation -no_legacy_server_connect Disallow initial connection to servers that don't support RI -allow_no_dhe_kex In TLSv1.3 allow non-(ec)dhe based key exchange on resumption -prioritize_chacha Prioritize ChaCha ciphers when preferred by clients -strict Enforce strict certificate checks as per TLS standard -sigalgs val Signature algorithms to support (colon-separated list) -client_sigalgs val Signature algorithms to support for client certificate authentication (colon-separated list) -groups val Groups to advertise (colon-separated list) -curves val Groups to advertise (colon-separated list) -named_curve val Elliptic curve used for ECDHE (server-side only) -cipher val Specify TLSv1.2 and below cipher list to be used -ciphersuites val Specify TLSv1.3 ciphersuites to be used -min_protocol val Specify the minimum protocol version to be used -max_protocol val Specify the maximum protocol version to be used -record_padding val Block size to pad TLS 1.3 records to. -debug_broken_protocol Perform all sorts of protocol violations for testing purposes -no_middlebox Disable TLSv1.3 middlebox compat mode -policy val adds policy to the acceptable policy set -purpose val certificate chain purpose -verify_name val verification policy name -verify_depth int chain depth limit -auth_level int chain authentication security level -attime intmax verification epoch time -verify_hostname val expected peer hostname -verify_email val expected peer email -verify_ip val expected peer IP address -ignore_critical permit unhandled critical extensions -issuer_checks (deprecated) -crl_check check leaf certificate revocation -crl_check_all check full chain revocation -policy_check perform rfc5280 policy checks -explicit_policy set policy variable require-explicit-policy -inhibit_any set policy variable inhibit-any-policy -inhibit_map set policy variable inhibit-policy-mapping -x509_strict disable certificate compatibility work-arounds -extended_crl enable extended CRL features -use_deltas use delta CRLs -policy_print print policy processing diagnostics -check_ss_sig check root CA self-signatures -trusted_first search trust store first (default) -suiteB_128_only Suite B 128-bit-only mode -suiteB_128 Suite B 128-bit mode allowing 192-bit algorithms -suiteB_192 Suite B 192-bit-only mode -partial_chain accept chains anchored by intermediate trust-store CAs -no_alt_chains (deprecated) -no_check_time ignore certificate validity time -allow_proxy_certs allow the use of proxy certificates -xkey infile key for Extended certificates -xcert infile cert for Extended certificates -xchain infile chain for Extended certificates -xchain_build build certificate chain for the extended certificates -xcertform PEM|DER format of Extended certificate (PEM or DER) PEM default -xkeyform PEM|DER format of Extended certificate's key (PEM or DER) PEM default -tls1 Just use TLSv1 -tls1_1 Just use TLSv1.1 -tls1_2 Just use TLSv1.2 -tls1_3 Just use TLSv1.3 -dtls Use any version of DTLS -timeout Enable send/receive timeout on DTLS connections -mtu +int Set the link layer MTU -dtls1 Just use DTLSv1 -dtls1_2 Just use DTLSv1.2 -nbio Use non-blocking IO -psk_identity val PSK identity -psk val PSK in hex (without 0x) -psk_session infile File to read PSK SSL session from -srpuser val SRP authentication for 'user' -srppass val Password for 'user' -srp_lateuser SRP username into second ClientHello message -srp_moregroups Tolerate other than the known g N values. -srp_strength +int Minimal length in bits for N -nextprotoneg val Enable NPN extension, considering named protocols supported (comma-separated list) -engine val Use engine, possibly a hardware device -ssl_client_engine val Specify engine to be used for client certificate operations -ct Request and parse SCTs (also enables OCSP stapling) -noct Do not request or parse SCTs (default) -ctlogfile infile CT log list CONF file -keylogfile outfile Write TLS secrets to file -early_data infile File to send as early data -enable_pha Enable post-handshake-authentication
-host host:設置服務地址。 -port port:設置服務端口,默認為4433。 -connect host:port:設置服務器地址和端口號。如果沒有設置,則默認為本地主機以及端口號4433。 -verify depth:設置證書的驗證深度。記得CA也是分層次的吧?如果對方的證書的簽名CA不是Root CA,那么你可以再去驗證給該CA的證書簽名的CA,一直到Root CA. 目前的驗證操作即使這條CA鏈上的某一個證書驗證有問題也不會影響對更深層的CA的身份的驗證。所以整個CA鏈上的問題都可以檢查出來。當然CA的驗證出問題並不會直接造成連接馬上斷開,好的應用程序可以讓你根據驗證結果決定下一步怎么走。 -cert filename:使用的證書文件。如果server不要求要證書,這個可以省略。 -certform DER|PEM:證書的格式,一般為DER和PEM。默認為PEM格式。 -key filename:使用的證書私鑰文件。 -keyform DER|PEM:證書私鑰文件的格式,一般為DER和PEM。默認為PEM格式。 -pass arg:私鑰保護口令來源,比如:-pass file:pwd.txt,將私鑰保護口令存放在一個文件中,通過此選項來指定,不需要用戶來輸入口令。 -CApath directory:設置信任CA文件所在路徑,此路徑中的ca文件名采用特殊的形式:xxx.0,其中xxx為CA證書持有者的哈希值,它通過x509 -hash命令獲得。 -CAfile filename:某文件,里面是所有你信任的CA的證書的內容。當你要建立client的證書鏈的時候也需要用到這個文件。 -reconnect:使用同樣的session-id連接同一個server五次,用來測試server的session緩沖功能是否有問題。 -pause:每當讀寫數據時,sleep 1秒。 -showcerts:顯示整條server的證書的CA的證書鏈。否則只顯示server的證書。 -debug:打印所有的調試信息。 -msg:用16進制顯示所有的協議數據。 -state:打印SSL session的狀態, ssl也是一個協議,當然有狀態。 -nbio_test:檢查非阻塞socket的I/O運行情況。 -nbio:使用非阻塞socket。 -crlf:把在終端輸入的換行回車轉化成/r/n送出去。 -ign_eof:當輸入文件到達文件尾的時候並不斷開連接。 -no_ign_eof:當輸入文件到達文件尾的時候斷開連接。 -quiet:不打印出session和證書的信息。同時會打開-ign_eof這個選項。 -ssl2、-ssl3、-tls1_1、-tls1_2、-tls1、-dtls1、-no_ssl2、-no_ssl3、-no_tls1、-no_tls1_1、-no_tls1_2:使用的協議狀態值。 -bugs:兼容老版本服務端的中的bug。 -cipher cipherlist:由我們自己來決定選用什么加密算法,盡管是由server來決定使用什么算法列表,但它一般都會采用我們送過去的cipher列表里的第一個cipher。 -starttls protocol:protocol可以為smtp或pop3,用於郵件安全傳輸。 -engine id:硬件引擎。 -tlsextdebug:打印TLS協議中服務器端接收到的額外信息值。 -no_ticket:不支持RFC4507bis會話類型。 -sess_out filename:輸出SSL會話信息值到filename中。 -sess_in filename:從filename中獲取SSL Session值。 -rand file(s):指定隨機數種子文件,多個文件間用分隔符分開,windows用“;”,OpenVMS用“,“,其他系統用“:”。
3.具體應用舉例
1.。連接某個網站
語法:-connect host:port
以下達成的是連接百度網址,顯示連接成功(顯示了證書信息,連接狀態,會話狀態等):
2.。使用自定CA列表進行驗證
語法:-status
3.。使用指定協議
語法:-snextprotoneg protocols
七、pkeyutl
1.用途
pkeyutl命令能夠測試所支持的密鑰算法的性能
2.實現功能
3.具體應用舉例
//首先我們有已產生的私鑰,以及包含數據“1234567”的.txt文件
1、用私鑰對數據進行簽名:(產生了sig)
2、恢復簽名數據(用RSA密鑰):
3、用DSA算法來驗證簽名://RSA大整數的因式分解被認為是‘難以破解’(困難的),而DSA安全性基於離散對數問題。
參考網址:https://www.cnblogs.com/aixiaoxiaoyu/articles/8872382.html
https://www.cnblogs.com/274914765qq/p/4675649.html
https://blog.csdn.net/funtasty/article/details/41822095
https://www.dazhuanlan.com/2019/12/07/5deb102344333/?__cf_chl_jschl_tk__=95fcf51902cf6d8955f44dc6664f2f84d841657d-1603025632-0-AQTNJQ_An0XNxay_ve7Rw5UfDqvthL0aSHRWsgoSiKHzdOkJ-VrlldiUlxICgsZEkoTSr4q2mM1O2kNqQaFX9huMJVylNN1RQl-K9W_IGb5Ls0o514l04xaOschMHuCHcN7uh-ToLGdCV7NKxzzshPOCuU_We4bFRTg4cLQDMQ3KFODTtJqB39mD42k1-91beOIcQXzZiPfBnSGZNIOxWL9GhVsfLLoMUyGhGXftmTNy48FCI8X9NZ6VVkpsdz2f3Q1sLy93qDlybZuy4r6W7vJKJnAB6e9EPs0Ci9sXI2Ttj_OTIyZSZUq8W-0L99W8JA
https://cloud.tencent.com/developer/news/254061