openssl使用多种方法签名、自签名

1.自建CA

自建CA的机制:1.生成私钥2.创建证书请求,在创建证书请求过程中由于需要提供公钥,而公钥来源于私钥,所以也需要指定私钥来创建证书请求,而实际上这里提供私钥的作用就是提取其中的公钥,这一点在后文给出了证明3.使用私钥对证书请求签名。

由于测试环境,所以自建的CA只能是根CA。配置文件如下。本文将使用该配置文件/ssl/ssl.conf进行CA相关命令的演示。

[default]
name = root-ca    /* 变量*/
default_ca = CA_default
name_opt = ca_default
cert_opt = ca_default

[CA_default]
home = .     /* 变量*/
database = $home/db/index
serial = $home/db/serial
crlnumber = $home/db/crlnumber
certificate = $home/$name.crt
private_key = $home/private/$name.key
RANDFILE = $home/private/random
new_certs_dir = $home/certs
unique_subject = no
copy_extensions = none
default_days = 3650
default_crl_days = 365
default_md = sha256
policy = policy_to_match

[policy_to_match]
countryName = match
stateOrProvinceName = optional
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional

[CA_DN]
countryName = "C"
contryName_default = "CN"
organizationName = "O"
organizationName_default = "jmu"
commonName = "CN"
commonName_default = "longshuai.com"

[req]
default_bits = 4096
encrypt_key = no
default_md = sha256
utf8 = yes
string_mask = utf8only
# prompt = no  /* 测试时该选项导致出错,所以将其注释掉*/
distinguished_name = CA_DN
req_extensions = ca_ext

[ca_ext]
basicConstraints = critical,CA:true
keyUsage = critical,keyCertSign,cRLSign
subjectKeyIdentifier = hash

(1).创建openssl的目录结构

(a).创建配置文件
[[email protected] ~]# mkdir /ssl;touch /ssl/ssl.conf

[[email protected] ~]# cd /ssl

[[email protected] ssl]# vim ssl.conf
(b).创建openssl的目录结构中的目录,在上述配置文件中的目录分别为/ssl/db、/ssl/private和/ssl/certs,可以考虑将private目录的权限设置为600或者400。
[[email protected] ssl]# mkdir /ssl/{db,private,certs}

[[email protected] ssl]# chmod -R 400 private/

(2).CA自签名

普通的证书请求需要使用CA的私钥进行签名变成证书,既然是自签名证书那当然是使用自己的私钥来签名。可以使用伪命令req、ca、x509来自签名。

使用req伪命令创建CA

这里有两种方法:1.一步完成,即私钥、证书请求、自签名都在一个命令中完成2.分步完成,先生成私钥、再创建证书请求、再指定私钥来签名。方法2中其实生成私钥和证书申请可以合并在一步中完成,证书申请和签名也可以合并在一步中完成。

方法一:一步完成

在下面的一步命令中,使用-new由于没有指定私钥输出位置,所以自动保存在ssl.conf中default_keyfile指定的private.pem中;由于ssl.conf中的req段设置了encrypt_key=no,所以交互时不需要输入私钥的加密密码;由于使用req -x509自签名的证书有效期默认为30天,而配置文件中req段又不能配置该期限,所以只能使用-days来指定有效期限,注意这个-days选项只作用于x509签名,证书请求中如果指定了时间是无效的。

[[email protected] ssl]# openssl req -x509 -new -out req.crt -config ssl.conf -days 365
[[email protected] ssl]# ll
total 24
drwxr-xr-x 2 root root 4096 Nov 22 09:05 certs
drwxr-xr-x 2 root root 4096 Nov 22 09:05 db
drwx------ 2 root root 4096 Nov 22 09:05 private
-rw-r--r-- 1 root root 3272 Nov 22 10:52 private.pem  ?/* 注意权限为644 */
-rw-r--r-- 1 root root 1753 Nov 22 10:52 req.crt
-rw-r--r-- 1 root root 1580 Nov 22 10:51 ssl.conf
[[email protected] ssl]# openssl x509 -noout -dates -in req.crt
notBefore=Nov 22 02:52:24 2016 GMT
notAfter=Nov 22 02:52:24 2017 GMT

方法二:分步完成,这里把各种可能的步骤合并都演示一遍

>>创建私钥和证书请求合并而签名独自进行的方法<<

