source: branches/samba-3.0/source/libsmb/clispnego.c@ 710

Last change on this file since 710 was 1, checked in by Paul Smedley, 19 years ago

Initial code import

File size: 12.9 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 simple kerberos5/SPNEGO routines
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Jim McDonough <[email protected]> 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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#include "includes.h"
24
25/*
26 generate a negTokenInit packet given a GUID, a list of supported
27 OIDs (the mechanisms) and a principal name string
28*/
29DATA_BLOB spnego_gen_negTokenInit(char guid[16],
30 const char *OIDs[],
31 const char *principal)
32{
33 int i;
34 ASN1_DATA data;
35 DATA_BLOB ret;
36
37 memset(&data, 0, sizeof(data));
38
39 asn1_write(&data, guid, 16);
40 asn1_push_tag(&data,ASN1_APPLICATION(0));
41 asn1_write_OID(&data,OID_SPNEGO);
42 asn1_push_tag(&data,ASN1_CONTEXT(0));
43 asn1_push_tag(&data,ASN1_SEQUENCE(0));
44
45 asn1_push_tag(&data,ASN1_CONTEXT(0));
46 asn1_push_tag(&data,ASN1_SEQUENCE(0));
47 for (i=0; OIDs[i]; i++) {
48 asn1_write_OID(&data,OIDs[i]);
49 }
50 asn1_pop_tag(&data);
51 asn1_pop_tag(&data);
52
53 asn1_push_tag(&data, ASN1_CONTEXT(3));
54 asn1_push_tag(&data, ASN1_SEQUENCE(0));
55 asn1_push_tag(&data, ASN1_CONTEXT(0));
56 asn1_write_GeneralString(&data,principal);
57 asn1_pop_tag(&data);
58 asn1_pop_tag(&data);
59 asn1_pop_tag(&data);
60
61 asn1_pop_tag(&data);
62 asn1_pop_tag(&data);
63
64 asn1_pop_tag(&data);
65
66 if (data.has_error) {
67 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data.ofs));
68 asn1_free(&data);
69 }
70
71 ret = data_blob(data.data, data.length);
72 asn1_free(&data);
73
74 return ret;
75}
76
77/*
78 Generate a negTokenInit as used by the client side ... It has a mechType
79 (OID), and a mechToken (a security blob) ...
80
81 Really, we need to break out the NTLMSSP stuff as well, because it could be
82 raw in the packets!
83*/
84DATA_BLOB gen_negTokenInit(const char *OID, DATA_BLOB blob)
85{
86 ASN1_DATA data;
87 DATA_BLOB ret;
88
89 memset(&data, 0, sizeof(data));
90
91 asn1_push_tag(&data, ASN1_APPLICATION(0));
92 asn1_write_OID(&data,OID_SPNEGO);
93 asn1_push_tag(&data, ASN1_CONTEXT(0));
94 asn1_push_tag(&data, ASN1_SEQUENCE(0));
95
96 asn1_push_tag(&data, ASN1_CONTEXT(0));
97 asn1_push_tag(&data, ASN1_SEQUENCE(0));
98 asn1_write_OID(&data, OID);
99 asn1_pop_tag(&data);
100 asn1_pop_tag(&data);
101
102 asn1_push_tag(&data, ASN1_CONTEXT(2));
103 asn1_write_OctetString(&data,blob.data,blob.length);
104 asn1_pop_tag(&data);
105
106 asn1_pop_tag(&data);
107 asn1_pop_tag(&data);
108
109 asn1_pop_tag(&data);
110
111 if (data.has_error) {
112 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data.ofs));
113 asn1_free(&data);
114 }
115
116 ret = data_blob(data.data, data.length);
117 asn1_free(&data);
118
119 return ret;
120}
121
122/*
123 parse a negTokenInit packet giving a GUID, a list of supported
124 OIDs (the mechanisms) and a principal name string
125*/
126BOOL spnego_parse_negTokenInit(DATA_BLOB blob,
127 char *OIDs[ASN1_MAX_OIDS],
128 char **principal)
129{
130 int i;
131 BOOL ret;
132 ASN1_DATA data;
133
134 asn1_load(&data, blob);
135
136 asn1_start_tag(&data,ASN1_APPLICATION(0));
137 asn1_check_OID(&data,OID_SPNEGO);
138 asn1_start_tag(&data,ASN1_CONTEXT(0));
139 asn1_start_tag(&data,ASN1_SEQUENCE(0));
140
141 asn1_start_tag(&data,ASN1_CONTEXT(0));
142 asn1_start_tag(&data,ASN1_SEQUENCE(0));
143 for (i=0; asn1_tag_remaining(&data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
144 char *oid_str = NULL;
145 asn1_read_OID(&data,&oid_str);
146 OIDs[i] = oid_str;
147 }
148 OIDs[i] = NULL;
149 asn1_end_tag(&data);
150 asn1_end_tag(&data);
151
152 *principal = NULL;
153 if (asn1_tag_remaining(&data) > 0) {
154 asn1_start_tag(&data, ASN1_CONTEXT(3));
155 asn1_start_tag(&data, ASN1_SEQUENCE(0));
156 asn1_start_tag(&data, ASN1_CONTEXT(0));
157 asn1_read_GeneralString(&data,principal);
158 asn1_end_tag(&data);
159 asn1_end_tag(&data);
160 asn1_end_tag(&data);
161 }