openssl建立私有CA和申请证书

实验环境:

虚拟机:VMware® Workstation 12 Pro
主机A:ip为10.1.255.55/16,创建CA并给其他主机提供CA服务
主机B:为httpd服务器,ip为10.1.249.115/16

1、查看openssl的配置文件/etc/pki/tls/openssl.cnf

[root@localhost ~]# cat /etc/pki/tls/openssl.cnf (查看配置文件的ca部分的内容)

......

[ ca ]
default_ca      = CA_default            # The default ca section

####################################################################
[ CA_default ]

dir             = /etc/pki/CA           # Where everything is kept
certs           = $dir/certs            # Where the issued certs are kept
crl_dir         = $dir/crl              # Where the issued crl are kept
database        = $dir/index.txt        # database index file.
#unique_subject = no                    # Set to 'no' to allow creation of
new_certs_dir   = $dir/newcerts         # default place for new certs.

certificate     = $dir/cacert.pem       # The CA certificate
serial          = $dir/serial           # The current serial number
crlnumber       = $dir/crlnumber        # the current crl number
crl             = $dir/crl.pem          # The current CRL
private_key     = $dir/private/cakey.pem# The private key
RANDFILE        = $dir/private/.rand    # private random number file

x509_extensions = usr_cert              # The extentions to add to the cert

# 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

# Extension copying option: use with caution.
# copy_extensions = copy

# Extensions to add to a CRL. Note: Netscape communicator chokes on V2 CRLs
# so this is commented out by default to leave a V1 CRL.
# crlnumber must also be commented out to leave a V1 CRL.
# crl_extensions        = crl_ext

default_days    = 365                   # how long to certify for
default_crl_days= 30                    # how long before next CRL
default_md      = sha256                # use SHA-256 by default
preserve        = no                    # keep passed DN ordering

# A few difference way of specifying how similar the request should look
# For type CA, the listed attributes must be the same, and the optional
# and supplied fields are just that :-)
policy          = policy_match

# For the CA policy
[ policy_match ]
countryName             = match
stateOrProvinceName     = match
organizationName        = match
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

......

2、根据配置文件创建所需要的文件

[root@localhost ~]# touch /etc/pki/CA/index.txt
[root@localhost ~]# echo 01 > /etc/pki/CA/serial
[root@localhost ~]# ls /etc/pki/CA/
certs  crl  index.txt  newcerts  private  serial

注意:文件名要与配置文件中的名字一样

3、在主机A上创建CA服务,并自签

(1)生成私钥

[root@localhost ~]# (umask 077 ; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
......................................................................................................................................................................................+++
....................................................................................................................+++
e is 65537 (0x10001)

用小括号说明是在子shell中执行括号内的命令,不会影响父shell的设置;将umask设置为077是为了防止其他人有权限查看和修改生成的私钥;在2048前还可以给私钥加上加密算法,比如3des rsa等,此例子不加密。

(2)生成自签证书

