gss_spkm3_token.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * linux/net/sunrpc/gss_spkm3_token.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. #include <linux/types.h>
  36. #include <linux/slab.h>
  37. #include <linux/jiffies.h>
  38. #include <linux/sunrpc/gss_spkm3.h>
  39. #include <linux/random.h>
  40. #include <linux/crypto.h>
  41. #ifdef RPC_DEBUG
  42. # define RPCDBG_FACILITY RPCDBG_AUTH
  43. #endif
  44. /*
  45. * asn1_bitstring_len()
  46. *
  47. * calculate the asn1 bitstring length of the xdr_netobject
  48. */
  49. void
  50. asn1_bitstring_len(struct xdr_netobj *in, int *enclen, int *zerobits)
  51. {
  52. int i, zbit = 0,elen = in->len;
  53. char *ptr;
  54. ptr = &in->data[in->len -1];
  55. /* count trailing 0's */
  56. for(i = in->len; i > 0; i--) {
  57. if (*ptr == 0) {
  58. ptr--;
  59. elen--;
  60. } else
  61. break;
  62. }
  63. /* count number of 0 bits in final octet */
  64. ptr = &in->data[elen - 1];
  65. for(i = 0; i < 8; i++) {
  66. short mask = 0x01;
  67. if (!((mask << i) & *ptr))
  68. zbit++;
  69. else
  70. break;
  71. }
  72. *enclen = elen;
  73. *zerobits = zbit;
  74. }
  75. /*
  76. * decode_asn1_bitstring()
  77. *
  78. * decode a bitstring into a buffer of the expected length.
  79. * enclen = bit string length
  80. * explen = expected length (define in rfc)
  81. */
  82. int
  83. decode_asn1_bitstring(struct xdr_netobj *out, char *in, int enclen, int explen)
  84. {
  85. if (!(out->data = kmalloc(explen,GFP_KERNEL)))
  86. return 0;
  87. out->len = explen;
  88. memset(out->data, 0, explen);
  89. memcpy(out->data, in, enclen);
  90. return 1;
  91. }
  92. /*
  93. * SPKMInnerContextToken choice SPKM_MIC asn1 token layout
  94. *
  95. * contextid is always 16 bytes plain data. max asn1 bitstring len = 17.
  96. *
  97. * tokenlen = pos[0] to end of token (max pos[45] with MD5 cksum)
  98. *
  99. * pos value
  100. * ----------
  101. * [0] a4 SPKM-MIC tag
  102. * [1] ?? innertoken length (max 44)
  103. *
  104. *
  105. * tok_hdr piece of checksum data starts here
  106. *
  107. * the maximum mic-header len = 9 + 17 = 26
  108. * mic-header
  109. * ----------
  110. * [2] 30 SEQUENCE tag
  111. * [3] ?? mic-header length: (max 23) = TokenID + ContextID
  112. *
  113. * TokenID - all fields constant and can be hardcoded
  114. * -------
  115. * [4] 02 Type 2
  116. * [5] 02 Length 2
  117. * [6][7] 01 01 TokenID (SPKM_MIC_TOK)
  118. *
  119. * ContextID - encoded length not constant, calculated
  120. * ---------
  121. * [8] 03 Type 3
  122. * [9] ?? encoded length
  123. * [10] ?? ctxzbit
  124. * [11] contextid
  125. *
  126. * mic_header piece of checksum data ends here.
  127. *
  128. * int-cksum - encoded length not constant, calculated
  129. * ---------
  130. * [??] 03 Type 3
  131. * [??] ?? encoded length
  132. * [??] ?? md5zbit
  133. * [??] int-cksum (NID_md5 = 16)
  134. *
  135. * maximum SPKM-MIC innercontext token length =
  136. * 10 + encoded contextid_size(17 max) + 2 + encoded
  137. * cksum_size (17 maxfor NID_md5) = 46
  138. */
  139. /*
  140. * spkm3_mic_header()
  141. *
  142. * Prepare the SPKM_MIC_TOK mic-header for check-sum calculation
  143. * elen: 16 byte context id asn1 bitstring encoded length
  144. */
  145. void
  146. spkm3_mic_header(unsigned char **hdrbuf, unsigned int *hdrlen, unsigned char *ctxdata, int elen, int zbit)
  147. {
  148. char *hptr = *hdrbuf;
  149. char *top = *hdrbuf;
  150. *(u8 *)hptr++ = 0x30;
  151. *(u8 *)hptr++ = elen + 7; /* on the wire header length */
  152. /* tokenid */
  153. *(u8 *)hptr++ = 0x02;
  154. *(u8 *)hptr++ = 0x02;
  155. *(u8 *)hptr++ = 0x01;
  156. *(u8 *)hptr++ = 0x01;
  157. /* coniextid */
  158. *(u8 *)hptr++ = 0x03;
  159. *(u8 *)hptr++ = elen + 1; /* add 1 to include zbit */
  160. *(u8 *)hptr++ = zbit;
  161. memcpy(hptr, ctxdata, elen);
  162. hptr += elen;
  163. *hdrlen = hptr - top;
  164. }
  165. /*
  166. * spkm3_mic_innercontext_token()
  167. *
  168. * *tokp points to the beginning of the SPKM_MIC token described
  169. * in rfc 2025, section 3.2.1:
  170. *
  171. * toklen is the inner token length
  172. */
  173. void
  174. spkm3_make_mic_token(unsigned char **tokp, int toklen, struct xdr_netobj *mic_hdr, struct xdr_netobj *md5cksum, int md5elen, int md5zbit)
  175. {
  176. unsigned char *ict = *tokp;
  177. *(u8 *)ict++ = 0xa4;
  178. *(u8 *)ict++ = toklen;
  179. memcpy(ict, mic_hdr->data, mic_hdr->len);
  180. ict += mic_hdr->len;
  181. *(u8 *)ict++ = 0x03;
  182. *(u8 *)ict++ = md5elen + 1; /* add 1 to include zbit */
  183. *(u8 *)ict++ = md5zbit;
  184. memcpy(ict, md5cksum->data, md5elen);
  185. }
  186. u32
  187. spkm3_verify_mic_token(unsigned char **tokp, int *mic_hdrlen, unsigned char **cksum)
  188. {
  189. struct xdr_netobj spkm3_ctx_id = {.len =0, .data = NULL};
  190. unsigned char *ptr = *tokp;
  191. int ctxelen;
  192. u32 ret = GSS_S_DEFECTIVE_TOKEN;
  193. /* spkm3 innercontext token preamble */
  194. if ((ptr[0] != 0xa4) || (ptr[2] != 0x30)) {
  195. dprintk("RPC: BAD SPKM ictoken preamble\n");
  196. goto out;
  197. }
  198. *mic_hdrlen = ptr[3];
  199. /* token type */
  200. if ((ptr[4] != 0x02) || (ptr[5] != 0x02)) {
  201. dprintk("RPC: BAD asn1 SPKM3 token type\n");
  202. goto out;
  203. }
  204. /* only support SPKM_MIC_TOK */
  205. if((ptr[6] != 0x01) || (ptr[7] != 0x01)) {
  206. dprintk("RPC: ERROR unsupported SPKM3 token \n");
  207. goto out;
  208. }
  209. /* contextid */
  210. if (ptr[8] != 0x03) {
  211. dprintk("RPC: BAD SPKM3 asn1 context-id type\n");
  212. goto out;
  213. }
  214. ctxelen = ptr[9];
  215. if (ctxelen > 17) { /* length includes asn1 zbit octet */
  216. dprintk("RPC: BAD SPKM3 contextid len %d\n", ctxelen);
  217. goto out;
  218. }
  219. /* ignore ptr[10] */
  220. if(!decode_asn1_bitstring(&spkm3_ctx_id, &ptr[11], ctxelen - 1, 16))
  221. goto out;
  222. /*
  223. * in the current implementation: the optional int-alg is not present
  224. * so the default int-alg (md5) is used the optional snd-seq field is
  225. * also not present
  226. */
  227. if (*mic_hdrlen != 6 + ctxelen) {
  228. dprintk("RPC: BAD SPKM_ MIC_TOK header len %d: we only support default int-alg (should be absent) and do not support snd-seq\n", *mic_hdrlen);
  229. goto out;
  230. }
  231. /* checksum */
  232. *cksum = (&ptr[10] + ctxelen); /* ctxelen includes ptr[10] */
  233. ret = GSS_S_COMPLETE;
  234. out:
  235. kfree(spkm3_ctx_id.data);
  236. return ret;
  237. }