gss_generic_token.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * linux/net/sunrpc/gss_generic_token.c
  3. *
  4. * Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/generic/util_token.c
  5. *
  6. * Copyright (c) 2000 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Andy Adamson <andros@umich.edu>
  10. */
  11. /*
  12. * Copyright 1993 by OpenVision Technologies, Inc.
  13. *
  14. * Permission to use, copy, modify, distribute, and sell this software
  15. * and its documentation for any purpose is hereby granted without fee,
  16. * provided that the above copyright notice appears in all copies and
  17. * that both that copyright notice and this permission notice appear in
  18. * supporting documentation, and that the name of OpenVision not be used
  19. * in advertising or publicity pertaining to distribution of the software
  20. * without specific, written prior permission. OpenVision makes no
  21. * representations about the suitability of this software for any
  22. * purpose. It is provided "as is" without express or implied warranty.
  23. *
  24. * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  25. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  26. * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  27. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  28. * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  29. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  30. * PERFORMANCE OF THIS SOFTWARE.
  31. */
  32. #include <linux/types.h>
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include <linux/string.h>
  36. #include <linux/sunrpc/sched.h>
  37. #include <linux/sunrpc/gss_asn1.h>
  38. #ifdef RPC_DEBUG
  39. # define RPCDBG_FACILITY RPCDBG_AUTH
  40. #endif
  41. /* TWRITE_STR from gssapiP_generic.h */
  42. #define TWRITE_STR(ptr, str, len) \
  43. memcpy((ptr), (char *) (str), (len)); \
  44. (ptr) += (len);
  45. /* XXXX this code currently makes the assumption that a mech oid will
  46. never be longer than 127 bytes. This assumption is not inherent in
  47. the interfaces, so the code can be fixed if the OSI namespace
  48. balloons unexpectedly. */
  49. /* Each token looks like this:
  50. 0x60 tag for APPLICATION 0, SEQUENCE
  51. (constructed, definite-length)
  52. <length> possible multiple bytes, need to parse/generate
  53. 0x06 tag for OBJECT IDENTIFIER
  54. <moid_length> compile-time constant string (assume 1 byte)
  55. <moid_bytes> compile-time constant string
  56. <inner_bytes> the ANY containing the application token
  57. bytes 0,1 are the token type
  58. bytes 2,n are the token data
  59. For the purposes of this abstraction, the token "header" consists of
  60. the sequence tag and length octets, the mech OID DER encoding, and the
  61. first two inner bytes, which indicate the token type. The token
  62. "body" consists of everything else.
  63. */
  64. static int
  65. der_length_size( int length)
  66. {
  67. if (length < (1<<7))
  68. return(1);
  69. else if (length < (1<<8))
  70. return(2);
  71. #if (SIZEOF_INT == 2)
  72. else
  73. return(3);
  74. #else
  75. else if (length < (1<<16))
  76. return(3);
  77. else if (length < (1<<24))
  78. return(4);
  79. else
  80. return(5);
  81. #endif
  82. }
  83. static void
  84. der_write_length(unsigned char **buf, int length)
  85. {
  86. if (length < (1<<7)) {
  87. *(*buf)++ = (unsigned char) length;
  88. } else {
  89. *(*buf)++ = (unsigned char) (der_length_size(length)+127);
  90. #if (SIZEOF_INT > 2)
  91. if (length >= (1<<24))
  92. *(*buf)++ = (unsigned char) (length>>24);
  93. if (length >= (1<<16))
  94. *(*buf)++ = (unsigned char) ((length>>16)&0xff);
  95. #endif
  96. if (length >= (1<<8))
  97. *(*buf)++ = (unsigned char) ((length>>8)&0xff);
  98. *(*buf)++ = (unsigned char) (length&0xff);
  99. }
  100. }
  101. /* returns decoded length, or < 0 on failure. Advances buf and
  102. decrements bufsize */
  103. static int
  104. der_read_length(unsigned char **buf, int *bufsize)
  105. {
  106. unsigned char sf;
  107. int ret;
  108. if (*bufsize < 1)
  109. return(-1);
  110. sf = *(*buf)++;
  111. (*bufsize)--;
  112. if (sf & 0x80) {
  113. if ((sf &= 0x7f) > ((*bufsize)-1))
  114. return(-1);
  115. if (sf > SIZEOF_INT)
  116. return (-1);
  117. ret = 0;
  118. for (; sf; sf--) {
  119. ret = (ret<<8) + (*(*buf)++);
  120. (*bufsize)--;
  121. }
  122. } else {
  123. ret = sf;
  124. }
  125. return(ret);
  126. }
  127. /* returns the length of a token, given the mech oid and the body size */
  128. int
  129. g_token_size(struct xdr_netobj *mech, unsigned int body_size)
  130. {
  131. /* set body_size to sequence contents size */
  132. body_size += 2 + (int) mech->len; /* NEED overflow check */
  133. return(1 + der_length_size(body_size) + body_size);
  134. }
  135. EXPORT_SYMBOL_GPL(g_token_size);
  136. /* fills in a buffer with the token header. The buffer is assumed to
  137. be the right size. buf is advanced past the token header */
  138. void
  139. g_make_token_header(struct xdr_netobj *mech, int body_size, unsigned char **buf)
  140. {
  141. *(*buf)++ = 0x60;
  142. der_write_length(buf, 2 + mech->len + body_size);
  143. *(*buf)++ = 0x06;
  144. *(*buf)++ = (unsigned char) mech->len;
  145. TWRITE_STR(*buf, mech->data, ((int) mech->len));
  146. }
  147. EXPORT_SYMBOL_GPL(g_make_token_header);
  148. /*
  149. * Given a buffer containing a token, reads and verifies the token,
  150. * leaving buf advanced past the token header, and setting body_size
  151. * to the number of remaining bytes. Returns 0 on success,
  152. * G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the
  153. * mechanism in the token does not match the mech argument. buf and
  154. * *body_size are left unmodified on error.
  155. */
  156. u32
  157. g_verify_token_header(struct xdr_netobj *mech, int *body_size,
  158. unsigned char **buf_in, int toksize)
  159. {
  160. unsigned char *buf = *buf_in;
  161. int seqsize;
  162. struct xdr_netobj toid;
  163. int ret = 0;
  164. if ((toksize-=1) < 0)
  165. return(G_BAD_TOK_HEADER);
  166. if (*buf++ != 0x60)
  167. return(G_BAD_TOK_HEADER);
  168. if ((seqsize = der_read_length(&buf, &toksize)) < 0)
  169. return(G_BAD_TOK_HEADER);
  170. if (seqsize != toksize)
  171. return(G_BAD_TOK_HEADER);
  172. if ((toksize-=1) < 0)
  173. return(G_BAD_TOK_HEADER);
  174. if (*buf++ != 0x06)
  175. return(G_BAD_TOK_HEADER);
  176. if ((toksize-=1) < 0)
  177. return(G_BAD_TOK_HEADER);
  178. toid.len = *buf++;
  179. if ((toksize-=toid.len) < 0)
  180. return(G_BAD_TOK_HEADER);
  181. toid.data = buf;
  182. buf+=toid.len;
  183. if (! g_OID_equal(&toid, mech))
  184. ret = G_WRONG_MECH;
  185. /* G_WRONG_MECH is not returned immediately because it's more important
  186. to return G_BAD_TOK_HEADER if the token header is in fact bad */
  187. if ((toksize-=2) < 0)
  188. return(G_BAD_TOK_HEADER);
  189. if (ret)
  190. return(ret);
  191. if (!ret) {
  192. *buf_in = buf;
  193. *body_size = toksize;
  194. }
  195. return(ret);
  196. }
  197. EXPORT_SYMBOL_GPL(g_verify_token_header);