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().

132 {
133  BIO *membuf;
134  size_t size;
135  char nullterm;
136  char *sp;
137  char *dp;
138  text *result;
139 
140  membuf = BIO_new(BIO_s_mem());
141  (void) BIO_set_close(membuf, BIO_CLOSE);
142  ASN1_STRING_print_ex(membuf, str,
143  ((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB)
144  | ASN1_STRFLGS_UTF8_CONVERT));
145  /* ensure null termination of the BIO's content */
146  nullterm = '\0';
147  BIO_write(membuf, &nullterm, 1);
148  size = BIO_get_mem_data(membuf, &sp);
149  dp = pg_any_to_server(sp, size - 1, PG_UTF8);
150  result = cstring_to_text(dp);
151  if (dp != sp)
152  pfree(dp);
153  BIO_free(membuf);
154 
155  PG_RETURN_TEXT_P(result);
156 }
void pfree(void *pointer)
Definition: mcxt.c:913
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:140
Definition: c.h:415
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:572
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.

61 {
62  if (MyProcPort->ssl == NULL)
64  PG_RETURN_TEXT_P(cstring_to_text(SSL_get_cipher(MyProcPort->ssl)));
65 }
struct Port * MyProcPort
Definition: globals.c:39
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:140
#define NULL
Definition: c.h:202
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum ssl_client_cert_present ( PG_FUNCTION_ARGS  )

Definition at line 76 of file sslinfo.c.

References MyProcPort, NULL, and PG_RETURN_BOOL.

77 {
78  PG_RETURN_BOOL(MyProcPort->peer != NULL);
79 }
struct Port * MyProcPort
Definition: globals.c:39
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define NULL
Definition: c.h:202
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().

333 {
334  if (!(MyProcPort->peer))
335  PG_RETURN_NULL();
336  return X509_NAME_to_text(X509_get_subject_name(MyProcPort->peer));
337 }
struct Port * MyProcPort
Definition: globals.c:39
static Datum X509_NAME_to_text(X509_NAME *name)
Definition: sslinfo.c:277
#define PG_RETURN_NULL()
Definition: fmgr.h:289
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().

214 {
215  text *fieldname = PG_GETARG_TEXT_P(0);
216  Datum result;
217 
218  if (!(MyProcPort->peer))
219  PG_RETURN_NULL();
220 
221  result = X509_NAME_field_to_text(X509_get_subject_name(MyProcPort->peer), fieldname);
222 
223  if (!result)
224  PG_RETURN_NULL();
225  else
226  return result;
227 }
struct Port * MyProcPort
Definition: globals.c:39
static Datum X509_NAME_field_to_text(X509_NAME *name, text *fieldName)
Definition: sslinfo.c:172
uintptr_t Datum
Definition: postgres.h:374
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:415
#define PG_RETURN_NULL()
Definition: fmgr.h:289
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.

93 {
94  Datum result;
95  Port *port = MyProcPort;
96  X509 *peer = port->peer;
97  ASN1_INTEGER *serial = NULL;
98  BIGNUM *b;
99  char *decimal;
100 
101  if (!peer)
102  PG_RETURN_NULL();
103  serial = X509_get_serialNumber(peer);
104  b = ASN1_INTEGER_to_BN(serial, NULL);
105  decimal = BN_bn2dec(b);
106 
107  BN_free(b);
109  CStringGetDatum(decimal),
110  ObjectIdGetDatum(0),
111  Int32GetDatum(-1));
112  OPENSSL_free(decimal);
113  return result;
114 }
struct Port * MyProcPort
Definition: globals.c:39
Definition: libpq-be.h:123
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define CStringGetDatum(X)
Definition: postgres.h:586
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:515
static int port
Definition: pg_regress.c:87
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:552
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:202
#define Int32GetDatum(X)
Definition: postgres.h:487
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum ssl_is_used ( PG_FUNCTION_ARGS  )

Definition at line 36 of file sslinfo.c.

References MyProcPort, PG_RETURN_BOOL, and Port::ssl_in_use.

37 {
39 }
struct Port * MyProcPort
Definition: globals.c:39
bool ssl_in_use
Definition: libpq-be.h:189
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
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().

