summaryrefslogtreecommitdiff
path: root/nx_secure/src/nx_secure_remote_certificate_verify.c
blob: c9e652fa24eddfdf6c912ef605e978581aa1b69a (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
/***************************************************************************
 * 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                                                 */
/**                                                                       */
/**    Transport Layer Security (TLS)                                     */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SECURE_SOURCE_CODE

#include "nx_secure_tls.h"
#include "nx_secure_x509.h"

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_secure_remote_certificate_verify                PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yanwu Cai, Microsoft Corporation                                    */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function verifies the authenticity of a certificate provided   */
/*    by the remote host by checking its digital signature against the    */
/*    trusted store, checking the certificate's validity period, and      */
/*    optionally checking the Common Name against the Top-Level Domain    */
/*    (TLD) name used to access the remote host.                          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    store                                 Pointer to certificate store  */
/*    certificate                           Pointer to cert chain         */
/*    current_time                          Current timestamp             */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Certificate validity status   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_secure_x509_certificate_chain_verify                            */
/*                                          Verify cert against stores    */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_secure_tls_remote_certificate_verify                            */
/*                                          Verify the server certificate */
/*                                                                        */
/**************************************************************************/
UINT _nx_secure_remote_certificate_verify(NX_SECURE_X509_CERTIFICATE_STORE *store,
                                          NX_SECURE_X509_CERT *certificate, ULONG current_time)
{
UINT status;

    /* Now verify our remote certificate chain. If the certificate can be linked to an issuer in the trusted store
       through an issuer chain, this function will return NX_SUCCESS. */
    status = _nx_secure_x509_certificate_chain_verify(store, certificate, current_time);

    if (status != NX_SUCCESS)
    {

        /* Translate some X.509 return values into TLS return values. NX_SECURE_X509_CERTIFICATE_NOT_FOUND is removed
           as _nx_secure_x509_certificate_chain_verify() will not return this value. */
        switch (status)
        {
        case NX_SECURE_X509_UNSUPPORTED_PUBLIC_CIPHER:
            return(NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER);
        case NX_SECURE_X509_UNKNOWN_CERT_SIG_ALGORITHM:
            return(NX_SECURE_TLS_UNKNOWN_CERT_SIG_ALGORITHM);
        case NX_SECURE_X509_CERTIFICATE_SIG_CHECK_FAILED:
            return(NX_SECURE_TLS_CERTIFICATE_SIG_CHECK_FAILED);
#ifndef NX_SECURE_ALLOW_SELF_SIGNED_CERTIFICATES
        case NX_SECURE_X509_INVALID_SELF_SIGNED_CERT:
            return(NX_SECURE_TLS_INVALID_SELF_SIGNED_CERT);
#endif
        case NX_SECURE_X509_ISSUER_CERTIFICATE_NOT_FOUND:
            return(NX_SECURE_TLS_ISSUER_CERTIFICATE_NOT_FOUND);
        case NX_SECURE_X509_MISSING_CRYPTO_ROUTINE:
            return(NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE);
        default:
            return(status);
        }
    }

    return(status);
}