summaryrefslogtreecommitdiffstats
path: root/test/unit/nts_ntp_server.c
blob: 27779427ddd35a8ba3ac0096394a5023343988b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 **********************************************************************
 * Copyright (C) Miroslav Lichvar  2020
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 **********************************************************************
 */

#include <config.h>
#include "test.h"

#ifdef FEAT_NTS

#include <local.h>
#include <sched.h>

#include <nts_ntp_server.c>

static void
prepare_request(NTP_Packet *packet, NTP_PacketInfo *info, int valid, int nak)
{
  unsigned char uniq_id[NTS_MIN_UNIQ_ID_LENGTH], nonce[NTS_MIN_UNPADDED_NONCE_LENGTH];
  SIV_Instance siv;
  NKE_Context context;
  NKE_Cookie cookie;
  int i, index, cookie_start, auth_start;

  context.algorithm = random() % 2 && SIV_GetKeyLength(AEAD_AES_128_GCM_SIV) > 0 ?
                      AEAD_AES_128_GCM_SIV : AEAD_AES_SIV_CMAC_256;
  context.c2s.length = SIV_GetKeyLength(context.algorithm);
  assert(context.c2s.length <= sizeof (context.c2s.key));
  UTI_GetRandomBytes(&context.c2s.key, context.c2s.length);
  context.s2c.length = SIV_GetKeyLength(context.algorithm);
  assert(context.s2c.length <= sizeof (context.s2c.key));
  UTI_GetRandomBytes(&context.s2c.key, context.s2c.length);

  TEST_CHECK(NKS_GenerateCookie(&context, &cookie));

  UTI_GetRandomBytes(uniq_id, sizeof (uniq_id));
  UTI_GetRandomBytes(nonce, sizeof (nonce));

  memset(packet, 0, sizeof (*packet));
  packet->lvm = NTP_LVM(0, 4, MODE_CLIENT);
  memset(info, 0, sizeof (*info));
  info->version = 4;
  info->mode = MODE_CLIENT;
  info->length = NTP_HEADER_LENGTH;

  if (valid)
    index = -1;
  else
    index = random() % 3;

  DEBUG_LOG("valid=%d nak=%d index=%d", valid, nak, index);

  if (index != 0)
    TEST_CHECK(NEF_AddField(packet, info, NTP_EF_NTS_UNIQUE_IDENTIFIER,
                            uniq_id, sizeof (uniq_id)));

  cookie_start = info->length;

  if (index != 1)
    TEST_CHECK(NEF_AddField(packet, info, NTP_EF_NTS_COOKIE,
                            cookie.cookie, cookie.length));

  for (i = random() % 4; i > 0; i--)
    TEST_CHECK(NEF_AddField(packet, info, NTP_EF_NTS_COOKIE_PLACEHOLDER,
                            cookie.cookie, cookie.length));

  auth_start = info->length;

  if (index != 2) {
    siv = SIV_CreateInstance(context.algorithm);
    TEST_CHECK(siv);
    TEST_CHECK(SIV_SetKey(siv, context.c2s.key, context.c2s.length));
    TEST_CHECK(NNA_GenerateAuthEF(packet, info, siv, nonce, sizeof (nonce),
                                  (const unsigned char *)"", 0, 0));
    SIV_DestroyInstance(siv);
  }

  if (nak)
    ((unsigned char *)packet)[(index == 2 ? cookie_start :
                               (index == 1 ? auth_start :
                                (random() % 2 ? cookie_start : auth_start))) +
                              4 + random() % 16]++;
}

static void
init_response(NTP_Packet *packet, NTP_PacketInfo *info)
{
  memset(packet, 0, sizeof (*packet));
  packet->lvm = NTP_LVM(0, 4, MODE_SERVER);
  memset(info, 0, sizeof (*info));
  info->version = 4;
  info->mode = MODE_SERVER;
  info->length = NTP_HEADER_LENGTH;
}

void
test_unit(void)
{
  NTP_PacketInfo req_info, res_info;
  NTP_Packet request, response;
  int i, valid, nak;
  uint32_t kod;

  char conf[][100] = {
    "ntsport 0",
    "ntsprocesses 0",
    "ntsserverkey nts_ke.key",
    "ntsservercert nts_ke.crt",
  };

  CNF_Initialise(0, 0);
  for (i = 0; i < sizeof conf / sizeof conf[0]; i++)
    CNF_ParseLine(NULL, i + 1, conf[i]);

  LCL_Initialise();
  TST_RegisterDummyDrivers();
  SCH_Initialise();
  NKS_PreInitialise(0, 0, 0);
  NKS_Initialise();
  NNS_Initialise();

  for (i = 0; i < 50000; i++) {
    valid = random() % 2;
    nak = random() % 2;
    prepare_request(&request, &req_info, valid, nak);

    TEST_CHECK(NNS_CheckRequestAuth(&request, &req_info, &kod) == (valid && !nak));

    if (valid && !nak) {
      TEST_CHECK(kod == 0);
      TEST_CHECK(server->num_cookies > 0);

      init_response(&response, &res_info);
      TEST_CHECK(NNS_GenerateResponseAuth(&request, &req_info, &response, &res_info, kod));

      TEST_CHECK(res_info.ext_fields == 2);
      TEST_CHECK(server->num_cookies == 0);
    } else if (valid && nak) {
      TEST_CHECK(kod == NTP_KOD_NTS_NAK);
      TEST_CHECK(server->num_cookies == 0);

      init_response(&response, &res_info);
      TEST_CHECK(NNS_GenerateResponseAuth(&request, &req_info, &response, &res_info, kod));

      TEST_CHECK(res_info.ext_fields == 1);
      TEST_CHECK(server->num_cookies == 0);
    } else {
      TEST_CHECK(kod == 0);
      TEST_CHECK(server->num_cookies == 0);
    }
  }

  NNS_Finalise();
  NKS_Finalise();
  SCH_Finalise();
  LCL_Finalise();
  CNF_Finalise();
}
#else
void
test_unit(void)
{
  TEST_REQUIRE(0);
}
#endif