[[email protected] ssl]# openssl req -newkey rsa:1024 -keyout key.pem -out req1.csr -config ssl.conf -days 365
[[email protected] ssl]# openssl req -x509 -in req1.csr -key key.pem -out req1.crt
[[email protected] ssl]# openssl x509 -noout -dates -in req1.crt?/* 注意签名不要配置文件 */
notBefore=Nov 22 02:58:25 2016 GMT
notAfter=Dec 22 02:58:25 2016 GMT  ?/* 可以看到证书请求中指定-days是无效的 */
[[email protected] ssl]# ll
total 36
drwxr-xr-x 2 root root 4096 Nov 22 09:05 certs
drwxr-xr-x 2 root root 4096 Nov 22 09:05 db
-rw-r--r-- 1 root root  912 Nov 22 10:57 key.pem
drwx------ 2 root root 4096 Nov 22 09:05 private
-rw-r--r-- 1 root root 3272 Nov 22 10:52 private.pem
-rw-r--r-- 1 root root  826 Nov 22 10:58 req1.crt
-rw-r--r-- 1 root root  688 Nov 22 10:57 req1.csr
-rw-r--r-- 1 root root 1753 Nov 22 10:52 req.crt
-rw-r--r-- 1 root root 1580 Nov 22 10:51 ssl.conf

>>独自生成私钥,而请求和签名合并的方法<<

[[email protected] ssl]# (umask 077;openssl genrsa -out key1.pem 1024)
[[email protected] ssl]# openssl req -x509 -new -key key1.pem -out req2.crt -config ssl.conf -days 365
[[email protected] ssl]# openssl x509 -noout -dates -in req2.crt
notBefore=Nov 22 03:28:31 2016 GMT
notAfter=Nov 22 03:28:31 2017 GMT
[[email protected] ssl]# ll
total 44
drwxr-xr-x 2 root root 4096 Nov 22 09:05 certs
drwxr-xr-x 2 root root 4096 Nov 22 09:05 db
-rw-r--r-- 1 root root  912 Nov 22 10:57 key1.pem
-rw------- 1 root root  887 Nov 22 11:26 key2.pem
drwx------ 2 root root 4096 Nov 22 09:05 private
-rw-r--r-- 1 root root 3272 Nov 22 10:52 private.pem
-rw-r--r-- 1 root root  826 Nov 22 10:58 req1.crt
-rw-r--r-- 1 root root  688 Nov 22 10:57 req1.csr
-rw-r--r-- 1 root root  709 Nov 22 11:28 req2.crt
-rw-r--r-- 1 root root 1753 Nov 22 10:52 req.crt
-rw-r--r-- 1 root root 1580 Nov 22 10:51 ssl.conf

>>完全分步进行<<

[[email protected] ssl]# rm -rf key* req* private.pem
[[email protected] ssl]# (umask 077;openssl genrsa -out key.pem 1024)
[[email protected] ssl]# openssl req -new -key key.pem -out req.csr -config ssl.conf
[[email protected] ssl]# openssl req -x509 -key key.pem -in req.csr -out req.crt -days 365
[[email protected] ssl]# openssl x509 -noout -dates -in req.crt
notBefore=Nov 22 04:29:21 2016 GMT
notAfter=Nov 22 04:29:21 2017 GMT
[[email protected] ssl]# ll
total 28
drwxr-xr-x 2 root root 4096 Nov 22 09:05 certs
drwxr-xr-x 2 root root 4096 Nov 22 09:05 db
-rw------- 1 root root  887 Nov 22 12:28 key.pem
drwx------ 2 root root 4096 Nov 22 09:05 private
-rw-r--r-- 1 root root  826 Nov 22 12:29 req.crt
-rw-r--r-- 1 root root  688 Nov 22 12:28 req.csr
-rw-r--r-- 1 root root 1580 Nov 22 10:51 ssl.conf

在本节的开头说明了创建证书请求时需要提供私钥,这个私钥的作用是为了提供公钥。下面是验证。

/* 提取私钥key.pem中的公钥到key.pub文件中 */
[[email protected] ssl]# openssl rsa -in key.pem -pubout -out key.pub
/* 输出证书请求req.csr中的公钥部分 */
[[email protected] ssl]# openssl req -noout -pubkey -in req.csr
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+YBneLYbh+OZWpiyPqIQHOsU5
D8il6UF7hi3NgEX/6vtciSmp7GXpXUV1tDglCCTPOfCHcEzeO0Gvky21LUenDsl/
aC2lraSijpl41+rT4mKNrCyDPZw4iG44+vLHfgHb3wJhBbBk0aw51dmxUat8FHCL
hU7nx+Du637UDlwdEQIDAQAB
-----END PUBLIC KEY-----
/* 查看key.pub,可以发现和req.csr中的公钥是一样的 */
[[email protected] ssl]# cat key.pub
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+YBneLYbh+OZWpiyPqIQHOsU5
D8il6UF7hi3NgEX/6vtciSmp7GXpXUV1tDglCCTPOfCHcEzeO0Gvky21LUenDsl/
aC2lraSijpl41+rT4mKNrCyDPZw4iG44+vLHfgHb3wJhBbBk0aw51dmxUat8FHCL
hU7nx+Du637UDlwdEQIDAQAB
-----END PUBLIC KEY-----

