2 * DCERPC Helper routines
3 * Günther Deschner <gd@samba.org> 2010.
4 * Simo Sorce <idra@samba.org> 2010.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "librpc/rpc/dcerpc.h"
23 #include "librpc/gen_ndr/ndr_dcerpc.h"
24 #include "librpc/gen_ndr/ndr_schannel.h"
25 #include "../libcli/auth/schannel.h"
26 #include "../libcli/auth/spnego.h"
27 #include "librpc/crypto/gse.h"
28 #include "librpc/crypto/spnego.h"
29 #include "auth/gensec/gensec.h"
32 #define DBGC_CLASS DBGC_RPC_PARSE
35 * @brief NDR Encodes a ncacn_packet
37 * @param mem_ctx The memory context the blob will be allocated on
38 * @param ptype The DCERPC packet type
39 * @param pfc_flags The DCERPC PFC Falgs
40 * @param auth_length The length of the trailing auth blob
41 * @param call_id The call ID
42 * @param u The payload of the packet
43 * @param blob [out] The encoded blob if successful
45 * @return an NTSTATUS error code
47 NTSTATUS dcerpc_push_ncacn_packet(TALLOC_CTX *mem_ctx,
48 enum dcerpc_pkt_type ptype,
52 union dcerpc_payload *u,
55 struct ncacn_packet r;
56 enum ndr_err_code ndr_err;
61 r.pfc_flags = pfc_flags;
62 r.drep[0] = DCERPC_DREP_LE;
66 r.auth_length = auth_length;
70 ndr_err = ndr_push_struct_blob(blob, mem_ctx, &r,
71 (ndr_push_flags_fn_t)ndr_push_ncacn_packet);
72 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
73 return ndr_map_error2ntstatus(ndr_err);
76 dcerpc_set_frag_length(blob, blob->length);
79 if (DEBUGLEVEL >= 10) {
80 /* set frag len for print function */
81 r.frag_length = blob->length;
82 NDR_PRINT_DEBUG(ncacn_packet, &r);
89 * @brief Decodes a ncacn_packet
91 * @param mem_ctx The memory context on which to allocate the packet
93 * @param blob The blob of data to decode
94 * @param r An empty ncacn_packet, must not be NULL
95 * @param bigendian Whether the packet is bignedian encoded
97 * @return a NTSTATUS error code
99 NTSTATUS dcerpc_pull_ncacn_packet(TALLOC_CTX *mem_ctx,
100 const DATA_BLOB *blob,
101 struct ncacn_packet *r,
104 enum ndr_err_code ndr_err;
105 struct ndr_pull *ndr;
107 ndr = ndr_pull_init_blob(blob, mem_ctx);
109 return NT_STATUS_NO_MEMORY;
112 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
115 ndr_err = ndr_pull_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, r);
117 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
119 return ndr_map_error2ntstatus(ndr_err);
123 if (DEBUGLEVEL >= 10) {
124 NDR_PRINT_DEBUG(ncacn_packet, r);
131 * @brief NDR Encodes a NL_AUTH_MESSAGE
133 * @param mem_ctx The memory context the blob will be allocated on
134 * @param r The NL_AUTH_MESSAGE to encode
135 * @param blob [out] The encoded blob if successful
137 * @return a NTSTATUS error code
139 NTSTATUS dcerpc_push_schannel_bind(TALLOC_CTX *mem_ctx,
140 struct NL_AUTH_MESSAGE *r,
143 enum ndr_err_code ndr_err;
145 ndr_err = ndr_push_struct_blob(blob, mem_ctx, r,
146 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
147 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
148 return ndr_map_error2ntstatus(ndr_err);
151 if (DEBUGLEVEL >= 10) {
152 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, r);
159 * @brief NDR Encodes a dcerpc_auth structure
161 * @param mem_ctx The memory context the blob will be allocated on
162 * @param auth_type The DCERPC Authentication Type
163 * @param auth_level The DCERPC Authentication Level
164 * @param auth_pad_length The padding added to the packet this blob will be
166 * @param auth_context_id The context id
167 * @param credentials The authentication credentials blob (signature)
168 * @param blob [out] The encoded blob if successful
170 * @return a NTSTATUS error code
172 NTSTATUS dcerpc_push_dcerpc_auth(TALLOC_CTX *mem_ctx,
173 enum dcerpc_AuthType auth_type,
174 enum dcerpc_AuthLevel auth_level,
175 uint8_t auth_pad_length,
176 uint32_t auth_context_id,
177 const DATA_BLOB *credentials,
180 struct dcerpc_auth r;
181 enum ndr_err_code ndr_err;
183 r.auth_type = auth_type;
184 r.auth_level = auth_level;
185 r.auth_pad_length = auth_pad_length;
187 r.auth_context_id = auth_context_id;
188 r.credentials = *credentials;
190 ndr_err = ndr_push_struct_blob(blob, mem_ctx, &r,
191 (ndr_push_flags_fn_t)ndr_push_dcerpc_auth);
192 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
193 return ndr_map_error2ntstatus(ndr_err);
196 if (DEBUGLEVEL >= 10) {
197 NDR_PRINT_DEBUG(dcerpc_auth, &r);
204 * @brief Decodes a dcerpc_auth blob
206 * @param mem_ctx The memory context on which to allocate the packet
208 * @param blob The blob of data to decode
209 * @param r An empty dcerpc_auth structure, must not be NULL
211 * @return a NTSTATUS error code
213 NTSTATUS dcerpc_pull_dcerpc_auth(TALLOC_CTX *mem_ctx,
214 const DATA_BLOB *blob,
215 struct dcerpc_auth *r,
218 enum ndr_err_code ndr_err;
219 struct ndr_pull *ndr;
221 ndr = ndr_pull_init_blob(blob, mem_ctx);
223 return NT_STATUS_NO_MEMORY;
226 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
229 ndr_err = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, r);
231 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
233 return ndr_map_error2ntstatus(ndr_err);
237 if (DEBUGLEVEL >= 10) {
238 NDR_PRINT_DEBUG(dcerpc_auth, r);
245 * @brief Calculate how much data we can in a packet, including calculating
246 * auth token and pad lengths.
248 * @param auth The pipe_auth_data structure for this pipe.
249 * @param header_len The length of the packet header
250 * @param data_left The data left in the send buffer
251 * @param max_xmit_frag The max fragment size.
252 * @param pad_alignment The NDR padding size.
253 * @param data_to_send [out] The max data we will send in the pdu
254 * @param frag_len [out] The total length of the fragment
255 * @param auth_len [out] The length of the auth trailer
256 * @param pad_len [out] The padding to be applied
258 * @return A NT Error status code.
260 NTSTATUS dcerpc_guess_sizes(struct pipe_auth_data *auth,
261 size_t header_len, size_t data_left,
262 size_t max_xmit_frag, size_t pad_alignment,
263 size_t *data_to_send, size_t *frag_len,
264 size_t *auth_len, size_t *pad_len)
268 struct gensec_security *gensec_security;
269 struct schannel_state *schannel_auth;
270 struct spnego_context *spnego_ctx;
273 /* no auth token cases first */
274 switch (auth->auth_level) {
275 case DCERPC_AUTH_LEVEL_NONE:
276 case DCERPC_AUTH_LEVEL_CONNECT:
277 case DCERPC_AUTH_LEVEL_PACKET:
278 max_len = max_xmit_frag - header_len;
279 *data_to_send = MIN(max_len, data_left);
282 *frag_len = header_len + *data_to_send;
285 case DCERPC_AUTH_LEVEL_PRIVACY:
288 case DCERPC_AUTH_LEVEL_INTEGRITY:
292 return NT_STATUS_INVALID_PARAMETER;
296 /* Sign/seal case, calculate auth and pad lengths */
298 max_len = max_xmit_frag - header_len - DCERPC_AUTH_TRAILER_LENGTH;
300 /* Treat the same for all authenticated rpc requests. */
301 switch (auth->auth_type) {
302 case DCERPC_AUTH_TYPE_SPNEGO:
303 spnego_ctx = talloc_get_type_abort(auth->auth_ctx,
304 struct spnego_context);
305 status = spnego_get_negotiated_mech(spnego_ctx, &gensec_security);
306 if (!NT_STATUS_IS_OK(status)) {
309 *auth_len = gensec_sig_size(gensec_security, max_len);
312 case DCERPC_AUTH_TYPE_NTLMSSP:
313 case DCERPC_AUTH_TYPE_KRB5:
314 gensec_security = talloc_get_type_abort(auth->auth_ctx,
315 struct gensec_security);
316 *auth_len = gensec_sig_size(gensec_security, max_len);
319 case DCERPC_AUTH_TYPE_SCHANNEL:
320 schannel_auth = talloc_get_type_abort(auth->auth_ctx,
321 struct schannel_state);
322 *auth_len = netsec_outgoing_sig_size(schannel_auth);
325 return NT_STATUS_INVALID_PARAMETER;
328 max_len -= *auth_len;
330 *data_to_send = MIN(max_len, data_left);
332 mod_len = (header_len + *data_to_send) % pad_alignment;
334 *pad_len = pad_alignment - mod_len;
339 if (*data_to_send + *pad_len > max_len) {
340 *data_to_send -= pad_alignment;
343 *frag_len = header_len + *data_to_send + *pad_len
344 + DCERPC_AUTH_TRAILER_LENGTH + *auth_len;
349 /*******************************************************************
350 Create and add the NTLMSSP sign/seal auth data.
351 ********************************************************************/
353 static NTSTATUS add_generic_auth_footer(struct gensec_security *gensec_security,
354 enum dcerpc_AuthLevel auth_level,
357 uint16_t data_and_pad_len = rpc_out->length
358 - DCERPC_RESPONSE_LENGTH
359 - DCERPC_AUTH_TRAILER_LENGTH;
363 if (!gensec_security) {
364 return NT_STATUS_INVALID_PARAMETER;
367 switch (auth_level) {
368 case DCERPC_AUTH_LEVEL_PRIVACY:
369 /* Data portion is encrypted. */
370 status = gensec_seal_packet(gensec_security,
373 + DCERPC_RESPONSE_LENGTH,
378 if (!NT_STATUS_IS_OK(status)) {
383 case DCERPC_AUTH_LEVEL_INTEGRITY:
384 /* Data is signed. */
385 status = gensec_sign_packet(gensec_security,
388 + DCERPC_RESPONSE_LENGTH,
393 if (!NT_STATUS_IS_OK(status)) {
400 smb_panic("bad auth level");
402 return NT_STATUS_INVALID_PARAMETER;
405 /* Finally attach the blob. */
406 if (!data_blob_append(NULL, rpc_out,
407 auth_blob.data, auth_blob.length)) {
408 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
409 (unsigned int)auth_blob.length));
410 return NT_STATUS_NO_MEMORY;
412 data_blob_free(&auth_blob);
417 /*******************************************************************
418 Check/unseal the NTLMSSP auth data. (Unseal in place).
419 ********************************************************************/
421 static NTSTATUS get_generic_auth_footer(struct gensec_security *gensec_security,
422 enum dcerpc_AuthLevel auth_level,
423 DATA_BLOB *data, DATA_BLOB *full_pkt,
424 DATA_BLOB *auth_token)
426 switch (auth_level) {
427 case DCERPC_AUTH_LEVEL_PRIVACY:
428 /* Data portion is encrypted. */
429 return gensec_unseal_packet(gensec_security,
436 case DCERPC_AUTH_LEVEL_INTEGRITY:
437 /* Data is signed. */
438 return gensec_check_packet(gensec_security,
446 return NT_STATUS_INVALID_PARAMETER;
450 /*******************************************************************
451 Create and add the schannel sign/seal auth data.
452 ********************************************************************/
454 static NTSTATUS add_schannel_auth_footer(struct schannel_state *sas,
455 enum dcerpc_AuthLevel auth_level,
458 uint8_t *data_p = rpc_out->data + DCERPC_RESPONSE_LENGTH;
459 size_t data_and_pad_len = rpc_out->length
460 - DCERPC_RESPONSE_LENGTH
461 - DCERPC_AUTH_TRAILER_LENGTH;
466 return NT_STATUS_INVALID_PARAMETER;
469 DEBUG(10,("add_schannel_auth_footer: SCHANNEL seq_num=%d\n",
472 switch (auth_level) {
473 case DCERPC_AUTH_LEVEL_PRIVACY:
474 status = netsec_outgoing_packet(sas,
481 case DCERPC_AUTH_LEVEL_INTEGRITY:
482 status = netsec_outgoing_packet(sas,
490 status = NT_STATUS_INTERNAL_ERROR;
494 if (!NT_STATUS_IS_OK(status)) {
495 DEBUG(1,("add_schannel_auth_footer: failed to process packet: %s\n",
500 if (DEBUGLEVEL >= 10) {
501 dump_NL_AUTH_SIGNATURE(talloc_tos(), &auth_blob);
504 /* Finally attach the blob. */
505 if (!data_blob_append(NULL, rpc_out,
506 auth_blob.data, auth_blob.length)) {
507 return NT_STATUS_NO_MEMORY;
509 data_blob_free(&auth_blob);
514 /*******************************************************************
515 Check/unseal the Schannel auth data. (Unseal in place).
516 ********************************************************************/
518 static NTSTATUS get_schannel_auth_footer(TALLOC_CTX *mem_ctx,
519 struct schannel_state *auth_state,
520 enum dcerpc_AuthLevel auth_level,
521 DATA_BLOB *data, DATA_BLOB *full_pkt,
522 DATA_BLOB *auth_token)
524 switch (auth_level) {
525 case DCERPC_AUTH_LEVEL_PRIVACY:
526 /* Data portion is encrypted. */
527 return netsec_incoming_packet(auth_state,
533 case DCERPC_AUTH_LEVEL_INTEGRITY:
534 /* Data is signed. */
535 return netsec_incoming_packet(auth_state,
542 return NT_STATUS_INVALID_PARAMETER;
547 * @brief Append an auth footer according to what is the current mechanism
549 * @param auth The pipe_auth_data associated with the connection
550 * @param pad_len The padding used in the packet
551 * @param rpc_out Packet blob up to and including the auth header
553 * @return A NTSTATUS error code.
555 NTSTATUS dcerpc_add_auth_footer(struct pipe_auth_data *auth,
556 size_t pad_len, DATA_BLOB *rpc_out)
558 struct schannel_state *schannel_auth;
559 struct gensec_security *gensec_security;
560 struct spnego_context *spnego_ctx;
561 char pad[CLIENT_NDR_PADDING_SIZE] = { 0, };
566 if (auth->auth_type == DCERPC_AUTH_TYPE_NONE ||
567 auth->auth_type == DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM) {
572 /* Copy the sign/seal padding data. */
573 if (!data_blob_append(NULL, rpc_out, pad, pad_len)) {
574 return NT_STATUS_NO_MEMORY;
578 /* marshall the dcerpc_auth with an actually empty auth_blob.
579 * This is needed because the ntmlssp signature includes the
580 * auth header. We will append the actual blob later. */
581 auth_blob = data_blob_null;
582 status = dcerpc_push_dcerpc_auth(rpc_out->data,
589 if (!NT_STATUS_IS_OK(status)) {
593 /* append the header */
594 if (!data_blob_append(NULL, rpc_out,
595 auth_info.data, auth_info.length)) {
596 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
597 (unsigned int)auth_info.length));
598 return NT_STATUS_NO_MEMORY;
600 data_blob_free(&auth_info);
602 /* Generate any auth sign/seal and add the auth footer. */
603 switch (auth->auth_type) {
604 case DCERPC_AUTH_TYPE_NONE:
605 case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
606 status = NT_STATUS_OK;
608 case DCERPC_AUTH_TYPE_SPNEGO:
609 spnego_ctx = talloc_get_type_abort(auth->auth_ctx,
610 struct spnego_context);
611 status = spnego_get_negotiated_mech(spnego_ctx, &gensec_security);
612 if (!NT_STATUS_IS_OK(status)) {
616 status = add_generic_auth_footer(gensec_security,
620 case DCERPC_AUTH_TYPE_KRB5:
621 case DCERPC_AUTH_TYPE_NTLMSSP:
622 gensec_security = talloc_get_type_abort(auth->auth_ctx,
623 struct gensec_security);
624 status = add_generic_auth_footer(gensec_security,
628 case DCERPC_AUTH_TYPE_SCHANNEL:
629 schannel_auth = talloc_get_type_abort(auth->auth_ctx,
630 struct schannel_state);
631 status = add_schannel_auth_footer(schannel_auth,
636 status = NT_STATUS_INVALID_PARAMETER;
644 * @brief Check authentication for request/response packets
646 * @param auth The auth data for the connection
647 * @param pkt The actual ncacn_packet
648 * @param pkt_trailer The stub_and_verifier part of the packet
649 * @param header_size The header size
650 * @param raw_pkt The whole raw packet data blob
651 * @param pad_len [out] The padding length used in the packet
653 * @return A NTSTATUS error code
655 NTSTATUS dcerpc_check_auth(struct pipe_auth_data *auth,
656 struct ncacn_packet *pkt,
657 DATA_BLOB *pkt_trailer,
662 struct schannel_state *schannel_auth;
663 struct gensec_security *gensec_security;
664 struct spnego_context *spnego_ctx;
666 struct dcerpc_auth auth_info;
667 uint32_t auth_length;
671 switch (auth->auth_level) {
672 case DCERPC_AUTH_LEVEL_PRIVACY:
673 DEBUG(10, ("Requested Privacy.\n"));
676 case DCERPC_AUTH_LEVEL_INTEGRITY:
677 DEBUG(10, ("Requested Integrity.\n"));
680 case DCERPC_AUTH_LEVEL_CONNECT:
681 if (pkt->auth_length != 0) {
687 case DCERPC_AUTH_LEVEL_NONE:
688 if (pkt->auth_length != 0) {
689 DEBUG(3, ("Got non-zero auth len on non "
690 "authenticated connection!\n"));
691 return NT_STATUS_INVALID_PARAMETER;
697 DEBUG(3, ("Unimplemented Auth Level %d",
699 return NT_STATUS_INVALID_PARAMETER;
702 /* Paranioa checks for auth_length. */
703 if (pkt->auth_length > pkt->frag_length) {
704 return NT_STATUS_INFO_LENGTH_MISMATCH;
706 if (((unsigned int)pkt->auth_length
707 + DCERPC_AUTH_TRAILER_LENGTH < (unsigned int)pkt->auth_length) ||
708 ((unsigned int)pkt->auth_length
709 + DCERPC_AUTH_TRAILER_LENGTH < DCERPC_AUTH_TRAILER_LENGTH)) {
710 /* Integer wrap attempt. */
711 return NT_STATUS_INFO_LENGTH_MISMATCH;
714 status = dcerpc_pull_auth_trailer(pkt, pkt, pkt_trailer,
715 &auth_info, &auth_length, false);
716 if (!NT_STATUS_IS_OK(status)) {
720 data = data_blob_const(raw_pkt->data + header_size,
721 pkt_trailer->length - auth_length);
722 full_pkt = data_blob_const(raw_pkt->data,
723 raw_pkt->length - auth_info.credentials.length);
725 switch (auth->auth_type) {
726 case DCERPC_AUTH_TYPE_NONE:
727 case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
730 case DCERPC_AUTH_TYPE_SPNEGO:
731 spnego_ctx = talloc_get_type_abort(auth->auth_ctx,
732 struct spnego_context);
733 status = spnego_get_negotiated_mech(spnego_ctx, &gensec_security);
734 if (!NT_STATUS_IS_OK(status)) {
738 status = get_generic_auth_footer(gensec_security,
741 &auth_info.credentials);
743 if (!NT_STATUS_IS_OK(status)) {
748 case DCERPC_AUTH_TYPE_KRB5:
749 case DCERPC_AUTH_TYPE_NTLMSSP:
751 DEBUG(10, ("GENSEC auth\n"));
753 gensec_security = talloc_get_type_abort(auth->auth_ctx,
754 struct gensec_security);
755 status = get_generic_auth_footer(gensec_security,
758 &auth_info.credentials);
759 if (!NT_STATUS_IS_OK(status)) {
764 case DCERPC_AUTH_TYPE_SCHANNEL:
766 DEBUG(10, ("SCHANNEL auth\n"));
768 schannel_auth = talloc_get_type_abort(auth->auth_ctx,
769 struct schannel_state);
770 status = get_schannel_auth_footer(pkt, schannel_auth,
773 &auth_info.credentials);
774 if (!NT_STATUS_IS_OK(status)) {
780 DEBUG(0, ("process_request_pdu: "
781 "unknown auth type %u set.\n",
782 (unsigned int)auth->auth_type));
783 return NT_STATUS_INVALID_PARAMETER;
786 /* TODO: remove later
787 * this is still needed because in the server code the
788 * pkt_trailer actually has a copy of the raw data, and they
789 * are still both used in later calls */
790 if (auth->auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
791 memcpy(pkt_trailer->data, data.data, data.length);
794 *pad_len = auth_info.auth_pad_length;
795 data_blob_free(&auth_info.credentials);