/src/FreeRDP/winpr/libwinpr/sspi/sspi_gss.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Generic Security Service Application Program Interface (GSSAPI) |
4 | | * |
5 | | * Copyright 2015 ANSSI, Author Thomas Calderon |
6 | | * Copyright 2015 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
7 | | * Copyright 2017 Dorian Ducournau <dorian.ducournau@gmail.com> |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <winpr/crt.h> |
23 | | #include <winpr/assert.h> |
24 | | #include <winpr/endian.h> |
25 | | #include <winpr/asn1.h> |
26 | | #include <winpr/stream.h> |
27 | | |
28 | | #include "sspi_gss.h" |
29 | | |
30 | | BOOL sspi_gss_wrap_token(SecBuffer* buf, const WinPrAsn1_OID* oid, uint16_t tok_id, |
31 | | const sspi_gss_data* token) |
32 | 0 | { |
33 | 0 | WinPrAsn1Encoder* enc = NULL; |
34 | 0 | BYTE tok_id_buf[2]; |
35 | 0 | WinPrAsn1_MemoryChunk mc = { 2, tok_id_buf }; |
36 | 0 | wStream s; |
37 | 0 | size_t len = 0; |
38 | 0 | BOOL ret = FALSE; |
39 | |
|
40 | 0 | WINPR_ASSERT(buf); |
41 | 0 | WINPR_ASSERT(oid); |
42 | 0 | WINPR_ASSERT(token); |
43 | | |
44 | 0 | winpr_Data_Write_UINT16_BE(tok_id_buf, tok_id); |
45 | |
|
46 | 0 | enc = WinPrAsn1Encoder_New(WINPR_ASN1_DER); |
47 | 0 | if (!enc) |
48 | 0 | return FALSE; |
49 | | |
50 | | /* initialContextToken [APPLICATION 0] */ |
51 | 0 | if (!WinPrAsn1EncAppContainer(enc, 0)) |
52 | 0 | goto cleanup; |
53 | | |
54 | | /* thisMech OID */ |
55 | 0 | if (!WinPrAsn1EncOID(enc, oid)) |
56 | 0 | goto cleanup; |
57 | | |
58 | | /* TOK_ID */ |
59 | 0 | if (!WinPrAsn1EncRawContent(enc, &mc)) |
60 | 0 | goto cleanup; |
61 | | |
62 | | /* innerToken */ |
63 | 0 | mc.data = (BYTE*)token->data; |
64 | 0 | mc.len = token->length; |
65 | 0 | if (!WinPrAsn1EncRawContent(enc, &mc)) |
66 | 0 | goto cleanup; |
67 | | |
68 | 0 | if (!WinPrAsn1EncEndContainer(enc)) |
69 | 0 | goto cleanup; |
70 | | |
71 | 0 | if (!WinPrAsn1EncStreamSize(enc, &len) || len > buf->cbBuffer) |
72 | 0 | goto cleanup; |
73 | | |
74 | 0 | Stream_StaticInit(&s, buf->pvBuffer, len); |
75 | 0 | if (WinPrAsn1EncToStream(enc, &s)) |
76 | 0 | { |
77 | 0 | buf->cbBuffer = (UINT32)len; |
78 | 0 | ret = TRUE; |
79 | 0 | } |
80 | |
|
81 | 0 | cleanup: |
82 | 0 | WinPrAsn1Encoder_Free(&enc); |
83 | 0 | return ret; |
84 | 0 | } |
85 | | |
86 | | BOOL sspi_gss_unwrap_token(const SecBuffer* buf, WinPrAsn1_OID* oid, uint16_t* tok_id, |
87 | | sspi_gss_data* token) |
88 | 0 | { |
89 | 0 | WinPrAsn1Decoder dec; |
90 | 0 | WinPrAsn1Decoder dec2; |
91 | 0 | WinPrAsn1_tagId tag = 0; |
92 | 0 | wStream sbuffer = { 0 }; |
93 | 0 | wStream* s = NULL; |
94 | |
|
95 | 0 | WINPR_ASSERT(buf); |
96 | 0 | WINPR_ASSERT(oid); |
97 | 0 | WINPR_ASSERT(token); |
98 | | |
99 | 0 | WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, buf->pvBuffer, buf->cbBuffer); |
100 | |
|
101 | 0 | if (!WinPrAsn1DecReadApp(&dec, &tag, &dec2) || tag != 0) |
102 | 0 | return FALSE; |
103 | | |
104 | 0 | if (!WinPrAsn1DecReadOID(&dec2, oid, FALSE)) |
105 | 0 | return FALSE; |
106 | | |
107 | 0 | sbuffer = WinPrAsn1DecGetStream(&dec2); |
108 | 0 | s = &sbuffer; |
109 | |
|
110 | 0 | if (Stream_Length(s) < 2) |
111 | 0 | return FALSE; |
112 | | |
113 | 0 | if (tok_id) |
114 | 0 | Stream_Read_UINT16_BE(s, *tok_id); |
115 | |
|
116 | 0 | token->data = Stream_Pointer(s); |
117 | 0 | token->length = (UINT)Stream_GetRemainingLength(s); |
118 | |
|
119 | 0 | return TRUE; |
120 | 0 | } |