11
11
*
12
12
*
13
13
* IDENTIFICATION
14
- * $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.55 2004/10/16 03:26:43 momjian Exp $
14
+ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.56 2004/11/20 00:18:18 tgl Exp $
15
15
*
16
16
* NOTES
17
17
* [ Most of these notes are wrong/obsolete, but perhaps not all ]
@@ -267,6 +267,11 @@ pqsecure_open_client(PGconn *conn)
267
267
close_SSL (conn );
268
268
return PGRES_POLLING_FAILED ;
269
269
}
270
+ /*
271
+ * Initialize errorMessage to empty. This allows open_client_SSL()
272
+ * to detect whether client_cert_cb() has stored a message.
273
+ */
274
+ resetPQExpBuffer (& conn -> errorMessage );
270
275
}
271
276
/* Begin or continue the actual handshake */
272
277
return open_client_SSL (conn );
@@ -797,7 +802,6 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
797
802
printfPQExpBuffer (& conn -> errorMessage ,
798
803
libpq_gettext ("certificate present, but not private key file \"%s\"\n" ),
799
804
fnbuf );
800
- X509_free (* x509 );
801
805
return 0 ;
802
806
}
803
807
if (!S_ISREG (buf .st_mode ) || (buf .st_mode & 0077 ) ||
@@ -806,23 +810,20 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
806
810
printfPQExpBuffer (& conn -> errorMessage ,
807
811
libpq_gettext ("private key file \"%s\" has wrong permissions\n" ),
808
812
fnbuf );
809
- X509_free (* x509 );
810
813
return 0 ;
811
814
}
812
815
if ((fp = fopen (fnbuf , "r" )) == NULL )
813
816
{
814
817
printfPQExpBuffer (& conn -> errorMessage ,
815
818
libpq_gettext ("could not open private key file \"%s\": %s\n" ),
816
819
fnbuf , pqStrerror (errno , sebuf , sizeof (sebuf )));
817
- X509_free (* x509 );
818
820
return 0 ;
819
821
}
820
822
if (fstat (fileno (fp ), & buf2 ) == -1 ||
821
823
buf .st_dev != buf2 .st_dev || buf .st_ino != buf2 .st_ino )
822
824
{
823
825
printfPQExpBuffer (& conn -> errorMessage ,
824
826
libpq_gettext ("private key file \"%s\" changed during execution\n" ), fnbuf );
825
- X509_free (* x509 );
826
827
return 0 ;
827
828
}
828
829
if (PEM_read_PrivateKey (fp , pkey , cb , NULL ) == NULL )
@@ -833,7 +834,6 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
833
834
libpq_gettext ("could not read private key file \"%s\": %s\n" ),
834
835
fnbuf , err );
835
836
SSLerrfree (err );
836
- X509_free (* x509 );
837
837
fclose (fp );
838
838
return 0 ;
839
839
}
@@ -848,8 +848,6 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
848
848
libpq_gettext ("certificate does not match private key file \"%s\": %s\n" ),
849
849
fnbuf , err );
850
850
SSLerrfree (err );
851
- X509_free (* x509 );
852
- EVP_PKEY_free (* pkey );
853
851
return 0 ;
854
852
}
855
853
@@ -1045,11 +1043,23 @@ open_client_SSL(PGconn *conn)
1045
1043
}
1046
1044
case SSL_ERROR_SSL :
1047
1045
{
1048
- char * err = SSLerrmessage ();
1046
+ /*
1047
+ * If there are problems with the local certificate files,
1048
+ * these will be detected by client_cert_cb() which is
1049
+ * called from SSL_connect(). We want to return that
1050
+ * error message and not the rather unhelpful error that
1051
+ * OpenSSL itself returns. So check to see if an error
1052
+ * message was already stored.
1053
+ */
1054
+ if (conn -> errorMessage .len == 0 )
1055
+ {
1056
+ char * err = SSLerrmessage ();
1049
1057
1050
- printfPQExpBuffer (& conn -> errorMessage ,
1051
- libpq_gettext ("SSL error: %s\n" ), err );
1052
- SSLerrfree (err );
1058
+ printfPQExpBuffer (& conn -> errorMessage ,
1059
+ libpq_gettext ("SSL error: %s\n" ),
1060
+ err );
1061
+ SSLerrfree (err );
1062
+ }
1053
1063
close_SSL (conn );
1054
1064
return PGRES_POLLING_FAILED ;
1055
1065
}
0 commit comments