LCOV - code coverage report
Current view: top level - src/test/modules/ssl_passphrase_callback - ssl_passphrase_func.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 100.0 % 21 21
Test Date: 2026-07-03 19:57:34 Functions: 100.0 % 4 4
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 83.3 % 24 20

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * ssl_passphrase_func.c
       4                 :             :  *
       5                 :             :  * Loadable PostgreSQL module fetch an ssl passphrase for the server cert.
       6                 :             :  * instead of calling an external program. This implementation just hands
       7                 :             :  * back the configured password rot13'd.
       8                 :             :  *
       9                 :             :  *-------------------------------------------------------------------------
      10                 :             :  */
      11                 :             : 
      12                 :             : #include "postgres.h"
      13                 :             : 
      14                 :             : #include <float.h>
      15                 :             : #include <stdio.h>
      16                 :             : 
      17                 :             : #include "libpq/libpq.h"
      18                 :             : #include "libpq/libpq-be.h"
      19                 :             : #include "utils/guc.h"
      20                 :             : 
      21                 :           4 : PG_MODULE_MAGIC;
      22                 :             : 
      23                 :             : static char *ssl_passphrase = NULL;
      24                 :             : 
      25                 :             : /* callback function */
      26                 :             : static int  rot13_passphrase(char *buf, int size, int rwflag, void *userdata);
      27                 :             : 
      28                 :             : /* hook function to set the callback */
      29                 :             : static void set_rot13(SSL_CTX *context, bool isServerStart);
      30                 :             : 
      31                 :             : /*
      32                 :             :  * Module load callback
      33                 :             :  */
      34                 :             : void
      35                 :           4 : _PG_init(void)
      36                 :             : {
      37                 :             :     /* Define custom GUC variable. */
      38                 :           4 :     DefineCustomStringVariable("ssl_passphrase.passphrase",
      39                 :             :                                "passphrase before transformation",
      40                 :             :                                NULL,
      41                 :             :                                &ssl_passphrase,
      42                 :             :                                NULL,
      43                 :             :                                PGC_SIGHUP,
      44                 :             :                                0,   /* no flags required */
      45                 :             :                                NULL,
      46                 :             :                                NULL,
      47                 :             :                                NULL);
      48                 :             : 
      49                 :           4 :     MarkGUCPrefixReserved("ssl_passphrase");
      50                 :             : 
      51         [ +  - ]:           4 :     if (ssl_passphrase)
      52                 :           4 :         openssl_tls_init_hook = set_rot13;
      53                 :           4 : }
      54                 :             : 
      55                 :             : static void
      56                 :           3 : set_rot13(SSL_CTX *context, bool isServerStart)
      57                 :             : {
      58                 :             :     /* warn if the user has set ssl_passphrase_command */
      59         [ +  + ]:           3 :     if (ssl_passphrase_command[0])
      60         [ +  - ]:           2 :         ereport(WARNING,
      61                 :             :                 (errmsg("\"ssl_passphrase_command\" setting ignored by ssl_passphrase_func module")));
      62                 :             : 
      63                 :           3 :     SSL_CTX_set_default_passwd_cb(context, rot13_passphrase);
      64                 :           3 : }
      65                 :             : 
      66                 :             : static int
      67                 :           3 : rot13_passphrase(char *buf, int size, int rwflag, void *userdata)
      68                 :             : {
      69                 :             : 
      70                 :             :     Assert(ssl_passphrase != NULL);
      71                 :           3 :     strlcpy(buf, ssl_passphrase, size);
      72         [ +  + ]:          23 :     for (char *p = buf; *p; p++)
      73                 :             :     {
      74                 :          20 :         char        c = *p;
      75                 :             : 
      76   [ +  +  +  +  :          20 :         if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
             +  +  +  + ]
      77                 :          10 :             *p = c + 13;
      78   [ +  +  -  +  :          10 :         else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
             +  +  +  - ]
      79                 :           8 :             *p = c - 13;
      80                 :             :     }
      81                 :             : 
      82                 :           3 :     return strlen(buf);
      83                 :             : }
        

Generated by: LCOV version 2.0-1