前 言
前几个月记录了使用Java生成根证书并签署下级证书的例子,然而细心地朋友会发现使用Java有很多局限性,比如说“增强型密钥用法”无法添加(也许是我不会添加)导致证书功能受限。
今天记录使用OpenSSL生成真正的证书
实际操作
生成根证书
1.使用命令生成私钥
openssl genrsa -aes256 -passout pass:设定私钥密码 -out prikey.pem 8192
其中:
“-aes256”为加密算法,OpenSSL支持常见的算法,甚至还支持一些不常用的算法,根据国际标准建议采用AES256位加密算法,在安全性和实用性(例如处理器硬件支持)上都有良好表现。
“-out”后面为输出文件名
“8192”为密钥长度,理论上越长越安全,最低安全标准为1024位,根据RSA的加密原理,在不出现算法捷径情况下其安全性主要依赖于密钥长度。
2.为方便使用将私钥解密
openssl rsa -in prikey.pem -out unenprikey.pem -passin pass:设定私钥密码
3.根据私钥生成自签名证书
openssl req -new -x509 -key unenprikey.pem -days 18250 -out cert.crt -sha512 -config ./openssl.cnf -subj /OU=某某部门/O=某某集团/C=国家(英文字符,如CN)/CN=证书通用名
其中:
“-days”后面的数字指证书有效时间
“-sha512”指混淆算法,同加密算法一样支持较多参数,给出的推荐参数为最优选项
“-config”后面的文件是指OpenSSL的配置文件,配置文件内容指定了证书的策略以及规则
“-subj”后面是定义证书使用者参数,若不加入此参数也会在生成证书时根据OpenSSL配置文件中的配置要求现场填写
签署下级证书
1.同根证书一样生成私钥并解密
2.根据私钥生成签名请求文件
openssl req -new -key unenprikey.pem -days 2190 -out cert.csr -sha512 -config ./openssl.cnf -subj /OU=某某部门/O=某某集团/C=国家(英文字符,如CN)/CN=证书通用名
3.使用根证书的证书文件、私钥文件签署下级证书
根据OpenSSL配置文件中的配置,部署根证书以及根证书密钥到指定位置,并建立对应功能的工作文件夹(少做一步都会报错)
openssl ca -in cert.csr -config ./openssl.cnf
签署后的证书文件会生成在OpenSSL配置文件指定的目录中
附 录
OpenSSL配置文件配置解析
我使用的OpenSSL文件内容如下:
内容做了很多注释、调整与汉化方便调节与理解
######################## # OpenSSL 证书配置文件 # ######################## # 如果未定义HOME后续参数将不会生效 HOME = . RANDFILE = $ENV::HOME/.rnd # 附加 OBJECT IDENTIFIER 信息: #oid_file = $ENV::HOME/.oid oid_section = new_oids # To use this configuration file with the "-extfile" option of the # "openssl x509" utility, name here the section containing the # X.509v3 extensions to use: # extensions = # (Alternatively, use a configuration file that has only # X.509v3 extensions in its main [= default] section.) [ new_oids ] # 我们可以在这里添加新的 OIDs 以供下面的 'ca', 'req' 和 'ts' 使用. # 添加示例: # testoid1=1.2.3.4 # 或者像这样引用上方声明的配置: # testoid2=${testoid1}.5.6 # TSA策略 tsa_policy1 = 1.2.3.4.1 tsa_policy2 = 1.2.3.4.5.6 tsa_policy3 = 1.2.3.4.5.7 ##############################################基础配置区 - 开始###################################################### #################################################################### [ ca ] default_ca = Certificate_Config # 选择默认的证书基础配置区 #################################################################### [ Certificate_Config ] dir = /usr/CA # 设定工作目录 certs = $dir/"Root CA" # CA证书保存位置 certificate = $dir/"Root CA"/CA.crt # CA证书文件位置 private_key = $dir/"Root CA"/CA.key # CA证书私钥位置 RANDFILE = $dir/"Root CA"/.rand # CA证书私钥随机数文件 database = $dir/Data/database # 数据库索引文件 serial = $dir/Data/serial # 序列号索引文件 #unique_subject = no # 设置为 'no' 则允许创建多个具有相同主题的证书 new_certs_dir = $dir/"Sign Certs" # 签署下级证书保存位置 crl_dir = $dir/Crl # 发行crl文件位置 crl = $dir/Crl/crl.pem # 当前的CRL文件 crlnumber = $dir/Crl/crlnumber # crl编号文件 # must be commented out to leave a V1 CRL #被签署证书的扩展规则(选择是CA证书还是用户证书) x509_extensions = usr_cert # 用户证书 #x509_extensions = v3_ca # CA证书 # Comment out the following two lines for the "traditional" # (and highly broken) format. name_opt = ca_default # Subject Name options cert_opt = ca_default # Certificate field options # 扩展复制选项: 谨慎使用. # copy_extensions = copy # 添加到 CRL 的扩展. Note:定义生成CRL时需要加入的扩展项字段。如果注释则生成v1而不是v2版本的CRL。 # crl_extensions = crl_ext default_days = 5475 # CA证书的有效时长 default_crl_days= 30 # how long before next CRL default_md = sha512 # 公钥混淆算法 preserve = no # keep passed DN ordering # CA使用者配置区域选择 policy = policy_match # CA使用者配置区域 [ policy_match ] countryName = match stateOrProvinceName = optional organizationName = match organizationalUnitName = optional commonName = supplied emailAddress = optional # 其他任何证书配置 # 您必须列出所有可以接受的 'object' 类型 [ policy_anything ] countryName = optional stateOrProvinceName = optional localityName = optional organizationName = optional organizationalUnitName = optional commonName = supplied emailAddress = optional #################################################################### ##############################################签署用户证书控制区开始###################################################### [ usr_cert ] # 当CA签署请求时会应用下列扩展 # 是否为CA证书 basicConstraints=CA:FALSE # 以下是 nsCertType 使用的一些示例. # 如果忽略该证书则可用于除对象签名之外的任何事情 # SSL服务. # nsCertType = server # 对象签名证书 # nsCertType = objsign # 使用场景 # nsCertType = client, email, objsign # 这是一个客户端证书中 keyUsage 的典型. # keyUsage = nonRepudiation, digitalSignature, keyEncipherment # 内容将显示在Netscape comment框中 #nsComment = "OpenSSL Generated Certificate" # PKIX recommendations harmless if included in all certificates. subjectKeyIdentifier=hash authorityKeyIdentifier=keyid,issuer # subjectAltName 和 issuerAltname 配置 # 导入电子邮件地址 # subjectAltName=email:copy # 根据PKIX生成不被弃用的证书替代方案 # deprecated according to PKIX. # subjectAltName=email:move # Copy subject details # issuerAltName=issuer:copy #nsCaRevocationUrl = http://www.domain.dom/ca-crl.pem #nsBaseUrl #nsRevocationUrl #nsRenewalUrl #nsCaPolicyUrl #nsSslServerName # 这是 TSA 证书必要选项.增强型秘钥用法 # extendedKeyUsage = critical,timeStamping ##############################################签署用户证书控制区结束###################################################### ##############################################CA证书控制区开始###################################################### [ v3_ca ] subjectKeyIdentifier=hash authorityKeyIdentifier=keyid:always,issuer basicConstraints = critical,CA:true # 密钥用法: 这是CA证书的标准用法. keyUsage = cRLSign, keyCertSign, nonRepudiation, digitalSignature, keyAgreement, keyEncipherment, dataEncipherment #这是 TSA 证书必要选项.增强型秘钥用法 extendedKeyUsage = critical,timeStamping,codeSigning,serverAuth,clientAuth,msCTLSign,1.3.6.1.5.5.8.2.2,emailProtection,1.3.6.1.4.1.311.10.3.11,msEFS,1.3.6.1.4.1.311.20.2.2 # 有些时候可能需要下列配置 # nsCertType = sslCA, emailCA # Include email address in subject alt name: another PKIX recommendation # subjectAltName=email:copy # Copy issuer details # issuerAltName=issuer:copy # DER hex encoding of an extension: beware experts only! # obj=DER:02:03 # Where 'obj' is a standard or added object # You can even override a supported extension: # basicConstraints= critical, DER:30:03:01:01:FF ##############################################CA证书控制区结束###################################################### ##################################################################### ##############################################自签名证书控制区开始###################################################### [ req ] default_bits = 8192 #默认密钥长度 default_keyfile = privkey.pem #默认密钥文件 distinguished_name = req_distinguished_name #证书信息规则 attributes = req_attributes #证书密码规则 x509_extensions = usr_cert #添加到自签名证书的扩展 # 私钥密码,如果未设置将会提示输入 input_password = ************ output_password = ************ # 设定编码类型(用于支持多平台与语言). 有如下选择 # default: PrintableString, T61String, BMPString. # pkix : PrintableString, BMPString (PKIX recommendation before 2004) # utf8only: 只使用 UTF8Strings (PKIX recommendation after 2004). # nombstr : PrintableString, T61String (不使用 BMPStrings 或 UTF8Strings). # MASK:XXXX a literal mask value. # 警告: 老版本的 Netscape crash 在 BMPStrings 或 UTF8Strings 中崩溃. string_mask = utf8only ##############################################自签名证书控制区结束###################################################### [ req_distinguished_name ] countryName = 国家名称 (两个字母的简称) countryName_default = CN countryName_min = 2 countryName_max = 2 stateOrProvinceName = 州或省的名称 (全名) #stateOrProvinceName_default= Some-State localityName = 地区名称 (例如:城市,直辖市区) 0.organizationName = 组织名称 (例如:公司,非营利性组织) # 可以加入多个组织名称,通常不需要 #0.organizationName_default = Internet Widgits Pty Ltd #1.organizationName = Second Organization Name (eg, company) #1.organizationName_default = World Wide Web Pty Ltd organizationalUnitName= 组织单位名称 (例如:部分,科室) #organizationalUnitName_default = commonName = 通用名称 (e.g. 域名 or YOUR name) commonName_max = 64 emailAddress = Email地址 emailAddress_max = 64 # SET-ex3 = SET extension number 3 [ req_attributes ] challengePassword = 一个高强度的密码(4-20位) challengePassword_min= 4 challengePassword_max= 20 unstructuredName = 可选的公司名称 [ crl_ext ] # CRL extensions. # Only issuerAltName and authorityKeyIdentifier make any sense in a CRL. # issuerAltName=issuer:copy authorityKeyIdentifier=keyid:always [ proxy_cert_ext ] # These extensions should be added when creating a proxy certificate # This goes against PKIX guidelines but some CAs do it and some software # requires this to avoid interpreting an end user certificate as a CA. basicConstraints=CA:FALSE # Here are some examples of the usage of nsCertType. If it is omitted # the certificate can be used for anything *except* object signing. # This is OK for an SSL server. # nsCertType = server # For an object signing certificate this would be used. # nsCertType = objsign # For normal client use this is typical # nsCertType = client, email # and for everything including object signing: # nsCertType = client, email, objsign # This is typical in keyUsage for a client certificate. # keyUsage = nonRepudiation, digitalSignature, keyEncipherment # This will be displayed in Netscape's comment listbox. nsComment = "OpenSSL Generated Certificate" # PKIX recommendations harmless if included in all certificates. subjectKeyIdentifier=hash authorityKeyIdentifier=keyid,issuer # This stuff is for subjectAltName and issuerAltname. # Import the email address. # subjectAltName=email:copy # An alternative to produce certificates that aren't # deprecated according to PKIX. # subjectAltName=email:move # Copy subject details # issuerAltName=issuer:copy #nsCaRevocationUrl = http://www.domain.dom/ca-crl.pem #nsBaseUrl #nsRevocationUrl #nsRenewalUrl #nsCaPolicyUrl #nsSslServerName # This really needs to be in place for it to be a proxy certificate. proxyCertInfo=critical,language:id-ppl-anyLanguage,pathlen:3,policy:foo #################################################################### [ tsa ] default_tsa = tsa_config1 # the default TSA section [ tsa_config1 ] # These are used by the TSA reply generation only. dir = /usr/CA # TSA root directory serial = $dir/tsaserial # The current serial number (mandatory) crypto_device = builtin # OpenSSL engine to use for signing signer_cert = $dir/tsacert.pem # The TSA signing certificate # (optional) certs = $dir/cacert.pem # Certificate chain to include in reply # (optional) signer_key = $dir/private/tsakey.pem # The TSA private key (optional) signer_digest = sha512 # Signing digest to use. (Optional) default_policy = tsa_policy1 # Policy if request did not specify it # (optional) other_policies = tsa_policy2, tsa_policy3 # acceptable policies (optional) digests = sha1, sha256, sha384, sha512 # Acceptable message digests (mandatory) accuracy = secs:1, millisecs:500, microsecs:100 # (optional) clock_precision_digits = 0 # number of digits after dot. (optional) ordering = yes # Is ordering defined for timestamps? # (optional, default: no) tsa_name = yes # Must the TSA name be included in the reply? # (optional, default: no) ess_cert_id_chain = no # Must the ESS cert id chain be included? # (optional, default: no) ess_cert_id_alg = sha512 # algorithm to compute certificate # identifier (optional, default: sha1)
发表回复