もし孫正義に 「財布を落としたから金を貸してくれ、後日10倍にして返すから」 と言われたら、それが本物の孫正義だったら、全財産貸すかもしれない。けどどうやって本人か、なりすましか見分けることが出来るんだろうか? 容姿が全く似ても似つかないならすぐにわかるが、大体合ってるような気がするが、確信が持てない時は、免許証や、保険証等、社会的に十分裏付けがある身分証明書を見せてもらうことが必要となる。 同様にネットで液晶テレビやら、掃除機やらを買うときに、例えば、アマゾンで買おうとした時に、本当にそこがアマゾンで、クレジットカードやその他もろもろの個人情報を入力しても問題ないだけの信用と秘匿性があるかどうかを知る手段として、サイトの身分証明書が必要となる。 ブラウザのURLが表示されてる所の鍵の所をクリックすると、この様な表示にたどり着くことが出来る。 アマゾンの証明書 ![]() 詳しくは、日本VeriSignのSSLまとめでわかる。 暗号化と証明書 TLS / SSL(Transport Layer Security / Secure Socket Layer)とは、クレジットカード番号や、個人情報を暗号化してやり取りすることで、第三者空の 盗聴 を防ぎ、それに加えて、サイトの身分証明書として、なりすましを防ぐ為にも使うことが出来る技術である。 初めてOPENSSLを使ってみる [root@centos ~]# openssl -h openssl:Error: '-h' is an invalid command. Standard commands asn1parse ca ciphers cms crl crl2pkcs7 dgst dh dhparam dsa dsaparam enc engine errstr gendh gendsa genpkey genrsa nseq ocsp passwd pkcs12 pkcs7 pkcs8 pkey pkeyparam pkeyutl prime rand req rsa rsautl s_client s_server s_time sess_id smime speed spkac ts verify version x509 どんなコマンドがあるか表示させてみると、標準コマンドという呪文が並ぶ。種類もドラゴンクエストの呪文並である。 とりあえずバージョンを調べてみる。 [root@centos ~]# openssl version OpenSSL 1.0.0-fips 29 Mar 2010 ハッシュを表示させてみるハッシュ関数 (ハッシュかんすう、hash function) とは、あるデータが与えられた場合にそのデータを代表する数値を得る操作、または、その様な数値を得るための関数のこと。ハッシュ関数から得られた数値のことをハッシュ値または単にハッシュという。ハッシュ関数は主に検索の高速化やデータ比較処理の高速化、さらには改竄の検出に使われる。 dgst コマンドに続くオプションは以下の通り。 [root@centos ~]# openssl dgst -h
unknown option '-h' options are -c to output the digest with separating colons -r to output the digest in coreutils format -d to output debug info -hex output as hex dump -binary output in binary form -sign file sign digest using private key in file -verify file verify a signature using public key in file -prverify file verify a signature using private key in file -keyform arg key file format (PEM or ENGINE) -out filename output to filename rather than stdout -signature file signature to verify -sigopt nm:v signature parameter -hmac key create hashed MAC with key -mac algorithm create MAC (not neccessarily HMAC) -macopt nm:v MAC algorithm parameters or key -engine e use engine e, possibly a hardware device. -md4 to use the md4 message digest algorithm -md5 to use the md5 message digest algorithm -ripemd160 to use the ripemd160 message digest algorithm -sha to use the sha message digest algorithm -sha1 to use the sha1 message digest algorithm -sha224 to use the sha224 message digest algorithm -sha256 to use the sha256 message digest algorithm -sha384 to use the sha384 message digest algorithm -sha512 to use the sha512 message digest algorithm -whirlpool to use the whirlpool message digest algorithm [root@centos html]# openssl dgst -md5 index.html
MD5(index.html)= 4261935afc2e1f9fd9f0a151df6413f0 [root@centos ]# openssl dgst -sha1 index.html
SHA1(index.html)= d57ddd619c7e9901334ea2336b9268459b64b9ae MD5では128bitでsha1では160bitの数値が出力された。 こんな写真でもやってみる [root@centos]# openssl dgst -sha1 tangocho.jpg SHA1(tangocho.jpg)= 4b3c0efa81193504f8b5ae85e04515962e1a2a7c 数値化されてるものなら動画でも音声でもなんでも、ハッシュ出来るようだ。 160bitというと、2の160乗だから計算してみると・・・ [root@centos ]# bc 2^160 1461501637330902918203684832716283019655932542976 1極4615載1637正3309澗02・・・・・ これだけ見ると、とてつもない数で地球上にある、どんな文章、データでも一意のハッシュ値になりそうな気がするが、例えば簡単な単語2000種を100使った文章の組み合わせの数は [root@centos ]# bc 2000^100 12676506002282294014967032053760000000000000000000000000000000000000\ 00000000000000000000000000000000000000000000000000000000000000000000\ 00000000000000000000000000000000000000000000000000000000000000000000\ 00000000000000000000000000000000000000000000000000000000000000000000\ 00000000000000000000000000000000000000000000000000000000000 10の330乗を超えることになる。 意図的に、同じハッシュ値の違うファイルを作ろうと思えばできてしまうんじゃないかと思ったら、やはり、sha1だったら、総当り回数で10の80乗必要な所を、10の69乗の計算で破られてしまうそうだ。 しかしながら、それが意味のある文章だったり、コードだったりする可能性は低いので意味のある改ざんはされにくいようだ。 必要なら、512bitも使用出来る。これなら10の154乗通り以上の組み合わせがあるので、同じハッシュ値を持つデータを見つけるのも困難であろう。 [root@centos html]# openssl dgst -sha512 tangocho.jpg SHA512(tangocho.jpg)= 17ece22ea13ad7c8485f35be5a157f04e26f38780d6c8ff44bf61c510677d9c258bc5c29f631a53119edba65aa00f16e982713ae4fa0 eb6509e03d1b6d1ca8bd
鍵を作ってみた genrsaに続くオプションは以下の通り [root@centos ~]# openssl genrsa -h
usage: genrsa [args] [numbits] -des encrypt the generated key with DES in cbc mode -des3 encrypt the generated key with DES in ede cbc mode (168 bit key) -seed encrypt PEM output with cbc seed -aes128, -aes192, -aes256 encrypt PEM output with cbc aes -camellia128, -camellia192, -camellia256 encrypt PEM output with cbc camellia -out file output the key to 'file -passout arg output file pass phrase source -f4 use F4 (0x10001) for the E value -3 use 3 for the E value -engine e use engine e, possibly a hardware device. -rand file:file:... load the file (or the files in the directory) into the random number generator openssl genrsaだけで実行すると・・・ [root@centos ~]# openssl genrsa
Generating RSA private key, 512 bit long modulus .++++++++++++ ..++++++++++++ e is 65537 (0x10001) -----BEGIN RSA PRIVATE KEY----- MIIBOgIBAAJBAKueBnfqV+ztXd40/+nM93P0kKCoxmV32B8IGZa0oDKSRxDHFQIX r4LW4qm5GfX3H/v/+LpHz9AmVTiKhX9udzkCAwEAAQJAasmuJLgBdkBNhJNeGtGb uN+Zjx72NPVZ2hVkNs62UECIcas4akBt0P+8mm66E+3xD/C90TaMskPWCw1c5h9c AQIhANPOmkpHkVAFDTTw76dLZLVGd18RyQ7zdIseOMV7wUlZAiEAz2y9NqszyN7a yzZ+VvPxur/EbVnsLBpv/5hGzWBVAOECIQC5X6Lty8FENYefhBdbZcLYXLnw/NHp Qe4ILAUn4e4C0QIgJ42TsyYIVOhyDkgQ5WHVGYhR24xHJc3Qx4txDN6utuECIHMB p29e2HMLmSEIgoN+RutGq/jKjihgTIuxZzqf6VSv -----END RSA PRIVATE KEY----- 標準出力(コンソール画面)にそのまま512bit長のRSA秘密鍵が出力される。これは、PEM (Privacy Enhanced Mail)形式である。 鍵自体に暗号をかけて、鍵自体をより安全に管理できるようにするには・・・ (パソコンとかサーバーから直接、鍵を盗難された場合の対策。物理的にハードディスクを盗まれた場合とか、他のデーターの盗難対策と一緒) [root@centos ~]# openssl genrsa -des3 ←暗号方式はその他にも aesやcamellia方式を選べる。
Generating RSA private key, 512 bit long modulus .....++++++++++++ .....++++++++++++ e is 65537 (0x10001) Enter pass phrase: ←鍵を守るためのパスフレーズを入力 Verifying - Enter pass phrase: -----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,FAA91BB525300B4C r1v09fkIKtSHXt1PGSAYX+LNsA5HSuwxOPDkgawlwN4M1mRmlE81tvaU0ZVT7AlL +ya1mEjdWJNX6qd4j4UB0SVYd+Vk6YS3/TrTcj14qu2ms3qpuMPkjApVPPKZJvBU xYg8bxC3FxmZC2u+A7cua3u0ReeuPy4yvEYLyU5GhdbmdKyGEI4JGbVBB3xA0zuQ /YUrqM9o1Q7M7d/JXT8m/PLn2PEB668ff623qvU7HY3ffFvztzMSiYEdrrYY+YHV 5q90ouqVD0+0kU0E9oW+MNG6fNFB7IMFbCOZVS0pFHmbi8tEqG4lx+0XIDkzHkDs LHCoHZm89onstSDrQVCUVOoRW2HBgG8Fd2vfXX071QIeFiR6MD9bNOQIgZooxvqa oSG3Yud6vEy8/rgs2VMeLrLte1zE9Mm33BZ3A1I0W+QMCsLMM3eLnw== -----END RSA PRIVATE KEY----- 次に
[root@centos ~]# openssl genrsa -out server.key 1024 ←鍵ファイルの名前は任意
Generating RSA private key, 1024 bit long modulus ...................++++++ ..++++++ e is 65537 (0x10001) 128bit長でも作れる。 [root@centos ~]# openssl genrsa -out short_server.key 128 rsa -textで鍵の内容を見れるので鍵の中身を見てみる。pem→text [root@centos ~]# openssl rsa -text -noout -in short_server.key ←-nooutで鍵を出力しないPrivate-Key: (128 bit) modulus: 00:9c:1d:e4:24:8a:28:f0:a9:61:31:a2:13:51:a0: 9a:f1 publicExponent: 65537 (0x10001) privateExponent: 00:95:1c:67:c0:0e:c6:33:b5:d6:4f:65:75:d4:97: a0:01 prime1: 14665429052367331697 (0xcb861205b90c6171) ←この二つの素数 prime1×prime2がmodulusの所の数になる prime2: 14149928412916683137 (0xc45ea4ec091b1181) exponent1: 11876318966328812657 (0xa4d1292d2b436471) exponent2: 7843486875878317057 (0x6cd9a9c1cd2a5c01) coefficient: 1978966876867526265 (0x1b76b3e3b5f3fe79) 上の秘密鍵から、公開鍵を生成して、中身を見てみる。 [root@centos ~]# openssl rsa -pubout -in short_server.key -out pub_short_server.key writing RSA key
[root@centos ~]# openssl rsa -text -pubin -noout -in pub_short_server.key
Public-Key: (128 bit) Modulus: 00:9c:1d:e4:24:8a:28:f0:a9:61:31:a2:13:51:a0: 9a:f1 Exponent: 65537 (0x10001) modulusのところの128bit(ここでは)の数は同じであるが、公開鍵には二つの素因数が書かれてない。 それはこの鍵が鍵として機能するのは、十分大きな数の素因数分解が困難であるという性質が使われているからである。 それは、公開鍵から秘密鍵を作り出すのは非常に困難であるし、十分な鍵の長さがあれば事実上不可能であるからだ。 しかし具体的な仕組みは、私にはまだわからない。 一体どのくらいの鍵の長さが十分な長さといえるかというと、 上の記事によると、768bitの数の素因数分解が成功したとのことなので、1024bitも近い将来解読される懸念が出てきたので、 より安全を期すには、2048bitの秘密鍵を生成することが必要となるだろう。 おまけ どこまで短い鍵を作れるかというと、30bit以下だと怒られてしまう。試しに、32bitで鍵を作って、textで内容を表示させてみると prime1×prime2 = modulus になってるのがはっきりわかる [root@centos ~]# openssl genrsa 32 | openssl rsa -text Generating RSA private key, 32 bit long modulus .+++++++++++++++++++++++++++ .+++++++++++++++++++++++++++ e is 65537 (0x10001) Private-Key: (32 bit) modulus: 3294057757 (0xc457551d) publicExponent: 65537 (0x10001) privateExponent: 716719153 (0x2ab84431) prime1: 61343 (0xef9f) prime2: 53699 (0xd1c3) exponent1: 60567 (0xec97) exponent2: 11947 (0x2eab) coefficient: 45927 (0xb367) writing RSA key -----BEGIN RSA PRIVATE KEY----- MC0CAQACBQDEV1UdAgMBAAECBCq4RDECAwDvnwIDANHDAgMA7JcCAi6rAgMAs2c= -----END RSA PRIVATE KEY----- 自己署名証明書を発行する とりあえず、自分で使う分に通信を暗号化するために、自己署名証明書を発行してみる。 [root@centos ~]# cd /etc/pki/tls/certs/ ←鍵と証明書を置いておくディレクトリに移動
[root@centos certs]# openssl genrsa -out server.key 1024 ←鍵を暗号化しないで、1024bit長で生成
[root@centos certs]# openssl req -new -x509 -nodes -sha1 -days 3650 -key server.key -out server.crt
apache(mod_ssl)の方の設定をする。 [root@centos certs]# cd /etc/httpd/conf.d ←場所を移動 [root@centos conf.d]# vim ssl.conf ←apacheが起動時に一緒に読み込まれるsslの設定ファイルを編集 100 # Server Certificate: 101 # Point SSLCertificateFile at a PEM encoded certificate. If 102 # the certificate is encrypted, then you will be prompted for a 103 # pass phrase. Note that a kill -HUP will prompt again. A new 104 # certificate can be generated using the genkey(1) command. 105 SSLCertificateFile /etc/pki/tls/certs/server.crt ←証明書の場所を指定 106 107 # Server Private Key: 108 # If the key is not combined with the certificate, use this 109 # directive to point at the key file. Keep in mind that if 110 # you've both a RSA and a DSA private key you can configure 111 # both in parallel (to also allow the use of DSA ciphers, etc.) 112 SSLCertificateKeyFile /etc/pki/tls/certs/server.key ←秘密鍵の場所を指定 再起動 [root@centos conf.d]# service httpd restart httpd を停止中: [ OK ] httpd を起動中: [ OK ] [root@centos conf.d]# サーバーにhttpsでアクセスしてみると、このサイトの証明書は信頼できませんと、表示されてしまうが、オレオレ証明書だからしょうがないのでそのまま続行すると、サイトは表示されるが、アドレスバーは次のようになる。(chromeの場合) ![]() 証明書の情報を見てみる。 ![]() これらはwebブラウザで、mysql等データベースを操作するときや、wordpressにログインする時などに必須な設定になる。 |
さくらのVPS >