File: cred.c

package info (click to toggle)
gss 1.0.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,172 kB
  • sloc: ansic: 22,018; sh: 7,411; python: 2,873; perl: 861; makefile: 335; xml: 52; sed: 16
file content (247 lines) | stat: -rw-r--r-- 6,156 bytes parent folder | download | duplicates (3)
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
/* krb5/cred.c --- Kerberos 5 GSS-API credential management functions.
 * Copyright (C) 2003-2022 Simon Josefsson
 *
 * This file is part of the GNU Generic Security Service Library.
 *
 * This file is free software: you can redistribute it and/or modify
 * it under the terms of either:
 *
 *  * the GNU Lesser General Public License as published by the Free
 *    Software Foundation; either version 3 of the License, or (at
 *    your option) any later version.
 *
 * or
 *
 * * the GNU General Public License as published by the Free Software
 *   Foundation; either version 2 of the License, or (at your option)
 *   any later version.
 *
 * or both in parallel, as here.
 *
 * This file 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
 * General Public License for more details.
 *
 * You should have received copies of the GNU General Public License
 * and the GNU Lesser General Public License along with this file.  If
 * not, see <http://www.gnu.org/licenses/>.
 *
 */

/* Get specification. */
#include "k5internal.h"

static OM_uint32
acquire_cred1 (OM_uint32 * minor_status,
	       const gss_name_t desired_name,
	       OM_uint32 time_req,
	       const gss_OID_set desired_mechs,
	       gss_cred_usage_t cred_usage,
	       gss_cred_id_t * output_cred_handle,
	       gss_OID_set * actual_mechs, OM_uint32 * time_rec)
{
  gss_name_t name = desired_name;
  _gss_krb5_cred_t k5 = (*output_cred_handle)->krb5;
  OM_uint32 maj_stat;

  if (desired_name == GSS_C_NO_NAME)
    {
      gss_buffer_desc buf = { 4, (char *) "host" };

      maj_stat = gss_import_name (minor_status, &buf,
				  GSS_C_NT_HOSTBASED_SERVICE, &name);
      if (GSS_ERROR (maj_stat))
	return maj_stat;
    }

  maj_stat = gss_krb5_canonicalize_name (minor_status, name,
					 GSS_KRB5, &k5->peerptr);
  if (GSS_ERROR (maj_stat))
    return maj_stat;

  if (k5->peerptr == GSS_C_NO_NAME)
    {
      maj_stat = gss_release_name (minor_status, &name);
      if (GSS_ERROR (maj_stat))
	return maj_stat;
      return GSS_S_BAD_NAME;
    }

  if (shishi_init_server (&k5->sh) != SHISHI_OK)
    return GSS_S_FAILURE;

  {
    char *p;

    p = malloc (k5->peerptr->length + 1);
    if (!p)
      {
	if (minor_status)
	  *minor_status = ENOMEM;
	return GSS_S_FAILURE;
      }
    memcpy (p, k5->peerptr->value, k5->peerptr->length);
    p[k5->peerptr->length] = 0;

    k5->key = shishi_hostkeys_for_serverrealm (k5->sh, p, NULL);

    free (p);
  }

  if (!k5->key)
    {
      if (minor_status)
	*minor_status = GSS_KRB5_S_KG_KEYTAB_NOMATCH;
      return GSS_S_NO_CRED;
    }

  if (time_rec)
    *time_rec = GSS_C_INDEFINITE;

  return GSS_S_COMPLETE;
}

