本文主要介绍如何使用 OpenSSL 来对数字证书进行管理;文中所使用到的软件版本:OpenSSL 1.1.1s、CentOS 7.9.2009。
摘要算法用来确保数据没有被篡改,非对称加密算法可以对数据进行加解密,签名算法可以确保数据完整性和抗否认性,把这些算法集合到一起,并搞一套完善的标准,这就是数字证书。因此,数字证书就是集合了多种密码学算法,用于实现数据加解密、身份认证、签名等多种功能的一种安全标准。
数字证书可以防止中间人攻击,因为它采用链式签名认证,即通过根证书(Root CA)去签名下一级证书,这样层层签名,直到最终的用户证书。而 Root CA 证书内置于操作系统中,所以,任何经过 CA 认证的数字证书都可以对其本身进行校验,确保证书本身不是伪造的。
我们在上网时常用的 HTTPS 协议就是数字证书的应用,浏览器会自动验证证书的有效性。
要使用数字证书,首先需要创建证书。正常情况下,一个合法的数字证书需要经过 CA 签名,这需要认证域名并支付一定的费用。开发的时候,我们可以使用自签名的证书,这种证书可以正常开发调试,但不能对外作为服务使用,因为其他客户端并不认可未经 CA 签名的证书。
通过 openssl req -help 命令查看用法:
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl req -help
Usage: req [options]
Valid options are:
-help Display this summary
-inform PEM|DER Input format - DER or PEM
-outform PEM|DER Output format - DER or PEM
-in infile Input file
-out outfile Output file
-key val Private key to use
-keyform format Key file format
-pubkey Output public key
-new New request
-config infile Request template file
-keyout outfile File to send the key to
-passin val Private key password source
-passout val Output file pass phrase source
-rand val Load the file(s) into the random number generator
-writerand outfile Write random data to the specified file
-newkey val Specify as type:bits
-pkeyopt val Public key options as opt:value
-sigopt val Signature parameter in n:v form
-batch Do not ask anything during request generation
-newhdr Output "NEW" in the header lines
-modulus RSA modulus
-verify Verify signature on REQ
-nodes Don't encrypt the output key
-noout Do not output REQ
-verbose Verbose output
-utf8 Input characters are UTF8 (default ASCII)
-nameopt val Various certificate name options
-reqopt val Various request text options
-text Text form of request
-x509 Output a x509 structure instead of a cert request
(Required by some CA's)
-subj val Set or modify request subject
-subject Output the request's subject
-multivalue-rdn Enable support for multivalued RDNs
-days +int Number of days cert is valid for
-set_serial val Serial number to use
-addext val Additional cert extension key=value pair (may be given more than once)
-extensions val Cert extension section (override value in config file)
-reqexts val Request extension section (override value in config file)
-precert Add a poison extension (implies -new)
-* Any supported digest
-engine val Use engine, possibly a hardware device
-keygen_engine val Specify engine to be used for key generation operations
参数说明:
| 参数 | 说明 |
| -in infile | 指定输入文件 |
| -out outfile | 指定输出文件 |
| -key val | 指定私钥 |
| -new | 生成一个新的 CSR(Cerificate Signing Request) |
| -keyout outfile | 指定私钥输出文件 |
| -newkey val | 生成新的私钥,指定算法及密钥长度;如:rsa:2048 |
| -nodes | 生成证书时不需设置密码 |
| -x509 | 输出 x509 格式证书,而不是输出证书请求 |
| -days +int | 指定证书有效天数 |
x509 命令是一个多用途的证书工具,可以显示证书信息、转换证书格式、签名证书请求以及改变证书的信任设置等;通过 openssl x509 -help 命令查看用法:
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl x509 -help
Usage: x509 [options]
Valid options are:
-help Display this summary
-inform format Input format - default PEM (one of DER or PEM)
-in infile Input file - default stdin
-outform format Output format - default PEM (one of DER or PEM)
-out outfile Output file - default stdout
-keyform PEM|DER|ENGINE Private key format - default PEM
-passin val Private key password/pass-phrase source
-serial Print serial number value
-subject_hash Print subject hash value
-issuer_hash Print issuer hash value
-hash Synonym for -subject_hash
-subject Print subject DN
-issuer Print issuer DN
-email Print email address(es)
-startdate Set notBefore field
-enddate Set notAfter field
-purpose Print out certificate purposes
-dates Both Before and After dates
-modulus Print the RSA key modulus
-pubkey Output the public key
-fingerprint Print the certificate fingerprint
-alias Output certificate alias
-noout No output, just status
-nocert No certificate output
-ocspid Print OCSP hash values for the subject name and public key
-ocsp_uri Print OCSP Responder URL(s)
-trustout Output a trusted certificate
-clrtrust Clear all trusted purposes
-clrext Clear all certificate extensions
-addtrust val Trust certificate for a given purpose
-addreject val Reject certificate for a given purpose
-setalias val Set certificate alias
-days int How long till expiry of a signed certificate - def 30 days
-checkend intmax Check whether the cert expires in the next arg seconds
Exit 1 if so, 0 if not
-signkey val Self sign cert with arg
-x509toreq Output a certification request object
-req Input is a certificate request, sign and output
-CA infile Set the CA certificate, must be PEM format
-CAkey val The CA key, must be PEM format; if not in CAfile
-CAcreateserial Create serial number file if it does not exist
-CAserial val Serial file
-set_serial val Serial number to use
-text Print the certificate in text form
-ext val Print various X509V3 extensions
-C Print out C code forms
-extfile infile File with X509V3 extensions to add
-rand val Load the file(s) into the random number generator
-writerand outfile Write random data to the specified file
-extensions val Section from config file to use
-nameopt val Various certificate name options
-certopt val Various certificate text options
-checkhost val Check certificate matches host
-checkemail val Check certificate matches email
-checkip val Check certificate matches ipaddr
-CAform PEM|DER CA format - default PEM
-CAkeyform PEM|DER|ENGINE CA key format - default PEM
-sigopt val Signature parameter in n:v form
-force_pubkey infile Force the Key to put inside certificate
-next_serial Increment current certificate serial number
-clrreject Clears all the prohibited or rejected uses of the certificate
-badsig Corrupt last byte of certificate signature (for test)
-* Any supported digest
-subject_hash_old Print old-style (MD5) subject hash value
-issuer_hash_old Print old-style (MD5) issuer hash value
-engine val Use engine, possibly a hardware device
-preserve_dates preserve existing dates when signing
参数说明:
| 参数 | 说明 |
| -inform format | 输入格式,DER 或 PEM |
| -in infile | 输入文件 |
| -outform format | 输出格式,DER 或 PEM |
| -out outfile | 输出文件 |
| -keyform PEM|DER|ENGINE | 私钥格式 |
| -noout | 不输出证书内容 |
| -signkey val | 指定签名私钥 |
| -text | 以文本格式打印证书信息 |
ca 命令是一个小型 CA 系统,它能签发证书请求和生成 CRL;它维护一个已签发证书状态的文本数据库。通过 openssl ca -help 命令查看用法:
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl ca -help
Usage: ca [options]
Valid options are:
-help Display this summary
-verbose Verbose output during processing
-config val A config file
-name val The particular CA definition to use
-subj val Use arg instead of request's subject
-utf8 Input characters are UTF8 (default ASCII)
-create_serial If reading serial fails, create a new random serial
-rand_serial Always create a random serial; do not store it
-multivalue-rdn Enable support for multivalued RDNs
-startdate val Cert notBefore, YYMMDDHHMMSSZ
-enddate val YYMMDDHHMMSSZ cert notAfter (overrides -days)
-days +int Number of days to certify the cert for
-md val md to use; one of md2, md5, sha or sha1
-policy val The CA 'policy' to support
-keyfile val Private key
-keyform format Private key file format (PEM or ENGINE)
-passin val Input file pass phrase source
-key val Key to decode the private key if it is encrypted
-cert infile The CA cert
-selfsign Sign a cert with the key associated with it
-in infile The input PEM encoded cert request(s)
-out outfile Where to put the output file(s)
-outdir dir Where to put output cert
-sigopt val Signature parameter in n:v form
-notext Do not print the generated certificate
-batch Don't ask questions
-preserveDN Don't re-order the DN
-noemailDN Don't add the EMAIL field to the DN
-gencrl Generate a new CRL
-msie_hack msie modifications to handle all those universal strings
-crldays +int Days until the next CRL is due
-crlhours +int Hours until the next CRL is due
-crlsec +int Seconds until the next CRL is due
-infiles The last argument, requests to process
-ss_cert infile File contains a self signed cert to sign
-spkac infile File contains DN and signed public key and challenge
-revoke infile Revoke a cert (given in file)
-valid val Add a Valid(not-revoked) DB entry about a cert (given in file)
-extensions val Extension section (override value in config file)
-extfile infile Configuration file with X509v3 extensions to add
-status val Shows cert status given the serial number
-updatedb Updates db for expired cert
-crlexts val CRL extension section (override value in config file)
-crl_reason val revocation reason
-crl_hold val the hold instruction, an OID. Sets revocation reason to certificateHold
-crl_compromise val sets compromise time to val and the revocation reason to keyCompromise
-crl_CA_compromise val sets compromise time to val and the revocation reason to CACompromise
-rand val Load the file(s) into the random number generator
-writerand outfile Write random data to the specified file
-engine val Use engine, possibly a hardware device
参数说明:
| 参数 | 说明 |
| -config val | 指定 OpenSSl 的配置文件 |
| -days +int | 指定证书的有效天数 |
| -keyfile val | 指定私钥 |
| -keyform format | 指定私钥格式,PEM 或 ENGINE |
| -in infile | 指定 PEM 格式的证书请求文件 |
| -out outfile | 指定输出文件 |
| -cert infile | 指定 CA 证书 |
pkcs12 命令能生成和分析 pkcs12 格式证书,通过 openssl pkcs12 -help 命令查看用法:
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl pkcs12 -help
Usage: pkcs12 [options]
Valid options are:
-help Display this summary
-nokeys Don't output private keys
-keyex Set MS key exchange type
-keysig Set MS key signature type
-nocerts Don't output certificates
-clcerts Only output client certificates
-cacerts Only output CA certificates
-noout Don't output anything, just verify
-info Print info about PKCS#12 structure
-chain Add certificate chain
-twopass Separate MAC, encryption passwords
-nomacver Don't verify MAC
-descert Encrypt output with 3DES (default RC2-40)
-certpbe val Certificate PBE algorithm (default RC2-40)
-export Output PKCS12 file
-noiter Don't use encryption iteration
-maciter Use MAC iteration
-nomaciter Don't use MAC iteration
-nomac Don't generate MAC
-LMK Add local machine keyset attribute to private key
-nodes Don't encrypt private keys
-macalg val Digest algorithm used in MAC (default SHA1)
-keypbe val Private key PBE algorithm (default 3DES)
-rand val Load the file(s) into the random number generator
-writerand outfile Write random data to the specified file
-inkey val Private key if not infile
-certfile infile Load certs from file
-name val Use name as friendly name
-CSP val Microsoft CSP name
-caname val Use name as CA friendly name (can be repeated)
-in infile Input filename
-out outfile Output filename
-passin val Input file pass phrase source
-passout val Output file pass phrase source
-password val Set import/export password 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
-* Any supported cipher
-engine val Use engine, possibly a hardware device
参数说明:
| 参数 | 说明 |
| -clcerts | 仅输出客户端证书,不输出 CA 证书 |
| -export | 导出 pkcs12 证书 |
| -inkey val | 指定输入私钥 |
| -in infile | 指定输出证书 |
| -out outfile | 指定输出的 pcks12 证书文件 |
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl genrsa -out nginx.key #生成私钥
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl req -new -key ca.key -out nginx.csr #生成证书请求
需要先生成私钥,再根据私钥生成证书请求;依次输入 Country Name、State or Province Name、Locality Name 、Organization Name、Organizational Unit Name、Common Name 信息,生成证书请求文件:nginx.csr。
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl x509 -req -days 3650 -in nginx.csr -signkey nginx.key -out nginx.pem
/home/mongo/soft/openssl-1.1.1s/bin/openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout nginx.key -out nginx.pem
等同于 3.1 和 3.2 中命令。
/home/mongo/soft/openssl-1.1.1s/bin/openssl x509 -in nginx.pem -noout -text
/home/mongo/soft/openssl-1.1.1s/bin/openssl x509 -in nginx.pem -out nginx.cer -outform der
把 PEM 格式证书转成 DER 格式证书。
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl genrsa -out ca.key #生成私钥
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl req -new -key ca.key -out ca.csr #生成证书请求
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl x509 -req -days 3650 -in ca.csr -signkey ca.key -out ca.pem #生成根证书
生成证书请求时输入如下信息:
Country Name = CN
State or Province Name = JS
Locality Name = NJ
Organization Name = ABC
Organizational Unit Name = ABC_123
Common Name = ROOT
生成服务端证书给 WEB 应用服务器(如:nginx)使用。
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl genrsa -out nginx.key #生成私钥
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl req -new -key nginx.key -out nginx.csr #生成证书请求
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl ca -days 3650 -in nginx.csr -cert ca.pem -keyfile ca.key -out nginx.pem #生成服务端证书
生成证书请求时输入如下信息:
Country Name = CN
State or Province Name = JS
Locality Name = NJ
Organization Name = ABC
Organizational Unit Name = ABC_123
Common Name = abc.com #可以输入服务器的域名或 IP
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl genrsa -des3 -out client.key 1024 #生成私钥,指定密钥的加密算法,提高安全性
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl req -new -key client.key -out client.csr #生成证书请求
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl ca -days 1500 -in client.csr -cert ca.pem -keyfile ca.key -out client.pem #生成客户端证书
shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl pkcs12 -export -clcerts -in client.pem -inkey client.key -out client.p12 #生成 pkcs12 格式的客户端证书
生成证书请求时输入如下信息:
Country Name = CN
State or Province Name = JS
Locality Name = NJ
Organization Name = ABC
Organizational Unit Name = ABC_123
Common Name = client #可以输入客户端的名称标识
生成 pkcs12 格式证书时,需要先输入私钥的密码,然后再设置输出的 pkcs12 证书的密码。
在进行 ca 命令操作时,可能会报如下的错误:
unable to access the xxx/newcerts directory
或
./demoCA/newcerts: No such file or directory
这主要是由于 CA 目录不存在或当前用户无访问权限导致的。
对于这种情况可以通过指定 OpenSSL 配置文件的方式来处理。
A、拷贝 OpenSSL 配置文件
cp /etc/pki/tls/openssl.cnf /home/mongo/openssl.cnf
B、修改拷贝的配置文件中 CA 目录
#dir = /etc/pki/CA # Where everything is kept
dir = /home/mongo/test/CA
在 CA 目录(/home/mongo/test/CA)下创建 newcerts 目录(空目录)、index.txt 文件(空文件)、serial 文件(内容为一个数字,如:00)。
C、执行 ca 命令时指定配置文件的位置
/home/mongo/soft/openssl-1.1.1s/bin/openssl ca -days 1000 -in client.csr -cert ca.pem -keyfile ca.key -out client.pem -config /home/mongo/openssl.cnf
对于这种情况可以先修改默认配置文件中的 CA 目录,再在设置的 CA 目录下创建对应的目录及文件。
A、修改默认配置文件中 CA 目录
找到 OpenSSL 的默认配置文件并修改:
#dir = ./demoCA # Where everything is kept
dir = /home/mongo/soft/openssl-1.1.1s/ssl/CA
在 CA 目录(/home/mongo/soft/openssl-1.1.1s/ssl/CA)下创建 newcerts 目录(空目录)、index.txt 文件(空文件)、serial 文件(内容为一个数字,如:00)。
B、正常执行 ca 命令
/home/mongo/soft/openssl-1.1.1s/bin/openssl ca -days 1000 -in client.csr -cert ca.pem -keyfile ca.key -out client.pem
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
目录一.加解密算法数字签名对称加密DES(DataEncryptionStandard)3DES(TripleDES)AES(AdvancedEncryptionStandard)RSA加密法DSA(DigitalSignatureAlgorithm)ECC(EllipticCurvesCryptography)非对称加密签名与加密过程非对称加密的应用对称加密与非对称加密的结合二.数字证书图解一.加解密算法加密简单而言就是通过一种算法将明文信息转换成密文信息,信息的的接收方能够通过密钥对密文信息进行解密获得明文信息的过程。根据加解密的密钥是否相同,算法可以分为对称加密、非对称加密、对称加密和非
?博客主页:https://xiaoy.blog.csdn.net?本文由呆呆敲代码的小Y原创,首发于CSDN??学习专栏推荐:Unity系统学习专栏?游戏制作专栏推荐:游戏制作?Unity实战100例专栏推荐:Unity实战100例教程?欢迎点赞?收藏⭐留言?如有错误敬请指正!?未来很长,值得我们全力奔赴更美好的生活✨------------------❤️分割线❤️-------------------------
在Ruby中,是否有一种简单的方法可以将n维数组中的每个元素乘以一个数字?这样:[1,2,3,4,5].multiplied_by2==[2,4,6,8,10]和[[1,2,3],[1,2,3]].multiplied_by2==[[2,4,6],[2,4,6]]?(很明显,我编写了multiplied_by函数以区别于*,它似乎连接了数组的多个副本,不幸的是这不是我需要的)。谢谢! 最佳答案 它的长格式等价物是:[1,2,3,4,5].collect{|n|n*2}其实并没有那么复杂。你总是可以使你的multiply_by方法:c
我正在使用Ruby解决一些ProjectEuler问题,特别是这里我要讨论的问题25(Fibonacci数列中包含1000位数字的第一项的索引是多少?)。起初,我使用的是Ruby2.2.3,我将问题编码为:number=3a=1b=2whileb.to_s.length但后来我发现2.4.2版本有一个名为digits的方法,这正是我需要的。我转换为代码:whileb.digits.length当我比较这两种方法时,digits慢得多。时间./025/problem025.rb0.13s用户0.02s系统80%cpu0.190总计./025/problem025.rb2.19s用户0.0
我正在构建一个小部件来显示奥运会的奖牌数。我有一个“国家”对象的集合,其中每个对象都有一个“名称”属性,以及奖牌计数的“金”、“银”、“铜”。列表应该排序:1.首先是奖牌总数2.如果奖牌相同,按类型分割(金>银>铜,即2金>1金+1银)3.如果奖牌和类型相同,则按字母顺序子排序我正在用ruby做这件事,但我想语言并不重要。我确实找到了一个解决方案,但如果感觉必须有更优雅的方法来实现它。这是我做的:使用加权奖牌总数创建一个虚拟属性。因此,如果他们有2个金牌和1个银牌,加权总数将为“3.020100”。1金1银1铜为“3.010101”由于我们希望将奖牌数排序为最高的,因此列表按降序排
我想为名字验证编写一个正则表达式。正则表达式应包括所有字母(拉丁/法语/德语字符等)。但是我想从中排除数字并允许-。所以基本上它是\w(减)数(加)-。请帮忙。 最佳答案 ^[\p{L}-]+$\p{L}匹配anykindofletterfromanylanguage. 关于ruby-on-rails-rails中的正则表达式匹配[\w]和"-"但不匹配数字,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.c
在我的应用程序中,我有一个文本字段,用户可以在其中输入类似这样的内容"1,2,3,4"存储到数据库中。现在,当我想使用内部数字时,我有两个选择:"1,2,3,4".split(',')或string.scan(/\d+/)do|x|a两种方式我都得到一个像这样的数组["1","2","3","4"]然后我可以通过在每个数字上调用to_i来使用这些数字。有没有更好的方法可以转换"1,2,3"to[1,2,3]andnot["1","2","3"] 最佳答案 str.split(",").map{|i|i.to_i}但是这个想法对你来说
我有一个随机大小的散列,它可能有类似"100"的值,我想将其转换为整数。我知道我可以使用value.to_iifvalue.to_i.to_s==value来做到这一点,但我不确定我将如何在我的散列中递归地做到这一点,考虑到一个值可以是一个字符串,或一个数组(哈希或字符串),或另一个哈希。 最佳答案 这是一个非常简单的递归实现(尽管必须同时处理数组和散列会增加一些技巧)。deffixnumifyobjifobj.respond_to?:to_i#IfwecancastittoaFixnum,doit.obj.to_ielsifobj
什么是测试格式验证的最佳方法让我们说一个用户名,使用字母数字的正则表达式,但不是纯数字?我一直在我的模型中使用以下验证validates:username,:format=>{:with=>/^[a-z0-9]+[-a-z0-9]*[a-z0-9]+$/i}数字用户名(例如“342”)通过了验证,这是我不想要的。 最佳答案 您想“向前看”一封信:/\A(?=.*[a-z])[a-z\d]+\Z/i 关于ruby-on-rails-Rails格式验证——字母数字,但不是纯数字,我们在Sta