安装完成后,配置好环境变量,把bin加到path环境变量中
C:\Users\ASUS>openssl OpenSSL>
E:\openssl>dir 驱动器 E 中的卷没有标签。 卷的序列号是 C4E0-A27C E:\openssl 的目录 2014/05/20 01:56 <DIR> . 2014/05/20 01:56 <DIR> .. 2014/05/20 01:56 <DIR> client 2014/05/20 01:56 <DIR> root 2014/05/20 01:56 <DIR> server 0 个文件 0 字节 5 个目录 257,668,497,408 可用字节
(所有密码全部默认123456)
E:\openssl\root>openssl OpenSSL> genrsa -des3 -out root-key.key 1024 Loading 'screen' into random state - done Generating RSA private key, 1024 bit long modulus .....++++++ .....................++++++ e is 65537 (0x10001) Enter pass phrase for root-key.key: Verifying - Enter pass phrase for root-key.key: OpenSSL>
genrsa 用于生成RSA密钥对的命令
-des3 (These options encrypt the private key with specified cipher before outputting it.)。今后使用该密钥时,需要输入相应的口令。
-out 令生成的密钥对保存到文件,输出root-key.key文件(文件的编码格式是pem格式,后缀名称无所谓)(the output filename. If this argument is not specified then standard output is used.)
OpenSSL> req -new -out root-req.csr -key root-key.key -keyform PEM Enter pass phrase for root-key.key: Loading 'screen' into random state - done You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:CN State or Province Name (full name) [Some-State]:sy Locality Name (eg, city) []:sy Organization Name (eg, company) [Internet Widgits Pty Ltd]:www.cnivi.cn Organizational Unit Name (eg, section) []:rjxy Common Name (e.g. server FQDN or YOUR name) []:wwww.cnivi.cn Email Address []:[email protected] Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:123456 An optional company name []:www.cnivi.com.cn OpenSSL>
req 产生证书签发申请命令
-new 表示新请求
-key ==This specifies the file to read the private key from. It also accepts PKCS#8 format private keys for PEM format files.
-out 输出路径 输出root-req.csr文件(This specifies the output filename to write to or standard output by default.)
-keyform PEM|DER -----the format of the private key file specified in the -key argument. PEM is the default.
得到根证书签发申请文件后,我们可以将其发送给CA机构签发。我们可以自行签发根证书。
OpenSSL> x509 -req -in root-req.csr -out root-cert.cer -signkey root-key.key -CAcreateserial -days 3 650 Loading 'screen' into random state - done Signature ok subject=/C=CN/ST=sy/L=sy/O=www.cnivi.cn/OU=rjxy/CN=wwww.cnivi.cn/[email protected] Getting Private key Enter pass phrase for root-key.key: OpenSSL>
x509 签发x509格式证书命令
-req 表示证书输入请求(by default a certificate is expected on input. With this option a certificate request is expected instead.)
-signkey 表示签名密钥(this option causes(导致引起) the input file to be self signed using the supplied(提供的) private key.)
-in 表示输入文件
-out 表示输出文件,root-cert.cer文件
OpenSSL> pkcs12 -export -clcerts -in root-cert.cer -inkey root-key.key -out root.p12 Loading 'screen' into random state - done Enter pass phrase for root-key.key: Enter Export Password: Verifying - Enter Export Password: OpenSSL>
pkcs12 PKCS#12编码格式证书命令
-export ===This option specifies that a PKCS#12 file will be created rather than parsed.
-inkey ==(file to read private key from. If not present then a private key must be present in the input file.)
-in (The filename to read certificates and private keys from, standard input by default. They must all be in PEM format.)
-out 表示输出文件,输出root.p12文件(This specifies filename to write the PKCS#12 file to. Standard output is used by default.)
E:\openssl\root>keytool -importcert -v -trustcacerts -storepass 123456 -alias root -file root-cert.c er -keystore root.jks 所有者: [email protected], CN=wwww.cnivi.cn, OU=rjxy, O=www.cnivi.cn, L=sy, ST=sy, C=CN 发布者: [email protected], CN=wwww.cnivi.cn, OU=rjxy, O=www.cnivi.cn, L=sy, ST=sy, C=CN 序列号: b341c806bf5e295a 有效期开始日期: Tue May 20 02:13:03 CST 2014, 截止日期: Fri May 17 02:13:03 CST 2024 证书指纹: MD5: C7:72:DE:47:C5:24:DD:A8:50:B4:9C:27:42:0E:59:37 SHA1: AD:6E:E8:CD:9C:FE:4F:69:39:D9:5D:E9:5E:78:1D:84:38:29:24:9A SHA256: 5F:CD:75:EB:4B:8F:BF:C2:B2:E2:0E:7C:7F:91:00:E1:AB:DD:B4:15:E3:B4:F6:0F:29:17:E2:98 :35:C3:1D:F2 签名算法名称: SHA1withRSA 版本: 1 是否信任此证书? [no]: y 证书已添加到密钥库中 [正在存储root.jks] E:\openssl\root>
E:\openssl\server>openssl OpenSSL> genrsa -des3 -out server-key.key Loading 'screen' into random state - done Generating RSA private key, 1024 bit long modulus .................................++++++ .++++++ e is 65537 (0x10001) Enter pass phrase for server-key.key: Verifying - Enter pass phrase for server-key.key: OpenSSL>
OpenSSL> req -new -out server-req.csr -key server-key.key Enter pass phrase for server-key.key: Loading 'screen' into random state - done You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:CN State or Province Name (full name) [Some-State]:sy Locality Name (eg, city) []:sy Organization Name (eg, company) [Internet Widgits Pty Ltd]:rjxy Organizational Unit Name (eg, section) []:rjgc Common Name (e.g. server FQDN or YOUR name) []:www.ceit.com Email Address []:[email protected] Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:123456 An optional company name []:123456 OpenSSL>
OpenSSL> x509 -req -in server-req.csr -out server-cert.cer -signkey server-key.key -CA ../root/root- cert.cer -CAkey ../root/root-key.key -CAcreateserial -days 3650 Loading 'screen' into random state - done Signature ok subject=/C=CN/ST=sy/L=sy/O=rjxy/OU=rjgc/CN=www.ceit.com/[email protected] Getting Private key Enter pass phrase for server-key.key: Getting CA Private Key Enter pass phrase for ../root/root-key.key: OpenSSL>
-CA 表示CA证书
-CAkey 表示CA证书密钥
-CAcreateserial 表示创建证书序列号
-out 输出文件
-in 输入文件
OpenSSL> pkcs12 -export -clcerts -in server-cert.cer -inkey server-key.key -out server.p12 Loading 'screen' into random state - done 1960:error:02001002:system library:fopen:No such file or directory:.\crypto\bio\bss_file.c:398:fopen ('.srl','rb') Enter pass phrase for server-key.key: Enter Export Password: Verifying - Enter Export Password: OpenSSL>
类似上面制作服务器端证书
@echo off echo. echo. echo 单向SSL验证 TOMCAT_HOME/conf/server.xml文件配置示例 echo. echo My Blog:ht^tp://blog.csdn.net/sunyujia/ echo. set OPENSSL_CONF=openssl.cnf echo. echo 制作根证书 echo 1.创建根证私钥 if not exist root-key.key ( echo 创建一个不加密的PEM格式的私钥root-key.key rem genrsa 用于生成一个 RSA 私钥 rem 1024是私钥的长度,默认是 512 ,最大是 1024 openssl genrsa -out root-key.key 1024 ) else ( echo 私钥root-key.key已经存在 ) echo 2.创建根证书请求文件 if not exist root-req.csr ( echo 使用root-key.key私钥 创建一个根证书请求文件root-req.csr rem -new 产生一个新的CSR, 它会要用户输入创建CSR的一些必须的信息. rem 至于需要哪些信息,是在config文件里面定义好了的. rem -key 指明我们的私有密钥文件名.允许该文件的格式是PKCS#8. rem 如果-key没有被set, 那么就将根据config文件里的信息先产生一对新的RSA密钥 rem -keyform 指定输入的私有密钥文件的格式是DEM还是DER.DER格式采用ASN1的DER标准格式。 rem PEM格式就是base64编码格式. openssl req -new -out root-req.csr -key root-key.key -keyform PEM ) else ( echo 根证书请求文件root-req.csr已经存在 ) echo 3.自签根证书 if not exist root-cert.cer ( echo 使用root-req.csr证书和root-key.key私钥签发x509格式的自签根证书root-cert.cer rem -in 要处理的CSR的文件名称,只有-new和-newkey俩个option没有被set,本option才有效 rem -signkey 签名私钥 rem -days证书的有效日期 openssl x509 -req -in root-req.csr -out root-cert.cer -signkey root-key.key -CAcreateserial -days 3650 ) else ( echo 自签根证书root-cert.cer已经存在 ) echo 4.导出p12格式根证书 if not exist root.p12 ( openssl pkcs12 -export -clcerts -in root-cert.cer -inkey root-key.key -out root.p12 ) else ( echo p12格式根证书root-cert.cer已经存在 ) echo 生成root.jks文件 set password= echo 输入jks的密码,密码至少必须为6个字符: set /p password= echo 密码是 %password% 稍后提示 信任这个认证?输入 y 即可 echo. if exist root.jks ( del root.jks ) keytool -import -v -trustcacerts -storepass %password% -alias root -file root-cert.cer -keystore root.jks echo 已生成root.jks文件 echo. echo 制作服务端证书 call :sub server echo. echo 制作客户端证书 call :sub client goto :end :sub echo 1.创建私钥 if not exist %1-key.key ( openssl genrsa -out %1-key.key 1024 ) else ( echo 私钥%1-key.key已经存在 ) echo 2.创建证书请求文件 if not exist %1-req.csr ( openssl req -new -out %1-req.csr -key %1-key.key ) else ( echo 证书请求文件%1-req.csr已经存在 ) echo 3.自签证书 if not exist %1-cert.cer ( openssl x509 -req -in %1-req.csr -out %1-cert.cer -signkey %1-key.key -CA root-cert.cer -CAkey root-key.key -CAcreateserial -days 3650 ) else ( echo 自签证书%1-cert.cer已经存在 ) echo 4.导出p12格式证书 if not exist %1.p12 ( openssl pkcs12 -export -clcerts -in %1-cert.cer -inkey %1-key.key -out %1.p12 ) else ( echo 自签证书%1.p12已经存在 ) goto :eof :end echo. echo 整理归档生成的证书文件 if not exist root ( md root ) move root.p12 root/root.p12 move root-cert.cer root/root-cert.cer move root-key.key root/root-key.key move root-req.csr root/root-req.csr move root-cert.srl root/root-cert.srl move root.jks root/root.jks if not exist server ( md server ) move server.p12 server/server.p12 move server-cert.cer server/server-cert.cer move server-key.key server/server-key.key move server-req.csr server/server-req.csr if not exist client ( md client ) move client.p12 client/client.p12 move client-cert.cer client/client-cert.cer move client-key.key client/client-key.key move client-req.csr client/client-req.csr if not exist 证书 ( md 证书 ) move root 证书/root move client 证书/client move server 证书/server echo 整理完毕 pause
主要功能:
建立CA证书
签发Server端证书
签发Client端证书
用keytool生成tomcat使用的jks文件
参考:http://blog.csdn.net/sunyujia/article/details/3017405