File: verify_x509_name.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (128 lines) | stat: -rw-r--r-- 3,635 bytes parent folder | download
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
//    OpenVPN -- An application to securely tunnel IP networks
//               over a single port, with support for SSL/TLS-based
//               session authentication and key exchange,
//               packet encryption, packet authentication, and
//               packet compression.
//
//    Copyright (C) 2012- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//
//

#pragma once

#include <openvpn/common/options.hpp>

namespace openvpn {

/**
 *  Parses the --verify-x509-name configuration option
 *  and provides the logic to validate an X.509 certificate subject
 *  against such an option.
 */
class VerifyX509Name
{
  public:
    enum Mode
    {
        VERIFY_X509_NONE = 0,
        VERIFY_X509_SUBJECT_DN = 1,
        VERIFY_X509_SUBJECT_RDN = 2,
        VERIFY_X509_SUBJECT_RDN_PREFIX = 3
    };

    VerifyX509Name() = default;

    VerifyX509Name(const OptionList &opt, const std::string &relay_prefix = "")
    {
        init(opt, relay_prefix);
    }

    ~VerifyX509Name() = default;

    void init(const OptionList &opt, const std::string &relay_prefix)
    {
        const Option *o = opt.get_ptr(relay_prefix + "verify-x509-name");
        if (o)
        {
            o->min_args(1);
            verify_value = o->get(1, 256);
            // If the mode flag is not present, we default to subject.
            // For details, see openvpn(8) man page.
            mode = parse_x509_verify_mode(o->get_default(2, 256, "subject"));
        }
    }

    std::string get_mode_str() const
    {
        switch (mode)
        {
        case VERIFY_X509_NONE:
            return "VERIFY_X509_NONE";
        case VERIFY_X509_SUBJECT_DN:
            return "VERIFY_X509_SUBJECT_DN";
        case VERIFY_X509_SUBJECT_RDN:
            return "VERIFY_X509_SUBJECT_RDN";
        case VERIFY_X509_SUBJECT_RDN_PREFIX:
            return "VERIFY_X509_SUBJECT_RDN_PREFIX";
        default:
            return "VERIFY_X509_NONE";
        }
    }

    Mode get_mode() const
    {
        return mode;
    }

    bool verify(const std::string &value) const
    {
        switch (mode)
        {
        case VERIFY_X509_NONE:
            // If no verification is configured, it is always a pass
            return true;

        case VERIFY_X509_SUBJECT_DN:
            // The input value is expected to be a full subject DN
            // where a perfect match is expected
            return verify_value == value;

        case VERIFY_X509_SUBJECT_RDN:
            // The input value is expected to be the certificate
            // Common Name (CN), and a perfect patch is expected
            return verify_value == value;

        case VERIFY_X509_SUBJECT_RDN_PREFIX:
            // The input value contains a prefix of the certificate
            // Common Name (CN), where we only require a perfect match
            // only on the matching prefix
            return value.compare(0, verify_value.length(), verify_value) == 0;
        }
        return false;
    }

  private:
    Mode mode = VERIFY_X509_NONE;
    std::string verify_value;

    static Mode parse_x509_verify_mode(const std::string &type)
    {
        if (type == "subject")
        {
            return VERIFY_X509_SUBJECT_DN;
        }
        else if (type == "name")
        {
            return VERIFY_X509_SUBJECT_RDN;
        }
        else if (type == "name-prefix")
        {
            return VERIFY_X509_SUBJECT_RDN_PREFIX;
        }
        throw option_error(ERR_INVALID_OPTION_CRYPTO, "Invalid verify-x509-name type: " + type);
    }

}; // class VerifyX509Name
} // namespace openvpn