u32 gss_get_mic_kerberos(struct gss_ctx *gss_ctx, struct xdr_buf *text, struct xdr_netobj *token) { struct krb5_ctx *ctx = gss_ctx->internal_ctx_id; char cksumdata[16]; struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata}; unsigned char *ptr, *krb5_hdr, *msg_start; s32 now; u32 seq_send; dprintk("RPC: gss_krb5_seal\n"); BUG_ON(ctx == NULL); now = get_seconds(); token->len = g_token_size(&ctx->mech_used, 22); ptr = token->data; g_make_token_header(&ctx->mech_used, 22, &ptr); *ptr++ = (unsigned char) ((KG_TOK_MIC_MSG>>8)&0xff); *ptr++ = (unsigned char) (KG_TOK_MIC_MSG&0xff); /* ptr now at byte 2 of header described in rfc 1964, section 1.2.1: */ krb5_hdr = ptr - 2; msg_start = krb5_hdr + 24; *(__be16 *)(krb5_hdr + 2) = htons(SGN_ALG_DES_MAC_MD5); memset(krb5_hdr + 4, 0xff, 4); if (make_checksum("md5", krb5_hdr, 8, text, 0, &md5cksum)) return GSS_S_FAILURE; if (krb5_encrypt(ctx->seq, NULL, md5cksum.data, md5cksum.data, md5cksum.len)) return GSS_S_FAILURE; memcpy(krb5_hdr + 16, md5cksum.data + md5cksum.len - KRB5_CKSUM_LENGTH, KRB5_CKSUM_LENGTH); spin_lock(&krb5_seq_lock); seq_send = ctx->seq_send++; spin_unlock(&krb5_seq_lock); if (krb5_make_seq_num(ctx->seq, ctx->initiate ? 0 : 0xff, ctx->seq_send, krb5_hdr + 16, krb5_hdr + 8)) return GSS_S_FAILURE; return (ctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE; }
/* * rotn_encrypt -- * A simple encryption example that passes data through unchanged. */ static int rotn_encrypt(WT_ENCRYPTOR *encryptor, WT_SESSION *session, uint8_t *src, size_t src_len, uint8_t *dst, size_t dst_len, size_t *result_lenp) { ROTN_ENCRYPTOR *rotn_encryptor = (ROTN_ENCRYPTOR *)encryptor; uint32_t i; (void)session; /* Unused */ if (dst_len < src_len + CHKSUM_LEN + IV_LEN) return (rotn_error(rotn_encryptor, session, ENOMEM, "encrypt buffer not big enough")); /* * !!! Most implementations would verify any needed * checksum and initialize the IV here. */ i = CHKSUM_LEN + IV_LEN; memcpy(&dst[i], &src[0], src_len); /* * Depending on whether we have a secret key or not, * call the common rotate or shift function on the text portion * of the destination buffer. Send in src_len as the length of * the text. */ if (rotn_encryptor->shift_len == 0) do_rotate((char *)dst + i, src_len, rotn_encryptor->rot_N); else do_shift(&dst[i], src_len, rotn_encryptor->shift_forw, rotn_encryptor->shift_len); /* * Checksum the encrypted buffer and add the IV. */ i = 0; make_checksum(&dst[i]); i += CHKSUM_LEN; make_iv(&dst[i]); *result_lenp = dst_len; return (0); }
/* * rotate_encrypt -- * A simple rotate encryption. */ static int rotate_encrypt(WT_ENCRYPTOR *encryptor, WT_SESSION *session, uint8_t *src, size_t src_len, uint8_t *dst, size_t dst_len, size_t *result_lenp) { MY_CRYPTO *my_crypto = (MY_CRYPTO *)encryptor; uint32_t i; (void)session; /* Unused */ ++my_crypto->num_calls; if (src == NULL) return (0); if (dst_len < src_len + CHKSUM_LEN + IV_LEN) return (ENOMEM); i = CHKSUM_LEN + IV_LEN; /* * Skip over space reserved for checksum and initialization * vector. Copy text into destination buffer then encrypt * in place. */ memcpy(&dst[i], &src[0], src_len); /* * Call common rotate function on the text portion of the * destination buffer. Send in src_len as the length of * the text. */ do_rotate((char *)dst + i, src_len, my_crypto->rot_N); /* * Checksum the encrypted buffer and add the IV. */ i = 0; make_checksum(&dst[i]); i += CHKSUM_LEN; make_iv(&dst[i]); *result_lenp = dst_len; return (0); }
static u32 gss_get_mic_v1(struct krb5_ctx *ctx, struct xdr_buf *text, struct xdr_netobj *token) { char cksumdata[GSS_KRB5_MAX_CKSUM_LEN]; struct xdr_netobj md5cksum = {.len = sizeof(cksumdata), .data = cksumdata}; void *ptr; s32 now; u32 seq_send; u8 *cksumkey; dprintk("RPC: %s\n", __func__); BUG_ON(ctx == NULL); now = get_seconds(); ptr = setup_token(ctx, token); if (ctx->gk5e->keyed_cksum) cksumkey = ctx->cksum; else cksumkey = NULL; if (make_checksum(ctx, ptr, 8, text, 0, cksumkey, KG_USAGE_SIGN, &md5cksum)) return GSS_S_FAILURE; memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data, md5cksum.len); spin_lock(&krb5_seq_lock); seq_send = ctx->seq_send++; spin_unlock(&krb5_seq_lock); if (krb5_make_seq_num(ctx, ctx->seq, ctx->initiate ? 0 : 0xff, seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8)) return GSS_S_FAILURE; return (ctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE; } static u32 gss_get_mic_v2(struct krb5_ctx *ctx, struct xdr_buf *text, struct xdr_netobj *token) { char cksumdata[GSS_KRB5_MAX_CKSUM_LEN]; struct xdr_netobj cksumobj = { .len = sizeof(cksumdata), .data = cksumdata}; void *krb5_hdr; s32 now; u64 seq_send; u8 *cksumkey; unsigned int cksum_usage; dprintk("RPC: %s\n", __func__); krb5_hdr = setup_token_v2(ctx, token); spin_lock(&krb5_seq_lock); seq_send = ctx->seq_send64++; spin_unlock(&krb5_seq_lock); *((u64 *)(krb5_hdr + 8)) = cpu_to_be64(seq_send); if (ctx->initiate) { cksumkey = ctx->initiator_sign; cksum_usage = KG_USAGE_INITIATOR_SIGN; } else { cksumkey = ctx->acceptor_sign; cksum_usage = KG_USAGE_ACCEPTOR_SIGN; } if (make_checksum_v2(ctx, krb5_hdr, GSS_KRB5_TOK_HDR_LEN, text, 0, cksumkey, cksum_usage, &cksumobj)) return GSS_S_FAILURE; memcpy(krb5_hdr + GSS_KRB5_TOK_HDR_LEN, cksumobj.data, cksumobj.len); now = get_seconds(); return (ctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE; } u32 gss_get_mic_kerberos(struct gss_ctx *gss_ctx, struct xdr_buf *text, struct xdr_netobj *token) { struct krb5_ctx *ctx = gss_ctx->internal_ctx_id; switch (ctx->enctype) { default: BUG(); case ENCTYPE_DES_CBC_RAW: case ENCTYPE_DES3_CBC_RAW: case ENCTYPE_ARCFOUR_HMAC: return gss_get_mic_v1(ctx, text, token); case ENCTYPE_AES128_CTS_HMAC_SHA1_96: case ENCTYPE_AES256_CTS_HMAC_SHA1_96: return gss_get_mic_v2(ctx, text, token); } }
/* * spkm3_read_token() * * only SPKM_MIC_TOK with md5 intg-alg is supported */ u32 spkm3_read_token(struct spkm3_ctx *ctx, struct xdr_netobj *read_token, /* checksum */ struct xdr_buf *message_buffer, /* signbuf */ int toktype) { s32 code; struct xdr_netobj wire_cksum = {.len =0, .data = NULL}; struct xdr_netobj md5cksum = {.len = 0, .data = NULL}; unsigned char *ptr = (unsigned char *)read_token->data; unsigned char *cksum; int bodysize, md5elen; int mic_hdrlen; u32 ret = GSS_S_DEFECTIVE_TOKEN; dprintk("RPC: spkm3_read_token read_token->len %d\n", read_token->len); if (g_verify_token_header((struct xdr_netobj *) &ctx->mech_used, &bodysize, &ptr, read_token->len)) goto out; /* decode the token */ if (toktype == SPKM_MIC_TOK) { if ((ret = spkm3_verify_mic_token(&ptr, &mic_hdrlen, &cksum))) goto out; if (*cksum++ != 0x03) { dprintk("RPC: spkm3_read_token BAD checksum type\n"); goto out; } md5elen = *cksum++; cksum++; /* move past the zbit */ if(!decode_asn1_bitstring(&wire_cksum, cksum, md5elen - 1, 16)) goto out; /* HARD CODED FOR MD5 */ /* compute the checksum of the message. * ptr + 2 = start of header piece of checksum * mic_hdrlen + 2 = length of header piece of checksum */ ret = GSS_S_DEFECTIVE_TOKEN; code = make_checksum(CKSUMTYPE_RSA_MD5, ptr + 2, mic_hdrlen + 2, message_buffer, 0, &md5cksum); if (code) goto out; dprintk("RPC: spkm3_read_token: digest wire_cksum.len %d:\n", wire_cksum.len); dprintk(" md5cksum.data\n"); print_hexl((u32 *) md5cksum.data, 16, 0); dprintk(" cksum.data:\n"); print_hexl((u32 *) wire_cksum.data, wire_cksum.len, 0); ret = GSS_S_BAD_SIG; code = memcmp(md5cksum.data, wire_cksum.data, wire_cksum.len); if (code) goto out; } else { dprintk("RPC: BAD or UNSUPPORTED SPKM3 token type: %d\n",toktype); goto out; } /* XXX: need to add expiration and sequencing */ ret = GSS_S_COMPLETE; out: kfree(md5cksum.data); kfree(wire_cksum.data); return ret; }