# SSL \[X.509\] Authentication in Rails?

**URL:** <https://www.ruby-forum.com/t/ssl-x-509-authentication-in-rails/98727>\
**Category:** Rails\
**Created:** [July 17, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/ssl-x-509-authentication-in-rails/98727 "2006-07-17T20:47:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![brez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@brez](https://www.ruby-forum.com/u/brez)\
**Post date:** [July 17, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/ssl-x-509-authentication-in-rails/98727/1 "2006-07-17T20:47:32Z")

</div>

Been doing some research but haven’t been able to determine much - has  
anyone had any experience parsing incoming [on the request] x.509  
certificates? Specifically, getting name, CA, etc. Here’s how you do in  
Java Servlets for reference:

X509Certificate[] certs = (X509Certificate[])  
request.getAttribute(“javax.servlet.request.X509Certificate”);

Thanks.

---

<div class="post-metadata">

**Author:** ![brez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@brez](https://www.ruby-forum.com/u/brez)\
**Post date:** [July 18, 2006, 1:25am UTC](https://www.ruby-forum.com/t/ssl-x-509-authentication-in-rails/98727/2 "2006-07-18T01:25:44Z")

</div>

Maybe another way to phrase the question is to say 'how do you get at  
the request object?

---

<div class="post-metadata">

**Author:** ![brez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@brez](https://www.ruby-forum.com/u/brez)\
**Post date:** [July 18, 2006, 2:26am UTC](https://www.ruby-forum.com/t/ssl-x-509-authentication-in-rails/98727/3 "2006-07-18T02:26:13Z")

</div>

Brez! !! wrote:

> Maybe another way to phrase the question is to say 'how do you get at  
> the request object?

You can probably find what you’re looking for in the OpenSSL wrapper  
library (lightly documented, but the source code is in the Ruby distro  
of course). I’m not aware that there is anything in pure Ruby to do what  
you want, but others are more knowledgeable than I am. If it turns out  
there is nothing, I’d be interested in adding it as an adjunct to the  
Net::LDAP library. (I’ve written a lot of X.509 processors in C over the  
years.) If anyone is interested, let me know here.

---

<div class="post-metadata">

**Author:** ![brez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@brez](https://www.ruby-forum.com/u/brez)\
**Post date:** [July 18, 2006, 5:51am UTC](https://www.ruby-forum.com/t/ssl-x-509-authentication-in-rails/98727/4 "2006-07-18T05:51:12Z")

</div>

> Net::LDAP library. (I’ve written a lot of X.509 processors in C over the  
> years.) If anyone is interested, let me know here.

Thanks for the suggestions , I’ll chk them out. Also found this progject  
in relation to rails and your LDAP project in particular:

[http://rubyforge.org/projects/railspki/](http://rubyforge.org/projects/railspki/)

Unfortunatly it doesn’t do what I’m looking to accomplish. Actually what  
I need has less to do with actual SSL and more to do with the request  
object supporting the cert chain [and subsequentially rails having  
access to it] - I will review it more for some hints tho. I’m also going  
to look at Jetty’s implementation of it as part of the servlet spec.  
Regardless tho, I’ll post up whatever I find / write it as plugin…  
Thanks again Francis.

---

<div class="post-metadata">

**Author:** ![brez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@brez](https://www.ruby-forum.com/u/brez)\
**Post date:** [July 18, 2006, 6:59am UTC](https://www.ruby-forum.com/t/ssl-x-509-authentication-in-rails/98727/5 "2006-07-18T06:59:19Z")

</div>

Someone correct me if I am wrong, but I think a client certificate is  
only sent to the server if the server requests one, and how the  
certificate is made available probably varies by webserver. I know  
for example with modssl and apache the certificate is an environment  
variable in the CGI namespace.

I don’t know if lighttpd has client certificate support, and if so if  
that information is available via the fastcgi environment. Might want  
to check out pound also, I believe it supports client certs, and it’s  
probably a better alternative since it can probably forward the  
certificate to something like mongrel. I’m guessing webrick is a dead  
end, I haven’t even seen complete directions on how to enable basic  
ssl with webrick.

If you make any headway on this please do post, I’ve been meaning to  
look at this myself but it just hasn’t been a top priority.

---

<div class="post-metadata">

**Author:** ![brez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@brez](https://www.ruby-forum.com/u/brez)\
**Post date:** [July 18, 2006, 7:02am UTC](https://www.ruby-forum.com/t/ssl-x-509-authentication-in-rails/98727/6 "2006-07-18T07:02:29Z")

</div>

Also, for parsing certificates you might want to look here:

[http://dataspill.org/articles/2005/01/10/goofing-off-simple-extensible-certificate-authority](http://dataspill.org/articles/2005/01/10/goofing-off-simple-extensible-certificate-authority)

I used this as a base to redo our again CA which was written in perl.  
There is code in there to parse X509 certificates. I think most of  
what you might be looking for is in lib/pki\_helper.rb.

Chris

---

<div class="post-metadata">

**Author:** ![brez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@brez](https://www.ruby-forum.com/u/brez)\
**Post date:** [July 18, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/ssl-x-509-authentication-in-rails/98727/7 "2006-07-18T19:42:46Z")

</div>

snacktime wrote:

> Someone correct me if I am wrong, but I think a client certificate is  
> only sent to the server if the server requests one, and how the  
> certificate is made available probably varies by webserver. I know  
> for example with modssl and apache the certificate is an environment  
> variable in the CGI namespace.
> 
> I don’t know if lighttpd has client certificate support, and if so if  
> that information is available via the fastcgi environment. Might want  
> to check out pound also, I believe it supports client certs, and it’s  
> probably a better alternative since it can probably forward the  
> certificate to something like mongrel. I’m guessing webrick is a dead  
> end, I haven’t even seen complete directions on how to enable basic  
> ssl with webrick.
> 
> If you make any headway on this please do post, I’ve been meaning to  
> look at this myself but it just hasn’t been a top priority.

Correct - Apache can make a cert optional or require it… Yea I’ll chk  
out pound [thanks] - most, if not all, of these servers support passing  
cert chains - what I need to produce is having the cert’s information  
available as part of an ActionController [primarily for the  
metadata/‘identification,’ not security, and \*not to do any actual  
authentication/revocation/etc - this is stuff that most servers already  
provide, and is arguably an issue of transport and not within scope of  
what rails, as an application framework, ought to provide].

Thanks

---

<div class="post-metadata">

**Author:** ![brez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@brez](https://www.ruby-forum.com/u/brez)\
**Post date:** [July 24, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/ssl-x-509-authentication-in-rails/98727/8 "2006-07-24T23:06:05Z")

</div>

> If you make any headway on this please do post, I’ve been meaning to  
> look at this myself but it just hasn’t been a top priority.

Ok - made some headway… I’m using Oracle’s OHS aka Apache because it  
comes with FastCGI installed, setup, etc. As far as I know it should be  
identical to Apache config, etc. [Lighttpd, Pound, et al same basic  
principles should apply but you’ll have to sort out the details] –

The solution was pretty straightforward in the end, essentially make  
sure that the SSL Environmental Variables are available to CGI/FastCGI  
by editing your ssl.conf file to include:

SSLOptions +StdEnvVars

There’s other options available as well see ssl.conf for specifics…

Once variables are available, they magically appear in your  
ActionController request object, e.g. to get the subject line of a  
client’s cert:

request.env[‘SSL\_CLIENT\_S\_DN\_CN’]

And that’s it… it’s running now and I’m using the subject line of the  
cert [with ID] to log people in [as opposed to a login/password box,  
etc]… i.e. discovering their ‘identity’… not necessarily a valid  
security solution [but this could be accomplished using revocation, etc  
as part of Apache- regardless, any solution is going to be somewhat  
dependent on the environment that it runs in.]

Here’s a list of all the SSL Env Vars for ref:

HTTPS SSL\_CLIENT\_M\_VERSION SSL\_SERVER\_M\_VERSION  
SSL\_CLIENT\_M\_SERIAL SSL\_SERVER\_M\_SERIAL  
SSL\_PROTOCOL SSL\_CLIENT\_V\_START SSL\_SERVER\_V\_START  
SSL\_SESSION\_ID SSL\_CLIENT\_V\_END SSL\_SERVER\_V\_END  
SSL\_CIPHER SSL\_CLIENT\_S\_DN SSL\_SERVER\_S\_DN  
SSL\_CIPHER\_EXPORT SSL\_CLIENT\_S\_DN\_C SSL\_SERVER\_S\_DN\_C  
SSL\_CIPHER\_ALGKEYSIZE SSL\_CLIENT\_S\_DN\_ST SSL\_SERVER\_S\_DN\_ST  
SSL\_CIPHER\_USEKEYSIZE SSL\_CLIENT\_S\_DN\_L SSL\_SERVER\_S\_DN\_L  
SSL\_VERSION\_LIBRARY SSL\_CLIENT\_S\_DN\_O SSL\_SERVER\_S\_DN\_O  
SSL\_VERSION\_INTERFACE SSL\_CLIENT\_S\_DN\_OU SSL\_SERVER\_S\_DN\_OU  
SSL\_CLIENT\_S\_DN\_CN SSL\_SERVER\_S\_DN\_CN  
SSL\_CLIENT\_S\_DN\_T SSL\_SERVER\_S\_DN\_T  
SSL\_CLIENT\_S\_DN\_I SSL\_SERVER\_S\_DN\_I  
SSL\_CLIENT\_S\_DN\_G SSL\_SERVER\_S\_DN\_G  
SSL\_CLIENT\_S\_DN\_S SSL\_SERVER\_S\_DN\_S  
SSL\_CLIENT\_S\_DN\_D SSL\_SERVER\_S\_DN\_D  
SSL\_CLIENT\_S\_DN\_UID SSL\_SERVER\_S\_DN\_UID  
SSL\_CLIENT\_S\_DN\_Email SSL\_SERVER\_S\_DN\_Email  
SSL\_CLIENT\_I\_DN SSL\_SERVER\_I\_DN  
SSL\_CLIENT\_I\_DN\_C SSL\_SERVER\_I\_DN\_C  
SSL\_CLIENT\_I\_DN\_ST SSL\_SERVER\_I\_DN\_ST  
SSL\_CLIENT\_I\_DN\_L SSL\_SERVER\_I\_DN\_L  
SSL\_CLIENT\_I\_DN\_O SSL\_SERVER\_I\_DN\_O  
SSL\_CLIENT\_I\_DN\_OU SSL\_SERVER\_I\_DN\_OU  
SSL\_CLIENT\_I\_DN\_CN SSL\_SERVER\_I\_DN\_CN  
SSL\_CLIENT\_I\_DN\_T SSL\_SERVER\_I\_DN\_T  
SSL\_CLIENT\_I\_DN\_I SSL\_SERVER\_I\_DN\_I  
SSL\_CLIENT\_I\_DN\_G SSL\_SERVER\_I\_DN\_G  
SSL\_CLIENT\_I\_DN\_S SSL\_SERVER\_I\_DN\_S  
SSL\_CLIENT\_I\_DN\_D SSL\_SERVER\_I\_DN\_D  
SSL\_CLIENT\_I\_DN\_UID SSL\_SERVER\_I\_DN\_UID  
SSL\_CLIENT\_I\_DN\_Email SSL\_SERVER\_I\_DN\_Email  
SSL\_CLIENT\_A\_SIG SSL\_SERVER\_A\_SIG  
SSL\_CLIENT\_A\_KEY SSL\_SERVER\_A\_KEY  
SSL\_CLIENT\_CERT SSL\_SERVER\_CERT  
SSL\_CLIENT\_CERT\_CHAINn  
SSL\_CLIENT\_VERIFY
