操作系统CentOS6.6

注:windows版本的Openssl无法做这个实验,由于所有编译的window版本openssl没有对openssl目录重新定向,导致在windows下找不到pki目录

初始化

  1. rm -rf /etc/pki/CA/*.old
  2. touch /etc/pki/CA/index.txt
  3. echo 01 > /etc/pki/CA/serial
  4. echo 02 > /etc/pki/CA/serial
  5. rm -rf keys
  6. mkdir keys

生成根CA并自签(Common Name填RootCA)

  1. openssl genrsa -des3 -out keys/RootCA.key 2048
  2. openssl req -new -x509 -days 3650 -key keys/RootCA.key -out keys/RootCA.crt

生成二级CA(Common Name填secondCA)

  1. openssl genrsa -des3 -out keys/secondCA.key 2048
  2. openssl rsa -in keys/secondCA.key -out keys/secondCA.key
  3. openssl req -new -days 3650 -key keys/secondCA.key -out keys/secondCA.csr
  4. openssl ca -extensions v3_ca -in keys/secondCA.csr -config /etc/pki/tls/openssl.cnf -days 3650 -out keys/secondCA.crt -cert keys/RootCA.crt -keyfile keys/RootCA.key

生成三级CA(Common Name填thirdCA)

  1. openssl genrsa -des3 -out keys/thirdCA.key 2048
  2. openssl rsa -in keys/thirdCA.key -out keys/thirdCA.key
  3. openssl req -new -days 3650 -key keys/thirdCA.key -out keys/thirdCA.csr
  4. openssl ca -extensions v3_ca -in keys/thirdCA.csr -config /etc/pki/tls/openssl.cnf -days 3650 -out keys/thirdCA.crt -cert keys/secondCA.crt -keyfile keys/secondCA.key

使用三级CA签发服务器证书

  1. openssl genrsa -des3 -out keys/server.key 2048
  2. openssl rsa -in keys/server.key -out keys/server.key
  3. openssl req -new -days 3650 -key keys/server.key -out keys/server.csr
  4. openssl ca -in keys/server.csr -config /etc/pki/tls/openssl.cnf -days 3650 -out keys/server.crt -cert keys/thirdCA.crt -keyfile keys/thirdCA.key

最后将RootCA导入受信任的根证书颁发机构,其他两个证书导入中级CA机构,服务器证书根据需要导入

结果如下:

笔者生成的证书下载:http://download.csdn.net/detail/gsls200808/8697635

报错情况记录

  1. The mandatory stateOrProvinceName field was missing

原因openssl.cnf中CA policy有三个match,必须要填一样的,或者改成optional

  1. # For the CA policy
  2. [ policy_match ]
  3. countryName = match
  4. stateOrProvinceName = match
  5. organizationName = match
  6. organizationalUnitName = optional
  7. commonName = supplied
  8. emailAddress = optional

解决方法:

分别填CN、LiaoNing、ORG

清空文件再次生成证书报错

  1. ERROR:Serial number 01 has already been issued,
  2. check the database/serial_file for corruption

官方承认这是个bug

解决方法:/etc/pki/CA/serial这个文件实际上清空还是会记录生成证书的次数,所以把它改成比较大的数

报错

  1. failed to update database
  2. TXT_DB error number 2

这个也是bug,三个方法

产生的原因是:
This thing happens when certificates share common data. You cannot have two
certificates that look otherwise the same.
方法一:
修改demoCA下 index.txt.attr
将unique_subject = yes改为unique_subject = no
方法二:
删除demoCA下的index.txt,并再touch下rm index.txt touch index.txt
方法三:
将 common name设置成不同的

openssl官方在2014年6月修复了这个已经存在十年的问题
http://rt.openssl.org/Ticket/Display.html?id=502&user=guest&pass=guest

补充:

CentOS6.6完整的openssl.cnf配置文件

  1. #
  2. # OpenSSL example configuration file.
  3. # This is mostly being used for generation of certificate requests.
  4. #
  5.  
  6. # This definition stops the following lines choking if HOME isn't
  7. # defined.
  8. HOME = .
  9. RANDFILE = $ENV::HOME/.rnd
  10.  
  11. # Extra OBJECT IDENTIFIER info:
  12. #oid_file = $ENV::HOME/.oid
  13. oid_section = new_oids
  14.  
  15. # To use this configuration file with the "-extfile" option of the
  16. # "openssl x509" utility, name here the section containing the
  17. # X.509v3 extensions to use:
  18. # extensions =
  19. # (Alternatively, use a configuration file that has only
  20. # X.509v3 extensions in its main [= default] section.)
  21.  
  22. [ new_oids ]
  23.  
  24. # We can add new OIDs in here for use by 'ca', 'req' and 'ts'.
  25. # Add a simple OID like this:
  26. # testoid1=1.2.3.4
  27. # Or use config file substitution like this:
  28. # testoid2=${testoid1}.5.6
  29.  
  30. # Policies used by the TSA examples.
  31. tsa_policy1 = 1.2.3.4.
  32. tsa_policy2 = 1.2.3.4.5.6
  33. tsa_policy3 = 1.2.3.4.5.7
  34.  
  35. ####################################################################
  36. [ ca ]
  37. default_ca = CA_default # The default ca section
  38.  
  39. ####################################################################
  40. [ CA_default ]
  41.  
  42. dir = /etc/pki/CA # Where everything is kept
  43. certs = $dir/certs # Where the issued certs are kept
  44. crl_dir = $dir/crl # Where the issued crl are kept
  45. database = $dir/index.txt # database index file.
  46. #unique_subject = no # Set to 'no' to allow creation of
  47. # several ctificates with same subject.
  48. new_certs_dir = $dir/newcerts # default place for new certs.
  49.  
  50. certificate = $dir/cacert.pem # The CA certificate
  51. serial = $dir/serial # The current serial number
  52. crlnumber = $dir/crlnumber # the current crl number
  53. # must be commented out to leave a V1 CRL
  54. crl = $dir/crl.pem # The current CRL
  55. private_key = $dir/private/cakey.pem# The private key
  56. RANDFILE = $dir/private/.rand # private random number file
  57.  
  58. x509_extensions = usr_cert # The extentions to add to the cert
  59.  
  60. # Comment out the following two lines for the "traditional"
  61. # (and highly broken) format.
  62. name_opt = ca_default # Subject Name options
  63. cert_opt = ca_default # Certificate field options
  64.  
  65. # Extension copying option: use with caution.
  66. # copy_extensions = copy
  67.  
  68. # Extensions to add to a CRL. Note: Netscape communicator chokes on V2 CRLs
  69. # so this is commented out by default to leave a V1 CRL.
  70. # crlnumber must also be commented out to leave a V1 CRL.
  71. # crl_extensions = crl_ext
  72.  
  73. default_days = # how long to certify for
  74. default_crl_days= # how long before next CRL
  75. default_md = default # use public key default MD
  76. preserve = no # keep passed DN ordering
  77.  
  78. # A few difference way of specifying how similar the request should look
  79. # For type CA, the listed attributes must be the same, and the optional
  80. # and supplied fields are just that :-)
  81. policy = policy_match
  82.  
  83. # For the CA policy
  84. [ policy_match ]
  85. countryName = match
  86. stateOrProvinceName = match
  87. organizationName = match
  88. organizationalUnitName = optional
  89. commonName = supplied
  90. emailAddress = optional
  91.  
  92. # For the 'anything' policy
  93. # At this point in time, you must list all acceptable 'object'
  94. # types.
  95. [ policy_anything ]
  96. countryName = optional
  97. stateOrProvinceName = optional
  98. localityName = optional
  99. organizationName = optional
  100. organizationalUnitName = optional
  101. commonName = supplied
  102. emailAddress = optional
  103.  
  104. ####################################################################
  105. [ req ]
  106. default_bits =
  107. default_md = sha1
  108. default_keyfile = privkey.pem
  109. distinguished_name = req_distinguished_name
  110. attributes = req_attributes
  111. x509_extensions = v3_ca # The extentions to add to the self signed cert
  112.  
  113. # Passwords for private keys if not present they will be prompted for
  114. # input_password = secret
  115. # output_password = secret
  116.  
  117. # This sets a mask for permitted string types. There are several options.
  118. # default: PrintableString, T61String, BMPString.
  119. # pkix : PrintableString, BMPString (PKIX recommendation before )
  120. # utf8only: only UTF8Strings (PKIX recommendation after ).
  121. # nombstr : PrintableString, T61String (no BMPStrings or UTF8Strings).
  122. # MASK:XXXX a literal mask value.
  123. # WARNING: ancient versions of Netscape crash on BMPStrings or UTF8Strings.
  124. string_mask = utf8only
  125.  
  126. # req_extensions = v3_req # The extensions to add to a certificate request
  127.  
  128. [ req_distinguished_name ]
  129. countryName = Country Name ( letter code)
  130. countryName_default = XX
  131. countryName_min =
  132. countryName_max =
  133.  
  134. stateOrProvinceName = State or Province Name (full name)
  135. #stateOrProvinceName_default = Default Province
  136.  
  137. localityName = Locality Name (eg, city)
  138. localityName_default = Default City
  139.  
  140. .organizationName = Organization Name (eg, company)
  141. .organizationName_default = Default Company Ltd
  142.  
  143. # we can do this but it is not needed normally :-)
  144. #.organizationName = Second Organization Name (eg, company)
  145. #.organizationName_default = World Wide Web Pty Ltd
  146.  
  147. organizationalUnitName = Organizational Unit Name (eg, section)
  148. #organizationalUnitName_default =
  149.  
  150. commonName = Common Name (eg, your name or your server\'s hostname)
  151. commonName_max =
  152.  
  153. emailAddress = Email Address
  154. emailAddress_max =
  155.  
  156. # SET-ex3 = SET extension number
  157.  
  158. [ req_attributes ]
  159. challengePassword = A challenge password
  160. challengePassword_min =
  161. challengePassword_max =
  162.  
  163. unstructuredName = An optional company name
  164.  
  165. [ usr_cert ]
  166.  
  167. # These extensions are added when 'ca' signs a request.
  168.  
  169. # This goes against PKIX guidelines but some CAs do it and some software
  170. # requires this to avoid interpreting an end user certificate as a CA.
  171.  
  172. basicConstraints=CA:FALSE
  173.  
  174. # Here are some examples of the usage of nsCertType. If it is omitted
  175. # the certificate can be used for anything *except* object signing.
  176.  
  177. # This is OK for an SSL server.
  178. # nsCertType = server
  179.  
  180. # For an object signing certificate this would be used.
  181. # nsCertType = objsign
  182.  
  183. # For normal client use this is typical
  184. # nsCertType = client, email
  185.  
  186. # and for everything including object signing:
  187. # nsCertType = client, email, objsign
  188.  
  189. # This is typical in keyUsage for a client certificate.
  190. # keyUsage = nonRepudiation, digitalSignature, keyEncipherment
  191.  
  192. # This will be displayed in Netscape's comment listbox.
  193. nsComment = "OpenSSL Generated Certificate"
  194.  
  195. # PKIX recommendations harmless if included in all certificates.
  196. subjectKeyIdentifier=hash
  197. authorityKeyIdentifier=keyid,issuer
  198.  
  199. # This stuff is for subjectAltName and issuerAltname.
  200. # Import the email address.
  201. # subjectAltName=email:copy
  202. # An alternative to produce certificates that aren't
  203. # deprecated according to PKIX.
  204. # subjectAltName=email:move
  205.  
  206. # Copy subject details
  207. # issuerAltName=issuer:copy
  208.  
  209. #nsCaRevocationUrl = http://www.domain.dom/ca-crl.pem
  210. #nsBaseUrl
  211. #nsRevocationUrl
  212. #nsRenewalUrl
  213. #nsCaPolicyUrl
  214. #nsSslServerName
  215.  
  216. # This is required for TSA certificates.
  217. # extendedKeyUsage = critical,timeStamping
  218.  
  219. [ v3_req ]
  220.  
  221. # Extensions to add to a certificate request
  222.  
  223. basicConstraints = CA:FALSE
  224. keyUsage = nonRepudiation, digitalSignature, keyEncipherment
  225.  
  226. [ v3_ca ]
  227.  
  228. # Extensions for a typical CA
  229.  
  230. # PKIX recommendation.
  231.  
  232. subjectKeyIdentifier=hash
  233.  
  234. authorityKeyIdentifier=keyid:always,issuer
  235.  
  236. # This is what PKIX recommends but some broken software chokes on critical
  237. # extensions.
  238. #basicConstraints = critical,CA:true
  239. # So we do this instead.
  240. basicConstraints = CA:true
  241.  
  242. # Key usage: this is typical for a CA certificate. However since it will
  243. # prevent it being used as an test self-signed certificate it is best
  244. # left out by default.
  245. # keyUsage = cRLSign, keyCertSign
  246.  
  247. # Some might want this also
  248. # nsCertType = sslCA, emailCA
  249.  
  250. # Include email address in subject alt name: another PKIX recommendation
  251. # subjectAltName=email:copy
  252. # Copy issuer details
  253. # issuerAltName=issuer:copy
  254.  
  255. # DER hex encoding of an extension: beware experts only!
  256. # obj=DER::
  257. # Where 'obj' is a standard or added object
  258. # You can even override a supported extension:
  259. # basicConstraints= critical, DER:::::FF
  260.  
  261. [ crl_ext ]
  262.  
  263. # CRL extensions.
  264. # Only issuerAltName and authorityKeyIdentifier make any sense in a CRL.
  265.  
  266. # issuerAltName=issuer:copy
  267. authorityKeyIdentifier=keyid:always
  268.  
  269. [ proxy_cert_ext ]
  270. # These extensions should be added when creating a proxy certificate
  271.  
  272. # This goes against PKIX guidelines but some CAs do it and some software
  273. # requires this to avoid interpreting an end user certificate as a CA.
  274.  
  275. basicConstraints=CA:FALSE
  276.  
  277. # Here are some examples of the usage of nsCertType. If it is omitted
  278. # the certificate can be used for anything *except* object signing.
  279.  
  280. # This is OK for an SSL server.
  281. # nsCertType = server
  282.  
  283. # For an object signing certificate this would be used.
  284. # nsCertType = objsign
  285.  
  286. # For normal client use this is typical
  287. # nsCertType = client, email
  288.  
  289. # and for everything including object signing:
  290. # nsCertType = client, email, objsign
  291.  
  292. # This is typical in keyUsage for a client certificate.
  293. # keyUsage = nonRepudiation, digitalSignature, keyEncipherment
  294.  
  295. # This will be displayed in Netscape's comment listbox.
  296. nsComment = "OpenSSL Generated Certificate"
  297.  
  298. # PKIX recommendations harmless if included in all certificates.
  299. subjectKeyIdentifier=hash
  300. authorityKeyIdentifier=keyid,issuer
  301.  
  302. # This stuff is for subjectAltName and issuerAltname.
  303. # Import the email address.
  304. # subjectAltName=email:copy
  305. # An alternative to produce certificates that aren't
  306. # deprecated according to PKIX.
  307. # subjectAltName=email:move
  308.  
  309. # Copy subject details
  310. # issuerAltName=issuer:copy
  311.  
  312. #nsCaRevocationUrl = http://www.domain.dom/ca-crl.pem
  313. #nsBaseUrl
  314. #nsRevocationUrl
  315. #nsRenewalUrl
  316. #nsCaPolicyUrl
  317. #nsSslServerName
  318.  
  319. # This really needs to be in place for it to be a proxy certificate.
  320. proxyCertInfo=critical,language:id-ppl-anyLanguage,pathlen:,policy:foo
  321.  
  322. ####################################################################
  323. [ tsa ]
  324.  
  325. default_tsa = tsa_config1 # the default TSA section
  326.  
  327. [ tsa_config1 ]
  328.  
  329. # These are used by the TSA reply generation only.
  330. dir = ./demoCA # TSA root directory
  331. serial = $dir/tsaserial # The current serial number (mandatory)
  332. crypto_device = builtin # OpenSSL engine to use for signing
  333. signer_cert = $dir/tsacert.pem # The TSA signing certificate
  334. # (optional)
  335. certs = $dir/cacert.pem # Certificate chain to include in reply
  336. # (optional)
  337. signer_key = $dir/private/tsakey.pem # The TSA private key (optional)
  338.  
  339. default_policy = tsa_policy1 # Policy if request did not specify it
  340. # (optional)
  341. other_policies = tsa_policy2, tsa_policy3 # acceptable policies (optional)
  342. digests = md5, sha1 # Acceptable message digests (mandatory)
  343. accuracy = secs:, millisecs:, microsecs: # (optional)
  344. clock_precision_digits = # number of digits after dot. (optional)
  345. ordering = yes # Is ordering defined for timestamps?
  346. # (optional, default: no)
  347. tsa_name = yes # Must the TSA name be included in the reply?
  348. # (optional, default: no)
  349. ess_cert_id_chain = no # Must the ESS cert id chain be included?
  350. # (optional, default: no)

openssl生成证书链多级证书的更多相关文章

  1. openssl生成自签名证书

    1.生成x509格式的CA自签名证书 openssl req -new -x509 -keyout ca.key -out ca.crt 2.生成服务端的私钥(key文件)及申请证书文件csr文件 o ...

  2. 使用openssl生成双向加密证书(转)

    要生成证书的目录下建立几个文件和文件夹,有./demoCA/./demoCA/newcerts/./demoCA/private/./demoCA/index.txt (空文件,生成证书时会将数据记录 ...

  3. java验证openssl生成的ssl证书和私钥是否匹配

    最近有一个需求上传ssl证书和私钥,但是上传之前需要验证ssl证书和私钥是否正确,其中的业务逻辑涉及到以下几点: 一.读取ssl证书,读取ssl证书公钥       要实现该功能比较简单,java里面 ...

  4. 如何在linux系统内用openssl 生成 过期的证书

    需求:验证过期的证书在系统中不能使用. 问题:如何生成过期的证书呢? 解决方法:1.调整系统时间 2.生成证书 3.验证证书startdate 和 enddate 是否符合你的预期 1.调整系统时间 ...

  5. 用OpenSSL生成自签名证书在IIS上搭建Https站点(用于iOS的https访问)

    前提: 先安装openssl,安装有两种方式,第一种直接下载安装包,装上就可运行:第二种可以自己下载源码,自己编译.这里推荐第一种. 安装包:http://slproweb.com/products/ ...

  6. Windows下使用OpenSSL生成自签证书

    下载OpenSSLhttp://slproweb.com/products/Win32OpenSSL.html 生成证书 生成crt证书CMD进入安装bin目录,执行命令:openssl req -x ...

  7. OpenSSL使用1(用OpenSSL生成自签名证书在IIS上搭建Https站点)(用于iOS的https访问)

    前提: 先安装openssl,安装有两种方式,第一种直接下载安装包,装上就可运行:第二种可以自己下载源码,自己编译.这里推荐第一种. 安装包:http://slproweb.com/products/ ...

  8. OPENSSL 生成https 客户端证书

    下面说下拿服务器证书.(前提是服务器是https,客户端认证用的时候),服务端不给的时候,我们自己去拿(不给怼他!,哈哈,开个玩笑,都会给的) openssl s_client -connect 域名 ...

  9. 使用 openssl 生成证书

    一.openssl 简介 目前最流行的 SSL 密码库工具官网:https://www.openssl.org/source/ 构成部分 密码算法库 密钥和证书封装管理功能 SSL通信API接口 用途 ...

随机推荐

  1. 制作Windows10政府版的小白教程

    制作Windows10政府版的小白教程 https://03k.org/make10entg.html 首先,宿主系统要比操作的系统新,因为低版本dism操作不了: 当然也可以单独下载ADK,提取最新 ...

  2. [NOIP2017 TG D2T2]宝藏

    题目大意:给定一个有重边,边有权值的无向图.从某一个点出发,求到达所有的点需要的最少费用,并且限制两点之间只有一条路径.费用的计算公式为:所有边的费用之和.而边$x->y$的费用就为:$y$到初 ...

  3. Adore 解题报告

    Adore 问题描述 小\(\text{w}\) 偶然间得到了\(1\)个 \(DAG\). 这个 \(DAG\) 有 \(m\) 层,第\(1\)层只有\(1\)个源点,最后\(1\)层只有\(1\ ...

  4. 遇到问题---java---myeclipse中maven项目引用另一个导致的resource文件混乱的问题

    遇到情况 情况是这样的,我们在构建项目时,经常会把一些公用的类和配置提取出去,作为一个公共项目.然后把公共项目作为一个jar包构件引入我们当前的项目中. 引入方式是 <dependency> ...

  5. nfs 和samba

    NFS,是Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS. NFS允许一个系统在网络上与他人共享目录和文件.通过使用N ...

  6. pmap用法小计

    By francis_hao    Aug 4,2017   pmap-报告进程的内存映射.   概要 pmap [options] pid [...]   描述 pmap命令用来报告一个或多个进程的 ...

  7. NOIP2017宝藏 [搜索/状压dp]

    NOIP2017 宝藏 题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 n 个深埋在地下的宝藏屋, 也给出了这 n 个宝藏屋之间可供开发的 m 条道路和它们的长度. 小明决心亲自前往挖掘 ...

  8. 安卓和html的互相调用

    1.写html和安卓布局 <Button android:id="@+id/btn" android:layout_width="wrap_content" ...

  9. tomcat work目录

    1    用tomcat作web服务器的时候,部署的程序在webApps下,这些程序都是编译后的程序(发布到tomcat的项目里含的类,会被编译成.class后才发布过来,源文件没有发布过来,但这里的 ...

  10. html与地图

    html:<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <meta ...