PostgreSQL Source Code  git master
crypt.h File Reference
Include dependency graph for crypt.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef enum PasswordType PasswordType
 

Enumerations

enum  PasswordType { PASSWORD_TYPE_PLAINTEXT = 0 , PASSWORD_TYPE_MD5 , PASSWORD_TYPE_SCRAM_SHA_256 }
 

Functions

PasswordType get_password_type (const char *shadow_pass)
 
char * encrypt_password (PasswordType target_type, const char *role, const char *password)
 
char * get_role_password (const char *role, const char **logdetail)
 
int md5_crypt_verify (const char *role, const char *shadow_pass, const char *client_pass, const char *md5_salt, int md5_salt_len, const char **logdetail)
 
int plain_crypt_verify (const char *role, const char *shadow_pass, const char *client_pass, const char **logdetail)
 

Typedef Documentation

◆ PasswordType

typedef enum PasswordType PasswordType

Enumeration Type Documentation

◆ PasswordType

Enumerator
PASSWORD_TYPE_PLAINTEXT 
PASSWORD_TYPE_MD5 
PASSWORD_TYPE_SCRAM_SHA_256 

Definition at line 27 of file crypt.h.

28 {
32 } PasswordType;
PasswordType
Definition: crypt.h:28
@ PASSWORD_TYPE_PLAINTEXT
Definition: crypt.h:29
@ PASSWORD_TYPE_SCRAM_SHA_256
Definition: crypt.h:31
@ PASSWORD_TYPE_MD5
Definition: crypt.h:30

Function Documentation

◆ encrypt_password()

char* encrypt_password ( PasswordType  target_type,
const char *  role,
const char *  password 
)

Definition at line 114 of file crypt.c.

116 {
117  PasswordType guessed_type = get_password_type(password);
118  char *encrypted_password;
119  const char *errstr = NULL;
120 
121  if (guessed_type != PASSWORD_TYPE_PLAINTEXT)
122  {
123  /*
124  * Cannot convert an already-encrypted password from one format to
125  * another, so return it as it is.
126  */
127  return pstrdup(password);
128  }
129 
130  switch (target_type)
131  {
132  case PASSWORD_TYPE_MD5:
133  encrypted_password = palloc(MD5_PASSWD_LEN + 1);
134 
135  if (!pg_md5_encrypt(password, role, strlen(role),
136  encrypted_password, &errstr))
137  elog(ERROR, "password encryption failed: %s", errstr);
138  return encrypted_password;
139 
142 
144  elog(ERROR, "cannot encrypt password with 'plaintext'");
145  }
146 
147  /*
148  * This shouldn't happen, because the above switch statements should
149  * handle every combination of source and target password types.
150  */
151  elog(ERROR, "cannot encrypt password to requested type");
152  return NULL; /* keep compiler quiet */
153 }
char * pg_be_scram_build_secret(const char *password)
Definition: auth-scram.c:462
PasswordType get_password_type(const char *shadow_pass)
Definition: crypt.c:89
#define ERROR
Definition: elog.h:35
char * pstrdup(const char *in)
Definition: mcxt.c:1483
void * palloc(Size size)
Definition: mcxt.c:1199
#define MD5_PASSWD_LEN
Definition: md5.h:26
bool pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len, char *buf, const char **errstr)
Definition: md5_common.c:144
static char * password
Definition: streamutil.c:53

References elog(), ERROR, get_password_type(), MD5_PASSWD_LEN, palloc(), password, PASSWORD_TYPE_MD5, PASSWORD_TYPE_PLAINTEXT, PASSWORD_TYPE_SCRAM_SHA_256, pg_be_scram_build_secret(), pg_md5_encrypt(), and pstrdup().

Referenced by AlterRole(), and CreateRole().

◆ get_password_type()

PasswordType get_password_type ( const char *  shadow_pass)

Definition at line 89 of file crypt.c.

