asn1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * The ASB.1/BER parsing code is derived from ip_nat_snmp_basic.c which was in
  3. * turn derived from the gxsnmp package by Gregory McLean & Jochen Friedrich
  4. *
  5. * Copyright (c) 2000 RP Internet (www.rpi.net.au).
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/slab.h>
  24. #include "cifspdu.h"
  25. #include "cifsglob.h"
  26. #include "cifs_debug.h"
  27. #include "cifsproto.h"
  28. /*****************************************************************************
  29. *
  30. * Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
  31. *
  32. *****************************************************************************/
  33. /* Class */
  34. #define ASN1_UNI 0 /* Universal */
  35. #define ASN1_APL 1 /* Application */
  36. #define ASN1_CTX 2 /* Context */
  37. #define ASN1_PRV 3 /* Private */
  38. /* Tag */
  39. #define ASN1_EOC 0 /* End Of Contents or N/A */
  40. #define ASN1_BOL 1 /* Boolean */
  41. #define ASN1_INT 2 /* Integer */
  42. #define ASN1_BTS 3 /* Bit String */
  43. #define ASN1_OTS 4 /* Octet String */
  44. #define ASN1_NUL 5 /* Null */
  45. #define ASN1_OJI 6 /* Object Identifier */
  46. #define ASN1_OJD 7 /* Object Description */
  47. #define ASN1_EXT 8 /* External */
  48. #define ASN1_SEQ 16 /* Sequence */
  49. #define ASN1_SET 17 /* Set */
  50. #define ASN1_NUMSTR 18 /* Numerical String */
  51. #define ASN1_PRNSTR 19 /* Printable String */
  52. #define ASN1_TEXSTR 20 /* Teletext String */
  53. #define ASN1_VIDSTR 21 /* Video String */
  54. #define ASN1_IA5STR 22 /* IA5 String */
  55. #define ASN1_UNITIM 23 /* Universal Time */
  56. #define ASN1_GENTIM 24 /* General Time */
  57. #define ASN1_GRASTR 25 /* Graphical String */
  58. #define ASN1_VISSTR 26 /* Visible String */
  59. #define ASN1_GENSTR 27 /* General String */
  60. /* Primitive / Constructed methods*/
  61. #define ASN1_PRI 0 /* Primitive */
  62. #define ASN1_CON 1 /* Constructed */
  63. /*
  64. * Error codes.
  65. */
  66. #define ASN1_ERR_NOERROR 0
  67. #define ASN1_ERR_DEC_EMPTY 2
  68. #define ASN1_ERR_DEC_EOC_MISMATCH 3
  69. #define ASN1_ERR_DEC_LENGTH_MISMATCH 4
  70. #define ASN1_ERR_DEC_BADVALUE 5
  71. #define SPNEGO_OID_LEN 7
  72. #define NTLMSSP_OID_LEN 10
  73. #define KRB5_OID_LEN 7
  74. #define MSKRB5_OID_LEN 7
  75. static unsigned long SPNEGO_OID[7] = { 1, 3, 6, 1, 5, 5, 2 };
  76. static unsigned long NTLMSSP_OID[10] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10 };
  77. static unsigned long KRB5_OID[7] = { 1, 2, 840, 113554, 1, 2, 2 };
  78. static unsigned long MSKRB5_OID[7] = { 1, 2, 840, 48018, 1, 2, 2 };
  79. /*
  80. * ASN.1 context.
  81. */
  82. struct asn1_ctx {
  83. int error; /* Error condition */
  84. unsigned char *pointer; /* Octet just to be decoded */
  85. unsigned char *begin; /* First octet */
  86. unsigned char *end; /* Octet after last octet */
  87. };
  88. /*
  89. * Octet string (not null terminated)
  90. */
  91. struct asn1_octstr {
  92. unsigned char *data;
  93. unsigned int len;
  94. };
  95. static void
  96. asn1_open(struct asn1_ctx *ctx, unsigned char *buf, unsigned int len)
  97. {
  98. ctx->begin = buf;
  99. ctx->end = buf + len;
  100. ctx->pointer = buf;
  101. ctx->error = ASN1_ERR_NOERROR;
  102. }
  103. static unsigned char
  104. asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
  105. {
  106. if (ctx->pointer >= ctx->end) {
  107. ctx->error = ASN1_ERR_DEC_EMPTY;
  108. return 0;
  109. }
  110. *ch = *(ctx->pointer)++;
  111. return 1;
  112. }
  113. static unsigned char
  114. asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
  115. {
  116. unsigned char ch;
  117. *tag = 0;
  118. do {
  119. if (!asn1_octet_decode(ctx, &ch))
  120. return 0;
  121. *tag <<= 7;
  122. *tag |= ch & 0x7F;
  123. } while ((ch & 0x80) == 0x80);
  124. return 1;
  125. }
  126. static unsigned char
  127. asn1_id_decode(struct asn1_ctx *ctx,
  128. unsigned int *cls, unsigned int *con, unsigned int *tag)
  129. {
  130. unsigned char ch;
  131. if (!asn1_octet_decode(ctx, &ch))
  132. return 0;
  133. *cls = (ch & 0xC0) >> 6;
  134. *con = (ch & 0x20) >> 5;
  135. *tag = (ch & 0x1F);
  136. if (*tag == 0x1F) {
  137. if (!asn1_tag_decode(ctx, tag))
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. static unsigned char
  143. asn1_length_decode(struct asn1_ctx *ctx, unsigned int *def, unsigned int *len)
  144. {
  145. unsigned char ch, cnt;
  146. if (!asn1_octet_decode(ctx, &ch))
  147. return 0;
  148. if (ch == 0x80)
  149. *def = 0;
  150. else {
  151. *def = 1;
  152. if (ch < 0x80)
  153. *len = ch;
  154. else {
  155. cnt = (unsigned char) (ch & 0x7F);
  156. *len = 0;
  157. while (cnt > 0) {
  158. if (!asn1_octet_decode(ctx, &ch))
  159. return 0;
  160. *len <<= 8;
  161. *len |= ch;
  162. cnt--;
  163. }
  164. }
  165. }
  166. return 1;
  167. }
  168. static unsigned char
  169. asn1_header_decode(struct asn1_ctx *ctx,
  170. unsigned char **eoc,
  171. unsigned int *cls, unsigned int *con, unsigned int *tag)
  172. {
  173. unsigned int def = 0;
  174. unsigned int len = 0;
  175. if (!asn1_id_decode(ctx, cls, con, tag))
  176. return 0;
  177. if (!asn1_length_decode(ctx, &def, &len))
  178. return 0;
  179. if (def)
  180. *eoc = ctx->pointer + len;
  181. else
  182. *eoc = NULL;
  183. return 1;
  184. }
  185. static unsigned char
  186. asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
  187. {
  188. unsigned char ch;
  189. if (eoc == NULL) {
  190. if (!asn1_octet_decode(ctx, &ch))
  191. return 0;
  192. if (ch != 0x00) {
  193. ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
  194. return 0;
  195. }
  196. if (!asn1_octet_decode(ctx, &ch))
  197. return 0;
  198. if (ch != 0x00) {
  199. ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
  200. return 0;
  201. }
  202. return 1;
  203. } else {
  204. if (ctx->pointer != eoc) {
  205. ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
  206. return 0;
  207. }
  208. return 1;
  209. }
  210. }
  211. /* static unsigned char asn1_null_decode(struct asn1_ctx *ctx,
  212. unsigned char *eoc)
  213. {
  214. ctx->pointer = eoc;
  215. return 1;
  216. }
  217. static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
  218. unsigned char *eoc, long *integer)
  219. {
  220. unsigned char ch;
  221. unsigned int len;
  222. if (!asn1_octet_decode(ctx, &ch))
  223. return 0;
  224. *integer = (signed char) ch;
  225. len = 1;
  226. while (ctx->pointer < eoc) {
  227. if (++len > sizeof(long)) {
  228. ctx->error = ASN1_ERR_DEC_BADVALUE;
  229. return 0;
  230. }
  231. if (!asn1_octet_decode(ctx, &ch))
  232. return 0;
  233. *integer <<= 8;
  234. *integer |= ch;
  235. }
  236. return 1;
  237. }
  238. static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
  239. unsigned char *eoc,
  240. unsigned int *integer)
  241. {
  242. unsigned char ch;
  243. unsigned int len;
  244. if (!asn1_octet_decode(ctx, &ch))
  245. return 0;
  246. *integer = ch;
  247. if (ch == 0)
  248. len = 0;
  249. else
  250. len = 1;
  251. while (ctx->pointer < eoc) {
  252. if (++len > sizeof(unsigned int)) {
  253. ctx->error = ASN1_ERR_DEC_BADVALUE;
  254. return 0;
  255. }
  256. if (!asn1_octet_decode(ctx, &ch))
  257. return 0;
  258. *integer <<= 8;
  259. *integer |= ch;
  260. }
  261. return 1;
  262. }
  263. static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
  264. unsigned char *eoc,
  265. unsigned long *integer)
  266. {
  267. unsigned char ch;
  268. unsigned int len;
  269. if (!asn1_octet_decode(ctx, &ch))
  270. return 0;
  271. *integer = ch;
  272. if (ch == 0)
  273. len = 0;
  274. else
  275. len = 1;
  276. while (ctx->pointer < eoc) {
  277. if (++len > sizeof(unsigned long)) {
  278. ctx->error = ASN1_ERR_DEC_BADVALUE;
  279. return 0;
  280. }
  281. if (!asn1_octet_decode(ctx, &ch))
  282. return 0;
  283. *integer <<= 8;
  284. *integer |= ch;
  285. }
  286. return 1;
  287. }
  288. static unsigned char
  289. asn1_octets_decode(struct asn1_ctx *ctx,
  290. unsigned char *eoc,
  291. unsigned char **octets, unsigned int *len)
  292. {
  293. unsigned char *ptr;
  294. *len = 0;
  295. *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
  296. if (*octets == NULL) {
  297. return 0;
  298. }
  299. ptr = *octets;
  300. while (ctx->pointer < eoc) {
  301. if (!asn1_octet_decode(ctx, (unsigned char *) ptr++)) {
  302. kfree(*octets);
  303. *octets = NULL;
  304. return 0;
  305. }
  306. (*len)++;
  307. }
  308. return 1;
  309. } */
  310. static unsigned char
  311. asn1_subid_decode(struct asn1_ctx *ctx, unsigned long *subid)
  312. {
  313. unsigned char ch;
  314. *subid = 0;
  315. do {
  316. if (!asn1_octet_decode(ctx, &ch))
  317. return 0;
  318. *subid <<= 7;
  319. *subid |= ch & 0x7F;
  320. } while ((ch & 0x80) == 0x80);
  321. return 1;
  322. }
  323. static int
  324. asn1_oid_decode(struct asn1_ctx *ctx,
  325. unsigned char *eoc, unsigned long **oid, unsigned int *len)
  326. {
  327. unsigned long subid;
  328. unsigned int size;
  329. unsigned long *optr;
  330. size = eoc - ctx->pointer + 1;
  331. *oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
  332. if (*oid == NULL)
  333. return 0;
  334. optr = *oid;
  335. if (!asn1_subid_decode(ctx, &subid)) {
  336. kfree(*oid);
  337. *oid = NULL;
  338. return 0;
  339. }
  340. if (subid < 40) {
  341. optr[0] = 0;
  342. optr[1] = subid;
  343. } else if (subid < 80) {
  344. optr[0] = 1;
  345. optr[1] = subid - 40;
  346. } else {
  347. optr[0] = 2;
  348. optr[1] = subid - 80;
  349. }
  350. *len = 2;
  351. optr += 2;
  352. while (ctx->pointer < eoc) {
  353. if (++(*len) > size) {
  354. ctx->error = ASN1_ERR_DEC_BADVALUE;
  355. kfree(*oid);
  356. *oid = NULL;
  357. return 0;
  358. }
  359. if (!asn1_subid_decode(ctx, optr++)) {
  360. kfree(*oid);
  361. *oid = NULL;
  362. return 0;
  363. }
  364. }
  365. return 1;
  366. }
  367. static int
  368. compare_oid(unsigned long *oid1, unsigned int oid1len,
  369. unsigned long *oid2, unsigned int oid2len)
  370. {
  371. unsigned int i;
  372. if (oid1len != oid2len)
  373. return 0;
  374. else {
  375. for (i = 0; i < oid1len; i++) {
  376. if (oid1[i] != oid2[i])
  377. return 0;
  378. }
  379. return 1;
  380. }
  381. }
  382. /* BB check for endian conversion issues here */
  383. int
  384. decode_negTokenInit(unsigned char *security_blob, int length,
  385. enum securityEnum *secType)
  386. {
  387. struct asn1_ctx ctx;
  388. unsigned char *end;
  389. unsigned char *sequence_end;
  390. unsigned long *oid = NULL;
  391. unsigned int cls, con, tag, oidlen, rc;
  392. int use_ntlmssp = FALSE;
  393. int use_kerberos = FALSE;
  394. *secType = NTLM; /* BB eventually make Kerberos or NLTMSSP the default*/
  395. /* cifs_dump_mem(" Received SecBlob ", security_blob, length); */
  396. asn1_open(&ctx, security_blob, length);
  397. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  398. cFYI(1, ("Error decoding negTokenInit header"));
  399. return 0;
  400. } else if ((cls != ASN1_APL) || (con != ASN1_CON)
  401. || (tag != ASN1_EOC)) {
  402. cFYI(1, ("cls = %d con = %d tag = %d", cls, con, tag));
  403. return 0;
  404. } else {
  405. /* remember to free obj->oid */
  406. rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
  407. if (rc) {
  408. if ((tag == ASN1_OJI) && (cls == ASN1_PRI)) {
  409. rc = asn1_oid_decode(&ctx, end, &oid, &oidlen);
  410. if (rc) {
  411. rc = compare_oid(oid, oidlen,
  412. SPNEGO_OID,
  413. SPNEGO_OID_LEN);
  414. kfree(oid);
  415. }
  416. } else
  417. rc = 0;
  418. }
  419. if (!rc) {
  420. cFYI(1, ("Error decoding negTokenInit header"));
  421. return 0;
  422. }
  423. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  424. cFYI(1, ("Error decoding negTokenInit"));
  425. return 0;
  426. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
  427. || (tag != ASN1_EOC)) {
  428. cFYI(1,
  429. ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
  430. cls, con, tag, end, *end));
  431. return 0;
  432. }
  433. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  434. cFYI(1, ("Error decoding negTokenInit"));
  435. return 0;
  436. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  437. || (tag != ASN1_SEQ)) {
  438. cFYI(1,
  439. ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
  440. cls, con, tag, end, *end));
  441. return 0;
  442. }
  443. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  444. cFYI(1, ("Error decoding 2nd part of negTokenInit"));
  445. return 0;
  446. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
  447. || (tag != ASN1_EOC)) {
  448. cFYI(1,
  449. ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
  450. cls, con, tag, end, *end));
  451. return 0;
  452. }
  453. if (asn1_header_decode
  454. (&ctx, &sequence_end, &cls, &con, &tag) == 0) {
  455. cFYI(1, ("Error decoding 2nd part of negTokenInit"));
  456. return 0;
  457. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  458. || (tag != ASN1_SEQ)) {
  459. cFYI(1,
  460. ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
  461. cls, con, tag, end, *end));
  462. return 0;
  463. }
  464. while (!asn1_eoc_decode(&ctx, sequence_end)) {
  465. rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
  466. if (!rc) {
  467. cFYI(1,
  468. ("Error decoding negTokenInit hdr exit2"));
  469. return 0;
  470. }
  471. if ((tag == ASN1_OJI) && (con == ASN1_PRI)) {
  472. if (asn1_oid_decode(&ctx, end, &oid, &oidlen)) {
  473. cFYI(1,
  474. ("OID len = %d oid = 0x%lx 0x%lx "
  475. "0x%lx 0x%lx",
  476. oidlen, *oid, *(oid + 1),
  477. *(oid + 2), *(oid + 3)));
  478. if (compare_oid(oid, oidlen,
  479. MSKRB5_OID,
  480. MSKRB5_OID_LEN))
  481. use_kerberos = TRUE;
  482. else if (compare_oid(oid, oidlen,
  483. KRB5_OID,
  484. KRB5_OID_LEN))
  485. use_kerberos = TRUE;
  486. else if (compare_oid(oid, oidlen,
  487. NTLMSSP_OID,
  488. NTLMSSP_OID_LEN))
  489. use_ntlmssp = TRUE;
  490. kfree(oid);
  491. }
  492. } else {
  493. cFYI(1, ("Should be an oid what is going on?"));
  494. }
  495. }
  496. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  497. cFYI(1,
  498. ("Error decoding last part negTokenInit exit3"));
  499. return 0;
  500. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
  501. /* tag = 3 indicating mechListMIC */
  502. cFYI(1,
  503. ("Exit 4 cls = %d con = %d tag = %d end = %p (%d)",
  504. cls, con, tag, end, *end));
  505. return 0;
  506. }
  507. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  508. cFYI(1,
  509. ("Error decoding last part negTokenInit exit5"));
  510. return 0;
  511. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  512. || (tag != ASN1_SEQ)) {
  513. cFYI(1, ("cls = %d con = %d tag = %d end = %p (%d)",
  514. cls, con, tag, end, *end));
  515. }
  516. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  517. cFYI(1,
  518. ("Error decoding last part negTokenInit exit 7"));
  519. return 0;
  520. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
  521. cFYI(1,
  522. ("Exit 8 cls = %d con = %d tag = %d end = %p (%d)",
  523. cls, con, tag, end, *end));
  524. return 0;
  525. }
  526. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  527. cFYI(1,
  528. ("Error decoding last part negTokenInit exit9"));
  529. return 0;
  530. } else if ((cls != ASN1_UNI) || (con != ASN1_PRI)
  531. || (tag != ASN1_GENSTR)) {
  532. cFYI(1,
  533. ("Exit10 cls = %d con = %d tag = %d end = %p (%d)",
  534. cls, con, tag, end, *end));
  535. return 0;
  536. }
  537. cFYI(1, ("Need to call asn1_octets_decode() function for %s",
  538. ctx.pointer)); /* is this UTF-8 or ASCII? */
  539. }
  540. if (use_kerberos)
  541. *secType = Kerberos;
  542. else if (use_ntlmssp)
  543. *secType = NTLMSSP;
  544. return 1;
  545. }