Showing posts with label ssl. Show all posts
Showing posts with label ssl. Show all posts

Wednesday, July 27, 2011

SPDY and Good SSL

‹prev | My Chain | next›

Now that I am a Certificate Authority (CA), I am ready to revisit some SSL timing woes from a few nights back. Specifically, I was seeing 700ms+ for SSL negotiation. Some of that was due to the imposed 100ms round trip time (RTT) on my little network, but that was still high.

To see if I can eliminate some of that, I am going to replace my faux SSL certificate with a real one signed by my new CA. First, I need to generate my private key and an associated certificate request (which will be sent to my CA):
➜  ~  openssl genrsa -out spdy.key 1024           
Generating RSA private key, 1024 bit long modulus
............................++++++
....................++++++
e is 65537 (0x10001)
➜ ~ openssl req -new -key spdy.key -out spdy.csr
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) [US]:
State or Province Name (full name) [Maryland]:
Locality Name (eg, city) []:
Organization Name (eg, company) [EEE Computes, LLC]:
Organizational Unit Name (eg, section) []:
Common Name (eg, YOUR name) []:spdy.local
Email Address []:spdy.local@eeecomputes.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Now, with my CA hat on, I sign the certificate request:
➜  ~  openssl ca -out ./CA/newcerts/spdy.crt -in spdy.csr              
Using configuration from /home/cstrom/local/ssl/openssl.cnf
Enter pass phrase for ./CA/private/cakey.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 2 (0x2)
Validity
Not Before: Jul 28 00:18:46 2011 GMT
Not After : Jul 27 00:18:46 2012 GMT
Subject:
countryName = US
stateOrProvinceName = Maryland
organizationName = EEE Computes, LLC
commonName = spdy.local
emailAddress = spdy.local@eeecomputes.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
BF:BB:1D:1F:6A:04:5D:48:79:8D:B6:E1:A3:88:40:8E:4B:DB:9D:77
X509v3 Authority Key Identifier:
keyid:3D:1B:A2:E4:94:D4:0C:D0:3B:D5:BC:78:B9:F7:97:40:73:C8:59:A2

Certificate is to be certified until Jul 27 00:18:46 2012 GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
After copying my private key and signed certificate to the spdy.local VM, I access the SPDY server to find that the new certificate is, indeed, working:



Looking at the networking tab of Chrome's developer tools (the only thing available to me since Speed Tracer is broken), I see that SSL negotiation is cut down:



Much better. It is still 400ms, but on a 100ms RTT network that may be the best I can hope for.

Before calling it a night, I try out the SPDY server push implementation. Observing in the network tab again, I see:



Hrm... The images are certainly pushed quickly, but for some reason the CSS and JS are taking longer in SPDY server push than in normal requests.

Overall, it saves about 300ms. That is nothing to sneeze at, but what is going on with the CSS and Javascript transfer times? I try to have a look at the SPDY tab in about:net-internals, but about:net-internals seems hopelessly broken in my current Chrome (14.0.835.0 dev):



Ugh. Well, I will leave that as mystery for tomorrow. For the time being, I am going to get back to finishing off SPDY Book.


Day #84

Tuesday, July 26, 2011

How Root Certificate Authorities Work (more or less)

‹prev | My Chain | next›

Up tonight I do something that I should have done a long time ago: set myself up as an SSL Certificate Authority (CA). Since SPDY is run over SSL, it makes sense to simulate as closely as possible real SSL. For the most part in SPDY Book I can get close enough with localhost certificates. Every now and then, I need more.

I could certainly buy a legitimate certificate, but I would worry that I would miss something and need to buy more and more to cover cases that I had not thought of in the first place. If I set myself up as a CA and tell my browser that I trust myself as a CA, then I can make myself legitimate testing certificates any time I need them.

This primarily involves the use of openssl's ca command. The openssl ca command is a bit ugly and hard to use. I am not making this up, it says so in its own documentation:
The ca command is quirky and at times downright unfriendly.
It was originally intended for reporting with limited ability to perform CA-like functions. As people have "mis-used" it for CA work, features got added. The result is, unfriendly.

I am more or less following along with Marcus Redivo's HOWTO. I think that guide will just work for normal installs, but I am using my locally installed ($HOME/local) openssl with NPN support. In many ways, that makes like easier for me because I can edit $HOME/local/ssl/openssl.cnf without fear of messing up my entire system (Marcus edits local configs and forces ca to use them via command line switches).

In my $HOME/local/ssl/openssl.cnf config file, I change the location were my CA work is going to take place:
#dir  = ./demoCA  # Where everything is kept
dir = ./CA
I am not going to be a demo CA, I am a real one, dammit!

Real CAs, at least those that use openssl ca need to do a little ground work before issuing commands wily-nily. The openssl ca command requires a flat file database to exist and a pointer to the next ID of the next certificate blessed by my awesome CA. The openssl.cnf configurations specifies these two pieces of information with:
database = $dir/index.txt # database index file.
#...
serial = $dir/serial # The current serial number
Remember how ca confessed to being really unfriendly? This is one of the ways: both of those files needs to be created before ca will work. No, ca cannot create them itself. The database file needs to simply exist. The serial pointer file needs to contain the ID of the first certificate that we will create. I will do all of this work in my home directory, so I create the CA directory that makes me a real CA:
mkdir CA
And then I initialize the flat file database and record index:
touch CA/index.txt
echo '01' > CA/serial
The last bit of setup involves organization. I need a couple of subdirectories to hold certificates that I, as a CA, sign, and a place to store my super-secret private keys:
mkdir CA/newcerts CA/private
NOTE: Were I a real boy, er... CA, I would need to guard that private directory with my life.

Before moving on, I make my life a bit easier by setting some useful defaults in openssl.cnf:
#countryName_default  = AU
countryName_default = US
#stateOrProvinceName_default = Some-State
stateOrProvinceName_default = Maryland
#0.organizationName_default = Internet Widgits Pty Ltd
0.organizationName_default = EEE Computes, LLC
With that, I can create my CA certificate:
openssl req -new -x509 \
-extensions v3_ca \
-keyout CA/private/cakey.pem \
-out CA/cacert.pem \
-days 3650
The keyout and out options store the private key and my CA certificate in the locations specified in the config file:
certificate = $dir/cacert.pem  # The CA certificate
private_key = $dir/private/cakey.pem# The private key
The openssl req writes the private key, and then requests some CA info:
Generating a 1024 bit RSA private key
........................++++++
....................................................++++++
writing new private key to 'CA/private/cakey.pem'
Enter PEM pass phrase:
Verifying - 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) [US]:
State or Province Name (full name) [Maryland]:
Locality Name (eg, city) []:
Organization Name (eg, company) [EEE Computes, LLC]:
Organizational Unit Name (eg, section) []:
Common Name (eg, YOUR name) []:EEE Computes Root CA
Email Address []:ca@eeecomputes.com
Most of that comes from the default values I put in the config. The common name is the name that will identify the CA certificate when it is installed on the browser.

To install the new root CA in Chrome, I go to Preferences // Under the Hood. There I Manage Certificates:



On the Root Certificates tab, I import my new, public cacert.pem:



And now I am official!



I am a real live CA now, but my CA still needs to issue a certificate.

My first customer generates a key and a certificate request to send my way:
openssl genrsa -out jaynestown.key 1024
openssl req -new -key jaynestown.key -out jaynestown.csr
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) [US]:
State or Province Name (full name) [Maryland]:
Locality Name (eg, city) []:
Organization Name (eg, company) [EEE Computes, LLC]:
Organizational Unit Name (eg, section) []:
Common Name (eg, YOUR name) []:jaynestown.local
Email Address []:chris@eeecomputes.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
My customer, who wants a certificate for the jaynestown.local server, sends me the jaynestown.csr certificate request. Once I have verified that the check has not bounced, I am ready to sign the jaynestown certificate. Naturally, I use the ugly ca command to do this:
openssl ca -out ./CA/newcerts/jaynestown.crt -in jaynestown.csr
Using configuration from /home/cstrom/local/ssl/openssl.cnf
Enter pass phrase for ./CA/private/cakey.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Jul 27 01:32:00 2011 GMT
Not After : Jul 26 01:32:00 2012 GMT
Subject:
countryName = US
stateOrProvinceName = Maryland
organizationName = EEE Computes, LLC
commonName = jaynestown.local
emailAddress = chris@eeecomputes.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
1C:21:62:29:B2:BB:84:26:4B:69:93:5D:E8:A2:82:A5:0C:EA:0C:00
X509v3 Authority Key Identifier:
keyid:3D:1B:A2:E4:94:D4:0C:D0:3B:D5:BC:78:B9:F7:97:40:73:C8:59:A2

