加密解密基础、PKI以及自建私有CA

加密解密基础、PKI以及自建私有CA

加密是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容。

解密就是加密的逆向操作,将加密过的不可读的数据经过处理,能够是之前不可读的信息还原成可读信息。

PKI是Public Key Infrastructure的首字母缩写,翻译过来就是公钥基础设施;PKI是一种遵循标准的利用公钥加密技术为电子商务的开展提供一套安全基础平台的技术和规范。


加密、解密

计算机进程间的通信可以分为同一主机上的和不同主机上的进程通信;同一主机上的进程间通讯通过IPC、Message Queue(消息队列)、SHM(空闲内存)、Semerphor等4种;而不同主机将的进程通信则是通过Socket完成,其主要的形式为:ServerIP:prot<—->ClientIP:port。信息从一台主机的某个端口出来,到达目的主机后,将数据送达某端口代表的运行程序,完成主机间的信息传送。 
如果在传送的过程中,信息被截获,那么数据在没有进行任何处理的情况下,截获信息的人就能很容易的指导传送的信息内容。这样就造成了信息泄露。为了不让这样的事情发生,于是就有了将数据在处送前进行处理,使得没有得到授权的人即使获取了处理后的信息,也没有办法将从中获得有用信息,这就是加密;只有被授权的人才能通过某种处理方式将处理过的信息二次处理,还原原信息,从而能够获取原有信息,这个过程称之为解密。 
随着互联网的发展,信息安全也被人越来越重视。因此,加密技术得到的很好的发展。加密方式可以分为对称加密、非对称加密、单向加密、认证协议等。

对称加密

即加密和解密使用同一个密钥,将数据块分割成固定大小的块逐个进行加密。常用的算法有:DES(Date Encryption Standard)、3DES(Triple DES)、AES(Advanced)、Blowfish、Twofish、IDEA, RC6, CAST5等; 
特性: 
1、加密、解密使用同一个密钥,效率高 
2、将原始数据分割成固定大小的块,逐个进行加密 
缺陷: 
1、 密钥过多 
2、密钥分发 
3、数据来源无法确认

非对称加密

与对称加密算法不同,非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey)。公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密;如果用私有密钥对数据进行加密,那么只有用对应的公开密钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。 
常见的非对称加密算法有:RSA(加密、签名)、Diffie-Hellman(DH)、El Gamal、DSA(只能数字签名用)。其中DH算法常用与密钥交换,其实现过程为: 
第一步:双方协商两个公开的数:整数a以及一个大素数P; 
第二步:一方生成隐私数m,计算a^m%p得到隐私数据发给另一方;另一方同样的生成隐私数n,从而生成隐私数据a^n%p发给对方; 
第三步:双方在得到对方发的隐私数据后,通过对方的隐私数据和自己的隐私数算出密钥:即(a^m%p)^n和(a^n%p)^m得到相同的密钥a^mn%p。 
使用非对称加密,公钥是任何人都可以得到的,而密钥只能使用者一个人使用,必须保证其私密性,若果泄露,则必须马上更换密钥对。 
特性: 
数字签名:主要在于让接收方确认发送方身份 
对称密钥交换:发送方用对方的公钥加密一个对称密钥后发送给 
对方 
数据加密:适合加密较小数据 
缺陷: 
密码长,加密解密效率低下

单向加密

不管加密的数据多长都会定长输出;不可逆转即无法将得到的数据在逆着解密;雪崩效应即数据微小的变化就会导致结果的巨大变化; 
常用的算法有:MD5、 SHA1 、CRC-32。

加密过程

3种加密算法各有优势,但是在现今复杂的网络环境都无法保证数据在传输过程中的安全。因此,在现今的网络数据传输中常常根据各算法的优势,在对不同的数据加密上使用不同的算法。以小明和小红在网络上通信为例: 
小明和小红在建立其链接以后,向小红发起数据传送请求,小红在收到请求以后,回应小明,双方一起协商,双方交换彼此的公钥,协商加密的算法。在完成协商以后,小明就开始将要传送的数据加密: 
第一步:用单向加密算法将要传输的数据提取特征码; 
第二步:将提取出的特征码,用小明的私钥加密; 
第三步:用协商好的对称加密协议对要传送的数据和已经加密后的特征码加密; 
第四步:用小红的公钥将第三步的密钥进行加密附加到第三步加密好的数据的后面; 
完成上面四步后,小明就将数据通过网络向小红发送。小红在拿到数据后经过以下步骤解密: 
第一步:小红用私钥解密,拿到加密数据的密钥; 
第二步:然后用拿到的密钥解密,拿到数据和加密后的特征码; 
第三步:用小明的公钥解密加密后的特征码,从而验证数据的来源; 
第四部:在获得数据特征码后,在通过相同的算法对数据进行单向加密,提取特征码进行对比,验证数据的完整性和正确性。 
上述的过程,在双方确定对方的身份没有问题后,传出数据的安全得到了很大的保障;但是,在传送数据前,如何保证对方的身份没有问题?这就需要一个权威的公证机构,即CA。

PKI

