Merge commit 'release-4-0-0alpha1' into v4-0-test
[kai/samba-autobuild/.git] / source4 / auth / kerberos / gssapi_parse.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    simple GSSAPI wrappers
5
6    Copyright (C) Andrew Tridgell 2001
7    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002
8    Copyright (C) Luke Howard     2003
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "libcli/util/asn_1.h"
26 #include "auth/gensec/gensec.h"
27
28 /*
29   generate a krb5 GSS-API wrapper packet given a ticket
30 */
31 DATA_BLOB gensec_gssapi_gen_krb5_wrap(TALLOC_CTX *mem_ctx, const DATA_BLOB *ticket, const uint8_t tok_id[2])
32 {
33         struct asn1_data *data;
34         DATA_BLOB ret;
35
36         if (!data || !ticket->data) {
37                 return data_blob(NULL,0);
38         }
39
40         data = asn1_init(mem_ctx);
41         if (data == NULL) {
42                 return data_blob(NULL,0);
43         }
44
45         asn1_push_tag(data, ASN1_APPLICATION(0));
46         asn1_write_OID(data, GENSEC_OID_KERBEROS5);
47
48         asn1_write(data, tok_id, 2);
49         asn1_write(data, ticket->data, ticket->length);
50         asn1_pop_tag(data);
51
52         if (data->has_error) {
53                 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
54                 asn1_free(data);
55                 return data_blob(NULL,0);
56         }
57
58         ret = data_blob_talloc(mem_ctx, data->data, data->length);
59         asn1_free(data);
60
61         return ret;
62 }
63
64 /*
65   parse a krb5 GSS-API wrapper packet giving a ticket
66 */
67 bool gensec_gssapi_parse_krb5_wrap(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, DATA_BLOB *ticket, uint8_t tok_id[2])
68 {
69         bool ret;
70         struct asn1_data *data = asn1_init(mem_ctx);
71         int data_remaining;
72
73         if (!data) {
74                 return false;
75         }
76
77         asn1_load(data, *blob);
78         asn1_start_tag(data, ASN1_APPLICATION(0));
79         asn1_check_OID(data, GENSEC_OID_KERBEROS5);
80
81         data_remaining = asn1_tag_remaining(data);
82
83         if (data_remaining < 3) {
84                 data->has_error = true;
85         } else {
86                 asn1_read(data, tok_id, 2);
87                 data_remaining -= 2;
88                 *ticket = data_blob_talloc(mem_ctx, NULL, data_remaining);
89                 asn1_read(data, ticket->data, ticket->length);
90         }
91
92         asn1_end_tag(data);
93
94         ret = !data->has_error;
95
96         asn1_free(data);
97
98         return ret;
99 }
100
101
102 /*
103   check a GSS-API wrapper packet givin an expected OID
104 */
105 bool gensec_gssapi_check_oid(const DATA_BLOB *blob, const char *oid)
106 {
107         bool ret;
108         struct asn1_data *data = asn1_init(NULL);
109
110         if (!data) return false;
111
112         asn1_load(data, *blob);
113         asn1_start_tag(data, ASN1_APPLICATION(0));
114         asn1_check_OID(data, oid);
115
116         ret = !data->has_error;
117
118         asn1_free(data);
119
120         return ret;
121 }
122
123