de6fa31afb151dc2924ac8a1646aa7aafe37a7ad
[metze/samba/wip.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 2 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, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "libcli/util/asn_1.h"
27 #include "auth/gensec/gensec.h"
28
29 /*
30   generate a krb5 GSS-API wrapper packet given a ticket
31 */
32 DATA_BLOB gensec_gssapi_gen_krb5_wrap(TALLOC_CTX *mem_ctx, const DATA_BLOB *ticket, const uint8_t tok_id[2])
33 {
34         struct asn1_data *data;
35         DATA_BLOB ret;
36
37         if (!data || !ticket->data) {
38                 return data_blob(NULL,0);
39         }
40
41         data = asn1_init(mem_ctx);
42         if (data == NULL) {
43                 return data_blob(NULL,0);
44         }
45
46         asn1_push_tag(data, ASN1_APPLICATION(0));
47         asn1_write_OID(data, GENSEC_OID_KERBEROS5);
48
49         asn1_write(data, tok_id, 2);
50         asn1_write(data, ticket->data, ticket->length);
51         asn1_pop_tag(data);
52
53         if (data->has_error) {
54                 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
55                 asn1_free(data);
56                 return data_blob(NULL,0);
57         }
58
59         ret = data_blob_talloc(mem_ctx, data->data, data->length);
60         asn1_free(data);
61
62         return ret;
63 }
64
65 /*
66   parse a krb5 GSS-API wrapper packet giving a ticket
67 */
68 BOOL gensec_gssapi_parse_krb5_wrap(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, DATA_BLOB *ticket, uint8_t tok_id[2])
69 {
70         BOOL ret;
71         struct asn1_data *data = asn1_init(mem_ctx);
72         int data_remaining;
73
74         if (!data) {
75                 return False;
76         }
77
78         asn1_load(data, *blob);
79         asn1_start_tag(data, ASN1_APPLICATION(0));
80         asn1_check_OID(data, GENSEC_OID_KERBEROS5);
81
82         data_remaining = asn1_tag_remaining(data);
83
84         if (data_remaining < 3) {
85                 data->has_error = True;
86         } else {
87                 asn1_read(data, tok_id, 2);
88                 data_remaining -= 2;
89                 *ticket = data_blob_talloc(mem_ctx, NULL, data_remaining);
90                 asn1_read(data, ticket->data, ticket->length);
91         }
92
93         asn1_end_tag(data);
94
95         ret = !data->has_error;
96
97         asn1_free(data);
98
99         return ret;
100 }
101
102
103 /*
104   check a GSS-API wrapper packet givin an expected OID
105 */
106 BOOL gensec_gssapi_check_oid(const DATA_BLOB *blob, const char *oid)
107 {
108         BOOL ret;
109         struct asn1_data *data = asn1_init(NULL);
110
111         if (!data) return False;
112
113         asn1_load(data, *blob);
114         asn1_start_tag(data, ASN1_APPLICATION(0));
115         asn1_check_OID(data, oid);
116
117         ret = !data->has_error;
118
119         asn1_free(data);
120
121         return ret;
122 }
123
124