Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
5.9 KiB

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