秘钥操作

这个命令会生成一个1024/2048位的密钥,包含私钥和公钥。

openssl genrsa -out private.key 1024/2038                     (with out password protected)

openssl genrsa -des3 -out private.key 1024/2048    (password protected)

这个命令可以利用private.key文件生成公钥。

openssl rsa -in private.key -pubout -out public.key

查看私钥命令

openssl rsa -noout -text -in public.key

证书请求

openssl req -new -key private.key -out cert.csr (-config openssl.cnf)

openssl req -new -nodes -key private.key -out cert.csr (-config openssl.cnf)

这个命令将会生成一个证书请求,当然,用到了前面生成的密钥private.key文件
这里将生成一个新的文件cert.csr,即一个证书请求文件,你可以拿着这个文件去数字证书颁发机构(即CA)申请一个数字证书。CA会给你一个新的文件cacert.pem,那才是包含公钥给对方用的数字证书。

查看证书请求

openssl req -noout -text -in cert.csr

生成证书

自签名证书,用于自己测试,不需要CA签发
openssl req -new -x509 -key private.key -out cacert.pem -days 1095 (-config openssl.cnf)

CA签发证书:

CA是专门签发证书的权威机构,处于证书的最顶端。自签是用自己的私钥给证书签名,CA签发则是用CA的私钥给自己的证书签名来保证证书的可靠性,

利用OpenSSL可以自己作为CA进行证书签发,当然这并不权威。

CA签发证书生成的cacert.pem 见“建立CA颁发证书

有了private.key和cacert.pem文件后就可以在自己的程序中使用了,比如做一个加密通讯的服务器

从证书中提取公钥

openssl x509 -in cacert.pem -pubkey >> public.key

查看证书信息

openssl x509 -noout -text -in cacert.pem

建立CA颁发证书

(1) 环境准备

首先,需要准备一个目录放置CA文件,包括颁发的证书和CRL(Certificate Revoke List)。
mkdir ./CA

(2) 创建配置文件

之前生成秘钥和证书可以进行命令行配置,但是在创建CA的时候必须使用配置文件,因为做证书颁发的时候只能使用配置文件。

创建配置文件如下:vi openssl.cnf

     ################################################################
# openssl example configuration file.
# This is mostly used for generation of certificate requests.
#################################################################
[ ca ]
default_ca= CA_default # The default ca section
################################################################# [ CA_default ] dir=/opt/iona/OrbixSSL1.0c/certs # Where everything is kept
certs=$dir # Where the issued certs are kept
crl_dir= $dir/crl # Where the issued crl are kept
database= $dir/index.txt # database index file
new_certs_dir= $dir/new_certs # default place for new certs
certificate=$dir/CA/OrbixCA # The CA certificate
serial= $dir/serial # The current serial number
crl= $dir/crl.pem # The current CRL
private_key= $dir/CA/OrbixCA.pk # The private key
RANDFILE= $dir/.rand # private random number file
default_days= # how long to certify for
default_crl_days= # how long before next CRL
default_md= md5 # which message digest to use
preserve= no # keep passed DN ordering # A few different ways of specifying how closely the request should
# conform to the details of the CA policy= policy_match # For the CA policy [ policy_match ]
countryName= match
stateOrProvinceName= match
organizationName= match
organizationalUnitName= optional
commonName= supplied
emailAddress= optional # For the `anything' policy
# At this point in time, you must list all acceptable `object'
# types [ policy_anything ]
countryName = optional
stateOrProvinceName= optional
localityName= optional
organizationName = optional
organizationalUnitName = optional
commonName= supplied
emailAddress= optional [ req ]
default_bits =
default_keyfile= privkey.pem
distinguished_name = req_distinguished_name
attributes = req_attributes [ req_distinguished_name ]
countryName= Country Name ( letter code)
countryName_min=
countryName_max =
stateOrProvinceName= State or Province Name (full name)
localityName = Locality Name (eg, city)
organizationName = Organization Name (eg, company)
organizationalUnitName = Organizational Unit Name (eg, section)
commonName = Common Name (eg. YOUR name)
commonName_max =
emailAddress = Email Address
emailAddress_max = [ req_attributes ]
challengePassword = A challenge password
challengePassword_min =
challengePassword_max =
unstructuredName= An optional company name

根据配置文件。创建以下三个文件:

touch index.txt

touch index.txt.attr

touch serial 内容为01

(3) 生成CA私钥和证书

openssl genrsa -out ca.key 1024

openssl req -new -x509 -key ca.key -out ca.pem -days 365 -config openssl.cnf   (CA只能自签名证书,注意信息与要颁发的证书信息一致)

(4) 颁发证书

颁发证书就是用CA的秘钥给其他人签名证书,输入需要证书请求,CA的私钥及CA的证书,输出的是签名好的还给用户的证书

