PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
sslinfo.c File Reference
#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>
Include dependency graph for sslinfo.c:

Go to the source code of this file.

Functions

static Datum X509_NAME_field_to_text (X509_NAME *name, text *fieldName)
static Datum X509_NAME_to_text (X509_NAME *name)
static Datum ASN1_STRING_to_text (ASN1_STRING *str)
 PG_FUNCTION_INFO_V1 (ssl_is_used)
Datum ssl_is_used (PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1 (ssl_version)
Datum ssl_version (PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1 (ssl_cipher)
Datum ssl_cipher (PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1 (ssl_client_cert_present)
Datum ssl_client_cert_present (PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1 (ssl_client_serial)
Datum ssl_client_serial (PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1 (ssl_client_dn_field)
Datum ssl_client_dn_field (PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1 (ssl_issuer_field)
Datum ssl_issuer_field (PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1 (ssl_client_dn)
Datum ssl_client_dn (PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1 (ssl_issuer_dn)
Datum ssl_issuer_dn (PG_FUNCTION_ARGS)

Variables

 PG_MODULE_MAGIC

Function Documentation

static Datum ASN1_STRING_to_text ( ASN1_STRING *  str)
static

Definition at line 131 of file sslinfo.c.

References cstring_to_text(), pfree(), pg_any_to_server(), PG_RETURN_TEXT_P, and PG_UTF8.

Referenced by X509_NAME_field_to_text().

{
BIO *membuf;
size_t size;
char nullterm;
char *sp;
char *dp;
text *result;
membuf = BIO_new(BIO_s_mem());
(void) BIO_set_close(membuf, BIO_CLOSE);
ASN1_STRING_print_ex(membuf, str,
((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB)
| ASN1_STRFLGS_UTF8_CONVERT));
/* ensure null termination of the BIO's content */
nullterm = '\0';
BIO_write(membuf, &nullterm, 1);
size = BIO_get_mem_data(membuf, &sp);
dp = pg_any_to_server(sp, size - 1, PG_UTF8);
result = cstring_to_text(dp);
if (dp != sp)
pfree(dp);
BIO_free(membuf);
}
PG_FUNCTION_INFO_V1 ( ssl_is_used  )
PG_FUNCTION_INFO_V1 ( ssl_version  )
PG_FUNCTION_INFO_V1 ( ssl_cipher  )
PG_FUNCTION_INFO_V1 ( ssl_client_cert_present  )
PG_FUNCTION_INFO_V1 ( ssl_client_serial  )
PG_FUNCTION_INFO_V1 ( ssl_client_dn_field  )
PG_FUNCTION_INFO_V1 ( ssl_issuer_field  )
PG_FUNCTION_INFO_V1 ( ssl_client_dn  )
PG_FUNCTION_INFO_V1 ( ssl_issuer_dn  )
Datum ssl_cipher ( PG_FUNCTION_ARGS  )

Definition at line 60 of file sslinfo.c.

References cstring_to_text(), MyProcPort, NULL, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

{
if (MyProcPort->ssl == NULL)
}
Datum ssl_client_cert_present ( PG_FUNCTION_ARGS  )

Definition at line 76 of file sslinfo.c.

References MyProcPort, NULL, and PG_RETURN_BOOL.

Datum ssl_client_dn ( PG_FUNCTION_ARGS  )

Definition at line 332 of file sslinfo.c.

References MyProcPort, PG_RETURN_NULL, and X509_NAME_to_text().

{
if (!(MyProcPort->peer))
return X509_NAME_to_text(X509_get_subject_name(MyProcPort->peer));
}
Datum ssl_client_dn_field ( PG_FUNCTION_ARGS  )

Definition at line 213 of file sslinfo.c.

References MyProcPort, PG_GETARG_TEXT_P, PG_RETURN_NULL, and X509_NAME_field_to_text().

{
text *fieldname = PG_GETARG_TEXT_P(0);
Datum result;
if (!(MyProcPort->peer))
result = X509_NAME_field_to_text(X509_get_subject_name(MyProcPort->peer), fieldname);
if (!result)
else
return result;
}
Datum ssl_client_serial ( PG_FUNCTION_ARGS  )

Definition at line 92 of file sslinfo.c.

References CStringGetDatum, DirectFunctionCall3, Int32GetDatum, MyProcPort, NULL, numeric_in(), ObjectIdGetDatum, PG_RETURN_NULL, and port.

{
Datum result;
X509 *peer = port->peer;
ASN1_INTEGER *serial = NULL;
BIGNUM *b;
char *decimal;
if (!peer)
serial = X509_get_serialNumber(peer);
b = ASN1_INTEGER_to_BN(serial, NULL);
decimal = BN_bn2dec(b);
BN_free(b);
CStringGetDatum(decimal),
OPENSSL_free(decimal);
return result;
}
Datum ssl_is_used ( PG_FUNCTION_ARGS  )

Definition at line 36 of file sslinfo.c.

References MyProcPort, NULL, and PG_RETURN_BOOL.

Datum ssl_issuer_dn ( PG_FUNCTION_ARGS  )

Definition at line 351 of file sslinfo.c.

References MyProcPort, PG_RETURN_NULL, and X509_NAME_to_text().

{
if (!(MyProcPort->peer))
return X509_NAME_to_text(X509_get_issuer_name(MyProcPort->peer));
}
Datum ssl_issuer_field ( PG_FUNCTION_ARGS  )

Definition at line 248 of file sslinfo.c.

References MyProcPort, PG_GETARG_TEXT_P, PG_RETURN_NULL, and X509_NAME_field_to_text().

{
text *fieldname = PG_GETARG_TEXT_P(0);
Datum result;
if (!(MyProcPort->peer))
result = X509_NAME_field_to_text(X509_get_issuer_name(MyProcPort->peer), fieldname);
if (!result)
else
return result;
}
Datum ssl_version ( PG_FUNCTION_ARGS  )

Definition at line 47 of file sslinfo.c.

References cstring_to_text(), MyProcPort, NULL, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

{
if (MyProcPort->ssl == NULL)
PG_RETURN_TEXT_P(cstring_to_text(SSL_get_version(MyProcPort->ssl)));
}
static Datum X509_NAME_field_to_text ( X509_NAME *  name,
text fieldName 
)
static

Definition at line 172 of file sslinfo.c.

References ASN1_STRING_to_text(), ereport, errcode(), errmsg(), ERROR, pfree(), and text_to_cstring().

Referenced by ssl_client_dn_field(), and ssl_issuer_field().

{
char *string_fieldname;
int nid,
ASN1_STRING *data;
string_fieldname = text_to_cstring(fieldName);
nid = OBJ_txt2nid(string_fieldname);
if (nid == NID_undef)
(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)
return (Datum) 0;
data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, index));
return ASN1_STRING_to_text(data);
}
static Datum X509_NAME_to_text ( X509_NAME *  name)
static

Definition at line 277 of file sslinfo.c.

References cstring_to_text(), i, pfree(), pg_any_to_server(), PG_RETURN_TEXT_P, and PG_UTF8.

Referenced by ssl_client_dn(), and ssl_issuer_dn().

{
BIO *membuf = BIO_new(BIO_s_mem());
int i,
nid,
count = X509_NAME_entry_count(name);
X509_NAME_ENTRY *e;
ASN1_STRING *v;
const char *field_name;
size_t size;
char nullterm;
char *sp;
char *dp;
text *result;
(void) BIO_set_close(membuf, BIO_CLOSE);
for (i = 0; i < count; i++)
{
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);
ASN1_STRING_print_ex(membuf, v,
((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB)
| ASN1_STRFLGS_UTF8_CONVERT));
}
/* ensure null termination of the BIO's content */
nullterm = '\0';
BIO_write(membuf, &nullterm, 1);
size = BIO_get_mem_data(membuf, &sp);
dp = pg_any_to_server(sp, size - 1, PG_UTF8);
result = cstring_to_text(dp);
if (dp != sp)
pfree(dp);
BIO_free(membuf);
}

Variable Documentation

PG_MODULE_MAGIC

Definition at line 21 of file sslinfo.c.