pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Support for cert auth in JDBC
28+ messages / 7 participants
[nested] [flat]

* Support for cert auth in JDBC
@ 2011-05-18 04:49  Marc-André Laverdière <marc-andre@atc.tcs.com>
  0 siblings, 1 reply; 28+ messages in thread

From: Marc-André Laverdière @ 2011-05-18 04:49 UTC (permalink / raw)
  To: pgsql-jdbc

[I posted this by mistake to the main dev mailing list... apologies if
you got this multiple times]

Hello developers,

My project had a requirement to use certificate authentication to the PG
server. Our application uses Hibernate.

We did just that and my boss has OKed a source release.

Now, the current version of the code has dependencies on our internal
libraries, so I'll need to spend a bit of time making this 'standard'
Java code.

Would you please tell me how you'd prefer for me to proceed to do that?
Do I need write access to your CVS repo, or should I just send the code
and test case by email?

Is there a specific version of the JDBC code you want me to work from,
should I just pick whatever is HEAD?

Any package you'd like me to choose?

Any specific crypto/ssl requirements to consider?

Any specific dependencies to use instead of others? (e.g. I like SLF4J,
but that's not everyone's choice...)

-- 
Marc-André Laverdière
Software Security Scientist
Innovation Labs, Tata Consultancy Services
Hyderabad, India



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-18 05:40  Craig Ringer <craig@postnewspapers.com.au>
  parent: Marc-André Laverdière <marc-andre@atc.tcs.com>
  0 siblings, 1 reply; 28+ messages in thread

From: Craig Ringer @ 2011-05-18 05:40 UTC (permalink / raw)
  To: Marc-André Laverdière <marc-andre@atc.tcs.com>; +Cc: pgsql-jdbc

On 18/05/2011 12:49 PM, Marc-André Laverdière wrote:
> [I posted this by mistake to the main dev mailing list... apologies if
> you got this multiple times]
>
> Hello developers,
>
> My project had a requirement to use certificate authentication to the PG
> server. Our application uses Hibernate.
>
> We did just that and my boss has OKed a source release.

Eh? Did what?

PgJDBC supports X.509 client certificates just fine.

What did you need to do? To which PgJDBC version did you do it? To 
resolve what perceived issue(s) in the current codebase?

-- 
Craig Ringer

Tech-related writing at http://soapyfrogs.blogspot.com/



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-18 06:06  Marc-André Laverdière <marc-andre@atc.tcs.com>
  parent: Craig Ringer <craig@postnewspapers.com.au>
  0 siblings, 1 reply; 28+ messages in thread

From: Marc-André Laverdière @ 2011-05-18 06:06 UTC (permalink / raw)
  To: Craig Ringer <craig@postnewspapers.com.au>; +Cc: pgsql-jdbc

Hello,

This implementations allows to specify which keystore and which
truststore to load. This allows certificate authentication in the
application easily.

In the official documentation, it says:

<quote>
Custom SSLSocketFactory

PostgreSQL™ provides a way for developers to customize how a SSL
connection is established. This may be used to provide a custom
certificate source or other extensions by allowing the developer to
create their own SSLContext instance. The connection URL parameters
sslfactory and sslfactoryarg allow the user to specify which custom
class to use for creating the SSLSocketFactory. The class name specified
by sslfactory must extend javax.net.ssl.SSLSocketFactory and be
available to the driver's classloader. This class must have a zero
argument constructor or a single argument constructor taking a String
argument. This argument may optionally be supplied by sslfactoryarg.

Information on how to actually implement such a class is beyond the
scope of this documentation. Places to look for help are the JSSE
Reference Guide and the source to the NonValidatingFactory provided by
the JDBC driver.

The Java SSL API is not very well known to the JDBC driver developers
and we would be interested in any interesting and generally useful
extensions that you have implemented using this mechanism. Specifically
it would be nice to be able to provide client certificates to be
validated by the server.
</quote>

What I'm talking about is this factory referred to in the last paragraph.

Marc-André Laverdière
Software Security Scientist
Innovation Labs, Tata Consultancy Services
Hyderabad, India

On Wednesday 18 May 2011 11:10 AM, Craig Ringer wrote:
> On 18/05/2011 12:49 PM, Marc-André Laverdière wrote:
>> [I posted this by mistake to the main dev mailing list... apologies if
>> you got this multiple times]
>>
>> Hello developers,
>>
>> My project had a requirement to use certificate authentication to the PG
>> server. Our application uses Hibernate.
>>
>> We did just that and my boss has OKed a source release.
> 
> Eh? Did what?
> 
> PgJDBC supports X.509 client certificates just fine.
> 
> What did you need to do? To which PgJDBC version did you do it? To
> resolve what perceived issue(s) in the current codebase?
> 



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-18 07:02  Craig Ringer <craig@postnewspapers.com.au>
  parent: Marc-André Laverdière <marc-andre@atc.tcs.com>
  0 siblings, 1 reply; 28+ messages in thread

From: Craig Ringer @ 2011-05-18 07:02 UTC (permalink / raw)
  To: Marc-André Laverdière <marc-andre@atc.tcs.com>; +Cc: pgsql-jdbc

On 18/05/2011 2:06 PM, Marc-André Laverdière wrote:
> Hello,
>
> This implementations allows to specify which keystore and which
> truststore to load. This allows certificate authentication in the
> application easily.

It's already pretty easy. You can:

- Load your keys in the standard keystore;
- Populate a new JECKSf-format keystore with your cert and key and use 
the javax.net.ssl.trustStore etc system properties on the cmdline or via 
System.setProperty() to use that store instead of the default store;
- Provide your own javax.net.ssl.SSLSocketFactory that wraps the default 
implementation with any additional behavior you need.

PgJDBC could use some documentation/examples for the latter option, but 
it shouldn't need any changes to the PgJDBC code.

> The Java SSL API is not very well known to the JDBC driver developers
> and we would be interested in any interesting and generally useful
> extensions that you have implemented using this mechanism. Specifically
> it would be nice to be able to provide client certificates to be
> validated by the server.
> </quote>
>
> What I'm talking about is this factory referred to in the last paragraph.

OK, so you've just written an easy-to-use canned SSLSocketFactory that 
loads client certificates from an application-configurable 
keystore/truststore?

I did this in my app to allow it to use a PKCS#12 format X.509 
certificate directly and it was a pretty low-pain procedure. It'd be 
nice to have more general purpose examples in the PgJDBC sources, 
though. Maybe I should dig out the examples I posted to this mailing 
list a while ago and submit them, too?

I'm not one of the PgJDBC developers, but I'd be happy to have a look at 
what you've done and see if I can be of any help, provide useful 
feedback, etc.

It'd probably be best if you published your work as a patch ("diff") 
against PgJDBC CVS HEAD, posting the patch to this mailing list so 
people can look at it and try it. Make sure the email you send the patch 
in clearly explains the reason for any change to the existing PgJDBC 
code, and if you do change the core PgJDBC code make sure it passes any 
tests and that it compiles under the oldest supported JDK.

-- 
Craig Ringer

Tech-related writing at http://soapyfrogs.blogspot.com/



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-18 07:48  Marc-André Laverdière <marc-andre@atc.tcs.com>
  parent: Craig Ringer <craig@postnewspapers.com.au>
  0 siblings, 3 replies; 28+ messages in thread

From: Marc-André Laverdière @ 2011-05-18 07:48 UTC (permalink / raw)
  To: pgsql-jdbc

It is true that anyone who knows the right Java APIs can put that
factory together with little pain. I just think that the average user
shouldn't have to bother about it.

Also, some use cases do not allow to use the default keystore and trust
store for everything.

Just for example, this is the setup in my application:
* 1 Cert for some server component that speaks TLS
* 1 Cert for the pgsql client component
* 1 Cert for the JNDI client component (connects to LDAP)

So we end up with 3 different keystores (and maybe trust stores too),
all of which may have different passwords. It is actually not that much
of a big deal to use, but the 'defaults' won't help us here too much I
think.

Using this kind of configurable setup allows much simpler deployments
than having to change stuff in a central keystore (which may have other
keys to NOT mess up with), and allows to test in different environments
and very easily.

Maybe our use case is overkill. Anyways, I'm attaching the code as I
have it now. I tested the code from which this one comes from, but this
incarnation isn't tested. I'll put more time on it once I hear back from
the maintainers. :)

I'd love to have some comments on this rough version also (CVS diff
didn't do anything with this file, so there is no point to include that).

P.S. Undocumented detail: we need to put the user's name in the
connection settings anyway, as it won't work without it.

Regards,

Marc-André Laverdière
Software Security Scientist
Innovation Labs, Tata Consultancy Services
Hyderabad, India

On Wednesday 18 May 2011 12:32 PM, Craig Ringer wrote:
> On 18/05/2011 2:06 PM, Marc-André Laverdière wrote:
>> Hello,
>>
>> This implementations allows to specify which keystore and which
>> truststore to load. This allows certificate authentication in the
>> application easily.
> 
> It's already pretty easy. You can:
> 
> - Load your keys in the standard keystore;
> - Populate a new JECKSf-format keystore with your cert and key and use
> the javax.net.ssl.trustStore etc system properties on the cmdline or via
> System.setProperty() to use that store instead of the default store;
> - Provide your own javax.net.ssl.SSLSocketFactory that wraps the default
> implementation with any additional behavior you need.
> 
> PgJDBC could use some documentation/examples for the latter option, but
> it shouldn't need any changes to the PgJDBC code.
> 
>> The Java SSL API is not very well known to the JDBC driver developers
>> and we would be interested in any interesting and generally useful
>> extensions that you have implemented using this mechanism. Specifically
>> it would be nice to be able to provide client certificates to be
>> validated by the server.
>> </quote>
>>
>> What I'm talking about is this factory referred to in the last paragraph.
> 
> OK, so you've just written an easy-to-use canned SSLSocketFactory that
> loads client certificates from an application-configurable
> keystore/truststore?
> 
> I did this in my app to allow it to use a PKCS#12 format X.509
> certificate directly and it was a pretty low-pain procedure. It'd be
> nice to have more general purpose examples in the PgJDBC sources,
> though. Maybe I should dig out the examples I posted to this mailing
> list a while ago and submit them, too?
> 
> I'm not one of the PgJDBC developers, but I'd be happy to have a look at
> what you've done and see if I can be of any help, provide useful
> feedback, etc.
> 
> It'd probably be best if you published your work as a patch ("diff")
> against PgJDBC CVS HEAD, posting the patch to this mailing list so
> people can look at it and try it. Make sure the email you send the patch
> in clearly explains the reason for any change to the existing PgJDBC
> code, and if you do change the core PgJDBC code make sure it passes any
> tests and that it compiles under the oldest supported JDK.
> 

Attachments:

