opensslコマンドによる公開鍵暗号の利用 ===================================== 秘密鍵の作成 (privkey.pem) ------------ openssl genrsa -des3 -out privkey.pem 1024 パスワードかけない場合は openssl genrsa -out privkey.pem 1024 秘密鍵(privkey.pem)は*絶対*に他人に盗まれないように、厳重管理すること! 電子証明要求書の作成 (csr.pem) -------------------- openssl req -new -days 365 -key privkey.pem -out csr.pem Enter PEM pass phrase: 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]:JP State or Province Name (full name) [Some-State]:Osaka Locality Name (eg, city) []:Ibaraki Organization Name (eg, company) [Internet Widgits Pty Ltd]:M.Y.H Organizational Unit Name (eg, section) []:CA section Common Name (eg, YOUR name) []:Masahiko Ito Email Address []:m-ito@myh.foo.org Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: 電子証明書の作成 (csr.pem -> cert.pem : 自己証明書) ---------------- openssl req -x509 -in csr.pem -key privkey.pem -out cert.pem 上記2つの操作を同時にする場合 (privkey.pem, cert.pem) ------------------------------ openssl req -x509 -new -key privkey.pem -out cert.pem -days 365 自己署名した証明書をCAにより署名しなおす場合 (cert.pem -> cert2.pem) -------------------------------------------- openssl x509 -in cert.pem -out cert2.pem -CA cacert.pem -CAkey caprivkey.pem -CAcreateserial -clrext 上記で作成した証明書のベリファイ -------------------------------- openssl verify -CAfile cacert.pem cert.pem CAで電子証明書を作成する場合 (csr.pem -> cert.pem) ---------------------------- openssl x509 -req -in csr.pem -out cert.pem -CA cacert.pem -CAkey caprivkey.pem -CAcreateserial -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 上記の方法で作成した電子証明書のCRLはどうやって作ればよいの か不明...なので正式にはCA局を構築して、以下のコマンドで電子 証明書を作成することになるような...。 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ openssl ca -policy policy_anything -days 365 -infiles csr.pem -out cert.pem 証明書廃棄作業 -------------- openssl ca -revoke cert.pem 廃棄リスト(CRL)作成作業 (crl.pem) ----------------------- openssl ca -gencrl -out crl.pem 秘密鍵を使っての署名 (plain.txt -> sign.txt by privkey.pem) -------------------- openssl rsautl -sign -in plain.txt -inkey privkey.pem -out sign.txt 署名の検証&復号 (sign.txt -> plain.txt by cert.pem) --------------- openssl rsautl -verify -in sign.txt -inkey cert.pem -certin -out plain.txt 電子証明書(≒公開鍵)を使っての暗号化 (plain.txt -> encrypt.txt by cert.pem) ------------------------------------ openssl rsautl -encrypt -in plain.txt -inkey cert.pem -certin -out encrypt.txt 暗号の復号 (plain.txt -> encrypt.txt by privkey.pem) ---------- openssl rsautl -decrypt -in encrypt.txt -inkey privkey.pem -out plain.txt -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 公開鍵暗号方式で署名及び暗号化できる平文は使用する鍵長より短 いデータでなければならない。よって、実際に文書データを署名及 び暗号化してやり取りする場合は共通鍵暗号方式と組み合わせて使 う。 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ opensslコマンドによる共通鍵暗号の利用 ===================================== 暗号化 (plain.txt -> encrypt.txt by PASSWORD) ------ openssl des3 -e -base64 -k PASSWORD -in plain.txt -out encrypt.txt 復号化 (encrypt.txt -> plain.txt by PASSWORD) ------ openssl des3 -d -base64 -k PASSWORD -in encrypt.txt -out plain.txt 公開鍵暗号&共通鍵暗号を組み合わせた署名 ======================================== 本文からダイジェストの作成 (plain.txt -> digest.txt) -------------------------- openssl dgst -sha1 -out digest.txt sign.txt by privkey.pem) -------------------- openssl rsautl -sign -in digest.txt -inkey privkey.pem -out sign.txt 署名の検証&復号 (sign.txt -> digest.txt by cert.pem) --------------- openssl rsautl -verify -in sign.txt -inkey cert.pem -certin -out digest-sign.txt openssl dgst -sha1 -out digest-plain.txt encrypt.txt by PASSWORD) ------------------------ openssl des3 -e -base64 -k PASSWORD -in plain.txt -out encrypt.txt 共通鍵暗号で使った鍵(パスワード)を公開鍵暗号で暗号化 (PASSWORD -> password.txt by cert.pem) ---------------------------------------------------- echo PASSWORD | openssl rsautl -encrypt -inkey cert.pem -certin -out password.txt 共通鍵暗号の鍵(パスワード)を公開鍵暗号の秘密鍵で復号化 (password.txt -> PASSWORD by privkey.pem) ------------------------------------------------------ key=`openssl rsautl -decrypt -in password.txt -inkey privkey.pem` 本文を共通鍵暗号で復号化 (encrypt.txt -> plain.txt by PASSWORD) ------------------------ openssl des3 -d -base64 -k ${key} -in encrypt.txt -out plain.txt smimeの利用 =========== smimeによる署名 (plain.txt -> sign.txt by cert.pem, privkey.pem) --------------- openssl smime -sign -in plain.txt -signer cert.pem -inkey privkey.pem -out sign.txt smimeによる署名検証 (sign.txt -> plain.txt by cacert.pem) ------------------- openssl smime -verify -in sign.txt -CAfile cacert.pem -out plain.txt smimeによる暗号化 (plain.txt -> encrypt.txt by cert.pem) ----------------- openssl smime -encrypt -des3 -in plain.txt -out encrypt.txt cert.pem smimeによる暗号復号化 (encrypt.txt -> plain.txt by cert.pem, privkey.pem) --------------------- openssl smime -decrypt -recip cert.pem -inkey privkey.pem -in encrypt.txt -out plain.txt おまけ (base64エンコード&デコード) ----------------------------------- mimencode binary >binary.txt mimencode -u binary.txt >binary