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

Go to the source code of this file.

Enumerations

enum  pg_saslprep_rc { SASLPREP_SUCCESS = 0 , SASLPREP_OOM = -1 , SASLPREP_INVALID_UTF8 = -2 , SASLPREP_PROHIBITED = -3 }
 

Functions

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

Enumeration Type Documentation

◆ pg_saslprep_rc

Enumerator
SASLPREP_SUCCESS 
SASLPREP_OOM 
SASLPREP_INVALID_UTF8 
SASLPREP_PROHIBITED 

Definition at line 20 of file saslprep.h.

21{
23 SASLPREP_OOM = -1, /* out of memory (only in frontend) */
24 SASLPREP_INVALID_UTF8 = -2, /* input is not a valid UTF-8 string */
25 SASLPREP_PROHIBITED = -3, /* output would contain prohibited characters */
pg_saslprep_rc
Definition: saslprep.h:21
@ 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

Function Documentation

◆ pg_saslprep()

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

Definition at line 1047 of file saslprep.c.

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

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().