blob: cc21ee860bb67005958c3f3f9af62067d6eaad3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/gss_spkm3_unseal.c
3 *
4 * Copyright (c) 2003 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Andy Adamson <andros@umich.edu>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36#include <linux/types.h>
37#include <linux/slab.h>
38#include <linux/jiffies.h>
39#include <linux/sunrpc/gss_spkm3.h>
40#include <linux/crypto.h>
41
42#ifdef RPC_DEBUG
43# define RPCDBG_FACILITY RPCDBG_AUTH
44#endif
45
46/*
47 * spkm3_read_token()
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -080048 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * only SPKM_MIC_TOK with md5 intg-alg is supported
50 */
51u32
52spkm3_read_token(struct spkm3_ctx *ctx,
53 struct xdr_netobj *read_token, /* checksum */
54 struct xdr_buf *message_buffer, /* signbuf */
J. Bruce Fields00fd6e12005-10-13 16:55:18 -040055 int toktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Olga Kornievskaiaadeb8132006-12-04 20:22:34 -050057 s32 checksum_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 s32 code;
59 struct xdr_netobj wire_cksum = {.len =0, .data = NULL};
J. Bruce Fields9e57b302006-03-20 23:23:11 -050060 char cksumdata[16];
61 struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned char *ptr = (unsigned char *)read_token->data;
Olga Kornievskaiaadeb8132006-12-04 20:22:34 -050063 unsigned char *cksum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int bodysize, md5elen;
65 int mic_hdrlen;
66 u32 ret = GSS_S_DEFECTIVE_TOKEN;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (g_verify_token_header((struct xdr_netobj *) &ctx->mech_used,
69 &bodysize, &ptr, read_token->len))
70 goto out;
71
72 /* decode the token */
73
Olga Kornievskaiaadeb8132006-12-04 20:22:34 -050074 if (toktype != SPKM_MIC_TOK) {
Chuck Lever8885cb32007-01-31 12:14:05 -050075 dprintk("RPC: BAD SPKM3 token type: %d\n", toktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 goto out;
77 }
78
Olga Kornievskaiaadeb8132006-12-04 20:22:34 -050079 if ((ret = spkm3_verify_mic_token(&ptr, &mic_hdrlen, &cksum)))
80 goto out;
81
82 if (*cksum++ != 0x03) {
Chuck Lever8885cb32007-01-31 12:14:05 -050083 dprintk("RPC: spkm3_read_token BAD checksum type\n");
Olga Kornievskaiaadeb8132006-12-04 20:22:34 -050084 goto out;
85 }
86 md5elen = *cksum++;
87 cksum++; /* move past the zbit */
88
89 if (!decode_asn1_bitstring(&wire_cksum, cksum, md5elen - 1, 16))
90 goto out;
91
92 /* HARD CODED FOR MD5 */
93
94 /* compute the checksum of the message.
95 * ptr + 2 = start of header piece of checksum
96 * mic_hdrlen + 2 = length of header piece of checksum
97 */
98 ret = GSS_S_DEFECTIVE_TOKEN;
99 if (!g_OID_equal(&ctx->intg_alg, &hmac_md5_oid)) {
Chuck Lever8885cb32007-01-31 12:14:05 -0500100 dprintk("RPC: gss_spkm3_seal: unsupported I-ALG "
101 "algorithm\n");
Olga Kornievskaiaadeb8132006-12-04 20:22:34 -0500102 goto out;
103 }
104
105 checksum_type = CKSUMTYPE_HMAC_MD5;
106
107 code = make_spkm3_checksum(checksum_type,
108 &ctx->derived_integ_key, ptr + 2, mic_hdrlen + 2,
109 message_buffer, 0, &md5cksum);
110
111 if (code)
112 goto out;
113
114 ret = GSS_S_BAD_SIG;
115 code = memcmp(md5cksum.data, wire_cksum.data, wire_cksum.len);
116 if (code) {
Chuck Lever8885cb32007-01-31 12:14:05 -0500117 dprintk("RPC: bad MIC checksum\n");
Olga Kornievskaiaadeb8132006-12-04 20:22:34 -0500118 goto out;
119 }
120
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* XXX: need to add expiration and sequencing */
123 ret = GSS_S_COMPLETE;
124out:
Jesper Juhla51482b2005-11-08 09:41:34 -0800125 kfree(wire_cksum.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return ret;
127}