zoukankan      html  css  js  c++  java
  • OpenSSL

    秘钥操作

    这个命令会生成一个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

     1     ################################################################ 
     2     # openssl example configuration file. 
     3     # This is mostly used for generation of certificate requests. 
     4     ################################################################# 
     5     [ ca ] 
     6     default_ca= CA_default          # The default ca section 
     7     ################################################################# 
     8      
     9     [ CA_default ] 
    10      
    11     dir=/opt/iona/OrbixSSL1.0c/certs # Where everything is kept 
    12     certs=$dir                       # Where the issued certs are kept 
    13     crl_dir= $dir/crl                # Where the issued crl are kept 
    14     database= $dir/index.txt         # database index file 
    15     new_certs_dir= $dir/new_certs    # default place for new certs 
    16     certificate=$dir/CA/OrbixCA      # The CA certificate 
    17     serial= $dir/serial              # The current serial number 
    18     crl= $dir/crl.pem                # The current CRL 
    19     private_key= $dir/CA/OrbixCA.pk  # The private key 
    20     RANDFILE= $dir/.rand             # private random number file 
    21     default_days= 365                # how long to certify for 
    22     default_crl_days= 30             # how long before next CRL 
    23     default_md= md5                  # which message digest to use 
    24     preserve= no                     # keep passed DN ordering 
    25      
    26     # A few different ways of specifying how closely the request should 
    27     # conform to the details of the CA 
    28      
    29     policy= policy_match            # For the CA policy 
    30     
    31     [ policy_match ]  
    32     countryName= match 
    33     stateOrProvinceName= match 
    34     organizationName= match 
    35     organizationalUnitName= optional 
    36     commonName= supplied 
    37     emailAddress= optional 
    38      
    39     # For the `anything' policy 
    40     # At this point in time, you must list all acceptable `object' 
    41     # types 
    42      
    43     [ policy_anything ] 
    44     countryName = optional 
    45     stateOrProvinceName= optional 
    46     localityName= optional 
    47     organizationName = optional 
    48     organizationalUnitName = optional 
    49     commonName= supplied 
    50     emailAddress= optional 
    51      
    52     [ req ] 
    53     default_bits = 1024 
    54     default_keyfile= privkey.pem 
    55     distinguished_name = req_distinguished_name 
    56     attributes = req_attributes 
    57      
    58     [ req_distinguished_name ] 
    59     countryName= Country Name (2 letter code) 
    60     countryName_min= 2 
    61     countryName_max = 2 
    62     stateOrProvinceName= State or Province Name (full name) 
    63     localityName = Locality Name (eg, city) 
    64     organizationName = Organization Name (eg, company) 
    65     organizationalUnitName  = Organizational Unit Name (eg, section) 
    66     commonName = Common Name (eg. YOUR name) 
    67     commonName_max = 64 
    68     emailAddress = Email Address 
    69     emailAddress_max = 40 
    70      
    71     [ req_attributes ] 
    72     challengePassword = A challenge password 
    73     challengePassword_min = 4 
    74     challengePassword_max = 20 
    75     unstructuredName= An optional company name 
    View Code

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

    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。

  • 相关阅读:
    查询job的几个语句
    fdisk与parted分区
    升级时针对Source oracle home 不存在解决办法
    oracle常用视图v$mystat v$sesstat v$sysstat v$statname v$thread v$ parameter v$session v$process
    x$ksppi与x$ksppcv查询隐藏参数
    查询表空间及已使用情況的SQL语句
    通过系统进程查找sql语句
    RAC日常维护命令
    创建ASM实例及ASM数据库(转载)
    安装数据库软件,节点未显示
  • 原文地址:https://www.cnblogs.com/binchen-china/p/5651142.html
Copyright © 2011-2022 走看看