PKI(Public Key Infrastructure)公钥基础设施是提供公钥加密和数字签名服务的系统或平台,目的是为了管理密钥和证书。一个机构通过采用PKI 框架管理密钥和证书可以建立一个安全的网络环境。PKI 主要包括四个部分:X.509 格式的证书(X.509 V3)和证书废止列表CRL(X.509 V2);CA 操作协议;CA 管理协议;CA 政策制定。一个典型、完整、有效的PKI 应用系统至少应具有以下五个部分; 
  1) 认证中心CA CA 是PKI 的核心,CA 负责管理PKI 结构下的所有用户(包括各种应用程序)的证书,把用户的公钥和用户的其他信息捆绑在一起,在网上验证用户的身份,CA 还要负责用户证书的黑名单登记和黑名单发布,后面有CA 的详细描述。 
  2) X.500 目录服务器 X.500 目录服务器用于发布用户的证书和黑名单信息,用户可通过标准的LDAP 协议查询自己或其他人的证书和下载黑名单信息。 
  3) 具有高强度密码算法(SSL)的安全WWW服务器 Secure socket layer(SSL)协议最初由Netscape 企业发展,现已成为网络用来鉴别网站和网页浏览者身份,以及在浏览器使用者及网页服务器之间进行加密通讯的全球化标准。 
  4) Web(安全通信平台) Web 有Web Client 端和Web Server 端两部分,分别安装在客户端和服务器端,通过具有高强度密码算法的SSL 协议保证客户端和服务器端数据的机密性、完整性、身份验证。 
  5) 自开发安全应用系统 自开发安全应用系统是指各行业自开发的各种具体应用系统,例如银行、证券的应用系统等。完整的PKI 包括认证政策的制定(包括遵循的技术标准、各CA 之间的上下级或同级关系、安全策略、安全程度、服务对象、管理原则和框架等)、认证规则、运作制度的制定、所涉及的各方法律关系内容以及技术的实现等。 
完整的PKI系统必须具有权威认证机构(CA)、数字证书库、密钥备份及恢复系统、证书作废系统、应用接口(API)等基本构成部分,构建PKI也将围绕着这五大系统来着手构建。 
PKI的基础技术包括加密、数字签名、数据完整性机制、数字信封、双重数字签名等。一个典型、完整、有效的PKI应用系统至少应具有以下部分: 
· 公钥密码证书管理。 
· 黑名单的发布和管理。 
· 密钥的备份和恢复。 
· 自动更新密钥。 
· 自动管理历史密钥。 
· 支持交叉认证。 
认证机构(CA):即数字证书的申请及签发机关,CA必须具备权威性的特征;

CA

认证中心CA 作为PKI 的核心部分,CA 实现了PKI 中一些很重要的功能,概括地说,认证中心(CA)的功能有:证书发放、证书更新、证书撤销和证书验证。CA 的核心功能就是发放和管理数字证书,具体描述如下: 
  (1)接收验证最终用户数字证书的申请。 
  (2)确定是否接受最终用户数字证书的申请-证书的审批。 
  (3)向申请者颁发、拒绝颁发数字证书-证书的发放。 
  (4)接收、处理最终用户的数字证书更新请求-证书的更新。 
  (5)接收最终用户数字证书的查询、撤销。 
  (6)产生和发布证书废止列表(CRL)。 
  (7)数字证书的归档。 
  (8)密钥归档。 
  (9)历史数据归档。 
  认证中心CA 为了实现其功能,主要由以下三部分组成: 
  注册服务器:通过 Web Server 建立的站点,可为客户提供24×7 不间断的服务。客户在网上提出证书申请和填写相应的证书申请表。 
  证书申请受理和审核机构:负责证书的申请和审核。它的主要功能是接受客户证书申请并进行审核。 
  认证中心服务器:是数字证书生成、发放的运行实体,同时提供发放证书的管理、证书废止列表(CRL)的生成和处理等服务。 
  在具体实施时,CA 的必须做到以下几点: 
  1) 验证并标识证书申请者的身份。 
  2) 确保CA 用于签名证书的非对称密钥的质量。 
  3) 确保整个签证过程的安全性,确保签名私钥的安全性。 
  4) 证书资料信息(包括公钥证书序列号,CA 标识等)的管理。 
  5) 确定并检查证书的有效期限。 
  6) 确保证书主体标识的唯一性,防止重名。 
  7) 发布并维护作废证书列表。 
  8) 对整个证书签发过程做日志记录。 
  9) 向申请人发出通知。 
  在这其中最重要的是CA 自己的一对密钥的管理,它必须确保其高度的机密性,防止他方伪造证书。CA 的公钥在网上公开,因此整个网络系统必须保证完整性。CA 的数字签名保证了证书(实质是持有者的公钥)的合法性和权威性。 
  用户的公钥有两种产生的方式: 
  (1)用户自己生成密钥队,然后将公钥以安全的方式传送给CA,该过程必须保证用户公钥的验证性和完整性。 
  (2)CA 替用户生成密钥队,然后将其以安全的方式传送给用户,该过程必须确保密钥对的机密性,完整性和可验证性。该方式下由于用户的私钥为CA 所产生,所以对CA 的可信性有更高的要求。CA 必须在事后销毁用户的私钥。 
  一般而言公钥有两大类用途,一个是用于验证数字签名,一个是用于加密信息。相应的在CA 系统中也需要配置用于数字签名/验证签名的密钥对和用于数据加密/脱密的密钥对,分别称为签名密钥对和加密密钥对。由于两种密钥对的功能不同,管理起来也不大相同,所以在CA 中为一个用户配置两对密钥,两张证书。

CA的作用

回到小明和小红通信的例子,在小明和小红都拿到对方的公钥后,不会轻易的信任拿到的公钥就是对方真正的公钥,这时候,双方都会验证对方公钥: 
首先,用签发对方公钥的机构即CA的公钥解密对方证书(CA签发的权威的认证),得到CA的签名,验证证书的来源合法性; 
然后,用同样的单向加密方式加密证书,得到证书的特则码,两者相比较,验证证书的完整性; 
再然后分别检查验证的有效期,验证是否过期,同时验证证书的名字和通信的对象是否一致;最后验证证书是否被吊销。 
所有验证都通过以后,才会有数据的加密通信过程。

