opensslコマンドでgmail

■はじめに
 良い子は真似しないように。
 ロックがかかる前にやめておくべき。ロックの解除は自己責任。
 分からない場合は、初めからやるべきでは無い操作である事に注意。

■目的
 postfixやsendmailによる転送がよいか、fetchmail等の転送専用のユーティリティを使うか。
 直接コマンドを叩いて温度をみたい。

■CA証明書、クライアント証明書等、外部サーバはひとつではないはず。。。
 よって、メールサーバに直接設定するよりは、fetchmail等で取りにいく方がよさそう。

--------------------行った作業-----------------------

■smtpsポートからルート証明書を確認します。

 ※違うポートは試さない方が無難です。ロックされてしまった場合は画像文字の入力が必要です。
  コマンドの実行結果にも出てきます。(はい、私はロックされました。。。)

 http://www.google.com/accounts/DisplayUnlockCaptcha

 ※「Verify return code: 20」と出るのはルート証明書をopensslコマンドに与えていないからです。

$ grep 465 /etc/services
ssmtp 465/tcp smtps # SMTP over SSL

$ openssl s_client -connect smtp.gmail.com:465 -showcerts

CONNECTED(00000003)
depth=1 /C=US/O=Google Inc/CN=Google Internet Authority
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=smtp.gmail.com
i:/C=US/O=Google Inc/CN=Google Internet Authority
-----BEGIN CERTIFICATE-----
〜省略〜
-----END CERTIFICATE-----
1 s:/C=US/O=Google Inc/CN=Google Internet Authority
i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
-----BEGIN CERTIFICATE-----
〜省略〜
-----END CERTIFICATE-----
---
Server certificate
subject=/C=US/ST=California/L=Mountain View/O=Google Inc/CN=smtp.gmail.com
issuer=/C=US/O=Google Inc/CN=Google Internet Authority
---
No client certificate CA names sent
---
SSL handshake has read 1715 bytes and written 307 bytes
---
New, TLSv1/SSLv3, Cipher is RC4-SHA
Server public key is 1024 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
Protocol : TLSv1
Cipher : RC4-SHA
Session-ID: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Session-ID-ctx:
Master-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Key-Arg : None
Start Time: 1318705089
Timeout : 300 (sec)
Verify return code: 20 (unable to get local issuer certificate)
---
220 mx.google.com ESMTP xxxxxxxxxxxxxxx.xx
quit
221 2.0.0 closing connection xxxxxxxxxxxxxxx.xx
read:errno=0

■CAルート証明書を取り込み再実行。
 ※CA証明書の取り込み方法は2種類。クライアント証明書は無い。

☆目的のルート証明書のみを取り込み、pemにフォーマット変換する。

http://www016.upp.so-net.ne.jp/kanemiki/windows/stunnel.html

$ openssl s_client -connect smtp.gmail.com:465 -CAfile cacerts.pem
〜省略〜
Verify return code: 0 (ok)
---
220 mx.google.com ESMTP xxxxxxxxxxxxxxx.xx
quit
221 2.0.0 closing connection xxxxxxxxxxxxxxx.xx
read:errno=0


☆root証明書をすべて取り込み、ディレクトリを指定。

https://www.verisign.com/support/roots.html

$ sudo apt-get install zip
$ mkdir certs;cd certs
$ unzip ../roots.zip
$ openssl s_client -connect smtp.gmail.com:465 -CApath ~/cacerts
〜省略〜
Verify return code: 0 (ok)
---
220 mx.google.com ESMTP xxxxxxxxxxxxxxx.xx
quit
221 2.0.0 closing connection xxxxxxxxxxxxxxx.xx
read:errno=0

■base64形式のパスワードを作成。
 ※UNAME=password,UPASS=passwaordとした場合のエンコードです。

$ UPASS=password
$ UNAME=`whoami`
$ perl -MMIME::Base64 -e 'print encode_base64("'${UNAME}\0${UNAME}\0${UPASS}'");'
cGFzc3dvcmQwcGFzc3dvcmQwcGFzc3dvcmQ=

■RCPTコマンドの「R」が認識しない場合を防ぐには、「-quiet」オプションを。
 「.」によるEOFを認識しない場合、LFをCRLFに変換する必要があるので「-crlf」オプションを付ける。