[root@localhost ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn 
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:haidian  
Organization Name (eg, company) [Default Company Ltd]:linuxca.org       
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:linuxCA  
Email Address []:admin@linuxca.org

-new : 生成新证书签署请求
-x509 : 专用于CA生成自签证书
-key : 生成请求时用到的私钥文件
-days: 证书的有效期限
-out /PATH/TO/SOMECERTFILE : 证书的保存路径

[root@localhost ~]# ls /etc/pki/CA/
cacert.pem  certs  crl  index.txt  newcerts  private  serial
[root@localhost ~]# cat /etc/pki/CA/cacert.pem 
-----BEGIN CERTIFICATE-----
MIID5zCCAs+gAwIBAgIJAJrY1Gr0+l+fMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD
VQQGEwJjbjEQMA4GA1UECAwHYmVpamluZzEQMA4GA1UEBwwHaGFpZGlhbjEUMBIG
A1UECgwLbGludXhjYS5vcmcxDDAKBgNVBAsMA29wczEQMA4GA1UEAwwHbGludXhD
QTEgMB4GCSqGSIb3DQEJARYRYWRtaW5AbGludXhjYS5vcmcwHhcNMTYwOTIzMDEw
NTE5WhcNMzY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pfP+j6uTVF3Cd5TEpxKuRxQ==
-----END CERTIFICATE-----

4、在需要使用证书的主机(B)生成证书请求

(1)给httpd服务器生成私钥

[root@localhost ~]# mkdir /etc/httpd/ssl
[root@localhost ~]# (umask 077 ; openssl genrsa -out /etc/httpd/ssl/httpd.key 2048)
Generating RSA private key, 2048 bit long modulus
...........+++
...............+++
e is 65537 (0x10001)

(2)生成证书申请文件

[root@localhost ~]# oepnssl req -new -key /etc/httpd/ssl/httpd.key -days 365 -out /etc/httpd/ssl/httpd.csr
-bash: oepnssl: command not found
[root@localhost ~]# openssl req -new -key /etc/httpd/ssl/httpd.key -days 365 -out /etc/httpd/ssl/httpd.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:chaoyang
Organization Name (eg, company) [Default Company Ltd]:linuxca.org
Organizational Unit Name (eg, section) []:cwb  
Common Name (eg, your name or your server's hostname) []:lovelinux
Email Address []:lovelinux@163.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:   此处不加密证书申请文件  
An optional company name []:

注意:默认国家,省,公司名称必须和CA一致

(3)将证书请求文件传输给CA

[root@localhost ~]# ls /etc/httpd/ssl/httpd.
httpd.csr  httpd.key  
[root@localhost ~]# scp /etc/httpd/ssl/httpd.csr 10.1.252.55:/testdir/
root@10.1.252.55's password: 
httpd.csr                                   100% 1054     1.0KB/s   00:00

5、在主机A上签署证书,并颁发给请求者

(1)签署证书

[root@localhost testdir]# openssl ca -in /testdir/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Sep 23 01:31:29 2016 GMT
            Not After : Sep 23 01:31:29 2017 GMT
        Subject:
            countryName               = cn
            stateOrProvinceName       = beijing
            organizationName          = linuxca.org
            organizationalUnitName    = cwb
            commonName                = lovelinux
            emailAddress              = lovelinux@163.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                D0:73:48:EE:41:62:D5:61:30:16:09:8D:9B:04:BD:5B:B3:5F:FD:1D
            X509v3 Authority Key Identifier: 
                keyid:1F:16:80:FF:36:CA:A4:33:A2:77:DF:ED:AF:DD:D6:CB:AE:AE:88:5B

Certificate is to be certified until Sep 23 01:31:29 2017 GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n] y
Write out database with 1 new entries
Data Base Updated

(2)颁发证书

[root@localhost testdir]# ls /etc/pki/CA/certs/
httpd.crt
[root@localhost testdir]# scp /etc/pki/CA/certs/httpd.crt 10.1.249.115:/etc/httpd/ssl
root@10.1.249.115's password: 
httpd.crt                                   100% 4613     4.5KB/s   00:00

至此,CA的创建和申请实验已完成。

原创文章,作者:pao,如若转载,请注明出处:http://www.178linux.com/48639

(0)
paopao
上一篇 2016-09-23
下一篇 2016-09-23

相关推荐

  • 第一周博客作业-Linux发行版历史、哲学思想、FHS、命令格式、帮助等

    All + All – 第一周作业 1.计算机组成及功能 计算机组成 硬件 冯诺依曼体系 CPU 运算器 对数据做算暖苏运算和逻辑运算 控制器 寄存器等 程序的运行,是CPU通过调用硬盘中的程序至内存中执行来实现的 存储器 内存 ROM RAM 外存 硬盘 光盘 输入设备 鼠标、麦克风等 实现指令和参数的输入 输出设备 显示器、打印机等 实现结果…

    Linux干货 2017-07-02
  • N25-第八周作业

    第八周 1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态; 在线的主机使用绿色显示; 不在线的主使用红色显示; #!/bin/bash # ip=172.16.250. for i in {1..254}; do if ping -w 1 -c 1 $ip$i &> /dev/n…

    Linux干货 2017-03-09
  • 文件相关命令

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 cp:copy 源文件:目标文件 单源复制:cp [OPTION]… [-T] SOURCE DEST 如果DEST不存在,则事先创建此文件,并复制源文件的数据流至DEST中 如果DEST存在: 如果DEST是非目录文件:则覆盖目标文件 如果DEST是目录文件:则先在D…

    Linux干货 2016-09-23
  • 通用二进制安装MySQL(MariaDB)

    一、前言     MySQL是一个关系型数据库管理系统,是最流行的关系型数据库管理系统,由于其体积小、速度快、总体拥有成本低,并且之前是完全开源,所以大受欢迎。但由于后面MySQL卖给了SUN,随后SUN被Oracle收购,虽然也有开源免费版本,但是很多功能都需要另外购买商业版本,导致现在MySQL使用份额逐渐减少。所…

    Linux干货 2015-10-15
  • Yacc 与 Lex 快速入门(词法分析和语法分析)

    Lex 代表 Lexical Analyzar。Yacc 代表 Yet Another Compiler Compiler。 让我们从 Lex 开始吧。 Lex Lex 是一种生成扫描器的工具。扫描器是一种识别文本中的词汇模式的程序。 这些词汇模式(或者常规表达式)在一种特殊的句子结构中定义,这个我们一会儿就要讨论。 一种匹配的常规表达式可能会包含相关的动作…

    Linux干货 2015-06-08
  • ☞实时数据同步方案{ rsync; inotify; }

    实时数据同步方案{ rsync; inotify; } rsync 传输数据 安装 rsyncd 服务 CentOS 6 CentOS 7 配置 rsyncd 服务 配置 rsync 手动同步示例     启动 rsyncd 服务 CentOS 6 CentOS 7 rsync 客户端命令 rsync 数据传输功能 基…

    Linux干货 2016-10-29

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-23 11:38

    自建CA与颁发证书过程很完整?想不想挑战一下自己,写脚本来实现这些步骤呢?