blob.png

证书类型: 
证书授权机构的证书 
服务器 
用户证书 
获取证书两种方法: 
使用证书授权机构 
生成签名请求( csr) 
将csr发送给CA 
从CA处接收签名 
自签名的证书 
自已签发自己的公钥

SSL/TLS协议

SSL即Secure Socket Layer,安全套接字层 
TLS即Transport Layer Security,传输层安全 
SSL/TLS工作在TCP/IP模型的传输层和因特网层之间,如下图:

 blob.png

分为两层:HandShake LayerRecord Layer
Handshake协议:包括协商安全参数和密码套件、服务器身份认证(客户端身份认证可选)、密钥交换: 
ChangeCipherSpec 协议:一条消息表明握手协议已经完成; 
Alert 协议:对握手协议中一些异常的错误提醒,分为fatal和warning两个级别, fatal类型错误会直接中断SSL链接,而warning级别的错误SSL链接仍可继续,只是会给出错误警告; 
Record 协议:包括对消息的分段、压缩、消息认证和完整性保护、加密等;

基于SSl/TLS协议通信的实现过程

可以通过一幅图来表示其通信的实现过程:

 blob.png

第一步:客户端向服务端发起握手请求,给出协议版本号、一个生成的随机数(Client random),以及客户端支持的加密方法。 
第二步,服务端确认双方使用的加密方法,并给出数字证书、以及一个服务端生成的随机数(Server random)。 
第三步,客户端确认数字证书有效,然后生成一个新的随机数(Premaster secret),并使用数字证书中的公钥,加密这个随机数,发给服务端。 
第四步,服务端使用自己的私钥,获取客户端发来的随机数(即Premaster secret)。 
第五步,双方根据约定的加密方法,使用前面的三个随机数,生成”对话密钥”(session key),用来加密接下来的整个对话过程。

自建私有CA

OpenSSL

OpenSSL是一个开放源代码的软件库包,应用程序可以使用这个包来进行安全通信,避免窃听,同时确认另一端连接者的身份。这个包广泛被应用在互联网的网页服务器上。实现了基本的加密功能,实现了SSL与TLS协议。 
OpenSSL由三部分组成:libssl库、libcrypto库以及openssl命令行工具,重点在于openssl工具的使用; 
openssl又分为标准命令、信息摘要命令和加密命令:

[root:~]#    openssl -l
openssl:Error: '-l' is an invalid command.

Standard commands
asn1parse         ca                ciphers           cms
crl               crl2pkcs7         dgst              dh
dhparam           dsa               dsaparam          ec
ecparam           enc               engine            errstr
gendh             gendsa            genpkey           genrsa
nseq              ocsp              passwd            pkcs12
pkcs7             pkcs8             pkey              pkeyparam
pkeyutl           prime             rand              req
rsa               rsautl            s_client          s_server
s_time            sess_id           smime             speed
spkac             ts                verify            version
x509

Message Digest commands (see the `dgst' command for more details)
md2               md4               md5               rmd160
sha               sha1

Cipher commands (see the `enc'
command for more details)
aes-128-cbc       aes-128-ecb       aes-192-cbc       aes-192-ecb
aes-256-cbc       aes-256-ecb       base64            bf
bf-cbc            bf-cfb            bf-ecb            bf-ofb
camellia-128-cbc  camellia-128-ecb  camellia-192-cbc  camellia-192-ecb
camellia-256-cbc  camellia-256-ecb  cast              cast-cbc
cast5-cbc         cast5-cfb         cast5-ecb         cast5-ofb
des               des-cbc           des-cfb           des-ecb
des-ede           des-ede-cbc       des-ede-cfb       des-ede-ofb
des-ede3          des-ede3-cbc      des-ede3-cfb      des-ede3-ofb
des-ofb           des3              desx              idea
idea-cbc          idea-cfb          idea-ecb          idea-ofb
rc2               rc2-40-cbc        rc2-64-cbc        rc2-cbc
rc2-cfb           rc2-ecb           rc2-ofb           rc4
rc4-40            seed              seed-cbc          seed-cfb
seed-ecb          seed-ofb          zlib

使用openssl工具可以实现对称加密、非对称加密和单向加密

对称加密

对称加密使用命令openssl enc,使用方法如下:

###加密过程
[root:~]#    openssl enc -e -des3 -a -in functions -out functions.ciphers
enter des-ede3-cbc encryption password:
Verifying - enter des-ede3-cbc encryption password:
[root:~]#    ll functions functions.ciphers
-rw-r--r-- 1 root root 13948 Oct 26 14:40 functions
-rw-r--r-- 1 root root 18915 Dec  4 14:19 functions.ciphers
###解密过程
[root:~]#    openssl enc -d -des3 -a -in functions.ciphers -out functions.pl
enter des-ede3-cbc decryption password:
[root:~]#    ll functions functions.pl
-rw-r--r-- 1 root root 13948 Oct 26 14:40 functions
-rw-r--r-- 1 root root 13948 Dec  4 14:22 functions.pl
[root:~]#    diff functions functions.
functions.ciphers  functions.pl
[root:~]#    diff functions functions.pl
[root:~]#

-e:加密; 
-d:解密; 
-des3:加密算法 
-a:Base64编码 
-in:后面跟被加密/解密的文件 
-out:后面输入文件名,解密/加密后的内容保存的文件 
单向加密使用命令openssl dgst,使用方法如下:

##默认使用md5算法
[root:~]#    openssl dgst functions
MD5(functions)= 7ef254bfccd88ec4c113d0a1bfaede89
###指定其他的加密算法
[root:~]#    openssl dgst -sha512 functions
SHA512(functions)= 75dd013dba7182aecbaa28c4df31a00b359106b8db0088d0c3c938ba0fa1aeca6c8548993326cd3c38b2776fe38b09c8edb3c0e660de24d542cd927a75a7e6b1

单向加密

使用openssl单向加密,可以使用很多的加密算法:

-md4 to use the md4 message digest algorithm 
-md5 to use the md5 message digest algorithm 
-ripemd160 to use the ripemd160 message digest algorithm 
-sha to use the sha message digest algorithm 
-sha1 to use the sha1 message digest algorithm 
-sha224 to use the sha224 message digest algorithm 
-sha256 to use the sha256 message digest algorithm 
-sha384 to use the sha384 message digest algorithm 
-sha512 to use the sha512 message digest algorithm 
-whirlpool to use the whirlpool message digest algorithm

在Linux系统中,用户登录的密码也是单向算法生成的,同时,openssl工具也有专门为用户生成登录密码的工具:openssl passwd,可以使用帮助命令man sslpasswd获取相应的帮助,使用方法如下:

[root:~]#    openssl passwd -1 -salt 123
Password:
$1$123$Ok9FhQy4YioYZeBPwQgm3/

-1:指定的加密算法是md5,也可以不指定;不指定加密算法,默认使用md5; 
-salt 123:给密码加”盐“(最多8位),也可以不指定; 
提示输入密码后,下面的字符串就是生成的密码。

非对称加密

非对称加密需要一对钥匙,即公钥和私钥,要生成这对钥匙,可以使用openssl工具和gpg工具。不同的工具生成的密钥的使用也是不一样。一般来说,openssl工具生成的密钥,很多使用用于CA证书的申请,gpg工具用来加密解密文件。

使用openssl工具生成

生成私钥:

[root:~]#    openssl genrsa -out abc.pri   ##生成一个私钥文件abc.pri,没有加密的私钥文件
Generating RSA private key, 1024 bit long modulus  ##长度为1024位
......++++++
.......................++++++
e is 65537 (0x10001)
[root:~]#    ll abc.pri ;file abc.pri
-rw-r--r-- 1 root root 887 Dec  4 16:30 abc.pri
abc.pri: PEM RSA private key
###一下生成一个机密的指定位数的机密私钥文件
[root:~]#    openssl genrsa -out abc.pir1 -des3 2048 ##-des3指定的加密私钥文件的加密算法,一般使用”-des|-des3|-idea“中的一个
Generating RSA private key, 2048 bit long modulus  ##2048位的长度
.................................................+++
.....................+++
e is 65537 (0x10001)
Enter pass phrase for abc.pir1:
Verifying - Enter pass phrase for abc.pir1:
[root:~]#    file abc.pir1;ll abc.pir1
abc.pir1: PEM RSA private key
-rw-r--r-- 1 root root 1751 Dec  4 16:36 abc.pir1

注意:上面的命令生成的私钥文件的权限为644,很危险,为了保证安全,将私钥文件的权限设置为600。可以使用chmod命令改变权限,也可以在命令之前使用umask命令指定权限掩码后在执行私钥生成命令。在此过程中,值得注意的是,指定umask的时候只能影响后面生成私钥的命令,不能改变整个shell环境的umask,因此,在执行的时候,需要用小括号,指定在子shell中生效:

[root:~]#    ( umask 066;openssl genrsa -out abc.pir2 -des3 2048 )
Generating RSA private key, 2048 bit long modulus
..........................+++
.......................................................................................................................................................................................................................+++
e is 65537 (0x10001)
Enter pass phrase for abc.pir2:
Verifying - Enter pass phrase for abc.pir2:
[root:~]#    ll abc.pir2
-rw------- 1 root root 1751 Dec  4 16:55 abc.pir2
[root:~]#    umask
0022
gpg工具生成密钥对

与openssl工具相比,gpg工具生成密钥,一次性生成一对:

[root:~]#    gpg --gen-key
gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
  (1) RSA and RSA (default)
  (2) DSA and Elgamal
  (3) DSA (sign only)
  (4) RSA (sign only)
Your selection?
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)     ##钥匙大小:1024-4096
Requested keysize is 2048 bits
Please specify how long the key should be valid.
        0 = key does not expire
     <n>  = key expires in n days
     <n>w = key expires in n weeks
     <n>m = key expires in n months
     <n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
Is this correct? (y/N) y

GnuPG needs to construct a user ID to identify your key.

Real name: wangcai    ##输入钥匙的名字,至少5字符以上
Email address:
Comment:
You selected this USER-ID:
   "wangcai"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.  ##提示输入钥匙密码,以保护钥匙的安全,可以不输入
###  下面是产生随机数以填充密钥,在此过程中需要产生IO操作,最好就是敲键盘,移动鼠标或者让磁盘使劲读写
You don't want a passphrase - this is probably a *bad* idea!
                                                           I will do it anyway.  You can change your passphrase at any time,
                                                                                                                            using this program with the option "--edit-key".

   We need to generate a lot of random bytes. It is a good idea to perform
                                                                          some other action (type on the keyboard, move the mouse, utilize the
                                                                                                                                              disks) during the prime generation; this gives the random number
                                     generator a better chance to gain enough entropy.

