r23792: convert Samba4 to GPLv3
[samba.git] / source / 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
29 static void init_lsa_String(struct lsa_String *name, const char *s)
30 {
31         name->string = s;
32 }
33
34 static BOOL test_CreateSecret_basic(struct dcerpc_pipe *p, 
35                                     TALLOC_CTX *mem_ctx, 
36                                     struct policy_handle *handle)
37 {
38         NTSTATUS status;
39         struct lsa_CreateSecret r;
40         struct lsa_SetSecret r3;
41         struct lsa_QuerySecret r4;
42         struct policy_handle sec_handle;
43         struct lsa_Delete d;
44         struct lsa_DATA_BUF buf1;
45         struct lsa_DATA_BUF_PTR bufp1;
46         DATA_BLOB enc_key;
47         BOOL ret = True;
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(mem_ctx, "torturesecret-%u", (uint_t)random());
56
57         printf("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, mem_ctx, &r);
66         if (!NT_STATUS_IS_OK(status)) {
67                 printf("CreateSecret failed - %s\n", nt_errstr(status));
68                 return False;
69         }
70         
71         status = dcerpc_fetch_session_key(p, &session_key);
72         if (!NT_STATUS_IS_OK(status)) {
73                 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
74                 ret = False;
75         }
76         
77         enc_key = sess_encrypt_string(secret1, &session_key);
78         
79         r3.in.sec_handle = &sec_handle;
80         r3.in.new_val = &buf1;
81         r3.in.old_val = NULL;
82         r3.in.new_val->data = enc_key.data;
83         r3.in.new_val->length = enc_key.length;
84         r3.in.new_val->size = enc_key.length;
85         
86         printf("Testing SetSecret\n");
87         
88         status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
89         if (!NT_STATUS_IS_OK(status)) {
90                 printf("SetSecret failed - %s\n", nt_errstr(status));
91                 ret = False;
92         }
93                 
94         r3.in.sec_handle = &sec_handle;
95         r3.in.new_val = &buf1;
96         r3.in.old_val = NULL;
97         r3.in.new_val->data = enc_key.data;
98         r3.in.new_val->length = enc_key.length;
99         r3.in.new_val->size = enc_key.length;
100         
101         /* break the encrypted data */
102         enc_key.data[0]++;
103         
104         printf("Testing SetSecret with broken key\n");
105         
106         status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
107         if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
108                 printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
109                 ret = False;
110         }
111         
112         data_blob_free(&enc_key);
113         
114         ZERO_STRUCT(new_mtime);
115         ZERO_STRUCT(old_mtime);
116         
117         /* fetch the secret back again */
118         r4.in.sec_handle = &sec_handle;
119         r4.in.new_val = &bufp1;
120         r4.in.new_mtime = &new_mtime;
121         r4.in.old_val = NULL;
122         r4.in.old_mtime = NULL;
123         
124         bufp1.buf = NULL;
125         
126         printf("Testing QuerySecret\n");
127         status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
128         if (!NT_STATUS_IS_OK(status)) {
129                 printf("QuerySecret failed - %s\n", nt_errstr(status));
130                 ret = False;
131         } else {
132                 if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
133                         printf("No secret buffer returned\n");
134                         ret = False;
135                 } else {
136                         blob1.data = r4.out.new_val->buf->data;
137                         blob1.length = r4.out.new_val->buf->size;
138                         
139                         blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
140                         
141                         secret2 = sess_decrypt_string(mem_ctx, &blob1, &session_key);
142                         
143                         if (strcmp(secret1, secret2) != 0) {
144                                 printf("Returned secret '%s' doesn't match '%s'\n", 
145                                        secret2, secret1);
146                                 ret = False;
147                         }
148                 }
149         }
150
151         d.in.handle = &sec_handle;
152         status = dcerpc_lsa_Delete(p, mem_ctx, &d);
153         if (!NT_STATUS_IS_OK(status)) {
154                 printf("delete should have returned OKINVALID_HANDLE - %s\n", nt_errstr(status));
155                 ret = False;
156         }
157         return ret;
158 }
159
160
161 /* TEST session key correctness by pushing and pulling secrets */
162
163 BOOL torture_rpc_lsa_secrets(struct torture_context *torture) 
164 {
165         NTSTATUS status;
166         struct dcerpc_pipe *p;
167         TALLOC_CTX *mem_ctx;
168         BOOL ret = True;
169         struct policy_handle *handle;
170
171         mem_ctx = talloc_init("torture_rpc_lsa_secrets");
172
173         status = torture_rpc_connection(mem_ctx, 
174                                         &p, 
175                                         &dcerpc_table_lsarpc);
176         if (!NT_STATUS_IS_OK(status)) {
177                 talloc_free(mem_ctx);
178                 return False;
179         }
180
181         if (test_lsa_OpenPolicy2(p, mem_ctx, &handle)) {
182                 if (!handle) {
183                         printf("OpenPolicy2 failed.  This test cannot run against this server\n");
184                         ret = False;
185                 } else if (!test_CreateSecret_basic(p, mem_ctx, handle)) {
186                         ret = False;
187                 }
188         } else {
189                 return False;
190         }
191
192         talloc_free(mem_ctx);
193
194         return ret;
195 }