File: clicreds.hpp

package info (click to toggle)
openvpn3-client 24.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 19,384 kB
  • sloc: cpp: 180,128; python: 11,591; ansic: 1,878; sh: 1,767; java: 402; lisp: 81; makefile: 44
file content (271 lines) | stat: -rw-r--r-- 6,694 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
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
//    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-2022 OpenVPN Inc.
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Affero General Public License Version 3
//    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 Affero General Public License for more details.
//
//    You should have received a copy of the GNU Affero General Public License
//    along with this program in the COPYING file.
//    If not, see <http://www.gnu.org/licenses/>.

// This class encapsulates the state of authentication credentials
// maintained by an OpenVPN client.  It understands dynamic
// challenge/response cookies, and Session Token IDs (where the
// password in the object is wiped and replaced by a token used
// for further authentications).

#ifndef OPENVPN_CLIENT_CLICREDS_H
#define OPENVPN_CLIENT_CLICREDS_H

#include <string>

#include <openvpn/common/exception.hpp>
#include <openvpn/common/rc.hpp>
#include <openvpn/transport/protocol.hpp>
#include <openvpn/auth/cr.hpp>

namespace openvpn {

class ClientCreds : public RC<thread_unsafe_refcount>
{
  public:
    typedef RCPtr<ClientCreds> Ptr;

    ClientCreds() = default;

    void set_username(const std::string &username_arg)
    {
        username = username_arg;
    }

    void set_password(const std::string &password_arg)
    {
        password = password_arg;
        if (!password.empty())
        {
            password_needed_ = true;
        }
    }

    void set_http_proxy_username(const std::string &username)
    {
        http_proxy_user = username;
    }

    void set_http_proxy_password(const std::string &password)
    {
        http_proxy_pass = password;
    }

    void set_response(const std::string &response_arg)
    {
        response = response_arg;
        if (!response.empty())
        {
            need_user_interaction_ = true;
        }
    }

    void set_dynamic_challenge_cookie(const std::string &cookie, const std::string &username)
    {
        if (!cookie.empty())
            dynamic_challenge.reset(new ChallengeResponse(cookie, username));
    }

    void set_session_id(const std::string &user, const std::string &sess_id)
    {
        if (dynamic_challenge)
        {
            session_id_username = dynamic_challenge->get_username();
            // for dynamic challenge we use dynamic password only once
            dynamic_challenge.reset();
        }
        else if (!user.empty())
        {
            session_id_username = user;
        }

        // response is used only once
        response.clear();

        session_id = sess_id;
    }

    std::string get_username() const
    {
        if (dynamic_challenge)
            return dynamic_challenge->get_username();
        else if (!session_id_username.empty())
            return session_id_username;
        else
            return username;
    }

    std::string get_password() const
    {
        if (dynamic_challenge)
            return dynamic_challenge->construct_dynamic_password(response);
        else if (response.empty())
        {
            if (!session_id.empty())
                return session_id;
            else
                return password;
        }
        else
            return ChallengeResponse::construct_static_password(password, response);
    }

    std::string get_http_proxy_username() const
    {
        return http_proxy_user;
    }

    std::string get_http_proxy_password() const
    {
        return http_proxy_pass;
    }

    bool username_defined() const
    {
        return !username.empty();
    }

    bool password_defined() const
    {
        return !password.empty();
    }

    bool http_proxy_username_defined() const
    {
        return !http_proxy_user.empty();
    }

    bool http_proxy_password_defined() const
    {
        return !http_proxy_pass.empty();
    }

    bool session_id_defined() const
    {
        return !session_id.empty();
    }

    void purge_session_id()
    {
        OPENVPN_LOG("Clearing session-id");
        session_id.clear();
        session_id_username.clear();
    }

    void purge_user_pass()
    {
        OPENVPN_LOG("Clearing credentials");
        username.clear();
        password.clear();
    }

    void save_username_for_session_id()
    {
        if (session_id_username.empty())
        {
            session_id_username = username;
        }
    }

    void set_need_user_interaction()
    {
        need_user_interaction_ = true;
    }

    bool need_user_interaction() const
    {
        return need_user_interaction_;
    }

    bool password_needed() const
    {
        return password_needed_;
    }

    std::string auth_info() const
    {
        std::string ret;
        if (dynamic_challenge)
        {
            ret = "DynamicChallenge";
        }
        else if (response.empty())
        {
            if (!username.empty())
            {
                ret += "Username";
            }
            else if (!session_id_username.empty())
            {
                ret += "UsernameSessionId";
            }
            else
            {
                ret += "UsernameEmpty";
            }
            ret += '/';
            if (!session_id.empty())
            {
                ret += "SessionID";
            }
            else if (!password.empty())
            {
                ret += "Password";
            }
            else
            {
                ret += "PasswordEmpty";
            }
        }
        else
        {
            ret = "StaticChallenge";
        }
        return ret;
    }

  private:
    // Standard credentials
    std::string username;
    std::string password;

    // HTTP proxy credentials
    std::string http_proxy_user;
    std::string http_proxy_pass;

    std::string session_id;
    std::string session_id_username;

    // Response to a challenge
    std::string response;

    // Need user interaction to authenticate - such as static/dynamic challenge or SAML
    bool need_user_interaction_ = false;

    // Non-empty password provided
    bool password_needed_ = false;

    // Info describing a dynamic challenge
    ChallengeResponse::Ptr dynamic_challenge;
};

} // namespace openvpn

#endif