352 {
353  if (!(MyProcPort->peer))
354  PG_RETURN_NULL();
355  return X509_NAME_to_text(X509_get_issuer_name(MyProcPort->peer));
356 }
struct Port * MyProcPort
Definition: globals.c:39
static Datum X509_NAME_to_text(X509_NAME *name)
Definition: sslinfo.c:277
#define PG_RETURN_NULL()
Definition: fmgr.h:289
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().

249 {
250  text *fieldname = PG_GETARG_TEXT_P(0);
251  Datum result;
252 
253  if (!(MyProcPort->peer))
254  PG_RETURN_NULL();
255 
256  result = X509_NAME_field_to_text(X509_get_issuer_name(MyProcPort->peer), fieldname);
257 
258  if (!result)
259  PG_RETURN_NULL();
260  else
261  return result;
262 }
struct Port * MyProcPort
Definition: globals.c:39
static Datum X509_NAME_field_to_text(X509_NAME *name, text *fieldName)
Definition: sslinfo.c:172
uintptr_t Datum
Definition: postgres.h:374
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:415
#define PG_RETURN_NULL()
Definition: fmgr.h:289
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.

48 {
49  if (MyProcPort->ssl == NULL)
51  PG_RETURN_TEXT_P(cstring_to_text(SSL_get_version(MyProcPort->ssl)));
52 }
struct Port * MyProcPort
Definition: globals.c:39
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:140
#define NULL
Definition: c.h:202
#define PG_RETURN_NULL()
Definition: fmgr.h:289
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().

173 {
174  char *string_fieldname;
175  int nid,
176  index;
177  ASN1_STRING *data;
178 
179  string_fieldname = text_to_cstring(fieldName);
180  nid = OBJ_txt2nid(string_fieldname);
181  if (nid == NID_undef)
182  ereport(ERROR,
183  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
184  errmsg("invalid X.509 field name: \"%s\"",
185  string_fieldname)));
186  pfree(string_fieldname);
187  index = X509_NAME_get_index_by_NID(name, nid, -1);
188  if (index < 0)
189  return (Datum) 0;
190  data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, index));
191  return ASN1_STRING_to_text(data);
192 }
int errcode(int sqlerrcode)
Definition: elog.c:569
Definition: type.h:90
static Datum ASN1_STRING_to_text(ASN1_STRING *str)
Definition: sslinfo.c:131
void pfree(void *pointer)
Definition: mcxt.c:913
#define ERROR
Definition: elog.h:41
#define ereport(elevel, rest)
Definition: elog.h:132
uintptr_t Datum
Definition: postgres.h:374
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:173
int errmsg(const char *fmt,...)
Definition: elog.c:791
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().

278 {
279  BIO *membuf = BIO_new(BIO_s_mem());
280  int i,
281  nid,
282  count = X509_NAME_entry_count(name);
283  X509_NAME_ENTRY *e;
284  ASN1_STRING *v;
285  const char *field_name;
286  size_t size;
287  char nullterm;
288  char *sp;
289  char *dp;
290  text *result;
291 
292  (void) BIO_set_close(membuf, BIO_CLOSE);
293  for (i = 0; i < count; i++)
294  {
295  e = X509_NAME_get_entry(name, i);
296  nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(e));
297  v = X509_NAME_ENTRY_get_data(e);
298  field_name = OBJ_nid2sn(nid);
299  if (!field_name)
300  field_name = OBJ_nid2ln(nid);
301  BIO_printf(membuf, "/%s=", field_name);
302  ASN1_STRING_print_ex(membuf, v,
303  ((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB)
304  | ASN1_STRFLGS_UTF8_CONVERT));
305  }
306 
307  /* ensure null termination of the BIO's content */
308  nullterm = '\0';
309  BIO_write(membuf, &nullterm, 1);
310  size = BIO_get_mem_data(membuf, &sp);
311  dp = pg_any_to_server(sp, size - 1, PG_UTF8);
312  result = cstring_to_text(dp);
313  if (dp != sp)
314  pfree(dp);
315  BIO_free(membuf);
316 
317  PG_RETURN_TEXT_P(result);
318 }
void pfree(void *pointer)
Definition: mcxt.c:913
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:140
const char * name
Definition: encode.c:521
e
Definition: preproc-init.c:82
int i
Definition: c.h:415
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:572

Variable Documentation

PG_MODULE_MAGIC

Definition at line 21 of file sslinfo.c.