PostgreSQL Source Code  git master
saslprep.c File Reference
#include "postgres.h"
#include "common/saslprep.h"
#include "common/unicode_norm.h"
#include "mb/pg_wchar.h"
Include dependency graph for saslprep.c:

Go to the source code of this file.

Macros

#define MAX_PASSWORD_LENGTH   1024
 
#define STRDUP(s)   pstrdup(s)
 
#define ALLOC(size)   palloc(size)
 
#define FREE(size)   pfree(size)
 
#define IS_CODE_IN_TABLE(code, map)   is_code_in_table(code, map, lengthof(map))
 

Functions

static int codepoint_range_cmp (const void *a, const void *b)
 
static bool is_code_in_table (pg_wchar code, const pg_wchar *map, int mapsize)
 
static int pg_utf8_string_len (const char *source)
 
static bool pg_is_ascii_string (const char *p)
 
pg_saslprep_rc pg_saslprep (const char *input, char **output)
 

Variables

static const pg_wchar non_ascii_space_ranges []
 
static const pg_wchar commonly_mapped_to_nothing_ranges []
 
static const pg_wchar prohibited_output_ranges []
 
static const pg_wchar unassigned_codepoint_ranges []
 
static const pg_wchar RandALCat_codepoint_ranges []
 
static const pg_wchar LCat_codepoint_ranges []
 

Macro Definition Documentation

◆ ALLOC

#define ALLOC (   size)    palloc(size)

Definition at line 50 of file saslprep.c.

Referenced by pg_saslprep().

◆ FREE

◆ IS_CODE_IN_TABLE

#define IS_CODE_IN_TABLE (   code,
  map 
)    is_code_in_table(code, map, lengthof(map))

Definition at line 977 of file saslprep.c.

Referenced by pg_saslprep().

◆ MAX_PASSWORD_LENGTH

#define MAX_PASSWORD_LENGTH   1024

Definition at line 42 of file saslprep.c.

Referenced by pg_saslprep().

◆ STRDUP

#define STRDUP (   s)    pstrdup(s)

Definition at line 49 of file saslprep.c.

Referenced by pg_saslprep().

Function Documentation

◆ codepoint_range_cmp()

static int codepoint_range_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 980 of file saslprep.c.

References sort-test::key, and range().

Referenced by is_code_in_table().

981 {
982  const pg_wchar *key = (const pg_wchar *) a;
983  const pg_wchar *range = (const pg_wchar *) b;
984 
985  if (*key < range[0])
986  return -1; /* less than lower bound */
987  if (*key > range[1])
988  return 1; /* greater than upper bound */
989 
990  return 0; /* within range */
991 }
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:416
unsigned int pg_wchar
Definition: mbprint.c:31

◆ is_code_in_table()

static bool is_code_in_table ( pg_wchar  code,
const pg_wchar map,
int  mapsize 
)
static

Definition at line 994 of file saslprep.c.

References Assert, and codepoint_range_cmp().

