LCOV - code coverage report
Current view: top level - src/backend/libpq - crypt.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 80.2 % 106 85
Test Date: 2026-07-03 19:57:34 Functions: 100.0 % 5 5
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 65.7 % 70 46

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * crypt.c
       4                 :             :  *    Functions for dealing with encrypted passwords stored in
       5                 :             :  *    pg_authid.rolpassword.
       6                 :             :  *
       7                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       8                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       9                 :             :  *
      10                 :             :  * src/backend/libpq/crypt.c
      11                 :             :  *
      12                 :             :  *-------------------------------------------------------------------------
      13                 :             :  */
      14                 :             : #include "postgres.h"
      15                 :             : 
      16                 :             : #include <unistd.h>
      17                 :             : 
      18                 :             : #include "catalog/pg_authid.h"
      19                 :             : #include "common/md5.h"
      20                 :             : #include "common/scram-common.h"
      21                 :             : #include "libpq/crypt.h"
      22                 :             : #include "libpq/scram.h"
      23                 :             : #include "miscadmin.h"
      24                 :             : #include "utils/builtins.h"
      25                 :             : #include "utils/memutils.h"
      26                 :             : #include "utils/syscache.h"
      27                 :             : #include "utils/timestamp.h"
      28                 :             : 
      29                 :             : /* Threshold for password expiration warnings. */
      30                 :             : int         password_expiration_warning_threshold = 604800;
      31                 :             : 
      32                 :             : /* Enables deprecation warnings for MD5 passwords. */
      33                 :             : bool        md5_password_warnings = true;
      34                 :             : 
      35                 :             : /*
      36                 :             :  * Fetch stored password for a user, for authentication.
      37                 :             :  *
      38                 :             :  * On error, returns NULL, and stores a palloc'd string describing the reason,
      39                 :             :  * for the postmaster log, in *logdetail.  The error reason should *not* be
      40                 :             :  * sent to the client, to avoid giving away user information!
      41                 :             :  */
      42                 :             : char *
      43                 :          91 : get_role_password(const char *role, const char **logdetail)
      44                 :             : {
      45                 :          91 :     TimestampTz vuntil = 0;
      46                 :             :     HeapTuple   roleTup;
      47                 :             :     Datum       datum;
      48                 :             :     bool        isnull;
      49                 :             :     char       *shadow_pass;
      50                 :             : 
      51                 :             :     /* Get role info from pg_authid */
      52                 :          91 :     roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(role));
      53         [ -  + ]:          91 :     if (!HeapTupleIsValid(roleTup))
      54                 :             :     {
      55                 :           0 :         *logdetail = psprintf(_("Role \"%s\" does not exist."),
      56                 :             :                               role);
      57                 :           0 :         return NULL;            /* no such user */
      58                 :             :     }
      59                 :             : 
      60                 :          91 :     datum = SysCacheGetAttr(AUTHNAME, roleTup,
      61                 :             :                             Anum_pg_authid_rolpassword, &isnull);
      62         [ -  + ]:          91 :     if (isnull)
      63                 :             :     {
      64                 :           0 :         ReleaseSysCache(roleTup);
      65                 :           0 :         *logdetail = psprintf(_("User \"%s\" has no password assigned."),
      66                 :             :                               role);
      67                 :           0 :         return NULL;            /* user has no password */
      68                 :             :     }
      69                 :          91 :     shadow_pass = TextDatumGetCString(datum);
      70                 :             : 
      71                 :          91 :     datum = SysCacheGetAttr(AUTHNAME, roleTup,
      72                 :             :                             Anum_pg_authid_rolvaliduntil, &isnull);
      73         [ +  + ]:          91 :     if (!isnull)
      74                 :           3 :         vuntil = DatumGetTimestampTz(datum);
      75                 :             : 
      76                 :          91 :     ReleaseSysCache(roleTup);
      77                 :             : 
      78                 :             :     /*
      79                 :             :      * Password OK, but check to be sure we are not past rolvaliduntil or
      80                 :             :      * password_expiration_warning_threshold.
      81                 :             :      */
      82         [ +  + ]:          91 :     if (!isnull)
      83                 :             :     {
      84                 :           3 :         TimestampTz now = GetCurrentTimestamp();
      85                 :           3 :         uint64      expire_time = TimestampDifferenceMicroseconds(now, vuntil);
      86                 :             : 
      87                 :             :         /*
      88                 :             :          * If we're past rolvaliduntil, the connection attempt should fail, so
      89                 :             :          * update logdetail and return NULL.
      90                 :             :          */
      91         [ +  + ]:           3 :         if (vuntil < now)
      92                 :             :         {
      93                 :           1 :             *logdetail = psprintf(_("User \"%s\" has an expired password."),
      94                 :             :                                   role);
      95                 :           1 :             return NULL;
      96                 :             :         }
      97                 :             : 
      98                 :             :         /*
      99                 :             :          * If we're past the warning threshold, the connection attempt should
     100                 :             :          * succeed, but we still want to emit a warning.  To do so, we queue
     101                 :             :          * the warning message using StoreConnectionWarning() so that it will
     102                 :             :          * be emitted at the end of InitPostgres(), and we return normally.
     103                 :             :          */
     104         [ +  + ]:           2 :         if (expire_time / USECS_PER_SEC < password_expiration_warning_threshold)
     105                 :             :         {
     106                 :             :             MemoryContext oldcontext;
     107                 :             :             int         days;
     108                 :             :             int         hours;
     109                 :             :             int         minutes;
     110                 :             :             char       *warning;
     111                 :             :             char       *detail;
     112                 :             : 
     113                 :           1 :             oldcontext = MemoryContextSwitchTo(TopMemoryContext);
     114                 :             : 
     115                 :           1 :             days = expire_time / USECS_PER_DAY;
     116                 :           1 :             hours = (expire_time % USECS_PER_DAY) / USECS_PER_HOUR;
     117                 :           1 :             minutes = (expire_time % USECS_PER_HOUR) / USECS_PER_MINUTE;
     118                 :             : 
     119                 :           1 :             warning = pstrdup(_("role password will expire soon"));
     120                 :             : 
     121         [ +  - ]:           1 :             if (days > 0)
     122                 :           1 :                 detail = psprintf(ngettext("The password for role \"%s\" will expire in %d day.",
     123                 :             :                                            "The password for role \"%s\" will expire in %d days.",
     124                 :             :                                            days),
     125                 :             :                                   role, days);
     126         [ #  # ]:           0 :             else if (hours > 0)
     127                 :           0 :                 detail = psprintf(ngettext("The password for role \"%s\" will expire in %d hour.",
     128                 :             :                                            "The password for role \"%s\" will expire in %d hours.",
     129                 :             :                                            hours),
     130                 :             :                                   role, hours);
     131         [ #  # ]:           0 :             else if (minutes > 0)
     132                 :           0 :                 detail = psprintf(ngettext("The password for role \"%s\" will expire in %d minute.",
     133                 :             :                                            "The password for role \"%s\" will expire in %d minutes.",
     134                 :             :                                            minutes),
     135                 :             :                                   role, minutes);
     136                 :             :             else
     137                 :           0 :                 detail = psprintf(_("The password for role \"%s\" will expire in less than 1 minute."),
     138                 :             :                                   role);
     139                 :             : 
     140                 :           1 :             StoreConnectionWarning(warning, detail, NULL);
     141                 :             : 
     142                 :           1 :             MemoryContextSwitchTo(oldcontext);
     143                 :             :         }
     144                 :             :     }
     145                 :             : 
     146                 :          90 :     return shadow_pass;
     147                 :             : }
     148                 :             : 
     149                 :             : /*
     150                 :             :  * What kind of a password type is 'shadow_pass'?
     151                 :             :  */
     152                 :             : PasswordType
     153                 :         501 : get_password_type(const char *shadow_pass)
     154                 :             : {
     155                 :             :     char       *encoded_salt;
     156                 :             :     int         iterations;
     157                 :         501 :     int         key_length = 0;
     158                 :             :     pg_cryptohash_type hash_type;
     159                 :             :     uint8       stored_key[SCRAM_MAX_KEY_LEN];
     160                 :             :     uint8       server_key[SCRAM_MAX_KEY_LEN];
     161                 :             : 
     162         [ +  + ]:         501 :     if (strncmp(shadow_pass, "md5", 3) == 0 &&
     163         [ +  + ]:          81 :         strlen(shadow_pass) == MD5_PASSWD_LEN &&
     164         [ +  + ]:          73 :         strspn(shadow_pass + 3, MD5_PASSWD_CHARSET) == MD5_PASSWD_LEN - 3)
     165                 :          65 :         return PASSWORD_TYPE_MD5;
     166         [ +  + ]:         436 :     if (parse_scram_secret(shadow_pass, &iterations, &hash_type, &key_length,
     167                 :             :                            &encoded_salt, stored_key, server_key))
     168                 :         271 :         return PASSWORD_TYPE_SCRAM_SHA_256;
     169                 :         165 :     return PASSWORD_TYPE_PLAINTEXT;
     170                 :             : }
     171                 :             : 
     172                 :             : /*
     173                 :             :  * Given a user-supplied password, convert it into a secret of
     174                 :             :  * 'target_type' kind.
     175                 :             :  *
     176                 :             :  * If the password is already in encrypted form, we cannot reverse the
     177                 :             :  * hash, so it is stored as it is regardless of the requested type.
     178                 :             :  */
     179                 :             : char *
     180                 :         108 : encrypt_password(PasswordType target_type, const char *role,
     181                 :             :                  const char *password)
     182                 :             : {
     183                 :         108 :     PasswordType guessed_type = get_password_type(password);
     184                 :         108 :     char       *encrypted_password = NULL;
     185                 :         108 :     const char *errstr = NULL;
     186                 :             : 
     187         [ +  + ]:         108 :     if (guessed_type != PASSWORD_TYPE_PLAINTEXT)
     188                 :             :     {
     189                 :             :         /*
     190                 :             :          * Cannot convert an already-encrypted password from one format to
     191                 :             :          * another, so return it as it is.
     192                 :             :          */
     193                 :          28 :         encrypted_password = pstrdup(password);
     194                 :             :     }
     195                 :             :     else
     196                 :             :     {
     197   [ +  +  -  - ]:          80 :         switch (target_type)
     198                 :             :         {
     199                 :          15 :             case PASSWORD_TYPE_MD5:
     200                 :          15 :                 encrypted_password = palloc(MD5_PASSWD_LEN + 1);
     201                 :             : 
     202         [ -  + ]:          15 :                 if (!pg_md5_encrypt(password, (const uint8 *) role, strlen(role),
     203                 :             :                                     encrypted_password, &errstr))
     204         [ #  # ]:           0 :                     elog(ERROR, "password encryption failed: %s", errstr);
     205                 :          15 :                 break;
     206                 :             : 
     207                 :          65 :             case PASSWORD_TYPE_SCRAM_SHA_256:
     208                 :          65 :                 encrypted_password = pg_be_scram_build_secret(password);
     209                 :          65 :                 break;
     210                 :             : 
     211                 :           0 :             case PASSWORD_TYPE_PLAINTEXT:
     212         [ #  # ]:           0 :                 elog(ERROR, "cannot encrypt password with 'plaintext'");
     213                 :             :                 break;
     214                 :             :         }
     215                 :             :     }
     216                 :             : 
     217                 :             :     Assert(encrypted_password);
     218                 :             : 
     219                 :             :     /*
     220                 :             :      * Valid password hashes may be very long, but we don't want to store
     221                 :             :      * anything that might need out-of-line storage, since de-TOASTing won't
     222                 :             :      * work during authentication because we haven't selected a database yet
     223                 :             :      * and cannot read pg_class. 512 bytes should be more than enough for all
     224                 :             :      * practical use, so fail for anything longer.
     225                 :             :      */
     226         [ +  - ]:         108 :     if (encrypted_password &&   /* keep compiler quiet */
     227         [ +  + ]:         108 :         strlen(encrypted_password) > MAX_ENCRYPTED_PASSWORD_LEN)
     228                 :             :     {
     229                 :             :         /*
     230                 :             :          * We don't expect any of our own hashing routines to produce hashes
     231                 :             :          * that are too long.
     232                 :             :          */
     233                 :             :         Assert(guessed_type != PASSWORD_TYPE_PLAINTEXT);
     234                 :             : 
     235         [ +  - ]:           8 :         ereport(ERROR,
     236                 :             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     237                 :             :                  errmsg("encrypted password is too long"),
     238                 :             :                  errdetail("Encrypted passwords must be no longer than %d bytes.",
     239                 :             :                            MAX_ENCRYPTED_PASSWORD_LEN)));
     240                 :             :     }
     241                 :             : 
     242   [ +  +  +  + ]:         197 :     if (md5_password_warnings &&
     243                 :          97 :         get_password_type(encrypted_password) == PASSWORD_TYPE_MD5)
     244         [ +  - ]:          23 :         ereport(WARNING,
     245                 :             :                 (errcode(ERRCODE_WARNING_DEPRECATED_FEATURE),
     246                 :             :                  errmsg("setting an MD5-encrypted password"),
     247                 :             :                  errdetail("MD5 password support is deprecated and will be removed in a future release of PostgreSQL."),
     248                 :             :                  errhint("Refer to the PostgreSQL documentation for details about migrating to another password type.")));
     249                 :             : 
     250                 :         100 :     return encrypted_password;
     251                 :             : }
     252                 :             : 
     253                 :             : /*
     254                 :             :  * Check MD5 authentication response, and return STATUS_OK or STATUS_ERROR.
     255                 :             :  *
     256                 :             :  * 'shadow_pass' is the user's correct password or password hash, as stored
     257                 :             :  * in pg_authid.rolpassword.
     258                 :             :  * 'client_pass' is the response given by the remote user to the MD5 challenge.
     259                 :             :  * 'md5_salt' is the salt used in the MD5 authentication challenge.
     260                 :             :  *
     261                 :             :  * In the error case, save a string at *logdetail that will be sent to the
     262                 :             :  * postmaster log (but not the client).
     263                 :             :  */
     264                 :             : int
     265                 :           2 : md5_crypt_verify(const char *role, const char *shadow_pass,
     266                 :             :                  const char *client_pass,
     267                 :             :                  const uint8 *md5_salt, int md5_salt_len,
     268                 :             :                  const char **logdetail)
     269                 :             : {
     270                 :             :     int         retval;
     271                 :             :     char        crypt_pwd[MD5_PASSWD_LEN + 1];
     272                 :           2 :     const char *errstr = NULL;
     273                 :             : 
     274                 :             :     Assert(md5_salt_len > 0);
     275                 :             : 
     276         [ -  + ]:           2 :     if (get_password_type(shadow_pass) != PASSWORD_TYPE_MD5)
     277                 :             :     {
     278                 :             :         /* incompatible password hash format. */
     279                 :           0 :         *logdetail = psprintf(_("User \"%s\" has a password that cannot be used with MD5 authentication."),
     280                 :             :                               role);
     281                 :           0 :         return STATUS_ERROR;
     282                 :             :     }
     283                 :             : 
     284                 :             :     /*
     285                 :             :      * Compute the correct answer for the MD5 challenge.
     286                 :             :      */
     287                 :             :     /* stored password already encrypted, only do salt */
     288         [ -  + ]:           2 :     if (!pg_md5_encrypt(shadow_pass + strlen("md5"),
     289                 :             :                         md5_salt, md5_salt_len,
     290                 :             :                         crypt_pwd, &errstr))
     291                 :             :     {
     292                 :           0 :         *logdetail = errstr;
     293                 :           0 :         return STATUS_ERROR;
     294                 :             :     }
     295                 :             : 
     296   [ +  -  +  - ]:           4 :     if (strlen(client_pass) == strlen(crypt_pwd) &&
     297                 :           2 :         timingsafe_bcmp(client_pass, crypt_pwd, strlen(crypt_pwd)) == 0)
     298                 :           2 :         retval = STATUS_OK;
     299                 :             :     else
     300                 :             :     {
     301                 :           0 :         *logdetail = psprintf(_("Password does not match for user \"%s\"."),
     302                 :             :                               role);
     303                 :           0 :         retval = STATUS_ERROR;
     304                 :             :     }
     305                 :             : 
     306                 :           2 :     return retval;
     307                 :             : }
     308                 :             : 
     309                 :             : /*
     310                 :             :  * Check given password for given user, and return STATUS_OK or STATUS_ERROR.
     311                 :             :  *
     312                 :             :  * 'shadow_pass' is the user's correct password hash, as stored in
     313                 :             :  * pg_authid.rolpassword.
     314                 :             :  * 'client_pass' is the password given by the remote user.
     315                 :             :  *
     316                 :             :  * In the error case, store a string at *logdetail that will be sent to the
     317                 :             :  * postmaster log (but not the client).
     318                 :             :  */
     319                 :             : int
     320                 :         129 : plain_crypt_verify(const char *role, const char *shadow_pass,
     321                 :             :                    const char *client_pass,
     322                 :             :                    const char **logdetail)
     323                 :             : {
     324                 :             :     char        crypt_client_pass[MD5_PASSWD_LEN + 1];
     325                 :         129 :     const char *errstr = NULL;
     326                 :             : 
     327                 :             :     /*
     328                 :             :      * Client sent password in plaintext.  If we have an MD5 hash stored, hash
     329                 :             :      * the password the client sent, and compare the hashes.  Otherwise
     330                 :             :      * compare the plaintext passwords directly.
     331                 :             :      */
     332   [ +  +  +  - ]:         129 :     switch (get_password_type(shadow_pass))
     333                 :             :     {
     334                 :          32 :         case PASSWORD_TYPE_SCRAM_SHA_256:
     335         [ +  + ]:          32 :             if (scram_verify_plain_password(role,
     336                 :             :                                             client_pass,
     337                 :             :                                             shadow_pass))
     338                 :             :             {
     339                 :          13 :                 return STATUS_OK;
     340                 :             :             }
     341                 :             :             else
     342                 :             :             {
     343                 :          19 :                 *logdetail = psprintf(_("Password does not match for user \"%s\"."),
     344                 :             :                                       role);
     345                 :          19 :                 return STATUS_ERROR;
     346                 :             :             }
     347                 :             :             break;
     348                 :             : 
     349                 :          17 :         case PASSWORD_TYPE_MD5:
     350         [ -  + ]:          17 :             if (!pg_md5_encrypt(client_pass,
     351                 :             :                                 (const uint8 *) role,
     352                 :             :                                 strlen(role),
     353                 :             :                                 crypt_client_pass,
     354                 :             :                                 &errstr))
     355                 :             :             {
     356                 :           0 :                 *logdetail = errstr;
     357                 :           0 :                 return STATUS_ERROR;
     358                 :             :             }
     359   [ +  -  +  + ]:          34 :             if (strlen(crypt_client_pass) == strlen(shadow_pass) &&
     360                 :          17 :                 timingsafe_bcmp(crypt_client_pass, shadow_pass, strlen(shadow_pass)) == 0)
     361                 :           7 :                 return STATUS_OK;
     362                 :             :             else
     363                 :             :             {
     364                 :          10 :                 *logdetail = psprintf(_("Password does not match for user \"%s\"."),
     365                 :             :                                       role);
     366                 :          10 :                 return STATUS_ERROR;
     367                 :             :             }
     368                 :             :             break;
     369                 :             : 
     370                 :          80 :         case PASSWORD_TYPE_PLAINTEXT:
     371                 :             : 
     372                 :             :             /*
     373                 :             :              * We never store passwords in plaintext, so this shouldn't
     374                 :             :              * happen.
     375                 :             :              */
     376                 :          80 :             break;
     377                 :             :     }
     378                 :             : 
     379                 :             :     /*
     380                 :             :      * This shouldn't happen.  Plain "password" authentication is possible
     381                 :             :      * with any kind of stored password hash.
     382                 :             :      */
     383                 :          80 :     *logdetail = psprintf(_("Password of user \"%s\" is in unrecognized format."),
     384                 :             :                           role);
     385                 :          80 :     return STATUS_ERROR;
     386                 :             : }
        

Generated by: LCOV version 2.0-1