summaryrefslogtreecommitdiff
path: root/test/regression/nx_secure_test/nx_secure_distinguished_name_compare_test.c
blob: 6c4e5fe949ddd4ada12faf5bd6bc3466e6cca62c (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation                                */
/* Copyright (c) 2026 Eclipse ThreadX contributors                         */
/*                                                                         */
/* This program and the accompanying materials are made available under    */
/* the terms of the MIT License which is available at                      */
/* https://opensource.org/licenses/MIT.                                    */
/*                                                                         */
/* SPDX-License-Identifier: MIT                                            */
/***************************************************************************/


#include <stdio.h>
#include "nx_secure_tls.h"

#include "tls_test_utility.h"

#include "nx_secure_x509.h"


/* Test data. */
static NX_SECURE_X509_DISTINGUISHED_NAME test_data[] =
{
        /* Generic distinguished name. */
        {
           "US",                              /* const UCHAR *nx_secure_x509_country;                              */
            2,                                /* USHORT       nx_secure_x509_country_length;                       */
                                              /*                                                                   */
           "Organization",                    /* const UCHAR *nx_secure_x509_organization;                         */
           sizeof("Organization"),            /* USHORT       nx_secure_x509_organization_length;                  */
                                              /*                                                                   */
           "Org unit",                        /* const UCHAR *nx_secure_x509_org_unit;                             */
           sizeof("Org unit"),                /* USHORT       nx_secure_x509_org_unit_length;                      */
                                              /*                                                                   */
           "Qualifier",                       /* const UCHAR *nx_secure_x509_distinguished_name_qualifier;         */
           sizeof("Qualifier"),               /* USHORT       nx_secure_x509_distinguished_name_qualifier_length;  */
                                              /*                                                                   */
           "California",                      /* const UCHAR *nx_secure_x509_state;                                */
           sizeof("California"),              /* USHORT       nx_secure_x509_state_length;                         */
                                              /*                                                                   */
           "Common Name",                     /* const UCHAR *nx_secure_x509_common_name;                          */
           sizeof("Common Name"),             /* USHORT       nx_secure_x509_common_name_length;                   */
                                              /*                                                                   */
           "1234",                            /* const UCHAR *nx_secure_x509_serial_number;                        */
           sizeof("1234"),                    /* USHORT       nx_secure_x509_serial_number_length;                 */
        },

        /* Permutation - change common name to proper subset of generic entry. */
        {
           "US",                              /* const UCHAR *nx_secure_x509_country;                              */
            2,                                /* USHORT       nx_secure_x509_country_length;                       */
                                              /*                                                                   */
           "Organization",                    /* const UCHAR *nx_secure_x509_organization;                         */
           sizeof("Organization"),            /* USHORT       nx_secure_x509_organization_length;                  */
                                              /*                                                                   */
           "Org unit",                        /* const UCHAR *nx_secure_x509_org_unit;                             */
           sizeof("Org unit"),                /* USHORT       nx_secure_x509_org_unit_length;                      */
                                              /*                                                                   */
           "Qualifier",                       /* const UCHAR *nx_secure_x509_distinguished_name_qualifier;         */
           sizeof("Qualifier"),               /* USHORT       nx_secure_x509_distinguished_name_qualifier_length;  */
                                              /*                                                                   */
           "California",                      /* const UCHAR *nx_secure_x509_state;                                */
           sizeof("California"),              /* USHORT       nx_secure_x509_state_length;                         */
                                              /*                                                                   */
           "Common",                          /* const UCHAR *nx_secure_x509_common_name;                          */
           sizeof("Common"),                  /* USHORT       nx_secure_x509_common_name_length;                   */
                                              /*                                                                   */
           "1234",                            /* const UCHAR *nx_secure_x509_serial_number;                        */
           sizeof("1234"),                    /* USHORT       nx_secure_x509_serial_number_length;                 */
        },

        /* Permutation - change common name to proper superset of generic entry. */
        {
           "US",                              /* const UCHAR *nx_secure_x509_country;                              */
            2,                                /* USHORT       nx_secure_x509_country_length;                       */
                                              /*                                                                   */
           "Organization",                    /* const UCHAR *nx_secure_x509_organization;                         */
           sizeof("Organization"),            /* USHORT       nx_secure_x509_organization_length;                  */
                                              /*                                                                   */
           "Org unit",                        /* const UCHAR *nx_secure_x509_org_unit;                             */
           sizeof("Org unit"),                /* USHORT       nx_secure_x509_org_unit_length;                      */
                                              /*                                                                   */
           "Qualifier",                       /* const UCHAR *nx_secure_x509_distinguished_name_qualifier;         */
           sizeof("Qualifier"),               /* USHORT       nx_secure_x509_distinguished_name_qualifier_length;  */
                                              /*                                                                   */
           "California",                      /* const UCHAR *nx_secure_x509_state;                                */
           sizeof("California"),              /* USHORT       nx_secure_x509_state_length;                         */
                                              /*                                                                   */
           "Common Name Extended",            /* const UCHAR *nx_secure_x509_common_name;                          */
           sizeof("Common Name Extended"),    /* USHORT       nx_secure_x509_common_name_length;                   */
                                              /*                                                                   */
           "1234",                            /* const UCHAR *nx_secure_x509_serial_number;                        */
           sizeof("1234"),                    /* USHORT       nx_secure_x509_serial_number_length;                 */
        },

        /* Permutation - omit all fields other than common name, which matches above.                              */
        /* Should NOT compare equal if strict comparison is on, but should compare equal otherwise!                */
        {
           "",                                /* const UCHAR *nx_secure_x509_country;                              */
            0,                                /* USHORT       nx_secure_x509_country_length;                       */
                                              /*                                                                   */
           "",                                /* const UCHAR *nx_secure_x509_organization;                         */
           0,                                 /* USHORT       nx_secure_x509_organization_length;                  */
                                              /*                                                                   */
           "",                                /* const UCHAR *nx_secure_x509_org_unit;                             */
           0,                                 /* USHORT       nx_secure_x509_org_unit_length;                      */
                                              /*                                                                   */
           "",                                /* const UCHAR *nx_secure_x509_distinguished_name_qualifier;         */
           0,                                 /* USHORT       nx_secure_x509_distinguished_name_qualifier_length;  */
                                              /*                                                                   */
           "",                                /* const UCHAR *nx_secure_x509_state;                                */
           0,                                 /* USHORT       nx_secure_x509_state_length;                         */
                                              /*                                                                   */
           "Common Name",                     /* const UCHAR *nx_secure_x509_common_name;                          */
           sizeof("Common Name"),             /* USHORT       nx_secure_x509_common_name_length;                   */
                                              /*                                                                   */
           "",                                /* const UCHAR *nx_secure_x509_serial_number;                        */
           0,                                 /* USHORT       nx_secure_x509_serial_number_length;                 */
        },

        /* Permutation - completely empty certificate.                                                             */
        /* Should compare equal only to itself.                                                                    */
        {
           "",                                /* const UCHAR *nx_secure_x509_country;                              */
            0,                                /* USHORT       nx_secure_x509_country_length;                       */
                                              /*                                                                   */
           "",                                /* const UCHAR *nx_secure_x509_organization;                         */
           0,                                 /* USHORT       nx_secure_x509_organization_length;                  */
                                              /*                                                                   */
           "",                                /* const UCHAR *nx_secure_x509_org_unit;                             */
           0,                                 /* USHORT       nx_secure_x509_org_unit_length;                      */
                                              /*                                                                   */
           "",                                /* const UCHAR *nx_secure_x509_distinguished_name_qualifier;         */
           0,                                 /* USHORT       nx_secure_x509_distinguished_name_qualifier_length;  */
                                              /*                                                                   */
           "",                                /* const UCHAR *nx_secure_x509_state;                                */
           0,                                 /* USHORT       nx_secure_x509_state_length;                         */
                                              /*                                                                   */
           "",                                /* const UCHAR *nx_secure_x509_common_name;                          */
           0,                                 /* USHORT       nx_secure_x509_common_name_length;                   */
                                              /*                                                                   */
           "",                                /* const UCHAR *nx_secure_x509_serial_number;                        */
           0,                                 /* USHORT       nx_secure_x509_serial_number_length;                 */
        },

        /* Permutation - change state field but leave all else the same.  */
        /* Should NOT compare equal if strict comparison is on, but should compare equal otherwise!                */
        {
           "US",                              /* const UCHAR *nx_secure_x509_country;                              */
            2,                                /* USHORT       nx_secure_x509_country_length;                       */
                                              /*                                                                   */
           "Organization",                    /* const UCHAR *nx_secure_x509_organization;                         */
           sizeof("Organization"),            /* USHORT       nx_secure_x509_organization_length;                  */
                                              /*                                                                   */
           "Org unit",                        /* const UCHAR *nx_secure_x509_org_unit;                             */
           sizeof("Org unit"),                /* USHORT       nx_secure_x509_org_unit_length;                      */
                                              /*                                                                   */
           "Qualifier",                       /* const UCHAR *nx_secure_x509_distinguished_name_qualifier;         */
           sizeof("Qualifier"),               /* USHORT       nx_secure_x509_distinguished_name_qualifier_length;  */
                                              /*                                                                   */
           "Nevada",                          /* const UCHAR *nx_secure_x509_state;                                */
           sizeof("Nevada"),                  /* USHORT       nx_secure_x509_state_length;                         */
                                              /*                                                                   */
           "Common Name",                     /* const UCHAR *nx_secure_x509_common_name;                          */
           sizeof("Common Name"),             /* USHORT       nx_secure_x509_common_name_length;                   */
                                              /*                                                                   */
           "1234",                            /* const UCHAR *nx_secure_x509_serial_number;                        */
           sizeof("1234"),                    /* USHORT       nx_secure_x509_serial_number_length;                 */
        },

        /* Permutation - change common name field to new value with same length as base case but leave all else    */
        /* the same.  */
        /* Should NOT compare equal! */
        {
           "US",                              /* const UCHAR *nx_secure_x509_country;                              */
            2,                                /* USHORT       nx_secure_x509_country_length;                       */
                                              /*                                                                   */
           "Organization",                    /* const UCHAR *nx_secure_x509_organization;                         */
           sizeof("Organization"),            /* USHORT       nx_secure_x509_organization_length;                  */
                                              /*                                                                   */
           "Org unit",                        /* const UCHAR *nx_secure_x509_org_unit;                             */
           sizeof("Org unit"),                /* USHORT       nx_secure_x509_org_unit_length;                      */
                                              /*                                                                   */
           "Qualifier",                       /* const UCHAR *nx_secure_x509_distinguished_name_qualifier;         */
           sizeof("Qualifier"),               /* USHORT       nx_secure_x509_distinguished_name_qualifier_length;  */
                                              /*                                                                   */
           "California",                      /* const UCHAR *nx_secure_x509_state;                                */
           sizeof("California"),              /* USHORT       nx_secure_x509_state_length;                         */
                                              /*                                                                   */
           "Fishy Name1",                     /* const UCHAR *nx_secure_x509_common_name;                          */
           sizeof("Fishy Name1"),             /* USHORT       nx_secure_x509_common_name_length;                   */
                                              /*                                                                   */
           "1234",                            /* const UCHAR *nx_secure_x509_serial_number;                        */
           sizeof("1234"),                    /* USHORT       nx_secure_x509_serial_number_length;                 */
        },


        /* Permutation - No common name, but other fields.  */
        /* Should NOT compare equal! */
        {
           "US",                              /* const UCHAR *nx_secure_x509_country;                              */
            0,                                /* USHORT       nx_secure_x509_country_length;                       */
                                              /*                                                                   */
           "Organization",                    /* const UCHAR *nx_secure_x509_organization;                         */
           sizeof("Organization"),            /* USHORT       nx_secure_x509_organization_length;                  */
                                              /*                                                                   */
           "Org unit",                        /* const UCHAR *nx_secure_x509_org_unit;                             */
           sizeof("Org unit"),                /* USHORT       nx_secure_x509_org_unit_length;                      */
                                              /*                                                                   */
           "Qualifier",                       /* const UCHAR *nx_secure_x509_distinguished_name_qualifier;         */
           sizeof("Qualifier"),               /* USHORT       nx_secure_x509_distinguished_name_qualifier_length;  */
                                              /*                                                                   */
           "California",                      /* const UCHAR *nx_secure_x509_state;                                */
           sizeof("California"),              /* USHORT       nx_secure_x509_state_length;                         */
                                              /*                                                                   */
           "",                                /* const UCHAR *nx_secure_x509_common_name;                          */
           0,                                 /* USHORT       nx_secure_x509_common_name_length;                   */
                                              /*                                                                   */
           "1234",                            /* const UCHAR *nx_secure_x509_serial_number;                        */
           sizeof("1234"),                    /* USHORT       nx_secure_x509_serial_number_length;                 */
        },

};

static UINT test_data_size = sizeof(test_data) / sizeof(NX_SECURE_X509_DISTINGUISHED_NAME);

static TX_THREAD thread_0;

static VOID thread_0_entry(ULONG thread_input);

#ifdef CTEST
void test_application_define(void *first_unused_memory);
void test_application_define(void *first_unused_memory)
#else
void nx_secure_distingushed_name_compare_test_application_define(void *first_unused_memory)
#endif
{
    tx_thread_create(&thread_0, "Thread 0", thread_0_entry, 0,
                     first_unused_memory, 4096,
                     16, 16, 4, TX_AUTO_START);
}

static VOID thread_0_entry(ULONG thread_input)
{
UINT i, j;
INT compare_result;
UINT error_count = 0;

    /* Print out test information banner.  */                                     
    printf("NetX Secure Test:   X590 Name Compare Test.............................");

    /* First, compare each entry to itself - should all compare equal. */
    for (i = 0; i < test_data_size ; i++)
    {
        compare_result = _nx_secure_x509_distinguished_name_compare(&test_data[i], &test_data[i], NX_SECURE_X509_NAME_ALL_FIELDS);
        EXPECT_EQ(0, compare_result);
    }

    /* Now compare all the entries with each other. Should all compare unequal if strict
       name comparison is on. */
    for(i = 0; i < test_data_size; ++i)
    {
        for(j = i + 1; j < test_data_size; ++j)
        {
            /* Compare entry i to entry j. */
            compare_result = compare_result = _nx_secure_x509_distinguished_name_compare(&test_data[i], &test_data[j], NX_SECURE_X509_NAME_ALL_FIELDS);
            if(compare_result == 0)
            {
                printf("\nError in distinguished name compare. First name index: %d, Second name index: %d\n", i, j);
                error_count++;
            }

            /* Compare entry j to entry i. Reversed to make sure order doesn't matter. */
            compare_result = compare_result = _nx_secure_x509_distinguished_name_compare(&test_data[j], &test_data[i], NX_SECURE_X509_NAME_ALL_FIELDS);

            if(compare_result == 0)
            {
                printf("\nError in distinguished name compare. First name index: %d, Second name index: %d\n", j, i);
                error_count++;
            }
        }
    }

    /* See if we got errors. */
    if(error_count > 0)
    {
        printf("ERROR\n");
        test_control_return(1);
    }
    else
    {
        printf("SUCCESS!\n");
        test_control_return(0);
    }
}