s4: popt: Global replace of cmdline_credentials -> popt_get_cmdline_credentials().
[gd/samba-autobuild/.git] / source4 / torture / rpc / backupkey_heimdal.c
1 /*
2    Unix SMB/CIFS implementation.
3    test suite for backupkey remote protocol rpc operations
4
5    Copyright (C) Matthieu Patou 2010-2011
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "../libcli/security/security.h"
23
24 #include "torture/rpc/torture_rpc.h"
25 #include "torture/ndr/ndr.h"
26
27 #include "librpc/gen_ndr/ndr_backupkey_c.h"
28 #include "librpc/gen_ndr/ndr_backupkey.h"
29 #include "librpc/gen_ndr/ndr_lsa_c.h"
30 #include "librpc/gen_ndr/ndr_security.h"
31 #include "lib/cmdline/popt_common.h"
32 #include "libcli/auth/proto.h"
33 #include "lib/crypto/arcfour.h"
34 #include <com_err.h>
35 #include <hcrypto/sha.h>
36 #include <system/network.h>
37 #include <hx509.h>
38 #include <der.h>
39 #include <hcrypto/rsa.h>
40 #include <hcrypto/hmac.h>
41 #include <hcrypto/sha.h>
42 #include <hcrypto/evp.h>
43
44 enum test_wrong {
45         WRONG_MAGIC,
46         WRONG_R2,
47         WRONG_PAYLOAD_LENGTH,
48         WRONG_CIPHERTEXT_LENGTH,
49         SHORT_PAYLOAD_LENGTH,
50         SHORT_CIPHERTEXT_LENGTH,
51         ZERO_PAYLOAD_LENGTH,
52         ZERO_CIPHERTEXT_LENGTH,
53         RIGHT_KEY,
54         WRONG_KEY,
55         WRONG_SID,
56 };
57
58 /* Our very special and valued secret */
59 /* No need to put const as we cast the array in uint8_t
60  * we will get a warning about the discared const
61  */
62 static const char secret[] = "tata yoyo mais qu'est ce qu'il y a sous ton grand chapeau ?";
63
64 /* Get the SID from a user */
65 static struct dom_sid *get_user_sid(struct torture_context *tctx,
66                                     TALLOC_CTX *mem_ctx,
67                                     const char *user)
68 {
69         struct lsa_ObjectAttribute attr;
70         struct lsa_QosInfo qos;
71         struct lsa_OpenPolicy2 r;
72         struct lsa_Close c;
73         NTSTATUS status;
74         struct policy_handle handle;
75         struct lsa_LookupNames l;
76         struct lsa_TransSidArray sids;
77         struct lsa_RefDomainList *domains = NULL;
78         struct lsa_String lsa_name;
79         uint32_t count = 0;
80         struct dom_sid *result;
81         TALLOC_CTX *tmp_ctx;
82         struct dcerpc_pipe *p2;
83         struct dcerpc_binding_handle *b;
84
85         const char *domain = cli_credentials_get_domain(
86                                 popt_get_cmdline_credentials());
87
88         torture_assert_ntstatus_ok(tctx,
89                                 torture_rpc_connection(tctx, &p2, &ndr_table_lsarpc),
90                                 "could not open lsarpc pipe");
91         b = p2->binding_handle;
92
93         if (!(tmp_ctx = talloc_new(mem_ctx))) {
94                 return NULL;
95         }
96         qos.len = 0;
97         qos.impersonation_level = 2;
98         qos.context_mode = 1;
99         qos.effective_only = 0;
100
101         attr.len = 0;
102         attr.root_dir = NULL;
103         attr.object_name = NULL;
104         attr.attributes = 0;
105         attr.sec_desc = NULL;
106         attr.sec_qos = &qos;
107
108         r.in.system_name = "\\";
109         r.in.attr = &attr;
110         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
111         r.out.handle = &handle;
112
113         status = dcerpc_lsa_OpenPolicy2_r(b, tmp_ctx, &r);
114         if (!NT_STATUS_IS_OK(status)) {
115                 torture_comment(tctx,
116                                 "OpenPolicy2 failed - %s\n",
117                                 nt_errstr(status));
118                 talloc_free(tmp_ctx);
119                 return NULL;
120         }
121         if (!NT_STATUS_IS_OK(r.out.result)) {
122                 torture_comment(tctx,
123                                 "OpenPolicy2_ failed - %s\n",
124                                 nt_errstr(r.out.result));
125                 talloc_free(tmp_ctx);
126                 return NULL;
127         }
128
129         sids.count = 0;
130         sids.sids = NULL;
131
132         lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, user);
133
134         l.in.handle = &handle;
135         l.in.num_names = 1;
136         l.in.names = &lsa_name;
137         l.in.sids = &sids;
138         l.in.level = 1;
139         l.in.count = &count;
140         l.out.count = &count;
141         l.out.sids = &sids;
142         l.out.domains = &domains;
143
144         status = dcerpc_lsa_LookupNames_r(b, tmp_ctx, &l);
145         if (!NT_STATUS_IS_OK(status)) {
146                 torture_comment(tctx,
147                                 "LookupNames of %s failed - %s\n",
148                                 lsa_name.string,
149                                 nt_errstr(status));
150                 talloc_free(tmp_ctx);
151                 return NULL;
152         }
153
154         if (domains->count == 0) {
155                 return NULL;
156         }
157
158         result = dom_sid_add_rid(mem_ctx,
159                                  domains->domains[0].sid,
160                                  l.out.sids->sids[0].rid);
161         c.in.handle = &handle;
162         c.out.handle = &handle;
163
164         status = dcerpc_lsa_Close_r(b, tmp_ctx, &c);
165
166         if (!NT_STATUS_IS_OK(status)) {
167                 torture_comment(tctx,
168                                 "dcerpc_lsa_Close failed - %s\n",
169                                 nt_errstr(status));
170                 talloc_free(tmp_ctx);
171                 return NULL;
172         }
173
174         if (!NT_STATUS_IS_OK(c.out.result)) {
175                 torture_comment(tctx,
176                                 "dcerpc_lsa_Close failed - %s\n",
177                                 nt_errstr(c.out.result));
178                 talloc_free(tmp_ctx);
179                 return NULL;
180         }
181
182         talloc_free(tmp_ctx);
183         talloc_free(p2);
184
185         torture_comment(tctx, "Get_user_sid finished\n");
186         return result;
187 }
188
189 /*
190  * Create a bkrp_encrypted_secret_vX structure
191  * the version depends on the version parameter
192  * the structure is returned as a blob.
193  * The broken flag is to indicate if we want
194  * to create a non conform to specification structre
195  */
196 static DATA_BLOB *create_unencryptedsecret(TALLOC_CTX *mem_ctx,
197                                            bool broken,
198                                            int version)
199 {
200         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
201         DATA_BLOB *blob = talloc_zero(mem_ctx, DATA_BLOB);
202         enum ndr_err_code ndr_err;
203
204         if (version == 2) {
205                 struct bkrp_encrypted_secret_v2 unenc_sec;
206
207                 ZERO_STRUCT(unenc_sec);
208                 unenc_sec.secret_len = sizeof(secret);
209                 unenc_sec.secret = discard_const_p(uint8_t, secret);
210                 generate_random_buffer(unenc_sec.payload_key,
211                                        sizeof(unenc_sec.payload_key));
212
213                 ndr_err = ndr_push_struct_blob(blob, blob, &unenc_sec,
214                                 (ndr_push_flags_fn_t)ndr_push_bkrp_encrypted_secret_v2);
215                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
216                         return NULL;
217                 }
218
219                 if (broken) {
220                         /* The magic value is correctly set by the NDR push
221                          * but we want to test the behavior of the server
222                          * if a differrent value is provided
223                          */
224                         ((uint8_t*)blob->data)[4] = 79; /* A great year !!! */
225                 }
226         }
227
228         if (version == 3) {
229                 struct bkrp_encrypted_secret_v3 unenc_sec;
230
231                 ZERO_STRUCT(unenc_sec);
232                 unenc_sec.secret_len = sizeof(secret);
233                 unenc_sec.secret = discard_const_p(uint8_t, secret);
234                 generate_random_buffer(unenc_sec.payload_key,
235                                        sizeof(unenc_sec.payload_key));
236
237                 ndr_err = ndr_push_struct_blob(blob, blob, &unenc_sec,
238                                         (ndr_push_flags_fn_t)ndr_push_bkrp_encrypted_secret_v3);
239                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
240                         return NULL;
241                 }
242
243                 if (broken) {
244                         /*
245                          * The magic value is correctly set by the NDR push
246                          * but we want to test the behavior of the server
247                          * if a differrent value is provided
248                          */
249                         ((uint8_t*)blob->data)[4] = 79; /* A great year !!! */
250                 }
251         }
252         talloc_free(tmp_ctx);
253         return blob;
254 }
255
256 /*
257  * Create an access check structure, the format depends on the version parameter.
258  * If broken is specified then we create a stucture that isn't conform to the
259  * specification.
260  *
261  * If the structure can't be created then NULL is returned.
262  */
263 static DATA_BLOB *create_access_check(struct torture_context *tctx,
264                                       struct dcerpc_pipe *p,
265                                       TALLOC_CTX *mem_ctx,
266                                       const char *user,
267                                       bool broken,
268                                       uint32_t version)
269 {
270         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
271         DATA_BLOB *blob = talloc_zero(mem_ctx, DATA_BLOB);
272         enum ndr_err_code ndr_err;
273         const struct dom_sid *sid = get_user_sid(tctx, tmp_ctx, user);
274
275         if (sid == NULL) {
276                 return NULL;
277         }
278
279         if (version == 2) {
280                 struct bkrp_access_check_v2 access_struct;
281                 struct sha sctx;
282                 uint8_t nonce[32];
283
284                 ZERO_STRUCT(access_struct);
285                 generate_random_buffer(nonce, sizeof(nonce));
286                 access_struct.nonce_len = sizeof(nonce);
287                 access_struct.nonce = nonce;
288                 access_struct.sid = *sid;
289
290                 ndr_err = ndr_push_struct_blob(blob, blob, &access_struct,
291                                 (ndr_push_flags_fn_t)ndr_push_bkrp_access_check_v2);
292                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
293                         return NULL;
294                 }
295
296                 /*
297                  * We pushed the whole structure including a null hash
298                  * but the hash need to be calculated only up to the hash field
299                  * so we reduce the size of what has to be calculated
300                  */
301
302                 SHA1_Init(&sctx);
303                 SHA1_Update(&sctx, blob->data,
304                             blob->length - sizeof(access_struct.hash));
305                 SHA1_Final(blob->data + blob->length - sizeof(access_struct.hash),
306                            &sctx);
307
308                 /* Altering the SHA */
309                 if (broken) {
310                         blob->data[blob->length - 1]++;
311                 }
312         }
313
314         if (version == 3) {
315                 struct bkrp_access_check_v3 access_struct;
316                 struct hc_sha512state sctx;
317                 uint8_t nonce[32];
318
319                 ZERO_STRUCT(access_struct);
320                 generate_random_buffer(nonce, sizeof(nonce));
321                 access_struct.nonce_len = sizeof(nonce);
322                 access_struct.nonce = nonce;
323                 access_struct.sid = *sid;
324
325                 ndr_err = ndr_push_struct_blob(blob, blob, &access_struct,
326                                 (ndr_push_flags_fn_t)ndr_push_bkrp_access_check_v3);
327                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
328                         return NULL;
329                 }
330
331                 /*We pushed the whole structure including a null hash
332                 * but the hash need to be calculated only up to the hash field
333                 * so we reduce the size of what has to be calculated
334                 */
335
336                 SHA512_Init(&sctx);
337                 SHA512_Update(&sctx, blob->data,
338                               blob->length - sizeof(access_struct.hash));
339                 SHA512_Final(blob->data + blob->length - sizeof(access_struct.hash),
340                              &sctx);
341
342                 /* Altering the SHA */
343                 if (broken) {
344                         blob->data[blob->length -1]++;
345                 }
346         }
347         talloc_free(tmp_ctx);
348         return blob;
349 }
350
351
352 static DATA_BLOB *encrypt_blob(struct torture_context *tctx,
353                                     TALLOC_CTX *mem_ctx,
354                                     DATA_BLOB *key,
355                                     DATA_BLOB *iv,
356                                     DATA_BLOB *to_encrypt,
357                                     const AlgorithmIdentifier *alg)
358 {
359         hx509_crypto crypto;
360         hx509_context hctx;
361         heim_octet_string ivos;
362         heim_octet_string *encrypted;
363         DATA_BLOB *blob = talloc_zero(mem_ctx, DATA_BLOB);
364         int res;
365
366         ivos.data = talloc_array(mem_ctx, uint8_t, iv->length);
367         ivos.length = iv->length;
368         memcpy(ivos.data, iv->data, iv->length);
369
370         hx509_context_init(&hctx);
371         res = hx509_crypto_init(hctx, NULL, &alg->algorithm, &crypto);
372         if (res) {
373                 torture_comment(tctx,
374                                 "error while doing the init of the crypto object\n");
375                 hx509_context_free(&hctx);
376                 return NULL;
377         }
378         res = hx509_crypto_set_key_data(crypto, key->data, key->length);
379         if (res) {
380                 torture_comment(tctx,
381                                 "error while setting the key of the crypto object\n");
382                 hx509_context_free(&hctx);
383                 return NULL;
384         }
385
386         hx509_crypto_set_padding(crypto, HX509_CRYPTO_PADDING_NONE);
387         res = hx509_crypto_encrypt(crypto,
388                                    to_encrypt->data,
389                                    to_encrypt->length,
390                                    &ivos,
391                                    &encrypted);
392         if (res) {
393                 torture_comment(tctx, "error while encrypting\n");
394                 hx509_crypto_destroy(crypto);
395                 hx509_context_free(&hctx);
396                 return NULL;
397         }
398
399         *blob = data_blob_talloc(blob, encrypted->data, encrypted->length);
400         der_free_octet_string(encrypted);
401         free(encrypted);
402         hx509_crypto_destroy(crypto);
403         hx509_context_free(&hctx);
404         return blob;
405 }
406
407 /*
408  * Certs used for this protocol have a GUID in the issuer_uniq_id field.
409  * This function fetch it.
410  */
411 static struct GUID *get_cert_guid(struct torture_context *tctx,
412                                   TALLOC_CTX *mem_ctx,
413                                   uint8_t *cert_data,
414                                   uint32_t cert_len)
415 {
416         hx509_context hctx;
417         hx509_cert cert;
418         heim_bit_string issuer_unique_id;
419         DATA_BLOB data;
420         int hret;
421         uint32_t size;
422         struct GUID *guid = talloc_zero(mem_ctx, struct GUID);
423         NTSTATUS status;
424
425         hx509_context_init(&hctx);
426
427         hret = hx509_cert_init_data(hctx, cert_data, cert_len, &cert);
428         if (hret) {
429                 torture_comment(tctx, "error while loading the cert\n");
430                 hx509_context_free(&hctx);
431                 return NULL;
432         }
433         hret = hx509_cert_get_issuer_unique_id(hctx, cert, &issuer_unique_id);
434         if (hret) {
435                 torture_comment(tctx, "error while getting the issuer_uniq_id\n");
436                 hx509_cert_free(cert);
437                 hx509_context_free(&hctx);
438                 return NULL;
439         }
440
441         /* The issuer_unique_id is a bit string,
442          * which means that the real size has to be divided by 8
443          * to have the number of bytes
444          */
445         hx509_cert_free(cert);
446         hx509_context_free(&hctx);
447         size = issuer_unique_id.length / 8;
448         data = data_blob_const(issuer_unique_id.data, size);
449
450         status = GUID_from_data_blob(&data, guid);
451         der_free_bit_string(&issuer_unique_id);
452         if (!NT_STATUS_IS_OK(status)) {
453                 return NULL;
454         }
455
456         return guid;
457 }
458
459 /*
460  * Encrypt a blob with the private key of the certificate
461  * passed as a parameter.
462  */
463 static DATA_BLOB *encrypt_blob_pk(struct torture_context *tctx,
464                                   TALLOC_CTX *mem_ctx,
465                                   uint8_t *cert_data,
466                                   uint32_t cert_len,
467                                   DATA_BLOB *to_encrypt)
468 {
469         hx509_context hctx;
470         hx509_cert cert;
471         heim_octet_string secretdata;
472         heim_octet_string encrypted;
473         heim_oid encryption_oid;
474         DATA_BLOB *blob;
475         int hret;
476
477         hx509_context_init(&hctx);
478
479         hret = hx509_cert_init_data(hctx, cert_data, cert_len, &cert);
480         if (hret) {
481                 torture_comment(tctx, "error while loading the cert\n");
482                 hx509_context_free(&hctx);
483                 return NULL;
484         }
485
486         secretdata.data = to_encrypt->data;
487         secretdata.length = to_encrypt->length;
488         hret = hx509_cert_public_encrypt(hctx, &secretdata,
489                                           cert, &encryption_oid,
490                                           &encrypted);
491         hx509_cert_free(cert);
492         hx509_context_free(&hctx);
493         if (hret) {
494                 torture_comment(tctx, "error while encrypting\n");
495                 return NULL;
496         }
497
498         blob = talloc_zero(mem_ctx, DATA_BLOB);
499         if (blob == NULL) {
500                 der_free_oid(&encryption_oid);
501                 der_free_octet_string(&encrypted);
502                 return NULL;
503         }
504
505         *blob = data_blob_talloc(blob, encrypted.data, encrypted.length);
506         der_free_octet_string(&encrypted);
507         der_free_oid(&encryption_oid);
508         if (blob->data == NULL) {
509                 return NULL;
510         }
511
512         return blob;
513 }
514
515
516 static struct bkrp_BackupKey *createRetrieveBackupKeyGUIDStruct(struct torture_context *tctx,
517                                 struct dcerpc_pipe *p, int version, DATA_BLOB *out)
518 {
519         struct dcerpc_binding *binding;
520         struct bkrp_client_side_wrapped data;
521         struct GUID *g = talloc(tctx, struct GUID);
522         struct bkrp_BackupKey *r = talloc_zero(tctx, struct bkrp_BackupKey);
523         enum ndr_err_code ndr_err;
524         DATA_BLOB blob;
525         NTSTATUS status;
526
527         if (r == NULL) {
528                 return NULL;
529         }
530
531         binding = dcerpc_binding_dup(tctx, p->binding);
532         if (binding == NULL) {
533                 return NULL;
534         }
535
536         status = dcerpc_binding_set_flags(binding, DCERPC_SEAL|DCERPC_AUTH_SPNEGO, 0);
537         if (!NT_STATUS_IS_OK(status)) {
538                 return NULL;
539         }
540
541         ZERO_STRUCT(data);
542         status = GUID_from_string(BACKUPKEY_RETRIEVE_BACKUP_KEY_GUID, g);
543         if (!NT_STATUS_IS_OK(status)) {
544                 return NULL;
545         }
546
547         r->in.guidActionAgent = g;
548         data.version = version;
549         ndr_err = ndr_push_struct_blob(&blob, tctx, &data,
550                         (ndr_push_flags_fn_t)ndr_push_bkrp_client_side_wrapped);
551         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
552                 return NULL;
553         }
554         r->in.data_in = blob.data;
555         r->in.data_in_len = blob.length;
556         r->out.data_out = &out->data;
557         r->out.data_out_len = talloc(r, uint32_t);
558         return r;
559 }
560
561 static struct bkrp_BackupKey *createRestoreGUIDStruct(struct torture_context *tctx,
562                                 struct dcerpc_pipe *p, int version, DATA_BLOB *out,
563                                 bool norevert,
564                                 bool broken_version,
565                                 bool broken_user,
566                                 bool broken_magic_secret,
567                                 bool broken_magic_access,
568                                 bool broken_hash_access,
569                                 bool broken_cert_guid)
570 {
571         struct dcerpc_binding_handle *b = p->binding_handle;
572         struct bkrp_client_side_wrapped data;
573         DATA_BLOB *xs;
574         DATA_BLOB *sec;
575         DATA_BLOB *enc_sec = NULL;
576         DATA_BLOB *enc_xs = NULL;
577         DATA_BLOB *blob2;
578         DATA_BLOB enc_sec_reverted;
579         DATA_BLOB des3_key;
580         DATA_BLOB aes_key;
581         DATA_BLOB iv;
582         DATA_BLOB out_blob;
583         struct GUID *guid, *g;
584         int t;
585         uint32_t size;
586         enum ndr_err_code ndr_err;
587         NTSTATUS status;
588         const char *user;
589         struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, version, &out_blob);
590         if (r == NULL) {
591                 return NULL;
592         }
593
594         if (broken_user) {
595                 /* we take a fake user*/
596                 user = "guest";
597         } else {
598                 user = cli_credentials_get_username(
599                                 popt_get_cmdline_credentials());
600         }
601
602
603         torture_assert_ntstatus_ok(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r),
604                                         "Get GUID");
605         torture_assert_werr_ok(tctx, r->out.result,
606                                "Get GUID");
607
608         /*
609          * We have to set it outside of the function createRetrieveBackupKeyGUIDStruct
610          * the len of the blob, this is due to the fact that they don't have the
611          * same size (one is 32bits the other 64bits)
612          */
613         out_blob.length = *r->out.data_out_len;
614
615         sec = create_unencryptedsecret(tctx, broken_magic_secret, version);
616         if (sec == NULL) {
617                 return NULL;
618         }
619
620         xs = create_access_check(tctx, p, tctx, user, broken_hash_access, version);
621         if (xs == NULL) {
622                 return NULL;
623         }
624
625         if (broken_magic_access){
626                 /* The start of the access_check structure contains the
627                  * GUID of the certificate
628                  */
629                 xs->data[0]++;
630         }
631
632         enc_sec = encrypt_blob_pk(tctx, tctx, out_blob.data, out_blob.length, sec);
633         if (!enc_sec) {
634                 return NULL;
635         }
636         enc_sec_reverted.data = talloc_array(tctx, uint8_t, enc_sec->length);
637         if (enc_sec_reverted.data == NULL) {
638                 return NULL;
639         }
640         enc_sec_reverted.length = enc_sec->length;
641
642         /*
643         * We DO NOT revert the array on purpose it's in order to check that
644         * when the server is not able to decrypt then it answer the correct error
645         */
646         if (norevert) {
647                 for(t=0; t< enc_sec->length; t++) {
648                         enc_sec_reverted.data[t] = ((uint8_t*)enc_sec->data)[t];
649                 }
650         } else {
651                 for(t=0; t< enc_sec->length; t++) {
652                         enc_sec_reverted.data[t] = ((uint8_t*)enc_sec->data)[enc_sec->length - t -1];
653                 }
654         }
655
656         size = sec->length;
657         if (version ==2) {
658                 const AlgorithmIdentifier *alg = hx509_crypto_des_rsdi_ede3_cbc();
659                 iv.data = sec->data+(size - 8);
660                 iv.length = 8;
661
662                 des3_key.data = sec->data+(size - 32);
663                 des3_key.length = 24;
664
665                 enc_xs = encrypt_blob(tctx, tctx, &des3_key, &iv, xs, alg);
666         }
667         if (version == 3) {
668                 const AlgorithmIdentifier *alg = hx509_crypto_aes256_cbc();
669                 iv.data = sec->data+(size-16);
670                 iv.length = 16;
671
672                 aes_key.data = sec->data+(size-48);
673                 aes_key.length = 32;
674
675                 enc_xs = encrypt_blob(tctx, tctx, &aes_key, &iv, xs, alg);
676         }
677
678         if (!enc_xs) {
679                 return NULL;
680         }
681
682         /* To cope with the fact that heimdal do padding at the end for the moment */
683         enc_xs->length = xs->length;
684
685         guid = get_cert_guid(tctx, tctx, out_blob.data, out_blob.length);
686         if (guid == NULL) {
687                 return NULL;
688         }
689
690         if (broken_version) {
691                 data.version = 1;
692         } else {
693                 data.version = version;
694         }
695
696         data.guid = *guid;
697         data.encrypted_secret = enc_sec_reverted.data;
698         data.access_check = enc_xs->data;
699         data.encrypted_secret_len = enc_sec->length;
700         data.access_check_len = enc_xs->length;
701
702         /* We want the blob to persist after this function so we don't
703          * allocate it in the stack
704          */
705         blob2 = talloc(tctx, DATA_BLOB);
706         if (blob2 == NULL) {
707                 return NULL;
708         }
709
710         ndr_err = ndr_push_struct_blob(blob2, tctx, &data,
711                         (ndr_push_flags_fn_t)ndr_push_bkrp_client_side_wrapped);
712         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
713                 return NULL;
714         }
715
716         if (broken_cert_guid) {
717                 blob2->data[12]++;
718         }
719
720         ZERO_STRUCT(*r);
721
722         g = talloc(tctx, struct GUID);
723         if (g == NULL) {
724                 return NULL;
725         }
726
727         status = GUID_from_string(BACKUPKEY_RESTORE_GUID, g);
728         if (!NT_STATUS_IS_OK(status)) {
729                 return NULL;
730         }
731
732         r->in.guidActionAgent = g;
733         r->in.data_in = blob2->data;
734         r->in.data_in_len = blob2->length;
735         r->in.param = 0;
736         r->out.data_out = &(out->data);
737         r->out.data_out_len = talloc(r, uint32_t);
738         return r;
739 }
740
741 /* Check that we are able to receive the certificate of the DCs
742  * used for client wrap version of the backup key protocol
743  */
744 static bool test_RetrieveBackupKeyGUID(struct torture_context *tctx,
745                                         struct dcerpc_pipe *p)
746 {
747         struct dcerpc_binding_handle *b = p->binding_handle;
748         DATA_BLOB out_blob;
749         struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, 2, &out_blob);
750         enum dcerpc_AuthType auth_type;
751         enum dcerpc_AuthLevel auth_level;
752
753         if (r == NULL) {
754                 return false;
755         }
756
757         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
758
759         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
760                 torture_assert_ntstatus_ok(tctx,
761                                 dcerpc_bkrp_BackupKey_r(b, tctx, r),
762                                 "Get GUID");
763
764                 out_blob.length = *r->out.data_out_len;
765                 torture_assert_werr_equal(tctx,
766                                                 r->out.result,
767                                                 WERR_OK,
768                                                 "Wrong dce/rpc error code");
769         } else {
770                 torture_assert_ntstatus_equal(tctx,
771                                                 dcerpc_bkrp_BackupKey_r(b, tctx, r),
772                                                 NT_STATUS_ACCESS_DENIED,
773                                                 "Get GUID");
774         }
775         return true;
776 }
777
778 /* Test to check the failure to recover a secret because the
779  * secret blob is not reversed
780  */
781 static bool test_RestoreGUID_ko(struct torture_context *tctx,
782                                 struct dcerpc_pipe *p)
783 {
784         enum ndr_err_code ndr_err;
785         struct dcerpc_binding_handle *b = p->binding_handle;
786         DATA_BLOB out_blob;
787         struct bkrp_client_side_unwrapped resp;
788         enum dcerpc_AuthType auth_type;
789         enum dcerpc_AuthLevel auth_level;
790
791         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
792
793         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
794                 struct bkrp_BackupKey *r = createRestoreGUIDStruct(tctx, p, 2, &out_blob,
795                                         true, false, false, false, false, false, false);
796                 torture_assert(tctx, r != NULL, "createRestoreGUIDStruct failed");
797                 torture_assert_ntstatus_ok(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r), "Restore GUID");
798                 out_blob.length = *r->out.data_out_len;
799                 ndr_err = ndr_pull_struct_blob(&out_blob, tctx, &resp, (ndr_pull_flags_fn_t)ndr_pull_bkrp_client_side_unwrapped);
800                 torture_assert_int_equal(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), 0, "Unable to unmarshall bkrp_client_side_unwrapped");
801                 if (!W_ERROR_EQUAL(r->out.result, WERR_INVALID_PARAMETER)) {
802                         torture_assert_werr_equal(tctx, r->out.result,
803                                                   WERR_INVALID_DATA,
804                                                   "Wrong error code");
805                 }
806         } else {
807                 struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, 2, &out_blob);
808                 torture_assert_ntstatus_equal(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r),
809                         NT_STATUS_ACCESS_DENIED, "Get GUID");
810         }
811         return true;
812 }
813
814 static bool test_RestoreGUID_wrongversion(struct torture_context *tctx,
815                                           struct dcerpc_pipe *p)
816 {
817         enum ndr_err_code ndr_err;
818         struct dcerpc_binding_handle *b = p->binding_handle;
819         DATA_BLOB out_blob;
820         struct bkrp_client_side_unwrapped resp;
821         enum dcerpc_AuthType auth_type;
822         enum dcerpc_AuthLevel auth_level;
823
824         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
825
826         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
827                 struct bkrp_BackupKey *r = createRestoreGUIDStruct(tctx, p, 2, &out_blob,
828                                         false, true, false, false, false, false, false);
829                 torture_assert(tctx, r != NULL, "createRestoreGUIDStruct failed");
830                 torture_assert_ntstatus_ok(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r), "Restore GUID");
831                 out_blob.length = *r->out.data_out_len;
832                 ndr_err = ndr_pull_struct_blob(&out_blob, tctx, &resp, (ndr_pull_flags_fn_t)ndr_pull_bkrp_client_side_unwrapped);
833                 torture_assert_int_equal(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), 0, "Unable to unmarshall bkrp_client_side_unwrapped");
834                 torture_assert_werr_equal(tctx, r->out.result, WERR_INVALID_PARAMETER, "Wrong error code on wrong version");
835         } else {
836                 struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, 2, &out_blob);
837                 torture_assert_ntstatus_equal(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r),
838                         NT_STATUS_ACCESS_DENIED, "Get GUID");
839         }
840         return true;
841 }
842
843 static bool test_RestoreGUID_wronguser(struct torture_context *tctx,
844                                        struct dcerpc_pipe *p)
845 {
846         enum ndr_err_code ndr_err;
847         struct dcerpc_binding_handle *b = p->binding_handle;
848         DATA_BLOB out_blob;
849         struct bkrp_client_side_unwrapped resp;
850         enum dcerpc_AuthType auth_type;
851         enum dcerpc_AuthLevel auth_level;
852
853         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
854
855         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
856                 struct bkrp_BackupKey *r = createRestoreGUIDStruct(tctx, p, 2, &out_blob,
857                                         false, false, true, false, false, false, false);
858                 torture_assert(tctx, r != NULL, "createRestoreGUIDStruct failed");
859                 torture_assert_ntstatus_ok(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r), "Restore GUID");
860                 out_blob.length = *r->out.data_out_len;
861                 ndr_err = ndr_pull_struct_blob(&out_blob, tctx, &resp, (ndr_pull_flags_fn_t)ndr_pull_bkrp_client_side_unwrapped);
862                 torture_assert_int_equal(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), 0, "Unable to unmarshall bkrp_client_side_unwrapped");
863                 torture_assert_werr_equal(tctx, r->out.result, WERR_INVALID_ACCESS, "Restore GUID");
864         } else {
865                 struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, 2, &out_blob);
866                 torture_assert_ntstatus_equal(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r),
867                         NT_STATUS_ACCESS_DENIED, "Get GUID");
868         }
869         return true;
870 }
871
872 static bool test_RestoreGUID_v3(struct torture_context *tctx,
873                                 struct dcerpc_pipe *p)
874 {
875         enum ndr_err_code ndr_err;
876         struct dcerpc_binding_handle *b = p->binding_handle;
877         DATA_BLOB out_blob;
878         struct bkrp_client_side_unwrapped resp;
879         enum dcerpc_AuthType auth_type;
880         enum dcerpc_AuthLevel auth_level;
881
882         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
883
884         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
885                 struct bkrp_BackupKey *r = createRestoreGUIDStruct(tctx, p, 3, &out_blob,
886                                         false, false, false, false, false, false, false);
887                 torture_assert(tctx, r != NULL, "createRestoreGUIDStruct failed");
888                 torture_assert_ntstatus_ok(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r), "Restore GUID");
889                 out_blob.length = *r->out.data_out_len;
890                 ndr_err = ndr_pull_struct_blob(&out_blob, tctx, &resp, (ndr_pull_flags_fn_t)ndr_pull_bkrp_client_side_unwrapped);
891                 torture_assert_int_equal(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), 1, "Unable to unmarshall bkrp_client_side_unwrapped");
892                 torture_assert_werr_equal(tctx, r->out.result, WERR_OK, "Restore GUID");
893                 torture_assert_str_equal(tctx, (char*)resp.secret.data, secret, "Wrong secret");
894         } else {
895                 struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, 2, &out_blob);
896                 torture_assert_ntstatus_equal(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r),
897                         NT_STATUS_ACCESS_DENIED, "Get GUID");
898         }
899         return true;
900 }
901
902 static bool test_RestoreGUID(struct torture_context *tctx,
903                              struct dcerpc_pipe *p)
904 {
905         struct dcerpc_binding_handle *b = p->binding_handle;
906         DATA_BLOB out_blob;
907         struct bkrp_client_side_unwrapped resp;
908         enum dcerpc_AuthType auth_type;
909         enum dcerpc_AuthLevel auth_level;
910
911         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
912
913         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
914                 struct bkrp_BackupKey *r = createRestoreGUIDStruct(tctx, p, 2, &out_blob,
915                                         false, false, false, false, false, false, false);
916                 torture_assert(tctx, r != NULL, "createRestoreGUIDStruct failed");
917                 torture_assert_ntstatus_ok(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r), "Restore GUID");
918                 out_blob.length = *r->out.data_out_len;
919                 torture_assert_werr_equal(tctx, r->out.result, WERR_OK, "Restore GUID");
920                 torture_assert_ndr_err_equal(tctx,
921                                              ndr_pull_struct_blob(&out_blob, tctx, &resp,
922                                                                 (ndr_pull_flags_fn_t)ndr_pull_bkrp_client_side_unwrapped),
923                                              NDR_ERR_SUCCESS,
924                                              "Unable to unmarshall bkrp_client_side_unwrapped");
925                 torture_assert_str_equal(tctx, (char*)resp.secret.data, secret, "Wrong secret");
926         } else {
927                 struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, 2, &out_blob);
928                 torture_assert_ntstatus_equal(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r),
929                         NT_STATUS_ACCESS_DENIED, "Get GUID");
930         }
931         return true;
932 }
933
934 static bool test_RestoreGUID_badmagiconsecret(struct torture_context *tctx,
935                                               struct dcerpc_pipe *p)
936 {
937         enum ndr_err_code ndr_err;
938         struct dcerpc_binding_handle *b = p->binding_handle;
939         DATA_BLOB out_blob;
940         struct bkrp_client_side_unwrapped resp;
941         enum dcerpc_AuthType auth_type;
942         enum dcerpc_AuthLevel auth_level;
943
944         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
945
946         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
947                 struct bkrp_BackupKey *r = createRestoreGUIDStruct(tctx, p, 3, &out_blob,
948                                         false, false, false, true, false, false, false);
949                 torture_assert(tctx, r != NULL, "createRestoreGUIDStruct failed");
950                 torture_assert_ntstatus_ok(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r), "Restore GUID");
951                 out_blob.length = *r->out.data_out_len;
952                 ndr_err = ndr_pull_struct_blob(&out_blob, tctx, &resp, (ndr_pull_flags_fn_t)ndr_pull_bkrp_client_side_unwrapped);
953                 torture_assert_int_equal(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), 0, "Unable to unmarshall bkrp_client_side_unwrapped");
954                 torture_assert_werr_equal(tctx, r->out.result, WERR_INVALID_DATA, "Wrong error code while providing bad magic in secret");
955         } else {
956                 struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, 2, &out_blob);
957                 torture_assert_ntstatus_equal(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r),
958                         NT_STATUS_ACCESS_DENIED, "Get GUID");
959         }
960         return true;
961 }
962
963 static bool test_RestoreGUID_emptyrequest(struct torture_context *tctx,
964                                           struct dcerpc_pipe *p)
965 {
966         struct dcerpc_binding_handle *b = p->binding_handle;
967         DATA_BLOB out_blob;
968         enum dcerpc_AuthType auth_type;
969         enum dcerpc_AuthLevel auth_level;
970
971         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
972
973         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
974                 struct bkrp_BackupKey *r = createRestoreGUIDStruct(tctx, p, 3, &out_blob,
975                                         false, false, false, true, false, false, true);
976
977                 torture_assert(tctx, r != NULL, "createRestoreGUIDStruct failed");
978                 r->in.data_in = talloc(tctx, uint8_t);
979                 r->in.data_in_len = 0;
980                 r->in.param = 0;
981                 torture_assert_ntstatus_ok(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r), "Restore GUID");
982                 out_blob.length = *r->out.data_out_len;
983                 torture_assert_werr_equal(tctx, r->out.result, WERR_INVALID_PARAMETER, "Bad error code on wrong has in access check");
984         } else {
985                 struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, 2, &out_blob);
986                 torture_assert_ntstatus_equal(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r),
987                         NT_STATUS_ACCESS_DENIED, "Get GUID");
988         }
989         return true;
990 }
991
992 static bool test_RestoreGUID_badcertguid(struct torture_context *tctx,
993                                          struct dcerpc_pipe *p)
994 {
995         enum ndr_err_code ndr_err;
996         struct dcerpc_binding_handle *b = p->binding_handle;
997         DATA_BLOB out_blob;
998         struct bkrp_client_side_unwrapped resp;
999         enum dcerpc_AuthType auth_type;
1000         enum dcerpc_AuthLevel auth_level;
1001
1002         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
1003
1004         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1005                 struct bkrp_BackupKey *r = createRestoreGUIDStruct(tctx, p, 3, &out_blob,
1006                                         false, false, false, false, false, false, true);
1007                 torture_assert(tctx, r != NULL, "createRestoreGUIDStruct() failed");
1008                 torture_assert_ntstatus_ok(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r), "Restore GUID");
1009                 out_blob.length = *r->out.data_out_len;
1010                 ndr_err = ndr_pull_struct_blob(&out_blob, tctx, &resp, (ndr_pull_flags_fn_t)ndr_pull_bkrp_client_side_unwrapped);
1011                 torture_assert_int_equal(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), 0, "Unable to unmarshall bkrp_client_side_unwrapped");
1012
1013                 /*
1014                  * Windows 2012R2 has, presumably, a programming error
1015                  * returning an NTSTATUS code on this interface
1016                  */
1017                 if (W_ERROR_V(r->out.result) != NT_STATUS_V(NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1018                         torture_assert_werr_equal(tctx, r->out.result, WERR_INVALID_DATA, "Bad error code on wrong has in access check");
1019                 }
1020         } else {
1021                 struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, 2, &out_blob);
1022                 torture_assert_ntstatus_equal(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r),
1023                         NT_STATUS_ACCESS_DENIED, "Get GUID");
1024         }
1025         return true;
1026 }
1027
1028 static bool test_RestoreGUID_badmagicaccesscheck(struct torture_context *tctx,
1029                                                  struct dcerpc_pipe *p)
1030 {
1031         enum ndr_err_code ndr_err;
1032         struct dcerpc_binding_handle *b = p->binding_handle;
1033         DATA_BLOB out_blob;
1034         struct bkrp_client_side_unwrapped resp;
1035         enum dcerpc_AuthType auth_type;
1036         enum dcerpc_AuthLevel auth_level;
1037
1038         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
1039
1040         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1041                 struct bkrp_BackupKey *r = createRestoreGUIDStruct(tctx, p, 2, &out_blob,
1042                                         false, false, false, false, true, false, false);
1043                 torture_assert(tctx, r != NULL, "createRestoreGUIDStruct failed");
1044                 torture_assert_ntstatus_ok(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r), "Restore GUID");
1045                 out_blob.length = *r->out.data_out_len;
1046                 ndr_err = ndr_pull_struct_blob(&out_blob, tctx, &resp, (ndr_pull_flags_fn_t)ndr_pull_bkrp_client_side_unwrapped);
1047                 torture_assert_int_equal(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), 0, "Unable to unmarshall bkrp_client_side_unwrapped");
1048                 torture_assert_werr_equal(tctx, r->out.result, WERR_INVALID_DATA, "Bad error code on wrong has in access check");
1049         } else {
1050                 struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, 2, &out_blob);
1051                 torture_assert_ntstatus_equal(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r),
1052                         NT_STATUS_ACCESS_DENIED, "Get GUID");
1053         }
1054         return true;
1055 }
1056
1057 static bool test_RestoreGUID_badhashaccesscheck(struct torture_context *tctx,
1058                                                 struct dcerpc_pipe *p)
1059 {
1060         enum ndr_err_code ndr_err;
1061         struct dcerpc_binding_handle *b = p->binding_handle;
1062         DATA_BLOB out_blob;
1063         struct bkrp_client_side_unwrapped resp;
1064         enum dcerpc_AuthType auth_type;
1065         enum dcerpc_AuthLevel auth_level;
1066
1067         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
1068
1069         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1070                 struct bkrp_BackupKey *r = createRestoreGUIDStruct(tctx, p, 2, &out_blob,
1071                                         false, false, false, false, false, true, false);
1072                 torture_assert(tctx, r != NULL, "createRestoreGUIDStruct failed");
1073                 torture_assert_ntstatus_ok(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r), "Restore GUID");
1074                 out_blob.length = *r->out.data_out_len;
1075                 ndr_err = ndr_pull_struct_blob(&out_blob, tctx, &resp, (ndr_pull_flags_fn_t)ndr_pull_bkrp_client_side_unwrapped);
1076                 torture_assert_int_equal(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), 0, "Unable to unmarshall bkrp_client_side_unwrapped");
1077                 torture_assert_werr_equal(tctx, r->out.result, WERR_INVALID_DATA, "Bad error code on wrong has in access check");
1078         } else {
1079                 struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, 2, &out_blob);
1080                 torture_assert_ntstatus_equal(tctx, dcerpc_bkrp_BackupKey_r(b, tctx, r),
1081                         NT_STATUS_ACCESS_DENIED, "Get GUID");
1082         }
1083         return true;
1084 }
1085
1086 /*
1087  * Check that the RSA modulus in the certificate of the DCs has 2048 bits.
1088  */
1089 static bool test_RetrieveBackupKeyGUID_2048bits(struct torture_context *tctx,
1090                                         struct dcerpc_pipe *p)
1091 {
1092         struct dcerpc_binding_handle *b = p->binding_handle;
1093         DATA_BLOB out_blob;
1094         struct bkrp_BackupKey *r = createRetrieveBackupKeyGUIDStruct(tctx, p, 2, &out_blob);
1095         enum dcerpc_AuthType auth_type;
1096         enum dcerpc_AuthLevel auth_level;
1097
1098         hx509_context hctx;
1099         int hret;
1100         hx509_cert cert;
1101         SubjectPublicKeyInfo spki;
1102         RSA *rsa;
1103         int RSA_returned_bits;
1104
1105         torture_assert(tctx, r != NULL, "createRetrieveBackupKeyGUIDStruct failed");
1106
1107         hx509_context_init(&hctx);
1108
1109         if (r == NULL) {
1110                 return false;
1111         }
1112
1113         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
1114
1115         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1116                 const unsigned char *spki_spk_data;
1117                 torture_assert_ntstatus_ok(tctx,
1118                                 dcerpc_bkrp_BackupKey_r(b, tctx, r),
1119                                 "Get GUID");
1120
1121                 torture_assert_werr_ok(tctx, r->out.result,
1122                                        "Get GUID");
1123
1124                 out_blob.length = *r->out.data_out_len;
1125
1126                 hret = hx509_cert_init_data(hctx, out_blob.data, out_blob.length, &cert);
1127                 torture_assert_int_equal(tctx, hret, 0, "hx509_cert_init_data failed");
1128
1129                 hret = hx509_cert_get_SPKI(hctx, cert , &spki);
1130                 torture_assert_int_equal(tctx, hret, 0, "hx509_cert_get_SPKI failed");
1131
1132                 /* We must take a copy, as d2i_RSAPublicKey *changes* the input parameter */
1133                 spki_spk_data = spki.subjectPublicKey.data;
1134                 rsa = d2i_RSAPublicKey(NULL, &spki_spk_data, spki.subjectPublicKey.length / 8);
1135                 torture_assert_int_equal(tctx, rsa != NULL, 1, "d2i_RSAPublicKey failed");
1136
1137                 RSA_returned_bits = BN_num_bits(rsa->n);
1138                 torture_assert_int_equal(tctx,
1139                                                 RSA_returned_bits,
1140                                                 2048,
1141                                                 "RSA Key doesn't have 2048 bits");
1142
1143                 RSA_free(rsa);
1144
1145                 /*
1146                  * Because we prevented spki from being changed above,
1147                  * we can now safely call this to free it
1148                  */
1149                 free_SubjectPublicKeyInfo(&spki);
1150                 hx509_cert_free(cert);
1151                 hx509_context_free(&hctx);
1152
1153         } else {
1154                 torture_assert_ntstatus_equal(tctx,
1155                                                 dcerpc_bkrp_BackupKey_r(b, tctx, r),
1156                                                 NT_STATUS_ACCESS_DENIED,
1157                                                 "Get GUID");
1158         }
1159         return true;
1160 }
1161
1162 static bool test_ServerWrap_encrypt_decrypt(struct torture_context *tctx,
1163                                             struct dcerpc_pipe *p)
1164 {
1165         struct bkrp_BackupKey r;
1166         struct GUID guid;
1167         DATA_BLOB plaintext = data_blob_const(secret, sizeof(secret));
1168         DATA_BLOB encrypted;
1169         uint32_t enclen;
1170         DATA_BLOB decrypted;
1171         uint32_t declen;
1172         struct dcerpc_binding_handle *b = p->binding_handle;
1173         enum dcerpc_AuthType auth_type;
1174         enum dcerpc_AuthLevel auth_level;
1175         ZERO_STRUCT(r);
1176
1177         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
1178
1179         /* Encrypt */
1180         torture_assert_ntstatus_ok(tctx,
1181                                    GUID_from_string(BACKUPKEY_BACKUP_GUID, &guid),
1182                                    "obtain GUID");
1183
1184         r.in.guidActionAgent = &guid;
1185         r.in.data_in = plaintext.data;
1186         r.in.data_in_len = plaintext.length;
1187         r.in.param = 0;
1188         r.out.data_out = &encrypted.data;
1189         r.out.data_out_len = &enclen;
1190         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1191                 torture_assert_ntstatus_ok(tctx,
1192                                            dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1193                                            "encrypt");
1194         } else {
1195                 torture_assert_ntstatus_equal(tctx,
1196                                               dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1197                                               NT_STATUS_ACCESS_DENIED,
1198                                               "encrypt");
1199                 return true;
1200         }
1201         torture_assert_werr_ok(tctx,
1202                                r.out.result,
1203                                "encrypt");
1204         encrypted.length = *r.out.data_out_len;
1205
1206         /* Decrypt */
1207         torture_assert_ntstatus_ok(tctx,
1208                                    GUID_from_string(BACKUPKEY_RESTORE_GUID, &guid),
1209                                    "obtain GUID");
1210
1211         r.in.guidActionAgent = &guid;
1212         r.in.data_in = encrypted.data;
1213         r.in.data_in_len = encrypted.length;
1214         r.in.param = 0;
1215         r.out.data_out = &(decrypted.data);
1216         r.out.data_out_len = &declen;
1217         torture_assert_ntstatus_ok(tctx,
1218                                    dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1219                                    "decrypt");
1220         torture_assert_werr_ok(tctx,
1221                                r.out.result,
1222                                "decrypt");
1223         decrypted.length = *r.out.data_out_len;
1224
1225         /* Compare */
1226         torture_assert_data_blob_equal(tctx, plaintext, decrypted, "Decrypt failed");
1227
1228         /* Decrypt */
1229         torture_assert_ntstatus_ok(tctx,
1230                                    GUID_from_string(BACKUPKEY_RESTORE_GUID_WIN2K, &guid),
1231                                    "obtain GUID");
1232
1233         r.in.guidActionAgent = &guid;
1234         r.in.data_in = encrypted.data;
1235         r.in.data_in_len = encrypted.length;
1236         r.in.param = 0;
1237         r.out.data_out = &(decrypted.data);
1238         r.out.data_out_len = &declen;
1239         torture_assert_ntstatus_ok(tctx,
1240                                    dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1241                                    "decrypt");
1242         torture_assert_werr_ok(tctx,
1243                                r.out.result,
1244                                "decrypt");
1245         decrypted.length = *r.out.data_out_len;
1246
1247         /* Compare */
1248         torture_assert_data_blob_equal(tctx, plaintext, decrypted, "Decrypt failed");
1249         return true;
1250 }
1251
1252 static bool test_ServerWrap_decrypt_wrong_keyGUID(struct torture_context *tctx,
1253                                                   struct dcerpc_pipe *p)
1254 {
1255         struct bkrp_BackupKey r;
1256         struct GUID guid;
1257         DATA_BLOB plaintext = data_blob_const(secret, sizeof(secret));
1258         DATA_BLOB encrypted;
1259         uint32_t enclen;
1260         DATA_BLOB decrypted;
1261         uint32_t declen;
1262         struct dcerpc_binding_handle *b = p->binding_handle;
1263         enum ndr_err_code ndr_err;
1264         struct bkrp_server_side_wrapped server_side_wrapped;
1265         enum dcerpc_AuthType auth_type;
1266         enum dcerpc_AuthLevel auth_level;
1267         ZERO_STRUCT(r);
1268
1269         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
1270
1271         /* Encrypt */
1272         torture_assert_ntstatus_ok(tctx,
1273                                    GUID_from_string(BACKUPKEY_BACKUP_GUID, &guid),
1274                                    "obtain GUID");
1275
1276         r.in.guidActionAgent = &guid;
1277         r.in.data_in = plaintext.data;
1278         r.in.data_in_len = plaintext.length;
1279         r.in.param = 0;
1280         r.out.data_out = &encrypted.data;
1281         r.out.data_out_len = &enclen;
1282         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1283                 torture_assert_ntstatus_ok(tctx,
1284                                            dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1285                                            "encrypt");
1286         } else {
1287                 torture_assert_ntstatus_equal(tctx,
1288                                               dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1289                                               NT_STATUS_ACCESS_DENIED,
1290                                               "encrypt");
1291                 return true;
1292         }
1293         torture_assert_werr_ok(tctx,
1294                                r.out.result,
1295                                "encrypt");
1296         encrypted.length = *r.out.data_out_len;
1297
1298         ndr_err = ndr_pull_struct_blob(&encrypted, tctx, &server_side_wrapped,
1299                                        (ndr_pull_flags_fn_t)ndr_pull_bkrp_server_side_wrapped);
1300         torture_assert_ndr_err_equal(tctx, ndr_err, NDR_ERR_SUCCESS, "pull of server_side_wrapped");
1301
1302         /* Change the GUID */
1303         server_side_wrapped.guid = GUID_random();
1304
1305         ndr_err = ndr_push_struct_blob(&encrypted, tctx, &server_side_wrapped,
1306                                        (ndr_push_flags_fn_t)ndr_push_bkrp_server_side_wrapped);
1307         torture_assert_ndr_err_equal(tctx, ndr_err, NDR_ERR_SUCCESS, "push of server_side_wrapped");
1308
1309         /* Decrypt */
1310         torture_assert_ntstatus_ok(tctx,
1311                                    GUID_from_string(BACKUPKEY_RESTORE_GUID, &guid),
1312                                    "obtain GUID");
1313
1314         r.in.guidActionAgent = &guid;
1315         r.in.data_in = encrypted.data;
1316         r.in.data_in_len = encrypted.length;
1317         r.in.param = 0;
1318         r.out.data_out = &(decrypted.data);
1319         r.out.data_out_len = &declen;
1320         torture_assert_ntstatus_ok(tctx,
1321                                    dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1322                                    "decrypt");
1323         torture_assert_werr_equal(tctx,
1324                                   r.out.result,
1325                                   WERR_INVALID_DATA,
1326                                   "decrypt should fail with WERR_INVALID_DATA");
1327
1328         /* Decrypt */
1329         torture_assert_ntstatus_ok(tctx,
1330                                    GUID_from_string(BACKUPKEY_RESTORE_GUID_WIN2K, &guid),
1331                                    "obtain GUID");
1332
1333         r.in.guidActionAgent = &guid;
1334         r.in.data_in = encrypted.data;
1335         r.in.data_in_len = encrypted.length;
1336         r.in.param = 0;
1337         r.out.data_out = &(decrypted.data);
1338         r.out.data_out_len = &declen;
1339         torture_assert_ntstatus_ok(tctx,
1340                                    dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1341                                    "decrypt");
1342         torture_assert_werr_equal(tctx,
1343                                   r.out.result,
1344                                   WERR_INVALID_DATA,
1345                                   "decrypt should fail with WERR_INVALID_DATA");
1346
1347         return true;
1348 }
1349
1350 static bool test_ServerWrap_decrypt_empty_request(struct torture_context *tctx,
1351                                                  struct dcerpc_pipe *p)
1352 {
1353         struct bkrp_BackupKey r;
1354         struct GUID guid;
1355         DATA_BLOB decrypted;
1356         uint32_t declen;
1357         struct dcerpc_binding_handle *b = p->binding_handle;
1358         uint8_t short_request[4] = { 1, 0, 0, 0 };
1359         enum dcerpc_AuthType auth_type;
1360         enum dcerpc_AuthLevel auth_level;
1361         ZERO_STRUCT(r);
1362
1363         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
1364
1365         /* Decrypt */
1366         torture_assert_ntstatus_ok(tctx,
1367                                    GUID_from_string(BACKUPKEY_RESTORE_GUID, &guid),
1368                                    "obtain GUID");
1369
1370         r.in.guidActionAgent = &guid;
1371         r.in.data_in = short_request;
1372         r.in.data_in_len = 0;
1373         r.in.param = 0;
1374         r.out.data_out = &(decrypted.data);
1375         r.out.data_out_len = &declen;
1376         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1377                 torture_assert_ntstatus_ok(tctx,
1378                                            dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1379                                            "encrypt");
1380         } else {
1381                 torture_assert_ntstatus_equal(tctx,
1382                                               dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1383                                               NT_STATUS_ACCESS_DENIED,
1384                                               "encrypt");
1385                 return true;
1386         }
1387         torture_assert_werr_equal(tctx,
1388                                   r.out.result,
1389                                   WERR_INVALID_PARAMETER,
1390                                   "decrypt should fail with WERR_INVALID_PARAMETER");
1391
1392         /* Decrypt */
1393         torture_assert_ntstatus_ok(tctx,
1394                                    GUID_from_string(BACKUPKEY_RESTORE_GUID_WIN2K, &guid),
1395                                    "obtain GUID");
1396
1397         r.in.guidActionAgent = &guid;
1398         r.in.data_in = short_request;
1399         r.in.data_in_len = 0;
1400         r.in.param = 0;
1401         r.out.data_out = &(decrypted.data);
1402         r.out.data_out_len = &declen;
1403         torture_assert_ntstatus_ok(tctx,
1404                                    dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1405                                    "decrypt");
1406         torture_assert_werr_equal(tctx,
1407                                   r.out.result,
1408                                   WERR_INVALID_PARAMETER,
1409                                   "decrypt should fail with WERR_INVALID_PARAMETER");
1410
1411         /* Decrypt */
1412         torture_assert_ntstatus_ok(tctx,
1413                                    GUID_from_string(BACKUPKEY_RESTORE_GUID, &guid),
1414                                    "obtain GUID");
1415
1416         r.in.guidActionAgent = &guid;
1417         r.in.data_in = NULL;
1418         r.in.data_in_len = 0;
1419         r.in.param = 0;
1420         r.out.data_out = &(decrypted.data);
1421         r.out.data_out_len = &declen;
1422         torture_assert_ntstatus_equal(tctx,
1423                                       dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1424                                       NT_STATUS_INVALID_PARAMETER_MIX,
1425                                       "decrypt");
1426
1427         /* Decrypt */
1428         torture_assert_ntstatus_ok(tctx,
1429                                    GUID_from_string(BACKUPKEY_RESTORE_GUID_WIN2K, &guid),
1430                                    "obtain GUID");
1431
1432         r.in.guidActionAgent = &guid;
1433         r.in.data_in = NULL;
1434         r.in.data_in_len = 0;
1435         r.in.param = 0;
1436         r.out.data_out = &(decrypted.data);
1437         r.out.data_out_len = &declen;
1438         torture_assert_ntstatus_equal(tctx,
1439                                       dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1440                                       NT_STATUS_INVALID_PARAMETER_MIX,
1441                                       "decrypt");
1442
1443         return true;
1444 }
1445
1446
1447 static bool test_ServerWrap_decrypt_short_request(struct torture_context *tctx,
1448                                                  struct dcerpc_pipe *p)
1449 {
1450         struct bkrp_BackupKey r;
1451         struct GUID guid;
1452         DATA_BLOB decrypted;
1453         uint32_t declen;
1454         struct dcerpc_binding_handle *b = p->binding_handle;
1455         uint8_t short_request[4] = { 1, 0, 0, 0 };
1456         enum dcerpc_AuthType auth_type;
1457         enum dcerpc_AuthLevel auth_level;
1458         ZERO_STRUCT(r);
1459
1460         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
1461
1462         /* Decrypt */
1463         torture_assert_ntstatus_ok(tctx,
1464                                    GUID_from_string(BACKUPKEY_RESTORE_GUID, &guid),
1465                                    "obtain GUID");
1466
1467         r.in.guidActionAgent = &guid;
1468         r.in.data_in = short_request;
1469         r.in.data_in_len = 4;
1470         r.in.param = 0;
1471         r.out.data_out = &(decrypted.data);
1472         r.out.data_out_len = &declen;
1473         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1474                 torture_assert_ntstatus_ok(tctx,
1475                                            dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1476                                            "encrypt");
1477         } else {
1478                 torture_assert_ntstatus_equal(tctx,
1479                                               dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1480                                               NT_STATUS_ACCESS_DENIED,
1481                                               "encrypt");
1482                 return true;
1483         }
1484         torture_assert_werr_equal(tctx,
1485                                   r.out.result,
1486                                   WERR_INVALID_PARAMETER,
1487                                   "decrypt should fail with WERR_INVALID_PARAMETER");
1488
1489         /* Decrypt */
1490         torture_assert_ntstatus_ok(tctx,
1491                                    GUID_from_string(BACKUPKEY_RESTORE_GUID_WIN2K, &guid),
1492                                    "obtain GUID");
1493
1494         r.in.guidActionAgent = &guid;
1495         r.in.data_in = short_request;
1496         r.in.data_in_len = 4;
1497         r.in.param = 0;
1498         r.out.data_out = &(decrypted.data);
1499         r.out.data_out_len = &declen;
1500         torture_assert_ntstatus_ok(tctx,
1501                                    dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1502                                    "decrypt");
1503         torture_assert_werr_equal(tctx,
1504                                   r.out.result,
1505                                   WERR_INVALID_PARAMETER,
1506                                   "decrypt should fail with WERR_INVALID_PARAMETER");
1507
1508         /* Decrypt */
1509         torture_assert_ntstatus_ok(tctx,
1510                                    GUID_from_string(BACKUPKEY_RESTORE_GUID, &guid),
1511                                    "obtain GUID");
1512
1513         r.in.guidActionAgent = &guid;
1514         r.in.data_in = short_request;
1515         r.in.data_in_len = 1;
1516         r.in.param = 0;
1517         r.out.data_out = &(decrypted.data);
1518         r.out.data_out_len = &declen;
1519         torture_assert_ntstatus_ok(tctx,
1520                                    dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1521                                    "decrypt");
1522         torture_assert_werr_equal(tctx,
1523                                   r.out.result,
1524                                   WERR_INVALID_PARAMETER,
1525                                   "decrypt should fail with WERR_INVALID_PARAMETER");
1526
1527         /* Decrypt */
1528         torture_assert_ntstatus_ok(tctx,
1529                                    GUID_from_string(BACKUPKEY_RESTORE_GUID_WIN2K, &guid),
1530                                    "obtain GUID");
1531
1532         r.in.guidActionAgent = &guid;
1533         r.in.data_in = short_request;
1534         r.in.data_in_len = 1;
1535         r.in.param = 0;
1536         r.out.data_out = &(decrypted.data);
1537         r.out.data_out_len = &declen;
1538         torture_assert_ntstatus_ok(tctx,
1539                                    dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1540                                    "decrypt");
1541         torture_assert_werr_equal(tctx,
1542                                   r.out.result,
1543                                   WERR_INVALID_PARAMETER,
1544                                   "decrypt should fail with WERR_INVALID_PARAMETER");
1545
1546         return true;
1547 }
1548
1549 static bool test_ServerWrap_encrypt_decrypt_manual(struct torture_context *tctx,
1550                                                    struct bkrp_server_side_wrapped *server_side_wrapped,
1551                                                    enum test_wrong wrong)
1552 {
1553         char *lsa_binding_string = NULL;
1554         struct dcerpc_binding *lsa_binding = NULL;
1555         struct dcerpc_pipe *lsa_p = NULL;
1556         struct dcerpc_binding_handle *lsa_b = NULL;
1557         struct lsa_OpenSecret r_secret;
1558         struct lsa_QuerySecret r_query_secret;
1559         struct policy_handle *handle, sec_handle;
1560         struct bkrp_BackupKey r;
1561         struct GUID preferred_key_guid;
1562         DATA_BLOB plaintext = data_blob_const(secret, sizeof(secret));
1563         DATA_BLOB preferred_key, preferred_key_clear, session_key,
1564                 decrypt_key, decrypt_key_clear, encrypted_blob, symkey_blob,
1565                 sid_blob;
1566         struct bkrp_dc_serverwrap_key server_key;
1567         struct lsa_DATA_BUF_PTR bufp1;
1568         char *key_guid_string;
1569         struct bkrp_rc4encryptedpayload rc4payload;
1570         struct dom_sid *caller_sid;
1571         uint8_t symkey[20]; /* SHA-1 hash len */
1572         uint8_t mackey[20]; /* SHA-1 hash len */
1573         uint8_t mac[20]; /* SHA-1 hash len */
1574         unsigned int hash_len;
1575         HMAC_CTX ctx;
1576         ZERO_STRUCT(r);
1577         ZERO_STRUCT(r_secret);
1578         ZERO_STRUCT(r_query_secret);
1579
1580         /* Now read BCKUPKEY_P and prove we can do a matching decrypt and encrypt */
1581
1582         /* lsa_OpenSecret only works with ncacn_np and AUTH_LEVEL_NONE */
1583         lsa_binding_string = talloc_asprintf(tctx, "ncacn_np:%s",
1584                                 torture_setting_string(tctx, "host", NULL));
1585         torture_assert(tctx, lsa_binding_string != NULL, "lsa_binding_string");
1586
1587         torture_assert_ntstatus_ok(tctx,
1588                 dcerpc_parse_binding(tctx, lsa_binding_string, &lsa_binding),
1589                 "Failed to parse dcerpc binding");
1590
1591         torture_assert_ntstatus_ok(tctx,
1592                                    dcerpc_pipe_connect_b(tctx, &lsa_p,
1593                                         lsa_binding, &ndr_table_lsarpc,
1594                                         popt_get_cmdline_credentials(),
1595                                         tctx->ev, tctx->lp_ctx),
1596                                    "Opening LSA pipe");
1597         lsa_b = lsa_p->binding_handle;
1598
1599         torture_assert(tctx, test_lsa_OpenPolicy2(lsa_b, tctx, &handle), "OpenPolicy failed");
1600         r_secret.in.name.string = "G$BCKUPKEY_P";
1601
1602         r_secret.in.handle = handle;
1603         r_secret.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1604         r_secret.out.sec_handle = &sec_handle;
1605
1606         torture_comment(tctx, "Testing OpenSecret\n");
1607
1608         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_OpenSecret_r(lsa_b, tctx, &r_secret),
1609                                    "OpenSecret failed");
1610         torture_assert_ntstatus_ok(tctx, r_secret.out.result,
1611                                    "OpenSecret failed");
1612
1613         r_query_secret.in.sec_handle = &sec_handle;
1614         r_query_secret.in.new_val = &bufp1;
1615         bufp1.buf = NULL;
1616
1617         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QuerySecret_r(lsa_b, tctx, &r_query_secret),
1618                 "QuerySecret failed");
1619         torture_assert_ntstatus_ok(tctx, r_query_secret.out.result,
1620                                    "QuerySecret failed");
1621
1622
1623         preferred_key.data = r_query_secret.out.new_val->buf->data;
1624         preferred_key.length = r_query_secret.out.new_val->buf->size;
1625         torture_assert_ntstatus_ok(tctx, dcerpc_fetch_session_key(lsa_p, &session_key),
1626                                    "dcerpc_fetch_session_key failed");
1627
1628         torture_assert_ntstatus_ok(tctx,
1629                                    sess_decrypt_blob(tctx,
1630                                                      &preferred_key, &session_key, &preferred_key_clear),
1631                                    "sess_decrypt_blob failed");
1632
1633         torture_assert_ntstatus_ok(tctx, GUID_from_ndr_blob(&preferred_key_clear, &preferred_key_guid),
1634                                    "GUID parse failed");
1635
1636         torture_assert_guid_equal(tctx, server_side_wrapped->guid,
1637                                   preferred_key_guid,
1638                                   "GUID didn't match value pointed at by G$BCKUPKEY_P");
1639
1640         /* And read BCKUPKEY_<guid> and get the actual key */
1641
1642         key_guid_string = GUID_string(tctx, &server_side_wrapped->guid);
1643         r_secret.in.name.string = talloc_asprintf(tctx, "G$BCKUPKEY_%s", key_guid_string);
1644
1645         r_secret.in.handle = handle;
1646         r_secret.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1647         r_secret.out.sec_handle = &sec_handle;
1648
1649         torture_comment(tctx, "Testing OpenSecret\n");
1650
1651         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_OpenSecret_r(lsa_b, tctx, &r_secret),
1652                                    "OpenSecret failed");
1653         torture_assert_ntstatus_ok(tctx, r_secret.out.result,
1654                                    "OpenSecret failed");
1655
1656         r_query_secret.in.sec_handle = &sec_handle;
1657         r_query_secret.in.new_val = &bufp1;
1658
1659         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QuerySecret_r(lsa_b, tctx, &r_query_secret),
1660                                    "QuerySecret failed");
1661         torture_assert_ntstatus_ok(tctx, r_query_secret.out.result,
1662                                    "QuerySecret failed");
1663
1664
1665         decrypt_key.data = r_query_secret.out.new_val->buf->data;
1666         decrypt_key.length = r_query_secret.out.new_val->buf->size;
1667
1668         torture_assert_ntstatus_ok(tctx,
1669                                    sess_decrypt_blob(tctx,
1670                                                      &decrypt_key, &session_key, &decrypt_key_clear),
1671                                    "sess_decrypt_blob failed");
1672
1673         torture_assert_ndr_err_equal(tctx, ndr_pull_struct_blob(&decrypt_key_clear, tctx, &server_key,
1674                                                                 (ndr_pull_flags_fn_t)ndr_pull_bkrp_dc_serverwrap_key),
1675                                      NDR_ERR_SUCCESS, "Failed to parse server_key");
1676
1677         torture_assert_int_equal(tctx, server_key.magic, 1, "Failed to correctly decrypt server key");
1678
1679         /*
1680          * This is *not* the leading 64 bytes, as indicated in MS-BKRP 3.1.4.1.1
1681          * BACKUPKEY_BACKUP_GUID, it really is the whole key
1682          */
1683         HMAC(EVP_sha1(), server_key.key, sizeof(server_key.key),
1684              server_side_wrapped->r2, sizeof(server_side_wrapped->r2),
1685              symkey, &hash_len);
1686
1687         /* rc4 decrypt sid and secret using sym key */
1688         symkey_blob = data_blob_const(symkey, sizeof(symkey));
1689
1690         encrypted_blob = data_blob_talloc(tctx, server_side_wrapped->rc4encryptedpayload,
1691                                           server_side_wrapped->ciphertext_length);
1692
1693         arcfour_crypt_blob(encrypted_blob.data, encrypted_blob.length, &symkey_blob);
1694
1695         torture_assert_ndr_err_equal(tctx, ndr_pull_struct_blob(&encrypted_blob, tctx, &rc4payload,
1696                                        (ndr_pull_flags_fn_t)ndr_pull_bkrp_rc4encryptedpayload),
1697                                      NDR_ERR_SUCCESS, "Failed to parse rc4encryptedpayload");
1698
1699         torture_assert_int_equal(tctx, rc4payload.secret_data.length,
1700                                  server_side_wrapped->payload_length,
1701                                  "length of decrypted payload not the length declared in surrounding structure");
1702
1703         /*
1704          * This is *not* the leading 64 bytes, as indicated in MS-BKRP 3.1.4.1.1
1705          * BACKUPKEY_BACKUP_GUID, it really is the whole key
1706          */
1707         HMAC(EVP_sha1(), server_key.key, sizeof(server_key.key),
1708              rc4payload.r3, sizeof(rc4payload.r3),
1709              mackey, &hash_len);
1710
1711         torture_assert_ndr_err_equal(tctx, ndr_push_struct_blob(&sid_blob, tctx, &rc4payload.sid,
1712                                                                 (ndr_push_flags_fn_t)ndr_push_dom_sid),
1713                                      NDR_ERR_SUCCESS, "unable to push SID");
1714
1715         HMAC_CTX_init(&ctx);
1716         HMAC_Init_ex(&ctx, mackey, hash_len, EVP_sha1(), NULL);
1717         /* SID field */
1718         HMAC_Update(&ctx, sid_blob.data, sid_blob.length);
1719         /* Secret field */
1720         HMAC_Update(&ctx, rc4payload.secret_data.data, rc4payload.secret_data.length);
1721         HMAC_Final(&ctx, mac, &hash_len);
1722         HMAC_CTX_cleanup(&ctx);
1723
1724         torture_assert_mem_equal(tctx, mac, rc4payload.mac, sizeof(mac), "mac not correct");
1725         torture_assert_int_equal(tctx, rc4payload.secret_data.length,
1726                                  plaintext.length, "decrypted data is not correct length");
1727         torture_assert_mem_equal(tctx, rc4payload.secret_data.data,
1728                                  plaintext.data, plaintext.length,
1729                                  "decrypted data is not correct");
1730
1731         /* Not strictly correct all the time, but good enough for this test */
1732         caller_sid = get_user_sid(tctx, tctx,
1733                         cli_credentials_get_username(
1734                                 popt_get_cmdline_credentials()));
1735
1736         torture_assert_sid_equal(tctx, &rc4payload.sid, caller_sid, "Secret saved with wrong SID");
1737
1738
1739         /* RE-encrypt */
1740
1741         if (wrong == WRONG_SID) {
1742                 rc4payload.sid.sub_auths[rc4payload.sid.num_auths - 1] = DOMAIN_RID_KRBTGT;
1743         }
1744
1745         dump_data_pw("mackey: \n", mackey, sizeof(mackey));
1746
1747         torture_assert_ndr_err_equal(tctx,
1748                                      ndr_push_struct_blob(&sid_blob, tctx, &rc4payload.sid,
1749                                                           (ndr_push_flags_fn_t)ndr_push_dom_sid),
1750                                      NDR_ERR_SUCCESS,
1751                                      "push of sid failed");
1752
1753         HMAC_CTX_init(&ctx);
1754         HMAC_Init_ex(&ctx, mackey, 20, EVP_sha1(), NULL);
1755         /* SID field */
1756         HMAC_Update(&ctx, sid_blob.data, sid_blob.length);
1757         /* Secret field */
1758         HMAC_Update(&ctx, rc4payload.secret_data.data, rc4payload.secret_data.length);
1759         HMAC_Final(&ctx, rc4payload.mac, &hash_len);
1760         HMAC_CTX_cleanup(&ctx);
1761
1762         dump_data_pw("rc4payload.mac: \n", rc4payload.mac, sizeof(rc4payload.mac));
1763
1764         torture_assert_ndr_err_equal(tctx,
1765                                      ndr_push_struct_blob(&encrypted_blob, tctx, &rc4payload,
1766                                                           (ndr_push_flags_fn_t)ndr_push_bkrp_rc4encryptedpayload),
1767                                      NDR_ERR_SUCCESS,
1768                                      "push of rc4payload failed");
1769
1770         if (wrong == WRONG_KEY) {
1771                 symkey_blob.data[0] = 78;
1772                 symkey_blob.data[1] = 78;
1773                 symkey_blob.data[2] = 78;
1774         }
1775
1776         /* rc4 encrypt sid and secret using sym key */
1777         arcfour_crypt_blob(encrypted_blob.data, encrypted_blob.length, &symkey_blob);
1778
1779         /* re-create server wrap structure */
1780
1781         torture_assert_int_equal(tctx, encrypted_blob.length,
1782                                  server_side_wrapped->ciphertext_length,
1783                                  "expected encrypted length not to change");
1784         if (wrong == RIGHT_KEY) {
1785                 torture_assert_mem_equal(tctx, server_side_wrapped->rc4encryptedpayload,
1786                                          encrypted_blob.data,
1787                                          encrypted_blob.length,
1788                                          "expected encrypted data not to change");
1789         }
1790
1791         server_side_wrapped->payload_length = rc4payload.secret_data.length;
1792         server_side_wrapped->ciphertext_length = encrypted_blob.length;
1793         server_side_wrapped->rc4encryptedpayload = encrypted_blob.data;
1794
1795         return true;
1796 }
1797
1798
1799 static bool test_ServerWrap_decrypt_wrong_stuff(struct torture_context *tctx,
1800                                                 struct dcerpc_pipe *p,
1801                                                 enum test_wrong wrong)
1802 {
1803         struct bkrp_BackupKey r;
1804         struct GUID guid;
1805         DATA_BLOB plaintext = data_blob_const(secret, sizeof(secret));
1806         DATA_BLOB encrypted;
1807         uint32_t enclen;
1808         DATA_BLOB decrypted;
1809         uint32_t declen;
1810         struct dcerpc_binding_handle *b = p->binding_handle;
1811         enum ndr_err_code ndr_err;
1812         struct bkrp_server_side_wrapped server_side_wrapped;
1813         bool repush = false;
1814         enum dcerpc_AuthType auth_type;
1815         enum dcerpc_AuthLevel auth_level;
1816         ZERO_STRUCT(r);
1817
1818         dcerpc_binding_handle_auth_info(b, &auth_type, &auth_level);
1819
1820         /* Encrypt */
1821         torture_assert_ntstatus_ok(tctx,
1822                                    GUID_from_string(BACKUPKEY_BACKUP_GUID, &guid),
1823                                    "obtain GUID");
1824
1825         r.in.guidActionAgent = &guid;
1826         r.in.data_in = plaintext.data;
1827         r.in.data_in_len = plaintext.length;
1828         r.in.param = 0;
1829         r.out.data_out = &encrypted.data;
1830         r.out.data_out_len = &enclen;
1831         if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1832                 torture_assert_ntstatus_ok(tctx,
1833                                            dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1834                                            "encrypt");
1835         } else {
1836                 torture_assert_ntstatus_equal(tctx,
1837                                               dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1838                                               NT_STATUS_ACCESS_DENIED,
1839                                               "encrypt");
1840                 return true;
1841         }
1842         torture_assert_werr_ok(tctx,
1843                                r.out.result,
1844                                "encrypt");
1845         encrypted.length = *r.out.data_out_len;
1846
1847         ndr_err = ndr_pull_struct_blob(&encrypted, tctx, &server_side_wrapped,
1848                                        (ndr_pull_flags_fn_t)ndr_pull_bkrp_server_side_wrapped);
1849         torture_assert_ndr_err_equal(tctx, ndr_err, NDR_ERR_SUCCESS, "pull of server_side_wrapped");
1850
1851         torture_assert_int_equal(tctx, server_side_wrapped.payload_length, plaintext.length,
1852                                  "wrong payload length");
1853
1854         switch (wrong) {
1855         case WRONG_MAGIC:
1856                 /* Change the magic.  Forced by our NDR layer, so do it raw */
1857                 SIVAL(encrypted.data, 0, 78);  /* valid values are 1-3 */
1858                 break;
1859         case WRONG_R2:
1860                 server_side_wrapped.r2[0] = 78;
1861                 server_side_wrapped.r2[1] = 78;
1862                 server_side_wrapped.r2[3] = 78;
1863                 repush = true;
1864                 break;
1865         case WRONG_PAYLOAD_LENGTH:
1866                 server_side_wrapped.payload_length = UINT32_MAX - 8;
1867                 repush = true;
1868                 break;
1869         case WRONG_CIPHERTEXT_LENGTH:
1870                 /*
1871                  * Change the ciphertext len.  We can't push this if
1872                  * we have it wrong, so do it raw
1873                  */
1874                 SIVAL(encrypted.data, 8, UINT32_MAX - 8);  /* valid values are 1-3 */
1875                 break;
1876         case SHORT_PAYLOAD_LENGTH:
1877                 server_side_wrapped.payload_length = server_side_wrapped.payload_length - 8;
1878                 repush = true;
1879                 break;
1880         case SHORT_CIPHERTEXT_LENGTH:
1881                 /*
1882                  * Change the ciphertext len.  We can't push this if
1883                  * we have it wrong, so do it raw
1884                  */
1885                 SIVAL(encrypted.data, 8, server_side_wrapped.ciphertext_length - 8);  /* valid values are 1-3 */
1886                 break;
1887         case ZERO_PAYLOAD_LENGTH:
1888                 server_side_wrapped.payload_length = 0;
1889                 repush = true;
1890                 break;
1891         case ZERO_CIPHERTEXT_LENGTH:
1892                 /*
1893                  * Change the ciphertext len.  We can't push this if
1894                  * we have it wrong, so do it raw
1895                  */
1896                 SIVAL(encrypted.data, 8, 0);  /* valid values are 1-3 */
1897                 break;
1898
1899         case RIGHT_KEY:
1900         case WRONG_KEY:
1901         case WRONG_SID:
1902                 torture_assert(tctx,
1903                                test_ServerWrap_encrypt_decrypt_manual(tctx, &server_side_wrapped, wrong),
1904                                "test_ServerWrap_encrypt_decrypt_manual failed");
1905                 repush = true;
1906                 break;
1907         }
1908
1909         if (repush) {
1910                 ndr_err = ndr_push_struct_blob(&encrypted, tctx, &server_side_wrapped,
1911                                                (ndr_push_flags_fn_t)ndr_push_bkrp_server_side_wrapped);
1912                 torture_assert_ndr_err_equal(tctx, ndr_err, NDR_ERR_SUCCESS, "push of server_side_wrapped");
1913         }
1914
1915         /* Decrypt */
1916         torture_assert_ntstatus_ok(tctx,
1917                                    GUID_from_string(BACKUPKEY_RESTORE_GUID, &guid),
1918                                    "obtain GUID");
1919
1920         r.in.guidActionAgent = &guid;
1921         r.in.data_in = encrypted.data;
1922         r.in.data_in_len = encrypted.length;
1923         r.in.param = 0;
1924         r.out.data_out = &(decrypted.data);
1925         r.out.data_out_len = &declen;
1926         torture_assert_ntstatus_ok(tctx,
1927                                    dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1928                                    "decrypt");
1929
1930         if ((wrong == WRONG_R2 || wrong == WRONG_KEY)
1931             && W_ERROR_EQUAL(r.out.result, WERR_INVALID_SID)) {
1932                 torture_assert_werr_equal(tctx,
1933                                           r.out.result,
1934                                           WERR_INVALID_SID,
1935                                           "decrypt should fail with WERR_INVALID_SID or WERR_INVALID_PARAMETER");
1936         } else if (wrong == RIGHT_KEY) {
1937                 torture_assert_werr_equal(tctx,
1938                                           r.out.result,
1939                                           WERR_OK,
1940                                           "decrypt should succeed!");
1941         } else if (wrong == WRONG_SID) {
1942                 torture_assert_werr_equal(tctx,
1943                                           r.out.result,
1944                                           WERR_INVALID_ACCESS,
1945                                           "decrypt should fail with WERR_INVALID_ACCESS");
1946         } else {
1947                 if (!W_ERROR_EQUAL(r.out.result, WERR_INVALID_PARAMETER)) {
1948                         torture_assert_werr_equal(tctx,
1949                                                   r.out.result,
1950                                                   WERR_INVALID_ACCESS,
1951                                                   "decrypt should fail with WERR_INVALID_ACCESS or WERR_INVALID_PARAMETER");
1952                 }
1953         }
1954
1955         /* Decrypt */
1956         torture_assert_ntstatus_ok(tctx,
1957                                    GUID_from_string(BACKUPKEY_RESTORE_GUID_WIN2K, &guid),
1958                                    "obtain GUID");
1959
1960         r.in.guidActionAgent = &guid;
1961         r.in.data_in = encrypted.data;
1962         r.in.data_in_len = encrypted.length;
1963         r.in.param = 0;
1964         r.out.data_out = &(decrypted.data);
1965         r.out.data_out_len = &declen;
1966         torture_assert_ntstatus_ok(tctx,
1967                                    dcerpc_bkrp_BackupKey_r(b, tctx, &r),
1968                                    "decrypt");
1969
1970         if ((wrong == WRONG_R2 || wrong == WRONG_KEY)
1971             && W_ERROR_EQUAL(r.out.result, WERR_INVALID_SID)) {
1972                 torture_assert_werr_equal(tctx,
1973                                           r.out.result,
1974                                           WERR_INVALID_SID,
1975                                           "decrypt should fail with WERR_INVALID_SID or WERR_INVALID_PARAMETER");
1976         } else if (wrong == RIGHT_KEY) {
1977                 torture_assert_werr_equal(tctx,
1978                                           r.out.result,
1979                                           WERR_OK,
1980                                           "decrypt should succeed!");
1981         } else if (wrong == WRONG_SID) {
1982                 torture_assert_werr_equal(tctx,
1983                                           r.out.result,
1984                                           WERR_INVALID_ACCESS,
1985                                           "decrypt should fail with WERR_INVALID_ACCESS");
1986         } else {
1987                 if (!W_ERROR_EQUAL(r.out.result, WERR_INVALID_ACCESS)
1988                     && !W_ERROR_EQUAL(r.out.result, WERR_INVALID_PARAMETER)) {
1989                         torture_assert_werr_equal(tctx, r.out.result,
1990                                                   WERR_INVALID_DATA,
1991                                                   "decrypt should fail with WERR_INVALID_ACCESS, WERR_INVALID_PARAMETER or WERR_INVALID_DATA");
1992                 }
1993         }
1994
1995         return true;
1996 }
1997
1998 static bool test_ServerWrap_decrypt_wrong_magic(struct torture_context *tctx,
1999                                                 struct dcerpc_pipe *p)
2000 {
2001         return test_ServerWrap_decrypt_wrong_stuff(tctx, p, WRONG_MAGIC);
2002 }
2003
2004 static bool test_ServerWrap_decrypt_wrong_r2(struct torture_context *tctx,
2005                                                 struct dcerpc_pipe *p)
2006 {
2007         return test_ServerWrap_decrypt_wrong_stuff(tctx, p, WRONG_R2);
2008 }
2009
2010 static bool test_ServerWrap_decrypt_wrong_payload_length(struct torture_context *tctx,
2011                                                          struct dcerpc_pipe *p)
2012 {
2013         return test_ServerWrap_decrypt_wrong_stuff(tctx, p, WRONG_PAYLOAD_LENGTH);
2014 }
2015
2016 static bool test_ServerWrap_decrypt_short_payload_length(struct torture_context *tctx,
2017                                                          struct dcerpc_pipe *p)
2018 {
2019         return test_ServerWrap_decrypt_wrong_stuff(tctx, p, SHORT_PAYLOAD_LENGTH);
2020 }
2021
2022 static bool test_ServerWrap_decrypt_zero_payload_length(struct torture_context *tctx,
2023                                                          struct dcerpc_pipe *p)
2024 {
2025         return test_ServerWrap_decrypt_wrong_stuff(tctx, p, ZERO_PAYLOAD_LENGTH);
2026 }
2027
2028 static bool test_ServerWrap_decrypt_wrong_ciphertext_length(struct torture_context *tctx,
2029                                                          struct dcerpc_pipe *p)
2030 {
2031         return test_ServerWrap_decrypt_wrong_stuff(tctx, p, WRONG_CIPHERTEXT_LENGTH);
2032 }
2033
2034 static bool test_ServerWrap_decrypt_short_ciphertext_length(struct torture_context *tctx,
2035                                                          struct dcerpc_pipe *p)
2036 {
2037         return test_ServerWrap_decrypt_wrong_stuff(tctx, p, SHORT_CIPHERTEXT_LENGTH);
2038 }
2039
2040 static bool test_ServerWrap_decrypt_zero_ciphertext_length(struct torture_context *tctx,
2041                                                            struct dcerpc_pipe *p)
2042 {
2043         return test_ServerWrap_decrypt_wrong_stuff(tctx, p, ZERO_CIPHERTEXT_LENGTH);
2044 }
2045
2046 static bool test_ServerWrap_encrypt_decrypt_remote_key(struct torture_context *tctx,
2047                                                        struct dcerpc_pipe *p)
2048 {
2049         return test_ServerWrap_decrypt_wrong_stuff(tctx, p, RIGHT_KEY);
2050 }
2051
2052 static bool test_ServerWrap_encrypt_decrypt_wrong_key(struct torture_context *tctx,
2053                                                        struct dcerpc_pipe *p)
2054 {
2055         return test_ServerWrap_decrypt_wrong_stuff(tctx, p, WRONG_KEY);
2056 }
2057
2058 static bool test_ServerWrap_encrypt_decrypt_wrong_sid(struct torture_context *tctx,
2059                                                       struct dcerpc_pipe *p)
2060 {
2061         return test_ServerWrap_decrypt_wrong_stuff(tctx, p, WRONG_SID);
2062 }
2063
2064 struct torture_suite *torture_rpc_backupkey(TALLOC_CTX *mem_ctx)
2065 {
2066         struct torture_suite *suite = torture_suite_create(mem_ctx, "backupkey");
2067
2068         struct torture_rpc_tcase *tcase;
2069
2070         tcase = torture_suite_add_rpc_iface_tcase(suite, "backupkey",
2071                                                   &ndr_table_backupkey);
2072
2073         torture_rpc_tcase_add_test(tcase, "retreive_backup_key_guid",
2074                                    test_RetrieveBackupKeyGUID);
2075
2076         torture_rpc_tcase_add_test(tcase, "restore_guid",
2077                                    test_RestoreGUID);
2078
2079         torture_rpc_tcase_add_test(tcase, "restore_guid version 3",
2080                                    test_RestoreGUID_v3);
2081
2082 /* We double the test in order to be sure that we don't mess stuff (ie. freeing static stuff) */
2083
2084         torture_rpc_tcase_add_test(tcase, "restore_guid_2nd",
2085                                    test_RestoreGUID);
2086
2087         torture_rpc_tcase_add_test(tcase, "unable_to_decrypt_secret",
2088                                    test_RestoreGUID_ko);
2089
2090         torture_rpc_tcase_add_test(tcase, "wrong_user_restore_guid",
2091                                    test_RestoreGUID_wronguser);
2092
2093         torture_rpc_tcase_add_test(tcase, "wrong_version_restore_guid",
2094                                    test_RestoreGUID_wrongversion);
2095
2096         torture_rpc_tcase_add_test(tcase, "bad_magic_on_secret_restore_guid",
2097                                    test_RestoreGUID_badmagiconsecret);
2098
2099         torture_rpc_tcase_add_test(tcase, "bad_hash_on_secret_restore_guid",
2100                                    test_RestoreGUID_badhashaccesscheck);
2101
2102         torture_rpc_tcase_add_test(tcase, "bad_magic_on_accesscheck_restore_guid",
2103                                    test_RestoreGUID_badmagicaccesscheck);
2104
2105         torture_rpc_tcase_add_test(tcase, "bad_cert_guid_restore_guid",
2106                                    test_RestoreGUID_badcertguid);
2107
2108         torture_rpc_tcase_add_test(tcase, "empty_request_restore_guid",
2109                                    test_RestoreGUID_emptyrequest);
2110
2111         torture_rpc_tcase_add_test(tcase, "retreive_backup_key_guid_2048_bits",
2112                                    test_RetrieveBackupKeyGUID_2048bits);
2113
2114         torture_rpc_tcase_add_test(tcase, "server_wrap_encrypt_decrypt",
2115                                    test_ServerWrap_encrypt_decrypt);
2116
2117         torture_rpc_tcase_add_test(tcase, "server_wrap_decrypt_wrong_keyGUID",
2118                                    test_ServerWrap_decrypt_wrong_keyGUID);
2119
2120         torture_rpc_tcase_add_test(tcase, "server_wrap_empty_request",
2121                                    test_ServerWrap_decrypt_empty_request);
2122
2123         torture_rpc_tcase_add_test(tcase, "server_wrap_decrypt_short_request",
2124                                    test_ServerWrap_decrypt_short_request);
2125
2126         torture_rpc_tcase_add_test(tcase, "server_wrap_decrypt_wrong_magic",
2127                                    test_ServerWrap_decrypt_wrong_magic);
2128
2129         torture_rpc_tcase_add_test(tcase, "server_wrap_decrypt_wrong_r2",
2130                                    test_ServerWrap_decrypt_wrong_r2);
2131
2132         torture_rpc_tcase_add_test(tcase, "server_wrap_decrypt_wrong_payload_length",
2133                                    test_ServerWrap_decrypt_wrong_payload_length);
2134
2135         torture_rpc_tcase_add_test(tcase, "server_wrap_decrypt_short_payload_length",
2136                                    test_ServerWrap_decrypt_short_payload_length);
2137
2138         torture_rpc_tcase_add_test(tcase, "server_wrap_decrypt_zero_payload_length",
2139                                    test_ServerWrap_decrypt_zero_payload_length);
2140
2141         torture_rpc_tcase_add_test(tcase, "server_wrap_decrypt_wrong_ciphertext_length",
2142                                    test_ServerWrap_decrypt_wrong_ciphertext_length);
2143
2144         torture_rpc_tcase_add_test(tcase, "server_wrap_decrypt_short_ciphertext_length",
2145                                    test_ServerWrap_decrypt_short_ciphertext_length);
2146
2147         torture_rpc_tcase_add_test(tcase, "server_wrap_decrypt_zero_ciphertext_length",
2148                                    test_ServerWrap_decrypt_zero_ciphertext_length);
2149
2150         torture_rpc_tcase_add_test(tcase, "server_wrap_encrypt_decrypt_remote_key",
2151                                    test_ServerWrap_encrypt_decrypt_remote_key);
2152
2153         torture_rpc_tcase_add_test(tcase, "server_wrap_encrypt_decrypt_wrong_key",
2154                                    test_ServerWrap_encrypt_decrypt_wrong_key);
2155
2156         torture_rpc_tcase_add_test(tcase, "server_wrap_encrypt_decrypt_wrong_sid",
2157                                    test_ServerWrap_encrypt_decrypt_wrong_sid);
2158
2159         return suite;
2160 }