CertAuthFactory.java (text/x-java, 5.4K) download /*------------------------------------------------------------------------- * * Copyright (c) 2011, PostgreSQL Global Development Group * * *------------------------------------------------------------------------- */ package org.postgresql.ssl; import java.io.FileInputStream; import java.io.IOException; import java.net.Socket; import java.net.InetAddress; import java.net.UnknownHostException; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.security.SecureRandom; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManagerFactory; /** * Provide an implementation of SSLSocketFactory that allows client authentication * @author Marc-André Laverdière (marc-andre@atc.tcs.com / marcandre.laverdiere@tcs.com) */ public class CertAuthFactory extends SSLSocketFactory { public final static String CONFIG_KEYSTORE_PATH = "org.postgresql.jdbc.keystore.path"; public final static String CONFIG_KEYSTORE_PWD = "org.postgresql.jdbc.keystore.password"; public final static String CONFIG_TRUSTSTORE_PATH = "org.postgresql.jdbc.truststore.path"; public final static String CONFIG_TRUSTSTORE_PWD = "org.postgresql.jdbc.truststore.password"; public final static String SSL_PROTOCOL_NAME = "SSL"; public final static String SECURE_RANDOM_NAME = "SHA1PRNG"; protected final SSLSocketFactory _internalFactory; public CertAuthFactory() throws IOException, GeneralSecurityException { SSLContext context = buildSSLContext(); _internalFactory = context.getSocketFactory(); } public CertAuthFactory(String ignored) throws IOException, GeneralSecurityException { SSLContext context = buildSSLContext(); _internalFactory = context.getSocketFactory(); } /** * Builds an SSLContext with the specified trust store and key store * */ protected static SSLContext buildSSLContext() throws IOException, GeneralSecurityException{ FileInputStream fInKeyStore = null; FileInputStream fInTrustStore = null; try{ //Load configuration String trustPath = System.getProperty(CONFIG_TRUSTSTORE_PATH); String trustPwd = System.getProperty(CONFIG_TRUSTSTORE_PWD); String keyPath = System.getProperty(CONFIG_KEYSTORE_PATH); String keyPwd = System.getProperty(CONFIG_KEYSTORE_PWD); KeyManagerFactory managerFactory = null; TrustManagerFactory trustFactory = null; if (keyPath != null && !"".equals(keyPath)){ //Load the Key Managers KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); fInKeyStore = new FileInputStream(keyPath); ks.load(fInKeyStore, keyPwd.toCharArray()); managerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); managerFactory.init(ks, keyPwd.toCharArray()); } if (trustPath != null && !"".equals(trustPath)){ // Load the trust store KeyStore trustKs = KeyStore.getInstance(KeyStore.getDefaultType()); fInTrustStore = new FileInputStream(keyPath); trustKs.load(fInTrustStore, trustPwd.toCharArray()); trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustFactory.init(trustKs); } //Create + Initialize TLS context SSLContext context = SSLContext.getInstance(SSL_PROTOCOL_NAME); //can be TLS too context.init(managerFactory.getKeyManagers(), trustFactory.getTrustManagers(), SecureRandom.getInstance(SECURE_RANDOM_NAME)); return context; } finally{ try{ if (fInKeyStore != null) fInKeyStore.close(); if (fInTrustStore != null) fInTrustStore.close(); } catch (IOException e){ //ignore it } } } /** * Enables the client mode and the client authentication * */ protected SSLSocket enableClientAuth(SSLSocket sock){ sock.setNeedClientAuth(true); sock.setUseClientMode(true); return sock; } @Override public Socket createSocket() throws IOException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(); return enableClientAuth(sock); } @Override public Socket createSocket(InetAddress address, int port, InetAddress localhost, int localPort) throws IOException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(address, port, localhost, localPort); return enableClientAuth(sock); } @Override public Socket createSocket(InetAddress host, int port) throws IOException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(host, port); return enableClientAuth(sock); } @Override public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(s, host, port, autoClose); return enableClientAuth(sock); } @Override public Socket createSocket(String host, int port) throws IOException, UnknownHostException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(host, port); return enableClientAuth(sock); } @Override public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException, UnknownHostException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(host, port, localAddress, localPort); return enableClientAuth(sock); } @Override public String[] getDefaultCipherSuites() { return _internalFactory.getDefaultCipherSuites(); } @Override public String[] getSupportedCipherSuites() { return _internalFactory.getSupportedCipherSuites(); } }
^ permalink raw reply [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-18 13:19  Craig Ringer <craig@postnewspapers.com.au>
  parent: Marc-André Laverdière <marc-andre@atc.tcs.com>
  2 siblings, 1 reply; 28+ messages in thread

From: Craig Ringer @ 2011-05-18 13:19 UTC (permalink / raw)
  To: Marc-André Laverdière <marc-andre@atc.tcs.com>; +Cc: pgsql-jdbc

On 05/18/2011 03:48 PM, Marc-André Laverdière wrote:
> It is true that anyone who knows the right Java APIs can put that
> factory together with little pain. I just think that the average user
> shouldn't have to bother about it.

In that, I agree. Canned factories for simple cases like loading a 
PKCS#12 format cert file, loading an app-specified keystore with a 
callback for a password prompt, etc would make a lot of sense.

> So we end up with 3 different keystores (and maybe trust stores too),
> all of which may have different passwords. It is actually not that much
> of a big deal to use, but the 'defaults' won't help us here too much I
> think.

Quite right, if your different keystores must have different passwords 
then you'd need to implement your own factory.

> P.S. Undocumented detail: we need to put the user's name in the
> connection settings anyway, as it won't work without it.

Interesting. I've only ever used setups where X.509 certs are 
supplementary to traditional password authentication. I'm surprised that 
the username isn't just deduced from the X.509 DN.


A few comments on your code: Overall, it looks pretty sensible at first 
reading. There are a few things I noticed:

- The code doesn't accept arguments for keystore and truststore type, so 
it won't be possible to load (eg) a PKCS#12 certificate file directly. 
Only stores of the default types will work.

- It's not very subclassing-friendly, which could be an issue if it's to 
be shipped in PgJDBC.


Specific subclassing use cases I see are allowing different 
configuration mechanisms (Properties instances in thread-local storage, 
JNDI, BeanManager contexts, blah blah) and accepting pre-configured 
KeyStore & TrustStore instances. I needed to use the latter in my app, 
and the former could become an issue whenever you need to create 
CertAuthFactory instances with different configurations in the same app 
- for example, for different users.

Personally, I dislike the use of system properties for configuration of 
the class. It's totally thread-unsafe and is a real pain in any 
situation where you don't necessarily want exactly the same 
configuration for every instance. There's nothing wrong with having it 
as a convenient default, but it'd be nice to be able to override it with 
another configuration mechanism if this code is to ship with PgJDBC.

Alas the SSLSocketFactory interface doesn't offer any really nice 
options like a way to pass a Properties instance to the ctor, and 
accepting URL-encoded query-string like params as a String isn't 
attractive given their length and the potential password security 
issues. What I'd want to do is allow subclasses to override the 
configuration behaviour by:

- Lazily initializing _internalFactory on first access rather than in 
the ctor, so that:

- a subclass's ctor can do something useful and application-defined with 
the String parameter passed to the one-arg form, like reading a config 
file, looking up a Properties instance from a config service or JNDI, 
etc. This would require that:

- Configuration values are acessed via protected methods rather than 
direct System.getProperty() calls so those can be overridden by a 
subclass to get the information from elsewhere.

Ideally (IMO) also:

- the KeyStore instantiation and loading for ks and trustKs is done in 
protected methods too, so these can be completely overridden to obtain 
instances another way.

--
Craig Ringer



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-18 14:03  Craig Ringer <craig@postnewspapers.com.au>
  parent: Marc-André Laverdière <marc-andre@atc.tcs.com>
  2 siblings, 0 replies; 28+ messages in thread

From: Craig Ringer @ 2011-05-18 14:03 UTC (permalink / raw)
  To: Marc-André Laverdière <marc-andre@atc.tcs.com>; +Cc: pgsql-jdbc

Oh, also, your code has a simple thinko or copy-n-paste bug. On line 79:

fInTrustStore = new FileInputStream(keyPath);

should read:

fInTrustStore = new FileInputStream(trustPath);

Currently, trustPath never escapes the method and is effectively unused; 
it's only tested for null/empty, then ignored.

--
Craig Ringer



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-18 15:32  Kris Jurka <books@ejurka.com>
  parent: Craig Ringer <craig@postnewspapers.com.au>
  0 siblings, 1 reply; 28+ messages in thread

From: Kris Jurka @ 2011-05-18 15:32 UTC (permalink / raw)
  To: Craig Ringer <craig@postnewspapers.com.au>; +Cc: Marc-André Laverdière <marc-andre@atc.tcs.com>; pgsql-jdbc



On Wed, 18 May 2011, Craig Ringer wrote:

> Alas the SSLSocketFactory interface doesn't offer any really nice options 
> like a way to pass a Properties instance to the ctor,

That'd be an easy thing to fix, just add another case to 
org.postgresql.ssl.MakeSSL#convert to pass through the provided Properties 
object.  I've thought about that in the past, but with no real use case, I 
wasn't in a big rush to do so.

Kris Jurka



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-19 05:46  Craig Ringer <craig@postnewspapers.com.au>
  parent: Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 28+ messages in thread

From: Craig Ringer @ 2011-05-19 05:46 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: Marc-André Laverdière <marc-andre@atc.tcs.com>; pgsql-jdbc

On 05/18/2011 11:32 PM, Kris Jurka wrote:
>
>
> On Wed, 18 May 2011, Craig Ringer wrote:
>
>> Alas the SSLSocketFactory interface doesn't offer any really nice
>> options like a way to pass a Properties instance to the ctor,
>
> That'd be an easy thing to fix, just add another case to
> org.postgresql.ssl.MakeSSL#convert to pass through the provided
> Properties object. I've thought about that in the past, but with no real
> use case, I wasn't in a big rush to do so.

OK, good to know.

It wasn't a big issue for me in the end, as I landed up fetching my 
configuration using the String argument form as a key to an 
application-specific configuration provider. It's much easier to pass a 
String argument through various layers one often has over the JDBC 
driver, like an ORM or the like.

--
Craig Ringer



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-19 05:58  Craig Ringer <craig@postnewspapers.com.au>
  parent: Marc-André Laverdière <marc-andre@atc.tcs.com>
  2 siblings, 1 reply; 28+ messages in thread

From: Craig Ringer @ 2011-05-19 05:58 UTC (permalink / raw)
  To: Marc-André Laverdière <marc-andre@atc.tcs.com>; +Cc: pgsql-jdbc

On 05/18/2011 03:48 PM, Marc-André Laverdière wrote:
> It is true that anyone who knows the right Java APIs can put that
> factory together with little pain. I just think that the average user
> shouldn't have to bother about it.
>
> Also, some use cases do not allow to use the default keystore and trust
> store for everything.

I agree, and I think you're right that the PgJDBC  code should have a 
re-usable class to provide support for these use cases without everyone 
having to code their own.

I've taken your code and re-worked it a bit to:

- Use the PgJDBC WrappedFactory rather than re-implementing an
   effectively identical one;

- Separate the basic construction process of the SSLSocketFactory
   from its configuration, providing an abstract superclass that does
   the construction via a few simple method calls, so someone who needs
   different configuration sources/methods can re-use the code;

- Provided a concrete subclass of the above that gets its configuration
   from system properties and uses the superclass methods to construct
   the factory. This class has the same functionality as what you posted
   originally plus a few additional configuration options for additional
   use cases like non-default KeyStore types (pkcs12 etc); and

- Added more extensive JavaDoc

It's also possible to pass your own KeyStore and/or TrustStore instances 
directly if your app has to do something funky like manage an in-memory 
KeyStore.

What I haven't done yet is tested it! This is a preview so you can 
comment on it and tell me if you think I've gone completely off the 
rails or not.

The simplest uses of the code remain as simple as they were with your 
original version. You just set some system properties and specify 
SysPropCertAuthFactory as the sslsocketfactory class. More complex use 
cases become possible without having to re-implement the whole lot - in 
particular, if you can't fetch your configuration from system properties 
you can provide alternative mechanisms to look it up without having to 
re-write all the JSSE crap.

So: thanks VERY much for posting this code. My original demos weren't 
flexible enough to be included in PgJDBC, and by posting this you really 
helped motivate me to try to turn your code and my original demoes into 
something that _was_.

What do you think? Note that you'll need PgJDBC on your classpath to 
complile this now, because it uses org.postgresql.ssl.WrappedFactory  .

--
Craig Ringer

Attachments:

AbstractCertAuthFactory.java (text/plain, 7.3K) download /*------------------------------------------------------------------------- * * Copyright (c) 2011, PostgreSQL Global Development Group * * *------------------------------------------------------------------------- */ package org.postgresql.ssl; import java.io.FileInputStream; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.security.SecureRandom; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; /** * Provide an implementation of SSLSocketFactory that allows client authentication. * * This code creates and wraps a SSLSocketFactory using the paths and passwords * obtained from calls to abstract methods that must be defined by a subclass. * You cannot use this class as-is. See SysPropCertAuthFactory for one you can * use unmodified. * * Subclasses <b>must</b> call buildSSLSocketFactory() at the end of their * constructors, and <b>must</b> call the superclass constructors at the beginning * of their constructors. Subclasses must also implement the abstract methods * so that appropriate configuration details are returned. * * @author Marc-André Laverdière (marc-andre@atc.tcs.com / marcandre.laverdiere@tcs.com) */ //// //// TODO: Delegate trust checks to system trustmanager if //// no match in our own trustmanager? //// //// TODO: way to override keymanager/trustmanager supply without //// haivng to override those abstracts? public abstract class AbstractCertAuthFactory extends WrappedFactory { protected final static String DEFAULT_SSL_PROTOCOL_NAME = "SSL"; protected AbstractCertAuthFactory() { } protected AbstractCertAuthFactory(String ignored) { } /** * Builds an SSLContext with a trust store and key store constructed * using the parameters provided by the subclass's implementation of * AbstractCertAuthFactory's abstract methods. * * If a null value is passed for any argument, the system default will * be used. For example, a null keyManagers array will cause * the SSLSocketFactory to use an SSLContext with a system-default KeyStore. * On the Sun JRE, this is created using the JSSE javax.net.ssl system * properties. Similar rules apply for the TrustStore and SecureRandom instance. * * See http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html . * * @param keyManagers KeyManager(s) to obtain key material from, or null for JSSE default keystore * @param trustManagers TrustManager(s) to obtain trust material from, or null for JSSE default truststore * @param secureRandom Secure random generator. Unless you know you need to do otherwise, leave this null to use the default. * */ protected void buildSSLSocketFactory(KeyManager[] keyManagers, TrustManager[] trustManagers, SecureRandom secureRandom) throws IOException, GeneralSecurityException{ if (_factory != null) { throw new IllegalStateException("buildSSLSocketFactory() already called!"); } //Create + Initialize TLS context SSLContext context = SSLContext.getInstance(getSSLProtocolName()); context.init(keyManagers, trustManagers, secureRandom); _factory = context.getSocketFactory(); } /** * Create and return KeyManager(s) using the passed information. * * If keyStoreType is null, the default keystore type for the system default * JSSE provider will be used. If the keyPass is null, the keyStorePass will * also be used to decode the key. No other arguments may be null. * * @param keyStorePath Location of keystore on file system. Non-null. * @param keyStorePass Password/passphrase to decrypt keystore with. Non-null. * @param keyPass Password/passphrase to decrypt key(s) of interest within KeyStore with. If null, keyStorePass used. * @param keyStoreType JSSE keystore type. If null, JSSE provider default is used. * @return KeyStore(s) created * @throws IOException if the KeyStore could not be read * @throws GeneralSecurityException (subtypes of) for most crypto errors, passphrase errors, etc. */ protected KeyManager[] createKeyManagers(String keyStorePath, char[] keyStorePass, char[] keyPass, String keyStoreType) throws IOException, GeneralSecurityException { //Load the Key Managers if (keyStoreType == null) { keyStoreType = KeyStore.getDefaultType(); } if (keyPass == null) { keyPass = keyStorePass; } KeyStore ks = KeyStore.getInstance(keyStoreType); FileInputStream fInKeyStore = new FileInputStream(keyStorePath); try { ks.load(fInKeyStore, keyStorePass); } finally { try { fInKeyStore.close(); } catch (IOException ex) { // Ignored } } KeyManagerFactory managerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); managerFactory.init(ks, keyPass); return managerFactory.getKeyManagers(); } /** * Create and return TrustManager(s) using the passed information. * * If trustStoreType is null, the JSSE default keystore type is used. * No other arguments may be null. * * If you want to use the Java default truststore, do not call this method * at all. Just pass a null TrustManager[] as the trustManagers argument to * buildSSLSocketFactory(...). * * @param trustStorePath Location of trust store on file system. Non-null. * @param trustStorePass Password to decrypt truststore. * @param trustStoreType JSSE TrustStore type. If null, default JSSE keystore type is used. * @return TrustStore(s) created * @throws IOException If the TrustStore cannot be read * @throws GeneralSecurityException (subtypes of) for most crypto errors, password errors, etc. */ protected TrustManager[] createTrustManagers(String trustStorePath, char[] trustStorePass, String trustStoreType) throws IOException, GeneralSecurityException { // Load the trust store if (trustStoreType == null) { trustStoreType = KeyStore.getDefaultType(); } KeyStore trustKs = KeyStore.getInstance(trustStoreType); FileInputStream fInTrustStore = new FileInputStream(trustStorePath); try { trustKs.load(fInTrustStore, trustStorePass); } finally { try { fInTrustStore.close(); } catch (IOException ex) { // Ignored } } TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustFactory.init(trustKs); return trustFactory.getTrustManagers(); } /** * If you wish to override SSL protocol selection, you may do so by * overriding this method. The defualt simply returns * AbstractCertAuthFactory.DEFAULT_SSL_PROTOCOL_NAME . * . * @return JSSE ssl/tls protocol name AbstractCertAuthFactory.DEFAULT_SSL_PROTOCOL_NAME */ protected String getSSLProtocolName() { return DEFAULT_SSL_PROTOCOL_NAME; } }
SysPropCertAuthFactory.java (text/plain, 5.6K) download /*------------------------------------------------------------------------- * * Copyright (c) 2011, PostgreSQL Global Development Group * * *------------------------------------------------------------------------- */ package org.postgresql.ssl; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.SecureRandom; import javax.net.ssl.KeyManager; import javax.net.ssl.TrustManager; /** * A CertAuthFactory that's configured using system properties, much like * (but independent from) the stock javax.net.ssl SSLSocketFactory implementation. */ public class SysPropCertAuthFactory extends AbstractCertAuthFactory { /** * File-system path to the keystore file to load, or null for system default keystore. * * May be the set to the same path as CONFIG_TRUSTSTORE_PATH if trust and key material are * in the same store. */ public final static String CONFIG_KEYSTORE_PATH = "org.postgresql.jdbc.keystore.path"; /** * Password to use to decode keystore. Ignored if CONFIG_KEYSTORE_PATH unset. */ public final static String CONFIG_KEYSTORE_PWD = "org.postgresql.jdbc.keystore.password"; /** * Password to use to decode keys within keystore. If unset, CONFIG_KEYSTORE_PWD used. * In most cases the KeyStore password is the same as the key password, so this may be * left unset. */ public final static String CONFIG_KEYSTORE_KEYPWD = "org.postgresql.jdbc.keystore.keypassword"; /** * KeyStore type, a JSSE KeyStore type name like JKS, JECKS, pkcs12, etc. Ignored if CONFIG_KEYSTORE_PATH unset. * * JECKS is the usual format used to store private keys with keytool, etc. */ public final static String CONFIG_KEYSTORE_TYPE = "org.postgresql.jdbc.keystore.type"; /** * File system path to the trust store file to load, or null for the system default trust store. * * May be the same path as CONFIG_KEYSTORE_PATH if trust and key material are * in the same store. * * The default Sun JDK trustStore is a JKS format store with password "changeme". * If you wish to use the system-default list of trusted signers, rather than * specifying the location, type, and password of the system default truststore * you should leave CONFIG_TRUSTSTORE_PATH unset so the system default truststore * is automatically loaded for you. */ public final static String CONFIG_TRUSTSTORE_PATH = "org.postgresql.jdbc.truststore.path"; /** * Password used to decode the truststore. Ignored if CONFIG_TRUSTSTORE_PATH unset. */ public final static String CONFIG_TRUSTSTORE_PWD = "org.postgresql.jdbc.truststore.password"; /** * TrustStore type. Just like CONFIG_KEYSTORE_TYPE, but for the TrustStore. * * If the TrustStore is not the same store as the KeyStore, it is common * to use the JKS format for it rather than the JECKS format usually used * for KeyStores and for combined TrustStore/KeyStores. */ public final static String CONFIG_TRUSTSTORE_TYPE = "org.postgresql.jdbc.truststore.type"; /** * The secure random algorithm to use. It is unlikely that you will need to change * this, but support for doing so is provided for completeness. */ public final static String CONFIG_SECURERANDOM = "org.postgresql.jdbc.securerandom"; public SysPropCertAuthFactory() throws IOException, GeneralSecurityException { super(); // Load configuration. First, configure and create the key manager: KeyManager[] keyManagers = null; String keyStorePath = System.getProperty(CONFIG_KEYSTORE_PATH); if (keyStorePath != null && !keyStorePath.isEmpty()) { // If only a keystore passphrase is specified but no // key passphrase is given, use the keystore passphrase for both the keystore // and the key. char[] keyStorePass = null, keyPass = null; String ksp = System.getProperty(CONFIG_KEYSTORE_PWD, null); if (ksp != null) { keyStorePass = ksp.toCharArray(); keyPass = keyStorePass; } String kp = System.getProperty(CONFIG_KEYSTORE_KEYPWD, null); if (kp != null) { keyPass = kp.toCharArray(); } keyManagers = createKeyManagers( keyStorePath, keyStorePass, keyPass, System.getProperty(CONFIG_KEYSTORE_TYPE, null)); } // then create the trust manager TrustManager[] trustManagers = null; String trustStorePath = System.getProperty(CONFIG_TRUSTSTORE_PATH); if (trustStorePath != null && !trustStorePath.isEmpty()) { char[] trustStorePass = null; String tsp = System.getProperty(CONFIG_TRUSTSTORE_PATH, null); if (tsp != null) { trustStorePass = tsp.toCharArray(); } trustManagers = createTrustManagers( trustStorePath, trustStorePass, System.getProperty(CONFIG_TRUSTSTORE_TYPE)); } // and SecureRandom instance if configured SecureRandom secureRandom = null; String secureRandomAlgo = System.getProperty(CONFIG_SECURERANDOM); if (secureRandomAlgo != null) { secureRandom = SecureRandom.getInstance(secureRandomAlgo); } // Finally, configure the factory. buildSSLSocketFactory(keyManagers, trustManagers, secureRandom); } public SysPropCertAuthFactory(String ignored) throws IOException, GeneralSecurityException { this(); } }
^ permalink raw reply [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-19 06:14  Marc-André Laverdière <marc-andre@atc.tcs.com>
  parent: Craig Ringer <craig@postnewspapers.com.au>
  0 siblings, 1 reply; 28+ messages in thread

From: Marc-André Laverdière @ 2011-05-19 06:14 UTC (permalink / raw)
  To: Craig Ringer <craig@postnewspapers.com.au>; +Cc: Kris Jurka <books@ejurka.com>; pgsql-jdbc

Hi guys,

Here are some improvements based on the comments I saw so far.
I'm also adding a small cheapo tester. Definitely not JUnit-ready :)

Marc-André Laverdière
Software Security Scientist
Innovation Labs, Tata Consultancy Services
Hyderabad, India

On Thursday 19 May 2011 11:16 AM, Craig Ringer wrote:
> On 05/18/2011 11:32 PM, Kris Jurka wrote:
>>
>>
>> On Wed, 18 May 2011, Craig Ringer wrote:
>>
>>> Alas the SSLSocketFactory interface doesn't offer any really nice
>>> options like a way to pass a Properties instance to the ctor,
>>
>> That'd be an easy thing to fix, just add another case to
>> org.postgresql.ssl.MakeSSL#convert to pass through the provided
>> Properties object. I've thought about that in the past, but with no real
>> use case, I wasn't in a big rush to do so.
> 
> OK, good to know.
> 
> It wasn't a big issue for me in the end, as I landed up fetching my
> configuration using the String argument form as a key to an
> application-specific configuration provider. It's much easier to pass a
> String argument through various layers one often has over the JDBC
> driver, like an ORM or the like.
> 
> -- 
> Craig Ringer

Attachments:

PostgreSQLCertConnTester.java (text/x-java, 1.6K) download /*------------------------------------------------------------------------- * * Copyright (c) 2011, PostgreSQL Global Development Group * * *------------------------------------------------------------------------- */ package org.postgresql.ssl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; /** * * Quick and dirty tester for the cert auth factory * @author Marc-Andre Laverdiere * */ public class PostgreSQLCertConnTester { public static void main(String[] args) throws ClassNotFoundException, SQLException{ // ========== TODO TODO TODO Set those values as needed for your test final String userId = ""; //Set the properties System.setProperty(CertAuthFactory.CONFIG_KEYSTORE_PATH, ""); System.setProperty(CertAuthFactory.CONFIG_KEYSTORE_PWD, ""); System.setProperty(CertAuthFactory.CONFIG_TRUSTSTORE_PATH, ""); System.setProperty(CertAuthFactory.CONFIG_TRUSTSTORE_PWD, ""); // ============== End TODO section final String connUrl = "jdbc:postgresql://127.0.0.1:5432/postgres?ssl=true&sslfactory=org.postgresql.ssl.CertAuthFactory"; final String query = "select * from pg_largeobject"; Class.forName("org.postgresql.Driver"); Properties props = new Properties(); props.setProperty("user",userId);//it somehow picks my logged-in user here Connection conn = DriverManager.getConnection(connUrl,props); PreparedStatement stmt = conn.prepareStatement(query); stmt.execute(); ResultSet rs = stmt.getResultSet(); while(rs.next()){ System.out.println("Reading row #" + rs.getRow()); } } }
CertAuthFactory.java (text/x-java, 8.5K) download /*------------------------------------------------------------------------- * * Copyright (c) 2011, PostgreSQL Global Development Group * * *------------------------------------------------------------------------- */ package org.postgresql.ssl; import java.io.FileInputStream; import java.io.IOException; import java.net.Socket; import java.net.InetAddress; import java.net.UnknownHostException; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.security.SecureRandom; import java.util.Properties; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManagerFactory; /** * Provide an implementation of SSLSocketFactory that allows client authentication * @author Marc-André Laverdière (marc-andre@atc.tcs.com / marcandre.laverdiere@tcs.com) */ public class CertAuthFactory extends SSLSocketFactory { // ---------------------------- Constants used internally public final static String CONFIG_KEYSTORE_PATH = "org.postgresql.jdbc.keystore.path"; public final static String CONFIG_KEYSTORE_PWD = "org.postgresql.jdbc.keystore.password"; public final static String CONFIG_TRUSTSTORE_PATH = "org.postgresql.jdbc.truststore.path"; public final static String CONFIG_TRUSTSTORE_PWD = "org.postgresql.jdbc.truststore.password"; public final static String SSL_PROTOCOL_NAME = "SSL"; public final static String TLS_PROTOCOL_NAME = "TLS"; public final static String SECURE_RANDOM_NAME = "SHA1PRNG"; public final static String KEYSTORE_TYPE_PKCS12 = "PKCS12"; public final static String KEYSTORE_TYPE_JKS = "JKS"; //------------------------------- Actual instance data protected final SSLSocketFactory _internalFactory; protected final boolean isUsingCertificateAuth; //------------------------------ Constructors public CertAuthFactory() throws IOException, GeneralSecurityException { this(System.getProperties()); } public CertAuthFactory(String ignored) throws IOException, GeneralSecurityException { this(System.getProperties()); } /** * Builds an SSLContext with the specified trust store and key store. * The parameters read are as follows: * <ul> * <li> (optional) <code>org.postgresql.jdbc.keystore.path</code> : the path to the keystore containing the client certificate * <li> (mandatory if previous is set) <code>org.postgresql.jdbc.keystore.password</code> : password for loading the keystore containing the client certificate * <li> (optional) <code>org.postgresql.jdbc.truststore.path</code> : keystore containing the CA certificate(s) that are trusted. * This should normally be the same CA as the one that signed the certificate of the server you would connect to. * <li> (mandatory if previous is set) <code>org.postgresql.jdbc.truststore.password</code> : the password to open the keystore containing the CA certificate(s) * </ul> * @param props the properties to load the configuration from. Must be non-null. * @return an SSLContext initialized based on those properties. * @throws IOException on any error loading the keystores * @throws GeneralSecurityException on keystore errors or crypto set-up errors */ public CertAuthFactory(Properties props) throws IOException, GeneralSecurityException { if (props == null) throw new IllegalArgumentException("Properties is null"); FileInputStream fInKeyStore = null; FileInputStream fInTrustStore = null; try{ //Load configuration String trustPath = props.getProperty(CONFIG_TRUSTSTORE_PATH); String trustPwd = props.getProperty(CONFIG_TRUSTSTORE_PWD); String keyPath = props.getProperty(CONFIG_KEYSTORE_PATH); String keyPwd = props.getProperty(CONFIG_KEYSTORE_PWD); KeyManagerFactory managerFactory = null; TrustManagerFactory trustFactory = null; if (keyPath != null && !"".equals(keyPath)){ if (keyPwd == null) throw new IllegalArgumentException("Keystore password must be specified"); //Load the Key Managers KeyStore ks = loadKeyStore(keyPath, keyPwd); managerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); managerFactory.init(ks, keyPwd.toCharArray()); isUsingCertificateAuth = true; } else isUsingCertificateAuth = false; if (trustPath != null && !"".equals(trustPath)){ if (trustPwd == null) throw new IllegalArgumentException("Trust store password must be specified"); // Load the trust store KeyStore trustKs = loadKeyStore(trustPath, trustPwd); trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustFactory.init(trustKs); } //Create + Initialize TLS context SSLContext context = SSLContext.getInstance(SSL_PROTOCOL_NAME); //can be TLS too context.init(managerFactory.getKeyManagers(), trustFactory.getTrustManagers(), SecureRandom.getInstance(SECURE_RANDOM_NAME)); _internalFactory = context.getSocketFactory(); } finally{ try{ if (fInKeyStore != null) fInKeyStore.close(); if (fInTrustStore != null) fInTrustStore.close(); } catch (IOException e){ //ignore it } } } //------------------------- SSLSocketFactory API implementation @Override public Socket createSocket() throws IOException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(); return enableClientAuth(sock); } @Override public Socket createSocket(InetAddress address, int port, InetAddress localhost, int localPort) throws IOException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(address, port, localhost, localPort); return enableClientAuth(sock); } @Override public Socket createSocket(InetAddress host, int port) throws IOException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(host, port); return enableClientAuth(sock); } @Override public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(s, host, port, autoClose); return enableClientAuth(sock); } @Override public Socket createSocket(String host, int port) throws IOException, UnknownHostException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(host, port); return enableClientAuth(sock); } @Override public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException, UnknownHostException { SSLSocket sock = (SSLSocket) _internalFactory.createSocket(host, port, localAddress, localPort); return enableClientAuth(sock); } @Override public String[] getDefaultCipherSuites() { return _internalFactory.getDefaultCipherSuites(); } @Override public String[] getSupportedCipherSuites() { return _internalFactory.getSupportedCipherSuites(); } // -------------------------------------- Internal Helper Methods /** * Load a keystore at the path specified. It will try both JKS and PKCS12 keystores * @param path the path to the keystore * @param password the keystore password * */ protected static KeyStore loadKeyStore(String path, String password) throws IOException, GeneralSecurityException{ if (path == null || "".equals(path)) throw new IllegalArgumentException("Path is empty or null"); if (password == null) throw new IllegalArgumentException("Password is null"); //first try with JKS try{ return loadKeyStore(path, password, KEYSTORE_TYPE_JKS); } catch (IOException e){ //docs say that this is what is loaded if the file format is wrong //try loading PKCS instead return loadKeyStore(path, password, KEYSTORE_TYPE_PKCS12); } } /** * Tries to open a keystore of the given type * @param path the path to the keystore * @param password the keystore password * @param type the keystore type * @return a valid keystore * @throws IOException If there is any error loading the keystore * @throws GeneralSecurityException if the certificates cannot be loaded or the type specified is invalid */ protected static KeyStore loadKeyStore(String path, String password, String type) throws IOException, GeneralSecurityException{ FileInputStream fIn = null; try{ KeyStore ks = KeyStore.getInstance(type); fIn = new FileInputStream(path); ks.load(fIn, password.toCharArray()); return ks; } finally{ if (fIn != null) fIn.close(); } } /** * Enables the client mode and the client authentication (if a certificate was loaded) * on the socket * */ protected SSLSocket enableClientAuth(SSLSocket sock){ sock.setNeedClientAuth(isUsingCertificateAuth); sock.setUseClientMode(true); return sock; } }
^ permalink raw reply [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-19 06:41  Marc-André Laverdière <marc-andre@atc.tcs.com>
  parent: Craig Ringer <craig@postnewspapers.com.au>
  0 siblings, 1 reply; 28+ messages in thread

From: Marc-André Laverdière @ 2011-05-19 06:41 UTC (permalink / raw)
  To: Craig Ringer <craig@postnewspapers.com.au>; +Cc: pgsql-jdbc

Hello,

Thanks for the changes. It is a good step forward in terms of
extensibility. That being said, I think we could just put a lot of the
loading code in AbstractCertAuthFactory, so that it is more reusable.

There was a tiny bug in SysPropCertAuthFactory whereas a path was sent
instead of a password. I'm attaching the fix.

I also added the code that allows to load both PKCS12 and JKS blindly. I
think that we can remove the property for specifying the type. I  doubt
we have to worry about other types of keystores than those two.

I'm also adding the slightly modified tester which uses the right class.

Regards,

Marc-André Laverdière
Software Security Scientist
Innovation Labs, Tata Consultancy Services
Hyderabad, India

On Thursday 19 May 2011 11:28 AM, Craig Ringer wrote:
> On 05/18/2011 03:48 PM, Marc-André Laverdière wrote:
>> It is true that anyone who knows the right Java APIs can put that
>> factory together with little pain. I just think that the average user
>> shouldn't have to bother about it.
>>
>> Also, some use cases do not allow to use the default keystore and trust
>> store for everything.
> 
> I agree, and I think you're right that the PgJDBC  code should have a
> re-usable class to provide support for these use cases without everyone
> having to code their own.
> 
> I've taken your code and re-worked it a bit to:
> 
> - Use the PgJDBC WrappedFactory rather than re-implementing an
>   effectively identical one;
> 
> - Separate the basic construction process of the SSLSocketFactory
>   from its configuration, providing an abstract superclass that does
>   the construction via a few simple method calls, so someone who needs
>   different configuration sources/methods can re-use the code;
> 
> - Provided a concrete subclass of the above that gets its configuration
>   from system properties and uses the superclass methods to construct
>   the factory. This class has the same functionality as what you posted
>   originally plus a few additional configuration options for additional
>   use cases like non-default KeyStore types (pkcs12 etc); and
> 
> - Added more extensive JavaDoc
> 
> It's also possible to pass your own KeyStore and/or TrustStore instances
> directly if your app has to do something funky like manage an in-memory
> KeyStore.
> 
> What I haven't done yet is tested it! This is a preview so you can
> comment on it and tell me if you think I've gone completely off the
> rails or not.
> 
> The simplest uses of the code remain as simple as they were with your
> original version. You just set some system properties and specify
> SysPropCertAuthFactory as the sslsocketfactory class. More complex use
> cases become possible without having to re-implement the whole lot - in
> particular, if you can't fetch your configuration from system properties
> you can provide alternative mechanisms to look it up without having to
> re-write all the JSSE crap.
> 
> So: thanks VERY much for posting this code. My original demos weren't
> flexible enough to be included in PgJDBC, and by posting this you really
> helped motivate me to try to turn your code and my original demoes into
> something that _was_.
> 
> What do you think? Note that you'll need PgJDBC on your classpath to
> complile this now, because it uses org.postgresql.ssl.WrappedFactory  .
> 
> -- 
> Craig Ringer

Attachments:

PostgreSQLCertConnTester.java (text/x-java, 1.7K) download /*------------------------------------------------------------------------- * * Copyright (c) 2011, PostgreSQL Global Development Group * * *------------------------------------------------------------------------- */ package org.postgresql.ssl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; /** * * Quick and dirty tester for the cert auth factory * @author Marc-Andre Laverdiere * */ public class PostgreSQLCertConnTester { public static void main(String[] args) throws ClassNotFoundException, SQLException{ // ========== TODO TODO TODO Set those values as needed for your test final String userId = ""; //Set the properties System.setProperty(SysPropCertAuthFactory.CONFIG_KEYSTORE_PATH, ""); System.setProperty(SysPropCertAuthFactory.CONFIG_KEYSTORE_PWD, ""); System.setProperty(SysPropCertAuthFactory.CONFIG_TRUSTSTORE_PATH, ""); System.setProperty(SysPropCertAuthFactory.CONFIG_TRUSTSTORE_PWD, ""); // ============== End TODO section final String connUrl = "jdbc:postgresql://127.0.0.1:5432/postgres?ssl=true&sslfactory=org.postgresql.ssl.SysPropCertAuthFactory"; final String query = "select * from pg_largeobject"; Class.forName("org.postgresql.Driver"); Properties props = new Properties(); props.setProperty("user",userId);//it somehow picks my logged-in user here Connection conn = DriverManager.getConnection(connUrl,props); PreparedStatement stmt = conn.prepareStatement(query); stmt.execute(); ResultSet rs = stmt.getResultSet(); while(rs.next()){ System.out.println("Reading row #" + rs.getRow()); } } }
SysPropCertAuthFactory.java (text/x-java, 5.6K) download /*------------------------------------------------------------------------- * * Copyright (c) 2011, PostgreSQL Global Development Group * * *------------------------------------------------------------------------- */ package org.postgresql.ssl; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.SecureRandom; import javax.net.ssl.KeyManager; import javax.net.ssl.TrustManager; /** * A CertAuthFactory that's configured using system properties, much like * (but independent from) the stock javax.net.ssl SSLSocketFactory implementation. */ public class SysPropCertAuthFactory extends AbstractCertAuthFactory { /** * File-system path to the keystore file to load, or null for system default keystore. * * May be the set to the same path as CONFIG_TRUSTSTORE_PATH if trust and key material are * in the same store. */ public final static String CONFIG_KEYSTORE_PATH = "org.postgresql.jdbc.keystore.path"; /** * Password to use to decode keystore. Ignored if CONFIG_KEYSTORE_PATH unset. */ public final static String CONFIG_KEYSTORE_PWD = "org.postgresql.jdbc.keystore.password"; /** * Password to use to decode keys within keystore. If unset, CONFIG_KEYSTORE_PWD used. * In most cases the KeyStore password is the same as the key password, so this may be * left unset. */ public final static String CONFIG_KEYSTORE_KEYPWD = "org.postgresql.jdbc.keystore.keypassword"; /** * KeyStore type, a JSSE KeyStore type name like JKS, JECKS, pkcs12, etc. Ignored if CONFIG_KEYSTORE_PATH unset. * * JECKS is the usual format used to store private keys with keytool, etc. */ public final static String CONFIG_KEYSTORE_TYPE = "org.postgresql.jdbc.keystore.type"; /** * File system path to the trust store file to load, or null for the system default trust store. * * May be the same path as CONFIG_KEYSTORE_PATH if trust and key material are * in the same store. * * The default Sun JDK trustStore is a JKS format store with password "changeme". * If you wish to use the system-default list of trusted signers, rather than * specifying the location, type, and password of the system default truststore * you should leave CONFIG_TRUSTSTORE_PATH unset so the system default truststore * is automatically loaded for you. */ public final static String CONFIG_TRUSTSTORE_PATH = "org.postgresql.jdbc.truststore.path"; /** * Password used to decode the truststore. Ignored if CONFIG_TRUSTSTORE_PATH unset. */ public final static String CONFIG_TRUSTSTORE_PWD = "org.postgresql.jdbc.truststore.password"; /** * TrustStore type. Just like CONFIG_KEYSTORE_TYPE, but for the TrustStore. * * If the TrustStore is not the same store as the KeyStore, it is common * to use the JKS format for it rather than the JECKS format usually used * for KeyStores and for combined TrustStore/KeyStores. */ public final static String CONFIG_TRUSTSTORE_TYPE = "org.postgresql.jdbc.truststore.type"; /** * The secure random algorithm to use. It is unlikely that you will need to change * this, but support for doing so is provided for completeness. */ public final static String CONFIG_SECURERANDOM = "org.postgresql.jdbc.securerandom"; public SysPropCertAuthFactory() throws IOException, GeneralSecurityException { super(); // Load configuration. First, configure and create the key manager: KeyManager[] keyManagers = null; String keyStorePath = System.getProperty(CONFIG_KEYSTORE_PATH); if (keyStorePath != null && !keyStorePath.isEmpty()) { // If only a keystore passphrase is specified but no // key passphrase is given, use the keystore passphrase for both the keystore // and the key. char[] keyStorePass = null, keyPass = null; String ksp = System.getProperty(CONFIG_KEYSTORE_PWD, null); if (ksp != null) { keyStorePass = ksp.toCharArray(); keyPass = keyStorePass; } String kp = System.getProperty(CONFIG_KEYSTORE_KEYPWD, null); if (kp != null) { keyPass = kp.toCharArray(); } keyManagers = createKeyManagers( keyStorePath, keyStorePass, keyPass, System.getProperty(CONFIG_KEYSTORE_TYPE, null)); } // then create the trust manager TrustManager[] trustManagers = null; String trustStorePath = System.getProperty(CONFIG_TRUSTSTORE_PATH); if (trustStorePath != null && !trustStorePath.isEmpty()) { char[] trustStorePass = null; String tsp = System.getProperty(CONFIG_TRUSTSTORE_PWD, null); if (tsp != null) { trustStorePass = tsp.toCharArray(); } trustManagers = createTrustManagers( trustStorePath, trustStorePass, System.getProperty(CONFIG_TRUSTSTORE_TYPE)); } // and SecureRandom instance if configured SecureRandom secureRandom = null; String secureRandomAlgo = System.getProperty(CONFIG_SECURERANDOM); if (secureRandomAlgo != null) { secureRandom = SecureRandom.getInstance(secureRandomAlgo); } // Finally, configure the factory. buildSSLSocketFactory(keyManagers, trustManagers, secureRandom); } public SysPropCertAuthFactory(String ignored) throws IOException, GeneralSecurityException { this(); } }
AbstractCertAuthFactory.java (text/x-java, 8.3K) download /*------------------------------------------------------------------------- * * Copyright (c) 2011, PostgreSQL Global Development Group * * *------------------------------------------------------------------------- */ package org.postgresql.ssl; import java.io.FileInputStream; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.security.SecureRandom; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; /** * Provide an implementation of SSLSocketFactory that allows client authentication. * * This code creates and wraps a SSLSocketFactory using the paths and passwords * obtained from calls to abstract methods that must be defined by a subclass. * You cannot use this class as-is. See SysPropCertAuthFactory for one you can * use unmodified. * * Subclasses <b>must</b> call buildSSLSocketFactory() at the end of their * constructors, and <b>must</b> call the superclass constructors at the beginning * of their constructors. Subclasses must also implement the abstract methods * so that appropriate configuration details are returned. * * @author Marc-André Laverdière (marc-andre@atc.tcs.com / marcandre.laverdiere@tcs.com) */ //// //// TODO: Delegate trust checks to system trustmanager if //// no match in our own trustmanager? //// //// TODO: way to override keymanager/trustmanager supply without //// haivng to override those abstracts? public abstract class AbstractCertAuthFactory extends WrappedFactory { protected final static String DEFAULT_SSL_PROTOCOL_NAME = "SSL"; public final static String KEYSTORE_TYPE_PKCS12 = "PKCS12"; public final static String KEYSTORE_TYPE_JKS = "JKS"; protected AbstractCertAuthFactory() { } protected AbstractCertAuthFactory(String ignored) { } /** * Builds an SSLContext with a trust store and key store constructed * using the parameters provided by the subclass's implementation of * AbstractCertAuthFactory's abstract methods. * * If a null value is passed for any argument, the system default will * be used. For example, a null keyManagers array will cause * the SSLSocketFactory to use an SSLContext with a system-default KeyStore. * On the Sun JRE, this is created using the JSSE javax.net.ssl system * properties. Similar rules apply for the TrustStore and SecureRandom instance. * * See http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html . * * @param keyManagers KeyManager(s) to obtain key material from, or null for JSSE default keystore * @param trustManagers TrustManager(s) to obtain trust material from, or null for JSSE default truststore * @param secureRandom Secure random generator. Unless you know you need to do otherwise, leave this null to use the default. * */ protected void buildSSLSocketFactory(KeyManager[] keyManagers, TrustManager[] trustManagers, SecureRandom secureRandom) throws IOException, GeneralSecurityException{ if (_factory != null) { throw new IllegalStateException("buildSSLSocketFactory() already called!"); } //Create + Initialize TLS context SSLContext context = SSLContext.getInstance(getSSLProtocolName()); context.init(keyManagers, trustManagers, secureRandom); _factory = context.getSocketFactory(); } /** * Create and return KeyManager(s) using the passed information. * * If keyStoreType is null, the default keystore type for the system default * JSSE provider will be used. If the keyPass is null, the keyStorePass will * also be used to decode the key. No other arguments may be null. * * @param keyStorePath Location of keystore on file system. Non-null. * @param keyStorePass Password/passphrase to decrypt keystore with. Non-null. * @param keyPass Password/passphrase to decrypt key(s) of interest within KeyStore with. If null, keyStorePass used. * @param keyStoreType JSSE keystore type. If null, JSSE provider default is used. * @return KeyStore(s) created * @throws IOException if the KeyStore could not be read * @throws GeneralSecurityException (subtypes of) for most crypto errors, passphrase errors, etc. */ protected KeyManager[] createKeyManagers(String keyStorePath, char[] keyStorePass, char[] keyPass, String keyStoreType) throws IOException, GeneralSecurityException { //Load the Key Managers if (keyStoreType == null) { keyStoreType = KeyStore.getDefaultType(); } if (keyPass == null) { keyPass = keyStorePass; } KeyStore ks = loadKeyStore(keyStorePath, keyStorePass); KeyManagerFactory managerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); managerFactory.init(ks, keyPass); return managerFactory.getKeyManagers(); } /** * Create and return TrustManager(s) using the passed information. * * If trustStoreType is null, the JSSE default keystore type is used. * No other arguments may be null. * * If you want to use the Java default truststore, do not call this method * at all. Just pass a null TrustManager[] as the trustManagers argument to * buildSSLSocketFactory(...). * * @param trustStorePath Location of trust store on file system. Non-null. * @param trustStorePass Password to decrypt truststore. * @param trustStoreType JSSE TrustStore type. If null, default JSSE keystore type is used. * @return TrustStore(s) created * @throws IOException If the TrustStore cannot be read * @throws GeneralSecurityException (subtypes of) for most crypto errors, password errors, etc. */ protected TrustManager[] createTrustManagers(String trustStorePath, char[] trustStorePass, String trustStoreType) throws IOException, GeneralSecurityException { // Load the trust store if (trustStoreType == null) { trustStoreType = KeyStore.getDefaultType(); } KeyStore trustKs = loadKeyStore(trustStorePath, trustStorePass); TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustFactory.init(trustKs); return trustFactory.getTrustManagers(); } /** * If you wish to override SSL protocol selection, you may do so by * overriding this method. The defualt simply returns * AbstractCertAuthFactory.DEFAULT_SSL_PROTOCOL_NAME . * . * @return JSSE ssl/tls protocol name AbstractCertAuthFactory.DEFAULT_SSL_PROTOCOL_NAME */ protected String getSSLProtocolName() { return DEFAULT_SSL_PROTOCOL_NAME; } // -------------------------------------- Internal Helper Methods /** * Load a keystore at the path specified. It will try both JKS and PKCS12 keystores * @param path the path to the keystore * @param password the keystore password * */ protected static KeyStore loadKeyStore(String path, char[] password) throws IOException, GeneralSecurityException{ if (path == null || "".equals(path)) throw new IllegalArgumentException("Path is empty or null"); if (password == null) throw new IllegalArgumentException("Password is null"); //first try with JKS try{ return loadKeyStore(path, password, KEYSTORE_TYPE_JKS); } catch (IOException e){ //docs say that this is what is loaded if the file format is wrong //try loading PKCS instead return loadKeyStore(path, password, KEYSTORE_TYPE_PKCS12); } } /** * Tries to open a keystore of the given type * @param path the path to the keystore * @param password the keystore password * @param type the keystore type * @return a valid keystore * @throws IOException If there is any error loading the keystore * @throws GeneralSecurityException if the certificates cannot be loaded or the type specified is invalid */ protected static KeyStore loadKeyStore(String path, char[] password, String type) throws IOException, GeneralSecurityException{ FileInputStream fIn = null; try{ KeyStore ks = KeyStore.getInstance(type); fIn = new FileInputStream(path); ks.load(fIn, password); return ks; } finally{ if (fIn != null) fIn.close(); } } }
^ permalink raw reply [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-19 07:27  Craig Ringer <craig@postnewspapers.com.au>
  parent: Marc-André Laverdière <marc-andre@atc.tcs.com>
  0 siblings, 2 replies; 28+ messages in thread

From: Craig Ringer @ 2011-05-19 07:27 UTC (permalink / raw)
  To: Marc-André Laverdière <marc-andre@atc.tcs.com>; +Cc: pgsql-jdbc

Thanks for those changes. Comments follow.

I'm attaching a further-tweaked version of the code you just sent. I 
only had to change AbstractCertAuthFactory, so the other files remain 
unaltered. Changes are:

- Only attempt to autodetect keystore type when null keystore type was
   passed, instead of always accepting but ignoring the keystore type
   argument.

- Make string constants private so nobody lands up relying on them
   so they can't be changed without breaking backward compat.

- Make "internal helper only" methods private. If they're protected,
   they're API not internal helpers.

Reasonable?

See further comments inline below:

On 05/19/2011 02:41 PM, Marc-André Laverdière wrote:

 > There was a tiny bug in SysPropCertAuthFactory whereas a path was sent
 > instead of a password. I'm attaching the fix.

Whoops. Thanks.

> Thanks for the changes. It is a good step forward in terms of
> extensibility. That being said, I think we could just put a lot of the
> loading code in AbstractCertAuthFactory, so that it is more reusable.

I thought it was, as much as already possible. What more would you move 
into AbstractCertAuthFactory ? Or do you want to create a single helper 
method that takes something like:

initFactory(
   String ksPath, char[] ksPass, char[] keyPass,
   String tsPath, char[] tsPass)

to reduce verbosity, one that uses the existing buildSSLSocketFactory(), 
createKeyManagers() and createTrustManagers() behind the scenes?

To me, it seems a pretty small thing to have to call createKeyManagers 
and createTrustManagers and pass the args to buildSSLSocketFactory(...).

I did intentionally move the checks for null/empty keystore path and 
truststore path out of AbstractCertAuthFactory and into 
SysPropCertAuthFactory. It's already possible to say "use system 
default" by passing null to buildSSLSocketFactory(...), so I don't think 
createKeyManagers(..) or createTrustManagers() should be making that 
decision implicitly for the caller. In security, it might be quite 
undesirable to have the app fail silently back to the system truststore 
- you might have good reasons to have specified your own.

I can see that it's necessary to treat a missing key as "use default" 
when using system properties as a configuration mechanism, but don't 
think that should be an assumption that AbstractCertAuthFactory makes 
for all users of that code.

> I also added the code that allows to load both PKCS12 and JKS blindly. I
> think that we can remove the property for specifying the type. I  doubt
> we have to worry about other types of keystores than those two.

JECKS is also common.

I don't love this part - it's making assumptions that may not prove true 
in the long run, and IOException is a very broad exception to rely on to 
detect a very specific thing like wrong keystore format. It could 
potentially result in confusing error messages, too. For example, a 
corrupt or damaged JKS keystore might fail to load with an error 
reporting that a "pkcs12 keystore could not be read", causing the user 
to wonder why the S@#$@ the code was trying to read it as PKCS#12 and 
not identify the real problem, that it tried JKS first and failed 
because the keystore was damaged.

It seems OK to me to attempt to auto-detect the keystore type when no 
keystore type is given. The code you posted _ignores_ the keystore type 
argument. If a non-null keystore type is passed it should be honoured, 
but if a null keystore type is passed then I think it's fine to try to 
detect it rather than blindly assuming KeyStore.getDefaultType() .

--
Craig Ringer

Attachments:

AbstractCertAuthFactory.java (text/plain, 8.6K) download /*------------------------------------------------------------------------- * * Copyright (c) 2011, PostgreSQL Global Development Group * * *------------------------------------------------------------------------- */ package org.postgresql.ssl; import java.io.FileInputStream; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.security.SecureRandom; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; /** * Provide an implementation of SSLSocketFactory that allows client authentication. * * This code creates and wraps a SSLSocketFactory using the paths and passwords * obtained from calls to abstract methods that must be defined by a subclass. * You cannot use this class as-is. See SysPropCertAuthFactory for one you can * use unmodified. * * Subclasses <b>must</b> call buildSSLSocketFactory() at the end of their * constructors, and <b>must</b> call the superclass constructors at the beginning * of their constructors. Subclasses must also implement the abstract methods * so that appropriate configuration details are returned. * * @author Marc-André Laverdière (marc-andre@atc.tcs.com / marcandre.laverdiere@tcs.com) */ //// //// TODO: Delegate trust checks to system trustmanager if //// no match in our own trustmanager? //// //// TODO: way to override keymanager/trustmanager supply without //// haivng to override those abstracts? public abstract class AbstractCertAuthFactory extends WrappedFactory { protected final static String DEFAULT_SSL_PROTOCOL_NAME = "SSL"; private final static String KEYSTORE_TYPE_PKCS12 = "PKCS12"; private final static String KEYSTORE_TYPE_JKS = "JKS"; protected AbstractCertAuthFactory() { } protected AbstractCertAuthFactory(String ignored) { } /** * Builds an SSLContext with a trust store and key store constructed * using the parameters provided by the subclass's implementation of * AbstractCertAuthFactory's abstract methods. * * If a null value is passed for any argument, the system default will * be used. For example, a null keyManagers array will cause * the SSLSocketFactory to use an SSLContext with a system-default KeyStore. * On the Sun JRE, this is created using the JSSE javax.net.ssl system * properties. Similar rules apply for the TrustStore and SecureRandom instance. * * See http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html . * * @param keyManagers KeyManager(s) to obtain key material from, or null for JSSE default keystore * @param trustManagers TrustManager(s) to obtain trust material from, or null for JSSE default truststore * @param secureRandom Secure random generator. Unless you know you need to do otherwise, leave this null to use the default. * */ protected void buildSSLSocketFactory(KeyManager[] keyManagers, TrustManager[] trustManagers, SecureRandom secureRandom) throws IOException, GeneralSecurityException{ if (_factory != null) { throw new IllegalStateException("buildSSLSocketFactory() already called!"); } //Create + Initialize TLS context SSLContext context = SSLContext.getInstance(getSSLProtocolName()); context.init(keyManagers, trustManagers, secureRandom); _factory = context.getSocketFactory(); } /** * Create and return KeyManager(s) using the passed information. * * If keyStoreType is null, the default keystore type for the system default * JSSE provider will be used. If the keyPass is null, the keyStorePass will * also be used to decode the key. No other arguments may be null. * * @param keyStorePath Location of keystore on file system. Non-null. * @param keyStorePass Password/passphrase to decrypt keystore with. Non-null. * @param keyPass Password/passphrase to decrypt key(s) of interest within KeyStore with. If null, keyStorePass used. * @param keyStoreType JSSE keystore type. If null, auto-detection of store type is attempted. * @return KeyStore(s) created * @throws IOException if the KeyStore could not be read * @throws GeneralSecurityException (subtypes of) for most crypto errors, passphrase errors, etc. */ protected KeyManager[] createKeyManagers(String keyStorePath, char[] keyStorePass, char[] keyPass, String keyStoreType) throws IOException, GeneralSecurityException { //Load the Key Managers if (keyPass == null) { keyPass = keyStorePass; } KeyStore ks = loadKeyStore(keyStorePath, keyPass, keyStoreType); KeyManagerFactory managerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); managerFactory.init(ks, keyPass); return managerFactory.getKeyManagers(); } /** * Create and return TrustManager(s) using the passed information. * * If trustStoreType is null, the JSSE default keystore type is used. * No other arguments may be null. * * If you want to use the Java default truststore, do not call this method * at all. Just pass a null TrustManager[] as the trustManagers argument to * buildSSLSocketFactory(...). * * @param trustStorePath Location of trust store on file system. Non-null. * @param trustStorePass Password to decrypt truststore. * @param trustStoreType JSSE TrustStore type. If null, auto-detection of store type is attempted. * @return TrustStore(s) created * @throws IOException If the TrustStore cannot be read * @throws GeneralSecurityException (subtypes of) for most crypto errors, password errors, etc. */ protected TrustManager[] createTrustManagers(String trustStorePath, char[] trustStorePass, String trustStoreType) throws IOException, GeneralSecurityException { // Load the trust store KeyStore trustKs = loadKeyStore(trustStorePath, trustStorePass, trustStoreType); TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustFactory.init(trustKs); return trustFactory.getTrustManagers(); } /** * If you wish to override SSL protocol selection, you may do so by * overriding this method. The defualt simply returns * AbstractCertAuthFactory.DEFAULT_SSL_PROTOCOL_NAME . * . * @return JSSE ssl/tls protocol name AbstractCertAuthFactory.DEFAULT_SSL_PROTOCOL_NAME */ protected String getSSLProtocolName() { return DEFAULT_SSL_PROTOCOL_NAME; } // Load a keystore at the path specified, trying to detect the keystore // type. JKS will be tried first, and PKCS12 if that fails. private static KeyStore loadKeyStore(String path, char[] password) throws IOException, GeneralSecurityException{ //first try with JKS try{ return loadKeyStore(path, password, KEYSTORE_TYPE_JKS); } catch (IOException e){ //docs say that this is what is loaded if the file format is wrong //try loading PKCS instead return loadKeyStore(path, password, KEYSTORE_TYPE_PKCS12); } } // Load a keystore at the path specified, Attempting to auto-detect the type // if null is passed for type. private static KeyStore loadKeyStore(String path, char[] password, String type) throws IOException, GeneralSecurityException{ if (type == null) { return loadKeyStore(path, password); } else { return loadKeyStoreOfType(path, password, type); } } // // Helper used by loadKeyStore(...) to do the actual keystore loading work. // private static KeyStore loadKeyStoreOfType(String path, char[] password, String type) throws IOException, GeneralSecurityException{ // Load the keystore with the specified keystore type. if (path == null || "".equals(path)) throw new IllegalArgumentException("Path is empty or null"); if (password == null) throw new IllegalArgumentException("Password is null"); if (type == null) throw new IllegalArgumentException("KeyStore type must be non-null"); FileInputStream fIn = null; try{ KeyStore ks = KeyStore.getInstance(type); fIn = new FileInputStream(path); ks.load(fIn, password); return ks; } finally{ if (fIn != null) { fIn.close(); } } } }
^ permalink raw reply [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-19 08:11  Marc-André Laverdière <marc-andre@atc.tcs.com>
  parent: Craig Ringer <craig@postnewspapers.com.au>
  1 sibling, 1 reply; 28+ messages in thread

From: Marc-André Laverdière @ 2011-05-19 08:11 UTC (permalink / raw)
  To: Craig Ringer <craig@postnewspapers.com.au>; +Cc: pgsql-jdbc

That's good changes.
I'm not super keen on the idea of asking the user of providing the type.
But I'm not gonna fight over that :)

Now, would you please elaborate on those todos?

Also, methinks the @author tag at the top needs an extra line as follows:
@author Craig Ringer (craig@postnewspapers.com.au)

Marc-André Laverdière
Software Security Scientist
Innovation Labs, Tata Consultancy Services
Hyderabad, India

On Thursday 19 May 2011 12:57 PM, Craig Ringer wrote:
> Thanks for those changes. Comments follow.
> 
> I'm attaching a further-tweaked version of the code you just sent. I
> only had to change AbstractCertAuthFactory, so the other files remain
> unaltered. Changes are:
> 
> - Only attempt to autodetect keystore type when null keystore type was
>   passed, instead of always accepting but ignoring the keystore type
>   argument.
> 
> - Make string constants private so nobody lands up relying on them
>   so they can't be changed without breaking backward compat.
> 
> - Make "internal helper only" methods private. If they're protected,
>   they're API not internal helpers.
> 
> Reasonable?
> 
> See further comments inline below:
> 
> On 05/19/2011 02:41 PM, Marc-André Laverdière wrote:
> 
>> There was a tiny bug in SysPropCertAuthFactory whereas a path was sent
>> instead of a password. I'm attaching the fix.
> 
> Whoops. Thanks.
> 
>> Thanks for the changes. It is a good step forward in terms of
>> extensibility. That being said, I think we could just put a lot of the
>> loading code in AbstractCertAuthFactory, so that it is more reusable.
> 
> I thought it was, as much as already possible. What more would you move
> into AbstractCertAuthFactory ? Or do you want to create a single helper
> method that takes something like:
> 
> initFactory(
>   String ksPath, char[] ksPass, char[] keyPass,
>   String tsPath, char[] tsPass)
> 
> to reduce verbosity, one that uses the existing buildSSLSocketFactory(),
> createKeyManagers() and createTrustManagers() behind the scenes?
> 
> To me, it seems a pretty small thing to have to call createKeyManagers
> and createTrustManagers and pass the args to buildSSLSocketFactory(...).
> 
> I did intentionally move the checks for null/empty keystore path and
> truststore path out of AbstractCertAuthFactory and into
> SysPropCertAuthFactory. It's already possible to say "use system
> default" by passing null to buildSSLSocketFactory(...), so I don't think
> createKeyManagers(..) or createTrustManagers() should be making that
> decision implicitly for the caller. In security, it might be quite
> undesirable to have the app fail silently back to the system truststore
> - you might have good reasons to have specified your own.
> 
> I can see that it's necessary to treat a missing key as "use default"
> when using system properties as a configuration mechanism, but don't
> think that should be an assumption that AbstractCertAuthFactory makes
> for all users of that code.
> 
>> I also added the code that allows to load both PKCS12 and JKS blindly. I
>> think that we can remove the property for specifying the type. I  doubt
>> we have to worry about other types of keystores than those two.
> 
> JECKS is also common.
> 
> I don't love this part - it's making assumptions that may not prove true
> in the long run, and IOException is a very broad exception to rely on to
> detect a very specific thing like wrong keystore format. It could
> potentially result in confusing error messages, too. For example, a
> corrupt or damaged JKS keystore might fail to load with an error
> reporting that a "pkcs12 keystore could not be read", causing the user
> to wonder why the S@#$@ the code was trying to read it as PKCS#12 and
> not identify the real problem, that it tried JKS first and failed
> because the keystore was damaged.
> 
> It seems OK to me to attempt to auto-detect the keystore type when no
> keystore type is given. The code you posted _ignores_ the keystore type
> argument. If a non-null keystore type is passed it should be honoured,
> but if a null keystore type is passed then I think it's fine to try to
> detect it rather than blindly assuming KeyStore.getDefaultType() .
> 
> -- 
> Craig Ringer
> 
> 
> 
> 



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-19 08:28  Craig Ringer <craig@postnewspapers.com.au>
  parent: Marc-André Laverdière <marc-andre@atc.tcs.com>
  0 siblings, 0 replies; 28+ messages in thread

From: Craig Ringer @ 2011-05-19 08:28 UTC (permalink / raw)
  To: Marc-André Laverdière <marc-andre@atc.tcs.com>; +Cc: pgsql-jdbc

On 05/19/2011 04:11 PM, Marc-André Laverdière wrote:
> That's good changes.
> I'm not super keen on the idea of asking the user of providing the type.
> But I'm not gonna fight over that :)

So long as the option exists, I'm happy. I think it's a reasonable idea 
to try to auto-detect it by default.

> Now, would you please elaborate on those todos?

Whoops, I never meant to send the code to you with those in it.

The latter one no longer applies, it's resolved.

The first one isn't important for now. The main use case is if you want 
to add additional trusted certs without "hiding" the system trust 
database. It's a separate task and now that it's possible to pass your 
own TrustManager can be done by apps that need it without modifying 
AbstractCertAuthFactory at all.

So both may be removed. Thanks for pointing that out.

I'll see if I can put together an example X509TrustManager that tries to 
verify trust against an app-supplied KeyStore first and failing that 
against the system store. I have one around that I can adapt, but won't 
be able to do that immediately as I have to get on with other work.

--
Craig Ringer



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-24 04:56  Marc-André Laverdière <marc-andre@atc.tcs.com>
  parent: Marc-André Laverdière <marc-andre@atc.tcs.com>
  0 siblings, 1 reply; 28+ messages in thread

From: Marc-André Laverdière @ 2011-05-24 04:56 UTC (permalink / raw)
  To: Craig Ringer <craig@postnewspapers.com.au>; +Cc: Kris Jurka <books@ejurka.com>; pgsql-jdbc

Hi folks,

It is not over... It is not in the CVS repository yet :D

What would be the next step?
My guess is that we need to update the testware for it. That sounds like
a tricky proposition that would mess up a bunch of things here and
there... Any ideas on how to do it best?

Marc-André Laverdière
Software Security Scientist
Innovation Labs, Tata Consultancy Services
Hyderabad, India

On Thursday 19 May 2011 11:44 AM, Marc-André Laverdière wrote:
> Hi guys,
> 
> Here are some improvements based on the comments I saw so far.
> I'm also adding a small cheapo tester. Definitely not JUnit-ready :)
> 
> Marc-André Laverdière
> Software Security Scientist
> Innovation Labs, Tata Consultancy Services
> Hyderabad, India
> 
> On Thursday 19 May 2011 11:16 AM, Craig Ringer wrote:
>> On 05/18/2011 11:32 PM, Kris Jurka wrote:
>>>
>>>
>>> On Wed, 18 May 2011, Craig Ringer wrote:
>>>
>>>> Alas the SSLSocketFactory interface doesn't offer any really nice
>>>> options like a way to pass a Properties instance to the ctor,
>>>
>>> That'd be an easy thing to fix, just add another case to
>>> org.postgresql.ssl.MakeSSL#convert to pass through the provided
>>> Properties object. I've thought about that in the past, but with no real
>>> use case, I wasn't in a big rush to do so.
>>
>> OK, good to know.
>>
>> It wasn't a big issue for me in the end, as I landed up fetching my
>> configuration using the String argument form as a key to an
>> application-specific configuration provider. It's much easier to pass a
>> String argument through various layers one often has over the JDBC
>> driver, like an ORM or the like.
>>
>> -- 
>> Craig Ringer
>>
>>
>>



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-24 16:27  Kris Jurka <books@ejurka.com>
  parent: Marc-André Laverdière <marc-andre@atc.tcs.com>
  0 siblings, 1 reply; 28+ messages in thread

From: Kris Jurka @ 2011-05-24 16:27 UTC (permalink / raw)
  To: Marc-André Laverdière <marc-andre@atc.tcs.com>; +Cc: Craig Ringer <craig@postnewspapers.com.au>; pgsql-jdbc



On Tue, 24 May 2011, Marc-Andr? Laverdi?re wrote:

> It is not over... It is not in the CVS repository yet :D
>
> What would be the next step?

It was not clear to me that the discussion between you and Craig had 
resulted in a final code version.  Apparently you think so.  Craig do you 
concur?  If so I'll take a look at it and see about committing it.

> My guess is that we need to update the testware for it. That sounds like
> a tricky proposition that would mess up a bunch of things here and
> there... Any ideas on how to do it best?

I don't think there's any good way to setup automated tests for things 
like client certificates which need significant server configuration.  So 
I wouldn't bother with that.  Perhaps some documentation updates would be 
in order, but I haven't looked at the code yet to know what might be 
appropriate.

Kris Jurka



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-05-25 01:39  Craig Ringer <craig@postnewspapers.com.au>
  parent: Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 28+ messages in thread

From: Craig Ringer @ 2011-05-25 01:39 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: Marc-André Laverdière <marc-andre@atc.tcs.com>; pgsql-jdbc

On 25/05/11 00:27, Kris Jurka wrote:
> 
> 
> On Tue, 24 May 2011, Marc-Andr? Laverdi?re wrote:
> 
>> It is not over... It is not in the CVS repository yet :D
>>
>> What would be the next step?
> 
> It was not clear to me that the discussion between you and Craig had
> resulted in a final code version.  Apparently you think so.  Craig do
> you concur?

I'm happy with the state of the code, but should really test it properly
before signing off on that. In particular, I need to test PKCS#12 cert
files and test a JECKS keystore containing multiple keys only one of
which is valid to access Pg.

On the other hand, I'm swamped at the moment and unsure if I'll get to
that in a reasonable amount of time. The tests Marc-André wrote
demonstrate the core functionality pretty well, and the code would be
good to get into the official codebase to save others from duplicating
the same work over and over as both Marc-André and I have each done already.

Argh. I'm going to have to come back to that, as I have a backup server
to fix. Maybe it's best if you have a look and see what you think of it,
while I try to find some time to do some more testing.

> Perhaps some documentation updates
> would be in order, but I haven't looked at the code yet to know what
> might be appropriate.

Some documentation updates are definitely in order, to sit alongside the
existing documentation for the non-validating ssl factory.


By the way, I _do_ think it'd be useful to add support for constructing
the socket factory with:

  FactoryClass(String arg, Properties jdbcProperties)

... where the properties argument contains all the Pg JDBC properties
like the user name and password. It'd make it easier for apps to pass
custom args into a socket factory, especially things like the password
to the user's private key that they don't want to have to put in the
sslocketfactoryarg string.

I could also then produce a second version of the cert factory for
people to use that got all its settings from the jdbc connection
properties instead of the sytem properties.

I wouldn't suggest adding that now, though, but maybe as a revision once
the working code is already committed.

--
Craig Ringer



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-06-27 06:18  Marc-André Laverdière <marc-andre@atc.tcs.com>
  parent: Craig Ringer <craig@postnewspapers.com.au>
  0 siblings, 1 reply; 28+ messages in thread

From: Marc-André Laverdière @ 2011-06-27 06:18 UTC (permalink / raw)
  To: Craig Ringer <craig@postnewspapers.com.au>; +Cc: Kris Jurka <books@ejurka.com>; pgsql-jdbc

Hello everybody,

I haven't heard back about this testing... did anyone get time to do it?

Marc-André Laverdière
Software Security Scientist
Innovation Labs, Tata Consultancy Services
Hyderabad, India

On 05/25/2011 07:09 AM, Craig Ringer wrote:
> On 25/05/11 00:27, Kris Jurka wrote:
>>
>>
>> On Tue, 24 May 2011, Marc-Andr? Laverdi?re wrote:
>>
>>> It is not over... It is not in the CVS repository yet :D
>>>
>>> What would be the next step?
>>
>> It was not clear to me that the discussion between you and Craig had
>> resulted in a final code version.  Apparently you think so.  Craig do
>> you concur?
>
> I'm happy with the state of the code, but should really test it properly
> before signing off on that. In particular, I need to test PKCS#12 cert
> files and test a JECKS keystore containing multiple keys only one of
> which is valid to access Pg.
>
> On the other hand, I'm swamped at the moment and unsure if I'll get to
> that in a reasonable amount of time. The tests Marc-André wrote
> demonstrate the core functionality pretty well, and the code would be
> good to get into the official codebase to save others from duplicating
> the same work over and over as both Marc-André and I have each done already.
>
> Argh. I'm going to have to come back to that, as I have a backup server
> to fix. Maybe it's best if you have a look and see what you think of it,
> while I try to find some time to do some more testing.
>
>> Perhaps some documentation updates
>> would be in order, but I haven't looked at the code yet to know what
>> might be appropriate.
>
> Some documentation updates are definitely in order, to sit alongside the
> existing documentation for the non-validating ssl factory.
>
>
> By the way, I _do_ think it'd be useful to add support for constructing
> the socket factory with:
>
>    FactoryClass(String arg, Properties jdbcProperties)
>
> ... where the properties argument contains all the Pg JDBC properties
> like the user name and password. It'd make it easier for apps to pass
> custom args into a socket factory, especially things like the password
> to the user's private key that they don't want to have to put in the
> sslocketfactoryarg string.
>
> I could also then produce a second version of the cert factory for
> people to use that got all its settings from the jdbc connection
> properties instead of the sytem properties.
>
> I wouldn't suggest adding that now, though, but maybe as a revision once
> the working code is already committed.
>
> --
> Craig Ringer



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-06-27 08:03  Craig Ringer <craig@postnewspapers.com.au>
  parent: Marc-André Laverdière <marc-andre@atc.tcs.com>
  0 siblings, 0 replies; 28+ messages in thread

From: Craig Ringer @ 2011-06-27 08:03 UTC (permalink / raw)
  To: Marc-André Laverdière <marc-andre@atc.tcs.com>; +Cc: Kris Jurka <books@ejurka.com>; pgsql-jdbc

On 27/06/11 14:18, Marc-André Laverdière wrote:
> Hello everybody,
> 
> I haven't heard back about this testing... did anyone get time to do it?

Argh. I've been head-down in work and completely forgot about it.

--
Craig Ringer



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2011-11-14 14:23  Bruno Harbulot <bruno@distributedmatter.net>
  parent: Craig Ringer <craig@postnewspapers.com.au>
  1 sibling, 0 replies; 28+ messages in thread

From: Bruno Harbulot @ 2011-11-14 14:23 UTC (permalink / raw)
  To: pgsql-jdbc; +Cc: Marc-André Laverdière <marc-andre@atc.tcs.com>

Hi all,

Following the recent discussions about SSL support, here is a bit of 
feedback on this version.

To fix the "TODO: Delegate trust checks to system trustmanager if
no match in our own trustmanager?" is fairly easy: just pass null to 
SSLContext.init(...).

I would do this in createTrustManager:

         KeyStore trustKs = loadKeyStore(trustStorePath, trustStorePass, 
trustStoreType);
         if (trustKs != null) {
             TrustManagerFactory trustFactory = 
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
             trustFactory.init(trustKs);
             return trustFactory.getTrustManagers();
         } else {
             return null;
         }

and this in loadKeyStore (instead of throwing an exception):

         if (path == null || "".equals(path))
             return null;

The null default also works for the SecureRandom if anyone isn't 
specifying it.

(I would also do it do avoid potential NPEs perhaps in createKeyManager, 
although there is no actual default.)




Since using PKCS#11 was discussed a few days ago on the mailing list 
[1], I would also suggest a couple of changes, similar to what I had 
done for Apache Tomcat [2].

Essentially, not all keystores are file-based (e.g. PKCS#11, NSS, OSX 
KeychainStore, Windows store). The "NONE" file-path should be used for 
those: 
http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#Customizat...
In this case, use a null InputStream (not necessarily a FileInputStream).

For PKCS#11, it's also useful to specify the provider (if the PKCS#11 
configuration is in the JRE security configuration. This could be done 
as follows (with an extra provider name parameter, which would be null 
by default):

     private static KeyStore loadKeyStoreOfType(String path, char[] 
password, String type, String provider) throws IOException, 
GeneralSecurityException{
         // Load the keystore with the specified keystore type.
         if (path == null || "".equals(path))
             return null;
         if (password == null)
             throw new IllegalArgumentException("Password is null");
         if (type == null)
             type = KeyStore.getDefaultType();
         InputStream fIn = null;
         try{
             KeyStore ks;
             if (provider != null) {
                 ks = KeyStore.getInstance(type, provider);
             } else {
                 ks = KeyStore.getInstance(type);
             }
             // Using "NONE" for keystore that are not file-based.
             // 
http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#Customizat...
             if (!"NONE".equals(path)) {
                 fIn = new FileInputStream(path);
             }
             ks.load(fIn, password);
             return ks;
         } finally{
             if (fIn != null) {
                 fIn.close();
             }
         }
     }


On 19/05/2011 08:27, Craig Ringer wrote:
> Thanks for those changes. Comments follow.
>
> I'm attaching a further-tweaked version of the code you just sent. I
> only had to change AbstractCertAuthFactory, so the other files remain
> unaltered. Changes are:
>
> - Only attempt to autodetect keystore type when null keystore type was
> passed, instead of always accepting but ignoring the keystore type
> argument.
>
> - Make string constants private so nobody lands up relying on them
> so they can't be changed without breaking backward compat.
>
> - Make "internal helper only" methods private. If they're protected,
> they're API not internal helpers.
>
> Reasonable?
>
>> I also added the code that allows to load both PKCS12 and JKS blindly. I
>> think that we can remove the property for specifying the type. I doubt
>> we have to worry about other types of keystores than those two.
>
> JECKS is also common.
>
> I don't love this part - it's making assumptions that may not prove true
> in the long run, and IOException is a very broad exception to rely on to
> detect a very specific thing like wrong keystore format. It could
> potentially result in confusing error messages, too. For example, a
> corrupt or damaged JKS keystore might fail to load with an error
> reporting that a "pkcs12 keystore could not be read", causing the user
> to wonder why the S@#$@ the code was trying to read it as PKCS#12 and
> not identify the real problem, that it tried JKS first and failed
> because the keystore was damaged.
>
> It seems OK to me to attempt to auto-detect the keystore type when no
> keystore type is given. The code you posted _ignores_ the keystore type
> argument. If a non-null keystore type is passed it should be honoured,
> but if a null keystore type is passed then I think it's fine to try to
> detect it rather than blindly assuming KeyStore.getDefaultType() .


I would personally prefer not to try to have the driver guess the 
keystore type blindly. SSL errors can be obscure enough for users will 
little SSL experience as it is (like "Could not find matching cipher 
suites" sometimes when it's a problem with the key/cert).

Having the user specify the type or fall back on 
KeyStore.getDefaultType() seems more in line with the rest of the 
documentation in the JSSR ref. guide.



If this is of interest, I have done a bit of work on trying to make it 
easier to configure SSLContexts while keeping default values in 
jSSLutils [3].
I found that creating an SSLContextFactory interface/abstract class 
tends to make this sort of configuration easier, as you don't need to 
extend the SSLSocketFactory as much.
A similar approach could be used in the JDBC driver, but it would 
require the creation of such an interface and modifications to 
"MakeSSL". It might be too much.




Best wishes,

Bruno.

[1] http://archives.postgresql.org/pgsql-jdbc/2011-11/msg00015.php
[2] https://issues.apache.org/bugzilla/show_bug.cgi?id=43094
[3] http://www.jsslutils.org/




^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Support for cert auth in JDBC
@ 2013-01-15 16:53  Paula Price <paula.price@issinc.com>
  0 siblings, 1 reply; 28+ messages in thread

From: Paula Price @ 2013-01-15 16:53 UTC (permalink / raw)
  To: pgsql-jdbc

Hello,

I followed this thread to the end - Support for cert auth in JDBC.  I have spent two weeks trying to figure out why hibernate does not work with my postgresql ssl.

I have openssl working great and I have the java certs working with a simple java program.  When I throw hibernate into the mix everything goes wrong.

I am trying to get full authentication working. My certs are valid (proved with simple java code).

Is anyone able to help me with the final steps needed to put the CertAuthFactory in the jdbc driver?  I have not done java for a couple of years so I may be a little slow (I would also like to see some examples of using the CertAuthFactory).  I think I only need it to validate one trust store, so I do not need to pass in the trust store - although I have been known to be wrong before.

Any assistance is greatly appreciated.

Thanks,
Paula Price

^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2013-01-16 11:20  Dave Cramer <pg@fastcrypt.com>
  parent: Paula Price <paula.price@issinc.com>
  0 siblings, 1 reply; 28+ messages in thread

From: Dave Cramer @ 2013-01-16 11:20 UTC (permalink / raw)
  To: Paula Price <paula.price@issinc.com>; +Cc: pgsql-jdbc

Hi Paula,

Can you provide us with a bit more information ? Have you talked to
hibernate guys to see what the problem is? It would seem that SSL works
fine with pg and java, it is when you add hibernate to the mix that
everything goes wrong.

Dave

Dave Cramer

dave.cramer(at)credativ(dot)ca
http://www.credativ.ca


On Tue, Jan 15, 2013 at 11:53 AM, Paula Price <paula.price@issinc.com>wrote:

>  Hello,****
>
> ** **
>
> I followed this thread to the end - Support for cert auth in JDBC.  I have
> spent two weeks trying to figure out why hibernate does not work with my
> postgresql ssl.****
>
> ** **
>
> I have openssl working great and I have the java certs working with a
> simple java program.  When I throw hibernate into the mix everything goes
> wrong.****
>
> ** **
>
> I am trying to get full authentication working. My certs are valid (proved
> with simple java code).****
>
> ** **
>
> Is anyone able to help me with the final steps needed to put the
> CertAuthFactory in the jdbc driver?  I have not done java for a couple of
> years so I may be a little slow (I would also like to see some examples of
> using the CertAuthFactory).  I think I only need it to validate one trust
> store, so I do not need to pass in the trust store – although I have been
> known to be wrong before.****
>
> ** **
>
> Any assistance is greatly appreciated.****
>
> ** **
>
> Thanks,****
>
> Paula Price****
>
> ** **
>
> ** **
>

^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2013-01-17 17:05  Paula Price <paula.price@issinc.com>
  parent: Dave Cramer <pg@fastcrypt.com>
  0 siblings, 1 reply; 28+ messages in thread

From: Paula Price @ 2013-01-17 17:05 UTC (permalink / raw)
  To: Dave Cramer <pg@fastcrypt.com>; +Cc: pgsql-jdbc

Dave,

I have not spoken with Hibernate although I do think that the problem is most likely with hibernate (or hibernate in tomcat).  Since I can get ssl certification working with the jdbc driver then the problem has to be elsewhere.  I only wrote to this forum because I found that someone mentioned a similar problem Nov 2, 2011 and added a CertAuthFactory.

Here is more detail on the problem:

Although I downloaded the CertAuthFactory class ( from above mentioned thread), I have not tried adding it to the jdbc driver yet.  My simple java code - that works fine - contains a connection call and returns an error if it cannot connect (client is windows 7, postgres 9.1.6 is running on red hat linux 5).    Also, full authentication works with Java based application DbVisualizer9.0.



My cert Common Name is postgres.  The only way into the database is with a valid cert (unless you are local - I wanted to make sure I did not lock myself out of the database).  Pg_hba.conf contains:

# TYPE  DATABASE        USER            CIDR-ADDRESS            METHOD



# "local" is for Unix domain socket connections only

local   all             all                                     trust

# IPv4 local connections:

#host    all             all             0.0.0.0/0            md5

hostssl   all             all             123.123.123.0  255.255.0.0            cert

# IPv6 local connections:

#host    all             all             ::1/128                 trust



When I use my simple java code, I am able to connect just fine using this notation:

set JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.trustStore=C:/certs/truststore.jks

set JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.trustStorePassword=password

set JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.keyStore=C:/certs/keystore.jks

set JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.keyStorePassword=password



When I try to mix hibernate into the code, it acts as if it does not read in my client cert.  I see that trustStore is  read and I am able to see the  Common Name in the stacktrace (javax.net.debug = all).  When authentication reads in the client cert, it reads in total garbage and I have no clue what it thinks it is reading.





Below is the relevant part of the stack trace.

*****Note by Paula - I made a few simple changes to the stack trace to obscure some readable info - but nothing that should cause problems debugging.

*** CertificateRequest

Cert Types: RSA, DSS

Cert Authorities:

<CN=Development, OU=Development, O=Software, L=Colorado, ST=Colorado, C=US>

[read] MD5 and SHA1 hashes:  len = 158

0000: 0D 00 00 9A 02 01 02 00   95 00 93 30 81 90 31 0B  ...........0..1.

0010: 30 09 06 03 55 04 06 13   02 55 53 31 11 30 0F 06  0...U....US1.0..

0020: 03 55 04 08 0C 08 43 6F   6C 6F 72 61 64 6F 31 19  .U....Colorado1.

0030: 30 17 06 03 55 04 07 0C   10 43 6F 6C 6F 72 61 64  0...U....Colorad

0040: 6F 20 53 70 72 69 6E 67   73 31 27 30 25 06 03 55  o1'0%..U

0050: 04 0A 0C 1E 49 6E 74 65   6C 6C 69 67 65 6E 74 20  ....

0060: 53 6F 66 74 77 61 72 65   20 53 6F 6C 75 74 69 6F  Software

0070: 6E 73 31 14 30 12 06 03   55 04 0B 0C 0B 44 65 76  1.0...U....Dev

0080: 65 6C 6F 70 6D 65 6E 74   31 14 30 12 06 03 55 04  elopment1.0...U.

0090: 03 0C 0B 44 65 76 65 6C   6F 70 6D 65 6E 74        ...Development

*** ServerHelloDone

[read] MD5 and SHA1 hashes:  len = 4

0000: 0E 00 00 00                                        ....

*** Certificate chain

***

*** ClientKeyExchange, RSA PreMasterSecret, TLSv1

[write] MD5 and SHA1 hashes:  len = 269

0000: 0B 00 00 03 00 00 00 10   00 01 02 01 00 20 20 D5  .............  .

0010: AB 4E 12 10 CE 70 A9 C3   52 1E 4D A9 E7 1B BC ED  .N...p..R.M.....

0020: DD 3C 35 F6 B8 8F BF CB   BE 31 8C A8 E2 0F E9 79  .<5......1.....y

0030: 0A 0B 58 B7 F7 D4 F8 F8   BC 01 9E 5A C4 9C B2 AF  ..X........Z....

0040: 16 17 EB 2E 1A 75 DF 24   D3 22 35 0E 47 B8 09 09  .....u.$."5.G...

0050: 85 01 8E 7F 0B BE D4 BE   F1 A0 C3 4E EF F4 10 5C  ...........N...\

0060: 85 D6 A0 60 99 E3 2B 88   F4 06 EA 45 2C 83 34 56  ...`..+....E,.4V

0070: B1 36 90 BD 9B 7A 44 C8   CB 00 FF 27 3B 01 CD 19  .6...zD....';...

0080: 70 A5 A7 AF 7D 15 BF 5C   C2 FA 7E 19 53 86 52 F0  p......\....S.R.

0090: A9 CA BF 5E 17 4C AA 63   BA 7D 6E 28 F9 2E FB C4  ...^.L.c..n(....

00A0: 17 68 24 8A 9B 28 41 D8   8E F6 3B EA 8E 21 C1 25  .h$..(A...;..!.%

00B0: 10 DB BD C6 07 5F 61 BD   73 F7 09 73 7C 64 CC 38  ....._a.s..s.d.8

00C0: EB 17 E1 8A 48 80 E2 44   C2 38 34 9D AD C6 FC 9F  ....H..D.84.....

00D0: EA E6 06 96 34 4A B8 02   E4 B2 72 12 70 A1 00 04  ....4J....r.p...

00E0: DA C0 FE 99 2F E2 E7 A9   DD 27 54 2C 6E 92 12 8E  ..../....'T,n...

00F0: D8 BC 27 CB 34 3D F0 F2   39 A5 8D 4E D9 8F FE DF  ..'.4=..9..N....

0100: D0 2F 16 AE F4 30 DF 16   F7 5F 63 6C 1E           ./...0..._cl.

http-bio-8080-exec-2, WRITE: TLSv1 Handshake, length = 269

[Raw write]: length = 274

0000: 16 03 01 01 0D 0B 00 00   03 00 00 00 10 00 01 02  ................

0010: 01 00 20 20 D5 AB 4E 12   10 CE 70 A9 C3 52 1E 4D  ..  ..N...p..R.M

0020: A9 E7 1B BC ED DD 3C 35   F6 B8 8F BF CB BE 31 8C  ......<5......1.

0030: A8 E2 0F E9 79 0A 0B 58   B7 F7 D4 F8 F8 BC 01 9E  ....y..X........

0040: 5A C4 9C B2 AF 16 17 EB   2E 1A 75 DF 24 D3 22 35  Z.........u.$."5

0050: 0E 47 B8 09 09 85 01 8E   7F 0B BE D4 BE F1 A0 C3  .G..............

0060: 4E EF F4 10 5C 85 D6 A0   60 99 E3 2B 88 F4 06 EA  N...\...`..+....

0070: 45 2C 83 34 56 B1 36 90   BD 9B 7A 44 C8 CB 00 FF  E,.4V.6...zD....

0080: 27 3B 01 CD 19 70 A5 A7   AF 7D 15 BF 5C C2 FA 7E  ';...p......\...

0090: 19 53 86 52 F0 A9 CA BF   5E 17 4C AA 63 BA 7D 6E  .S.R....^.L.c..n

00A0: 28 F9 2E FB C4 17 68 24   8A 9B 28 41 D8 8E F6 3B  (.....h$..(A...;

00B0: EA 8E 21 C1 25 10 DB BD   C6 07 5F 61 BD 73 F7 09  ..!.%....._a.s..

00C0: 73 7C 64 CC 38 EB 17 E1   8A 48 80 E2 44 C2 38 34  s.d.8....H..D.84

00D0: 9D AD C6 FC 9F EA E6 06   96 34 4A B8 02 E4 B2 72  .........4J....r

00E0: 12 70 A1 00 04 DA C0 FE   99 2F E2 E7 A9 DD 27 54  .p......./....'T

00F0: 2C 6E 92 12 8E D8 BC 27   CB 34 3D F0 F2 39 A5 8D  ,n.....'.4=..9..

0100: 4E D9 8F FE DF D0 2F 16   AE F4 30 DF 16 F7 5F 63  N...../...0..._c

0110: 6C 1E                                              l.

SESSION KEYGEN:

PreMaster Secret:

0000: 03 01 47 EE 92 FF 8C 4C   4E FC 58 28 FB 11 0C 98  ..G....LN.X(....

0010: F2 F5 CA 42 46 02 6E 8D   09 AB C3 C5 BD C6 CB AA  ...BF.n.........

0020: 4E DB F5 62 FB 2A B8 66   E2 43 C6 B7 DB 50 07 E0  N..b.*.f.C...P..

CONNECTION KEYGEN:

Client Nonce:

0000: 50 F8 2B DE 26 56 50 F1   8E 81 CB F9 39 0A CE A1  P.+.&VP.....9...

0010: D7 6D 45 20 21 B2 E1 BA   12 DB FB 83 8B D0 37 85  .mE !.........7.

Server Nonce:

0000: 50 F8 2B DE C6 C5 A2 14   8B F0 12 1D 64 04 C1 91  P.+.........d...

0010: 8B 16 E6 88 A3 CF 45 82   98 F6 09 1A 06 61 58 10  ......E......aX.

Master Secret:

0000: 4F CE 52 E8 17 2E 62 CE   43 0A B5 92 CE BA 7F EC  O.R...b.C.......

0010: F7 8F 5B 12 89 5C C2 93   2C 5B 93 D8 F4 FF 8A 41  ..[..\..,[.....A

0020: 55 4E 9A 23 3F 55 4A BE   15 D5 09 54 D3 B4 52 AC  UN.#?UJ....T..R.

Client MAC write Secret:

0000: A2 03 04 80 08 E7 02 73   78 16 68 4B 37 DD 9C 2B  .......sx.hK7..+

0010: 4A 0D 79 25                                        J.y%

Server MAC write Secret:

0000: 9C 85 E5 FF 7C D4 23 9B   FA C8 A8 79 40 C6 E4 D1  ......#....y@...

0010: 77 8E 5D 90                                        w.].

Client write key:

0000: 84 21 98 68 3D B5 C6 C5   02 72 F5 25 DA FA 26 52  .!.h=....r.%..&R

Server write key:

0000: 6C 9F 46 C6 C7 28 D7 65   05 B6 88 8F CF 91 09 B5  l.F..(.e........

... no IV used for this cipher

http-bio-8080-exec-2, WRITE: TLSv1 Change Cipher Spec, length = 1

[Raw write]: length = 6

0000: 14 03 01 00 01 01                                  ......

*** Finished

verify_data:  { 6, 123, 192, 247, 189, 254, 84, 150, 77, 120, 177, 92 }

***

[write] MD5 and SHA1 hashes:  len = 16

0000: 14 00 00 0C 06 7B C0 F7   BD FE 54 96 4D 78 B1 5C  ..........T.Mx.\

Padded plaintext before ENCRYPTION:  len = 36

0000: 14 00 00 0C 06 7B C0 F7   BD FE 54 96 4D 78 B1 5C  ..........T.Mx.\

0010: 4F E1 08 3B F8 8A 9A 46   5B 85 39 0C 66 01 F2 A6  O..;...F[.9.f...

0020: E4 4C B9 99                                        .L..

http-bio-8080-exec-2, WRITE: TLSv1 Handshake, length = 36

[Raw write]: length = 41

0000: 16 03 01 00 24 1C A3 2E   D6 86 DE A9 5A DD 23 19  ....$.......Z.#.

0010: 2C D3 31 99 B6 D6 EF 88   8A 8C 91 E6 A7 72 A7 A8  ,.1..........r..

0020: DC F0 A7 05 69 49 37 8E   47                       ....iI7.G

[Raw read]: length = 5

0000: 14 03 01 00 01                                     .....

[Raw read]: length = 1

0000: 01                                                 .

http-bio-8080-exec-2, READ: TLSv1 Change Cipher Spec, length = 1

[Raw read]: length = 5

0000: 16 03 01 00 24                                     ....$

[Raw read]: length = 36

0000: 80 90 1E 1A 2A 5B 32 58   42 4B 67 7C 2B 2E D7 02  ....*[2XBKg.+...

0010: 0B 93 9D 5D 9E FE 2B 8E   A1 2F BB CA 7C 82 18 C7  ...]..+../......

0020: 78 84 81 0D                                        x...

http-bio-8080-exec-2, READ: TLSv1 Handshake, length = 36

Padded plaintext after DECRYPTION:  len = 36

0000: 14 00 00 0C 3E BF 40 C7   B6 62 E0 F5 38 B6 EC DD  ....>.@..b..8...

0010: 7E D7 D0 BE DC 5B 6B 0F   DD B3 CD DC 95 A6 7D 4B  .....[k........K

0020: 5D C4 B7 55                                        ]..U

*** Finished

verify_data:  { 62, 191, 64, 199, 182, 98, 224, 245, 56, 182, 236, 221 }

***

%% Cached client session: [Session-1, SSL_RSA_WITH_RC4_128_SHA]

[read] MD5 and SHA1 hashes:  len = 16



***Note by paula******Here is the URL call to hibernate *********************

0000: 14 00 00 0C 3E BF 40 C7   B6 62 E0 F5 38 B6 EC DD  ....>.@..b..8...

Padded plaintext before ENCRYPTION:  len = 119

0000: 00 00 00 63 00 03 00 00   75 73 65 72 00 70 6F 73  ...c....user.pos

0010: 74 67 72 65 73 00 64 61   74 61 62 61 73 65 00 72  tgres.database.r

0020: 75 6E 6E 65 72 73 00 63   6C 69 65 6E 74 5F 65 6E  unners.client_en

0030: 63 6F 64 69 6E 67 00 55   4E 49 43 4F 44 45 00 44  coding.UNICODE.D

0040: 61 74 65 53 74 79 6C 65   00 49 53 4F 00 65 78 74  ateStyle.ISO.ext

0050: 72 61 5F 66 6C 6F 61 74   5F 64 69 67 69 74 73 00  ra_float_digits.

0060: 32 00 00 10 FC 5E CF D9   20 3E 76 EB A5 0E 01 57  2....^.. >v....W

0070: 45 99 8A 55 A1 6C F6                               E..U.l.

http-bio-8080-exec-2, WRITE: TLSv1 Application Data, length = 119

[Raw write]: length = 124

0000: 17 03 01 00 77 E5 F7 04   85 3E D3 5B 5C 54 B5 A6  ....w....>.[\T..

0010: B1 B1 31 2B FB 09 BC 93   B4 93 7C 6E 35 FE 90 ED  ..1+.......n5...

0020: 4C A7 44 0F 4B 00 C5 5C   4C 31 E5 9A D3 21 E6 93  L.D.K..\L1...!..

0030: 24 06 02 F0 04 63 6B 96   D2 57 63 C5 DE C7 62 09  $....ck..Wc...b.

0040: 43 04 83 C7 80 FD 18 57   AA C0 DF 26 14 CD B7 F9  C......W...&....

0050: 5C 1F 28 2C CF 9F 54 2F   48 4B AC F4 0E 1B FA CA  \.(,..T/HK......

0060: 0C FE 0B F8 73 25 EA 4E   94 80 91 DE E6 90 1A 63  ....s%.N.......c

0070: 71 17 01 76 21 34 C8 D5   F3 A0 2C 88              q..v!4....,.

[Raw read]: length = 5

0000: 17 03 01 00 7B                                     .....

[Raw read]: length = 123

0000: 3A 60 92 1E AA 94 F1 28   39 95 91 1D 44 8E E9 8B  :`.....(9...D...

0010: 99 DD CA A9 21 F5 08 F9   C2 EB 35 88 51 D5 0D F1  ....!.....5.Q...

0020: DC 0F D8 5A E3 90 A2 C6   19 CA F3 2D 32 7D 78 8D  ...Z.......-2.x.

0030: 5B AB 5E F1 E9 58 31 60   FF 48 34 E9 C5 9A 88 B6  [.^..X1`.H4.....

0040: DD 75 44 B8 BB 18 29 29   56 5E FB F2 11 05 D7 3C  .uD...))V^.....<

0050: 60 FA 1A B1 A5 56 33 36   94 E5 BE 1F 8A F3 B7 CC  `....V36........

0060: 2A 5D CC B8 99 62 2B D0   BA F8 2B B2 5A 9F 99 F6  *]...b+...+.Z...

0070: AF 8C 7F DF 4E D5 F5 4B   8F 3B F3                 ....N..K.;.

http-bio-8080-exec-2, READ: TLSv1 Application Data, length = 123

Padded plaintext after DECRYPTION:  len = 123

0000: 45 00 00 00 66 53 46 41   54 41 4C 00 43 32 38 30  E...fSFATAL.C280

0010: 30 30 00 4D 63 6F 6E 6E   65 63 74 69 6F 6E 20 72  00.Mconnection r

0020: 65 71 75 69 72 65 73 20   61 20 76 61 6C 69 64 20  equires a valid

0030: 63 6C 69 65 6E 74 20 63   65 72 74 69 66 69 63 61  client certifica

0040: 74 65 00 46 61 75 74 68   2E 63 00 4C 33 35 36 00  te.Fauth.c.L356.

0050: 52 43 6C 69 65 6E 74 41   75 74 68 65 6E 74 69 63  RClientAuthentic

0060: 61 74 69 6F 6E 00 00 A3   E8 79 7F 76 28 24 67 05  ation....y.v($g.

0070: C3 07 19 CE 31 00 31 B0   4D FA F0                 ....1.1.M..

http-bio-8080-exec-2, called close()

http-bio-8080-exec-2, called closeInternal(true)

http-bio-8080-exec-2, SEND TLSv1 ALERT:  warning, description = close_notify

Paula Price
paula.price@issinc.com<mailto:paula.price@issinc.com>

From: davecramer@gmail.com [mailto:davecramer@gmail.com] On Behalf Of Dave Cramer
Sent: Wednesday, January 16, 2013 4:20 AM
To: Paula Price
Cc: pgsql-jdbc@postgresql.org
Subject: Re: [JDBC] Support for cert auth in JDBC

Hi Paula,

Can you provide us with a bit more information ? Have you talked to hibernate guys to see what the problem is? It would seem that SSL works fine with pg and java, it is when you add hibernate to the mix that everything goes wrong.

Dave

Dave Cramer

dave.cramer(at)credativ(dot)ca
http://www.credativ.ca

On Tue, Jan 15, 2013 at 11:53 AM, Paula Price <paula.price@issinc.com<mailto:paula.price@issinc.com>> wrote:
Hello,

I followed this thread to the end - Support for cert auth in JDBC.  I have spent two weeks trying to figure out why hibernate does not work with my postgresql ssl.

I have openssl working great and I have the java certs working with a simple java program.  When I throw hibernate into the mix everything goes wrong.

I am trying to get full authentication working. My certs are valid (proved with simple java code).

Is anyone able to help me with the final steps needed to put the CertAuthFactory in the jdbc driver?  I have not done java for a couple of years so I may be a little slow (I would also like to see some examples of using the CertAuthFactory).  I think I only need it to validate one trust store, so I do not need to pass in the trust store - although I have been known to be wrong before.

Any assistance is greatly appreciated.

Thanks,
Paula Price

^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2013-01-17 17:44  dmp <danap@ttc-cmc.net>
  parent: Paula Price <paula.price@issinc.com>
  0 siblings, 1 reply; 28+ messages in thread

From: dmp @ 2013-01-17 17:44 UTC (permalink / raw)
  To: Paula Price <paula.price@issinc.com>; pgsql-jdbc

Hello,

Perhaps someone in this forum may be able to help with implementing the
solution you desire, but perhaps you should speak more directly to the
individual who created the CerAuthFactory class or initiating the
report on Nov. 2, 2011.

I'm not sure how this forum is going to be of help to you with pgJDBC
when on your own acknowledgment the problem of connecting via SSL appears
to be with with the use of Hibernate.

danap.


Paula Price wrote:
> Dave,
>
> I have not spoken with Hibernate although I do think that the problem is
> most likely with hibernate (or hibernate in tomcat). Since I can get ssl
> certification working with the jdbc driver then the problem has to be
> elsewhere. I only wrote to this forum because I found that someone
> mentioned a similar problem Nov 2, 2011 and added a CertAuthFactory.
>
> Here is more detail on the problem:
>
> Although I downloaded the CertAuthFactory class ( from above mentioned
> thread), I have not tried adding it to the jdbc driver yet. My simple
> java code – that works fine - contains a connection call and returns an
> error if it cannot connect (client is windows 7, postgres 9.1.6 is
> running on red hat linux 5). Also, full authentication works with Java
> based application DbVisualizer9.0.
>
> My cert Common Name is postgres. The only way into the database is with
> a valid cert (unless you are local - I wanted to make sure I did not
> lock myself out of the database). Pg_hba.conf contains:
>
> # TYPE DATABASE USER CIDR-ADDRESS METHOD
>
> # "local" is for Unix domain socket connections only
>
> local all all trust
>
> # IPv4 local connections:
>
> #host all all 0.0.0.0/0 md5
>
> hostssl all all 123.123.123.0 255.255.0.0 cert
>
> # IPv6 local connections:
>
> #host all all ::1/128 trust
>
> When I use my simple java code, I am able to connect just fine using
> this notation:
>
> set JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.trustStore=C:/certs/truststore.jks
>
> set JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.trustStorePassword=password
>
> set JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.keyStore=C:/certs/keystore.jks
>
> set JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.keyStorePassword=password
>
> When I try to mix hibernate into the code, it acts as if it does not
> read in my client cert. I see that trustStore is read and I am able to
> see the Common Name in the stacktrace (javax.net.debug = all). When
> authentication reads in the client cert, it reads in total garbage and I
> have no clue what it thinks it is reading.
>
> Below is the relevant part of the stack trace.
>
> *****Note by Paula – I made a few simple changes to the stack trace to
> obscure some readable info – but nothing that should cause problems
> debugging.
>
> *** CertificateRequest
>
> Cert Types: RSA, DSS
>
> Cert Authorities:
>
> <CN=Development, OU=Development, O=Software, L=Colorado, ST=Colorado, C=US>
>
> [read] MD5 and SHA1 hashes: len = 158
>
> 0000: 0D 00 00 9A 02 01 02 00 95 00 93 30 81 90 31 0B ...........0..1.
>
> 0010: 30 09 06 03 55 04 06 13 02 55 53 31 11 30 0F 06 0...U....US1.0..
>
> 0020: 03 55 04 08 0C 08 43 6F 6C 6F 72 61 64 6F 31 19 .U....Colorado1.
>
> 0030: 30 17 06 03 55 04 07 0C 10 43 6F 6C 6F 72 61 64 0...U....Colorad
>
> 0040: 6F 20 53 70 72 69 6E 67 73 31 27 30 25 06 03 55 o1'0%..U
>
> 0050: 04 0A 0C 1E 49 6E 74 65 6C 6C 69 67 65 6E 74 20 ....
>
> 0060: 53 6F 66 74 77 61 72 65 20 53 6F 6C 75 74 69 6F Software
>
> 0070: 6E 73 31 14 30 12 06 03 55 04 0B 0C 0B 44 65 76 1.0...U....Dev
>
> 0080: 65 6C 6F 70 6D 65 6E 74 31 14 30 12 06 03 55 04 elopment1.0...U.
>
> 0090: 03 0C 0B 44 65 76 65 6C 6F 70 6D 65 6E 74 ...Development
>
> *** ServerHelloDone
>
> [read] MD5 and SHA1 hashes: len = 4
>
> 0000: 0E 00 00 00 ....
>
> *** Certificate chain
>
> ***
>
> *** ClientKeyExchange, RSA PreMasterSecret, TLSv1
>
> [write] MD5 and SHA1 hashes: len = 269
>
> 0000: 0B 00 00 03 00 00 00 10 00 01 02 01 00 20 20 D5 ............. .
>
> 0010: AB 4E 12 10 CE 70 A9 C3 52 1E 4D A9 E7 1B BC ED .N...p..R.M.....
>
> 0020: DD 3C 35 F6 B8 8F BF CB BE 31 8C A8 E2 0F E9 79 .<5......1.....y
>
> 0030: 0A 0B 58 B7 F7 D4 F8 F8 BC 01 9E 5A C4 9C B2 AF ..X........Z....
>
> 0040: 16 17 EB 2E 1A 75 DF 24 D3 22 35 0E 47 B8 09 09 .....u.$."5.G...
>
> 0050: 85 01 8E 7F 0B BE D4 BE F1 A0 C3 4E EF F4 10 5C ...........N...\
>
> 0060: 85 D6 A0 60 99 E3 2B 88 F4 06 EA 45 2C 83 34 56 ...`..+....E,.4V
>
> 0070: B1 36 90 BD 9B 7A 44 C8 CB 00 FF 27 3B 01 CD 19 .6...zD....';...
>
> 0080: 70 A5 A7 AF 7D 15 BF 5C C2 FA 7E 19 53 86 52 F0 p......\....S.R.
>
> 0090: A9 CA BF 5E 17 4C AA 63 BA 7D 6E 28 F9 2E FB C4 ...^.L.c..n(....
>
> 00A0: 17 68 24 8A 9B 28 41 D8 8E F6 3B EA 8E 21 C1 25 .h$..(A...;..!.%
>
> 00B0: 10 DB BD C6 07 5F 61 BD 73 F7 09 73 7C 64 CC 38 ....._a.s..s.d.8
>
> 00C0: EB 17 E1 8A 48 80 E2 44 C2 38 34 9D AD C6 FC 9F ....H..D.84.....
>
> 00D0: EA E6 06 96 34 4A B8 02 E4 B2 72 12 70 A1 00 04 ....4J....r.p...
>
> 00E0: DA C0 FE 99 2F E2 E7 A9 DD 27 54 2C 6E 92 12 8E ..../....'T,n...
>
> 00F0: D8 BC 27 CB 34 3D F0 F2 39 A5 8D 4E D9 8F FE DF ..'.4=..9..N....
>
> 0100: D0 2F 16 AE F4 30 DF 16 F7 5F 63 6C 1E ./...0..._cl.
>
> http-bio-8080-exec-2, WRITE: TLSv1 Handshake, length = 269
>
> [Raw write]: length = 274
>
> 0000: 16 03 01 01 0D 0B 00 00 03 00 00 00 10 00 01 02 ................
>
> 0010: 01 00 20 20 D5 AB 4E 12 10 CE 70 A9 C3 52 1E 4D .. ..N...p..R.M
>
> 0020: A9 E7 1B BC ED DD 3C 35 F6 B8 8F BF CB BE 31 8C ......<5......1.
>
> 0030: A8 E2 0F E9 79 0A 0B 58 B7 F7 D4 F8 F8 BC 01 9E ....y..X........
>
> 0040: 5A C4 9C B2 AF 16 17 EB 2E 1A 75 DF 24 D3 22 35 Z.........u.$."5
>
> 0050: 0E 47 B8 09 09 85 01 8E 7F 0B BE D4 BE F1 A0 C3 .G..............
>
> 0060: 4E EF F4 10 5C 85 D6 A0 60 99 E3 2B 88 F4 06 EA N...\...`..+....
>
> 0070: 45 2C 83 34 56 B1 36 90 BD 9B 7A 44 C8 CB 00 FF E,.4V.6...zD....
>
> 0080: 27 3B 01 CD 19 70 A5 A7 AF 7D 15 BF 5C C2 FA 7E ';...p......\...
>
> 0090: 19 53 86 52 F0 A9 CA BF 5E 17 4C AA 63 BA 7D 6E .S.R....^.L.c..n
>
> 00A0: 28 F9 2E FB C4 17 68 24 8A 9B 28 41 D8 8E F6 3B (.....h$..(A...;
>
> 00B0: EA 8E 21 C1 25 10 DB BD C6 07 5F 61 BD 73 F7 09 ..!.%....._a.s..
>
> 00C0: 73 7C 64 CC 38 EB 17 E1 8A 48 80 E2 44 C2 38 34 s.d.8....H..D.84
>
> 00D0: 9D AD C6 FC 9F EA E6 06 96 34 4A B8 02 E4 B2 72 .........4J....r
>
> 00E0: 12 70 A1 00 04 DA C0 FE 99 2F E2 E7 A9 DD 27 54 .p......./....'T
>
> 00F0: 2C 6E 92 12 8E D8 BC 27 CB 34 3D F0 F2 39 A5 8D ,n.....'.4=..9..
>
> 0100: 4E D9 8F FE DF D0 2F 16 AE F4 30 DF 16 F7 5F 63 N...../...0..._c
>
> 0110: 6C 1E l.
>
> SESSION KEYGEN:
>
> PreMaster Secret:
>
> 0000: 03 01 47 EE 92 FF 8C 4C 4E FC 58 28 FB 11 0C 98 ..G....LN.X(....
>
> 0010: F2 F5 CA 42 46 02 6E 8D 09 AB C3 C5 BD C6 CB AA ...BF.n.........
>
> 0020: 4E DB F5 62 FB 2A B8 66 E2 43 C6 B7 DB 50 07 E0 N..b.*.f.C...P..
>
> CONNECTION KEYGEN:
>
> Client Nonce:
>
> 0000: 50 F8 2B DE 26 56 50 F1 8E 81 CB F9 39 0A CE A1 P.+.&VP.....9...
>
> 0010: D7 6D 45 20 21 B2 E1 BA 12 DB FB 83 8B D0 37 85 .mE !.........7.
>
> Server Nonce:
>
> 0000: 50 F8 2B DE C6 C5 A2 14 8B F0 12 1D 64 04 C1 91 P.+.........d...
>
> 0010: 8B 16 E6 88 A3 CF 45 82 98 F6 09 1A 06 61 58 10 ......E......aX.
>
> Master Secret:
>
> 0000: 4F CE 52 E8 17 2E 62 CE 43 0A B5 92 CE BA 7F EC O.R...b.C.......
>
> 0010: F7 8F 5B 12 89 5C C2 93 2C 5B 93 D8 F4 FF 8A 41 ..[..\..,[.....A
>
> 0020: 55 4E 9A 23 3F 55 4A BE 15 D5 09 54 D3 B4 52 AC UN.#?UJ....T..R.
>
> Client MAC write Secret:
>
> 0000: A2 03 04 80 08 E7 02 73 78 16 68 4B 37 DD 9C 2B .......sx.hK7..+
>
> 0010: 4A 0D 79 25 J.y%
>
> Server MAC write Secret:
>
> 0000: 9C 85 E5 FF 7C D4 23 9B FA C8 A8 79 40 C6 E4 D1 ......#....y@...
>
> 0010: 77 8E 5D 90 w.].
>
> Client write key:
>
> 0000: 84 21 98 68 3D B5 C6 C5 02 72 F5 25 DA FA 26 52 .!.h=....r.%..&R
>
> Server write key:
>
> 0000: 6C 9F 46 C6 C7 28 D7 65 05 B6 88 8F CF 91 09 B5 l.F..(.e........
>
> ... no IV used for this cipher
>
> http-bio-8080-exec-2, WRITE: TLSv1 Change Cipher Spec, length = 1
>
> [Raw write]: length = 6
>
> 0000: 14 03 01 00 01 01 ......
>
> *** Finished
>
> verify_data: { 6, 123, 192, 247, 189, 254, 84, 150, 77, 120, 177, 92 }
>
> ***
>
> [write] MD5 and SHA1 hashes: len = 16
>
> 0000: 14 00 00 0C 06 7B C0 F7 BD FE 54 96 4D 78 B1 5C ..........T.Mx.\
>
> Padded plaintext before ENCRYPTION: len = 36
>
> 0000: 14 00 00 0C 06 7B C0 F7 BD FE 54 96 4D 78 B1 5C ..........T.Mx.\
>
> 0010: 4F E1 08 3B F8 8A 9A 46 5B 85 39 0C 66 01 F2 A6 O..;...F[.9.f...
>
> 0020: E4 4C B9 99 .L..
>
> http-bio-8080-exec-2, WRITE: TLSv1 Handshake, length = 36
>
> [Raw write]: length = 41
>
> 0000: 16 03 01 00 24 1C A3 2E D6 86 DE A9 5A DD 23 19 ....$.......Z.#.
>
> 0010: 2C D3 31 99 B6 D6 EF 88 8A 8C 91 E6 A7 72 A7 A8 ,.1..........r..
>
> 0020: DC F0 A7 05 69 49 37 8E 47 ....iI7.G
>
> [Raw read]: length = 5
>
> 0000: 14 03 01 00 01 .....
>
> [Raw read]: length = 1
>
> 0000: 01 .
>
> http-bio-8080-exec-2, READ: TLSv1 Change Cipher Spec, length = 1
>
> [Raw read]: length = 5
>
> 0000: 16 03 01 00 24 ....$
>
> [Raw read]: length = 36
>
> 0000: 80 90 1E 1A 2A 5B 32 58 42 4B 67 7C 2B 2E D7 02 ....*[2XBKg.+...
>
> 0010: 0B 93 9D 5D 9E FE 2B 8E A1 2F BB CA 7C 82 18 C7 ...]..+../......
>
> 0020: 78 84 81 0D x...
>
> http-bio-8080-exec-2, READ: TLSv1 Handshake, length = 36
>
> Padded plaintext after DECRYPTION: len = 36
>
> 0000: 14 00 00 0C 3E BF 40 C7 B6 62 E0 F5 38 B6 EC DD ....>.@..b..8...
>
> 0010: 7E D7 D0 BE DC 5B 6B 0F DD B3 CD DC 95 A6 7D 4B .....[k........K
>
> 0020: 5D C4 B7 55 ]..U
>
> *** Finished
>
> verify_data: { 62, 191, 64, 199, 182, 98, 224, 245, 56, 182, 236, 221 }
>
> ***
>
> %% Cached client session: [Session-1, SSL_RSA_WITH_RC4_128_SHA]
>
> [read] MD5 and SHA1 hashes: len = 16
>
> ***Note by paula******Here is the URL call to hibernate
> *********************
>
> 0000: 14 00 00 0C 3E BF 40 C7 B6 62 E0 F5 38 B6 EC DD ....>.@..b..8...
>
> Padded plaintext before ENCRYPTION: len = 119
>
> 0000: 00 00 00 63 00 03 00 00 75 73 65 72 00 70 6F 73 ...c....user.pos
>
> 0010: 74 67 72 65 73 00 64 61 74 61 62 61 73 65 00 72 tgres.database.r
>
> 0020: 75 6E 6E 65 72 73 00 63 6C 69 65 6E 74 5F 65 6E unners.client_en
>
> 0030: 63 6F 64 69 6E 67 00 55 4E 49 43 4F 44 45 00 44 coding.UNICODE.D
>
> 0040: 61 74 65 53 74 79 6C 65 00 49 53 4F 00 65 78 74 ateStyle.ISO.ext
>
> 0050: 72 61 5F 66 6C 6F 61 74 5F 64 69 67 69 74 73 00 ra_float_digits.
>
> 0060: 32 00 00 10 FC 5E CF D9 20 3E 76 EB A5 0E 01 57 2....^.. >v....W
>
> 0070: 45 99 8A 55 A1 6C F6 E..U.l.
>
> http-bio-8080-exec-2, WRITE: TLSv1 Application Data, length = 119
>
> [Raw write]: length = 124
>
> 0000: 17 03 01 00 77 E5 F7 04 85 3E D3 5B 5C 54 B5 A6 ....w....>.[\T..
>
> 0010: B1 B1 31 2B FB 09 BC 93 B4 93 7C 6E 35 FE 90 ED ..1+.......n5...
>
> 0020: 4C A7 44 0F 4B 00 C5 5C 4C 31 E5 9A D3 21 E6 93 L.D.K..\L1...!..
>
> 0030: 24 06 02 F0 04 63 6B 96 D2 57 63 C5 DE C7 62 09 $....ck..Wc...b.
>
> 0040: 43 04 83 C7 80 FD 18 57 AA C0 DF 26 14 CD B7 F9 C......W...&....
>
> 0050: 5C 1F 28 2C CF 9F 54 2F 48 4B AC F4 0E 1B FA CA \.(,..T/HK......
>
> 0060: 0C FE 0B F8 73 25 EA 4E 94 80 91 DE E6 90 1A 63 ....s%.N.......c
>
> 0070: 71 17 01 76 21 34 C8 D5 F3 A0 2C 88 q..v!4....,.
>
> [Raw read]: length = 5
>
> 0000: 17 03 01 00 7B .....
>
> [Raw read]: length = 123
>
> 0000: 3A 60 92 1E AA 94 F1 28 39 95 91 1D 44 8E E9 8B :`.....(9...D...
>
> 0010: 99 DD CA A9 21 F5 08 F9 C2 EB 35 88 51 D5 0D F1 ....!.....5.Q...
>
> 0020: DC 0F D8 5A E3 90 A2 C6 19 CA F3 2D 32 7D 78 8D ...Z.......-2.x.
>
> 0030: 5B AB 5E F1 E9 58 31 60 FF 48 34 E9 C5 9A 88 B6 [.^..X1`.H4.....
>
> 0040: DD 75 44 B8 BB 18 29 29 56 5E FB F2 11 05 D7 3C .uD...))V^.....<
>
> 0050: 60 FA 1A B1 A5 56 33 36 94 E5 BE 1F 8A F3 B7 CC `....V36........
>
> 0060: 2A 5D CC B8 99 62 2B D0 BA F8 2B B2 5A 9F 99 F6 *]...b+...+.Z...
>
> 0070: AF 8C 7F DF 4E D5 F5 4B 8F 3B F3 ....N..K.;.
>
> http-bio-8080-exec-2, READ: TLSv1 Application Data, length = 123
>
> Padded plaintext after DECRYPTION: len = 123
>
> 0000: 45 00 00 00 66 53 46 41 54 41 4C 00 43 32 38 30 E...fSFATAL.C280
>
> 0010: 30 30 00 4D 63 6F 6E 6E 65 63 74 69 6F 6E 20 72 00.Mconnection r
>
> 0020: 65 71 75 69 72 65 73 20 61 20 76 61 6C 69 64 20 equires a valid
>
> 0030: 63 6C 69 65 6E 74 20 63 65 72 74 69 66 69 63 61 client certifica
>
> 0040: 74 65 00 46 61 75 74 68 2E 63 00 4C 33 35 36 00 te.Fauth.c.L356.
>
> 0050: 52 43 6C 69 65 6E 74 41 75 74 68 65 6E 74 69 63 RClientAuthentic
>
> 0060: 61 74 69 6F 6E 00 00 A3 E8 79 7F 76 28 24 67 05 ation....y.v($g.
>
> 0070: C3 07 19 CE 31 00 31 B0 4D FA F0 ....1.1.M..
>
> http-bio-8080-exec-2, called close()
>
> http-bio-8080-exec-2, called closeInternal(true)
>
> http-bio-8080-exec-2, SEND TLSv1 ALERT: warning, description = close_notify
>
> Paula Price
>
> paula.price@issinc.com <mailto:paula.price@issinc.com>
>
> *From:* davecramer@gmail.com [mailto:davecramer@gmail.com] *On Behalf Of
> *Dave Cramer
> *Sent:* Wednesday, January 16, 2013 4:20 AM
> *To:* Paula Price
> *Cc:* pgsql-jdbc@postgresql.org
> *Subject:* Re: [JDBC] Support for cert auth in JDBC
>
> Hi Paula,
>
> Can you provide us with a bit more information ? Have you talked to
> hibernate guys to see what the problem is? It would seem that SSL works
> fine with pg and java, it is when you add hibernate to the mix that
> everything goes wrong.
>
> Dave
>
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
> On Tue, Jan 15, 2013 at 11:53 AM, Paula Price <paula.price@issinc.com
> <mailto:paula.price@issinc.com>> wrote:
>
> Hello,
>
> I followed this thread to the end - Support for cert auth in JDBC. I
> have spent two weeks trying to figure out why hibernate does not work
> with my postgresql ssl.
>
> I have openssl working great and I have the java certs working with a
> simple java program. When I throw hibernate into the mix everything goes
> wrong.
>
> I am trying to get full authentication working. My certs are valid
> (proved with simple java code).
>
> Is anyone able to help me with the final steps needed to put the
> CertAuthFactory in the jdbc driver? I have not done java for a couple of
> years so I may be a little slow (I would also like to see some examples
> of using the CertAuthFactory). I think I only need it to validate one
> trust store, so I do not need to pass in the trust store – although I
> have been known to be wrong before.
>
> Any assistance is greatly appreciated.
>
> Thanks,
>
> Paula Price
>



-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2013-01-17 18:06  Paula Price <paula.price@issinc.com>
  parent: dmp <danap@ttc-cmc.net>
  0 siblings, 2 replies; 28+ messages in thread

From: Paula Price @ 2013-01-17 18:06 UTC (permalink / raw)
  To: dmp <danap@ttc-cmc.net>; pgsql-jdbc

Again, I would not have posted this to this forum except for the fact that I found the initial thread and the last message on the thread said that the CertAuthFactory  was going to be added to the jdbc code.  So, I thought I would give it a try and see if it fixed my problem.  I did not mean to bother anyone, I just wanted to know why the CertAuthFactory code never made it into the jdbc jar file and a small example of how to use it.  Please forgive me for any aggravation I have caused, I had run into a wall and was not making progress and I know postgres a lot better than I know hibernate. 

Thank you for your time,
Paula Price
paula.price@issinc.com

-----Original Message-----
From: dmp [mailto:danap@ttc-cmc.net] 
Sent: Thursday, January 17, 2013 10:45 AM
To: Paula Price; PostgreSQL JDBC
Subject: Re: [JDBC] Support for cert auth in JDBC

Hello,

Perhaps someone in this forum may be able to help with implementing the solution you desire, but perhaps you should speak more directly to the individual who created the CerAuthFactory class or initiating the report on Nov. 2, 2011.

I'm not sure how this forum is going to be of help to you with pgJDBC when on your own acknowledgment the problem of connecting via SSL appears to be with with the use of Hibernate.

danap.


Paula Price wrote:
> Dave,
>
> I have not spoken with Hibernate although I do think that the problem 
> is most likely with hibernate (or hibernate in tomcat). Since I can 
> get ssl certification working with the jdbc driver then the problem 
> has to be elsewhere. I only wrote to this forum because I found that 
> someone mentioned a similar problem Nov 2, 2011 and added a CertAuthFactory.
>
> Here is more detail on the problem:
>
> Although I downloaded the CertAuthFactory class ( from above mentioned 
> thread), I have not tried adding it to the jdbc driver yet. My simple 
> java code - that works fine - contains a connection call and returns 
> an error if it cannot connect (client is windows 7, postgres 9.1.6 is 
> running on red hat linux 5). Also, full authentication works with Java 
> based application DbVisualizer9.0.
>
> My cert Common Name is postgres. The only way into the database is 
> with a valid cert (unless you are local - I wanted to make sure I did 
> not lock myself out of the database). Pg_hba.conf contains:
>
> # TYPE DATABASE USER CIDR-ADDRESS METHOD
>
> # "local" is for Unix domain socket connections only
>
> local all all trust
>
> # IPv4 local connections:
>
> #host all all 0.0.0.0/0 md5
>
> hostssl all all 123.123.123.0 255.255.0.0 cert
>
> # IPv6 local connections:
>
> #host all all ::1/128 trust
>
> When I use my simple java code, I am able to connect just fine using 
> this notation:
>
> set JAVA_OPTS=%JAVA_OPTS% 
> -Djavax.net.ssl.trustStore=C:/certs/truststore.jks
>
> set JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.trustStorePassword=password
>
> set JAVA_OPTS=%JAVA_OPTS% 
> -Djavax.net.ssl.keyStore=C:/certs/keystore.jks
>
> set JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.keyStorePassword=password
>
> When I try to mix hibernate into the code, it acts as if it does not 
> read in my client cert. I see that trustStore is read and I am able to 
> see the Common Name in the stacktrace (javax.net.debug = all). When 
> authentication reads in the client cert, it reads in total garbage and 
> I have no clue what it thinks it is reading.
>
> Below is the relevant part of the stack trace.
>
> *****Note by Paula - I made a few simple changes to the stack trace to 
> obscure some readable info - but nothing that should cause problems 
> debugging.
>
> *** CertificateRequest
>
> Cert Types: RSA, DSS
>
> Cert Authorities:
>
> <CN=Development, OU=Development, O=Software, L=Colorado, ST=Colorado, 
> C=US>
>
> [read] MD5 and SHA1 hashes: len = 158
>
> 0000: 0D 00 00 9A 02 01 02 00 95 00 93 30 81 90 31 0B ...........0..1.
>
> 0010: 30 09 06 03 55 04 06 13 02 55 53 31 11 30 0F 06 0...U....US1.0..
>
> 0020: 03 55 04 08 0C 08 43 6F 6C 6F 72 61 64 6F 31 19 .U....Colorado1.
>
> 0030: 30 17 06 03 55 04 07 0C 10 43 6F 6C 6F 72 61 64 0...U....Colorad
>
> 0040: 6F 20 53 70 72 69 6E 67 73 31 27 30 25 06 03 55 o1'0%..U
>
> 0050: 04 0A 0C 1E 49 6E 74 65 6C 6C 69 67 65 6E 74 20 ....
>
> 0060: 53 6F 66 74 77 61 72 65 20 53 6F 6C 75 74 69 6F Software
>
> 0070: 6E 73 31 14 30 12 06 03 55 04 0B 0C 0B 44 65 76 1.0...U....Dev
>
> 0080: 65 6C 6F 70 6D 65 6E 74 31 14 30 12 06 03 55 04 elopment1.0...U.
>
> 0090: 03 0C 0B 44 65 76 65 6C 6F 70 6D 65 6E 74 ...Development
>
> *** ServerHelloDone
>
> [read] MD5 and SHA1 hashes: len = 4
>
> 0000: 0E 00 00 00 ....
>
> *** Certificate chain
>
> ***
>
> *** ClientKeyExchange, RSA PreMasterSecret, TLSv1
>
> [write] MD5 and SHA1 hashes: len = 269
>
> 0000: 0B 00 00 03 00 00 00 10 00 01 02 01 00 20 20 D5 ............. .
>
> 0010: AB 4E 12 10 CE 70 A9 C3 52 1E 4D A9 E7 1B BC ED .N...p..R.M.....
>
> 0020: DD 3C 35 F6 B8 8F BF CB BE 31 8C A8 E2 0F E9 79 .<5......1.....y
>
> 0030: 0A 0B 58 B7 F7 D4 F8 F8 BC 01 9E 5A C4 9C B2 AF ..X........Z....
>
> 0040: 16 17 EB 2E 1A 75 DF 24 D3 22 35 0E 47 B8 09 09 .....u.$."5.G...
>
> 0050: 85 01 8E 7F 0B BE D4 BE F1 A0 C3 4E EF F4 10 5C ...........N...\
>
> 0060: 85 D6 A0 60 99 E3 2B 88 F4 06 EA 45 2C 83 34 56 ...`..+....E,.4V
>
> 0070: B1 36 90 BD 9B 7A 44 C8 CB 00 FF 27 3B 01 CD 19 .6...zD....';...
>
> 0080: 70 A5 A7 AF 7D 15 BF 5C C2 FA 7E 19 53 86 52 F0 p......\....S.R.
>
> 0090: A9 CA BF 5E 17 4C AA 63 BA 7D 6E 28 F9 2E FB C4 ...^.L.c..n(....
>
> 00A0: 17 68 24 8A 9B 28 41 D8 8E F6 3B EA 8E 21 C1 25 .h$..(A...;..!.%
>
> 00B0: 10 DB BD C6 07 5F 61 BD 73 F7 09 73 7C 64 CC 38 ....._a.s..s.d.8
>
> 00C0: EB 17 E1 8A 48 80 E2 44 C2 38 34 9D AD C6 FC 9F ....H..D.84.....
>
> 00D0: EA E6 06 96 34 4A B8 02 E4 B2 72 12 70 A1 00 04 ....4J....r.p...
>
> 00E0: DA C0 FE 99 2F E2 E7 A9 DD 27 54 2C 6E 92 12 8E ..../....'T,n...
>
> 00F0: D8 BC 27 CB 34 3D F0 F2 39 A5 8D 4E D9 8F FE DF ..'.4=..9..N....
>
> 0100: D0 2F 16 AE F4 30 DF 16 F7 5F 63 6C 1E ./...0..._cl.
>
> http-bio-8080-exec-2, WRITE: TLSv1 Handshake, length = 269
>
> [Raw write]: length = 274
>
> 0000: 16 03 01 01 0D 0B 00 00 03 00 00 00 10 00 01 02 ................
>
> 0010: 01 00 20 20 D5 AB 4E 12 10 CE 70 A9 C3 52 1E 4D .. ..N...p..R.M
>
> 0020: A9 E7 1B BC ED DD 3C 35 F6 B8 8F BF CB BE 31 8C ......<5......1.
>
> 0030: A8 E2 0F E9 79 0A 0B 58 B7 F7 D4 F8 F8 BC 01 9E ....y..X........
>
> 0040: 5A C4 9C B2 AF 16 17 EB 2E 1A 75 DF 24 D3 22 35 Z.........u.$."5
>
> 0050: 0E 47 B8 09 09 85 01 8E 7F 0B BE D4 BE F1 A0 C3 .G..............
>
> 0060: 4E EF F4 10 5C 85 D6 A0 60 99 E3 2B 88 F4 06 EA N...\...`..+....
>
> 0070: 45 2C 83 34 56 B1 36 90 BD 9B 7A 44 C8 CB 00 FF E,.4V.6...zD....
>
> 0080: 27 3B 01 CD 19 70 A5 A7 AF 7D 15 BF 5C C2 FA 7E ';...p......\...
>
> 0090: 19 53 86 52 F0 A9 CA BF 5E 17 4C AA 63 BA 7D 6E .S.R....^.L.c..n
>
> 00A0: 28 F9 2E FB C4 17 68 24 8A 9B 28 41 D8 8E F6 3B (.....h$..(A...;
>
> 00B0: EA 8E 21 C1 25 10 DB BD C6 07 5F 61 BD 73 F7 09 ..!.%....._a.s..
>
> 00C0: 73 7C 64 CC 38 EB 17 E1 8A 48 80 E2 44 C2 38 34 s.d.8....H..D.84
>
> 00D0: 9D AD C6 FC 9F EA E6 06 96 34 4A B8 02 E4 B2 72 .........4J....r
>
> 00E0: 12 70 A1 00 04 DA C0 FE 99 2F E2 E7 A9 DD 27 54 .p......./....'T
>
> 00F0: 2C 6E 92 12 8E D8 BC 27 CB 34 3D F0 F2 39 A5 8D ,n.....'.4=..9..
>
> 0100: 4E D9 8F FE DF D0 2F 16 AE F4 30 DF 16 F7 5F 63 N...../...0..._c
>
> 0110: 6C 1E l.
>
> SESSION KEYGEN:
>
> PreMaster Secret:
>
> 0000: 03 01 47 EE 92 FF 8C 4C 4E FC 58 28 FB 11 0C 98 ..G....LN.X(....
>
> 0010: F2 F5 CA 42 46 02 6E 8D 09 AB C3 C5 BD C6 CB AA ...BF.n.........
>
> 0020: 4E DB F5 62 FB 2A B8 66 E2 43 C6 B7 DB 50 07 E0 N..b.*.f.C...P..
>
> CONNECTION KEYGEN:
>
> Client Nonce:
>
> 0000: 50 F8 2B DE 26 56 50 F1 8E 81 CB F9 39 0A CE A1 P.+.&VP.....9...
>
> 0010: D7 6D 45 20 21 B2 E1 BA 12 DB FB 83 8B D0 37 85 .mE !.........7.
>
> Server Nonce:
>
> 0000: 50 F8 2B DE C6 C5 A2 14 8B F0 12 1D 64 04 C1 91 P.+.........d...
>
> 0010: 8B 16 E6 88 A3 CF 45 82 98 F6 09 1A 06 61 58 10 ......E......aX.
>
> Master Secret:
>
> 0000: 4F CE 52 E8 17 2E 62 CE 43 0A B5 92 CE BA 7F EC O.R...b.C.......
>
> 0010: F7 8F 5B 12 89 5C C2 93 2C 5B 93 D8 F4 FF 8A 41 ..[..\..,[.....A
>
> 0020: 55 4E 9A 23 3F 55 4A BE 15 D5 09 54 D3 B4 52 AC UN.#?UJ....T..R.
>
> Client MAC write Secret:
>
> 0000: A2 03 04 80 08 E7 02 73 78 16 68 4B 37 DD 9C 2B .......sx.hK7..+
>
> 0010: 4A 0D 79 25 J.y%
>
> Server MAC write Secret:
>
> 0000: 9C 85 E5 FF 7C D4 23 9B FA C8 A8 79 40 C6 E4 D1 ......#....y@...
>
> 0010: 77 8E 5D 90 w.].
>
> Client write key:
>
> 0000: 84 21 98 68 3D B5 C6 C5 02 72 F5 25 DA FA 26 52 .!.h=....r.%..&R
>
> Server write key:
>
> 0000: 6C 9F 46 C6 C7 28 D7 65 05 B6 88 8F CF 91 09 B5 l.F..(.e........
>
> ... no IV used for this cipher
>
> http-bio-8080-exec-2, WRITE: TLSv1 Change Cipher Spec, length = 1
>
> [Raw write]: length = 6
>
> 0000: 14 03 01 00 01 01 ......
>
> *** Finished
>
> verify_data: { 6, 123, 192, 247, 189, 254, 84, 150, 77, 120, 177, 92 }
>
> ***
>
> [write] MD5 and SHA1 hashes: len = 16
>
> 0000: 14 00 00 0C 06 7B C0 F7 BD FE 54 96 4D 78 B1 5C ..........T.Mx.\
>
> Padded plaintext before ENCRYPTION: len = 36
>
> 0000: 14 00 00 0C 06 7B C0 F7 BD FE 54 96 4D 78 B1 5C ..........T.Mx.\
>
> 0010: 4F E1 08 3B F8 8A 9A 46 5B 85 39 0C 66 01 F2 A6 O..;...F[.9.f...
>
> 0020: E4 4C B9 99 .L..
>
> http-bio-8080-exec-2, WRITE: TLSv1 Handshake, length = 36
>
> [Raw write]: length = 41
>
> 0000: 16 03 01 00 24 1C A3 2E D6 86 DE A9 5A DD 23 19 ....$.......Z.#.
>
> 0010: 2C D3 31 99 B6 D6 EF 88 8A 8C 91 E6 A7 72 A7 A8 ,.1..........r..
>
> 0020: DC F0 A7 05 69 49 37 8E 47 ....iI7.G
>
> [Raw read]: length = 5
>
> 0000: 14 03 01 00 01 .....
>
> [Raw read]: length = 1
>
> 0000: 01 .
>
> http-bio-8080-exec-2, READ: TLSv1 Change Cipher Spec, length = 1
>
> [Raw read]: length = 5
>
> 0000: 16 03 01 00 24 ....$
>
> [Raw read]: length = 36
>
> 0000: 80 90 1E 1A 2A 5B 32 58 42 4B 67 7C 2B 2E D7 02 ....*[2XBKg.+...
>
> 0010: 0B 93 9D 5D 9E FE 2B 8E A1 2F BB CA 7C 82 18 C7 ...]..+../......
>
> 0020: 78 84 81 0D x...
>
> http-bio-8080-exec-2, READ: TLSv1 Handshake, length = 36
>
> Padded plaintext after DECRYPTION: len = 36
>
> 0000: 14 00 00 0C 3E BF 40 C7 B6 62 E0 F5 38 B6 EC DD ....>.@..b..8...
>
> 0010: 7E D7 D0 BE DC 5B 6B 0F DD B3 CD DC 95 A6 7D 4B .....[k........K
>
> 0020: 5D C4 B7 55 ]..U
>
> *** Finished
>
> verify_data: { 62, 191, 64, 199, 182, 98, 224, 245, 56, 182, 236, 221 
> }
>
> ***
>
> %% Cached client session: [Session-1, SSL_RSA_WITH_RC4_128_SHA]
>
> [read] MD5 and SHA1 hashes: len = 16
>
> ***Note by paula******Here is the URL call to hibernate
> *********************
>
> 0000: 14 00 00 0C 3E BF 40 C7 B6 62 E0 F5 38 B6 EC DD ....>.@..b..8...
>
> Padded plaintext before ENCRYPTION: len = 119
>
> 0000: 00 00 00 63 00 03 00 00 75 73 65 72 00 70 6F 73 ...c....user.pos
>
> 0010: 74 67 72 65 73 00 64 61 74 61 62 61 73 65 00 72 tgres.database.r
>
> 0020: 75 6E 6E 65 72 73 00 63 6C 69 65 6E 74 5F 65 6E unners.client_en
>
> 0030: 63 6F 64 69 6E 67 00 55 4E 49 43 4F 44 45 00 44 coding.UNICODE.D
>
> 0040: 61 74 65 53 74 79 6C 65 00 49 53 4F 00 65 78 74 ateStyle.ISO.ext
>
> 0050: 72 61 5F 66 6C 6F 61 74 5F 64 69 67 69 74 73 00 ra_float_digits.
>
> 0060: 32 00 00 10 FC 5E CF D9 20 3E 76 EB A5 0E 01 57 2....^.. >v....W
>
> 0070: 45 99 8A 55 A1 6C F6 E..U.l.
>
> http-bio-8080-exec-2, WRITE: TLSv1 Application Data, length = 119
>
> [Raw write]: length = 124
>
> 0000: 17 03 01 00 77 E5 F7 04 85 3E D3 5B 5C 54 B5 A6 ....w....>.[\T..
>
> 0010: B1 B1 31 2B FB 09 BC 93 B4 93 7C 6E 35 FE 90 ED ..1+.......n5...
>
> 0020: 4C A7 44 0F 4B 00 C5 5C 4C 31 E5 9A D3 21 E6 93 L.D.K..\L1...!..
>
> 0030: 24 06 02 F0 04 63 6B 96 D2 57 63 C5 DE C7 62 09 $....ck..Wc...b.
>
> 0040: 43 04 83 C7 80 FD 18 57 AA C0 DF 26 14 CD B7 F9 C......W...&....
>
> 0050: 5C 1F 28 2C CF 9F 54 2F 48 4B AC F4 0E 1B FA CA \.(,..T/HK......
>
> 0060: 0C FE 0B F8 73 25 EA 4E 94 80 91 DE E6 90 1A 63 ....s%.N.......c
>
> 0070: 71 17 01 76 21 34 C8 D5 F3 A0 2C 88 q..v!4....,.
>
> [Raw read]: length = 5
>
> 0000: 17 03 01 00 7B .....
>
> [Raw read]: length = 123
>
> 0000: 3A 60 92 1E AA 94 F1 28 39 95 91 1D 44 8E E9 8B :`.....(9...D...
>
> 0010: 99 DD CA A9 21 F5 08 F9 C2 EB 35 88 51 D5 0D F1 ....!.....5.Q...
>
> 0020: DC 0F D8 5A E3 90 A2 C6 19 CA F3 2D 32 7D 78 8D ...Z.......-2.x.
>
> 0030: 5B AB 5E F1 E9 58 31 60 FF 48 34 E9 C5 9A 88 B6 [.^..X1`.H4.....
>
> 0040: DD 75 44 B8 BB 18 29 29 56 5E FB F2 11 05 D7 3C .uD...))V^.....<
>
> 0050: 60 FA 1A B1 A5 56 33 36 94 E5 BE 1F 8A F3 B7 CC `....V36........
>
> 0060: 2A 5D CC B8 99 62 2B D0 BA F8 2B B2 5A 9F 99 F6 *]...b+...+.Z...
>
> 0070: AF 8C 7F DF 4E D5 F5 4B 8F 3B F3 ....N..K.;.
>
> http-bio-8080-exec-2, READ: TLSv1 Application Data, length = 123
>
> Padded plaintext after DECRYPTION: len = 123
>
> 0000: 45 00 00 00 66 53 46 41 54 41 4C 00 43 32 38 30 E...fSFATAL.C280
>
> 0010: 30 30 00 4D 63 6F 6E 6E 65 63 74 69 6F 6E 20 72 00.Mconnection r
>
> 0020: 65 71 75 69 72 65 73 20 61 20 76 61 6C 69 64 20 equires a valid
>
> 0030: 63 6C 69 65 6E 74 20 63 65 72 74 69 66 69 63 61 client certifica
>
> 0040: 74 65 00 46 61 75 74 68 2E 63 00 4C 33 35 36 00 te.Fauth.c.L356.
>
> 0050: 52 43 6C 69 65 6E 74 41 75 74 68 65 6E 74 69 63 RClientAuthentic
>
> 0060: 61 74 69 6F 6E 00 00 A3 E8 79 7F 76 28 24 67 05 ation....y.v($g.
>
> 0070: C3 07 19 CE 31 00 31 B0 4D FA F0 ....1.1.M..
>
> http-bio-8080-exec-2, called close()
>
> http-bio-8080-exec-2, called closeInternal(true)
>
> http-bio-8080-exec-2, SEND TLSv1 ALERT: warning, description = 
> close_notify
>
> Paula Price
>
> paula.price@issinc.com <mailto:paula.price@issinc.com>
>
> *From:* davecramer@gmail.com [mailto:davecramer@gmail.com] *On Behalf 
> Of *Dave Cramer
> *Sent:* Wednesday, January 16, 2013 4:20 AM
> *To:* Paula Price
> *Cc:* pgsql-jdbc@postgresql.org
> *Subject:* Re: [JDBC] Support for cert auth in JDBC
>
> Hi Paula,
>
> Can you provide us with a bit more information ? Have you talked to 
> hibernate guys to see what the problem is? It would seem that SSL 
> works fine with pg and java, it is when you add hibernate to the mix 
> that everything goes wrong.
>
> Dave
>
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
> On Tue, Jan 15, 2013 at 11:53 AM, Paula Price <paula.price@issinc.com 
> <mailto:paula.price@issinc.com>> wrote:
>
> Hello,
>
> I followed this thread to the end - Support for cert auth in JDBC. I 
> have spent two weeks trying to figure out why hibernate does not work 
> with my postgresql ssl.
>
> I have openssl working great and I have the java certs working with a 
> simple java program. When I throw hibernate into the mix everything 
> goes wrong.
>
> I am trying to get full authentication working. My certs are valid 
> (proved with simple java code).
>
> Is anyone able to help me with the final steps needed to put the 
> CertAuthFactory in the jdbc driver? I have not done java for a couple 
> of years so I may be a little slow (I would also like to see some 
> examples of using the CertAuthFactory). I think I only need it to 
> validate one trust store, so I do not need to pass in the trust store 
> - although I have been known to be wrong before.
>
> Any assistance is greatly appreciated.
>
> Thanks,
>
> Paula Price
>





-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2013-01-17 18:31  dmp <danap@ttc-cmc.net>
  parent: Paula Price <paula.price@issinc.com>
  1 sibling, 0 replies; 28+ messages in thread

From: dmp @ 2013-01-17 18:31 UTC (permalink / raw)
  To: Paula Price <paula.price@issinc.com>; pgsql-jdbc

I know of the CertAuthFactory code and have it in my list of items to
review at some point when I verify SSL with my project. That code as
indicated in the comments has the author as:

@author Marc-André Laverdière (marc-andre@atc.tcs.com / 
marcandre.laverdiere@tcs.com)

I think that Craig Ringer, who has been active on this forum, was maybe
reviewing for possible inclusion in the PgJDBC.

As with any open source project there are limitations on resources
and contributors. If the thread ended on Nov 2, 2011 then that is
all the farther it went with the PgJDBC. If a patch had been submitted
for code to be included then Dave would have probably known more.

danap.

Paula Price wrote:
 > Again, I would not have posted this to this forum except for the fact
 > that I found the initial thread and the last message on the thread said
 > that the CertAuthFactory  was going to be added to the jdbc code.  So,
 > I thought I would give it a try and see if it fixed my problem. I did
 > not mean to bother anyone, I just wanted to know why the CertAuthFactory
 > code never made it into the jdbc jar file and a small example of how
 > to use it.  Please forgive me for any aggravation I have caused, I
 > had run into a wall and was not making progress and I know postgres
 > a lot better than I know hibernate.
 >
 > Thank you for your time,
 > Paula Price
 > paula.price@issinc.com
 >

dmp wrote:
 > Hello,
 >
 > Perhaps someone in this forum may be able to help with implementing
 > the solution you desire, but perhaps you should speak more directly
 > to the individual who created the CerAuthFactory class or initiating
 > the report on Nov. 2, 2011.
 >
 > I'm not sure how this forum is going to be of help to you with pgJDBC
 > when on your own acknowledgment the problem of connecting via SSL
 > appears to be with with the use of Hibernate.
 >
 > danap.


-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 28+ messages in thread

* Re: Support for cert auth in JDBC
@ 2013-01-18 11:45  Dave Cramer <pg@fastcrypt.com>
  parent: Paula Price <paula.price@issinc.com>
  1 sibling, 0 replies; 28+ messages in thread

From: Dave Cramer @ 2013-01-18 11:45 UTC (permalink / raw)
  To: Paula Price <paula.price@issinc.com>; +Cc: dmp <danap@ttc-cmc.net>; pgsql-jdbc

Paula,

You don't need to apologize I don't want you to not post here, just realize
that we don't always have all the answers.

Dave

Dave Cramer

dave.cramer(at)credativ(dot)ca
http://www.credativ.ca


On Thu, Jan 17, 2013 at 1:06 PM, Paula Price <paula.price@issinc.com> wrote:

> Again, I would not have posted this to this forum except for the fact that
> I found the initial thread and the last message on the thread said that the
> CertAuthFactory  was going to be added to the jdbc code.  So, I thought I
> would give it a try and see if it fixed my problem.  I did not mean to
> bother anyone, I just wanted to know why the CertAuthFactory code never
> made it into the jdbc jar file and a small example of how to use it.
>  Please forgive me for any aggravation I have caused, I had run into a wall
> and was not making progress and I know postgres a lot better than I know
> hibernate.
>
> Thank you for your time,
> Paula Price
> paula.price@issinc.com
>
> -----Original Message-----
> From: dmp [mailto:danap@ttc-cmc.net]
> Sent: Thursday, January 17, 2013 10:45 AM
> To: Paula Price; PostgreSQL JDBC
> Subject: Re: [JDBC] Support for cert auth in JDBC
>
> Hello,
>
> Perhaps someone in this forum may be able to help with implementing the
> solution you desire, but perhaps you should speak more directly to the
> individual who created the CerAuthFactory class or initiating the report on
> Nov. 2, 2011.
>
> I'm not sure how this forum is going to be of help to you with pgJDBC when
> on your own acknowledgment the problem of connecting via SSL appears to be
> with with the use of Hibernate.
>
> danap.
>
>
> Paula Price wrote:
> > Dave,
> >
> > I have not spoken with Hibernate although I do think that the problem
> > is most likely with hibernate (or hibernate in tomcat). Since I can
> > get ssl certification working with the jdbc driver then the problem
> > has to be elsewhere. I only wrote to this forum because I found that
> > someone mentioned a similar problem Nov 2, 2011 and added a
> CertAuthFactory.
> >
> > Here is more detail on the problem:
> >
> > Although I downloaded the CertAuthFactory class ( from above mentioned
> > thread), I have not tried adding it to the jdbc driver yet. My simple
> > java code - that works fine - contains a connection call and returns
> > an error if it cannot connect (client is windows 7, postgres 9.1.6 is
> > running on red hat linux 5). Also, full authentication works with Java
> > based application DbVisualizer9.0.
> >
> > My cert Common Name is postgres. The only way into the database is
> > with a valid cert (unless you are local - I wanted to make sure I did
> > not lock myself out of the database). Pg_hba.conf contains:
> >
> > # TYPE DATABASE USER CIDR-ADDRESS METHOD
> >
> > # "local" is for Unix domain socket connections only
> >
> > local all all trust
> >
> > # IPv4 local connections:
> >
> > #host all all 0.0.0.0/0 md5
> >
> > hostssl all all 123.123.123.0 255.255.0.0 cert
> >
> > # IPv6 local connections:
> >
> > #host all all ::1/128 trust
> >
> > When I use my simple java code, I am able to connect just fine using
> > this notation:
> >
> > set JAVA_OPTS=%JAVA_OPTS%
> > -Djavax.net.ssl.trustStore=C:/certs/truststore.jks
> >
> > set JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.trustStorePassword=password
> >
> > set JAVA_OPTS=%JAVA_OPTS%
> > -Djavax.net.ssl.keyStore=C:/certs/keystore.jks
> >
> > set JAVA_OPTS=%JAVA_OPTS% -Djavax.net.ssl.keyStorePassword=password
> >
> > When I try to mix hibernate into the code, it acts as if it does not
> > read in my client cert. I see that trustStore is read and I am able to
> > see the Common Name in the stacktrace (javax.net.debug = all). When
> > authentication reads in the client cert, it reads in total garbage and
> > I have no clue what it thinks it is reading.
> >
> > Below is the relevant part of the stack trace.
> >
> > *****Note by Paula - I made a few simple changes to the stack trace to
> > obscure some readable info - but nothing that should cause problems
> > debugging.
> >
> > *** CertificateRequest
> >
> > Cert Types: RSA, DSS
> >
> > Cert Authorities:
> >
> > <CN=Development, OU=Development, O=Software, L=Colorado, ST=Colorado,
> > C=US>
> >
> > [read] MD5 and SHA1 hashes: len = 158
> >
> > 0000: 0D 00 00 9A 02 01 02 00 95 00 93 30 81 90 31 0B ...........0..1.
> >
> > 0010: 30 09 06 03 55 04 06 13 02 55 53 31 11 30 0F 06 0...U....US1.0..
> >
> > 0020: 03 55 04 08 0C 08 43 6F 6C 6F 72 61 64 6F 31 19 .U....Colorado1.
> >
> > 0030: 30 17 06 03 55 04 07 0C 10 43 6F 6C 6F 72 61 64 0...U....Colorad
> >
> > 0040: 6F 20 53 70 72 69 6E 67 73 31 27 30 25 06 03 55 o1'0%..U
> >
> > 0050: 04 0A 0C 1E 49 6E 74 65 6C 6C 69 67 65 6E 74 20 ....
> >
> > 0060: 53 6F 66 74 77 61 72 65 20 53 6F 6C 75 74 69 6F Software
> >
> > 0070: 6E 73 31 14 30 12 06 03 55 04 0B 0C 0B 44 65 76 1.0...U....Dev
> >
> > 0080: 65 6C 6F 70 6D 65 6E 74 31 14 30 12 06 03 55 04 elopment1.0...U.
> >
> > 0090: 03 0C 0B 44 65 76 65 6C 6F 70 6D 65 6E 74 ...Development
> >
> > *** ServerHelloDone
> >
> > [read] MD5 and SHA1 hashes: len = 4
> >
> > 0000: 0E 00 00 00 ....
> >
> > *** Certificate chain
> >
> > ***
> >
> > *** ClientKeyExchange, RSA PreMasterSecret, TLSv1
> >
> > [write] MD5 and SHA1 hashes: len = 269
> >
> > 0000: 0B 00 00 03 00 00 00 10 00 01 02 01 00 20 20 D5 ............. .
> >
> > 0010: AB 4E 12 10 CE 70 A9 C3 52 1E 4D A9 E7 1B BC ED .N...p..R.M.....
> >
> > 0020: DD 3C 35 F6 B8 8F BF CB BE 31 8C A8 E2 0F E9 79 .<5......1.....y
> >
> > 0030: 0A 0B 58 B7 F7 D4 F8 F8 BC 01 9E 5A C4 9C B2 AF ..X........Z....
> >
> > 0040: 16 17 EB 2E 1A 75 DF 24 D3 22 35 0E 47 B8 09 09 .....u.$."5.G...
> >
> > 0050: 85 01 8E 7F 0B BE D4 BE F1 A0 C3 4E EF F4 10 5C ...........N...\
> >
> > 0060: 85 D6 A0 60 99 E3 2B 88 F4 06 EA 45 2C 83 34 56 ...`..+....E,.4V
> >
> > 0070: B1 36 90 BD 9B 7A 44 C8 CB 00 FF 27 3B 01 CD 19 .6...zD....';...
> >
> > 0080: 70 A5 A7 AF 7D 15 BF 5C C2 FA 7E 19 53 86 52 F0 p......\....S.R.
> >
> > 0090: A9 CA BF 5E 17 4C AA 63 BA 7D 6E 28 F9 2E FB C4 ...^.L.c..n(....
> >
> > 00A0: 17 68 24 8A 9B 28 41 D8 8E F6 3B EA 8E 21 C1 25 .h$..(A...;..!.%
> >
> > 00B0: 10 DB BD C6 07 5F 61 BD 73 F7 09 73 7C 64 CC 38 ....._a.s..s.d.8
> >
> > 00C0: EB 17 E1 8A 48 80 E2 44 C2 38 34 9D AD C6 FC 9F ....H..D.84.....
> >
> > 00D0: EA E6 06 96 34 4A B8 02 E4 B2 72 12 70 A1 00 04 ....4J....r.p...
> >
> > 00E0: DA C0 FE 99 2F E2 E7 A9 DD 27 54 2C 6E 92 12 8E ..../....'T,n...
> >
> > 00F0: D8 BC 27 CB 34 3D F0 F2 39 A5 8D 4E D9 8F FE DF ..'.4=..9..N....
> >
> > 0100: D0 2F 16 AE F4 30 DF 16 F7 5F 63 6C 1E ./...0..._cl.
> >
> > http-bio-8080-exec-2, WRITE: TLSv1 Handshake, length = 269
> >
> > [Raw write]: length = 274
> >
> > 0000: 16 03 01 01 0D 0B 00 00 03 00 00 00 10 00 01 02 ................
> >
> > 0010: 01 00 20 20 D5 AB 4E 12 10 CE 70 A9 C3 52 1E 4D .. ..N...p..R.M
> >
> > 0020: A9 E7 1B BC ED DD 3C 35 F6 B8 8F BF CB BE 31 8C ......<5......1.
> >
> > 0030: A8 E2 0F E9 79 0A 0B 58 B7 F7 D4 F8 F8 BC 01 9E ....y..X........
> >
> > 0040: 5A C4 9C B2 AF 16 17 EB 2E 1A 75 DF 24 D3 22 35 Z.........u.$."5
> >
> > 0050: 0E 47 B8 09 09 85 01 8E 7F 0B BE D4 BE F1 A0 C3 .G..............
> >
> > 0060: 4E EF F4 10 5C 85 D6 A0 60 99 E3 2B 88 F4 06 EA N...\...`..+....
> >
> > 0070: 45 2C 83 34 56 B1 36 90 BD 9B 7A 44 C8 CB 00 FF E,.4V.6...zD....
> >
> > 0080: 27 3B 01 CD 19 70 A5 A7 AF 7D 15 BF 5C C2 FA 7E ';...p......\...
> >
> > 0090: 19 53 86 52 F0 A9 CA BF 5E 17 4C AA 63 BA 7D 6E .S.R....^.L.c..n
> >
> > 00A0: 28 F9 2E FB C4 17 68 24 8A 9B 28 41 D8 8E F6 3B (.....h$..(A...;
> >
> > 00B0: EA 8E 21 C1 25 10 DB BD C6 07 5F 61 BD 73 F7 09 ..!.%....._a.s..
> >
> > 00C0: 73 7C 64 CC 38 EB 17 E1 8A 48 80 E2 44 C2 38 34 s.d.8....H..D.84
> >
> > 00D0: 9D AD C6 FC 9F EA E6 06 96 34 4A B8 02 E4 B2 72 .........4J....r
> >
> > 00E0: 12 70 A1 00 04 DA C0 FE 99 2F E2 E7 A9 DD 27 54 .p......./....'T
> >
> > 00F0: 2C 6E 92 12 8E D8 BC 27 CB 34 3D F0 F2 39 A5 8D ,n.....'.4=..9..
> >
> > 0100: 4E D9 8F FE DF D0 2F 16 AE F4 30 DF 16 F7 5F 63 N...../...0..._c
> >
> > 0110: 6C 1E l.
> >
> > SESSION KEYGEN:
> >
> > PreMaster Secret:
> >
> > 0000: 03 01 47 EE 92 FF 8C 4C 4E FC 58 28 FB 11 0C 98 ..G....LN.X(....
> >
> > 0010: F2 F5 CA 42 46 02 6E 8D 09 AB C3 C5 BD C6 CB AA ...BF.n.........
> >
> > 0020: 4E DB F5 62 FB 2A B8 66 E2 43 C6 B7 DB 50 07 E0 N..b.*.f.C...P..
> >
> > CONNECTION KEYGEN:
> >
> > Client Nonce:
> >
> > 0000: 50 F8 2B DE 26 56 50 F1 8E 81 CB F9 39 0A CE A1 P.+.&VP.....9...
> >
> > 0010: D7 6D 45 20 21 B2 E1 BA 12 DB FB 83 8B D0 37 85 .mE !.........7.
> >
> > Server Nonce:
> >
> > 0000: 50 F8 2B DE C6 C5 A2 14 8B F0 12 1D 64 04 C1 91 P.+.........d...
> >
> > 0010: 8B 16 E6 88 A3 CF 45 82 98 F6 09 1A 06 61 58 10 ......E......aX.
> >
> > Master Secret:
> >
> > 0000: 4F CE 52 E8 17 2E 62 CE 43 0A B5 92 CE BA 7F EC O.R...b.C.......
> >
> > 0010: F7 8F 5B 12 89 5C C2 93 2C 5B 93 D8 F4 FF 8A 41 ..[..\..,[.....A
> >
> > 0020: 55 4E 9A 23 3F 55 4A BE 15 D5 09 54 D3 B4 52 AC UN.#?UJ....T..R.
> >
> > Client MAC write Secret:
> >
> > 0000: A2 03 04 80 08 E7 02 73 78 16 68 4B 37 DD 9C 2B .......sx.hK7..+
> >
> > 0010: 4A 0D 79 25 J.y%
> >
> > Server MAC write Secret:
> >
> > 0000: 9C 85 E5 FF 7C D4 23 9B FA C8 A8 79 40 C6 E4 D1 ......#....y@...
> >
> > 0010: 77 8E 5D 90 w.].
> >
> > Client write key:
> >
> > 0000: 84 21 98 68 3D B5 C6 C5 02 72 F5 25 DA FA 26 52 .!.h=....r.%..&R
> >
> > Server write key:
> >
> > 0000: 6C 9F 46 C6 C7 28 D7 65 05 B6 88 8F CF 91 09 B5 l.F..(.e........
> >
> > ... no IV used for this cipher
> >
> > http-bio-8080-exec-2, WRITE: TLSv1 Change Cipher Spec, length = 1
> >
> > [Raw write]: length = 6
> >
> > 0000: 14 03 01 00 01 01 ......
> >
> > *** Finished
> >
> > verify_data: { 6, 123, 192, 247, 189, 254, 84, 150, 77, 120, 177, 92 }
> >
> > ***
> >
> > [write] MD5 and SHA1 hashes: len = 16
> >
> > 0000: 14 00 00 0C 06 7B C0 F7 BD FE 54 96 4D 78 B1 5C ..........T.Mx.\
> >
> > Padded plaintext before ENCRYPTION: len = 36
> >
> > 0000: 14 00 00 0C 06 7B C0 F7 BD FE 54 96 4D 78 B1 5C ..........T.Mx.\
> >
> > 0010: 4F E1 08 3B F8 8A 9A 46 5B 85 39 0C 66 01 F2 A6 O..;...F[.9.f...
> >
> > 0020: E4 4C B9 99 .L..
> >
> > http-bio-8080-exec-2, WRITE: TLSv1 Handshake, length = 36
> >
> > [Raw write]: length = 41
> >
> > 0000: 16 03 01 00 24 1C A3 2E D6 86 DE A9 5A DD 23 19 ....$.......Z.#.
> >
> > 0010: 2C D3 31 99 B6 D6 EF 88 8A 8C 91 E6 A7 72 A7 A8 ,.1..........r..
> >
> > 0020: DC F0 A7 05 69 49 37 8E 47 ....iI7.G
> >
> > [Raw read]: length = 5
> >
> > 0000: 14 03 01 00 01 .....
> >
> > [Raw read]: length = 1
> >
> > 0000: 01 .
> >
> > http-bio-8080-exec-2, READ: TLSv1 Change Cipher Spec, length = 1
> >
> > [Raw read]: length = 5
> >
> > 0000: 16 03 01 00 24 ....$
> >
> > [Raw read]: length = 36
> >
> > 0000: 80 90 1E 1A 2A 5B 32 58 42 4B 67 7C 2B 2E D7 02 ....*[2XBKg.+...
> >
> > 0010: 0B 93 9D 5D 9E FE 2B 8E A1 2F BB CA 7C 82 18 C7 ...]..+../......
> >
> > 0020: 78 84 81 0D x...
> >
> > http-bio-8080-exec-2, READ: TLSv1 Handshake, length = 36
> >
> > Padded plaintext after DECRYPTION: len = 36
> >
> > 0000: 14 00 00 0C 3E BF 40 C7 B6 62 E0 F5 38 B6 EC DD ....>.@..b..8...
> >
> > 0010: 7E D7 D0 BE DC 5B 6B 0F DD B3 CD DC 95 A6 7D 4B .....[k........K
> >
> > 0020: 5D C4 B7 55 ]..U
> >
> > *** Finished
> >
> > verify_data: { 62, 191, 64, 199, 182, 98, 224, 245, 56, 182, 236, 221
> > }
> >
> > ***
> >
> > %% Cached client session: [Session-1, SSL_RSA_WITH_RC4_128_SHA]
> >
> > [read] MD5 and SHA1 hashes: len = 16
> >
> > ***Note by paula******Here is the URL call to hibernate
> > *********************
> >
> > 0000: 14 00 00 0C 3E BF 40 C7 B6 62 E0 F5 38 B6 EC DD ....>.@..b..8...
> >
> > Padded plaintext before ENCRYPTION: len = 119
> >
> > 0000: 00 00 00 63 00 03 00 00 75 73 65 72 00 70 6F 73 ...c....user.pos
> >
> > 0010: 74 67 72 65 73 00 64 61 74 61 62 61 73 65 00 72 tgres.database.r
> >
> > 0020: 75 6E 6E 65 72 73 00 63 6C 69 65 6E 74 5F 65 6E unners.client_en
> >
> > 0030: 63 6F 64 69 6E 67 00 55 4E 49 43 4F 44 45 00 44 coding.UNICODE.D
> >
> > 0040: 61 74 65 53 74 79 6C 65 00 49 53 4F 00 65 78 74 ateStyle.ISO.ext
> >
> > 0050: 72 61 5F 66 6C 6F 61 74 5F 64 69 67 69 74 73 00 ra_float_digits.
> >
> > 0060: 32 00 00 10 FC 5E CF D9 20 3E 76 EB A5 0E 01 57 2....^.. >v....W
> >
> > 0070: 45 99 8A 55 A1 6C F6 E..U.l.
> >
> > http-bio-8080-exec-2, WRITE: TLSv1 Application Data, length = 119
> >
> > [Raw write]: length = 124
> >
> > 0000: 17 03 01 00 77 E5 F7 04 85 3E D3 5B 5C 54 B5 A6 ....w....>.[\T..
> >
> > 0010: B1 B1 31 2B FB 09 BC 93 B4 93 7C 6E 35 FE 90 ED ..1+.......n5...
> >
> > 0020: 4C A7 44 0F 4B 00 C5 5C 4C 31 E5 9A D3 21 E6 93 L.D.K..\L1...!..
> >
> > 0030: 24 06 02 F0 04 63 6B 96 D2 57 63 C5 DE C7 62 09 $....ck..Wc...b.
> >
> > 0040: 43 04 83 C7 80 FD 18 57 AA C0 DF 26 14 CD B7 F9 C......W...&....
> >
> > 0050: 5C 1F 28 2C CF 9F 54 2F 48 4B AC F4 0E 1B FA CA \.(,..T/HK......
> >
> > 0060: 0C FE 0B F8 73 25 EA 4E 94 80 91 DE E6 90 1A 63 ....s%.N.......c
> >
> > 0070: 71 17 01 76 21 34 C8 D5 F3 A0 2C 88 q..v!4....,.
> >
> > [Raw read]: length = 5
> >
> > 0000: 17 03 01 00 7B .....
> >
> > [Raw read]: length = 123
> >
> > 0000: 3A 60 92 1E AA 94 F1 28 39 95 91 1D 44 8E E9 8B :`.....(9...D...
> >
> > 0010: 99 DD CA A9 21 F5 08 F9 C2 EB 35 88 51 D5 0D F1 ....!.....5.Q...
> >
> > 0020: DC 0F D8 5A E3 90 A2 C6 19 CA F3 2D 32 7D 78 8D ...Z.......-2.x.
> >
> > 0030: 5B AB 5E F1 E9 58 31 60 FF 48 34 E9 C5 9A 88 B6 [.^..X1`.H4.....
> >
> > 0040: DD 75 44 B8 BB 18 29 29 56 5E FB F2 11 05 D7 3C .uD...))V^.....<
> >
> > 0050: 60 FA 1A B1 A5 56 33 36 94 E5 BE 1F 8A F3 B7 CC `....V36........
> >
> > 0060: 2A 5D CC B8 99 62 2B D0 BA F8 2B B2 5A 9F 99 F6 *]...b+...+.Z...
> >
> > 0070: AF 8C 7F DF 4E D5 F5 4B 8F 3B F3 ....N..K.;.
> >
> > http-bio-8080-exec-2, READ: TLSv1 Application Data, length = 123
> >
> > Padded plaintext after DECRYPTION: len = 123
> >
> > 0000: 45 00 00 00 66 53 46 41 54 41 4C 00 43 32 38 30 E...fSFATAL.C280
> >
> > 0010: 30 30 00 4D 63 6F 6E 6E 65 63 74 69 6F 6E 20 72 00.Mconnection r
> >
> > 0020: 65 71 75 69 72 65 73 20 61 20 76 61 6C 69 64 20 equires a valid
> >
> > 0030: 63 6C 69 65 6E 74 20 63 65 72 74 69 66 69 63 61 client certifica
> >
> > 0040: 74 65 00 46 61 75 74 68 2E 63 00 4C 33 35 36 00 te.Fauth.c.L356.
> >
> > 0050: 52 43 6C 69 65 6E 74 41 75 74 68 65 6E 74 69 63 RClientAuthentic
> >
> > 0060: 61 74 69 6F 6E 00 00 A3 E8 79 7F 76 28 24 67 05 ation....y.v($g.
> >
> > 0070: C3 07 19 CE 31 00 31 B0 4D FA F0 ....1.1.M..
> >
> > http-bio-8080-exec-2, called close()
> >
> > http-bio-8080-exec-2, called closeInternal(true)
> >
> > http-bio-8080-exec-2, SEND TLSv1 ALERT: warning, description =
> > close_notify
> >
> > Paula Price
> >
> > paula.price@issinc.com <mailto:paula.price@issinc.com>
> >
> > *From:* davecramer@gmail.com [mailto:davecramer@gmail.com] *On Behalf
> > Of *Dave Cramer
> > *Sent:* Wednesday, January 16, 2013 4:20 AM
> > *To:* Paula Price
> > *Cc:* pgsql-jdbc@postgresql.org
> > *Subject:* Re: [JDBC] Support for cert auth in JDBC
> >
> > Hi Paula,
> >
> > Can you provide us with a bit more information ? Have you talked to
> > hibernate guys to see what the problem is? It would seem that SSL
> > works fine with pg and java, it is when you add hibernate to the mix
> > that everything goes wrong.
> >
> > Dave
> >
> >
> > Dave Cramer
> >
> > dave.cramer(at)credativ(dot)ca
> > http://www.credativ.ca
> >
> > On Tue, Jan 15, 2013 at 11:53 AM, Paula Price <paula.price@issinc.com
> > <mailto:paula.price@issinc.com>> wrote:
> >
> > Hello,
> >
> > I followed this thread to the end - Support for cert auth in JDBC. I
> > have spent two weeks trying to figure out why hibernate does not work
> > with my postgresql ssl.
> >
> > I have openssl working great and I have the java certs working with a
> > simple java program. When I throw hibernate into the mix everything
> > goes wrong.
> >
> > I am trying to get full authentication working. My certs are valid
> > (proved with simple java code).
> >
> > Is anyone able to help me with the final steps needed to put the
> > CertAuthFactory in the jdbc driver? I have not done java for a couple
> > of years so I may be a little slow (I would also like to see some
> > examples of using the CertAuthFactory). I think I only need it to
> > validate one trust store, so I do not need to pass in the trust store
> > - although I have been known to be wrong before.
> >
> > Any assistance is greatly appreciated.
> >
> > Thanks,
> >
> > Paula Price
> >
>
>
>
>
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc
>

^ permalink  raw  reply  [nested|flat] 28+ messages in thread


end of thread, other threads:[~2013-01-18 11:45 UTC | newest]

Thread overview: 28+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-05-18 04:49 Support for cert auth in JDBC Marc-André Laverdière <marc-andre@atc.tcs.com>
2011-05-18 05:40 ` Craig Ringer <craig@postnewspapers.com.au>
2011-05-18 06:06   ` Marc-André Laverdière <marc-andre@atc.tcs.com>
2011-05-18 07:02     ` Craig Ringer <craig@postnewspapers.com.au>
2011-05-18 07:48       ` Marc-André Laverdière <marc-andre@atc.tcs.com>
2011-05-18 13:19         ` Craig Ringer <craig@postnewspapers.com.au>
2011-05-18 15:32           ` Kris Jurka <books@ejurka.com>
2011-05-19 05:46             ` Craig Ringer <craig@postnewspapers.com.au>
2011-05-19 06:14               ` Marc-André Laverdière <marc-andre@atc.tcs.com>
2011-05-24 04:56                 ` Marc-André Laverdière <marc-andre@atc.tcs.com>
2011-05-24 16:27                   ` Kris Jurka <books@ejurka.com>
2011-05-25 01:39                     ` Craig Ringer <craig@postnewspapers.com.au>
2011-06-27 06:18                       ` Marc-André Laverdière <marc-andre@atc.tcs.com>
2011-06-27 08:03                         ` Craig Ringer <craig@postnewspapers.com.au>
2011-05-18 14:03         ` Craig Ringer <craig@postnewspapers.com.au>
2011-05-19 05:58         ` Craig Ringer <craig@postnewspapers.com.au>
2011-05-19 06:41           ` Marc-André Laverdière <marc-andre@atc.tcs.com>
2011-05-19 07:27             ` Craig Ringer <craig@postnewspapers.com.au>
2011-05-19 08:11               ` Marc-André Laverdière <marc-andre@atc.tcs.com>
2011-05-19 08:28                 ` Craig Ringer <craig@postnewspapers.com.au>
2011-11-14 14:23               ` Bruno Harbulot <bruno@distributedmatter.net>
2013-01-15 16:53 Support for cert auth in JDBC Paula Price <paula.price@issinc.com>
2013-01-16 11:20 ` Dave Cramer <pg@fastcrypt.com>
2013-01-17 17:05   ` Paula Price <paula.price@issinc.com>
2013-01-17 17:44     ` dmp <danap@ttc-cmc.net>
2013-01-17 18:06       ` Paula Price <paula.price@issinc.com>
2013-01-17 18:31         ` dmp <danap@ttc-cmc.net>
2013-01-18 11:45         ` Dave Cramer <pg@fastcrypt.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox