PostgreSQL Source Code git master
saslprep.c File Reference
#include "postgres.h"
#include "utils/memutils.h"
#include "common/saslprep.h"
#include "common/string.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 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)
 
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 40 of file saslprep.c.

◆ FREE

#define FREE (   size)    pfree(size)

Definition at line 41 of file saslprep.c.

◆ IS_CODE_IN_TABLE

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

Definition at line 966 of file saslprep.c.

◆ STRDUP

#define STRDUP (   s)    pstrdup(s)

Definition at line 39 of file saslprep.c.

Function Documentation

◆ codepoint_range_cmp()

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

Definition at line 969 of file saslprep.c.

970{
971 const pg_wchar *key = (const pg_wchar *) a;
972 const pg_wchar *range = (const pg_wchar *) b;
973
974 if (*key < range[0])
975 return -1; /* less than lower bound */
976 if (*key > range[1])
977 return 1; /* greater than upper bound */
978
979 return 0; /* within range */
980}
int b
Definition: isn.c:69
int a
Definition: isn.c:68
unsigned int pg_wchar
Definition: mbprint.c:31
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412

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

Referenced by is_code_in_table().

◆ is_code_in_table()

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

Definition at line 983 of file saslprep.c.

984{
985 Assert(mapsize % 2 == 0);
986
987 if (code < map[0] || code > map[mapsize - 1])
988 return false;
989
990 if (bsearch(&code, map, mapsize / 2, sizeof(pg_wchar) * 2,
992 return true;
993 else
994 return false;
995}
#define Assert(condition)
Definition: c.h:815
static int codepoint_range_cmp(const void *a, const void *b)
Definition: saslprep.c:969

References Assert, and codepoint_range_cmp().

◆ pg_saslprep()

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

Definition at line 1047 of file saslprep.c.

1048{
1049 pg_wchar *input_chars = NULL;
1050 pg_wchar *output_chars = NULL;
1051 int input_size;
1052 char *result;
1053 int result_size;
1054 int count;
1055 int i;
1056 bool contains_RandALCat;
1057 unsigned char *p;
1058 pg_wchar *wp;
1059
1060 /* Ensure we return *output as NULL on failure */
1061 *output = NULL;
1062
1063 /*
1064 * Quick check if the input is pure ASCII. An ASCII string requires no
1065 * further processing.
1066 */
1067 if (pg_is_ascii(input))
1068 {
1069 *output = STRDUP(input);
1070 if (!(*output))
1071 goto oom;
1072 return SASLPREP_SUCCESS;
1073 }
1074
1075 /*
1076 * Convert the input from UTF-8 to an array of Unicode codepoints.
1077 *
1078 * This also checks that the input is a legal UTF-8 string.
1079 */
1080 input_size = pg_utf8_string_len(input);
1081 if (input_size < 0)
1082 return SASLPREP_INVALID_UTF8;
1083 if (input_size >= MaxAllocSize / sizeof(pg_wchar))
1084 goto oom;
1085
1086 input_chars = ALLOC((input_size + 1) * sizeof(pg_wchar));
1087 if (!input_chars)
1088 goto oom;
1089
1090 p = (unsigned char *) input;
1091 for (i = 0; i < input_size; i++)
1092 {
1093 input_chars[i] = utf8_to_unicode(p);
1094 p += pg_utf_mblen(p);
1095 }
1096 input_chars[i] = (pg_wchar) '\0';
1097
1098 /*
1099 * The steps below correspond to the steps listed in [RFC3454], Section
1100 * "2. Preparation Overview"
1101 */
1102
1103 /*
1104 * 1) Map -- For each character in the input, check if it has a mapping
1105 * and, if so, replace it with its mapping.
1106 */
1107 count = 0;
1108 for (i = 0; i < input_size; i++)
1109 {
1110 pg_wchar code = input_chars[i];
1111
1113 input_chars[count++] = 0x0020;
1115 {
1116 /* map to nothing */
1117 }
1118 else
1119 input_chars[count++] = code;
1120 }
1121 input_chars[count] = (pg_wchar) '\0';
1122 input_size = count;
1123
1124 if (input_size == 0)
1125 goto prohibited; /* don't allow empty password */
1126
1127 /*
1128 * 2) Normalize -- Normalize the result of step 1 using Unicode
1129 * normalization.
1130 */
1131 output_chars = unicode_normalize(UNICODE_NFKC, input_chars);
1132 if (!output_chars)
1133 goto oom;
1134
1135 /*
1136 * 3) Prohibit -- Check for any characters that are not allowed in the
1137 * output. If any are found, return an error.
1138 */
1139 for (i = 0; i < input_size; i++)
1140 {
1141 pg_wchar code = input_chars[i];
1142
1144 goto prohibited;
1146 goto prohibited;
1147 }
1148
1149 /*
1150 * 4) Check bidi -- Possibly check for right-to-left characters, and if
1151 * any are found, make sure that the whole string satisfies the
1152 * requirements for bidirectional strings. If the string does not satisfy
1153 * the requirements for bidirectional strings, return an error.
1154 *
1155 * [RFC3454], Section "6. Bidirectional Characters" explains in more
1156 * detail what that means:
1157 *
1158 * "In any profile that specifies bidirectional character handling, all
1159 * three of the following requirements MUST be met:
1160 *
1161 * 1) The characters in section 5.8 MUST be prohibited.
1162 *
1163 * 2) If a string contains any RandALCat character, the string MUST NOT
1164 * contain any LCat character.
1165 *
1166 * 3) If a string contains any RandALCat character, a RandALCat character
1167 * MUST be the first character of the string, and a RandALCat character
1168 * MUST be the last character of the string."
1169 */
1170 contains_RandALCat = false;
1171 for (i = 0; i < input_size; i++)
1172 {
1173 pg_wchar code = input_chars[i];
1174
1176 {
1177 contains_RandALCat = true;
1178 break;
1179 }
1180 }
1181
1182 if (contains_RandALCat)
1183 {
1184 pg_wchar first = input_chars[0];
1185 pg_wchar last = input_chars[input_size - 1];
1186
1187 for (i = 0; i < input_size; i++)
1188 {
1189 pg_wchar code = input_chars[i];
1190
1192 goto prohibited;
1193 }
1194
1197 goto prohibited;
1198 }
1199
1200 /*
1201 * Finally, convert the result back to UTF-8.
1202 */
1203 result_size = 0;
1204 for (wp = output_chars; *wp; wp++)
1205 {
1206 unsigned char buf[4];
1207
1208 unicode_to_utf8(*wp, buf);
1209 result_size += pg_utf_mblen(buf);
1210 }
1211
1212 result = ALLOC(result_size + 1);
1213 if (!result)
1214 goto oom;
1215
1216 /*
1217 * There are no error exits below here, so the error exit paths don't need
1218 * to worry about possibly freeing "result".
1219 */
1220 p = (unsigned char *) result;
1221 for (wp = output_chars; *wp; wp++)
1222 {
1223 unicode_to_utf8(*wp, p);
1224 p += pg_utf_mblen(p);
1225 }
1226 Assert((char *) p == result + result_size);
1227 *p = '\0';
1228
1229 FREE(input_chars);
1230 FREE(output_chars);
1231
1232 *output = result;
1233 return SASLPREP_SUCCESS;
1234
1235prohibited:
1236 if (input_chars)
1237 FREE(input_chars);
1238 if (output_chars)
1239 FREE(output_chars);
1240
1241 return SASLPREP_PROHIBITED;
1242
1243oom:
1244 if (input_chars)
1245 FREE(input_chars);
1246 if (output_chars)
1247 FREE(output_chars);
1248
1249 return SASLPREP_OOM;
1250}
#define MaxAllocSize
Definition: fe_memutils.h:22
FILE * input
FILE * output
int i
Definition: isn.c:72
static pg_wchar utf8_to_unicode(const unsigned char *c)
Definition: mbprint.c:53
static char * buf
Definition: pg_test_fsync.c:72
#define pg_utf_mblen
Definition: pg_wchar.h:633
static unsigned char * unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
Definition: pg_wchar.h:575
static const pg_wchar unassigned_codepoint_ranges[]
Definition: saslprep.c:158
static const pg_wchar non_ascii_space_ranges[]
Definition: saslprep.c:67
static const pg_wchar RandALCat_codepoint_ranges[]
Definition: saslprep.c:559
#define STRDUP(s)
Definition: saslprep.c:39
#define IS_CODE_IN_TABLE(code, map)
Definition: saslprep.c:966
#define ALLOC(size)
Definition: saslprep.c:40
#define FREE(size)
Definition: saslprep.c:41
static const pg_wchar LCat_codepoint_ranges[]
Definition: saslprep.c:598
static const pg_wchar commonly_mapped_to_nothing_ranges[]
Definition: saslprep.c:82
static const pg_wchar prohibited_output_ranges[]
Definition: saslprep.c:117
static int pg_utf8_string_len(const char *source)
Definition: saslprep.c:1003
@ SASLPREP_INVALID_UTF8
Definition: saslprep.h:24
@ SASLPREP_PROHIBITED
Definition: saslprep.h:25
@ SASLPREP_OOM
Definition: saslprep.h:23
@ SASLPREP_SUCCESS
Definition: saslprep.h:22
bool pg_is_ascii(const char *str)
Definition: string.c:132
pg_wchar * unicode_normalize(UnicodeNormalizationForm form, const pg_wchar *input)
Definition: unicode_norm.c:402
@ UNICODE_NFKC
Definition: unicode_norm.h:23

References ALLOC, Assert, buf, commonly_mapped_to_nothing_ranges, FREE, i, input, IS_CODE_IN_TABLE, LCat_codepoint_ranges, MaxAllocSize, non_ascii_space_ranges, output, pg_is_ascii(), 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_NFKC, unicode_normalize(), unicode_to_utf8(), and utf8_to_unicode().

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

◆ pg_utf8_string_len()

static int pg_utf8_string_len ( const char *  source)
static

Definition at line 1003 of file saslprep.c.

1004{
1005 const unsigned char *p = (const unsigned char *) source;
1006 int l;
1007 int num_chars = 0;
1008 size_t len = strlen(source);
1009
1010 while (len)
1011 {
1012 l = pg_utf_mblen(p);
1013
1014 if (len < l || !pg_utf8_islegal(p, l))
1015 return -1;
1016
1017 p += l;
1018 len -= l;
1019 num_chars++;
1020 }
1021
1022 return num_chars;
1023}
const void size_t len
static rewind_source * source
Definition: pg_rewind.c:89
bool pg_utf8_islegal(const unsigned char *source, int length)
Definition: wchar.c:1953

References len, pg_utf8_islegal(), pg_utf_mblen, and source.

Referenced by pg_saslprep().

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 82 of file saslprep.c.

Referenced by pg_saslprep().

◆ LCat_codepoint_ranges

const pg_wchar LCat_codepoint_ranges[]
static

Definition at line 598 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 67 of file saslprep.c.

Referenced by pg_saslprep().

◆ prohibited_output_ranges

const pg_wchar prohibited_output_ranges[]
static

Definition at line 117 of file saslprep.c.

Referenced by pg_saslprep().

◆ RandALCat_codepoint_ranges

const pg_wchar RandALCat_codepoint_ranges[]
static

Definition at line 559 of file saslprep.c.

Referenced by pg_saslprep().

◆ unassigned_codepoint_ranges

const pg_wchar unassigned_codepoint_ranges[]
static

Definition at line 158 of file saslprep.c.

Referenced by pg_saslprep().