summaryrefslogtreecommitdiff
path: root/nx_secure/src/nx_secure_x509_wildcard_compare.c
blob: 0b62d28d6af90b78cf0d2c3bae1906bd2be4b8d1 (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
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present 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
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Secure Component                                                 */
/**                                                                       */
/**    X.509 Digital Certificates                                         */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SECURE_SOURCE_CODE

#include "nx_secure_x509.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_secure_x509_wildcard_compare                    PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function compares a name (string) against a name string using  */
/*    wildcards as found in the Common Name and subjectAltName fields of  */
/*    an X.509 certificate. This is primarily used when checking a DNS    */
/*    name against an X.509 certificate provided by a remote host.        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    dns_name                              Name to check                 */
/*    dns_name_len                          Length of name                */
/*    wildcard_name                         String with name or wildcard  */
/*    wildcard_len                          Length of wildcard            */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    compare value                         0 if equal, else non-zero     */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_secure_x509_common_name_dns_check Check Common Name by DNS      */
/*    _nx_secure_x509_subject_alt_names_find                              */
/*                                          Find subject alt names        */
/*                                                                        */
/**************************************************************************/
INT _nx_secure_x509_wildcard_compare(const UCHAR *dns_name, UINT dns_name_len,
                                     const UCHAR *wildcard_name, UINT wildcard_len)
{
INT dns_offset;
INT wildcard_offset;

    dns_offset = (INT)dns_name_len - 1;
    wildcard_offset = (INT)wildcard_len - 1;

    /* Walk backwards through each name. */
    while (dns_offset >= 0 && wildcard_offset >= 0)
    {
        /* Check each character. */
        if (dns_name[dns_offset] != wildcard_name[wildcard_offset])
        {
            /* Characters do not match, check for wildcard. */
            if (wildcard_name[wildcard_offset] == '*')
            {
                if (wildcard_offset != 0 || wildcard_name[1] != '.')
                {
                    /* Only match wildcard character when it is
                       the only character of the left-most label. */
                    return(1);
                }

                while (dns_offset >= 0)
                {
                    if (dns_name[dns_offset] == '.')
                    {
                        /* Wildcard does not match full stops.  */
                        return(1);
                    }
                    dns_offset--;
                }
                /* Wildcard match, they are OK. */
                return(0);
            }

            /* No match and no wildcard. */
            return(1);
        }

        /* Adjust offsets. */
        dns_offset--;
        wildcard_offset--;
    }

    if (dns_offset != -1 || wildcard_offset != -1)
    {
        /* Length mismatch. */
        return(1);
    }
    /* Both names are exactly the same. */
    return(0);
}