这里用户的证书请求信息填写的国家省份等需要与CA配置一致,否则颁发的证书将会无效。

openssl ca -in ../cert.csr -out cacert.pem -cert ca.pem -keyfile ca.key -config openssl.cnf

对比CA颁发的证书提取公钥和私钥导出的公钥是否一致:

同时产生01.pem,这个是CA的备份保留,与生成发送给请求证书的内容一致,serial内序号自动+1。

OpenSSL - 利用OpenSSL自签证书和CA颁发证书的更多相关文章

  1. CA证书、自颁发证书、自签名证书联系

    一.理论基础 ssl:secure socket layer(安全套接层协议)的缩写,通过此协议可以保证两个应用通信的可靠性和保密性.openssl:是ssl协议的实现.提供了对称加密算法.非对称加密 ...

  2. 自己搭建CA颁发证书做https加密网站

    192.168.10.187 CA服务器 192.168.10.190 web服务器 (1)搭建CA cd /etc/pki/CA 在这个目录下创建serial和index.txt两个文件 echo ...

  3. 自建 ca 及使用 ca 颁发证书

    创建CA: 一.安装openssl [root@localhost ~]# yum install -y openssl 二.创建CA的相关文件及目录 mkdir /opt/root_ca & ...

  4. 搭建CA颁发证书做https加密网站

    92.168.10.187 CA服务器 192.168.10.190 web服务器 (1)搭建CA cd /etc/pki/CA 在这个目录下创建serial和index.txt两个文件 echo 0 ...

  5. [加密]证书、CA、证书信任链

    转自:https://www.jianshu.com/p/6bf2f9a37feb TLS 传输层安全性协定 TLS(Transport Layer Security),及其前身安全套接层 SSL(S ...

  6. 部署自建CA颁发证书实现https加密

    理论忽略:百度上很多 需求:自建证书并实现域名的https加密 部署: 在linux机器上执行以下命令生成私钥 mkdir -p /opt/ssl-cert cd  /opt/ssl-cert 1.# ...

  7. 【openssl】利用openssl完成X509证书和PFX证书之间的互转

    利用openssl完成X509证书和PFX证书之间的互转 # OpenSSL的下载与安装: 1.下载地址: 官方网址—— https://www.openssl.org/source/ OpenSSL ...

  8. 数字证书的理解以及自建CA机构颁发证书

    一.理解什么是数字证书   http://www.cnblogs.com/JeffreySun/archive/2010/06/24/1627247.html    理解数字证书等概念,无数次想好好看 ...

  9. 利用openssl管理证书及SSL编程第1部分: openssl证书管理

    利用openssl管理证书及SSL编程第1部分 参考:1) 利用openssl创建一个简单的CAhttp://www.cppblog.com/flyonok/archive/2010/10/30/13 ...

随机推荐

  1. 彻底删除java*

    提示:先备份重要数据 1. 移除所有 Java相关包 (Sun, Oracle, OpenJDK, IcedTea plugins, GIJ): apt-get update apt-cache se ...

  2. 在网上看到的一篇文章关于js和php编码的

    解决办法: 采用js对URL中的汉字进行escape编码. <a href="" onclick="window.open('product_list.php?p_ ...

  3. java 代理模式一: 静态代理

    代理模式: 代理模式的作用:为其他对象提供一种代理以控制对 特定对象  的访问. 某种情况下,一个客户不想或者直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用:通过代理对象引用. ...

  4. monkey中的一些问题

    一起来看下导致App Crash的那些原因: 1.     空指针异常:错误日志定位java.lang.NullPointerException,详细日志记录如下 2.     安全异常:错误日志定位 ...

  5. .NET IL学习笔记(一)

    参考资料: 1. <.NET IL Assembler> 2. NGEN代码产生器 3. NGEN的使用 4. IL编辑器下载 5. IL编辑器的使用 知识点: ● Common Lang ...

  6. Linux nginx日志按天分割实例

    Linux nginx日志按天分割实例   nginx的日志有个小缺点,日志文件一直就是一个,不会自动地进行切割,如果访问量很大的话,将导致日志文件非常大,不便于管理这就需要我们自己来实现了,按日期每 ...

  7. easyui tree 折叠节点

    <ul id="jihuidian" class="easyui-tree" data-options="onBeforeLoad:functi ...

  8. linux修改密码的几种方法

    1.  启动电脑 ,进入grub模式.  也就是下面这个模式: 按下e键,进入下面这个画面.... 选第二个(kernel的那个):  然后按下e键之后进入 下面这个版面: 之后敲入  single ...

  9. C#窗体 LISTVIEW

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. JQuery 对 Select option 的操作

    下拉框: <select id="selectID" >         <option value="1">1</option& ...