虽然创建证书请求时使用的是公钥,但是却不能使用-key选项指定公钥,而是只能指定私钥,因为req -new或-newkey选项会调用openssl rsa命令来提取公钥,指定公钥该调用将执行失败。

使用x509伪命令创建CA

使用x509伪命令需要提供请求文件,因此需要先创建证书请求文件。由于x509伪命令签名时不读取配置文件,所以不需要设置配置文件,若需要某选项,只需使用x509中对应的选项来达成即可。

以下x509 -req用于自签名,需要-signkey提供签名所需私钥key.pem。

[[email protected] ssl]# openssl req -new -keyout key.pem -out req.csr -config ssl.conf
[[email protected] ssl]# openssl x509 -req -in req.csr -signkey key.pem -out x509.crt
使用ca伪命令创建CA

使用ca伪命令自签名会读取配置文件中的ca部分,所以配置文件中所需的目录和文件结构都需要创建好,包括目录db、private、certs,文件db/index、db/serial,并向serial中写入一个序列号。由于是自签名,可以自行指定私钥文件,因此对于签名所需CA私钥文件无需放置在private目录中。

[[email protected] ssl]# touch db/{serial,index}
[[email protected] ssl]# echo "01" > db/serial
[[email protected] ssl]# openssl req -new -keyout key.pem -out req.csr -config ssl.conf
[[email protected] ssl]# openssl ca -selfsign -keyfile key.pem -in req.csr -config ssl.conf

在此签名过程中有两次询问,如下:

