gssapi-layer-c++  20230108
gsslayer.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright © 2023 by Andrey Afletdinov <public.irkutsk@gmail.com> *
3  * *
4  * https://github.com/AndreyBarmaley/gssapi-layer-cpp *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 3 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20  ***************************************************************************/
21 
22 #ifndef _GSS_LAYER_
23 #define _GSS_LAYER_
24 
25 #include <gssapi/gssapi.h>
26 #include <gssapi/gssapi_ext.h>
27 
28 #include <memory>
29 #include <vector>
30 #include <string>
31 #include <list>
32 
33 namespace Gss
34 {
35  enum class NameType
36  {
37  NoName,
38  NoOid,
39  NtAnonymous,
40  NtExportName,
41  NtHostService,
42  NtMachineUid,
43  NtStringUid,
45  };
46 
48  {
49  Initiate = GSS_C_INITIATE,
50  Accept = GSS_C_ACCEPT,
51  Both = GSS_C_BOTH
52  };
53 
55  {
56  Delegate = GSS_C_DELEG_FLAG,
57  Mutual = GSS_C_MUTUAL_FLAG,
58  Replay = GSS_C_REPLAY_FLAG,
59  Sequence = GSS_C_SEQUENCE_FLAG,
60  Confidential = GSS_C_CONF_FLAG,
61  Integrity = GSS_C_INTEG_FLAG,
62  Anonymous = GSS_C_ANON_FLAG,
63  Protection = GSS_C_PROT_READY_FLAG,
64  Transfer = GSS_C_TRANS_FLAG
65  };
66 
67  struct ErrorCodes
68  {
69  const char* func = nullptr;
70  OM_uint32 code1 = 0;
71  OM_uint32 code2 = 0;
72  };
73 
74  gss_name_t importName(std::string_view name, const NameType &, ErrorCodes* = nullptr);
75 
76  std::string exportName(const gss_name_t &, ErrorCodes* = nullptr);
77  std::string exportOID(const gss_OID &, ErrorCodes* = nullptr);
78 
79  std::list<ContextFlag> exportFlags(int);
80  const char* flagName(const ContextFlag &);
81 
82  std::string error2str(OM_uint32 code1, OM_uint32 code2);
83 
85  class Context
86  {
87  protected:
88  gss_OID mech_types = nullptr;
89  gss_ctx_id_t context_handle = nullptr;
90  gss_name_t src_name = nullptr;
91  gss_name_t service_name = nullptr;
92  gss_cred_id_t creds = nullptr;
93  OM_uint32 support_flags = 0;
94  OM_uint32 time_rec = 0;
95 
96  public:
97  Context() = default;
98  virtual ~Context();
99 
100  Context(const Context &) = delete;
101  Context & operator= (const Context &) = delete;
102 
103  virtual std::vector<uint8_t> recvToken(void) = 0;
104  virtual void sendToken(const void*, size_t) = 0;
105  virtual void error(const char* func, const char* subfunc, OM_uint32 code1, OM_uint32 code2) const;
106 
107  std::vector<uint8_t> recvMessage(void);
108  bool sendMessage(const void*, size_t, bool encrypt = true);
109 
110  bool recvMIC(const void*, size_t);
111  bool sendMIC(const void*, size_t);
112 
113  const gss_name_t & srcName(void) const { return src_name; }
114  const gss_OID & mechTypes(void) const { return mech_types; }
115  const OM_uint32 & supportFlags(void) const { return support_flags; }
116  const OM_uint32 & timeRec(void) const { return time_rec; }
117 
118  bool acquireCredential(std::string_view, const NameType &, const CredentialUsage & = Gss::CredentialUsage::Accept);
119 
120  std::list<std::string> mechNames(void) const;
121  };
122 
124  class ServiceContext : public Context
125  {
126  public:
127  ServiceContext() = default;
128 
129  bool acceptClient(void);
130  };
131 
133  class ClientContext : public Context
134  {
135  public:
136  ClientContext() = default;
137 
138  bool initConnect(std::string_view, const NameType &, int flags = GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG);
139  };
140 }
141 
142 #endif
Gss::Context::context_handle
gss_ctx_id_t context_handle
Definition: gsslayer.h:89
Gss::Context::recvToken
virtual std::vector< uint8_t > recvToken(void)=0
Gss::exportOID
std::string exportOID(const gss_OID &oid, ErrorCodes *err)
Definition: gsslayer.cpp:106
Gss::ErrorCodes::code2
OM_uint32 code2
Definition: gsslayer.h:71
Gss::Delegate
@ Delegate
delegated credentials are available by means of the delegated_cred_handle parameter
Definition: gsslayer.h:56
Gss::Context::sendMessage
bool sendMessage(const void *, size_t, bool encrypt=true)
Definition: gsslayer.cpp:218
Gss::Context::recvMIC
bool recvMIC(const void *, size_t)
Definition: gsslayer.cpp:242
Gss::Context::mechTypes
const gss_OID & mechTypes(void) const
Definition: gsslayer.h:114
Gss::Context::mechNames
std::list< std::string > mechNames(void) const
Definition: gsslayer.cpp:285
Gss::flagName
const char * flagName(const ContextFlag &flag)
Definition: gsslayer.cpp:128
Gss::ServiceContext::ServiceContext
ServiceContext()=default
Gss::Context::supportFlags
const OM_uint32 & supportFlags(void) const
Definition: gsslayer.h:115
Gss::Context::service_name
gss_name_t service_name
Definition: gsslayer.h:91
Gss::Both
@ Both
Identifies applications that can initiate or accept security contexts.
Definition: gsslayer.h:51
Gss::NameType
NameType
Definition: gsslayer.h:36
Gss::Transfer
@ Transfer
the resultant security context may be transferred to other processes by means of a call to gss_export...
Definition: gsslayer.h:64
Gss::ErrorCodes::code1
OM_uint32 code1
Definition: gsslayer.h:70
Gss::Integrity
@ Integrity
integrity service may be invoked by calling either the gss_get_mic(3GSS) or the gss_wrap(3GSS) routin...
Definition: gsslayer.h:61
Gss::Protection
@ Protection
the protection services specified by the states of GSS_C_CONF_FLAG and GSS_C_INTEG_FLAG are available...
Definition: gsslayer.h:63
Gss::Context::Context
Context()=default
Gss::error2str
std::string error2str(OM_uint32 code1, OM_uint32 code2)
Definition: gsslayer.cpp:29
Gss::Context::operator=
Context & operator=(const Context &)=delete
Gss::Context::~Context
virtual ~Context()
Definition: gsslayer.cpp:161
Gss::ErrorCodes
Definition: gsslayer.h:68
Gss::CredentialUsage
CredentialUsage
Definition: gsslayer.h:48
Gss::ErrorCodes::func
const char * func
Definition: gsslayer.h:69
Gss::Mutual
@ Mutual
a remote peer asked for mutual authentication
Definition: gsslayer.h:57
Gss::Context::sendMIC
bool sendMIC(const void *, size_t)
Definition: gsslayer.cpp:261
Gss::Context::sendToken
virtual void sendToken(const void *, size_t)=0
Gss::Replay
@ Replay
replay of protected messages will be detected
Definition: gsslayer.h:58
Gss::Context::timeRec
const OM_uint32 & timeRec(void) const
Definition: gsslayer.h:116
Gss::Accept
@ Accept
Identifies applications that only accept security contexts.
Definition: gsslayer.h:50
Gss::Context::recvMessage
std::vector< uint8_t > recvMessage(void)
Definition: gsslayer.cpp:193
Gss::ServiceContext::acceptClient
bool acceptClient(void)
Definition: gsslayer.cpp:351
Gss::Initiate
@ Initiate
Identifies applications that only initiate security contexts.
Definition: gsslayer.h:49
Gss::importName
gss_name_t importName(std::string_view name, const NameType &type, ErrorCodes *err)
Definition: gsslayer.cpp:48
Gss::ContextFlag
ContextFlag
Definition: gsslayer.h:55
Gss::NameType::NoName
@ NoName
Gss::ClientContext::initConnect
bool initConnect(std::string_view, const NameType &, int flags=GSS_C_MUTUAL_FLAG|GSS_C_REPLAY_FLAG)
Definition: gsslayer.cpp:391
Gss::Context
BaseContext.
Definition: gsslayer.h:86
Gss::ServiceContext
ServiceContext.
Definition: gsslayer.h:125
Gss::exportFlags
std::list< ContextFlag > exportFlags(int flags)
Definition: gsslayer.cpp:147
Gss::Context::error
virtual void error(const char *func, const char *subfunc, OM_uint32 code1, OM_uint32 code2) const
Definition: gsslayer.cpp:188
Gss::ClientContext
ClientContext.
Definition: gsslayer.h:134
Gss::Context::support_flags
OM_uint32 support_flags
Definition: gsslayer.h:93
Gss::Context::creds
gss_cred_id_t creds
Definition: gsslayer.h:92
Gss::Context::Context
Context(const Context &)=delete
Gss::Context::src_name
gss_name_t src_name
Definition: gsslayer.h:90
Gss::Confidential
@ Confidential
confidentiality service may be invoked by calling the gss_wrap() routine
Definition: gsslayer.h:60
Gss::Context::acquireCredential
bool acquireCredential(std::string_view, const NameType &, const CredentialUsage &=Gss::CredentialUsage::Accept)
Definition: gsslayer.cpp:319
Gss
Definition: gsslayer.cpp:28
Gss::Sequence
@ Sequence
out of sequence protected messages will be detected
Definition: gsslayer.h:59
Gss::exportName
std::string exportName(const gss_name_t &name, ErrorCodes *err)
Definition: gsslayer.cpp:84
Gss::Context::srcName
const gss_name_t & srcName(void) const
Definition: gsslayer.h:113
Gss::Context::time_rec
OM_uint32 time_rec
Definition: gsslayer.h:94
Gss::Anonymous
@ Anonymous
the initiator does not wish to be authenticated. The src_name parameter, if requested,...
Definition: gsslayer.h:62
Gss::ClientContext::ClientContext
ClientContext()=default
Gss::Context::mech_types
gss_OID mech_types
Definition: gsslayer.h:88