quickder.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. Optimized ASN.1 DER decoder
  6. */
  7. #include "secerr.h"
  8. #include "secasn1.h" /* for SEC_ASN1GetSubtemplate */
  9. #include "secitem.h"
  10. /*
  11. * simple definite-length ASN.1 decoder
  12. */
  13. static unsigned char*
  14. definite_length_decoder(const unsigned char* buf,
  15. const unsigned int buf_length,
  16. unsigned int* out_data_length,
  17. PRBool includeTag)
  18. {
  19. unsigned char tag;
  20. unsigned int used_length = 0;
  21. unsigned int data_length = 0;
  22. unsigned char length_field_len = 0;
  23. unsigned char byte;
  24. unsigned int i;
  25. if (used_length >= buf_length) {
  26. /* Tag field was not found! */
  27. return NULL;
  28. }
  29. tag = buf[used_length++];
  30. if (tag == 0) {
  31. /* End-of-contents octects should not be present in DER because
  32. DER doesn't use the indefinite length form. */
  33. return NULL;
  34. }
  35. if ((tag & 0x1F) == 0x1F) {
  36. /* High tag number (a tag number > 30) is not supported */
  37. return NULL;
  38. }
  39. if (used_length >= buf_length) {
  40. /* Length field was not found! */
  41. return NULL;
  42. }
  43. byte = buf[used_length++];
  44. if (!(byte & 0x80)) {
  45. /* Short form: The high bit is not set. */
  46. data_length = byte; /* clarity; we're returning a 32-bit int. */
  47. } else {
  48. /* Long form. Extract the field length */
  49. length_field_len = byte & 0x7F;
  50. if (length_field_len == 0) {
  51. /* DER doesn't use the indefinite length form. */
  52. return NULL;
  53. }
  54. if (length_field_len > sizeof(data_length)) {
  55. /* We don't support an extended length field longer than
  56. 4 bytes (2^32) */
  57. return NULL;
  58. }
  59. if (length_field_len > (buf_length - used_length)) {
  60. /* Extended length field was not found */
  61. return NULL;
  62. }
  63. /* Iterate across the extended length field */
  64. for (i = 0; i < length_field_len; i++) {
  65. byte = buf[used_length++];
  66. data_length = (data_length << 8) | byte;
  67. if (i == 0) {
  68. PRBool too_long = PR_FALSE;
  69. if (length_field_len == 1) {
  70. too_long = ((byte & 0x80) == 0); /* Short form suffices */
  71. } else {
  72. too_long = (byte == 0); /* This zero byte can be omitted */
  73. }
  74. if (too_long) {
  75. /* The length is longer than needed. */
  76. return NULL;
  77. }
  78. }
  79. }
  80. }
  81. if (data_length > (buf_length - used_length)) {
  82. /* The decoded length exceeds the available buffer */
  83. return NULL;
  84. }
  85. if (includeTag) {
  86. data_length += used_length;
  87. }
  88. *out_data_length = data_length;
  89. return ((unsigned char*)buf + (includeTag ? 0 : used_length));
  90. }
  91. static SECStatus
  92. GetItem(SECItem* src, SECItem* dest, PRBool includeTag)
  93. {
  94. if ((!src) || (!dest) || (!src->data && src->len)) {
  95. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  96. return SECFailure;
  97. }
  98. if (!src->len) {
  99. /* reaching the end of the buffer is not an error */
  100. dest->data = NULL;
  101. dest->len = 0;
  102. return SECSuccess;
  103. }
  104. dest->data = definite_length_decoder(src->data, src->len, &dest->len,
  105. includeTag);
  106. if (dest->data == NULL) {
  107. PORT_SetError(SEC_ERROR_BAD_DER);
  108. return SECFailure;
  109. }
  110. src->len -= (int)(dest->data - src->data) + dest->len;
  111. src->data = dest->data + dest->len;
  112. return SECSuccess;
  113. }
  114. /* check if the actual component's type matches the type in the template */
  115. static SECStatus
  116. MatchComponentType(const SEC_ASN1Template* templateEntry,
  117. SECItem* item, PRBool* match, void* dest)
  118. {
  119. unsigned long kind = 0;
  120. unsigned char tag = 0;
  121. if ((!item) || (!item->data && item->len) || (!templateEntry) || (!match)) {
  122. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  123. return SECFailure;
  124. }
  125. if (!item->len) {
  126. *match = PR_FALSE;
  127. return SECSuccess;
  128. }
  129. kind = templateEntry->kind;
  130. tag = *(unsigned char*)item->data;
  131. if (((kind & SEC_ASN1_INLINE) ||
  132. (kind & SEC_ASN1_POINTER)) &&
  133. (0 == (kind & SEC_ASN1_TAG_MASK))) {
  134. /* These cases are special because the template's "kind" does not
  135. give us the information for the ASN.1 tag of the next item. It can
  136. only be figured out from the subtemplate. */
  137. if (!(kind & SEC_ASN1_OPTIONAL)) {
  138. /* This is a required component. If there is a type mismatch,
  139. the decoding of the subtemplate will fail, so assume this
  140. is a match at the parent level and let it fail later. This
  141. avoids a redundant check in matching cases */
  142. *match = PR_TRUE;
  143. return SECSuccess;
  144. } else {
  145. /* optional component. This is the hard case. Now we need to
  146. look at the subtemplate to get the expected kind */
  147. const SEC_ASN1Template* subTemplate =
  148. SEC_ASN1GetSubtemplate(templateEntry, dest, PR_FALSE);
  149. if (!subTemplate) {
  150. PORT_SetError(SEC_ERROR_BAD_TEMPLATE);
  151. return SECFailure;
  152. }
  153. if ((subTemplate->kind & SEC_ASN1_INLINE) ||
  154. (subTemplate->kind & SEC_ASN1_POINTER)) {
  155. /* disallow nesting SEC_ASN1_POINTER and SEC_ASN1_INLINE,
  156. otherwise you may get a false positive due to the recursion
  157. optimization above that always matches the type if the
  158. component is required . Nesting these should never be
  159. required, so that no one should miss this ability */
  160. PORT_SetError(SEC_ERROR_BAD_TEMPLATE);
  161. return SECFailure;
  162. }
  163. return MatchComponentType(subTemplate, item, match,
  164. (void*)((char*)dest + templateEntry->offset));
  165. }
  166. }
  167. if (kind & SEC_ASN1_CHOICE) {
  168. /* we need to check the component's tag against each choice's tag */
  169. /* XXX it would be nice to save the index of the choice here so that
  170. DecodeChoice wouldn't have to do this again. However, due to the
  171. recursivity of MatchComponentType, we don't know if we are in a
  172. required or optional component, so we can't write anywhere in
  173. the destination within this function */
  174. unsigned choiceIndex = 1;
  175. const SEC_ASN1Template* choiceEntry;
  176. while ((choiceEntry = &templateEntry[choiceIndex++]) && (choiceEntry->kind)) {
  177. if ((SECSuccess == MatchComponentType(choiceEntry, item, match,
  178. (void*)((char*)dest + choiceEntry->offset))) &&
  179. (PR_TRUE == *match)) {
  180. return SECSuccess;
  181. }
  182. }
  183. /* no match, caller must decide if this is BAD DER, or not. */
  184. *match = PR_FALSE;
  185. return SECSuccess;
  186. }
  187. if (kind & SEC_ASN1_ANY) {
  188. /* SEC_ASN1_ANY always matches */
  189. *match = PR_TRUE;
  190. return SECSuccess;
  191. }
  192. if ((0 == ((unsigned char)kind & SEC_ASN1_TAGNUM_MASK)) &&
  193. (!(kind & SEC_ASN1_EXPLICIT)) &&
  194. (((kind & SEC_ASN1_SAVE) ||
  195. (kind & SEC_ASN1_SKIP)) &&
  196. (!(kind & SEC_ASN1_OPTIONAL)))) {
  197. /* when saving or skipping a required component, a type is not
  198. required in the template. This is for legacy support of
  199. SEC_ASN1_SAVE and SEC_ASN1_SKIP only. XXX I would like to
  200. deprecate these usages and always require a type, as this
  201. disables type checking, and effectively forbids us from
  202. transparently ignoring optional components we aren't aware of */
  203. *match = PR_TRUE;
  204. return SECSuccess;
  205. }
  206. /* first, do a class check */
  207. if ((tag & SEC_ASN1_CLASS_MASK) !=
  208. (((unsigned char)kind) & SEC_ASN1_CLASS_MASK)) {
  209. /* this is only to help debugging of the decoder in case of problems */
  210. /* unsigned char tagclass = tag & SEC_ASN1_CLASS_MASK; */
  211. /* unsigned char expectedclass = (unsigned char)kind & SEC_ASN1_CLASS_MASK; */
  212. *match = PR_FALSE;
  213. return SECSuccess;
  214. }
  215. /* now do a tag check */
  216. if (((unsigned char)kind & SEC_ASN1_TAGNUM_MASK) !=
  217. (tag & SEC_ASN1_TAGNUM_MASK)) {
  218. *match = PR_FALSE;
  219. return SECSuccess;
  220. }
  221. /* now, do a method check. This depends on the class */
  222. switch (tag & SEC_ASN1_CLASS_MASK) {
  223. case SEC_ASN1_UNIVERSAL:
  224. /* For types of the SEC_ASN1_UNIVERSAL class, we know which must be
  225. primitive or constructed based on the tag */
  226. switch (tag & SEC_ASN1_TAGNUM_MASK) {
  227. case SEC_ASN1_SEQUENCE:
  228. case SEC_ASN1_SET:
  229. case SEC_ASN1_EMBEDDED_PDV:
  230. /* this component must be a constructed type */
  231. /* XXX add any new universal constructed type here */
  232. if (tag & SEC_ASN1_CONSTRUCTED) {
  233. *match = PR_TRUE;
  234. return SECSuccess;
  235. }
  236. break;
  237. default:
  238. /* this component must be a primitive type */
  239. if (!(tag & SEC_ASN1_CONSTRUCTED)) {
  240. *match = PR_TRUE;
  241. return SECSuccess;
  242. }
  243. break;
  244. }
  245. break;
  246. default:
  247. /* for all other classes, we check the method based on the template */
  248. if ((unsigned char)(kind & SEC_ASN1_METHOD_MASK) ==
  249. (tag & SEC_ASN1_METHOD_MASK)) {
  250. *match = PR_TRUE;
  251. return SECSuccess;
  252. }
  253. /* method does not match between template and component */
  254. break;
  255. }
  256. *match = PR_FALSE;
  257. return SECSuccess;
  258. }
  259. #ifdef DEBUG
  260. static SECStatus
  261. CheckSequenceTemplate(const SEC_ASN1Template* sequenceTemplate)
  262. {
  263. SECStatus rv = SECSuccess;
  264. const SEC_ASN1Template* sequenceEntry = NULL;
  265. unsigned long seqIndex = 0;
  266. unsigned long lastEntryIndex = 0;
  267. unsigned long ambiguityIndex = 0;
  268. PRBool foundAmbiguity = PR_FALSE;
  269. do {
  270. sequenceEntry = &sequenceTemplate[seqIndex++];
  271. if (sequenceEntry->kind) {
  272. /* ensure that we don't have an optional component of SEC_ASN1_ANY
  273. in the middle of the sequence, since we could not handle it */
  274. /* XXX this function needs to dig into the subtemplates to find
  275. the next tag */
  276. if ((PR_FALSE == foundAmbiguity) &&
  277. (sequenceEntry->kind & SEC_ASN1_OPTIONAL) &&
  278. (sequenceEntry->kind & SEC_ASN1_ANY)) {
  279. foundAmbiguity = PR_TRUE;
  280. ambiguityIndex = seqIndex - 1;
  281. }
  282. }
  283. } while (sequenceEntry->kind);
  284. lastEntryIndex = seqIndex - 2;
  285. if (PR_FALSE != foundAmbiguity) {
  286. if (ambiguityIndex < lastEntryIndex) {
  287. /* ambiguity can only be tolerated on the last entry */
  288. PORT_SetError(SEC_ERROR_BAD_TEMPLATE);
  289. rv = SECFailure;
  290. }
  291. }
  292. /* XXX also enforce ASN.1 requirement that tags be
  293. distinct for consecutive optional components */
  294. return rv;
  295. }
  296. #endif
  297. static SECStatus DecodeItem(void* dest,
  298. const SEC_ASN1Template* templateEntry,
  299. SECItem* src, PLArenaPool* arena, PRBool checkTag);
  300. static SECStatus
  301. DecodeSequence(void* dest,
  302. const SEC_ASN1Template* templateEntry,
  303. SECItem* src, PLArenaPool* arena)
  304. {
  305. SECStatus rv = SECSuccess;
  306. SECItem source;
  307. SECItem sequence;
  308. const SEC_ASN1Template* sequenceTemplate = &(templateEntry[1]);
  309. const SEC_ASN1Template* sequenceEntry = NULL;
  310. unsigned long seqindex = 0;
  311. #ifdef DEBUG
  312. /* for a sequence, we need to validate the template. */
  313. rv = CheckSequenceTemplate(sequenceTemplate);
  314. #endif
  315. source = *src;
  316. /* get the sequence */
  317. if (SECSuccess == rv) {
  318. rv = GetItem(&source, &sequence, PR_FALSE);
  319. }
  320. /* process it */
  321. if (SECSuccess == rv)
  322. do {
  323. sequenceEntry = &sequenceTemplate[seqindex++];
  324. if ((sequenceEntry && sequenceEntry->kind) &&
  325. (sequenceEntry->kind != SEC_ASN1_SKIP_REST)) {
  326. rv = DecodeItem(dest, sequenceEntry, &sequence, arena, PR_TRUE);
  327. }
  328. } while ((SECSuccess == rv) &&
  329. (sequenceEntry->kind &&
  330. sequenceEntry->kind != SEC_ASN1_SKIP_REST));
  331. /* we should have consumed all the bytes in the sequence by now
  332. unless the caller doesn't care about the rest of the sequence */
  333. if (SECSuccess == rv && sequence.len &&
  334. sequenceEntry && sequenceEntry->kind != SEC_ASN1_SKIP_REST) {
  335. /* it isn't 100% clear whether this is a bad DER or a bad template.
  336. The problem is that logically, they don't match - there is extra
  337. data in the DER that the template doesn't know about */
  338. PORT_SetError(SEC_ERROR_BAD_DER);
  339. rv = SECFailure;
  340. }
  341. return rv;
  342. }
  343. static SECStatus
  344. DecodeInline(void* dest,
  345. const SEC_ASN1Template* templateEntry,
  346. SECItem* src, PLArenaPool* arena, PRBool checkTag)
  347. {
  348. const SEC_ASN1Template* inlineTemplate =
  349. SEC_ASN1GetSubtemplate(templateEntry, dest, PR_FALSE);
  350. return DecodeItem((void*)((char*)dest + templateEntry->offset),
  351. inlineTemplate, src, arena, checkTag);
  352. }
  353. static SECStatus
  354. DecodePointer(void* dest,
  355. const SEC_ASN1Template* templateEntry,
  356. SECItem* src, PLArenaPool* arena, PRBool checkTag)
  357. {
  358. const SEC_ASN1Template* ptrTemplate =
  359. SEC_ASN1GetSubtemplate(templateEntry, dest, PR_FALSE);
  360. if (!ptrTemplate) {
  361. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  362. return SECFailure;
  363. }
  364. void* subdata = PORT_ArenaZAlloc(arena, ptrTemplate->size);
  365. *(void**)((char*)dest + templateEntry->offset) = subdata;
  366. if (subdata) {
  367. return DecodeItem(subdata, ptrTemplate, src, arena, checkTag);
  368. } else {
  369. PORT_SetError(SEC_ERROR_NO_MEMORY);
  370. return SECFailure;
  371. }
  372. }
  373. static SECStatus
  374. DecodeImplicit(void* dest,
  375. const SEC_ASN1Template* templateEntry,
  376. SECItem* src, PLArenaPool* arena)
  377. {
  378. if (templateEntry->kind & SEC_ASN1_POINTER) {
  379. return DecodePointer((void*)((char*)dest),
  380. templateEntry, src, arena, PR_FALSE);
  381. } else {
  382. return DecodeInline((void*)((char*)dest),
  383. templateEntry, src, arena, PR_FALSE);
  384. }
  385. }
  386. static SECStatus
  387. DecodeChoice(void* dest,
  388. const SEC_ASN1Template* templateEntry,
  389. SECItem* src, PLArenaPool* arena)
  390. {
  391. SECStatus rv = SECSuccess;
  392. SECItem choice;
  393. const SEC_ASN1Template* choiceTemplate = &(templateEntry[1]);
  394. const SEC_ASN1Template* choiceEntry = NULL;
  395. unsigned long choiceindex = 0;
  396. /* XXX for a choice component, we should validate the template to make
  397. sure the tags are distinct, in debug builds. This hasn't been
  398. implemented yet */
  399. /* rv = CheckChoiceTemplate(sequenceTemplate); */
  400. /* process it */
  401. do {
  402. choice = *src;
  403. choiceEntry = &choiceTemplate[choiceindex++];
  404. if (choiceEntry->kind) {
  405. rv = DecodeItem(dest, choiceEntry, &choice, arena, PR_TRUE);
  406. }
  407. } while ((SECFailure == rv) && (choiceEntry->kind));
  408. if (SECFailure == rv) {
  409. /* the component didn't match any of the choices */
  410. PORT_SetError(SEC_ERROR_BAD_DER);
  411. } else {
  412. /* set the type in the union here */
  413. int* which = (int*)((char*)dest + templateEntry->offset);
  414. *which = (int)choiceEntry->size;
  415. }
  416. /* we should have consumed all the bytes by now */
  417. /* fail if we have not */
  418. if (SECSuccess == rv && choice.len) {
  419. /* there is extra data that isn't listed in the template */
  420. PORT_SetError(SEC_ERROR_BAD_DER);
  421. rv = SECFailure;
  422. }
  423. return rv;
  424. }
  425. static SECStatus
  426. DecodeGroup(void* dest,
  427. const SEC_ASN1Template* templateEntry,
  428. SECItem* src, PLArenaPool* arena)
  429. {
  430. SECStatus rv = SECSuccess;
  431. SECItem source;
  432. SECItem group;
  433. PRUint32 totalEntries = 0;
  434. PRUint32 entryIndex = 0;
  435. void** entries = NULL;
  436. const SEC_ASN1Template* subTemplate =
  437. SEC_ASN1GetSubtemplate(templateEntry, dest, PR_FALSE);
  438. source = *src;
  439. /* get the group */
  440. if (SECSuccess == rv) {
  441. rv = GetItem(&source, &group, PR_FALSE);
  442. }
  443. /* XXX we should check the subtemplate in debug builds */
  444. if (SECSuccess == rv) {
  445. /* first, count the number of entries. Benchmarking showed that this
  446. counting pass is more efficient than trying to allocate entries as
  447. we read the DER, even if allocating many entries at a time
  448. */
  449. SECItem counter = group;
  450. do {
  451. SECItem anitem;
  452. rv = GetItem(&counter, &anitem, PR_TRUE);
  453. if (SECSuccess == rv && (anitem.len)) {
  454. totalEntries++;
  455. }
  456. } while ((SECSuccess == rv) && (counter.len));
  457. if (SECSuccess == rv) {
  458. /* allocate room for pointer array and entries */
  459. /* we want to allocate the array even if there is 0 entry */
  460. entries = (void**)PORT_ArenaZAlloc(arena, sizeof(void*) * (totalEntries + 1) + /* the extra one is for NULL termination */
  461. subTemplate->size * totalEntries);
  462. if (entries) {
  463. entries[totalEntries] = NULL; /* terminate the array */
  464. } else {
  465. PORT_SetError(SEC_ERROR_NO_MEMORY);
  466. rv = SECFailure;
  467. }
  468. if (SECSuccess == rv) {
  469. void* entriesData = (unsigned char*)entries + (unsigned long)(sizeof(void*) * (totalEntries + 1));
  470. /* and fix the pointers in the array */
  471. PRUint32 entriesIndex = 0;
  472. for (entriesIndex = 0; entriesIndex < totalEntries; entriesIndex++) {
  473. entries[entriesIndex] =
  474. (char*)entriesData + (subTemplate->size * entriesIndex);
  475. }
  476. }
  477. }
  478. }
  479. if (SECSuccess == rv && totalEntries)
  480. do {
  481. if (!(entryIndex < totalEntries)) {
  482. rv = SECFailure;
  483. break;
  484. }
  485. rv = DecodeItem(entries[entryIndex++], subTemplate, &group, arena, PR_TRUE);
  486. } while ((SECSuccess == rv) && (group.len));
  487. /* we should be at the end of the set by now */
  488. /* save the entries where requested */
  489. memcpy(((char*)dest + templateEntry->offset), &entries, sizeof(void**));
  490. return rv;
  491. }
  492. static SECStatus
  493. DecodeExplicit(void* dest,
  494. const SEC_ASN1Template* templateEntry,
  495. SECItem* src, PLArenaPool* arena)
  496. {
  497. SECStatus rv = SECSuccess;
  498. SECItem subItem;
  499. SECItem constructed = *src;
  500. rv = GetItem(&constructed, &subItem, PR_FALSE);
  501. if (SECSuccess == rv) {
  502. if (templateEntry->kind & SEC_ASN1_POINTER) {
  503. rv = DecodePointer(dest, templateEntry, &subItem, arena, PR_TRUE);
  504. } else {
  505. rv = DecodeInline(dest, templateEntry, &subItem, arena, PR_TRUE);
  506. }
  507. }
  508. return rv;
  509. }
  510. /* new decoder implementation. This is a recursive function */
  511. static SECStatus
  512. DecodeItem(void* dest,
  513. const SEC_ASN1Template* templateEntry,
  514. SECItem* src, PLArenaPool* arena, PRBool checkTag)
  515. {
  516. SECStatus rv = SECSuccess;
  517. SECItem temp;
  518. SECItem mark = { siBuffer, NULL, 0 };
  519. PRBool pop = PR_FALSE;
  520. PRBool decode = PR_TRUE;
  521. PRBool save = PR_FALSE;
  522. unsigned long kind;
  523. PRBool match = PR_TRUE;
  524. PR_ASSERT(src && dest && templateEntry && arena);
  525. #if 0
  526. if (!src || !dest || !templateEntry || !arena)
  527. {
  528. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  529. rv = SECFailure;
  530. }
  531. #endif
  532. if (SECSuccess == rv) {
  533. /* do the template validation */
  534. kind = templateEntry->kind;
  535. if (!kind) {
  536. PORT_SetError(SEC_ERROR_BAD_TEMPLATE);
  537. rv = SECFailure;
  538. }
  539. }
  540. if (SECSuccess == rv) {
  541. #ifdef DEBUG
  542. if (kind & SEC_ASN1_DEBUG_BREAK) {
  543. /* when debugging the decoder or a template that fails to
  544. decode, put SEC_ASN1_DEBUG in the component that gives you
  545. trouble. The decoder will then get to this block and assert.
  546. If you want to debug the rest of the code, you can set a
  547. breakpoint and set dontassert to PR_TRUE, which will let
  548. you skip over the assert and continue the debugging session
  549. past it. */
  550. PRBool dontassert = PR_FALSE;
  551. PR_ASSERT(dontassert); /* set bkpoint here & set dontassert*/
  552. }
  553. #endif
  554. if ((kind & SEC_ASN1_SKIP) ||
  555. (kind & SEC_ASN1_SAVE)) {
  556. /* if skipping or saving this component, don't decode it */
  557. decode = PR_FALSE;
  558. }
  559. if (kind & (SEC_ASN1_SAVE | SEC_ASN1_OPTIONAL)) {
  560. /* if saving this component, or if it is optional, we may not want to
  561. move past it, so save the position in case we have to rewind */
  562. mark = *src;
  563. if (kind & SEC_ASN1_SAVE) {
  564. save = PR_TRUE;
  565. if (0 == (kind & SEC_ASN1_SKIP)) {
  566. /* we will for sure have to rewind when saving this
  567. component and not skipping it. This is true for all
  568. legacy uses of SEC_ASN1_SAVE where the following entry
  569. in the template would causes the same component to be
  570. processed again */
  571. pop = PR_TRUE;
  572. }
  573. }
  574. }
  575. rv = GetItem(src, &temp, PR_TRUE);
  576. }
  577. if (SECSuccess == rv) {
  578. /* now check if the component matches what we expect in the template */
  579. if (PR_TRUE == checkTag)
  580. {
  581. rv = MatchComponentType(templateEntry, &temp, &match, dest);
  582. }
  583. if ((SECSuccess == rv) && (PR_TRUE != match)) {
  584. if (kind & SEC_ASN1_OPTIONAL) {
  585. /* the optional component is missing. This is not fatal. */
  586. /* Rewind, don't decode, and don't save */
  587. pop = PR_TRUE;
  588. decode = PR_FALSE;
  589. save = PR_FALSE;
  590. } else {
  591. /* a required component is missing. abort */
  592. PORT_SetError(SEC_ERROR_BAD_DER);
  593. rv = SECFailure;
  594. }
  595. }
  596. }
  597. if ((SECSuccess == rv) && (PR_TRUE == decode)) {
  598. /* the order of processing here is is the tricky part */
  599. /* we start with our special cases */
  600. /* first, check the component class */
  601. if (kind & SEC_ASN1_INLINE) {
  602. /* decode inline template */
  603. rv = DecodeInline(dest, templateEntry, &temp, arena, PR_TRUE);
  604. }
  605. else if (kind & SEC_ASN1_EXPLICIT) {
  606. rv = DecodeExplicit(dest, templateEntry, &temp, arena);
  607. } else if ((SEC_ASN1_UNIVERSAL != (kind & SEC_ASN1_CLASS_MASK)) &&
  608. (!(kind & SEC_ASN1_EXPLICIT))) {
  609. /* decode implicitly tagged components */
  610. rv = DecodeImplicit(dest, templateEntry, &temp, arena);
  611. } else if (kind & SEC_ASN1_POINTER) {
  612. rv = DecodePointer(dest, templateEntry, &temp, arena, PR_TRUE);
  613. } else if (kind & SEC_ASN1_CHOICE) {
  614. rv = DecodeChoice(dest, templateEntry, &temp, arena);
  615. } else if (kind & SEC_ASN1_ANY) {
  616. /* catch-all ANY type, don't decode */
  617. save = PR_TRUE;
  618. if (kind & SEC_ASN1_INNER) {
  619. /* skip the tag and length */
  620. SECItem newtemp = temp;
  621. rv = GetItem(&newtemp, &temp, PR_FALSE);
  622. }
  623. } else if (kind & SEC_ASN1_GROUP) {
  624. if ((SEC_ASN1_SEQUENCE == (kind & SEC_ASN1_TAGNUM_MASK)) ||
  625. (SEC_ASN1_SET == (kind & SEC_ASN1_TAGNUM_MASK))) {
  626. rv = DecodeGroup(dest, templateEntry, &temp, arena);
  627. } else {
  628. /* a group can only be a SET OF or SEQUENCE OF */
  629. PORT_SetError(SEC_ERROR_BAD_TEMPLATE);
  630. rv = SECFailure;
  631. }
  632. } else if (SEC_ASN1_SEQUENCE == (kind & SEC_ASN1_TAGNUM_MASK)) {
  633. /* plain SEQUENCE */
  634. rv = DecodeSequence(dest, templateEntry, &temp, arena);
  635. } else {
  636. /* handle all other types as "save" */
  637. /* we should only get here for primitive universal types */
  638. SECItem newtemp = temp;
  639. rv = GetItem(&newtemp, &temp, PR_FALSE);
  640. save = PR_TRUE;
  641. if ((SECSuccess == rv) &&
  642. SEC_ASN1_UNIVERSAL == (kind & SEC_ASN1_CLASS_MASK)) {
  643. unsigned long tagnum = kind & SEC_ASN1_TAGNUM_MASK;
  644. if (temp.len == 0 && (tagnum == SEC_ASN1_BOOLEAN ||
  645. tagnum == SEC_ASN1_INTEGER ||
  646. tagnum == SEC_ASN1_BIT_STRING ||
  647. tagnum == SEC_ASN1_OBJECT_ID ||
  648. tagnum == SEC_ASN1_ENUMERATED ||
  649. tagnum == SEC_ASN1_UTC_TIME ||
  650. tagnum == SEC_ASN1_GENERALIZED_TIME)) {
  651. /* these types MUST have at least one content octet */
  652. PORT_SetError(SEC_ERROR_BAD_DER);
  653. rv = SECFailure;
  654. } else
  655. switch (tagnum) {
  656. /* special cases of primitive types */
  657. case SEC_ASN1_INTEGER: {
  658. /* remove leading zeroes if the caller requested
  659. siUnsignedInteger
  660. This is to allow RSA key operations to work */
  661. SECItem* destItem = (SECItem*)((char*)dest +
  662. templateEntry->offset);
  663. if (destItem && (siUnsignedInteger == destItem->type)) {
  664. while (temp.len > 1 && temp.data[0] == 0) { /* leading 0 */
  665. temp.data++;
  666. temp.len--;
  667. }
  668. }
  669. break;
  670. }
  671. case SEC_ASN1_BIT_STRING: {
  672. /* Can't be 8 or more spare bits, or any spare bits
  673. * if there are no octets. */
  674. if (temp.data[0] >= 8 || (temp.data[0] > 0 && temp.len == 1)) {
  675. PORT_SetError(SEC_ERROR_BAD_DER);
  676. rv = SECFailure;
  677. break;
  678. }
  679. /* change the length in the SECItem to be the number
  680. of bits */
  681. temp.len = (temp.len - 1) * 8 - (temp.data[0] & 0x7);
  682. temp.data++;
  683. break;
  684. }
  685. default: {
  686. break;
  687. }
  688. }
  689. }
  690. }
  691. }
  692. if ((SECSuccess == rv) && (PR_TRUE == save)) {
  693. SECItem* destItem = (SECItem*)((char*)dest + templateEntry->offset);
  694. if (destItem) {
  695. /* we leave the type alone in the destination SECItem.
  696. If part of the destination was allocated by the decoder, in
  697. cases of POINTER, SET OF and SEQUENCE OF, then type is set to
  698. siBuffer due to the use of PORT_ArenaZAlloc*/
  699. destItem->data = temp.len ? temp.data : NULL;
  700. destItem->len = temp.len;
  701. } else {
  702. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  703. rv = SECFailure;
  704. }
  705. }
  706. if (PR_TRUE == pop) {
  707. /* we don't want to move ahead, so restore the position */
  708. *src = mark;
  709. }
  710. return rv;
  711. }
  712. /* the function below is the public one */
  713. SECStatus
  714. SEC_QuickDERDecodeItem(PLArenaPool* arena, void* dest,
  715. const SEC_ASN1Template* templateEntry,
  716. const SECItem* src)
  717. {
  718. SECStatus rv = SECSuccess;
  719. SECItem newsrc;
  720. if (!arena || !templateEntry || !src) {
  721. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  722. rv = SECFailure;
  723. }
  724. if (SECSuccess == rv) {
  725. newsrc = *src;
  726. rv = DecodeItem(dest, templateEntry, &newsrc, arena, PR_TRUE);
  727. if (SECSuccess == rv && newsrc.len) {
  728. rv = SECFailure;
  729. PORT_SetError(SEC_ERROR_EXTRA_INPUT);
  730. }
  731. }
  732. return rv;
  733. }