d35d9fd4fc01083a34e689db3460af0f2d1cfaa1
[gd/samba-autobuild/.git] / source4 / torture / rpc / session_key.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for lsa rpc operations
4
5    Copyright (C) Andrew Tridgell 2003
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
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 "includes.h"
23 #include "torture/torture.h"
24 #include "librpc/gen_ndr/ndr_lsa_c.h"
25
26 #include "libcli/auth/libcli_auth.h"
27 #include "torture/rpc/rpc.h"
28 #include "lib/cmdline/popt_common.h"
29
30 static void init_lsa_String(struct lsa_String *name, const char *s)
31 {
32         name->string = s;
33 }
34
35 static bool test_CreateSecret_basic(struct dcerpc_pipe *p, 
36                                     struct torture_context *tctx,
37                                     struct policy_handle *handle)
38 {
39         NTSTATUS status;
40         struct lsa_CreateSecret r;
41         struct lsa_SetSecret r3;
42         struct lsa_QuerySecret r4;
43         struct policy_handle sec_handle;
44         struct lsa_Delete d;
45         struct lsa_DATA_BUF buf1;
46         struct lsa_DATA_BUF_PTR bufp1;
47         DATA_BLOB enc_key;
48         DATA_BLOB session_key;
49         NTTIME old_mtime, new_mtime;
50         DATA_BLOB blob1, blob2;
51         const char *secret1 = "abcdef12345699qwerty";
52         char *secret2;
53         char *secname;
54
55         secname = talloc_asprintf(tctx, "torturesecret-%u", (uint_t)random());
56
57         torture_comment(tctx, "Testing CreateSecret of %s\n", secname);
58                 
59         init_lsa_String(&r.in.name, secname);
60         
61         r.in.handle = handle;
62         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
63         r.out.sec_handle = &sec_handle;
64         
65         status = dcerpc_lsa_CreateSecret(p, tctx, &r);
66         torture_assert_ntstatus_ok(tctx, status, "CreateSecret failed");
67         
68         status = dcerpc_fetch_session_key(p, &session_key);
69         torture_assert_ntstatus_ok(tctx, status, "dcerpc_fetch_session_key failed");
70         
71         enc_key = sess_encrypt_string(secret1, &session_key);
72         
73         r3.in.sec_handle = &sec_handle;
74         r3.in.new_val = &buf1;
75         r3.in.old_val = NULL;
76         r3.in.new_val->data = enc_key.data;
77         r3.in.new_val->length = enc_key.length;
78         r3.in.new_val->size = enc_key.length;
79         
80         torture_comment(tctx, "Testing SetSecret\n");
81         
82         status = dcerpc_lsa_SetSecret(p, tctx, &r3);
83         torture_assert_ntstatus_ok(tctx, status, "SetSecret failed");
84                 
85         r3.in.sec_handle = &sec_handle;
86         r3.in.new_val = &buf1;
87         r3.in.old_val = NULL;
88         r3.in.new_val->data = enc_key.data;
89         r3.in.new_val->length = enc_key.length;
90         r3.in.new_val->size = enc_key.length;
91         
92         /* break the encrypted data */
93         enc_key.data[0]++;
94         
95         torture_comment(tctx, "Testing SetSecret with broken key\n");
96         
97         status = dcerpc_lsa_SetSecret(p, tctx, &r3);
98         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_UNKNOWN_REVISION, 
99                 "SetSecret should have failed UNKNOWN_REVISION");
100         
101         data_blob_free(&enc_key);
102         
103         ZERO_STRUCT(new_mtime);
104         ZERO_STRUCT(old_mtime);
105         
106         /* fetch the secret back again */
107         r4.in.sec_handle = &sec_handle;
108         r4.in.new_val = &bufp1;
109         r4.in.new_mtime = &new_mtime;
110         r4.in.old_val = NULL;
111         r4.in.old_mtime = NULL;
112         
113         bufp1.buf = NULL;
114         
115         torture_comment(tctx, "Testing QuerySecret\n");
116         status = dcerpc_lsa_QuerySecret(p, tctx, &r4);
117         torture_assert_ntstatus_ok(tctx, status, "QuerySecret failed");
118         if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL)
119                 torture_fail(tctx, "No secret buffer returned");
120         blob1.data = r4.out.new_val->buf->data;
121         blob1.length = r4.out.new_val->buf->size;
122         
123         blob2 = data_blob_talloc(tctx, NULL, blob1.length);
124         
125         secret2 = sess_decrypt_string(tctx, &blob1, &session_key);
126         
127         torture_assert_str_equal(tctx, secret1, secret2, "Returned secret invalid");
128
129         d.in.handle = &sec_handle;
130         status = dcerpc_lsa_Delete(p, tctx, &d);
131         torture_assert_ntstatus_ok(tctx, status, "delete should have returned OKINVALID_HANDLE");
132         return true;
133 }
134
135 struct secret_settings {
136         uint32_t bindoptions;
137         bool keyexchange;
138         bool ntlm2;
139         bool lm_key;
140 };
141
142 static bool test_secrets(struct torture_context *torture, const void *_data)
143 {
144         struct dcerpc_pipe *p;
145         struct policy_handle *handle;
146         struct dcerpc_binding *binding;
147         const struct secret_settings *settings = _data;
148
149         lp_set_cmdline("ntlmssp client:keyexchange", settings->keyexchange?"True":"False");
150         lp_set_cmdline("ntlmssp_client:ntlm2", settings->ntlm2?"True":"False");
151         lp_set_cmdline("ntlmssp_client:lm_key", settings->lm_key?"True":"False");
152
153         torture_assert_ntstatus_ok(torture, torture_rpc_binding(torture, &binding), 
154                                    "Getting bindoptions");
155
156         binding->flags |= settings->bindoptions;
157
158         torture_assert_ntstatus_ok(torture, 
159                                    dcerpc_pipe_connect_b(torture, &p, binding, &ndr_table_lsarpc, cmdline_credentials, NULL),
160                                    "connect");
161
162         if (!test_lsa_OpenPolicy2(p, torture, &handle)) {
163                 return false;
164         }
165
166         torture_assert(torture, handle, "OpenPolicy2 failed.  This test cannot run against this server");
167         
168         if (!test_CreateSecret_basic(p, torture, handle)) {
169                 return false;
170         }
171
172         return true;
173 }
174
175 static struct torture_tcase *add_test(struct torture_suite *suite, uint32_t bindoptions, 
176                                      bool keyexchange, bool ntlm2, bool lm_key)
177 {
178         char *name = NULL;
179         struct secret_settings *settings;
180
181         settings = talloc_zero(suite, struct secret_settings);
182         settings->bindoptions = bindoptions;
183
184         if (bindoptions == DCERPC_PUSH_BIGENDIAN)
185                 name = talloc_strdup(suite, "bigendian");
186         else if (bindoptions == DCERPC_SEAL)
187                 name = talloc_strdup(suite, "seal");
188         else if (bindoptions == 0) 
189                 name = talloc_strdup(suite, "none");
190         else
191                 name = talloc_strdup(suite, "unknown");
192
193         name = talloc_asprintf_append(name, " keyexchange:%s", keyexchange?"yes":"no");
194         settings->keyexchange = keyexchange;
195
196         name = talloc_asprintf_append(name, " ntlm2:%s", ntlm2?"yes":"no");
197         settings->ntlm2 = ntlm2;
198
199         name = talloc_asprintf_append(name, " lm_key:%s", lm_key?"yes":"no");
200         settings->lm_key = lm_key;
201
202         return torture_suite_add_simple_tcase(suite, name, test_secrets, settings);
203 }
204
205 static const bool bool_vals[] = { true, false };
206
207 /* TEST session key correctness by pushing and pulling secrets */
208 struct torture_suite *torture_rpc_lsa_secrets(TALLOC_CTX *mem_ctx)
209 {
210         struct torture_suite *suite = torture_suite_create(mem_ctx, "SECRETS");
211         int keyexchange, ntlm2, lm_key;
212
213         for (keyexchange = 0; keyexchange < ARRAY_SIZE(bool_vals); keyexchange++) {
214                 for (ntlm2 = 0; ntlm2 < ARRAY_SIZE(bool_vals); ntlm2++) {
215                         for (lm_key = 0; lm_key < ARRAY_SIZE(bool_vals); lm_key++) {
216                                 add_test(suite, DCERPC_PUSH_BIGENDIAN, bool_vals[keyexchange], bool_vals[ntlm2], 
217                                          bool_vals[lm_key]);
218                                 add_test(suite, DCERPC_SEAL, bool_vals[keyexchange], bool_vals[ntlm2], bool_vals[lm_key]);
219                                 add_test(suite, 0, bool_vals[keyexchange], bool_vals[ntlm2], bool_vals[lm_key]);
220                         }
221                 }
222         }
223
224         return suite;
225 }