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