上述操作完成后,可以使用命令查看密钥对:

[root:~]#    gpg --list-key
/root/.gnupg/pubring.gpg   ##密钥存放的位置
------------------------
pub   2048R/B1A2454A 2016-12-04
uid                  wangcai     ###密钥对的ID
sub   2048R/D23D0478 2016-12-04
[root:~]#    cd .gnupg/
[root:.gnupg]#    ll
total 28
-rw------- 1 root root 7680 Nov 30 16:37 gpg.conf
drwx------ 2 root root    6 Nov 30 16:37 private-keys-v1.d
-rw------- 1 root root 1169 Dec  4 17:18 pubring.gpg  ##公钥
-rw------- 1 root root 1169 Dec  4 17:18 pubring.gpg~
-rw------- 1 root root  600 Dec  4 17:18 random_seed
-rw------- 1 root root 2471 Dec  4 17:18 secring.gpg  ##私钥
srwxr-xr-x 1 root root    0 Dec  4 17:20 S.gpg-agent
-rw------- 1 root root 1280 Dec  4 17:18 trustdb.gpg

要使用公钥,必须将公钥导出并且传递给需要的对象,并且将公钥导入对方的公钥库中。

##导出密钥并发送给对方主机
[root:.gnupg]#    gpg -a --export -o wangcai.pu
[root:.gnupg]#    scp wangcai.pu root@172.16.251.219:/root
root@172.16.251.219's password:
wangcai.pu                                                                                                                           100% 1687     1.7KB/s   00:00
###对方生成的密钥对
[root:~]#   gpg --list-key
/root/.gnupg/pubring.gpg
------------------------
pub   2048R/8BF9F56C 2016-12-04
uid                  gentoo
sub   2048R/991066D5 2016-12-04
###将收到的公钥文件导入
[root:~]#   gpg --import wangcai.pu
gpg: key 0CA15C47: public key "wangcai" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
[root:~]#   gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub   1024R/32E2119F 2016-12-04
uid                  gentoo
sub   1024R/25F92931 2016-12-04

pub   1024R/0CA15C47 2016-12-04
uid                  wangcai
sub   1024R/A0FDCCAD 2016-12-04

将wangcai公钥加密的文件发送回去,就能够用wangcai私钥解:

[root:~]#   gpg -e -r wangcai adb.txt  ## -r 指定用谁的钥匙加密
gpg: A0FDCCAD: There is no assurance this key belongs to the named user

pub  1024R/A0FDCCAD 2016-12-04 wangcai
Primary key fingerprint: 9D56 F853 D32F 2B9C A380  1111 ABD9 3929 0CA1 5C47
     Subkey fingerprint: 8A6F ECE9 A098 B7F4 2F7A  DB16 EE56 E7E8 A0FD CCAD

It is NOT certain that the key belongs to the person named
in the user ID.  If you *really* know what you are doing,
you may answer the next question with yes.

Use this key anyway? (y/N) y
[root:~]#   scp adb.txt.gpg 172.16.252.81:/root
root@172.16.252.81's password:
adb.txt.gpg                                                                                                                            100%  246     0.2KB/s   00:00
###在wangcai主机上用wangci的私钥解开文件
[root:~]#    gpg -d adb.txt.gpg  ## -d 解密文件
gpg: encrypted with 1024-bit RSA key, ID A0FDCCAD, created 2016-12-04
     "wangcai"
name score sex
yang    80  m
zhang   46  f
[root:~]#    gpg -d adb.txt.gpg -o adb.txt
usage: gpg [options] --decrypt [filename]
[root:~]#    gpg -o adb.txt -d adb.txt.gpg   ## -o 将解密后的内容保存为文件
gpg: encrypted with 1024-bit RSA key, ID A0FDCCAD, created 2016-12-04
     "wangcai"
[root:~]#    ll adb.txt
-rw-r--r-- 1 root root 43 Dec  4 18:41 adb.txt
[root:~]#    cat adb.txt
name score sex
yang    80  m
zhang   46  f

删除主机上的公钥:

##删除主机上的wangcai的公钥
[root:~]#   gpg --delete-key wangcai
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


pub  1024R/0CA15C47 2016-12-04 wangcai

Delete this key from the keyring? (y/N) y
[root:~]#   gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub   1024R/32E2119F 2016-12-04
uid                  gentoo
sub   1024R/25F92931 2016-12-04
##删除主机上的密钥对

[root:~]#   gpg --delete-secret-key gentoo   ##删除私钥
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


sec  1024R/32E2119F 2016-12-04 gentoo

Delete this key from the keyring? (y/N) y
This is a secret key! - really delete? (y/N) y
[root:~]#   gpg --list-key
/root/.gnupg/pubring.gpg
------------------------
pub   1024R/32E2119F 2016-12-04
uid                  gentoo
sub   1024R/25F92931 2016-12-04

[root:~]#   cd .gnupg/
[root:.gnupg]#   ll
total 28
-rw------- 1 root root 7856 Oct 15 15:16 gpg.conf
drwx------ 2 root root 4096 Dec  4 16:16 private-keys-v1.d
-rw------- 1 root root  649 Dec  4 19:30 pubring.gpg
-rw------- 1 root root 1300 Dec  4 18:12 pubring.gpg~
-rw------- 1 root root  600 Dec  4 18:38 random_seed
-rw------- 1 root root    0 Dec  4 19:33 secring.gpg  ##私钥大小为0 已经删除
-rw------- 1 root root 1360 Dec  4 16:43 trustdb.gpg
[root:.gnupg]#   gpg --delete-key gentoo   ##删除公钥
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


