2006-09-04 23:07:46 +08:00
|
|
|
/*
|
|
|
|
* module for PostgreSQL to access client SSL certificate information
|
|
|
|
*
|
2006-09-15 04:50:51 +08:00
|
|
|
* Written by Victor B. Wagner <vitus@cryptocom.ru>, Cryptocom LTD
|
2006-09-04 23:07:46 +08:00
|
|
|
* This file is distributed under BSD-style license.
|
2006-10-01 02:44:37 +08:00
|
|
|
*
|
2008-11-10 22:57:46 +08:00
|
|
|
* $PostgreSQL: pgsql/contrib/sslinfo/sslinfo.c,v 1.6.2.1 2008/11/10 14:57:46 tgl Exp $
|
2006-09-04 23:07:46 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "fmgr.h"
|
|
|
|
#include "utils/numeric.h"
|
|
|
|
#include "libpq/libpq-be.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "mb/pg_wchar.h"
|
|
|
|
|
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/asn1.h>
|
|
|
|
|
|
|
|
|
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum ssl_is_used(PG_FUNCTION_ARGS);
|
|
|
|
Datum ssl_client_cert_present(PG_FUNCTION_ARGS);
|
|
|
|
Datum ssl_client_serial(PG_FUNCTION_ARGS);
|
|
|
|
Datum ssl_client_dn_field(PG_FUNCTION_ARGS);
|
|
|
|
Datum ssl_issuer_field(PG_FUNCTION_ARGS);
|
|
|
|
Datum ssl_client_dn(PG_FUNCTION_ARGS);
|
|
|
|
Datum ssl_issuer_dn(PG_FUNCTION_ARGS);
|
|
|
|
Datum X509_NAME_field_to_text(X509_NAME *name, text *fieldName);
|
|
|
|
Datum X509_NAME_to_text(X509_NAME *name);
|
|
|
|
Datum ASN1_STRING_to_text(ASN1_STRING *str);
|
2006-09-04 23:07:46 +08:00
|
|
|
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
2006-09-04 23:07:46 +08:00
|
|
|
* Indicates whether current session uses SSL
|
|
|
|
*
|
|
|
|
* Function has no arguments. Returns bool. True if current session
|
|
|
|
* is SSL session and false if it is local or non-ssl session.
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(ssl_is_used);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum
|
|
|
|
ssl_is_used(PG_FUNCTION_ARGS)
|
2006-09-04 23:07:46 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_RETURN_BOOL(MyProcPort->ssl != NULL);
|
2006-09-04 23:07:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Indicates whether current client have provided a certificate
|
|
|
|
*
|
|
|
|
* Function has no arguments. Returns bool. True if current session
|
|
|
|
* is SSL session and client certificate is verified, otherwise false.
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(ssl_client_cert_present);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum
|
|
|
|
ssl_client_cert_present(PG_FUNCTION_ARGS)
|
2006-09-04 23:07:46 +08:00
|
|
|
{
|
|
|
|
PG_RETURN_BOOL(MyProcPort->peer != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns serial number of certificate used to establish current
|
|
|
|
* session
|
|
|
|
*
|
|
|
|
* Function has no arguments. It returns the certificate serial
|
|
|
|
* number as numeric or null if current session doesn't use SSL or if
|
|
|
|
* SSL connection is established without sending client certificate.
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(ssl_client_serial);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum
|
|
|
|
ssl_client_serial(PG_FUNCTION_ARGS)
|
2006-09-04 23:07:46 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum result;
|
|
|
|
Port *port = MyProcPort;
|
|
|
|
X509 *peer = port->peer;
|
2006-09-04 23:07:46 +08:00
|
|
|
ASN1_INTEGER *serial = NULL;
|
2006-10-04 08:30:14 +08:00
|
|
|
BIGNUM *b;
|
|
|
|
char *decimal;
|
2006-09-04 23:07:46 +08:00
|
|
|
|
|
|
|
if (!peer)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
serial = X509_get_serialNumber(peer);
|
|
|
|
b = ASN1_INTEGER_to_BN(serial, NULL);
|
|
|
|
decimal = BN_bn2dec(b);
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-04 23:07:46 +08:00
|
|
|
BN_free(b);
|
|
|
|
result = DirectFunctionCall3(numeric_in,
|
|
|
|
CStringGetDatum(decimal),
|
|
|
|
ObjectIdGetDatum(0),
|
|
|
|
Int32GetDatum(-1));
|
|
|
|
OPENSSL_free(decimal);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts OpenSSL ASN1_STRING structure into text
|
|
|
|
*
|
|
|
|
* Converts ASN1_STRING into text, converting all the characters into
|
|
|
|
* current database encoding if possible. Any invalid characters are
|
|
|
|
* replaced by question marks.
|
|
|
|
*
|
2006-10-04 08:30:14 +08:00
|
|
|
* Parameter: str - OpenSSL ASN1_STRING structure. Memory managment
|
2006-09-04 23:07:46 +08:00
|
|
|
* of this structure is responsibility of caller.
|
|
|
|
*
|
|
|
|
* Returns Datum, which can be directly returned from a C language SQL
|
|
|
|
* function.
|
|
|
|
*/
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum
|
|
|
|
ASN1_STRING_to_text(ASN1_STRING *str)
|
2006-09-04 23:07:46 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
BIO *membuf = NULL;
|
|
|
|
size_t size,
|
|
|
|
outlen;
|
|
|
|
char *sp;
|
|
|
|
char *dp;
|
|
|
|
text *result;
|
2006-09-04 23:07:46 +08:00
|
|
|
|
|
|
|
membuf = BIO_new(BIO_s_mem());
|
2006-10-01 02:44:37 +08:00
|
|
|
(void) BIO_set_close(membuf, BIO_CLOSE);
|
2006-10-04 08:30:14 +08:00
|
|
|
ASN1_STRING_print_ex(membuf, str,
|
2006-09-04 23:07:46 +08:00
|
|
|
((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB)
|
|
|
|
| ASN1_STRFLGS_UTF8_CONVERT));
|
|
|
|
|
|
|
|
outlen = 0;
|
|
|
|
BIO_write(membuf, &outlen, 1);
|
|
|
|
size = BIO_get_mem_data(membuf, &sp);
|
2006-09-06 07:07:16 +08:00
|
|
|
dp = (char *) pg_do_encoding_conversion((unsigned char *) sp,
|
2006-10-04 08:30:14 +08:00
|
|
|
size - 1,
|
2006-09-06 07:07:16 +08:00
|
|
|
PG_UTF8,
|
|
|
|
GetDatabaseEncoding());
|
2006-09-04 23:07:46 +08:00
|
|
|
outlen = strlen(dp);
|
|
|
|
result = palloc(VARHDRSZ + outlen);
|
|
|
|
memcpy(VARDATA(result), dp, outlen);
|
2007-02-28 07:48:10 +08:00
|
|
|
SET_VARSIZE(result, VARHDRSZ + outlen);
|
|
|
|
|
2006-09-04 23:07:46 +08:00
|
|
|
if (dp != sp)
|
|
|
|
pfree(dp);
|
|
|
|
BIO_free(membuf);
|
2007-02-28 07:48:10 +08:00
|
|
|
|
2006-09-04 23:07:46 +08:00
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns specified field of specified X509_NAME structure
|
|
|
|
*
|
|
|
|
* Common part of ssl_client_dn and ssl_issuer_dn functions.
|
|
|
|
*
|
|
|
|
* Parameter: X509_NAME *name - either subject or issuer of certificate
|
|
|
|
* Parameter: text fieldName - field name string like 'CN' or commonName
|
2006-10-04 08:30:14 +08:00
|
|
|
* to be looked up in the OpenSSL ASN1 OID database
|
2006-09-04 23:07:46 +08:00
|
|
|
*
|
|
|
|
* Returns result of ASN1_STRING_to_text applied to appropriate
|
|
|
|
* part of name
|
|
|
|
*/
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum
|
|
|
|
X509_NAME_field_to_text(X509_NAME *name, text *fieldName)
|
2006-09-04 23:07:46 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
char *sp;
|
|
|
|
char *string_fieldname;
|
|
|
|
char *dp;
|
|
|
|
size_t name_len = VARSIZE(fieldName) - VARHDRSZ;
|
|
|
|
int nid,
|
|
|
|
index,
|
|
|
|
i;
|
2006-09-04 23:07:46 +08:00
|
|
|
ASN1_STRING *data;
|
|
|
|
|
|
|
|
string_fieldname = palloc(name_len + 1);
|
|
|
|
sp = VARDATA(fieldName);
|
|
|
|
dp = string_fieldname;
|
|
|
|
for (i = 0; i < name_len; i++)
|
|
|
|
*dp++ = *sp++;
|
|
|
|
*dp = '\0';
|
|
|
|
nid = OBJ_txt2nid(string_fieldname);
|
|
|
|
if (nid == NID_undef)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid X.509 field name: \"%s\"",
|
|
|
|
string_fieldname)));
|
|
|
|
pfree(string_fieldname);
|
|
|
|
index = X509_NAME_get_index_by_NID(name, nid, -1);
|
|
|
|
if (index < 0)
|
2006-10-04 08:30:14 +08:00
|
|
|
return (Datum) 0;
|
2006-09-04 23:07:46 +08:00
|
|
|
data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, index));
|
|
|
|
return ASN1_STRING_to_text(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns specified field of client certificate distinguished name
|
|
|
|
*
|
|
|
|
* Receives field name (like 'commonName' and 'emailAddress') and
|
|
|
|
* returns appropriate part of certificate subject converted into
|
|
|
|
* database encoding.
|
|
|
|
*
|
|
|
|
* Parameter: fieldname text - will be looked up in OpenSSL object
|
|
|
|
* identifier database
|
|
|
|
*
|
|
|
|
* Returns text string with appropriate value.
|
|
|
|
*
|
|
|
|
* Throws an error if argument cannot be converted into ASN1 OID by
|
|
|
|
* OpenSSL. Returns null if no client certificate is present, or if
|
|
|
|
* there is no field with such name in the certificate.
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(ssl_client_dn_field);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum
|
|
|
|
ssl_client_dn_field(PG_FUNCTION_ARGS)
|
2006-09-04 23:07:46 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
text *fieldname = PG_GETARG_TEXT_P(0);
|
|
|
|
Datum result;
|
2006-09-04 23:07:46 +08:00
|
|
|
|
|
|
|
if (!(MyProcPort->peer))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
result = X509_NAME_field_to_text(X509_get_subject_name(MyProcPort->peer), fieldname);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
else
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns specified field of client certificate issuer name
|
|
|
|
*
|
|
|
|
* Receives field name (like 'commonName' and 'emailAddress') and
|
|
|
|
* returns appropriate part of certificate subject converted into
|
|
|
|
* database encoding.
|
|
|
|
*
|
|
|
|
* Parameter: fieldname text - would be looked up in OpenSSL object
|
|
|
|
* identifier database
|
|
|
|
*
|
|
|
|
* Returns text string with appropriate value.
|
|
|
|
*
|
|
|
|
* Throws an error if argument cannot be converted into ASN1 OID by
|
|
|
|
* OpenSSL. Returns null if no client certificate is present, or if
|
|
|
|
* there is no field with such name in the certificate.
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(ssl_issuer_field);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum
|
|
|
|
ssl_issuer_field(PG_FUNCTION_ARGS)
|
2006-09-04 23:07:46 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
text *fieldname = PG_GETARG_TEXT_P(0);
|
|
|
|
Datum result;
|
2006-09-04 23:07:46 +08:00
|
|
|
|
|
|
|
if (!(MyProcPort->peer))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
result = X509_NAME_field_to_text(X509_get_issuer_name(MyProcPort->peer), fieldname);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
else
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Equivalent of X509_NAME_oneline that respects encoding
|
|
|
|
*
|
|
|
|
* This function converts X509_NAME structure to the text variable
|
|
|
|
* converting all textual data into current database encoding.
|
|
|
|
*
|
|
|
|
* Parameter: X509_NAME *name X509_NAME structure to be converted
|
|
|
|
*
|
|
|
|
* Returns: text datum which contains string representation of
|
|
|
|
* X509_NAME
|
|
|
|
*/
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum
|
|
|
|
X509_NAME_to_text(X509_NAME *name)
|
2006-09-04 23:07:46 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
BIO *membuf = BIO_new(BIO_s_mem());
|
|
|
|
int i,
|
|
|
|
nid,
|
|
|
|
count = X509_NAME_entry_count(name);
|
2006-09-04 23:07:46 +08:00
|
|
|
X509_NAME_ENTRY *e;
|
|
|
|
ASN1_STRING *v;
|
|
|
|
|
|
|
|
const char *field_name;
|
2006-10-04 08:30:14 +08:00
|
|
|
size_t size,
|
|
|
|
outlen;
|
|
|
|
char *sp;
|
|
|
|
char *dp;
|
|
|
|
text *result;
|
2006-09-04 23:07:46 +08:00
|
|
|
|
2006-10-01 02:44:37 +08:00
|
|
|
(void) BIO_set_close(membuf, BIO_CLOSE);
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = 0; i < count; i++)
|
2006-09-04 23:07:46 +08:00
|
|
|
{
|
|
|
|
e = X509_NAME_get_entry(name, i);
|
|
|
|
nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(e));
|
|
|
|
v = X509_NAME_ENTRY_get_data(e);
|
|
|
|
field_name = OBJ_nid2sn(nid);
|
|
|
|
if (!field_name)
|
|
|
|
field_name = OBJ_nid2ln(nid);
|
|
|
|
BIO_printf(membuf, "/%s=", field_name);
|
2006-10-04 08:30:14 +08:00
|
|
|
ASN1_STRING_print_ex(membuf, v,
|
2006-09-04 23:07:46 +08:00
|
|
|
((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB)
|
|
|
|
| ASN1_STRFLGS_UTF8_CONVERT));
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
i = 0;
|
2006-09-04 23:07:46 +08:00
|
|
|
BIO_write(membuf, &i, 1);
|
|
|
|
size = BIO_get_mem_data(membuf, &sp);
|
|
|
|
|
2006-09-06 07:07:16 +08:00
|
|
|
dp = (char *) pg_do_encoding_conversion((unsigned char *) sp,
|
2006-10-04 08:30:14 +08:00
|
|
|
size - 1,
|
2006-09-06 07:07:16 +08:00
|
|
|
PG_UTF8,
|
|
|
|
GetDatabaseEncoding());
|
2006-09-04 23:07:46 +08:00
|
|
|
outlen = strlen(dp);
|
|
|
|
result = palloc(VARHDRSZ + outlen);
|
|
|
|
memcpy(VARDATA(result), dp, outlen);
|
2007-02-28 07:48:10 +08:00
|
|
|
SET_VARSIZE(result, VARHDRSZ + outlen);
|
2006-09-04 23:07:46 +08:00
|
|
|
|
|
|
|
if (dp != sp)
|
|
|
|
pfree(dp);
|
2008-11-10 22:57:46 +08:00
|
|
|
BIO_free(membuf);
|
2007-02-28 07:48:10 +08:00
|
|
|
|
2006-09-04 23:07:46 +08:00
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns current client certificate subject as one string
|
|
|
|
*
|
|
|
|
* This function returns distinguished name (subject) of the client
|
|
|
|
* certificate used in the current SSL connection, converting it into
|
|
|
|
* the current database encoding.
|
|
|
|
*
|
|
|
|
* Returns text datum.
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(ssl_client_dn);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum
|
|
|
|
ssl_client_dn(PG_FUNCTION_ARGS)
|
2006-09-04 23:07:46 +08:00
|
|
|
{
|
|
|
|
if (!(MyProcPort->peer))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
return X509_NAME_to_text(X509_get_subject_name(MyProcPort->peer));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns current client certificate issuer as one string
|
|
|
|
*
|
|
|
|
* This function returns issuer's distinguished name of the client
|
|
|
|
* certificate used in the current SSL connection, converting it into
|
|
|
|
* the current database encoding.
|
|
|
|
*
|
|
|
|
* Returns text datum.
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(ssl_issuer_dn);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum
|
|
|
|
ssl_issuer_dn(PG_FUNCTION_ARGS)
|
2006-09-04 23:07:46 +08:00
|
|
|
{
|
|
|
|
if (!(MyProcPort->peer))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
return X509_NAME_to_text(X509_get_issuer_name(MyProcPort->peer));
|
|
|
|
}
|