Certificate is to be certified until Jul 26 01:32:00 2012 GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
I send the signed jaynestown.crt certificate back to my first customer, who might install it (and the private key) under Apache thusly:
        SSLCertificateFile    /etc/ssl/certs/jaynestown.crt
SSLCertificateKeyFile /etc/ssl/private/jaynestown.key
And, with that, my first customer has a legit certificate:



And the certificate is signed by me:



Nice! I have the feeling my new CA is going to have more than a few certificates to sign in the next couple of days.


Day #84

Monday, July 25, 2011

It is Really Easy to Do Dumb SSL Things

‹prev | My Chain | next›

In an ongoing attempt to figure out why my SSL negotiation is suddenly taking ~700ms instead of 250ms, tonight I try SSL in nginx. It is conceivable that the node.js SSL implementation is somehow causing problems (connections to other HTTPS sites work). So trying the same key + certificate with a reference SSL implementation seems like a good next step.

To enable SSL, I edit /etc/nginx/sites-enabled/default, uncommenting the HTTP server configuration:
# HTTPS server
#
server {
listen 443;
server_name localhost;

ssl on;
ssl_certificate cert.pem;
ssl_certificate_key cert.key;

ssl_session_timeout 5m;

ssl_protocols SSLv3 TLSv1;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
ssl_ciphers DES-CBC3-SHA;
ssl_prefer_server_ciphers on;

location / {
#root html;
root /var/www;
index index.html index.htm;
}
}
I change the root setting to re-use the root directory that already contains static HTML (/var/www). I also stick with a cipher that I know Wireshark can decode.

When I load the site, I find a normal SSL client hello from the browser:



And then another client hello from the browser:



Followed by several connection closures (FINs):



Until finally, I see a new connection open that successfully complete the handshake:



Since I am seeing the same behavior with nginx and my express-spdy implementation, it is time to admit that either there is something wrong with my certificate or my methodology.

Either way it's my mistake. But how to track it down?

Aha! Chrome's about:net-internals page has more than just a SPDY tab. The Events tab keeps track of all sorts of events. Maybe it logs SSL events?

It does!

Filtering events on my VM's www.local hostname, I see:



Hrm... there are three different groups of CONNECT_JOB events. Taking a closer look, I find a common name error:



In fact, that common name error happens twice:
t=1311649121151 [st=17]    -SOCKET_POOL_CONNECT_JOB_CONNECT  
--> net_error = -200 (CERT_COMMON_NAME_INVALID)
Arrgh! Now that I think about it, these SSL woes started right about the time that I started messing about with VMs and differently named hosts.

Chrome remembers that I OK'd the invalid certificate (even after I clear the cache). It seems as though Chrome is still trying to verify the SSL certificate like normal, but eventually giving up and falling back to the session exemption that I have already made for this invalid certificate.

The SSL certificate is for localhost. When I am testing against localhost (as I did for most of my book), I am not incurring this obscene negotiation penalty. When I am trying to use the localhost SSL certificate against a differently named server all sorts of badness arises.

Sigh.

Up tomorrow, I think I go back to generating and signing certificates.


Day #83

Sunday, July 24, 2011

Really Slow SSL

‹prev | My Chain | next›

Tonight, I try to take a closer look at the long delay in SSL connection establishment that I noticed last night. The delay is somewhat to be expected because I am testing with a 50ms delay in each direction, for a round trip time (RTT) of 100ms. I am still perplexed because it is taking a very long time to SSL negotiate (700-800ms). Also, this delay seems to be relatively recent. In previous runs, I saw less than 300ms.

To the Wiresharks!

My VM is restarted so there are no existing connections to the server. I also clear my browser cache (all time), which clears out the certificate as well. When I access the site, I see:



So far, so good. Nice, normal TCP connection establishment. SSL negotiation is off to a good start. It has take 200ms so far, but if the key exchange / SSL handshake come through quickly things might get done within 400ms.

Unfortunately, what happens next is:



And this I do not understand at all. The TCP/IP connection that was being used for SSL negotiation is closed, a new stream is established and SSL negotiation starts all over!



By the time the first bits of SSL data hits the wire, more than 600ms have elapsed. No wonder I am seeing such a big delay. But why?

My VMs no longer have Apache SSL installed on them. I will have to get that back tomorrow, but first I check out a normal SSL conversation—with github. Loading up my dashboard (after again clearing the cache) looks like:



Whoa. That's just weird. The browser is starting up three interweb tubes. It is as if Chrome knows that github is going to need three interweb tubes and opens them immediately after I request the page. That seems crazy because the SSL connection needs to be negotiated on all three tubes right away. Happily, it does conclude just fine, without any connection resets:



So the extremely slow SSL seems specific to my internal VM. Tomorrow, I will install SSL on an Apache VM to see if I can determine if this is specific to my node.js server or if it is a VM + network issue.

For now, I really need to finish off SPDY Book!

Day #83

Friday, May 6, 2011

Wireshark is Pretty Awesome for SPDY Sniffing

‹prev | My Chain | next›

I have worked through a mini-SSL obsession for the past week or so. It is distracting me from SPDY proper, so hopefully I can kick the habit soon. Still, with SPDY being built on top of SSL, it is an important thing for me know better than I did. And so the obsession continues for one more day.

Although I was able to get Wireshark decrypting SSL packets for requests to Apache, I could not do the same for SPDY packets to an NPN-enabled eventmachine (courtesy Carson McDonald). Acking through eventmachine, I find this line in ext/ssl.cpp:
       SSL_CTX_set_cipher_list (pCtx, "ALL:!ADH:!LOW:!EXP:!DES-CBC3-SHA:@STRENGTH");
WTF? The DES-CBC3-SHA cipher that is being explicitly excluded there is the one that I want to use (it worked last night and is among a handful of RSA key exchange ciphers).

For now, I change that line to:
       SSL_CTX_set_cipher_list (pCtx, "DES-CBC3-SHA");
And re-build the gemspec:
chris@chris-VirtualBox:~/repos/eventmachine$ rake gem
(in /home/chris/repos/eventmachine)
rake-compiler must be configured first to enable cross-compilation
rake-compiler must be configured first to enable cross-compilation
rake-compiler must be configured first to enable cross-compilation
rake-compiler must be configured first to enable cross-compilation
Successfully built RubyGem
Name: eventmachine
Version: 1.0.0.beta.3
File: eventmachine-1.0.0.beta.3.gem
mv eventmachine-1.0.0.beta.3.gem pkg/eventmachine-1.0.0.beta.3.gem
And build and install the gem itself:
chris@chris-VirtualBox:~/repos/eventmachine$ gem install pkg/eventmachine-1.0.0.beta.3.gem
Building native extensions. This could take a while...
Successfully installed eventmachine-1.0.0.beta.3
1 gem installed
Back in the SPDY gem, I am still using the NPN server from Carson's gist explicitly using the keys from yesterday:
     start_tls(:private_key_file => 'key.pem', :cert_chain_file => 'cert.pem', :verify_peer => false)
