zoukankan      html  css  js  c++  java
  • 生成证书

    基本知识

    证书生成网站:

    https://csr.chinassl.net/index.html

    证书会包含一个服务器的域名,或者包含多个域名( 多域名证书,SAN certificater == SubjectAltName Certificater)

    自签名证书:服务器生成一个服务器私钥,然后通过私钥生成证书请求文件,正常情况是把 “ 证书请求文件 ” 发给 CA 让其用 CA 私钥生成证书;如果 “ 证书请求文件 ” 用服务器私钥而非CA私钥生成证书,就叫做自签名证书。服务器使用自签名证书就不需要导入 CA 证书。

    openssl req 命令主要的功能有,生成证书请求文件, 查看验证证书请求文件,还有就是生成自签名证书。
    以下就主要记录一下openssl命令选项的意义,并记录一下简单的命令示例。

    首先说明下生成证书请求需要什么:申请者需要将自己的信息及其公钥放入证书请求中。但在实际操作过程中,
    所需要提供的是私钥而非公钥,因为它会自动从私钥中提取公钥。另外,还需要将提供的数据进行数字签名
    (使用单向加密),保证该证书请求文件的完整性和一致性,防止他人盗取后进行篡改,

    主要命令选项:

         -new    :说明生成证书请求文件
         -x509   :说明生成自签名证书
         -key    :指定已有的秘钥文件生成秘钥请求,只与生成证书请求选项-new配合。
         -newkey :-newkey是与-key互斥的,-newkey是指在生成证书请求或者自签名证书的时候自动生成密钥,
                  然后生成的密钥名称由-keyout参数指定。当指定newkey选项时,后面指定rsa:bits说明产生
                  rsa密钥,位数由bits指定。 如果没有指定选项-key和-newkey,默认自动生成秘钥。
         -out    :-out 指定生成的证书请求或者自签名证书名称
         -config :默认参数在ubuntu上为 /etc/ssl/openssl.cnf, 可以使用-config指定特殊路径的配置文件
         -nodes  :如果指定-newkey自动生成秘钥,那么-nodes选项说明生成的秘钥不需要加密,即不需要输入passphase.  
         -batch  :指定非交互模式,直接读取config文件配置参数,或者使用默认参数值     

    命令示例:

    1.#生成自签名证书,证书名client.crt,采用自动生成秘钥的方式,指定生成秘钥长度为1024,加密,秘钥文件client.key.
    openssl req -x509 -newkey rsa:1024 -out client.crt -keyout client.key -batch -nodes
    #上面的命令加上-new选项是同样的执行效果
    openssl req -new -x509 -newkey rsa:1024 -out client.crt -keyout client.key -batch -nodes  
    2.#生成自签名证书,证书名client.crt,指定秘钥文件,秘钥文件为rsa_private_key.pem。
    openssl req -new -x509 -key ./rsa_private_key.pem -out client.crt -nodes -batch
    #注意下面命令不能正确执行,不加-new 命令貌似不能指定秘钥文件
    openssl req -x509 -key ./rsa_private_key.pem -out client.crt -nodes -batch
    3.#指定秘钥文件pri_key.pem,生成证书请求文件 req.csr
    openssl req -new -key pri_key.pem -out req.csr
    #使用req命令,以文本方式查看刚生成的证书请求文件
    openssl req -in req1.csr -text
    #查看证书请求文件的公钥, 这个公钥就是从pri_key.pem私钥文件导出的公钥
    openssl req -in req1.csr -noout -pubkey
    下列选项和-noout选项配合分别查看证书请求文件的部分内容
    -noout -pubkey   #查看证书请求文件的公钥
    -noout -subject  #查看证书请求文件的个人信息部分
    最后可以通过man req 查看详细的openssl req的详细信息。
     
     

    实例:生成自签名证书

    1、生成私钥和证书请求文件

    openssl 有一个默认的配置文件 openssl.cnf,路径:

    可参考 openssl.cnf 创建自己的配置文件

    新建文件 san.conf,内容如下:

    [ req ]
    default_bits = 2048
    default_keyfile = server_privateKey.pem
    distinguished_name = req_distinguished_name
    req_extensions = req_ext

    [ req_distinguished_name ]
    countryName = Country Name (2 letter code)
    countryName_default = CN
    stateOrProvinceName = State or Province Name (full name)
    stateOrProvinceName_default = Fujian
    localityName = Locality Name (eg, city)
    localityName_default = Xiamen
    organizationName = Organization Name (eg, company)
    organizationName_default = none_company
    organizationalUnitName = Organizational Unit Name (eg, section)
    organizationalUnitName_default = IT
    commonName = Common Name (e.g. server FQDN or YOUR name)
    commonName_max = 64
    commonName_default = localhost

    [ req_ext ]
    subjectAltName = @alt_names

    [alt_names]
    DNS.1 = god1
    DNS.2 = god2
    DNS.3 = 192.168.0.1
    DNS.4 = 127.0.0.1

    运行以下命令:

    openssl req -new -nodes -out myreq.csr -config san.conf -batch

    或者

    openssl genrsa -out server_privateKey.pem 2048
    openssl req -new -out myreq.csr -key server_privateKey.pem -config san.conf -batch

    查看证书请求文件

    openssl req -text -noout -in myreq.csr

    2、生成证书

    生成带有 SAN 域名的证书

    openssl x509 -req -days 365 -in myreq.csr -signkey server_privateKey.pem -out server.crt -extensions req_ext -extfile san.conf

    生成不带 SAN 域名的证书

    openssl x509 -req -days 365 -in myreq.csr -signkey server_privateKey.pem -out server.crt
     
  • 相关阅读:
    Linux之文件处理命令
    Linux基础命令
    rip实验
    Linux基础之磁盘分区
    mysql安装
    centos Apache、php、mysql默认安装路径
    You probably tried to upload too large file. Please refer to documentation for ways to workaround this limit.
    Wrong permissions on configuration file, should not be world writable!
    机器会学习么 学习总结
    实验 5 Spark SQL 编程初级实践
  • 原文地址:https://www.cnblogs.com/god-of-death/p/14934351.html
Copyright © 2011-2022 走看看