$ openssl s_client -crlf -connect -starttls smtp -connect smtp.gmail.com:587
depth=1 /C=US/O=Google Inc/CN=Google Internet Authority
verify error:num=20:unable to get local issuer certificate
verify return:0
250 ENHANCEDSTATUSCODES
EHLO localhost
250-mx.google.com at your service, [122.25.5.191]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250 ENHANCEDSTATUSCODES
AUTH PLAIN cGFzc3dvcmQwcGFzc3dvcmQwcGFzc3dvcmQ=
235 2.7.0 Accepted
MAIL FROM:<********@gmail.com>
250 2.1.0 OK xxxxxxxxxxxxxxx.x
RCPT TO:<********@gmail.com>
250 2.1.5 OK xxxxxxxxxxxxxxx.x
DATA
354 Go ahead xxxxxxxxxxxxxxx.x
Subject: You Have New mail

test mail from opwnssl command
Thank you.
.
250 2.0.0 OK 1318704821 xxxxxxxxxxxxxxx.x
QUIT
221 2.0.0 closing connection xxxxxxxxxxxxxxx.x
read:errno=0

■popで取りに行く。


$ openssl s_client -connect pop.gmail.com:995 -CApath ~/certsCONNECTED(00000003)
depth=2 /C=US/O=Equifax/OU=Equifax Secure Certificate Authority
verify return:1
depth=1 /C=US/O=Google Inc/CN=Google Internet Authority
verify return:1
depth=0 /C=US/ST=California/L=Mountain View/O=Google Inc/CN=pop.gmail.com
verify return:1
---
Certificate chain
0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=pop.gmail.com
i:/C=US/O=Google Inc/CN=Google Internet Authority
1 s:/C=US/O=Google Inc/CN=Google Internet Authority
i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
---
Server certificate
-----BEGIN CERTIFICATE-----
〜省略〜
-----END CERTIFICATE-----
subject=/C=US/ST=California/L=Mountain View/O=Google Inc/CN=pop.gmail.com
issuer=/C=US/O=Google Inc/CN=Google Internet Authority
---
No client certificate CA names sent
---
SSL handshake has read 1714 bytes and written 307 bytes
---
New, TLSv1/SSLv3, Cipher is RC4-SHA
Server public key is 1024 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
Protocol : TLSv1
Cipher : RC4-SHA
〜省略〜
Key-Arg : None
Start Time: 1318711628
Timeout : 300 (sec)
Verify return code: 0 (ok)
---
+OK Gpop ready for requests from 122.25.5.191 xxxxxxxxxxxxxxx.x
USER username
+OK send PASS
PASS password
+OK Welcome.
LIST
〜省略〜
20876 3408
.
QUIT

■おまけ
 ※IMAPにはLOGINはできるが、その後何も出来なかった。
 ※webで良く見かけるimapコマンドの前に必ず「1 」を添える。

$ openssl s_client -connect pop.gmail.com:993 -CApath ~/certs
CONNECTED(00000003)
depth=2 /C=US/O=Equifax/OU=Equifax Secure Certificate Authority
verify return:1
depth=1 /C=US/O=Google Inc/CN=Google Internet Authority
verify return:1
depth=0 /C=US/ST=California/L=Mountain View/O=Google Inc/CN=imap.gmail.com
verify return:1
---
Certificate chain
0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=imap.gmail.com
i:/C=US/O=Google Inc/CN=Google Internet Authority
1 s:/C=US/O=Google Inc/CN=Google Internet Authority
i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
---
Server certificate
-----BEGIN CERTIFICATE-----
〜省略〜
-----END CERTIFICATE-----
subject=/C=US/ST=California/L=Mountain View/O=Google Inc/CN=imap.gmail.com
issuer=/C=US/O=Google Inc/CN=Google Internet Authority
---
No client certificate CA names sent
---
SSL handshake has read 1715 bytes and written 307 bytes
---
New, TLSv1/SSLv3, Cipher is RC4-SHA
Server public key is 1024 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
Protocol : TLSv1
Cipher : RC4-SHA
〜省略〜
Key-Arg : None
Start Time: 1318713143
Timeout : 300 (sec)
Verify return code: 0 (ok)
---
* OK Gimap ready for requests from 122.25.5.191 xxxxxxxxxxxxxxx.x
1 LOGIN username password
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE
1 OK ********@gmail.com username . authenticated (Success)
1 LOGOUT
〜ここで応答が無くなる〜
^C
〜Ctrl+Cで強制終了〜





☆参考までに。

$ telnet localhost 143
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
*1325176279* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE STARTTLS AUTH=PLAIN] Dovecot ready.
1 LOGIN username password
1 OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE SORT SORT=DISPLAY THREAD=REFERENCES
THREAD=REFS MULTIAPPEND UNSELECT IDLE CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC
ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS] Logged in
1 LOGOUT
*1325176280* BYE Logging out
1 OK Logout completed.
Connection closed by foreign host.