Certificate is to be certified until Nov 20 06:34:41 2026 GMT (3650 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

若要无交互,则使用-batch进入批处理模式。

[[email protected] ssl]# openssl ca -selfsign -keyfile key.pem -in req.csr -config ssl.conf -batch

2.为其他证书请求签名

CA为其他请求或证书签名时,需要使用到的文件有:自己的CA证书和自己的私钥文件。因此签名过程中需要提供这两个文件。

(1).使用ca伪命令为其他证书请求签名

使用ca伪命令自建根CA后,目录结构如下:

[[email protected] ssl]# tree -R -C
.
├── certs
│   └── 01.pem
├── db
│   ├── index
│   ├── index.attr
│   ├── index.old
│   ├── serial
│   └── serial.old
├── key.pem
├── private
├── req.csr
└── ssl.conf

其中01.pem是根CA证书,key.pem是根CA私钥。

现在要为其他证书请求签名,首先创建其他请求吧,假设该请求文件/tmp/req.csr。

[[email protected] ssl]# openssl req -new -keyout /tmp/key.pem -out /tmp/req.csr -config ssl.conf

使用根证书01.pem为/tmp/req.csr签名。

[[email protected] ssl]# openssl ca -in /tmp/req.csr -keyfile key.pem -cert certs/01.pem -config ssl.conf -batch

这样挺麻烦,因为每次为别人签名时都要指定-cert和-keyfile,可以将CA的证书和CA的私钥移动到配置文件中指定的路径下:


certificate = $home/$name.crt

private_key = $home/private/$name.key

[[email protected] ssl]# mv certs/01.pem root-ca.crt
[[email protected] ssl]# mv key.pem private/root-ca.key

再使用ca签名时将可以使用默认值。

[[email protected] ssl]# openssl ca -in /tmp/req.csr -config ssl.conf -batch

(2).使用x509伪命令为其他证书请求签名

现在根CA证书为root-ca.crt,CA的私钥为private/root-ca.key。

下面使用x509伪命令实现签名。由于x509不会读取配置文件,所以需要提供签名的序列号,使用-CAcreateserial可以在没有序列号文件时自动创建;由于x509默认-in指定的输入文件是证书文件,所以要对请求文件签名,需要使用-req来表示输入文件为请求文件。

[[email protected] ssl]# openssl x509 -req -in /tmp/req.csr -CA root-ca.crt -CAkey private/root-ca.key -out x509.crt -CAcreateserial
时间: 2024-08-05 15:29:45

openssl使用多种方法签名、自签名的相关文章

JNI 方法注册与签名+BufferedReader使用readLine问题

最近了解了关于JavaJNI接口的一些关于方法注册与签名相关的知识,在此进行一下总结. 使用JNI接口时,我们首先需要把Java方法声明为native: [java] view plain copy public native void f(); 然后编写对应的C/C++代码,并编译成为动态链接库(.dll或.so),在调用Java方法前载入动态链接库即可调用: [java] view plain copy static { System.loadLibrary("native-lib"

android自动打包方法(ant+proguard+签名)

前段时间做了一个android的网游项目,现在优化减少体积和防止别人反编译,需要把编译后.class进行混淆,开始在网上看了一些关于 ProGuard的介绍,基本上都是使用ADT自带的打包方式,那个打包方式太慢了,还要手工输密码,一个字烦. 于是开始寻找ant+proguard+签名的打包方式,遗憾的是资料不是缺手就是断脚. 好吧,看来得食自己了,!@#¥@#!@#!@##¥@#¥!@#@ 转眼一周,我++,终于把东西搞出来 ps:我们项目还有一个特殊需求,要把版本号,推广ID打到包里去,方便做

在域中获取域管理员权限的多种方法及一些缓解措施

翻译:hac425 前言 现在攻击者有很多方法可以用来获取域管理员权限.这篇文章的目的是介绍一些当前较受欢迎的方式.这里所介绍的技术的基本点是攻击者已经拿到了一台域中的服务器,并已获得域用户凭据. 对大多数企业来说有一个不幸的现实:对于一个攻击者来说,从域用户权限提升到域管理员权限往往不需要太长的时间.为什么会这样呢?本文会介绍其中使用的一些技巧. 现在针对一个企业,组织的攻击越来越频繁通过一些钓鱼手法来开始.比如通过发送大量的钓鱼邮件给目标的成员来在目标网络的机器上执行代码.一旦攻击者能够在企

使用mshta.exe绕过应用程序白名单(多种方法)

0x00 简介 很长一段时间以来,HTA文件一直被web攻击或在野恶意软件下载程序用作恶意程序的一部分.HTA文件在网络安全领域内广为人知,从红队和蓝队的角度来看,它是绕过应用程序白名单有价值的"古老"方式之一.运行Microsoft HTML应用程序主机的Mshta.exe,Windows OS实用程序负责运行HTA(HTML应用程序)文件.我们可以运行JavaScript或Visual的HTML文件.您可以使用Microsoft MSHTA.exe工具解析这些文件. 0x01 HT

C语言轻松高效学习方法之:多种方法实现

多种方法实现同一个功能,可以调动你学的所有知识去做,有助于你学的融会贯通. 下面举例来看: 实现功能:求一个整数的位数: 实现语言:C语言: 开发环境:Visual Studio 2017 如:3215是4位数 实现原理: 3215/10 = 321 ----1位数 321/10 = 32 ----又是1位数 32/10 = 3 ----又是1位数 3/10 = 0 ----又是1位数 共4位数,且终止计算条件是/10结果为0的时候: 根据这个原理,先写一个最笨的原始方法: 效果: 这种实现方案

教你用多种方法将pdf文件转换成jpg

我们在工作中经常会遇到pdf与word.excel.jpg等格式文件的转换,可是怎样才能把两种文件转换的完整有效呢?下面小编就pdf与jpg这两种格式来讲一下它们是如何转换的! pdf文件转换成jpg都有哪些方法? pdf文件转换成jpg有很多种方法,这里我们介绍最简单的三种,第一种是使用Adobe acrobat另存为jpg图片:另一种是在线将pdf转换成jpg; 还有一种方法可以使用第三方软件迅捷pdf转换器将pdf转换成jpg. 一.使用Adobe acrobat另存为jpg图片 用Ado

js返回上一页并刷新的多种方法

js返回上一页并刷新的几种方法.参考链接:http://www.jbxue.com/article/11230.html <a href="javascript:history.go(-1)">返回上一页</a><a href="javascript:location.reload()">刷新当前页面</a><a href="javascript:" onclick="history

VC开发多语言界面 多种方法(很简单) 有源码

(需源码先留邮箱)先上图 1.通过遍历 得到所有控件ID号与TEXT,得到一个中文语言配置文件 void CVV_485Dlg::getCaptionForWindow() //做程序时用,其它时间不用 { //枚举对话框中所有组件 CWnd *pCtrl = GetWindow(GW_CHILD); while(pCtrl!=NULL) { UINT ctrlID = pCtrl->GetDlgCtrlID(); // setControlCaption(pCtrl,ctrlID); CStr

shell编程基础一(多种方法求值1+2+..+100)

#SHELL编程基础一(多种方法求值1+2+..+100)##为什么要学好shell shell脚本语言是实现linux系统管理及自动化运维所必备的重要工具,linux系统的底层及基础应用软件的核心大都涉及shell脚本的内容. 每一个合格的linux系统管理员或运维工程师,都需要能够熟练地编写shell脚本语言,并能够阅读系统及各类软件附带的shell脚本内容. 只有这样才能提升运维人员的工作效率,适应日益复杂的工作环境,减少不必要的工作,从而为个人的职场发展奠定较好的基础.# 本文的宗旨是熟