File: util_moonshot.c

package info (click to toggle)
moonshot-gss-eap 0.9.2-3+deb8u1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,456 kB
  • ctags: 5,386
  • sloc: ansic: 33,657; sh: 16,977; cpp: 3,582; makefile: 363
file content (273 lines) | stat: -rw-r--r-- 9,273 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright (c) 2011, JANET(UK)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of JANET(UK) nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "gssapiP_eap.h"

#ifdef HAVE_MOONSHOT_GET_IDENTITY
#include <libmoonshot.h>

static OM_uint32
libMoonshotMapError(OM_uint32 *minor,
                    MoonshotError **pError)
{
    MoonshotError *error = *pError;

    GSSEAP_ASSERT(error != NULL);

    switch (error->code) {
    case MOONSHOT_ERROR_UNABLE_TO_START_SERVICE:
        *minor = GSSEAP_UNABLE_TO_START_IDENTITY_SERVICE;
        break;
    case MOONSHOT_ERROR_NO_IDENTITY_SELECTED:
        *minor = GSSEAP_NO_IDENTITY_SELECTED;
        break;
    case MOONSHOT_ERROR_INSTALLATION_ERROR:
        *minor = GSSEAP_IDENTITY_SERVICE_INSTALL_ERROR;
        break;
    case MOONSHOT_ERROR_OS_ERROR:
        *minor = GSSEAP_IDENTITY_SERVICE_OS_ERROR;
        break;
    case MOONSHOT_ERROR_IPC_ERROR:
        *minor = GSSEAP_IDENTITY_SERVICE_IPC_ERROR;
        break;
    default:
        *minor = GSSEAP_IDENTITY_SERVICE_UNKNOWN_ERROR;
        break;
    }

    gssEapSaveStatusInfo(*minor, error->message);
    moonshot_error_free(error);
    *pError = NULL;

    return GSS_S_CRED_UNAVAIL;
}

OM_uint32
libMoonshotResolveDefaultIdentity(OM_uint32 *minor,
                                  const gss_cred_id_t cred,
                                  gss_name_t *pName)
{
    OM_uint32 major, tmpMinor;
    gss_OID nameMech = gssEapPrimaryMechForCred(cred);
    gss_name_t name = GSS_C_NO_NAME;
    gss_buffer_desc tmpBuffer = GSS_C_EMPTY_BUFFER;
    char *nai = NULL;
    char *password = NULL;
    char *serverCertificateHash = NULL;
    char *caCertificate = NULL;
    char *subjectNameConstraint = NULL;
    char *subjectAltNameConstraint = NULL;
    MoonshotError *error = NULL;

    *pName = GSS_C_NO_NAME;

    if (!moonshot_get_default_identity(&nai,
                                       &password,
                                       &serverCertificateHash,
                                       &caCertificate,
                                       &subjectNameConstraint,
                                       &subjectAltNameConstraint,
                                       &error)) {
        if (error->code == MOONSHOT_ERROR_NO_IDENTITY_SELECTED) {
            major = GSS_S_CRED_UNAVAIL;
            *minor = GSSEAP_NO_DEFAULT_IDENTITY;
            moonshot_error_free(error);
        } else
            major = libMoonshotMapError(minor, &error);
        goto cleanup;
    }

    tmpBuffer.value = nai;
    tmpBuffer.length = strlen(nai);

    major = gssEapImportName(minor, &tmpBuffer, GSS_C_NT_USER_NAME, nameMech, &name);
    if (GSS_ERROR(major))
        goto cleanup;

    *pName = name;
    name = GSS_C_NO_NAME;

cleanup:
    moonshot_free(nai);
    moonshot_free(password);
    moonshot_free(serverCertificateHash);
    moonshot_free(caCertificate);
    moonshot_free(subjectNameConstraint);
    moonshot_free(subjectAltNameConstraint);

    gssEapReleaseName(&tmpMinor, &name);

    return major;
}

static int stringEmpty(const char * s)
{
    if (s == NULL)
      return 1;
    if (strlen(s) > 0)
	return 0;
    return 1;
}