90 {
91  char *encoded_salt;
92  int iterations;
93  uint8 stored_key[SCRAM_KEY_LEN];
94  uint8 server_key[SCRAM_KEY_LEN];
95 
96  if (strncmp(shadow_pass, "md5", 3) == 0 &&
97  strlen(shadow_pass) == MD5_PASSWD_LEN &&
98  strspn(shadow_pass + 3, MD5_PASSWD_CHARSET) == MD5_PASSWD_LEN - 3)
99  return PASSWORD_TYPE_MD5;
100  if (parse_scram_secret(shadow_pass, &iterations, &encoded_salt,
101  stored_key, server_key))
104 }
bool parse_scram_secret(const char *secret, int *iterations, char **salt, uint8 *stored_key, uint8 *server_key)
Definition: auth-scram.c:574
unsigned char uint8
Definition: c.h:440
#define MD5_PASSWD_CHARSET
Definition: md5.h:25
#define SCRAM_KEY_LEN
Definition: scram-common.h:24

References MD5_PASSWD_CHARSET, MD5_PASSWD_LEN, parse_scram_secret(), PASSWORD_TYPE_MD5, PASSWORD_TYPE_PLAINTEXT, PASSWORD_TYPE_SCRAM_SHA_256, and SCRAM_KEY_LEN.

Referenced by AlterRole(), CheckPWChallengeAuth(), CreateRole(), encrypt_password(), md5_crypt_verify(), plain_crypt_verify(), RenameRole(), and scram_init().

◆ get_role_password()

char* get_role_password ( const char *  role,
const char **  logdetail 
)

Definition at line 37 of file crypt.c.

38 {
39  TimestampTz vuntil = 0;
40  HeapTuple roleTup;
41  Datum datum;
42  bool isnull;
43  char *shadow_pass;
44 
45  /* Get role info from pg_authid */
46  roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(role));
47  if (!HeapTupleIsValid(roleTup))
48  {
49  *logdetail = psprintf(_("Role \"%s\" does not exist."),
50  role);
51  return NULL; /* no such user */
52  }
53 
54  datum = SysCacheGetAttr(AUTHNAME, roleTup,
55  Anum_pg_authid_rolpassword, &isnull);
56  if (isnull)
57  {
58  ReleaseSysCache(roleTup);
59  *logdetail = psprintf(_("User \"%s\" has no password assigned."),
60  role);
61  return NULL; /* user has no password */
62  }
63  shadow_pass = TextDatumGetCString(datum);
64 
65  datum = SysCacheGetAttr(AUTHNAME, roleTup,
66  Anum_pg_authid_rolvaliduntil, &isnull);
67  if (!isnull)
68  vuntil = DatumGetTimestampTz(datum);
69 
70  ReleaseSysCache(roleTup);
71 
72  /*
73  * Password OK, but check to be sure we are not past rolvaliduntil
74  */
75  if (!isnull && vuntil < GetCurrentTimestamp())
76  {
77  *logdetail = psprintf(_("User \"%s\" has an expired password."),
78  role);
79  return NULL;
80  }
81 
82  return shadow_pass;
83 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1573
#define TextDatumGetCString(d)
Definition: builtins.h:86
int64 TimestampTz
Definition: timestamp.h:39
#define _(x)
Definition: elog.c:90
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
uintptr_t Datum
Definition: postgres.h:412
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1173
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1434
@ AUTHNAME
Definition: syscache.h:44
static TimestampTz DatumGetTimestampTz(Datum X)
Definition: timestamp.h:34

References _, AUTHNAME, DatumGetTimestampTz(), GetCurrentTimestamp(), HeapTupleIsValid, PointerGetDatum(), psprintf(), ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), and TextDatumGetCString.

Referenced by CheckPasswordAuth(), and CheckPWChallengeAuth().

◆ md5_crypt_verify()

int md5_crypt_verify ( const char *  role,
const char *  shadow_pass,
const char *  client_pass,
const char *  md5_salt,
int  md5_salt_len,
const char **  logdetail 
)

Definition at line 167 of file crypt.c.

171 {
172  int retval;
173  char crypt_pwd[MD5_PASSWD_LEN + 1];
174  const char *errstr = NULL;
175 
176  Assert(md5_salt_len > 0);
177 
178  if (get_password_type(shadow_pass) != PASSWORD_TYPE_MD5)
179  {
180  /* incompatible password hash format. */
181  *logdetail = psprintf(_("User \"%s\" has a password that cannot be used with MD5 authentication."),
182  role);
183  return STATUS_ERROR;
184  }
185 
186  /*
187  * Compute the correct answer for the MD5 challenge.
188  */
189  /* stored password already encrypted, only do salt */
190  if (!pg_md5_encrypt(shadow_pass + strlen("md5"),
191  md5_salt, md5_salt_len,
192  crypt_pwd, &errstr))
193  {
194  *logdetail = errstr;
195  return STATUS_ERROR;
196  }
197 
198  if (strcmp(client_pass, crypt_pwd) == 0)
199  retval = STATUS_OK;
200  else
201  {
202  *logdetail = psprintf(_("Password does not match for user \"%s\"."),
203  role);
204  retval = STATUS_ERROR;
205  }
206 
207  return retval;
208 }
#define STATUS_OK
Definition: c.h:1108
#define STATUS_ERROR
Definition: c.h:1109
Assert(fmt[strlen(fmt) - 1] !='\n')

References _, Assert(), get_password_type(), MD5_PASSWD_LEN, PASSWORD_TYPE_MD5, pg_md5_encrypt(), psprintf(), STATUS_ERROR, and STATUS_OK.

Referenced by CheckMD5Auth().

◆ plain_crypt_verify()

int plain_crypt_verify ( const char *  role,
const char *  shadow_pass,
const char *  client_pass,
const char **  logdetail 
)

Definition at line 221 of file crypt.c.

224 {
225  char crypt_client_pass[MD5_PASSWD_LEN + 1];
226  const char *errstr = NULL;
227 
228  /*
229  * Client sent password in plaintext. If we have an MD5 hash stored, hash
230  * the password the client sent, and compare the hashes. Otherwise
231  * compare the plaintext passwords directly.
232  */
233  switch (get_password_type(shadow_pass))
234  {
237  client_pass,
238  shadow_pass))
239  {
240  return STATUS_OK;
241  }
242  else
243  {
244  *logdetail = psprintf(_("Password does not match for user \"%s\"."),
245  role);
246  return STATUS_ERROR;
247  }
248  break;
249 
250  case PASSWORD_TYPE_MD5:
251  if (!pg_md5_encrypt(client_pass,
252  role,
253  strlen(role),
254  crypt_client_pass,
255  &errstr))
256  {
257  *logdetail = errstr;
258  return STATUS_ERROR;
259  }
260  if (strcmp(crypt_client_pass, shadow_pass) == 0)
261  return STATUS_OK;
262  else
263  {
264  *logdetail = psprintf(_("Password does not match for user \"%s\"."),
265  role);
266  return STATUS_ERROR;
267  }
268  break;
269 
271 
272  /*
273  * We never store passwords in plaintext, so this shouldn't
274  * happen.
275  */
276  break;
277  }
278 
279  /*
280  * This shouldn't happen. Plain "password" authentication is possible
281  * with any kind of stored password hash.
282  */
283  *logdetail = psprintf(_("Password of user \"%s\" is in unrecognized format."),
284  role);
285  return STATUS_ERROR;
286 }
bool scram_verify_plain_password(const char *username, const char *password, const char *secret)
Definition: auth-scram.c:501

References _, get_password_type(), MD5_PASSWD_LEN, PASSWORD_TYPE_MD5, PASSWORD_TYPE_PLAINTEXT, PASSWORD_TYPE_SCRAM_SHA_256, pg_md5_encrypt(), psprintf(), scram_verify_plain_password(), STATUS_ERROR, and STATUS_OK.

Referenced by AlterRole(), check_password(), CheckPasswordAuth(), and CreateRole().