22
22
PG_MODULE_MAGIC ;
23
23
24
24
static Datum X509_NAME_field_to_text (X509_NAME * name , text * fieldName );
25
- static Datum X509_NAME_to_text (X509_NAME * name );
26
25
static Datum ASN1_STRING_to_text (ASN1_STRING * str );
27
26
28
27
/*
@@ -54,9 +53,16 @@ PG_FUNCTION_INFO_V1(ssl_version);
54
53
Datum
55
54
ssl_version (PG_FUNCTION_ARGS )
56
55
{
57
- if (MyProcPort -> ssl == NULL )
56
+ const char * version ;
57
+
58
+ if (!MyProcPort -> ssl_in_use )
59
+ PG_RETURN_NULL ();
60
+
61
+ version = be_tls_get_version (MyProcPort );
62
+ if (version == NULL )
58
63
PG_RETURN_NULL ();
59
- PG_RETURN_TEXT_P (cstring_to_text (SSL_get_version (MyProcPort -> ssl )));
64
+
65
+ PG_RETURN_TEXT_P (cstring_to_text (version ));
60
66
}
61
67
62
68
@@ -67,9 +73,16 @@ PG_FUNCTION_INFO_V1(ssl_cipher);
67
73
Datum
68
74
ssl_cipher (PG_FUNCTION_ARGS )
69
75
{
70
- if (MyProcPort -> ssl == NULL )
76
+ const char * cipher ;
77
+
78
+ if (!MyProcPort -> ssl_in_use )
79
+ PG_RETURN_NULL ();
80
+
81
+ cipher = be_tls_get_cipher (MyProcPort );
82
+ if (cipher == NULL )
71
83
PG_RETURN_NULL ();
72
- PG_RETURN_TEXT_P (cstring_to_text (SSL_get_cipher (MyProcPort -> ssl )));
84
+
85
+ PG_RETURN_TEXT_P (cstring_to_text (cipher ));
73
86
}
74
87
75
88
@@ -83,7 +96,7 @@ PG_FUNCTION_INFO_V1(ssl_client_cert_present);
83
96
Datum
84
97
ssl_client_cert_present (PG_FUNCTION_ARGS )
85
98
{
86
- PG_RETURN_BOOL (MyProcPort -> peer != NULL );
99
+ PG_RETURN_BOOL (MyProcPort -> peer_cert_valid );
87
100
}
88
101
89
102
@@ -99,25 +112,21 @@ PG_FUNCTION_INFO_V1(ssl_client_serial);
99
112
Datum
100
113
ssl_client_serial (PG_FUNCTION_ARGS )
101
114
{
115
+ char decimal [NAMEDATALEN ];
102
116
Datum result ;
103
- Port * port = MyProcPort ;
104
- X509 * peer = port -> peer ;
105
- ASN1_INTEGER * serial = NULL ;
106
- BIGNUM * b ;
107
- char * decimal ;
108
117
109
- if (!peer )
118
+ if (!MyProcPort -> ssl_in_use || !MyProcPort -> peer_cert_valid )
119
+ PG_RETURN_NULL ();
120
+
121
+ be_tls_get_peer_serial (MyProcPort , decimal , NAMEDATALEN );
122
+
123
+ if (!* decimal )
110
124
PG_RETURN_NULL ();
111
- serial = X509_get_serialNumber (peer );
112
- b = ASN1_INTEGER_to_BN (serial , NULL );
113
- decimal = BN_bn2dec (b );
114
125
115
- BN_free (b );
116
126
result = DirectFunctionCall3 (numeric_in ,
117
127
CStringGetDatum (decimal ),
118
128
ObjectIdGetDatum (0 ),
119
129
Int32GetDatum (-1 ));
120
- OPENSSL_free (decimal );
121
130
return result ;
122
131
}
123
132
@@ -228,7 +237,7 @@ ssl_client_dn_field(PG_FUNCTION_ARGS)
228
237
text * fieldname = PG_GETARG_TEXT_PP (0 );
229
238
Datum result ;
230
239
231
- if (!( MyProcPort -> peer ) )
240
+ if (!MyProcPort -> ssl_in_use || ! MyProcPort -> peer_cert_valid )
232
241
PG_RETURN_NULL ();
233
242
234
243
result = X509_NAME_field_to_text (X509_get_subject_name (MyProcPort -> peer ), fieldname );
@@ -275,76 +284,6 @@ ssl_issuer_field(PG_FUNCTION_ARGS)
275
284
}
276
285
277
286
278
- /*
279
- * Equivalent of X509_NAME_oneline that respects encoding
280
- *
281
- * This function converts X509_NAME structure to the text variable
282
- * converting all textual data into current database encoding.
283
- *
284
- * Parameter: X509_NAME *name X509_NAME structure to be converted
285
- *
286
- * Returns: text datum which contains string representation of
287
- * X509_NAME
288
- */
289
- static Datum
290
- X509_NAME_to_text (X509_NAME * name )
291
- {
292
- BIO * membuf = BIO_new (BIO_s_mem ());
293
- int i ,
294
- nid ,
295
- count = X509_NAME_entry_count (name );
296
- X509_NAME_ENTRY * e ;
297
- ASN1_STRING * v ;
298
- const char * field_name ;
299
- size_t size ;
300
- char nullterm ;
301
- char * sp ;
302
- char * dp ;
303
- text * result ;
304
-
305
- if (membuf == NULL )
306
- ereport (ERROR ,
307
- (errcode (ERRCODE_OUT_OF_MEMORY ),
308
- errmsg ("could not create OpenSSL BIO structure" )));
309
-
310
- (void ) BIO_set_close (membuf , BIO_CLOSE );
311
- for (i = 0 ; i < count ; i ++ )
312
- {
313
- e = X509_NAME_get_entry (name , i );
314
- nid = OBJ_obj2nid (X509_NAME_ENTRY_get_object (e ));
315
- if (nid == NID_undef )
316
- ereport (ERROR ,
317
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
318
- errmsg ("could not get NID for ASN1_OBJECT object" )));
319
- v = X509_NAME_ENTRY_get_data (e );
320
- field_name = OBJ_nid2sn (nid );
321
- if (field_name == NULL )
322
- field_name = OBJ_nid2ln (nid );
323
- if (field_name == NULL )
324
- ereport (ERROR ,
325
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
326
- errmsg ("could not convert NID %d to an ASN1_OBJECT structure" , nid )));
327
- BIO_printf (membuf , "/%s=" , field_name );
328
- ASN1_STRING_print_ex (membuf , v ,
329
- ((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB )
330
- | ASN1_STRFLGS_UTF8_CONVERT ));
331
- }
332
-
333
- /* ensure null termination of the BIO's content */
334
- nullterm = '\0' ;
335
- BIO_write (membuf , & nullterm , 1 );
336
- size = BIO_get_mem_data (membuf , & sp );
337
- dp = pg_any_to_server (sp , size - 1 , PG_UTF8 );
338
- result = cstring_to_text (dp );
339
- if (dp != sp )
340
- pfree (dp );
341
- if (BIO_free (membuf ) != 1 )
342
- elog (ERROR , "could not free OpenSSL BIO structure" );
343
-
344
- PG_RETURN_TEXT_P (result );
345
- }
346
-
347
-
348
287
/*
349
288
* Returns current client certificate subject as one string
350
289
*
@@ -358,9 +297,17 @@ PG_FUNCTION_INFO_V1(ssl_client_dn);
358
297
Datum
359
298
ssl_client_dn (PG_FUNCTION_ARGS )
360
299
{
361
- if (!(MyProcPort -> peer ))
300
+ char subject [NAMEDATALEN ];
301
+
302
+ if (!MyProcPort -> ssl_in_use || !MyProcPort -> peer_cert_valid )
303
+ PG_RETURN_NULL ();
304
+
305
+ be_tls_get_peer_subject_name (MyProcPort , subject , NAMEDATALEN );
306
+
307
+ if (!* subject )
362
308
PG_RETURN_NULL ();
363
- return X509_NAME_to_text (X509_get_subject_name (MyProcPort -> peer ));
309
+
310
+ PG_RETURN_TEXT_P (cstring_to_text (subject ));
364
311
}
365
312
366
313
@@ -377,9 +324,17 @@ PG_FUNCTION_INFO_V1(ssl_issuer_dn);
377
324
Datum
378
325
ssl_issuer_dn (PG_FUNCTION_ARGS )
379
326
{
380
- if (!(MyProcPort -> peer ))
327
+ char issuer [NAMEDATALEN ];
328
+
329
+ if (!MyProcPort -> ssl_in_use || !MyProcPort -> peer_cert_valid )
381
330
PG_RETURN_NULL ();
382
- return X509_NAME_to_text (X509_get_issuer_name (MyProcPort -> peer ));
331
+
332
+ be_tls_get_peer_issuer_name (MyProcPort , issuer , NAMEDATALEN );
333
+
334
+ if (!* issuer )
335
+ PG_RETURN_NULL ();
336
+
337
+ PG_RETURN_TEXT_P (cstring_to_text (issuer ));
383
338
}
384
339
385
340
0 commit comments