4a80e1d79920d37839bb9a0838fecc52c8acc168
[bbaumbach/samba-autobuild/.git] / source4 / libcli / auth / 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
27 /*
28   generate a krb5 GSS-API wrapper packet given a ticket
29 */
30 DATA_BLOB gensec_gssapi_gen_krb5_wrap(TALLOC_CTX *mem_ctx, const DATA_BLOB *ticket, const uint8 tok_id[2])
31 {
32         ASN1_DATA data;
33         DATA_BLOB ret;
34
35         ZERO_STRUCT(data);
36
37         asn1_push_tag(&data, ASN1_APPLICATION(0));
38         asn1_write_OID(&data, OID_KERBEROS5);
39
40         asn1_write(&data, tok_id, 2);
41         asn1_write(&data, ticket->data, ticket->length);
42         asn1_pop_tag(&data);
43
44         if (data.has_error) {
45                 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data.ofs));
46                 asn1_free(&data);
47         }
48
49         ret = data_blob_talloc(mem_ctx, data.data, data.length);
50         asn1_free(&data);
51
52         return ret;
53 }
54
55 /*
56   parse a krb5 GSS-API wrapper packet giving a ticket
57 */
58 BOOL gensec_gssapi_parse_krb5_wrap(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, DATA_BLOB *ticket, uint8 tok_id[2])
59 {
60         BOOL ret;
61         ASN1_DATA data;
62         int data_remaining;
63
64         asn1_load(&data, *blob);
65         asn1_start_tag(&data, ASN1_APPLICATION(0));
66         asn1_check_OID(&data, OID_KERBEROS5);
67
68         data_remaining = asn1_tag_remaining(&data);
69
70         if (data_remaining < 3) {
71                 data.has_error = True;
72         } else {
73                 asn1_read(&data, tok_id, 2);
74                 data_remaining -= 2;
75                 *ticket = data_blob_talloc(mem_ctx, NULL, data_remaining);
76                 asn1_read(&data, ticket->data, ticket->length);
77         }
78
79         asn1_end_tag(&data);
80
81         ret = !data.has_error;
82
83         asn1_free(&data);
84
85         return ret;
86 }
87
88