995 {
996  Assert(mapsize % 2 == 0);
997 
998  if (code < map[0] || code > map[mapsize - 1])
999  return false;
1000 
1001  if (bsearch(&code, map, mapsize / 2, sizeof(pg_wchar) * 2,
1003  return true;
1004  else
1005  return false;
1006 }
unsigned int pg_wchar
Definition: mbprint.c:31
#define Assert(condition)
Definition: c.h:739
static int codepoint_range_cmp(const void *a, const void *b)
Definition: saslprep.c:980

◆ pg_is_ascii_string()

static bool pg_is_ascii_string ( const char *  p)
static

Definition at line 1038 of file saslprep.c.

References IS_HIGHBIT_SET.

Referenced by pg_saslprep().

1039 {
1040  while (*p)
1041  {
1042  if (IS_HIGHBIT_SET(*p))
1043  return false;
1044  p++;
1045  }
1046  return true;
1047 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1106

◆ pg_saslprep()

pg_saslprep_rc pg_saslprep ( const char *  input,
char **  output 
)

Definition at line 1071 of file saslprep.c.

References ALLOC, Assert, buf, commonly_mapped_to_nothing_ranges, ereport, errcode(), errmsg(), ERROR, FREE, i, IS_CODE_IN_TABLE, LCat_codepoint_ranges, MAX_PASSWORD_LENGTH, non_ascii_space_ranges, pg_is_ascii_string(), pg_utf8_string_len(), pg_utf_mblen(), prohibited_output_ranges, RandALCat_codepoint_ranges, SASLPREP_INVALID_UTF8, SASLPREP_OOM, SASLPREP_PROHIBITED, SASLPREP_SUCCESS, STRDUP, unassigned_codepoint_ranges, unicode_normalize_kc(), unicode_to_utf8(), and utf8_to_unicode().

Referenced by pg_be_scram_build_secret(), pg_fe_scram_build_secret(), pg_fe_scram_init(), and scram_verify_plain_password().

1072 {
1073  pg_wchar *input_chars = NULL;
1074  pg_wchar *output_chars = NULL;
1075  int input_size;
1076  char *result;
1077  int result_size;
1078  int count;
1079  int i;
1080  bool contains_RandALCat;
1081  unsigned char *p;
1082  pg_wchar *wp;
1083 
1084  /* Ensure we return *output as NULL on failure */
1085  *output = NULL;
1086 
1087  /* Check that the password isn't stupendously long */
1088  if (strlen(input) > MAX_PASSWORD_LENGTH)
1089  {
1090 #ifndef FRONTEND
1091  ereport(ERROR,
1092  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1093  errmsg("password too long")));
1094 #else
1095  return SASLPREP_OOM;
1096 #endif
1097  }
1098 
1099  /*
1100  * Quick check if the input is pure ASCII. An ASCII string requires no
1101  * further processing.
1102  */
1103  if (pg_is_ascii_string(input))
1104  {
1105  *output = STRDUP(input);
1106  if (!(*output))
1107  goto oom;
1108  return SASLPREP_SUCCESS;
1109  }
1110 
1111  /*
1112  * Convert the input from UTF-8 to an array of Unicode codepoints.
1113  *
1114  * This also checks that the input is a legal UTF-8 string.
1115  */
1116  input_size = pg_utf8_string_len(input);
1117  if (input_size < 0)
1118  return SASLPREP_INVALID_UTF8;
1119 
1120  input_chars = ALLOC((input_size + 1) * sizeof(pg_wchar));
1121  if (!input_chars)
1122  goto oom;
1123 
1124  p = (unsigned char *) input;
1125  for (i = 0; i < input_size; i++)
1126  {
1127  input_chars[i] = utf8_to_unicode(p);
1128  p += pg_utf_mblen(p);
1129  }
1130  input_chars[i] = (pg_wchar) '\0';
1131 
1132  /*
1133  * The steps below correspond to the steps listed in [RFC3454], Section
1134  * "2. Preparation Overview"
1135  */
1136 
1137  /*
1138  * 1) Map -- For each character in the input, check if it has a mapping
1139  * and, if so, replace it with its mapping.
1140  */
1141  count = 0;
1142  for (i = 0; i < input_size; i++)
1143  {
1144  pg_wchar code = input_chars[i];
1145 
1147  input_chars[count++] = 0x0020;
1149  {
1150  /* map to nothing */
1151  }
1152  else
1153  input_chars[count++] = code;
1154  }
1155  input_chars[count] = (pg_wchar) '\0';
1156  input_size = count;
1157 
1158  if (input_size == 0)
1159  goto prohibited; /* don't allow empty password */
1160 
1161  /*
1162  * 2) Normalize -- Normalize the result of step 1 using Unicode
1163  * normalization.
1164  */
1165  output_chars = unicode_normalize_kc(input_chars);
1166  if (!output_chars)
1167  goto oom;
1168 
1169  /*
1170  * 3) Prohibit -- Check for any characters that are not allowed in the
1171  * output. If any are found, return an error.
1172  */
1173  for (i = 0; i < input_size; i++)
1174  {
1175  pg_wchar code = input_chars[i];
1176 
1178  goto prohibited;
1180  goto prohibited;
1181  }
1182 
1183  /*
1184  * 4) Check bidi -- Possibly check for right-to-left characters, and if
1185  * any are found, make sure that the whole string satisfies the
1186  * requirements for bidirectional strings. If the string does not satisfy
1187  * the requirements for bidirectional strings, return an error.
1188  *
1189  * [RFC3454], Section "6. Bidirectional Characters" explains in more
1190  * detail what that means:
1191  *
1192  * "In any profile that specifies bidirectional character handling, all
1193  * three of the following requirements MUST be met:
1194  *
1195  * 1) The characters in section 5.8 MUST be prohibited.
1196  *
1197  * 2) If a string contains any RandALCat character, the string MUST NOT
1198  * contain any LCat character.
1199  *
1200  * 3) If a string contains any RandALCat character, a RandALCat character
1201  * MUST be the first character of the string, and a RandALCat character
1202  * MUST be the last character of the string."
1203  */
1204  contains_RandALCat = false;
1205  for (i = 0; i < input_size; i++)
1206  {
1207  pg_wchar code = input_chars[i];
1208 
1210  {
1211  contains_RandALCat = true;
1212  break;
1213  }
1214  }
1215 
1216  if (contains_RandALCat)
1217  {
1218  pg_wchar first = input_chars[0];
1219  pg_wchar last = input_chars[input_size - 1];
1220 
1221  for (i = 0; i < input_size; i++)
1222  {
1223  pg_wchar code = input_chars[i];
1224 
1226  goto prohibited;
1227  }
1228 
1231  goto prohibited;
1232  }
1233 
1234  /*
1235  * Finally, convert the result back to UTF-8.
1236  */
1237  result_size = 0;
1238  for (wp = output_chars; *wp; wp++)
1239  {
1240  unsigned char buf[4];
1241 
1242  unicode_to_utf8(*wp, buf);
1243  result_size += pg_utf_mblen(buf);
1244  }
1245 
1246  result = ALLOC(result_size + 1);
1247  if (!result)
1248  goto oom;
1249 
1250  /*
1251  * There are no error exits below here, so the error exit paths don't need
1252  * to worry about possibly freeing "result".
1253  */
1254  p = (unsigned char *) result;
1255  for (wp = output_chars; *wp; wp++)
1256  {
1257  unicode_to_utf8(*wp, p);
1258  p += pg_utf_mblen(p);
1259  }
1260  Assert((char *) p == result + result_size);
1261  *p = '\0';
1262 
1263  FREE(input_chars);
1264  FREE(output_chars);
1265 
1266  *output = result;
1267  return SASLPREP_SUCCESS;
1268 
1269 prohibited:
1270  if (input_chars)
1271  FREE(input_chars);
1272  if (output_chars)
1273  FREE(output_chars);
1274 
1275  return SASLPREP_PROHIBITED;
1276 
1277 oom:
1278  if (input_chars)
1279  FREE(input_chars);
1280  if (output_chars)
1281  FREE(output_chars);
1282 
1283  return SASLPREP_OOM;
1284 }
static const pg_wchar commonly_mapped_to_nothing_ranges[]
Definition: saslprep.c:93
#define ALLOC(size)
Definition: saslprep.c:50
static const pg_wchar prohibited_output_ranges[]
Definition: saslprep.c:128
pg_wchar utf8_to_unicode(const unsigned char *c)
Definition: wchar.c:751
unsigned char * unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
Definition: wchar.c:482
static int pg_utf8_string_len(const char *source)
Definition: saslprep.c:1014
static void output(uint64 loop_count)
static const pg_wchar non_ascii_space_ranges[]
Definition: saslprep.c:78
#define STRDUP(s)
Definition: saslprep.c:49
#define MAX_PASSWORD_LENGTH
Definition: saslprep.c:42
int errcode(int sqlerrcode)
Definition: elog.c:608
static const pg_wchar unassigned_codepoint_ranges[]
Definition: saslprep.c:169
#define ERROR
Definition: elog.h:43
#define IS_CODE_IN_TABLE(code, map)
Definition: saslprep.c:977
static char * buf
Definition: pg_test_fsync.c:67
#define ereport(elevel, rest)
Definition: elog.h:141
unsigned int pg_wchar
Definition: mbprint.c:31
static bool pg_is_ascii_string(const char *p)
Definition: saslprep.c:1038
static const pg_wchar LCat_codepoint_ranges[]
Definition: saslprep.c:609
pg_wchar * unicode_normalize_kc(const pg_wchar *input)
Definition: unicode_norm.c:307
#define Assert(condition)
Definition: c.h:739
int pg_utf_mblen(const unsigned char *s)
Definition: wchar.c:548
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
static const pg_wchar RandALCat_codepoint_ranges[]
Definition: saslprep.c:570
#define FREE(size)
Definition: saslprep.c:51

◆ pg_utf8_string_len()

static int pg_utf8_string_len ( const char *  source)
static

Definition at line 1014 of file saslprep.c.

References pg_utf8_islegal(), and pg_utf_mblen().

Referenced by pg_saslprep().

1015 {
1016  const unsigned char *p = (const unsigned char *) source;
1017  int l;
1018  int num_chars = 0;
1019 
1020  while (*p)
1021  {
1022  l = pg_utf_mblen(p);
1023 
1024  if (!pg_utf8_islegal(p, l))
1025  return -1;
1026 
1027  p += l;
1028  num_chars++;
1029  }
1030 
1031  return num_chars;
1032 }
bool pg_utf8_islegal(const unsigned char *source, int length)
Definition: wchar.c:1506
int pg_utf_mblen(const unsigned char *s)
Definition: wchar.c:548

Variable Documentation

◆ commonly_mapped_to_nothing_ranges

const pg_wchar commonly_mapped_to_nothing_ranges[]
static
Initial value:
=
{
0x00AD, 0x00AD,
0x034F, 0x034F,
0x1806, 0x1806,
0x180B, 0x180D,
0x200B, 0x200D,
0x2060, 0x2060,
0xFE00, 0xFE0F,
0xFEFF, 0xFEFF
}

Definition at line 93 of file saslprep.c.

Referenced by pg_saslprep().

◆ LCat_codepoint_ranges

const pg_wchar LCat_codepoint_ranges[]
static

Definition at line 609 of file saslprep.c.

Referenced by pg_saslprep().

◆ non_ascii_space_ranges

const pg_wchar non_ascii_space_ranges[]
static
Initial value:
=
{
0x00A0, 0x00A0,
0x1680, 0x1680,
0x2000, 0x200B,
0x202F, 0x202F,
0x205F, 0x205F,
0x3000, 0x3000
}

Definition at line 78 of file saslprep.c.

Referenced by pg_saslprep().

◆ prohibited_output_ranges

const pg_wchar prohibited_output_ranges[]
static

Definition at line 128 of file saslprep.c.

Referenced by pg_saslprep().

◆ RandALCat_codepoint_ranges

const pg_wchar RandALCat_codepoint_ranges[]
static

Definition at line 570 of file saslprep.c.

Referenced by pg_saslprep().

◆ unassigned_codepoint_ranges

const pg_wchar unassigned_codepoint_ranges[]
static

Definition at line 169 of file saslprep.c.

Referenced by pg_saslprep().