cifs-utils: fix some sparse warnings
[jlayton/cifs-utils.git] / spnego.c
1 /* 
2    Unix SMB/CIFS implementation.
3    simple kerberos5/SPNEGO routines
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002
6    Copyright (C) Luke Howard     2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <talloc.h>
23 #include <stdint.h>
24
25 #include "replace.h"
26 #include "data_blob.h"
27 #include "asn1.h"
28 #include "spnego.h"
29
30 /*
31   generate a krb5 GSS-API wrapper packet given a ticket
32 */
33 DATA_BLOB spnego_gen_krb5_wrap(const DATA_BLOB ticket, const uint8_t tok_id[2])
34 {
35         ASN1_DATA *data;
36         DATA_BLOB ret;
37         TALLOC_CTX *mem_ctx = talloc_init("gssapi");
38
39         data = asn1_init(mem_ctx);
40         if (data == NULL) {
41                 return data_blob_null;
42         }
43
44         asn1_push_tag(data, ASN1_APPLICATION(0));
45         asn1_write_OID(data, OID_KERBEROS5);
46
47         asn1_write(data, tok_id, 2);
48         asn1_write(data, ticket.data, ticket.length);
49         asn1_pop_tag(data);
50
51 #if 0
52         if (data->has_error) {
53                 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
54         }
55 #endif
56
57         ret = data_blob(data->data, data->length);
58         asn1_free(data);
59         talloc_free(mem_ctx);
60
61         return ret;
62 }
63
64 /*
65   Generate a negTokenInit as used by the client side ... It has a mechType
66   (OID), and a mechToken (a security blob) ... 
67
68   Really, we need to break out the NTLMSSP stuff as well, because it could be
69   raw in the packets!
70 */
71 DATA_BLOB gen_negTokenInit(const char *OID, DATA_BLOB blob)
72 {
73         ASN1_DATA *data;
74         DATA_BLOB ret;
75         TALLOC_CTX *mem_ctx = talloc_init("spnego");
76
77         data = asn1_init(mem_ctx);
78         if (data == NULL) {
79                 return data_blob_null;
80         }
81
82         asn1_push_tag(data, ASN1_APPLICATION(0));
83         asn1_write_OID(data,OID_SPNEGO);
84         asn1_push_tag(data, ASN1_CONTEXT(0));
85         asn1_push_tag(data, ASN1_SEQUENCE(0));
86
87         asn1_push_tag(data, ASN1_CONTEXT(0));
88         asn1_push_tag(data, ASN1_SEQUENCE(0));
89         asn1_write_OID(data, OID);
90         asn1_pop_tag(data);
91         asn1_pop_tag(data);
92
93         asn1_push_tag(data, ASN1_CONTEXT(2));
94         asn1_write_OctetString(data,blob.data,blob.length);
95         asn1_pop_tag(data);
96
97         asn1_pop_tag(data);
98         asn1_pop_tag(data);
99
100         asn1_pop_tag(data);
101
102 #if 0
103         if (data->has_error) {
104                 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
105         }
106 #endif
107
108         ret = data_blob(data->data, data->length);
109         asn1_free(data);
110         talloc_free(mem_ctx);
111
112         return ret;
113 }
114