OM_uint32
libMoonshotResolveInitiatorCred(OM_uint32 *minor,
                                gss_cred_id_t cred,
                                const gss_name_t targetName)
{
    OM_uint32 major, tmpMinor;
    gss_OID nameMech = gssEapPrimaryMechForCred(cred);
    gss_buffer_desc initiator = GSS_C_EMPTY_BUFFER;
    gss_buffer_desc target = GSS_C_EMPTY_BUFFER;
    gss_buffer_desc tmpBuffer = GSS_C_EMPTY_BUFFER;
    char *nai = NULL;
    char *password = NULL;
    char *serverCertificateHash = NULL;
    char *caCertificate = NULL;
    char *subjectNameConstraint = NULL;
    char *subjectAltNameConstraint = NULL;
    MoonshotError *error = NULL;

    if (cred->name != GSS_C_NO_NAME) {
        major = gssEapDisplayName(minor, cred->name, &initiator, NULL);
        if (GSS_ERROR(major))
            goto cleanup;
    }

    if (targetName != GSS_C_NO_NAME) {
        major = gssEapDisplayName(minor, targetName, &target, NULL);
        if (GSS_ERROR(major))
            goto cleanup;
    }

    if (!moonshot_get_identity((const char *)initiator.value,
                               (const char *)cred->password.value,
                               (const char *)target.value,
                               &nai,
                               &password,
                               &serverCertificateHash,
                               &caCertificate,
                               &subjectNameConstraint,
                               &subjectAltNameConstraint,
                               &error)) {
        major = libMoonshotMapError(minor, &error);
        goto cleanup;
    }

    gssEapReleaseName(&tmpMinor, &cred->name);

    tmpBuffer.value = nai;
    tmpBuffer.length = strlen(nai);

    major = gssEapImportName(minor, &tmpBuffer, GSS_C_NT_USER_NAME,
                             nameMech, &cred->name);
    if (GSS_ERROR(major))
        goto cleanup;

    tmpBuffer.value = password;
    tmpBuffer.length = strlen(password);

    major = gssEapSetCredPassword(minor, cred, &tmpBuffer);
    if (GSS_ERROR(major))
        goto cleanup;

    gss_release_buffer(&tmpMinor, &cred->caCertificate);
    gss_release_buffer(&tmpMinor, &cred->caCertificateBlob);
    gss_release_buffer(&tmpMinor, &cred->subjectNameConstraint);
    gss_release_buffer(&tmpMinor, &cred->subjectAltNameConstraint);

    if (!stringEmpty(serverCertificateHash)) {
        size_t len = strlen(serverCertificateHash);

        #define HASH_PREFIX             "hash://server/sha256/"
        #define HASH_PREFIX_LEN         (sizeof(HASH_PREFIX) - 1)

        cred->caCertificate.value = GSSEAP_MALLOC(HASH_PREFIX_LEN + len + 1);
        if (cred->caCertificate.value == NULL) {
            major = GSS_S_FAILURE;
            *minor = ENOMEM;
            goto cleanup;
        }

        memcpy(cred->caCertificate.value, HASH_PREFIX, HASH_PREFIX_LEN);
        memcpy((char *)cred->caCertificate.value + HASH_PREFIX_LEN, serverCertificateHash, len);

        ((char *)cred->caCertificate.value)[HASH_PREFIX_LEN + len] = '\0';

        cred->caCertificate.length = HASH_PREFIX_LEN + len;
    } else if (!stringEmpty(caCertificate)) {
        void *blobData;
        ssize_t blobLength;
        ssize_t maxLength = ((strlen(caCertificate) + 3) / 4) * 3;
        if (maxLength < 3) {
            major = GSS_S_FAILURE;
            *minor = GSSEAP_BAD_CACERTIFICATE;
            goto cleanup;
        }
        blobData = GSSEAP_MALLOC(maxLength);
        if (blobData == NULL) {
            major = GSS_S_FAILURE;
            *minor = ENOMEM;
            goto cleanup;
        }

        blobLength = base64Decode(caCertificate, blobData);

        if (blobLength <= 0) {
            major = GSS_S_DEFECTIVE_CREDENTIAL;
            *minor = GSSEAP_BAD_CACERTIFICATE;
            GSSEAP_FREE(blobData);
            goto cleanup;
        }
        cred->caCertificateBlob.value = blobData;
        cred->caCertificateBlob.length = blobLength;
        makeStringBufferOrCleanup("blob://ca-cert", &cred->caCertificate);
    }

    if (!stringEmpty(subjectNameConstraint))
        makeStringBufferOrCleanup(subjectNameConstraint, &cred->subjectNameConstraint);
    if (!stringEmpty(subjectAltNameConstraint))
        makeStringBufferOrCleanup(subjectAltNameConstraint, &cred->subjectAltNameConstraint);

cleanup:
    moonshot_free(nai);
    moonshot_free(password);
    moonshot_free(serverCertificateHash);
    moonshot_free(caCertificate);
    moonshot_free(subjectNameConstraint);
    moonshot_free(subjectAltNameConstraint);

    gss_release_buffer(&tmpMinor, &initiator);
    gss_release_buffer(&tmpMinor, &target);

    return major;
}
#endif /* HAVE_MOONSHOT_GET_IDENTITY */