### 目录
概念:
PKI、CA、X.509
PKI: Public Key Infrastructure
公开密钥基础建设: 是一组由硬件、软件、参与者、管理政策与流程组成的基础架构,其目的在于创造、管理、分配、使用、存储以及撤销数字证书。
X.509: 是由ITU-T为了公开密钥基础建设(PKI)与授权管理基础建设(PMI)提出的产业标准。X.509标准,规范了公开密钥认证、证书吊销列表、授权证书、证书路径验证算法等。
CA: 数字证书认证机构(英文全称:Catificate Authority)。主要负责:证书发放、证书更新、证书撤销和证书验证。
证书获取
获取证书两种方法:
使用证书授权机构
- 生成签名请求(csr
)
- 将csr
发送给CA
- 从CA
处接收签名
自签名的证书
- 自已签发自己的公钥
安全协议
SSL和TLS协议
SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议。TLS与SSL在传输层对网络连接进行加密。
Handshake
协议:包括协商安全参数和密码套件、服务器身份认证(客户端身份认证可选)、密钥交换ChangeCipherSpec
协议:一条消息表明握手协议已经完成Alert
协议:对握手协议中一些异常的错误提醒,分为fatal``和``warning``两个级别,
fatal类型错误会直接中断
SSL链接,而
warning级别的错误
SSL`链接仍可继续,只是会给出错误警告Record
协议:包括对消息的分段、压缩、消息认证和完整性保护、加密等HTTPS
协议:就是“HTTP 协议”
和“SSL/TLS 协议
”的组合。HTTP over SSL”
或“HTTP over TLS”
,对http
协议的文本数据进行加密处理后,成为二进制形式传输
OpenSSL
OpenSSL:开源项目
OpenSSL
是一个安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL
协议,并提供丰富的应用程序供测试或其它目的使用。
三个组件:
openssl
: 多用途的命令行工具,包openssl
libcrypto
: 加密算法库,包openssl-libs
libssl
:加密模块应用库,实现了ssl
及tls
,包nss
OpenSSL命令:
- 两种运行模式:交互模式和批处理模式
openssl version
:查看OpenSSL
程序版本号- 标准命令:enc
(对称加密),
ca(签署证书),
req`(生成自签名证书), …
对称加密enc:
- 工具:
openssl enc
,gpg
- 算法:
3des
,aes
,blowfish
,twofish
enc
命令: - 帮助:
man enc
加密:
openssl enc -e -des3 -a -salt -in testfile -out testfile.cipher
[root@centos7 app]# openssl enc -e -des3 -a -salt -in testfile -out testfile.cipher #<==使用des3算法对testfile文件加密,输出为testfile.cipher enter des-ede3-cbc encryption password: #<==输入密码 Verifying - enter des-ede3-cbc encryption password: #<==再次输入密码 [root@centos7 app]# cat testfile testfile.cipher #<==查看加密和未加密的文件 this is a test file U2FsdGVkX1/a0QHbk2ol6aB39zpO7+yS2cZ+jI7YqUQiKfGhC4SqZg==
解密:
openssl enc -d -des3 -a -salt –in testfile.cipher-out testfile
[root@centos7 app]# rm -f testfile #<==删除testfile文件 [root@centos7 app]# openssl enc -d -des3 -a -salt -in testfile.cipher -out testfile #<==使用des3算法对testfile.cipher文件 enter des-ede3-cbc decryption password: #<==输入密码 [root@centos7 app]# ls testfile testfile.cipher [root@centos7 app]# cat testfile #<==查看testfile内容和原来是一样的。 this is a test file
单向加密dgst
单向加密:
工具:
md5sum
,sha1sum
,sha224sum
,sha256sum
openssl dgst…
dgst
命令:
man dgst`
帮助:
openssl dgst -md5 [-hex默认] /PATH/SOMEFILE
openssl dgst -md5 testfile
md5sum /PATH/TO/SOMEFILE
[root@centos7 app]# openssl dgst -md5 testfile #<==计算testfile的md5 MD5(testfile)= 4221d002ceb5d3c9e9137e495ceaa647 [root@centos7 app]# openssl dgst -md5 -hex testfile #<==-hex是转换为16进制(默认) MD5(testfile)= 4221d002ceb5d3c9e9137e495ceaa647 [root@centos7 app]# md5sum testfile #<==计算testfile的md5 4221d002ceb5d3c9e9137e495ceaa647 testfile
生成用户密码和随机数:
生成用户密码:
passwd
命令:帮助:
man sslpasswd
openssl passwd -1 -salt SALT
(最多8位)openssl passwd -1 –salt centos
[root@centos7 app]# openssl passwd -1 -salt 12345678 #<==生成openssl对passwd密码加密并加盐 Password: $1$12345678$tRy4cXc3kmcfRZVj4iFXr/ [root@centos7 app]# openssl passwd -1 -salt 123456789 #<==注:盐最多8位,如果输入9位是不显示的 Password: $1$12345678$tRy4cXc3kmcfRZVj4iFXr/
生成随机数:
帮助:
man sslrand
openssl rand -base64|-hex NUM
NUM
: 表示字节数;hex
时,每个字符为十六进制,相当于4位二进制,出现的字符数为NUM*2
[root@centos7 app]# openssl rand -hex 16 #<==生成的是32位的随机数 5666c7c5f5369120f0c8c3254fa82abe [root@centos7 app]# openssl rand -base64 16 #<==生成的是16位的随机数 EHpDSxUBcX/uCvYBfhW61w== [root@centos7 app]# openssl rand -base64 -hex 16 #<==-hex和-base64是不能放在一块使用的 Usage: rand [options] num
生成密码对儿:man genresa
生成私钥:
- openssl genrsa -out /PATH/TO/PRIVATEKEY.FILE NUM_BITS
- (umask 077; openssl genrsa –out test.key –des 2048)
从私钥中提取出公钥
- openssl rsa -in PRIVATEKEYFILE –pubout –out PUBLICKEYFILE
Openssl rsa –in test.key –pubout –out test.key.pub
[root@centos7 app]# (umask 077 ; openssl genrsa -out test.key -des3 2048) #<==生成私钥,其权限为600 Generating RSA private key, 2048 bit long modulus ................................+++ .........+++ e is 65537 (0x10001) Enter pass phrase for test.key: Verifying - Enter pass phrase for test.key: [root@centos7 app]# openssl rsa -in test.key -pubout -out test.key.pub #<==从私钥中,提取出公钥 Enter pass phrase for test.key: writing RSA key [root@centos7 app]# ll #<==查看生成的私钥和提取出来的公钥 total 8 -rw-------. 1 root root 1751 Sep 9 21:29 test.key -rw-r--r--. 1 root root 451 Sep 9 21:30 test.key.pub
随机数生成器:伪随机数字
键盘和鼠标
块设备中断:cp一个大文件可以生成很多随机数
/dev/random
:仅从熵池返回随机数;随机数用尽,阻塞/dev/urandom
:从熵池返回随机数;随机数用尽,会利用软件生成伪随机数,非阻塞[root@centos6 script]# head /dev/urandom | cksum #<==通过/dev/urandom配合cksum生成随机数 1712568099 3350 [root@centos6 script]# head /dev/urandom | cksum 1558657086 1998
建立私有CA
openssl
的配置文件:/etc/pki/tls/openssl.cnf
- 三种策略: 匹配、支持和可选
- 匹配指要求申请填写的信息跟
CA
设置信息必须一致, 支持指必须填写这项申请信息, 可选指可有可无
1. 创建所需要的文件
touch /etc/pki/CA/index.txt
生成证书索引数据库文件
echo 01 > /etc/pki/CA/serial
指定第一个颁发证书的序列号
2. CA自签证书
生成私钥
cd /etc/pki/CA/
(umask 066; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
3. 生成自签名证书
openssl req -new -x509 –key
/etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem
-new
: 生成新证书签署请求
-x509
: 专用于CA
生成自签证书
-key
: 生成请求时用到的私钥文件
-days n
:证书的有效期限
-out /PATH/TO/SOMECERTFILE
: 证书的保存路径
4. 生成证书的各字段详解:
DN字段名 | 缩写 | 说明 | 填写要求 |
---|---|---|---|
Country Name | C | 证书持有者所在国家 | 要求填写国家代码,用2个字母表示 |
State or Province Name | ST | 证书持有者所在州或省份 | 填写全称,可省略不填 |
Locality Name | L | 证书持有者所在城市 | 可省略不填 |
Organization Name | O | 证书持有者所属组织或公司 | 最好还是填一下 |
Organizational Unit Name | OU | 证书持有者所属部门 | 可省略不填 |
Common Name | CN | 证书持有者的通用名 | 必填。对于非应用证书,它应该在一定程度上具有惟一性;对于应用证书,一般填写服务器域名或通配符样式的域名。 |
Email Address | 证书持有者的通信邮箱 | 可省略不填 |
[root@centos7 ~]# touch /etc/pki/CA/index.txt #<==生成证书索引数据库文件
[root@centos7 ~]# echo 01 > /etc/pki/CA/serial #<==指定第一个颁发证书的序列号
[root@centos7 ~]# cd /etc/pki/CA/
[root@centos7 CA]# (umask 066 ; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048) #<== 生成私钥
Generating RSA private key, 2048 bit long modulus
.................................+++
......................................+++
e is 65537 (0x10001)
[root@centos7 CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem #<==生成自签名证书
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]:iav18.com
Organizational Unit Name (eg, section) []:opt
Common Name (eg, your name or your server's hostname) []:ca.iav18.com
Email Address []:
证书申请过程:
证书申请及签署步骤:
- 生成申请请求
RA
核验CA
签署- 获取证书
创建CA和申请证书
1. 在需要使用证书的主机生成证书请求
给web
服务器生成私钥
(umask 066; openssl genrsa -out /etc/pki/tls/private/test.key 2048)
生成证书申请文件
openssl req -new -key /etc/pki/tls/private/test.key -days 365 -out etc/pki/tls/test.csr
将证书请求文件传输给CA
[root@centos6 ~]# (umask 066 ; openssl genrsa -out /etc/pki/tls/private/test.key 2048) #<== 生成私钥
Generating RSA private key, 2048 bit long modulus
...............................................+++
..................................................................................................................................+++
e is 65537 (0x10001)
[root@centos6 ~]# openssl req -new -key /etc/pki/tls/private/test.key -days 365 -out /etc/pki/tls/test.csr #<==生成证书申请文件
[root@centos6 ~]# openssl req -new -key /etc/pki/CA/private/test.key -out test.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.
-