openssl生成rsa公私钥 签发证书 pfx打包

2016-11-30 21:13:00
admin
原创 3132
摘要:openssl生成rsa公私钥 签发证书 pfx打包

一、openssl生成rsa公私钥、签发证书、pfx打包

1、什么是ASN.1编码?

1.1、ASN.1,Abstract Syntax Notation One,抽象语法标记,描述了一种对数据进行表示、编码、传输、解码的数据格式;

1.2、通常使用BER编码规则,使用TLV编码具体信息,TLV全称是Tag-Length-Value,BER常用子集有DER和CER;

1.3、编码规则:https://www.oss.com/asn1/resources/asn1-made-simple/encoding-rules.html

1.4、数据类型:https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference.html


2、asn1parse工具用于解析ASN.1结构。

    openssl asn1parse -in public.pem 
    0:d=0  hl=4 l= 290 cons: SEQUENCE          
    4:d=1  hl=2 l=  13 cons: SEQUENCE          
    6:d=2  hl=2 l=   9 prim: OBJECT            :rsaEncryption
   17:d=2  hl=2 l=   0 prim: NULL              
   19:d=1  hl=4 l= 271 prim: BIT STRING

   节点偏移:节点深度、节点头部长度、节点数据长度、节点类型;

   节点类型:cons是结构节点,prim是数据节点;


3、openssl命令如何使用?

3.1、openssl -h,显示帮助;

3.2、openssl cmd -h,显示命令帮助;

3.3、export OPENSSL_CONF=openssl.cnf,设置配置文件

3.4、encdgst包含很多扩展命令,扩展命令可以直接跟在openssl后面使用;


4、证书规范说明

4.1、证书规范:https://www.rfc-editor.org/rfc/rfc3280

4.2、AKI和SKI是为了做证书链关联,当前证书的AKI是签发证书的SKI;

4.3、V1和V3证书区别在于是否存在扩展属性,签发证书带extensions参数时生成V3证书;


5、pkcs12要求使用密码保护私钥,所以脚本需要用户输入密码,但是密码可以为空。

6、脚本下载genkey.sh


#!/bin/bash

usage() {
    echo "usage: command gen|show|help where_to_use [key_size(default is 2048)]"
    exit
}

[ $# -ge 2 ] && {
    mkdir -p $2
    cd $2
}

key_size=2048
[ $# -ge 3 ] && {
    key_size="$3"
}

countryName="CN"
stateOrProvinceName="guangdong"
localityName="shenzhen"
organizationName="3scard Co., Ltd."
organizationalUnitName="IT Dept"
commonName="$2"
#emailAddress="feinenxiang@3scard.com"
challengePassword="123456"
unstructuredName=""

subject="\
/C=$countryName\
/ST=$stateOrProvinceName\
/L=$localityName\
/O=$organizationName\
/OU=$organizationalUnitName\
/CN="$commonName""
#/emailAddress="$emailAddress"

case "$1" in
gen)
    openssl genrsa -out private.pem "$key_size"
    openssl rsa -in private.pem -outform pem -pubout -out public.pem
    
    openssl req -new -key private.pem -sha256 -out certReq.csr -subj "$subject"
    openssl x509 -req -days 3650 -in certReq.csr -signkey private.pem -sha256 -out cert.crt
    openssl x509 -outform der -in cert.crt -out cert.der
    
    openssl pkcs12 -export -out private.p12 -inkey private.pem -in cert.crt
    cp -rf private.p12 private.pfx
    ;;
show)
    openssl x509 -text -in cert.crt
    ;;
*)
    usage
esac

发表评论
评论通过审核之后才会显示。