pub  1024R/32E2119F 2016-12-04 gentoo

Delete this key from the keyring? (y/N) y
[root:.gnupg]#   ll
total 24
-rw------- 1 root root 7856 Oct 15 15:16 gpg.conf
drwx------ 2 root root 4096 Dec  4 16:16 private-keys-v1.d
-rw------- 1 root root    0 Dec  4 19:35 pubring.gpg   ##公钥大小为0 已经删除
-rw------- 1 root root  649 Dec  4 19:30 pubring.gpg~
-rw------- 1 root root  600 Dec  4 18:38 random_seed
-rw------- 1 root root    0 Dec  4 19:33 secring.gpg
-rw------- 1 root root 1360 Dec  4 19:35 trustdb.gpg

自建CA

在构建CA之前,查看PKI的相关配置文件:

[root:~]#    ll /etc/pki/tls/openssl.cnf
-rw-r--r--. 1 root root 10923 Mar  6  2015 /etc/pki/tls/openssl.cnf

打开该文件,寻找关于CA的配置,在CentOS7 中的配置如下:

####################################################################
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    = 365           # how long to certify for
74 default_crl_days= 30            # how long before next CRL
75 default_md  = sha256        # use SHA-256 by default
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

创建CA的相关配置按照配置文件的配置进行,否则会出错。 
在操作之前,在/etc/pki/CA目录下创建必要的文件以及目录:

[root:CA]#    ll
total 4
drwxr-xr-x. 2 root root   6 Jun 29  2015 certs
drwxr-xr-x. 2 root root   6 Jun 29  2015 crl
-rw-r--r--  1 root root   0 Dec  4 20:25 index.txt
drwxr-xr-x. 2 root root   6 Jun 29  2015 newcerts
drwx------. 2 root root  22 Dec  4 20:06 private
-rw-r--r--  1 root root   0 Dec  4 20:25 serial

第一步:在要当CA服务器的主机上生成私钥,使用工具openssl。注意:私钥的存放位置要按照配置文件的要求,默认的位置为:private_key = $dir/private/cakey.pem,即/etc/pki/CA/private目录下面:

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

第二步:生成自签证书

[root:private]#    cd ..   ##证书的位置,按照配置文件的要求
[root:CA]#    openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 7000
\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) []:BEIJNG
Locality Name (eg, city) [Default City]:BEIJING
Organization Name (eg, company) [Default Company Ltd]:zonekey
Organizational Unit Name (eg, section) []:tl
Common Name (eg, your name or your server's hostname) []:roger
Email Address []:
[root:CA]#    ll
total 4
-rw-r--r--  1 root root 944 Dec  4 20:13 cacert.pem
drwxr-xr-x. 2 root root   6 Jun 29  2015 certs   #生成的证书,自签证书生成。
-rw-r--r--  1 root root   0 Dec  4 20:25 index.txt
drwxr-xr-x. 2 root root   6 Jun 29  2015 crl
drwxr-xr-x. 2 root root   6 Jun 29  2015 newcerts
drwx------. 2 root root  22 Dec  4 20:06 private
-rw-r--r--  1 root root   0 Dec  4 20:25 serial

注意:生成的自签证书的命令中: 
-new:生成新证书的签署请求 
-x509:生成自签格式证书,专用于创建私有CA时 
-key:生成请求时用到的私有文件路径 
-out:生成的请求文件路径,如果自签操作将直接生成签署过的证书 
-days:证书的有效时长,单位为天

到此,CA服务器的搭建完成,就可以为其他的主机签署证书了。

签署证书

在被签署的主机上生成私钥:

[root:~]#    cd /etc/pki/tls
[root:tls]#    ls
cert.pem  certs  misc  openssl.cnf  private
[root:tls]#    cd private/
[root:private]#    ls
[root:private]#    (umask 077;openssl genrsa -out http.key)
Generating RSA private key, 1024 bit long modulus
.......................++++++
.............++++++
e is 65537 (0x10001)

生成证书签署请求:

[root:private]#    openssl req  -new -key http.key -out ../http.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]:beijing
Organization Name (eg, company) [Default Company Ltd]:zoekey
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:roger
Email Address []:

Please enter the following '
extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root:private]#    cd ..
[root:tls]#    ls
cert.pem  certs  http.csr  misc  openssl.cnf  private

将上步的请求证书拷贝到CA主机上,在CA主机上签署证书:

[root:CA]#    openssl ca -in /root/http.csr -out certs/http.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
       Serial Number: 2 (0x2)
       Validity
           Not Before: Dec  4 14:09:42 2016 GMT
           Not After : Dec  4 14:09:42 2017 GMT
       Subject:
           countryName               = CN
           stateOrProvinceName       = beijing
           organizationName          = zoekey
           organizationalUnitName    = it
           commonName                = roger
       X509v3 extensions:
           X509v3 Basic Constraints:
               CA:FALSE
           Netscape Comment:
               OpenSSL Generated Certificate
           X509v3 Subject Key Identifier:
               A2:B5:9A:22:A3:33:6D:7F:C6:16:6D:3B:FA:38:B4:3D:EC:AC:2F:EB
           X509v3 Authority Key Identifier:
               keyid:E3:13:89:7C:66:67:D5:14:A6:1D:22:65:71:32:47:C1:06:86:A2:8A

Certificate is to be certified until Dec  4 14:09:42 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

将生成的http.crt证书发送给请求方,到此,证书签发完成。

吊销证书

