OM_uint32 GSSAPI_CALLCONV _gss_spnego_wrap_iov(OM_uint32 * minor_status, gss_ctx_id_t context_handle, int conf_req_flag, gss_qop_t qop_req, int * conf_state, gss_iov_buffer_desc *iov, int iov_count) { gssspnego_ctx ctx = (gssspnego_ctx)context_handle; *minor_status = 0; if (ctx == NULL || ctx->negotiated_ctx_id == GSS_C_NO_CONTEXT) return GSS_S_NO_CONTEXT; return gss_wrap_iov(minor_status, ctx->negotiated_ctx_id, conf_req_flag, qop_req, conf_state, iov, iov_count); }
NTSTATUS gssapi_seal_packet(gss_ctx_id_t gssapi_context, const gss_OID mech, bool hdr_signing, size_t sig_size, uint8_t *data, size_t length, const uint8_t *whole_pdu, size_t pdu_length, TALLOC_CTX *mem_ctx, DATA_BLOB *sig) { OM_uint32 maj_stat, min_stat; gss_iov_buffer_desc iov[4]; int req_seal = 1; int sealed = 0; const uint8_t *pre_sign_ptr = NULL; size_t pre_sign_len = 0; const uint8_t *post_sign_ptr = NULL; size_t post_sign_len = 0; if (hdr_signing) { const uint8_t *de = data + length; const uint8_t *we = whole_pdu + pdu_length; if (data < whole_pdu) { return NT_STATUS_INVALID_PARAMETER; } if (de > we) { return NT_STATUS_INVALID_PARAMETER; } pre_sign_len = data - whole_pdu; if (pre_sign_len > 0) { pre_sign_ptr = whole_pdu; } post_sign_len = we - de; if (post_sign_len > 0) { post_sign_ptr = de; } } sig->length = sig_size; if (sig->length == 0) { return NT_STATUS_ACCESS_DENIED; } sig->data = talloc_zero_array(mem_ctx, uint8_t, sig->length); if (sig->data == NULL) { return NT_STATUS_NO_MEMORY; } iov[0].type = GSS_IOV_BUFFER_TYPE_HEADER; iov[0].buffer.length = sig->length; iov[0].buffer.value = sig->data; if (pre_sign_ptr != NULL) { iov[1].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY; iov[1].buffer.length = pre_sign_len; iov[1].buffer.value = discard_const(pre_sign_ptr); } else { iov[1].type = GSS_IOV_BUFFER_TYPE_EMPTY; iov[1].buffer.length = 0; iov[1].buffer.value = NULL; } /* data is encrypted in place, which is ok */ iov[2].type = GSS_IOV_BUFFER_TYPE_DATA; iov[2].buffer.length = length; iov[2].buffer.value = data; if (post_sign_ptr != NULL) { iov[3].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY; iov[3].buffer.length = post_sign_len; iov[3].buffer.value = discard_const(post_sign_ptr); } else { iov[3].type = GSS_IOV_BUFFER_TYPE_EMPTY; iov[3].buffer.length = 0; iov[3].buffer.value = NULL; } maj_stat = gss_wrap_iov(&min_stat, gssapi_context, req_seal, GSS_C_QOP_DEFAULT, &sealed, iov, ARRAY_SIZE(iov)); if (GSS_ERROR(maj_stat)) { char *error_string = gssapi_error_string(mem_ctx, maj_stat, min_stat, mech); DEBUG(1, ("gss_wrap_iov failed: %s\n", error_string)); talloc_free(error_string); data_blob_free(sig); return NT_STATUS_ACCESS_DENIED; } if (req_seal == 1 && sealed == 0) { DEBUG(0, ("gss_wrap_iov says data was not sealed!\n")); data_blob_free(sig); return NT_STATUS_ACCESS_DENIED; } dump_data_pw("gssapi_seal_packet: sig\n", sig->data, sig->length); dump_data_pw("gssapi_seal_packet: sealed\n", data, length); DEBUG(10, ("Sealed %d bytes, and got %d bytes header/signature.\n", (int)iov[2].buffer.length, (int)iov[0].buffer.length)); return NT_STATUS_OK; }
static int HandleOP(WrapExt) { OM_uint32 maj_stat, min_stat; int32_t hContext, flags, bflags; krb5_data token, header, trailer; gss_ctx_id_t ctx; unsigned char *p; int conf_state, iov_len; gss_iov_buffer_desc iov[6]; ret32(c, hContext); ret32(c, flags); ret32(c, bflags); retdata(c, header); retdata(c, token); retdata(c, trailer); ctx = find_handle(c->handles, hContext, handle_context); if (ctx == NULL) errx(1, "wrap: reference to unknown context"); memset(&iov, 0, sizeof(iov)); iov_len = sizeof(iov)/sizeof(iov[0]); if (bflags & WRAP_EXP_ONLY_HEADER) iov_len -= 2; /* skip trailer and padding, aka dce-style */ iov[0].type = GSS_IOV_BUFFER_TYPE_HEADER | GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATE; if (header.length != 0) { iov[1].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY; iov[1].buffer.length = header.length; iov[1].buffer.value = header.data; } else { iov[1].type = GSS_IOV_BUFFER_TYPE_EMPTY; } iov[2].type = GSS_IOV_BUFFER_TYPE_DATA; iov[2].buffer.length = token.length; iov[2].buffer.value = token.data; if (trailer.length != 0) { iov[3].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY; iov[3].buffer.length = trailer.length; iov[3].buffer.value = trailer.data; } else { iov[3].type = GSS_IOV_BUFFER_TYPE_EMPTY; } iov[4].type = GSS_IOV_BUFFER_TYPE_PADDING | GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATE; iov[5].type = GSS_IOV_BUFFER_TYPE_TRAILER | GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATE; maj_stat = gss_wrap_iov_length(&min_stat, ctx, flags, 0, &conf_state, iov, iov_len); if (maj_stat != GSS_S_COMPLETE) errx(1, "gss_wrap_iov_length failed"); maj_stat = gss_wrap_iov(&min_stat, ctx, flags, 0, &conf_state, iov, iov_len); if (maj_stat != GSS_S_COMPLETE) errx(1, "gss_wrap_iov failed"); krb5_data_free(&token); token.length = iov[0].buffer.length + iov[2].buffer.length + iov[4].buffer.length + iov[5].buffer.length; token.data = malloc(token.length); p = token.data; memcpy(p, iov[0].buffer.value, iov[0].buffer.length); p += iov[0].buffer.length; memcpy(p, iov[2].buffer.value, iov[2].buffer.length); p += iov[2].buffer.length; memcpy(p, iov[4].buffer.value, iov[4].buffer.length); p += iov[4].buffer.length; memcpy(p, iov[5].buffer.value, iov[5].buffer.length); p += iov[5].buffer.length; gss_release_iov_buffer(NULL, iov, iov_len); put32(c, 0); /* XXX fix gsm_error */ putdata(c, token); free(token.data); return 0; }
/* * AEAD wrap API for a single piece of associated data, for compatibility * with MIT and as specified by draft-howard-gssapi-aead-00.txt. * * @ingroup gssapi */ GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL gss_wrap_aead(OM_uint32 *minor_status, gss_ctx_id_t context_handle, int conf_req_flag, gss_qop_t qop_req, gss_buffer_t input_assoc_buffer, gss_buffer_t input_payload_buffer, int *conf_state, gss_buffer_t output_message_buffer) { OM_uint32 major_status, tmp, flags = 0; gss_iov_buffer_desc iov[5]; size_t i; unsigned char *p; memset(iov, 0, sizeof(iov)); iov[0].type = GSS_IOV_BUFFER_TYPE_HEADER; iov[1].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY; if (input_assoc_buffer) iov[1].buffer = *input_assoc_buffer; iov[2].type = GSS_IOV_BUFFER_TYPE_DATA; if (input_payload_buffer) iov[2].buffer.length = input_payload_buffer->length; gss_inquire_context(minor_status, context_handle, NULL, NULL, NULL, NULL, &flags, NULL, NULL); /* krb5 mech rejects padding/trailer if DCE-style is set */ iov[3].type = (flags & GSS_C_DCE_STYLE) ? GSS_IOV_BUFFER_TYPE_EMPTY : GSS_IOV_BUFFER_TYPE_PADDING; iov[4].type = (flags & GSS_C_DCE_STYLE) ? GSS_IOV_BUFFER_TYPE_EMPTY : GSS_IOV_BUFFER_TYPE_TRAILER; major_status = gss_wrap_iov_length(minor_status, context_handle, conf_req_flag, qop_req, conf_state, iov, 5); if (GSS_ERROR(major_status)) return major_status; for (i = 0, output_message_buffer->length = 0; i < 5; i++) { if (GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_SIGN_ONLY) continue; output_message_buffer->length += iov[i].buffer.length; } output_message_buffer->value = malloc(output_message_buffer->length); if (output_message_buffer->value == NULL) { *minor_status = ENOMEM; return GSS_S_FAILURE; } for (i = 0, p = output_message_buffer->value; i < 5; i++) { if (GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_SIGN_ONLY) continue; else if (GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_DATA) memcpy(p, input_payload_buffer->value, input_payload_buffer->length); iov[i].buffer.value = p; p += iov[i].buffer.length; } major_status = gss_wrap_iov(minor_status, context_handle, conf_req_flag, qop_req, conf_state, iov, 5); if (GSS_ERROR(major_status)) gss_release_buffer(&tmp, output_message_buffer); return major_status; }
int authenticate_gss_client_wrap_iov(gss_client_state* state, const char* challenge, int protect, int *pad_len) { OM_uint32 maj_stat, min_stat; int iov_count = 3; gss_iov_buffer_desc iov[iov_count]; size_t len = 0; int ret = AUTH_GSS_CONTINUE; int conf_state; unsigned char * data = (unsigned char*)""; // Always clear out the old response if (state->response != NULL) { free(state->response); state->response = NULL; } if (challenge && *challenge) { data = base64_decode(challenge, &len); } iov[0].type = GSS_IOV_BUFFER_TYPE_HEADER | GSS_IOV_BUFFER_FLAG_ALLOCATE; iov[1].type = GSS_IOV_BUFFER_TYPE_DATA; iov[1].buffer.value = data; iov[1].buffer.length = len; iov[2].type = GSS_IOV_BUFFER_TYPE_PADDING | GSS_IOV_BUFFER_FLAG_ALLOCATE; maj_stat = gss_wrap_iov(&min_stat, /* minor_status */ state->context, /* context_handle */ protect, /* conf_req_flag */ GSS_C_QOP_DEFAULT, /* qop_req */ &conf_state, /* conf_state */ iov, /* iov */ iov_count); /* iov_count */ if (maj_stat != GSS_S_COMPLETE) { set_gss_error(maj_stat, min_stat); ret = AUTH_GSS_ERROR; } else { ret = AUTH_GSS_COMPLETE; int index = 4; OM_uint32 stoken_len= 0; int bufsize = iov[0].buffer.length+ iov[1].buffer.length+ iov[2].buffer.length+ sizeof(unsigned int); char * response = (char*)malloc(bufsize); memset(response,0,bufsize); /****************************************************** Per Microsoft 2.2.9.1.2.2.2 for kerberos encrypted data First section of data is a 32-bit unsigned int containing the length of the Security Token followed by the encrypted message. Encrypted data = |32-bit unsigned int|Message| The message must start with the security token, followed by the actual encrypted message. Message = |Security Token|encrypted data|padding iov[0] = security token iov[1] = encrypted message iov[2] = padding ******************************************************/ /* Security Token length */ stoken_len = iov[0].buffer.length; memcpy(response, &stoken_len, sizeof(unsigned int)); /* Security Token */ memcpy(response+index, iov[0].buffer.value, iov[0].buffer.length); index += iov[0].buffer.length; /* Message */ memcpy(response+index, iov[1].buffer.value, iov[1].buffer.length); index += iov[1].buffer.length; /* Padding */ *pad_len = iov[2].buffer.length; if (*pad_len > 0) { memcpy(response+index, iov[2].buffer.value, iov[2].buffer.length); index += iov[2].buffer.length; } /* encode to python returnable string */ state->responseConf = conf_state; state->response = base64_encode((const unsigned char *)response,index); free(response); } (void)gss_release_iov_buffer(&min_stat, iov, iov_count); free(data); return ret; }
static void wrapunwrap_iov(gss_ctx_id_t cctx, gss_ctx_id_t sctx, int flags, gss_OID mechoid) { krb5_data token, header, trailer; OM_uint32 min_stat, maj_stat; gss_qop_t qop_state; int conf_state, conf_state2; gss_iov_buffer_desc iov[6]; unsigned char *p; int iov_len; char header_data[9] = "ABCheader"; char trailer_data[10] = "trailerXYZ"; char token_data[16] = "0123456789abcdef"; memset(&iov, 0, sizeof(iov)); if (flags & USE_SIGN_ONLY) { header.data = header_data; header.length = 9; trailer.data = trailer_data; trailer.length = 10; } else { header.data = NULL; header.length = 0; trailer.data = NULL; trailer.length = 0; } token.data = token_data; token.length = 16; iov_len = sizeof(iov)/sizeof(iov[0]); memset(iov, 0, sizeof(iov)); iov[0].type = GSS_IOV_BUFFER_TYPE_HEADER | GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATE; if (header.length != 0) { iov[1].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY; iov[1].buffer.length = header.length; iov[1].buffer.value = header.data; } else { iov[1].type = GSS_IOV_BUFFER_TYPE_EMPTY; iov[1].buffer.length = 0; iov[1].buffer.value = NULL; } iov[2].type = GSS_IOV_BUFFER_TYPE_DATA; iov[2].buffer.length = token.length; iov[2].buffer.value = token.data; if (trailer.length != 0) { iov[3].type = GSS_IOV_BUFFER_TYPE_SIGN_ONLY; iov[3].buffer.length = trailer.length; iov[3].buffer.value = trailer.data; } else { iov[3].type = GSS_IOV_BUFFER_TYPE_EMPTY; iov[3].buffer.length = 0; iov[3].buffer.value = NULL; } if (dce_style_flag) { iov[4].type = GSS_IOV_BUFFER_TYPE_EMPTY; } else { iov[4].type = GSS_IOV_BUFFER_TYPE_PADDING | GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATE; } iov[4].buffer.length = 0; iov[4].buffer.value = 0; if (dce_style_flag) { iov[5].type = GSS_IOV_BUFFER_TYPE_EMPTY; } else if (flags & USE_HEADER_ONLY) { iov[5].type = GSS_IOV_BUFFER_TYPE_EMPTY; } else { iov[5].type = GSS_IOV_BUFFER_TYPE_TRAILER | GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATE; } iov[5].buffer.length = 0; iov[5].buffer.value = 0; maj_stat = gss_wrap_iov(&min_stat, cctx, dce_style_flag || flags & USE_CONF, 0, &conf_state, iov, iov_len); if (maj_stat != GSS_S_COMPLETE) errx(1, "gss_wrap_iov failed"); token.length = iov[0].buffer.length + iov[1].buffer.length + iov[2].buffer.length + iov[3].buffer.length + iov[4].buffer.length + iov[5].buffer.length; token.data = emalloc(token.length); p = token.data; memcpy(p, iov[0].buffer.value, iov[0].buffer.length); p += iov[0].buffer.length; memcpy(p, iov[1].buffer.value, iov[1].buffer.length); p += iov[1].buffer.length; memcpy(p, iov[2].buffer.value, iov[2].buffer.length); p += iov[2].buffer.length; memcpy(p, iov[3].buffer.value, iov[3].buffer.length); p += iov[3].buffer.length; memcpy(p, iov[4].buffer.value, iov[4].buffer.length); p += iov[4].buffer.length; memcpy(p, iov[5].buffer.value, iov[5].buffer.length); p += iov[5].buffer.length; assert(p - ((unsigned char *)token.data) == token.length); if ((flags & (USE_SIGN_ONLY|FORCE_IOV)) == 0) { gss_buffer_desc input, output; input.value = token.data; input.length = token.length; maj_stat = gss_unwrap(&min_stat, sctx, &input, &output, &conf_state2, &qop_state); if (maj_stat != GSS_S_COMPLETE) errx(1, "gss_unwrap from gss_wrap_iov failed: %s", gssapi_err(maj_stat, min_stat, mechoid)); gss_release_buffer(&min_stat, &output); } else { maj_stat = gss_unwrap_iov(&min_stat, sctx, &conf_state2, &qop_state, iov, iov_len); if (maj_stat != GSS_S_COMPLETE) errx(1, "gss_unwrap_iov failed: %x %s", flags, gssapi_err(maj_stat, min_stat, mechoid)); } if (conf_state2 != conf_state) errx(1, "conf state wrong for iov: %x", flags); free(token.data); }