With that, I can start up Wireshark, the NPN server and access it with Chrome:
chris@chris-VirtualBox:~/repos/spdy/examples$ ruby ./npn_spdy_server.rb 
[:SPDY, :connection_closed]
[:SPDY_HEADERS, {"accept"=>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "accept-charset"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.3", "accept-encoding"=>"gzip,deflate,sdch", "accept-language"=>"en-US,en;q=0.8", "cache-control"=>"no-cache", "host"=>"localhost:10000", "method"=>"GET", "pragma"=>"no-cache", "scheme"=>"https", "url"=>"/", "user-agent"=>"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.16 Safari/534.30", "version"=>"HTTP/1.1"}]
[:SPDY, :sent, :SYN_REPLY]
[:SPDY, :sent, :DATA]
[:SPDY, :sent, :DATA_FIN]
[:SPDY, :connection_closed]
Back in Wireshark, I see three green (decrypted) packets:



Yay! I have finally achieved the magic combination of SPDY-enabled backend, viable cipher for SSL packet sniffing, and tool capable of reading those packets.

As a bonus, Wireshark has some very SPDY-friendly capabilities. Clicking on the second of the three SPDY packets (a response from the server):



In the middle pane, I see a bunch of information about the packet (TCP/IP information and the like) including that there were three separate data segments in the packet (the SYN_REPLY, DATA, and DATA_FIN shown in the npn_spdy_server.rb output above). Even nicer, I can see the contents of those segments in separate tabs in the bottom most pane. Shown is the DATA response verifying that SPDY is working—in hex and ASCII. Nice.

So the magic combination that I need right now is:That's a bit of a laundry list, but it is good to know that it is do-able.

Thus endeth my SSL obsession. Tomorrow, I return to SPDY proper.


Day #12

Thursday, May 5, 2011

SSL that Can Be Sniffed by Wireshark

‹prev | My Chain | next›

My day has ended in frustration the past two nights. Each nice I swore that I was abandoning my attempts to sniff the contents of SSL packets. And, just like the past two days, here I am, at it again. Loathe the problem unsolved.

The unsolved problem remains how to see the contents of SSL packets when I have the server's private key? Modern SSL v3 seems to baffle ssldump, but I still hope this is possible. Since SPDY is a binary protocol, sometime I have to believe I am going to want to see that data. And since that binary data is built on top of HTTP / SSL, I need a means to get at that data.

I could relax browser SSL restrictions like a did a few days ago. But I really want to stick as close as possible to real, modern sessions. Besides, relaxing Chrome's SSL restrictions made it pretty much unusable for all other sites on the web.

My new weapons for today: Wireshark and Apache's SSLCipherSuite. I always prefer the command line to the GUI, but I am hoping that Wireshark will prove more adept at decrypting SSL than ssldump. Similarly, I hope than the Apache documentation will prove better than the openssl / ssldump documentation has.

Starting with the Wireshark documentation on SSL, I note that it "only works for RSA key exchange if the RSA keys can be provided" (emphasis mine). I am still working with keys generated thusly with openssl:
openssl genrsa -out key.pem 1024
openssl req -new -key key.pem -out request.pem
openssl x509 -req -days 30 -in request.pem -signkey key.pem -out cert.pem
In the Wireshark preferences for the SSL protocol, I add:
127.0.0.1,10443,http,/home/cstrom/key.pem
This comma separate list includes the IP address of the SSL-enabled server (localhost/127.0.0.1), the port (10443 is port-forwarded to 443 on my VM, the protocol (http), and the private server key that generated for Apache with openssl.

With that, I can start up a Wireshark session listening for tcp.port == 10443:



In the top pane is the list of packets observed when I access my SSL-enabled VM. The middle pane has various attributes of one of the packets. The bottom pane holds the data contents of that packet.

And it is still freaking encrypted.

Before I get too frustrated, I recall that Wireshark asked for "RSA key exchange". Checking the SSL info in Chrome, I see:



Hrm... DHE-RSA key exchange sounds like RSA key exchange. At least it has the letters RSA in it. But referring back to the Apache documentation, I do not see it listed amongst the RSA keys. Lets see if adding something like SSLCipherSuite DES-CBC3-SHA to the Apache SSL configuration will do the trick.

Now, when I reload the page in Chrome, I see that I am definitely using RSA key exchange:



But what really matters is what I see in Wireshark:



Glory Hallelujah! In the middle pane, I clearly see unencrypted SSL.

For good measure I try this again in ssldump, which still fails to decrypt the data—most of the time. Every now and then (maybe one out of ten page loads), ssldump does manage to decrypt the data. But even then it takes tens of seconds to return the data. Clearly I am going to dump ssldump and move forward with Wireshark. I doubt it will replace my beloved tcpdump, but it is definitely my go-to tool for SSL.

Before calling it a night, I give Wireshark a try on the NPN-enabled SPDY from the other night. It fails to decrypt the data, but I have a good handle on what needs to change to rectify that. I will pick back up with that tomorrow.

Ah, sweet progress.

Day #11

Wednesday, May 4, 2011

In Which I Try to Learn SSL

‹prev | My Chain | next›

I had planned on moving back into SPDY proper, but I cannot let my difficulties with SSL pass without giving it one more try. Yesterday, I was unable to sniff SSL packets (even with the private key) of a SPDY session. I really do not understand where I am having difficulty, which is not a place that I like to be.

So tonight, I would like to try to it again with an Apache server. Following along with the Ubuntu guide, I
sudo apt-get install apache2
sudo a2enmod ssl
sudo a2ensite default-ssl

# Edit /etc/apache2/sites-enabled/default-ssl
SSLCertificateFile /etc/ssl/certs/cert.pem
SSLCertificateKeyFile /etc/ssl/private/key.pem
With that, I again try to sniff SSL packets with ssldump:
ssldump -n -a -A -dX -H -k key.pem -i eth1
...
2 3 0.0626 (0.0000) S>CV3.1(563) Handshake
Certificate
2 4 0.0626 (0.0000) S>CV3.1(397) Handshake
ServerKeyExchange
2 5 0.0626 (0.0000) S>CV3.1(4) Handshake
ServerHelloDone
2 6 0.0662 (0.0036) C>SV3.1(134) Handshake
ClientKeyExchange
2 7 0.0662 (0.0000) C>SV3.1(1) ChangeCipherSpec
2 8 0.0662 (0.0000) C>SV3.1(48) Handshake
2 9 0.0662 (0.0000) C>SV3.1(368) application_data
2 10 0.1155 (0.0492) S>CV3.1(218) Handshake
dss_fixed_dh2 11 0.1155 (0.0000) S>CV3.1(1) ChangeCipherSpec
2 12 0.1155 (0.0000) S>CV3.1(48) Handshake
2 13 0.1291 (0.0136) S>CV3.1(272) application_data
2 14 0.1291 (0.0000) S>CV3.1(48) application_data
2 15 0.1291 (0.0000) S>CV3.1(160) application_data
2 16 0.1291 (0.0000) S>CV3.1(48) application_data
2 17 0.2809 (0.1518) C>SV3.1(64) application_data
2 18 0.2850 (0.0041) S>CV3.1(96) application_data
2 19 0.2850 (0.0000) S>CV3.1(32) application_data
2 20 0.2850 (0.0000) S>CV3.1(256) application_data
2 21 0.2850 (0.0000) S>CV3.1(48) application_data
Ah ha! So I am just and SSL idiot, not an ssldump idiot. I am still not seeing the actual application data even with Apache.

Per the ssldump troubleshooting guide, the two most common reasons for not seeing application data are (1) not supplying a private key and (2) supplying the wrong one. I am definitely supplying a key. I am also certain that I am supplying the right one. I inspect the certificate in the browser:



That looks to be the same as the one I generated last night. For good measure, I initiate a root session and run ssldump directly in /etc/ssl/private with no change in the results.

With the first two causes of non-decryption checked off, I am left with the third and final, "using one of the ephemeral keying modes". Eh?

I am unsure how I could be using such a beast, but the troubleshooting guide goes on to say that a "sure sign of this case is when the server is sending the ServerKeyExchange message. Indeed, the server is sending a ServerKeyExchange message. So how do I get a static key? Fortunately, that question has already been asked and answered, I need an engine in the list:
chris@chris-VirtualBox:/etc/apache2$ openssl ciphers -v kRSA
AES256-SHA256 SSLv3 Kx=RSA Au=RSA Enc=AES(256) Mac=SHA256
AES256-SHA SSLv3 Kx=RSA Au=RSA Enc=AES(256) Mac=SHA1
CAMELLIA256-SHA SSLv3 Kx=RSA Au=RSA Enc=Camellia(256) Mac=SHA1
DES-CBC3-SHA SSLv3 Kx=RSA Au=RSA Enc=3DES(168) Mac=SHA1
DES-CBC3-MD5 SSLv2 Kx=RSA Au=RSA Enc=3DES(168) Mac=MD5
AES128-SHA256 SSLv3 Kx=RSA Au=RSA Enc=AES(128) Mac=SHA256
AES128-SHA SSLv3 Kx=RSA Au=RSA Enc=AES(128) Mac=SHA1
SEED-SHA SSLv3 Kx=RSA Au=RSA Enc=SEED(128) Mac=SHA1
CAMELLIA128-SHA SSLv3 Kx=RSA Au=RSA Enc=Camellia(128) Mac=SHA1
IDEA-CBC-SHA SSLv3 Kx=RSA Au=RSA Enc=IDEA(128) Mac=SHA1
IDEA-CBC-MD5 SSLv2 Kx=RSA Au=RSA Enc=IDEA(128) Mac=MD5
RC2-CBC-MD5 SSLv2 Kx=RSA Au=RSA Enc=RC2(128) Mac=MD5
RC4-SHA SSLv3 Kx=RSA Au=RSA Enc=RC4(128) Mac=SHA1
RC4-MD5 SSLv3 Kx=RSA Au=RSA Enc=RC4(128) Mac=MD5
RC4-MD5 SSLv2 Kx=RSA Au=RSA Enc=RC4(128) Mac=MD5
DES-CBC-SHA SSLv3 Kx=RSA Au=RSA Enc=DES(56) Mac=SHA1
DES-CBC-MD5 SSLv2 Kx=RSA Au=RSA Enc=DES(56) Mac=MD5
EXP-DES-CBC-SHA SSLv3 Kx=RSA(512) Au=RSA Enc=DES(40) Mac=SHA1 export
EXP-RC2-CBC-MD5 SSLv3 Kx=RSA(512) Au=RSA Enc=RC2(40) Mac=MD5 export
EXP-RC2-CBC-MD5 SSLv2 Kx=RSA(512) Au=RSA Enc=RC2(40) Mac=MD5 export
EXP-RC4-MD5 SSLv3 Kx=RSA(512) Au=RSA Enc=RC4(40) Mac=MD5 export
EXP-RC4-MD5 SSLv2 Kx=RSA(512) Au=RSA Enc=RC4(40) Mac=MD5 export

NULL-SHA256 SSLv3 Kx=RSA Au=RSA Enc=None Mac=SHA256
NULL-SHA SSLv3 Kx=RSA Au=RSA Enc=None Mac=SHA1
NULL-MD5 SSLv3 Kx=RSA Au=RSA Enc=None Mac=MD5
(except, I believe, the EXP / export ones)

Unfortunately, I still get no joy regardless of the options I send into genrsa:
openssl genrsa -out key.pem -des
openssl req -new -key key.pem -out request.pem
openssl x509 -req -days 30 -in request.pem -signkey key.pem -out cert.pem
I did have this working the other day, but I am forced to come to the conclusion that the difference was the client (using the --with-spdy=ssl Chrome option).

I think I can live with this. If I really need to packet sniff SSL, I can use that option, otherwise I will stick with Chrome's built-in SPDY log.


Day #10

Tuesday, May 3, 2011

Unable to ssldump a Real SPDY Session

‹prev | My Chain | next›

Last night, I got NPN enabled SPDY working in a VM—thanks to Carson McDonald's work. I am still not quite sure how I see this fitting into a development environment, but I would like to verify that I can sniff SSL/SPDY packets.

So I start my VM back up, with port forwarding on port 10000.

As I did the other night, I use last night's custom installed openssl to generate some server SSL keys:
openssl genrsa -out key.pem 1024
openssl req -new -key key.pem -out request.pem
openssl x509 -req -days 30 -in request.pem -signkey key.pem -out cert.pem
In the npn_spdy_server.rb script from Carson, I modify the start_tls line to use my key:
    set_negotiable_protocols(["spdy/2", "http/1.1", "http/1.0"])
#start_tls
start_tls(:private_key_file => 'key.pem', :cert_chain_file => 'cert.pem', :verify_peer => false)
Back on the host machine, on my development machine, I copy the private key file, key.pem locally. With that, I start up an ssldump session:
sudo ssldump -k key.pem -i lo -dX

New TCP connection #2: localhost.localdomain(47229) <-> localhost.localdomain(10000)
2 1 0.0012 (0.0012) C>S Handshake
ClientHello
Version 3.1
cipher suites
Unknown value 0xc00a
Unknown value 0xc014
Unknown value 0x88
Unknown value 0x87
TLS_DHE_RSA_WITH_AES_256_CBC_SHA
TLS_DHE_DSS_WITH_AES_256_CBC_SHA
Unknown value 0xc00f
Unknown value 0xc005
Unknown value 0x84
TLS_RSA_WITH_AES_256_CBC_SHA
Unknown value 0xc007
Unknown value 0xc009
Unknown value 0xc011
Unknown value 0xc013
Unknown value 0x45
Unknown value 0x44
TLS_DHE_DSS_WITH_RC4_128_SHA
TLS_DHE_RSA_WITH_AES_128_CBC_SHA
TLS_DHE_DSS_WITH_AES_128_CBC_SHA
Unknown value 0xc00c
Unknown value 0xc00e
Unknown value 0xc002
Unknown value 0xc004
Unknown value 0x96
Unknown value 0x41
TLS_RSA_WITH_RC4_128_MD5
TLS_RSA_WITH_RC4_128_SHA
TLS_RSA_WITH_AES_128_CBC_SHA
Unknown value 0xc008
Unknown value 0xc012
TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA
Unknown value 0xc00d
Unknown value 0xc003
Unknown value 0xfeff
TLS_RSA_WITH_3DES_EDE_CBC_SHA
compression methods
unknown value
NULL

....

2 3 0.0021 (0.0000) S>CV3.1(563) Handshake
Certificate
2 4 0.0021 (0.0000) S>CV3.1(4) Handshake
ServerHelloDone
2 5 0.0038 (0.0016) C>SV3.1(134) Handshake
ClientKeyExchange
2 6 0.0038 (0.0000) C>SV3.1(1) ChangeCipherSpec
2 7 0.0038 (0.0000) C>SV3.1(80) Handshake
2 8 0.0038 (0.0000) C>SV3.1(336) application_data
2 9 0.0181 (0.0143) S>CV3.1(170) Handshake
dss_fixed_dh2 10 0.0181 (0.0000) S>CV3.1(1) ChangeCipherSpec
2 11 0.0181 (0.0000) S>CV3.1(48) Handshake
2 12 0.0181 (0.0000) S>CV3.1(96) application_data
2 13 0.0181 (0.0000) S>CV3.1(48) application_data
2 14 0.0181 (0.0000) S>CV3.1(32) application_data
2 15 0.1775 (0.1594) C>SV3.1(80) application_data
2 0.1798 (0.0022) S>C TCP FIN
2 0.1811 (0.0013) C>S TCP FIN
Ew. I miss my nice hex output.

I sudo apt-get install ssldump inside the VM, but get similar results. I suspect that this might be caused by using a key generated from a different version of openssl. To verify this suspicion, I download the latest version of the ssldump source code into my VM and install it into my local directory:
wget http://www.rtfm.com/ssldump/ssldump-0.9b3.tar.gz
cd ~/src
tar zxf ssldump-0.9b3.tar.gz
chris@chris-VirtualBox:~/src/ssldump-0.9b3$ ./configure --prefix=$HOME/local
creating cache ./config.cache
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
checking build system type... i686-pc-linux-gnu
checking for gcc... gcc
checking whether the C compiler (gcc ) works... yes
checking whether the C compiler (gcc ) is a cross-compiler... no
checking whether we are using GNU C... yes
checking whether gcc accepts -g... yes
checking whether make sets ${MAKE}... yes
checking for ranlib... ranlib
checking for a BSD compatible install... /usr/bin/install -c
configuring for linux-gnu
checking for pow in -lm... yes
checking for PCAP include files...
configure: error: Couldn't find PCAP includes: needed for ssldump
Hrm... wonder if there is a package named libpcap-dev:
chris@chris-VirtualBox:~/src/ssldump-0.9b3$ sudo apt-get install libpcap-dev
[sudo] password for chris:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libpcap0.8-dev
The following NEW packages will be installed:
libpcap-dev libpcap0.8-dev
First guess -- amazing! When I run configure again, it completes, but I notice that it does not seem to be using openssl:
checking for OpenSSL include files... not found.
configure: warning: compiling without OpenSSL
I would have thought it could find it based on the environment variables in my VM's bashrc from last night:
export PATH=$HOME/local/bin/:$PATH
export LD_LIBRARY_PATH=$HOME/local/lib
export PKG_CONFIG_PATH=$HOME/local/lib/pkgconfig
export CPATH=$HOME/local/include
No matter, I can make sure that it finds it with the --with-openssl command line switch:
chris@chris-VirtualBox:~/src/ssldump-0.9b3$ ./configure --prefix=$HOME/local --with-openssl=$HOME/local
loading cache ./config.cache
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
checking build system type... i686-pc-linux-gnu
checking for gcc... (cached) gcc
checking whether the C compiler (gcc ) works... yes
checking whether the C compiler (gcc ) is a cross-compiler... no
checking whether we are using GNU C... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking whether make sets ${MAKE}... (cached) yes
checking for ranlib... (cached) ranlib
checking for a BSD compatible install... (cached) /usr/bin/install -c
configuring for linux-gnu
checking for pow in -lm... (cached) yes
checking for PCAP include files... found in /usr/include
checking for PCAP library... found in /usr/lib
checking for OpenSSL include files... found in /home/chris/local/include
checking for OpenSSL libraries... found in /home/chris/local/lib

checking for pcap_open in -lpcap... (cached) no
checking how to run the C preprocessor... (cached) gcc -E
checking for sys/time.h... (cached) yes
checking for ANSI C header files... (cached) yes
checking for working const... (cached) yes
checking whether time.h and sys/time.h may both be included... (cached) yes
checking size of unsigned short... (cached) 2
checking size of unsigned int... (cached) 4
checking size of unsigned long... (cached) 4
checking size of unsigned long long... (cached) 8
checking for 8-bit clean memcmp... (cached) yes
checking return type of signal handlers... (cached) void
checking for vprintf... (cached) yes
checking for strdup... (cached) yes
creating ./config.status
creating Makefile
Running make, I get:
...
./ssl/ssldecode.c: In function ‘ssl3_generate_export_iv’:
./ssl/ssldecode.c:672: error: ‘MD5_CTX’ undeclared (first use in this function)
./ssl/ssldecode.c:672: error: (Each undeclared identifier is reported only once
./ssl/ssldecode.c:672: error: for each function it appears in.)
./ssl/ssldecode.c:672: error: expected ‘;’ before ‘md5’
./ssl/ssldecode.c:675: error: ‘md5’ undeclared (first use in this function)
./ssl/ssldecode.c: In function ‘ssl3_prf’:
./ssl/ssldecode.c:693: error: ‘MD5_CTX’ undeclared (first use in this function)
./ssl/ssldecode.c:693: error: expected ‘;’ before ‘md5’
./ssl/ssldecode.c:706: error: ‘md5’ undeclared (first use in this function)
./ssl/ssldecode.c: In function ‘ssl_generate_keying_material’:
./ssl/ssldecode.c:849: error: ‘MD5_CTX’ undeclared (first use in this function)
./ssl/ssldecode.c:849: error: expected ‘;’ before ‘md5’
./ssl/ssldecode.c:851: error: ‘md5’ undeclared (first use in this function)
make: *** [ssldecode.o] Error 1
Sigh. This looks to be a night of yak shaving.

After slogging through C code for a bit, I eventually sudo apt-get source ssldump, which ought to yield a compilable version of ssldump. Unfortunately, I get:
...
gcc debug.o r_assoc.o r_bitfield.o r_data.o r_errors.o r_list.o r_replace.o r_time.o network.o pcap-snoop.o proto_mod.o tcpconn.o tcppack.o null_analyze.o ciphersuites.o ssl.enums.o ssl_analyze.o ssl_rec.o ssldecode.o sslprint.o sslxprint.o -o ssldump -L/home/chris/local/lib -lssl -lcrypto -lpcap -lm
/home/chris/local/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':
dso_dlfcn.c:(.text+0x2d): undefined reference to `dlopen'
dso_dlfcn.c:(.text+0x43): undefined reference to `dlsym'
dso_dlfcn.c:(.text+0x4d): undefined reference to `dlclose'
/home/chris/local/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_pathbyaddr':
dso_dlfcn.c:(.text+0x91): undefined reference to `dladdr'
dso_dlfcn.c:(.text+0xf9): undefined reference to `dlerror'
/home/chris/local/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_func':
dso_dlfcn.c:(.text+0x4b1): undefined reference to `dlsym'
dso_dlfcn.c:(.text+0x590): undefined reference to `dlerror'
/home/chris/local/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_var':
dso_dlfcn.c:(.text+0x611): undefined reference to `dlsym'
dso_dlfcn.c:(.text+0x6f0): undefined reference to `dlerror'
/home/chris/local/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_unload':
dso_dlfcn.c:(.text+0x755): undefined reference to `dlclose'
/home/chris/local/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load':
dso_dlfcn.c:(.text+0x837): undefined reference to `dlopen'
dso_dlfcn.c:(.text+0x8ae): undefined reference to `dlclose'
dso_dlfcn.c:(.text+0x8f5): undefined reference to `dlerror'
collect2: ld returned 1 exit status
make: *** [ssldump] Error 1
This really is not going to be my night. I eventually solve this by adding -ldl to the Makefile:
LIBS += -L/home/chris/local/lib  -lssl -lcrypto -lpcap -lm  -ldl
After installing that in my $HOME/local directory, I re-run my previous ssldump only to find:
...
2 3 0.0021 (0.0000) S>CV3.1(563) Handshake
Certificate
2 4 0.0021 (0.0000) S>CV3.1(4) Handshake
ServerHelloDone
2 5 0.0038 (0.0016) C>SV3.1(134) Handshake
ClientKeyExchange
2 6 0.0038 (0.0000) C>SV3.1(1) ChangeCipherSpec
2 7 0.0038 (0.0000) C>SV3.1(80) Handshake
2 8 0.0038 (0.0000) C>SV3.1(336) application_data
2 9 0.0181 (0.0143) S>CV3.1(170) Handshake
dss_fixed_dh2 10 0.0181 (0.0000) S>CV3.1(1) ChangeCipherSpec
2 11 0.0181 (0.0000) S>CV3.1(48) Handshake
2 12 0.0181 (0.0000) S>CV3.1(96) application_data
2 13 0.0181 (0.0000) S>CV3.1(48) application_data
2 14 0.0181 (0.0000) S>CV3.1(32) application_data
2 15 0.1775 (0.1594) C>SV3.1(80) application_data
2 0.1798 (0.0022) S>C TCP FIN
2 0.1811 (0.0013) C>S TCP FIN
Sigh. I really do not understand why the application_data does not include the actually decrypted packets. I have the private key and an ssldump compiled against the same openssl that is being used by the eventmachine server. That should work.


Frustrated, I call it a night at this point. SSL sniffing is a nice-to-have, but with the SPDY inspector in chrome://net-internals, not desperately needed. So unless inspiration strikes, I will likely move on tomorrow.


Day #9

Monday, May 2, 2011

Real SPDY with Eventmachine

‹prev | My Chain | next›

After an unsuccessful attempt last night, I hope to build on what I learned to get NPN SPDY working with eventmachine today. My ultimate hope is that this will allow me to build SPDY services without needing to run Chrome with the --with-spdy=ssl option (which seems to break things).

I eventually dead ended with a 64-bit openssl issue yesterday, so today I start by firing up my 32-bit Ubuntu 10.10 VM. I have yet to do much beyond installing build-essentials on that machine, so this should prove a fairly exhaustive step-by-step for getting going on Ubuntu.

Note: it was so new that I needed to install ssh, curl, git-core, and rvm.

On the VM, I stick with ruby 1.9.2 under rvm (somehow I started with 1.8.7 as my default on my laptop), and git clone the gem:
chris@chris-VirtualBox:~/repos$ git clone git://github.com/igrigorik/spdy.git
Initialized empty Git repository in /home/chris/repos/spdy/.git/
remote: Counting objects: 275, done.
remote: Compressing objects: 100% (167/167), done.
remote: Total 275 (delta 150), reused 177 (delta 93)
Receiving objects: 100% (275/275), 32.16 KiB, done.
Resolving deltas: 100% (150/150), done.
With that, I am ready to pick back up from where I left off yesterday. Specifically, I follow along with Carson McDonald's instructions.

First, I sudo apt-get install zlib1g-dev, which is the equivalent to the Fedora zlib-devel listed in Carson's instructions. The strange next step is to install cvs (way to keep up with the times, openssl), after which I can:
chris@chris-VirtualBox:~/repos$ cvs -d anonymous@cvs.openssl.org:/openssl-cvs co openssl
The authenticity of host 'cvs.openssl.org (194.97.152.144)' can't be established.
RSA key fingerprint is 55:c1:16:04:82:09:3a:ac:88:14:27:3f:46:7c:25:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'cvs.openssl.org,194.97.152.144' (RSA) to the list of known hosts.
cvs server: WARNING: Read-only repository access mode selected via `cvs -R'.
Using this option to access a repository which some users write to may
cause intermittent sandbox corruption.
cvs checkout: Updating openssl
U openssl/.cvsignore
U openssl/ACKNOWLEDGMENTS
...
U openssl/util/pl/ultrix.pl
U openssl/util/pl/unix.pl
Next, I configure openssl to install into $HOME/local, which is where I like to install custom built binaries that would normally install into /usr/bin or /usr/local/bin:
chris@chris-VirtualBox:~/repos/openssl$ ./config --prefix=$HOME/local
Operating system: i686-whatever-linux2
Configuring for linux-elf
Configuring for linux-elf
no-ec-nistp224-64-gcc-128 [default] OPENSSL_NO_EC_NISTP224_64_GCC_128 (skip dir)
no-gmp [default] OPENSSL_NO_GMP (skip dir)
no-jpake [experimental] OPENSSL_NO_JPAKE (skip dir)
no-krb5 [krb5-flavor not specified] OPENSSL_NO_KRB5
no-md2 [default] OPENSSL_NO_MD2 (skip dir)
no-rc5 [default] OPENSSL_NO_RC5 (skip dir)
no-rfc3779 [default] OPENSSL_NO_RFC3779 (skip dir)
no-shared [default]
no-store [experimental] OPENSSL_NO_STORE (skip dir)
no-zlib [default]
no-zlib-dynamic [default]
IsMK1MF=0
...
make[1]: Leaving directory `/home/chris/repos/openssl/test'

Since you've disabled or enabled at least one algorithm, you need to do
the following before building:

make depend

Configured for linux-elf.
With that, I can make depend:
chris@chris-VirtualBox:~/repos/openssl$ make depend
making depend in crypto...
make[1]: Entering directory `/home/chris/repos/openssl/crypto'
making depend in crypto/objects...
make[2]: Entering directory `/home/chris/repos/openssl/crypto/objects'
...
making depend in tools...
make[1]: Entering directory `/home/chris/repos/openssl/tools'
make[1]: Nothing to be done for `depend'.
make[1]: Leaving directory `/home/chris/repos/openssl/tools'
And make install:
chris@chris-VirtualBox:~/repos/openssl$ make install
...
installing libcrypto.a
installing libssl.a
cp libcrypto.pc /home/chris/local/lib/pkgconfig
chmod 644 /home/chris/local/lib/pkgconfig/libcrypto.pc
cp libssl.pc /home/chris/local/lib/pkgconfig
chmod 644 /home/chris/local/lib/pkgconfig/libssl.pc
cp openssl.pc /home/chris/local/lib/pkgconfig
chmod 644 /home/chris/local/lib/pkgconfig/openssl.pc
With openssl (hopefully) compiled OK, I add the $HOME/local directory to various paths by adding this to my .bashrc:
export PATH=$HOME/local/bin/:$PATH
export LD_LIBRARY_PATH=$HOME/local/lib
export PKG_CONFIG_PATH=$HOME/local/lib/pkgconfig
export CPATH=$HOME/local/include
That should allow a newly compiled ruby to find and use the newly installed edge-openssl:
chris@chris-VirtualBox:~/repos/openssl$ rvm install 1.9.2 -C --with-openssl-dir=$HOME/local
Installing Ruby from source to: /home/chris/.rvm/rubies/ruby-1.9.2-p180, this may take a while depending on your cpu(s)...

ruby-1.9.2-p180 - #fetching
ruby-1.9.2-p180 - #extracted to /home/chris/.rvm/src/ruby-1.9.2-p180 (already extracted)
ruby-1.9.2-p180 - #configuring
ruby-1.9.2-p180 - #compiling
ruby-1.9.2-p180 - #installing
Removing old Rubygems files...
Installing rubygems dedicated to ruby-1.9.2-p180...
Installing rubygems for /home/chris/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
Installation of rubygems completed successfully.
ruby-1.9.2-p180 - adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
ruby-1.9.2-p180 - #importing default gemsets (/home/chris/.rvm/gemsets/)
Install of ruby-1.9.2-p180 - #complete
chris@chris-VirtualBox:~/repos$ rvm --default use 1.9.2
Using /home/chris/.rvm/gems/ruby-1.9.2-p180
I create an rvm gemset (rvm use default@spdy) for my spdy work. This is more a force of habit than a real need since I will be using this VM exclusively for SPDY work. In that gemset, I install bundler:
chris@chris-VirtualBox:~/repos/spdy$ gem install bundler
Fetching: bundler-1.0.12.gem (100%)
Successfully installed bundler-1.0.12
1 gem installed
And, per Carson's instructions, I also install rake-compiler:
chris@chris-VirtualBox:~/repos/spdy$ gem install rake-compiler
Fetching: rake-compiler-0.7.8.gem (100%)
Successfully installed rake-compiler-0.7.8
1 gem installed
And the rest of the ruby gems required by the SPDY gem (with and assist from bundler):
chris@chris-VirtualBox:~/repos/spdy$ bundle install
Fetching source index for http://rubygems.org/
Using rake (0.8.7)
Installing bindata (1.3.1)
Installing diff-lcs (1.1.2)
Installing ffi (1.0.7) with native extensions
Installing ffi-zlib (0.2.0)
Installing rspec-core (2.5.1)
Installing rspec-expectations (2.5.0)
Installing rspec-mocks (2.5.0)
Installing rspec (2.5.0)
Using spdy (0.0.2) from source at /home/chris/repos/spdy
Using bundler (1.0.12)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
Now, I install Carson's NPN-enabled eventmachine by cloning it from github:
chris@chris-VirtualBox:~/repos$ git clone git://github.com/carsonmcdonald/eventmachine.git
Initialized empty Git repository in /home/chris/repos/eventmachine/.git/
remote: Counting objects: 3792, done.
remote: Compressing objects: 100% (1263/1263), done.
Receiving objects: 100% (3792/3792), 794.81 KiB | 159 KiB/s, done.
remote: Total 3792 (delta 2702), reused 3339 (delta 2398)
Resolving deltas: 100% (2702/2702), done.
chris@chris-VirtualBox:~/repos$ cd eventmachine
chris@chris-VirtualBox:~/repos/eventmachine$ git checkout tls-npn
Branch tls-npn set up to track remote branch tls-npn from origin.
Switched to a new branch 'tls-npn'
chris@chris-VirtualBox:~/repos/eventmachine$ rake gem
(in /home/chris/repos/eventmachine)
rake-compiler must be configured first to enable cross-compilation
rake-compiler must be configured first to enable cross-compilation
rake-compiler must be configured first to enable cross-compilation
rake-compiler must be configured first to enable cross-compilation
mkdir -p pkg
Successfully built RubyGem
Name: eventmachine
Version: 1.0.0.beta.3
File: eventmachine-1.0.0.beta.3.gem
mv eventmachine-1.0.0.beta.3.gem pkg/eventmachine-1.0.0.beta.3.gem
chris@chris-VirtualBox:~/repos/eventmachine$ gem install pkg/eventmachine-1.0.0.beta.3.gem
Building native extensions. This could take a while...
Successfully installed eventmachine-1.0.0.beta.3
1 gem installed
Yay! That is progress. I could not compile yesterday so I am now officially one step further. So, does it work now?
chris@chris-VirtualBox:~/repos/spdy/examples$ ruby ./npn_spdy_server.rb
/home/chris/.rvm/gems/ruby-1.9.2-p180@spdy/gems/eventmachine-1.0.0.beta.3/lib/em/connection.rb:404:in `set_negotiable_protocols': undefined method `set_neg
otiable_protocols' for EventMachine:Module (NoMethodError)
from ./npn_spdy_server.rb:35:in `post_init'
from /home/chris/.rvm/gems/ruby-1.9.2-p180@spdy/gems/eventmachine-1.0.0.beta.3/lib/em/connection.rb:45:in `block in new'
from /home/chris/.rvm/gems/ruby-1.9.2-p180@spdy/gems/eventmachine-1.0.0.beta.3/lib/em/connection.rb:36:in `instance_eval'
from /home/chris/.rvm/gems/ruby-1.9.2-p180@spdy/gems/eventmachine-1.0.0.beta.3/lib/em/connection.rb:36:in `new'
from /home/chris/.rvm/gems/ruby-1.9.2-p180@spdy/gems/eventmachine-1.0.0.beta.3/lib/eventmachine.rb:1415:in `event_callback'
from /home/chris/.rvm/gems/ruby-1.9.2-p180@spdy/gems/eventmachine-1.0.0.beta.3/lib/eventmachine.rb:206:in `run_machine'
from /home/chris/.rvm/gems/ruby-1.9.2-p180@spdy/gems/eventmachine-1.0.0.beta.3/lib/eventmachine.rb:206:in `run'
from ./npn_spdy_server.rb:66:in `<main>'
Noooo!!!!!

Luckily I determine that this is caused by a bad gem install. Specifically, I had run the eventmachine install in a separate gnu-screen window (without the local openssl in its path). I add the necessary paths, re-run the gem install, and am good to go.

Unfortunately, when I access the service from Chrome, I now see this in the eventmachine output:
chris@chris-VirtualBox:~/repos/spdy/examples$ ruby ./npn_spdy_server.rb
[:SPDY, :connection_closed]
[:SPDY, :connection_closed]
[:SPDY, :connection_closed]
And, in the browser:


Dang. This is very similar to the behavior that I was seeing when I ran Chrome without --with-spdy=ssl the other day.

Soo.... I shut down Chrome and restart with with --with-spdy=ssl. My pinned tabs and extensions break (because that option seems to affect all sites) and it still does not work.

And then I restart and try once more without any command line options to find:



Hrm... better. I wish it would have worked right away, but I will take that as a small victory. It worked (it is SPDY) and I can still access GMail and other web pages. That is definitely progress.

That will suffice as a stopping point for the day. I will pick back up tomorrow to determine how stable this is and to see if I can use ssldump to sniff packets.


Day #8

Sunday, May 1, 2011

64 Bit NPN (no-worky)

‹prev | My Chain | next›

Last night, I was able to get ssldump to sniff SPDY packets. It was still not a full SPDY session, so tonight I hope to get a little closer.

Fortunately, Carson McDonald has done yoeman's work toward this, and presented a gist of how to get NPN working with eventmachine.

Rather than following the very detailed instructions in that gist, I wonder if it will "just work". So I copy the npn-enabled event machine script and give it a whirl:
➜  spdy git:(master) ✗ ruby ./examples/npn_spdy_server.rb
./examples/npn_spdy_server.rb:35:in `post_init': undefined method `set_negotiable_protocols' for #<SPDYHandler:0x00000000ef9fc0> (NoMethodError)
from /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-0.12.10/lib/em/connection.rb:45:in `block in new'
from /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-0.12.10/lib/em/connection.rb:36:in `instance_eval'
from /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-0.12.10/lib/em/connection.rb:36:in `new'
from /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-0.12.10/lib/eventmachine.rb:1430:in `event_callback'
from /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
from /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
from ./examples/npn_spdy_server.rb:66:in `<main>'
OK I am none too surprised at that. Maybe a newer version of eventmachine?
➜  spdy git:(master) ✗ gem uninstall eventmachine
Successfully uninstalled eventmachine-0.12.10
➜ spdy git:(master) ✗ gem install eventmachine --pre
Building native extensions. This could take a while...
Successfully installed eventmachine-1.0.0.beta.3
1 gem installed
Unfortunately, I get no joy from the most recent pre-release version of the gem:
➜  spdy git:(master) ✗ ruby ./examples/npn_spdy_server.rb
./examples/npn_spdy_server.rb:35:in `post_init': undefined method `set_negotiable_protocols' for # (NoMethodError)
from /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-1.0.0.beta.3/lib/em/connection.rb:45:in `block in new'
from /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-1.0.0.beta.3/lib/em/connection.rb:36:in `instance_eval'
from /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-1.0.0.beta.3/lib/em/connection.rb:36:in `new'
from /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-1.0.0.beta.3/lib/eventmachine.rb:1384:in `event_callback'
from /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-1.0.0.beta.3/lib/eventmachine.rb:199:in `run_machine'
from /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-1.0.0.beta.3/lib/eventmachine.rb:199:in `run'
from ./examples/npn_spdy_server.rb:66:in `<main>'
OK. That does not solve it. To the google machine! Er... Google is also unable to find anything about set_negotiable_protocols. So... to the githubs!

It is on github that I find the answer, none too surprisingly from Carson:https://github.com/carsonmcdonald/eventmachine/commit/ba9c3892e05fc8c50e6bed88c3a381e0a922c74d. Ah. Turns out I did need to install Carson's eventmachine fork—he added the set_negotiable_protocols method in a branch of his fork. So...
➜  repos  git clone git://github.com/carsonmcdonald/eventmachine.git
Cloning into eventmachine...
remote: Counting objects: 3792, done.
remote: Compressing objects: 100% (1263/1263), done.
remote: Total 3792 (delta 2702), reused 3339 (delta 2398)
Receiving objects: 100% (3792/3792), 794.81 KiB | 159 KiB/s, done.
Resolving deltas: 100% (2702/2702), done.
➜ repos cd eventmachine
➜ eventmachine git:(master) git checkout tls-npn
Branch tls-npn set up to track remote branch tls-npn from origin.
Switched to a new branch 'tls-npn'
➜ eventmachine git:(tls-npn) rake gem
...
That last command errors because I need rake-compiler installed:
➜  eventmachine git:(tls-npn) gem install rake-compiler
Successfully installed rake-compiler-0.7.8
1 gem installed
➜ eventmachine git:(tls-npn) rake gem
(in /home/cstrom/repos/eventmachine)
rake-compiler must be configured first to enable cross-compilation
rake-compiler must be configured first to enable cross-compilation
rake-compiler must be configured first to enable cross-compilation
rake-compiler must be configured first to enable cross-compilation
mkdir -p pkg
Successfully built RubyGem
Name: eventmachine
Version: 1.0.0.beta.3
File: eventmachine-1.0.0.beta.3.gem
mv eventmachine-1.0.0.beta.3.gem pkg/eventmachine-1.0.0.beta.3.gem
Better. And with that, I can install the version of eventmachine with set_negotiable_protocols:
➜  eventmachine git:(tls-npn) gem install pkg/eventmachine-1.0.0.beta.3.gem
Building native extensions. This could take a while...
Successfully installed eventmachine-1.0.0.beta.3
1 gem installed
Unfortunately, I still get no joy. When I attempt to access the eventmachine server, I continue to get the set_negotiable_protocols error. Sigh. So I really did need to follow along with the entire list of instructions that Carson provided.

Following along with his instructions, I first install an npn-enabled version of openssl:
➜  eventmachine git:(tls-npn) cd ..
➜ repos mkdir openssl
➜ repos cd !$
➜ repos cd openssl
➜ openssl mkdir openssl-dist
➜ openssl cvs -d anonymous@cvs.openssl.org:/openssl-cvs co openssl
zsh: correct 'cvs' to '_cvs' [nyae]? n
zsh: command not found: cvs
➜ openssl sudo apt-get install cvs
# weep uncontrollably
➜ openssl cvs -d anonymous@cvs.openssl.org:/openssl-cvs co openssl
zsh: correct 'cvs' to '_cvs' [nyae]? n
# No, sadly, I really do want cvs...
The authenticity of host 'cvs.openssl.org (194.97.152.144)' can't be established.
RSA key fingerprint is 55:c1:16:04:82:09:3a:ac:88:14:27:3f:46:7c:25:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'cvs.openssl.org,194.97.152.144' (RSA) to the list of known hosts.
cvs server: WARNING: Read-only repository access mode selected via `cvs -R'.
Using this option to access a repository which some users write to may
cause intermittent sandbox corruption.
cvs checkout: Updating openssl
U openssl/.cvsignore
U openssl/ACKNOWLEDGMENTS
U openssl/CHANGES
U openssl/CHANGES.SSLeay
U openssl/Configure
...
cvs checkout: Updating openssl/util/pl
U openssl/util/pl/BC-32.pl
U openssl/util/pl/Mingw32.pl
U openssl/util/pl/OS2-EMX.pl
U openssl/util/pl/VC-32.pl
U openssl/util/pl/linux.pl
U openssl/util/pl/netware.pl
U openssl/util/pl/ultrix.pl
U openssl/util/pl/unix.pl
With that, I can compile:
➜  openssl-src  ./config --prefix=$HOME/repos/openssl/openssl-dist/
Operating system: x86_64-whatever-linux2
Configuring for linux-x86_64
Configuring for linux-x86_64
no-ec-nistp224-64-gcc-128 [default] OPENSSL_NO_EC_NISTP224_64_GCC_128 (skip dir)
no-gmp [default] OPENSSL_NO_GMP (skip dir)
no-jpake [experimental] OPENSSL_NO_JPAKE (skip dir)
no-krb5 [krb5-flavor not specified] OPENSSL_NO_KRB5
no-md2 [default] OPENSSL_NO_MD2 (skip dir)
no-rc5 [default] OPENSSL_NO_RC5 (skip dir)
no-rfc3779 [default] OPENSSL_NO_RFC3779 (skip dir)
no-shared [default]
no-store [experimental] OPENSSL_NO_STORE (skip dir)
no-zlib [default]
no-zlib-dynamic [default]
...

Since you've disabled or enabled at least one algorithm, you need to do
the following before building:

make depend

Configured for linux-x86_64.
So, I make depend:
➜  openssl-src  make depend
making depend in crypto...
make[1]: Entering directory `/home/cstrom/repos/openssl/openssl-src/crypto'
making depend in crypto/objects...
...
make[1]: Nothing to be done for `depend'.
make[1]: Leaving directory `/home/cstrom/repos/openssl/openssl-src/tools'
And... make install:
➜  openssl-src  make install
...
make[2]: Leaving directory `/home/cstrom/repos/openssl/openssl-src/engines/ccgost'
make[1]: Leaving directory `/home/cstrom/repos/openssl/openssl-src/engines'
making install in apps...
make[1]: Entering directory `/home/cstrom/repos/openssl/openssl-src/apps'
installing openssl
installing CA.sh
installing CA.pl
installing tsget
make[1]: Leaving directory `/home/cstrom/repos/openssl/openssl-src/apps'
making install in test...
make[1]: Entering directory `/home/cstrom/repos/openssl/openssl-src/test'
make[1]: Nothing to be done for `install'.
make[1]: Leaving directory `/home/cstrom/repos/openssl/openssl-src/test'
making install in tools...
make[1]: Entering directory `/home/cstrom/repos/openssl/openssl-src/tools'
make[1]: Leaving directory `/home/cstrom/repos/openssl/openssl-src/tools'
installing libcrypto.a
installing libssl.a
cp libcrypto.pc /home/cstrom/repos/openssl/openssl-dist/lib/pkgconfig
chmod 644 /home/cstrom/repos/openssl/openssl-dist/lib/pkgconfig/libcrypto.pc
cp libssl.pc /home/cstrom/repos/openssl/openssl-dist/lib/pkgconfig
chmod 644 /home/cstrom/repos/openssl/openssl-dist/lib/pkgconfig/libssl.pc
cp openssl.pc /home/cstrom/repos/openssl/openssl-dist/lib/pkgconfig
chmod 644 /home/cstrom/repos/openssl/openssl-dist/lib/pkgconfig/openssl.pc
Nice. Hopefully I am getting close. I export some openssl-related environment variables:
➜  openssl-src  export PATH=$HOME/repos/openssl/openssl-dist/bin/:$PATH
➜ openssl-src export LD_LIBRARY_PATH=$HOME/repos/openssl/openssl-dist/lib
➜ openssl-src export PKG_CONFIG_PATH=$HOME/repos/openssl/openssl-dist/lib/pkgconfig
➜ openssl-src export CPATH=$HOME/repos/openssl/openssl-dist/include
And finally, I re-build the gem:
➜  eventmachine git:(tls-npn) gem install pkg/eventmachine-1.0.0.beta.3.gem
Building native extensions. This could take a while...
ERROR: Error installing pkg/eventmachine-1.0.0.beta.3.gem:
ERROR: Failed to build gem native extension.

/home/cstrom/.rvm/rubies/ruby-1.9.2-p0/bin/ruby extconf.rb
checking for rb_trap_immediate in ruby.h,rubysig.h... no
checking for rb_thread_blocking_region()... yes
checking for inotify_init() in sys/inotify.h... yes
checking for writev() in sys/uio.h... yes
checking for rb_thread_check_ints()... yes
checking for rb_time_new()... yes
checking for sys/event.h... no
checking for epoll_create() in sys/epoll.h... yes
creating Makefile

make
...
g++ -shared -o rubyeventmachine.so ssl.o ed.o cmain.o em.o rubymain.o page.o pipe.o kb.o binder.o -L. -L/home/cstrom/.rvm/rubies/ruby-1.9.2-p0/lib -Wl,-R/home/cstrom/.rvm/rubies/ruby-1.9.2-p0/lib -L. -rdynamic -Wl,-export-dynamic -L/home/cstrom/repos/openssl/openssl-dist/lib -Wl,-R -Wl,/home/cstrom/.rvm/rubies/ruby-1.9.2-p0/lib -L/home/cstrom/.rvm/rubies/ruby-1.9.2-p0/lib -lruby -lssl -lcrypto -ldl -lpthread -lrt -ldl -lcrypt -lm -lc
/usr/bin/ld: /home/cstrom/repos/openssl/openssl-dist/lib/libssl.a(s23_srvr.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/home/cstrom/repos/openssl/openssl-dist/lib/libssl.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [rubyeventmachine.so] Error 1


Gem files will remain installed in /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-1.0.0.beta.3 for inspection.
Results logged to /home/cstrom/.rvm/gems/ruby-1.9.2-p0@spdy/gems/eventmachine-1.0.0.beta.3/ext/gem_make.out
Ugh.

Unfortunately, I have to call it a night at this point. Initial research into that error seems to indicate that it is a 64 bit thing and that openssl and 64 bit machines are difficult, at best. I think I will give it a try in a 32 bit VM tomorrow.


Day #7

Saturday, April 30, 2011

Sniffing SPDY with ssldump

‹prev | My Chain | next›

Up today, I would like to mess about with SPDY over SSL. Last night, I was able to play around a bit with the eventmachine example implementation in the SPDY gem. I even got to inspect SPDY packets with tcpdump.

The only thing lacking from last night's experiment was SSL—a necessary component of SPDY. Tonight I hope to repeat that success with ssldump.

First, I re-enable the start_tls line in the example eventmachine code. I then open the application which now resides at: https://localhost:10000/.

I get a warning that the certificate is not trusted, but proceed anyway only to get... nothing. The browser never receives a result, though the event machine server kindly reports:
[:SPDY, :connection_closed]
This message indicates that the browser has unbound the connection:
  def unbind
p [:SPDY, :connection_closed]
end
Hrm...

Next up, I actually follow the instructions in the example server and start Chrome with the --use-spdy=ssl. According to the SPDY debugging page, this option "forces Chrome to always use SPDY, even if not negotiated over SSL". That actually does the trick:



Interesting. So if the debugging page is correct, then the reason this works is that, otherwise the connection is made while negotiating over plain text. That is something to investigate another day.

For today, if I am going to sniff SSL packets, I am going to need a server certificate. The whole point of SSL, of course, is to make packet sniffing impossible (or at least prohibitively difficult). It is possible, however, if you have the server's private key. To make one, I use openssl:
➜  spdy git:(master)     openssl genrsa -out key.pem 1024
Generating RSA private key, 1024 bit long modulus
.........................................................++++++
.........................++++++
e is 65537 (0x10001)
➜ spdy git:(master) ✗ openssl req -new -key key.pem -out request.pem
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]:US
State or Province Name (full name) [Some-State]:Maryland
Locality Name (eg, city) []:Baltimore
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, YOUR name) []:Chris Strom
Email Address []:chris@eeecomputes.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:password
An optional company name []:
➜ spdy git:(master) ✗ openssl x509 -req -days 30 -in request.pem -signkey key.pem -out cert.pem
Signature ok
subject=/C=US/ST=Maryland/L=Baltimore/O=Internet Widgits Pty Ltd/CN=Chris Strom/emailAddress=chris@eeecomputes.com
Getting Private key
To use that in eventmachine, I change the start_tls:
  def post_init
@parser = SPDY::Parser.new
@parser.on_headers_complete do |stream_id, associated_stream, priority, headers|
# ...
end

#start_tls
start_tls(:private_key_file => 'key.pem', :cert_chain_file => 'cert.pem', :verify_peer => false)
end
After restarting the server, I verify that I can still access the example SPDY site. Once I am confident that everything still works with the new certificate, I am ready to try out ssldump:
sudo ssldump -k key.pem -i lo -dX
The -k option uses the same private key file that I just generated and that eventmachine is now using. The example server is listening on localhost, so I need to sniff the packets on the loopback interface, lo. The -dX option tells ssldump to dump the packet contents in hex—ideally this is exactly what I did last night using tcpdump, but now I will be able to see an (almost) real SPDY conversation take place.

And, sure enough, that does the trick:
➜  examples git:(master) ✗ sudo ssldump -k key.pem -i lo -dX
New TCP connection #1: localhost.localdomain(35098) <-> localhost.localdomain(10000)
New TCP connection #2: localhost.localdomain(35099) <-> localhost.localdomain(10000)
1 1 0.0071 (0.0071) C>S Handshake
ClientHello
Version 3.1
...
3 7 0.0040 (0.0000) C>S Handshake3 8 0.0040 (0.0000) C>S application_data
---------------------------------------------------------------
00 2c 01 d3 fe 80 02 00 01 01 00 01 24 00 00 00
01 00 00 00 00 00 00 38 ea df a2 51 b2 62 e0 61
60 83 a4 17 06 7b b8 0b 75 30 2c d6 ae 40 17 cd
cd b1 2e b4 35 d0 b3 d4 d1 d2 d7 02 b3 2c 18 f8
50 73 2c 83 9c 67 b0 3f d4 3d 3a 60 07 81 d5 99
eb 40 d4 1b 33 f0 a3 e5 69 06 41 90 8b 75 a0 4e
d6 29 4e 49 ce 80 ab 81 25 03 06 be d4 3c dd d0
60 9d d4 3c a8 a5 bc 28 89 8d 81 23 2f 5f 17 2c
c2 c0 02 ca fc 0c fc a0 14 92 03 62 5a 01 73 a4
81 01 03 5b 2e b0 d4 c9 4f 61 60 76 77 0d 61 60
2b 28 4a 4c cf 4d 44 d2 c5 56 0c 24 73 53 19 58
33 4a 4a 0a 8a 19 98 41 61 c4 a8 cf 00 10 17 22
63 33 a4 fb e6 57 65 e6 e4 24 ea 9b ea 19 28 68
44 18 1a 5a 2b f8 64 e6 95 56 28 54 58 98 c5 9b
99 68 2a 38 02 c3 29 35 3c 35 c9 3b b3 44 df d4
d8 44 cf 18 a8 cc db 23 c4 d7 47 47 21 27 33 3b
55 c1 3d 35 39 3b 5f 53 c1 39 03 58 42 a5 ea 1b
1a e9 01 03 c5 c4 48 cf d0 48 21 38 31 2d b1 28
13 aa 89 81 1d 1a 4f 0c 1c b0 e8 03 00 00 00 ff
ff 00 00 00 ff ff
---------------------------------------------------------------
...
The fourth byte in bold indicates that this is a SYN_STREAM just like I saw last night.

That is a good place to stop for the night. Hopefully I will not have to rely on this technique much while working with SPDY, but it is good to know that it is available if needed.


SPDY protocol (draft 2)

Day #6