分两步,第一步,客户机上获取需要吊销的证书的序列号等信息:

[root:certs]#    openssl  x509 -in http.crt -noout -serial -subject
serial=02
subject= /C=CN/ST=beijing/O=zoekey/OU=it/CN=roger

根据客户机提交的信息,在CA主机上对比信息是否一致:

[root:newcerts]#    cat 02.pem
Certificate:
   Data:
       Version: 3 (0x2)
       Serial Number: 2 (0x2)
   Signature Algorithm: sha256WithRSAEncryption
       Issuer: C=CN, ST=beijing, L=beijing, O=zonekey, OU=tl, CN=roger
       Validity
           Not Before: Dec  4 14:09:42 2016 GMT
           Not After : Dec  4 14:09:42 2017 GMT
       Subject: C=CN, ST=beijing, O=zoekey, OU=it, CN=roger
       Subject Public Key Info:
           Public Key Algorithm: rsaEncryption
               Public-Key: (1024 bit)
               Modulus:
                   00:be:86:b5:95:f8:0d:6c:62:a0:7e:27:b6:44:cb:
                   77:2b:1f:3b:ea:d4:9e:16:79:23:9e:ab:ed:56:91:
                   9d:c7:5e:b3:12:d2:34:c5:d2:f8:f6:6e:a7:8a:b0:
                   ae:a9:3d:e0:f1:2a:96:3e:59:c3:1b:6f:be:1f:06:
                   2d:6e:0e:8a:46:67:c4:e4:92:77:81:6c:b2:56:ec:
                   fd:65:cb:c5:2e:b4:01:de:f7:5e:cf:57:22:0d:b7:
                   5d:e3:f9:ba:9b:d6:18:23:50:e8:e5:ef:8e:dc:91:
                   9a:ab:40:de:06:8c:8a:9c:cb:8b:23:c4:c6:84:9d:
                   27:f2:6a:42:c0:f7:14:df:ab
               Exponent: 65537 (0x10001)
       X509v3 extensions:
           X509v3 Basic Constraints:
               CA:FALSE
           Netscape Comment:
               OpenSSL Generated Certificate
           X509v3 Subject Key Identifier:
               A2:B5:9A:22:A3:33:6D:7F:C6:16:6D:3B:FA:38:B4:3D:EC:AC:2F:EB
           X509v3 Authority Key Identifier:
               keyid:E3:13:89:7C:66:67:D5:14:A6:1D:22:65:71:32:47:C1:06:86:A2:8A

   Signature Algorithm: sha256WithRSAEncryption
        80:c1:53:2f:d0:17:ce:a6:22:a5:05:2c:a8:6a:b9:be:35:db:
        bb:be:45:df:5a:31:b3:83:82:ed:57:f0:9a:f9:6e:ba:d5:a1:
        d2:ae:80:a4:1d:26:04:e9:55:a0:b9:89:0e:17:b2:8a:35:2c:
        9f:df:ac:08:fe:a7:98:29:6e:9c:9a:12:2c:00:73:ae:d7:b5:
        2c:c0:d4:21:4f:07:8f:1b:19:3a:06:5a:86:38:a0:22:f0:de:
        fc:a8:53:98:b8:c7:04:13:c2:94:87:90:ea:ad:4b:67:f8:dd:
        d6:2f:7e:76:8c:03:a8:5a:4f:49:7f:0a:b6:93:47:df:e7:b4:
        78:1e
-----BEGIN CERTIFICATE-----
MIICnjCCAgegAwIBAgIBAjANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJDTjEQ
MA4GA1UECAwHYmVpamluZzEQMA4GA1UEBwwHYmVpamluZzEQMA4GA1UECgwHem9u
ZWtleTELMAkGA1UECwwCdGwxDjAMBgNVBAMMBXJvZ2VyMB4XDTE2MTIwNDE0MDk0
MloXDTE3MTIwNDE0MDk0MlowTTELMAkGA1UEBhMCQ04xEDAOBgNVBAgMB2JlaWpp
bmcxDzANBgNVBAoMBnpvZWtleTELMAkGA1UECwwCaXQxDjAMBgNVBAMMBXJvZ2Vy
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+hrWV+A1sYqB+J7ZEy3crHzvq
1J4WeSOeq+1WkZ3HXrMS0jTF0vj2bqeKsK6pPeDxKpY+WcMbb74fBi1uDopGZ8Tk
kneBbLJW7P1ly8UutAHe917PVyINt13j+bqb1hgjUOjl747ckZqrQN4GjIqcy4sj
xMaEnSfyakLA9xTfqwIDAQABo3sweTAJBgNVHRMEAjAAMCwGCWCGSAGG+EIBDQQf
Fh1PcGVuU1NMIEdlbmVyYXRlZCBDZXJ0aWZpY2F0ZTAdBgNVHQ4EFgQUorWaIqMz
bX/GFm07+ji0PeysL+swHwYDVR0jBBgwFoAU4xOJfGZn1RSmHSJlcTJHwQaGooow
DQYJKoZIhvcNAQELBQADgYEAgMFTL9AXzqYipQUsqGq5vjXbu75F31oxs4OC7Vfw
mvluutWh0q6ApB0mBOlVoLmJDheyijUsn9+sCP6nmClunJoSLABzrte1LMDUIU8H
jxsZOgZahjigIvDe/KhTmLjHBBPClIeQ6q1LZ/jd1i9+dowDqFpPSX8KtpNH3+e0
eB4=
-----END CERTIFICATE-----
[root:newcerts]#    cd ..
[root:CA]#    cat index.txt
V   171204124322Z       01  unknown /C=CN/ST=beijing/O=zonekey/OU=tl/CN=rogerr
V   171204140942Z       02  unknown /C=CN/ST=beijing/O=zoekey/OU=it/CN=roger

