summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/sspi/sspi_gss.c
blob: 749973d20a8a765c8617589fad293f091e1628cd (plain)
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
/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * Generic Security Service Application Program Interface (GSSAPI)
 *
 * Copyright 2015 ANSSI, Author Thomas Calderon
 * Copyright 2015 Marc-Andre Moreau <marcandre.moreau@gmail.com>
 * Copyright 2017 Dorian Ducournau <dorian.ducournau@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <winpr/crt.h>
#include <winpr/assert.h>
#include <winpr/endian.h>
#include <winpr/asn1.h>
#include <winpr/stream.h>

#include "sspi_gss.h"

BOOL sspi_gss_wrap_token(SecBuffer* buf, const WinPrAsn1_OID* oid, uint16_t tok_id,
                         const sspi_gss_data* token)
{
	WinPrAsn1Encoder* enc = NULL;
	BYTE tok_id_buf[2];
	WinPrAsn1_MemoryChunk mc = { 2, tok_id_buf };
	wStream s;
	size_t len = 0;
	BOOL ret = FALSE;

	WINPR_ASSERT(buf);
	WINPR_ASSERT(oid);
	WINPR_ASSERT(token);

	Data_Write_UINT16_BE(tok_id_buf, tok_id);

	enc = WinPrAsn1Encoder_New(WINPR_ASN1_DER);
	if (!enc)
		return FALSE;

	/* initialContextToken [APPLICATION 0] */
	if (!WinPrAsn1EncAppContainer(enc, 0))
		goto cleanup;

	/* thisMech OID */
	if (!WinPrAsn1EncOID(enc, oid))
		goto cleanup;

	/* TOK_ID */
	if (!WinPrAsn1EncRawContent(enc, &mc))
		goto cleanup;

	/* innerToken */
	mc.data = (BYTE*)token->data;
	mc.len = token->length;
	if (!WinPrAsn1EncRawContent(enc, &mc))
		goto cleanup;

	if (!WinPrAsn1EncEndContainer(enc))
		goto cleanup;

	if (!WinPrAsn1EncStreamSize(enc, &len) || len > buf->cbBuffer)
		goto cleanup;

	Stream_StaticInit(&s, buf->pvBuffer, len);
	if (WinPrAsn1EncToStream(enc, &s))
	{
		buf->cbBuffer = len;
		ret = TRUE;
	}

cleanup:
	WinPrAsn1Encoder_Free(&enc);
	return ret;
}

BOOL sspi_gss_unwrap_token(const SecBuffer* buf, WinPrAsn1_OID* oid, uint16_t* tok_id,
                           sspi_gss_data* token)
{
	WinPrAsn1Decoder dec;
	WinPrAsn1Decoder dec2;
	WinPrAsn1_tagId tag = 0;
	wStream sbuffer = { 0 };
	wStream* s = NULL;

	WINPR_ASSERT(buf);
	WINPR_ASSERT(oid);
	WINPR_ASSERT(token);

	WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, buf->pvBuffer, buf->cbBuffer);

	if (!WinPrAsn1DecReadApp(&dec, &tag, &dec2) || tag != 0)
		return FALSE;

	if (!WinPrAsn1DecReadOID(&dec2, oid, FALSE))
		return FALSE;

	sbuffer = WinPrAsn1DecGetStream(&dec2);
	s = &sbuffer;

	if (Stream_Length(s) < 2)
		return FALSE;

	if (tok_id)
		Stream_Read_INT16_BE(s, *tok_id);

	token->data = Stream_Pointer(s);
	token->length = (UINT)Stream_GetRemainingLength(s);

	return TRUE;
}