OM_uint32
gss_krb5_acquire_cred (OM_uint32 * minor_status,
		       const gss_name_t desired_name,
		       OM_uint32 time_req,
		       const gss_OID_set desired_mechs,
		       gss_cred_usage_t cred_usage,
		       gss_cred_id_t * output_cred_handle,
		       gss_OID_set * actual_mechs, OM_uint32 * time_rec)
{
  OM_uint32 maj_stat;
  gss_cred_id_t p = *output_cred_handle;

  p->krb5 = calloc (sizeof (*p->krb5), 1);
  if (!p->krb5)
    {
      if (minor_status)
	*minor_status = ENOMEM;
      return GSS_S_FAILURE;
    }

  if (actual_mechs)
    {
      maj_stat = gss_create_empty_oid_set (minor_status, actual_mechs);
      if (GSS_ERROR (maj_stat))
	{
	  free (p->krb5);
	  return maj_stat;
	}
      maj_stat = gss_add_oid_set_member (minor_status, GSS_KRB5,
					 actual_mechs);
      if (GSS_ERROR (maj_stat))
	{
	  free (p->krb5);
	  return maj_stat;
	}
    }

  maj_stat = acquire_cred1 (minor_status, desired_name, time_req,
			    desired_mechs, cred_usage,
			    &p, actual_mechs, time_rec);
  if (GSS_ERROR (maj_stat))
    {
      if (actual_mechs)
	gss_release_oid_set (NULL, actual_mechs);
      free (p->krb5);

      return maj_stat;
    }

  if (minor_status)
    *minor_status = 0;
  return GSS_S_COMPLETE;
}

static OM_uint32
inquire_cred (OM_uint32 * minor_status,
	      const gss_cred_id_t cred_handle,
	      gss_name_t * name,
	      OM_uint32 * lifetime,
	      gss_cred_usage_t * cred_usage, gss_OID_set * mechanisms)
{
  OM_uint32 maj_stat;

  if (cred_handle == GSS_C_NO_CREDENTIAL)
    return GSS_S_NO_CRED;

  if (mechanisms)
    {
      maj_stat = gss_create_empty_oid_set (minor_status, mechanisms);
      if (GSS_ERROR (maj_stat))
	return maj_stat;
      maj_stat = gss_add_oid_set_member (minor_status, GSS_KRB5, mechanisms);
      if (GSS_ERROR (maj_stat))
	return maj_stat;
    }

  if (name)
    {
      maj_stat = gss_duplicate_name (minor_status, cred_handle->krb5->peerptr,
				     name);
      if (GSS_ERROR (maj_stat))
	return maj_stat;
    }

  if (cred_usage)
    *cred_usage = GSS_C_BOTH;

  if (lifetime)
    *lifetime = GSS_C_INDEFINITE;

  if (minor_status)
    *minor_status = 0;
  return GSS_S_COMPLETE;
}

OM_uint32
gss_krb5_inquire_cred (OM_uint32 * minor_status,
		       const gss_cred_id_t cred_handle,
		       gss_name_t * name,
		       OM_uint32 * lifetime,
		       gss_cred_usage_t * cred_usage,
		       gss_OID_set * mechanisms)
{
  return inquire_cred (minor_status, cred_handle, name, lifetime,
		       cred_usage, mechanisms);
}

OM_uint32
gss_krb5_inquire_cred_by_mech (OM_uint32 * minor_status,
			       const gss_cred_id_t cred_handle,
			       const gss_OID mech_type,
			       gss_name_t * name,
			       OM_uint32 * initiator_lifetime,
			       OM_uint32 * acceptor_lifetime,
			       gss_cred_usage_t * cred_usage)
{
  OM_uint32 maj_stat;

  maj_stat = inquire_cred (minor_status, cred_handle, name,
			   initiator_lifetime, cred_usage, NULL);

  if (acceptor_lifetime)
    *acceptor_lifetime = *initiator_lifetime;

  return maj_stat;
}

OM_uint32
gss_krb5_release_cred (OM_uint32 * minor_status, gss_cred_id_t * cred_handle)
{
  _gss_krb5_cred_t k5 = (*cred_handle)->krb5;

  if (k5->peerptr != GSS_C_NO_NAME)
    gss_release_name (NULL, &k5->peerptr);

  shishi_key_done (k5->key);
  shishi_done (k5->sh);
  free (k5);

  if (minor_status)
    *minor_status = 0;
  return GSS_S_COMPLETE;
}