确认无误后,吊销证书:

[root:CA]#    openssl ca -revoke newcerts/02.pem
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 02.
Data Base Updated
[root:CA]#    touch crlnumber   ##生成吊销证书的编号,第一次吊销的时候执行
[root:CA]#    echo 01 > crlnumber
##查看现在的证书情况
[root:CA]#    cat index.txt
V   171204124322Z       01  unknown /C=CN/ST=beijing/O=zonekey/OU=tl/CN=rogerr
R   171204140942Z   161204142254Z   02  unknown /C=CN/ST=beijing/O=zoekey/OU=it/CN=roger
##更新吊销证书的列表
[root:CA]#    openssl ca -gencrl -out crl/ca.crl
Using configuration from /etc/pki/tls/openssl.cnf
[root:CA]#    cat crl/ca.crl
-----BEGIN X509 CRL-----
MIIBTjCBuAIBATANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJDTjEQMA4GA1UE
CAwHYmVpamluZzEQMA4GA1UEBwwHYmVpamluZzEQMA4GA1UECgwHem9uZWtleTEL
MAkGA1UECwwCdGwxDjAMBgNVBAMMBXJvZ2VyFw0xNjEyMDQxNDI4NDFaFw0xNzAx
MDMxNDI4NDFaMBQwEgIBAhcNMTYxMjA0MTQyMjU0WqAOMAwwCgYDVR0UBAMCAQEw
DQYJKoZIhvcNAQELBQADgYEAj7xOVlkJrmk416MkrORKzxRtPcK5LImnLUW9Nzjc
5XHvrbntahv2oaJKQ35yO6dQlbox7spIvIuKMIV+JLABFzcelWrkfWC2qqejOXIN
8PAq0boCjX7GJkMJ7iKiXeSp/ou0dWk7O7KxbxpNE7hdJxMN9B6nXT3bLRvszpTo
2ig=
-----END X509 CRL-----
[root:CA]#    openssl crl -in crl/ca.crl -noout -text
Certificate Revocation List (CRL):
       Version 2 (0x1)
   Signature Algorithm: sha256WithRSAEncryption
       Issuer: /C=CN/ST=beijing/L=beijing/O=zonekey/OU=tl/CN=roger
       Last Update: Dec  4 14:28:41 2016 GMT
       Next Update: Jan  3 14:28:41 2017 GMT
       CRL extensions:
           X509v3 CRL Number:
               1
Revoked Certificates:
   Serial Number: 02
       Revocation Date: Dec  4 14:22:54 2016 GMT
   Signature Algorithm: sha256WithRSAEncryption
        8f:bc:4e:56:59:09:ae:69:38:d7:a3:24:ac:e4:4a:cf:14:6d:
        3d:c2:b9:2c:89:a7:2d:45:bd:37:38:dc:e5:71:ef:ad:b9:ed:
        6a:1b:f6:a1:a2:4a:43:7e:72:3b:a7:50:95:ba:31:ee:ca:48:
        bc:8b:8a:30:85:7e:24:b0:01:17:37:1e:95:6a:e4:7d:60:b6:
        aa:a7:a3:39:72:0d:f0:f0:2a:d1:ba:02:8d:7e:c6:26:43:09:
        ee:22:a2:5d:e4:a9:fe:8b:b4:75:69:3b:3b:b2:b1:6f:1a:4d:
        13:b8:5d:27:13:0d:f4:1e:a7:5d:3d:db:2d:1b:ec:ce:94:e8:
        da:28

证书吊销完成。

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

(2)
上一篇 2016-12-07 16:38
下一篇 2016-12-07 20:39

相关推荐

  • 马哥教育网络班20期+第5周课程练习

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; grep "^[[:space:]]\+.*" /boot/grub/grub.conf   2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; grep&nb…

    Linux干货 2016-07-12
  • haproxy

    12.1 高性能负载均衡软件HAProxy介绍 随着互联网业务的迅猛发展,大型电商平台和门户网站对系统的可用性和可靠性要求越来越高,高可用集群、负载均衡集群成为一种热门的系统架构解决方案。在众多的负载均衡集群解决方案中,有基于硬件的负载均衡设备,例如F5、Big-IP等,也有基于软件的负载均衡产品,例如HAProxy、LVS、Nginx等。在软件的负载均衡产…

    2018-01-03
  • 马哥教育网络班20期+第三周课程练习

    1. 列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [oracle@ocp ~]$ who root     pts/1        2016-06-26 …

    Linux干货 2016-06-26
  • N25_第二周作业_leon

    第二周博客作业_leon 1.       Linux上的文件管理类命令有哪些?其常用的使用方法及其相关示例演示 常用文件管理类命令有: mkdir、touch、rmdir、cp、rm、mv   mkdir—-创建目录 -m  直接设置权限(非默认权限) -v&nbs…

    Linux干货 2016-12-23
  • debian8下安装配置部署zabbix3.0

    一、安装配置zabbix server     web server服务器:172.28.0.187     mysql服务器:172.28.0.237     1、安装web server(172.28.0.187)  &nbs…

    Linux干货 2016-05-07
  • 软件包管理和磁盘管理

    软件运行和编译 ABI :Application Binary Interface 应用程序二进制接口     Windows和Linux不兼容      PE格式   ELF格式 库级别的虚拟化:       Linu…

    2017-04-24