netatalk  4.6.0
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
srp.h
Go to the documentation of this file.
1/* Shared SRP parameters, verifier record format, and permission policy. */
2#ifndef _ATALK_SRP_H
3#define _ATALK_SRP_H 1
4
5#include <ctype.h>
6#include <string.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9
10#define SRP_SALT_LEN 16
11#define SRP_NBYTES 192 /* 1536-bit prime */
12#define SRP_SHA1_LEN 20
13#define SRP_GROUP_INDEX 0x0002 /* RFC 5054 group #2 */
14#define SRP_USERNAME_MAX_LEN 255
15#define SRP_DISABLED_CHAR '*'
16
17/* Hex lengths exclude separators and the terminating NUL. */
18#define SRP_HEX_SALT_LEN (SRP_SALT_LEN * 2)
19#define SRP_HEX_V_LEN (SRP_NBYTES * 2)
20/* salt:verifier\n, excluding the terminating NUL. */
21#define SRP_FIELDS_LEN (SRP_HEX_SALT_LEN + 1 + SRP_HEX_V_LEN + 1)
22/* :salt:verifier\n, excluding the username and terminating NUL. */
23#define SRP_FORMAT_LEN (1 + SRP_FIELDS_LEN)
24
25/*
26 * RFC 5054 group #2: 1536-bit MODP group. N is the safe prime, g = 2.
27 */
28static const unsigned char srp_N_bytes[SRP_NBYTES] = {
29 0x9D, 0xEF, 0x3C, 0xAF, 0xB9, 0x39, 0x27, 0x7A,
30 0xB1, 0xF1, 0x2A, 0x86, 0x17, 0xA4, 0x7B, 0xBB,
31 0xDB, 0xA5, 0x1D, 0xF4, 0x99, 0xAC, 0x4C, 0x80,
32 0xBE, 0xEE, 0xA9, 0x61, 0x4B, 0x19, 0xCC, 0x4D,
33 0x5F, 0x4F, 0x5F, 0x55, 0x6E, 0x27, 0xCB, 0xDE,
34 0x51, 0xC6, 0xA9, 0x4B, 0xE4, 0x60, 0x7A, 0x29,
35 0x15, 0x58, 0x90, 0x3B, 0xA0, 0xD0, 0xF8, 0x43,
36 0x80, 0xB6, 0x55, 0xBB, 0x9A, 0x22, 0xE8, 0xDC,
37 0xDF, 0x02, 0x8A, 0x7C, 0xEC, 0x67, 0xF0, 0xD0,
38 0x81, 0x34, 0xB1, 0xC8, 0xB9, 0x79, 0x89, 0x14,
39 0x9B, 0x60, 0x9E, 0x0B, 0xE3, 0xBA, 0xB6, 0x3D,
40 0x47, 0x54, 0x83, 0x81, 0xDB, 0xC5, 0xB1, 0xFC,
41 0x76, 0x4E, 0x3F, 0x4B, 0x53, 0xDD, 0x9D, 0xA1,
42 0x15, 0x8B, 0xFD, 0x3E, 0x2B, 0x9C, 0x8C, 0xF5,
43 0x6E, 0xDF, 0x01, 0x95, 0x39, 0x34, 0x96, 0x27,
44 0xDB, 0x2F, 0xD5, 0x3D, 0x24, 0xB7, 0xC4, 0x86,
45 0x65, 0x77, 0x2E, 0x43, 0x7D, 0x6C, 0x7F, 0x8C,
46 0xE4, 0x42, 0x73, 0x4A, 0xF7, 0xCC, 0xB7, 0xAE,
47 0x83, 0x7C, 0x26, 0x4A, 0xE3, 0xA9, 0xBE, 0xB8,
48 0x7F, 0x8A, 0x2F, 0xE9, 0xB8, 0xB5, 0x29, 0x2E,
49 0x5A, 0x02, 0x1F, 0xFF, 0x5E, 0x91, 0x47, 0x9E,
50 0x8C, 0xE7, 0xA2, 0x8C, 0x24, 0x42, 0xC6, 0xF3,
51 0x15, 0x18, 0x0F, 0x93, 0x49, 0x9A, 0x23, 0x4D,
52 0xCF, 0x76, 0xE3, 0xFE, 0xD1, 0x35, 0xF9, 0xBB,
53};
54
55static const unsigned char srp_g_byte = 0x02;
56
57/* Validate a NUL-terminated username for a verifier record. */
58static inline int srp_valid_username(const char *name)
59{
60 size_t length = strnlen(name, SRP_USERNAME_MAX_LEN + 1);
61 return length > 0 && length <= SRP_USERNAME_MAX_LEN &&
62 strchr(name, ':') == NULL && strchr(name, '\n') == NULL &&
63 strchr(name, '\r') == NULL;
64}
65
66/*
67 * Validate NUL-terminated salt:verifier\n fields, including fully disabled
68 * records. Authentication must reject disabled records separately.
69 */
70static inline int srp_valid_fields(const char *fields)
71{
72 int salt_disabled = 1, verifier_disabled = 1;
73
74 if (strlen(fields) != SRP_FIELDS_LEN ||
75 fields[SRP_HEX_SALT_LEN] != ':' ||
76 fields[SRP_FIELDS_LEN - 1] != '\n') {
77 return 0;
78 }
79
80 for (size_t i = 0; i < SRP_HEX_SALT_LEN; i++) {
81 salt_disabled &= fields[i] == SRP_DISABLED_CHAR;
82 }
83
84 for (size_t i = 0; i < SRP_HEX_V_LEN; i++) {
85 verifier_disabled &= fields[SRP_HEX_SALT_LEN + 1 + i] == SRP_DISABLED_CHAR;
86 }
87
88 if (salt_disabled || verifier_disabled) {
89 return salt_disabled && verifier_disabled;
90 }
91
92 for (size_t i = 0; i < SRP_HEX_SALT_LEN; i++) {
93 if (!isxdigit((unsigned char)fields[i])) {
94 return 0;
95 }
96 }
97
98 for (size_t i = 0; i < SRP_HEX_V_LEN; i++) {
99 if (!isxdigit((unsigned char)fields[SRP_HEX_SALT_LEN + 1 + i])) {
100 return 0;
101 }
102 }
103
104 return 1;
105}
106
107/* Owner permissions may vary; group and other must have no access. */
108static inline int srp_verifier_mode_is_safe(mode_t mode)
109{
110 return (mode & (S_IRWXG | S_IRWXO)) == 0;
111}
112
113#endif
size_t strnlen(const char *s, size_t n)
Definition misc.c:19
#define SRP_HEX_V_LEN
Definition srp.h:19
static const unsigned char srp_N_bytes[SRP_NBYTES]
Definition srp.h:28
#define SRP_USERNAME_MAX_LEN
Definition srp.h:14
static int srp_valid_fields(const char *fields)
Definition srp.h:70
static int srp_valid_username(const char *name)
Definition srp.h:58
#define SRP_NBYTES
Definition srp.h:11
#define SRP_HEX_SALT_LEN
Definition srp.h:18
#define SRP_FIELDS_LEN
Definition srp.h:21
static const unsigned char srp_g_byte
Definition srp.h:55
static int srp_verifier_mode_is_safe(mode_t mode)
Definition srp.h:108
#define SRP_DISABLED_CHAR
Definition srp.h:15
#define NULL
Definition utf8util.c:47