secasn1d.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416
  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. * Support for DEcoding ASN.1 data based on BER/DER (Basic/Distinguished
  6. * Encoding Rules).
  7. */
  8. /* #define DEBUG_ASN1D_STATES 1 */
  9. #ifdef DEBUG_ASN1D_STATES
  10. #include <stdio.h>
  11. #define PR_Assert sec_asn1d_Assert
  12. #endif
  13. #include <limits.h>
  14. #include "secasn1.h"
  15. #include "secerr.h"
  16. typedef enum {
  17. beforeIdentifier,
  18. duringIdentifier,
  19. afterIdentifier,
  20. beforeLength,
  21. duringLength,
  22. afterLength,
  23. beforeBitString,
  24. duringBitString,
  25. duringConstructedString,
  26. duringGroup,
  27. duringLeaf,
  28. duringSaveEncoding,
  29. duringSequence,
  30. afterConstructedString,
  31. afterGroup,
  32. afterExplicit,
  33. afterImplicit,
  34. afterInline,
  35. afterPointer,
  36. afterSaveEncoding,
  37. beforeEndOfContents,
  38. duringEndOfContents,
  39. afterEndOfContents,
  40. beforeChoice,
  41. duringChoice,
  42. afterChoice,
  43. notInUse
  44. } sec_asn1d_parse_place;
  45. #ifdef DEBUG_ASN1D_STATES
  46. static const char *const place_names[] = {
  47. "beforeIdentifier",
  48. "duringIdentifier",
  49. "afterIdentifier",
  50. "beforeLength",
  51. "duringLength",
  52. "afterLength",
  53. "beforeBitString",
  54. "duringBitString",
  55. "duringConstructedString",
  56. "duringGroup",
  57. "duringLeaf",
  58. "duringSaveEncoding",
  59. "duringSequence",
  60. "afterConstructedString",
  61. "afterGroup",
  62. "afterExplicit",
  63. "afterImplicit",
  64. "afterInline",
  65. "afterPointer",
  66. "afterSaveEncoding",
  67. "beforeEndOfContents",
  68. "duringEndOfContents",
  69. "afterEndOfContents",
  70. "beforeChoice",
  71. "duringChoice",
  72. "afterChoice",
  73. "notInUse"
  74. };
  75. static const char *const class_names[] = {
  76. "UNIVERSAL",
  77. "APPLICATION",
  78. "CONTEXT_SPECIFIC",
  79. "PRIVATE"
  80. };
  81. static const char *const method_names[] = { "PRIMITIVE", "CONSTRUCTED" };
  82. static const char *const type_names[] = {
  83. "END_OF_CONTENTS",
  84. "BOOLEAN",
  85. "INTEGER",
  86. "BIT_STRING",
  87. "OCTET_STRING",
  88. "NULL",
  89. "OBJECT_ID",
  90. "OBJECT_DESCRIPTOR",
  91. "(type 08)",
  92. "REAL",
  93. "ENUMERATED",
  94. "EMBEDDED",
  95. "UTF8_STRING",
  96. "(type 0d)",
  97. "(type 0e)",
  98. "(type 0f)",
  99. "SEQUENCE",
  100. "SET",
  101. "NUMERIC_STRING",
  102. "PRINTABLE_STRING",
  103. "T61_STRING",
  104. "VIDEOTEXT_STRING",
  105. "IA5_STRING",
  106. "UTC_TIME",
  107. "GENERALIZED_TIME",
  108. "GRAPHIC_STRING",
  109. "VISIBLE_STRING",
  110. "GENERAL_STRING",
  111. "UNIVERSAL_STRING",
  112. "(type 1d)",
  113. "BMP_STRING",
  114. "HIGH_TAG_VALUE"
  115. };
  116. static const char *const flag_names[] = {
  117. /* flags, right to left */
  118. "OPTIONAL",
  119. "EXPLICIT",
  120. "ANY",
  121. "INLINE",
  122. "POINTER",
  123. "GROUP",
  124. "DYNAMIC",
  125. "SKIP",
  126. "INNER",
  127. "SAVE",
  128. "", /* decoder ignores "MAY_STREAM", */
  129. "SKIP_REST",
  130. "CHOICE",
  131. "NO_STREAM",
  132. "DEBUG_BREAK",
  133. "unknown 08",
  134. "unknown 10",
  135. "unknown 20",
  136. "unknown 40",
  137. "unknown 80"
  138. };
  139. static int /* bool */
  140. formatKind(unsigned long kind, char *buf)
  141. {
  142. int i;
  143. unsigned long k = kind & SEC_ASN1_TAGNUM_MASK;
  144. unsigned long notag = kind & (SEC_ASN1_CHOICE | SEC_ASN1_POINTER |
  145. SEC_ASN1_INLINE | SEC_ASN1_ANY | SEC_ASN1_SAVE);
  146. buf[0] = 0;
  147. if ((kind & SEC_ASN1_CLASS_MASK) != SEC_ASN1_UNIVERSAL) {
  148. sprintf(buf, " %s", class_names[(kind & SEC_ASN1_CLASS_MASK) >> 6]);
  149. buf += strlen(buf);
  150. }
  151. if (kind & SEC_ASN1_METHOD_MASK) {
  152. sprintf(buf, " %s", method_names[1]);
  153. buf += strlen(buf);
  154. }
  155. if ((kind & SEC_ASN1_CLASS_MASK) == SEC_ASN1_UNIVERSAL) {
  156. if (k || !notag) {
  157. sprintf(buf, " %s", type_names[k]);
  158. if ((k == SEC_ASN1_SET || k == SEC_ASN1_SEQUENCE) &&
  159. (kind & SEC_ASN1_GROUP)) {
  160. buf += strlen(buf);
  161. sprintf(buf, "_OF");
  162. }
  163. }
  164. } else {
  165. sprintf(buf, " [%lu]", k);
  166. }
  167. buf += strlen(buf);
  168. for (k = kind >> 8, i = 0; k; k >>= 1, ++i) {
  169. if (k & 1) {
  170. sprintf(buf, " %s", flag_names[i]);
  171. buf += strlen(buf);
  172. }
  173. }
  174. return notag != 0;
  175. }
  176. #endif /* DEBUG_ASN1D_STATES */
  177. typedef enum {
  178. allDone,
  179. decodeError,
  180. keepGoing,
  181. needBytes
  182. } sec_asn1d_parse_status;
  183. struct subitem {
  184. const void *data;
  185. unsigned long len; /* only used for substrings */
  186. struct subitem *next;
  187. };
  188. typedef struct sec_asn1d_state_struct {
  189. SEC_ASN1DecoderContext *top;
  190. const SEC_ASN1Template *theTemplate;
  191. void *dest;
  192. void *our_mark; /* free on completion */
  193. struct sec_asn1d_state_struct *parent; /* aka prev */
  194. struct sec_asn1d_state_struct *child; /* aka next */
  195. sec_asn1d_parse_place place;
  196. /*
  197. * XXX explain the next fields as clearly as possible...
  198. */
  199. unsigned char found_tag_modifiers;
  200. unsigned char expect_tag_modifiers;
  201. unsigned long check_tag_mask;
  202. unsigned long found_tag_number;
  203. unsigned long expect_tag_number;
  204. unsigned long underlying_kind;
  205. unsigned long contents_length;
  206. unsigned long pending;
  207. unsigned long consumed;
  208. int depth;
  209. /*
  210. * Bit strings have their length adjusted -- the first octet of the
  211. * contents contains a value between 0 and 7 which says how many bits
  212. * at the end of the octets are not actually part of the bit string;
  213. * when parsing bit strings we put that value here because we need it
  214. * later, for adjustment of the length (when the whole string is done).
  215. */
  216. unsigned int bit_string_unused_bits;
  217. /*
  218. * The following are used for indefinite-length constructed strings.
  219. */
  220. struct subitem *subitems_head;
  221. struct subitem *subitems_tail;
  222. PRPackedBool
  223. allocate, /* when true, need to allocate the destination */
  224. endofcontents, /* this state ended up parsing end-of-contents octets */
  225. explicit, /* we are handling an explicit header */
  226. indefinite, /* the current item has indefinite-length encoding */
  227. missing, /* an optional field that was not present */
  228. optional, /* the template says this field may be omitted */
  229. substring; /* this is a substring of a constructed string */
  230. } sec_asn1d_state;
  231. #define IS_HIGH_TAG_NUMBER(n) ((n) == SEC_ASN1_HIGH_TAG_NUMBER)
  232. #define LAST_TAG_NUMBER_BYTE(b) (((b)&0x80) == 0)
  233. #define TAG_NUMBER_BITS 7
  234. #define TAG_NUMBER_MASK 0x7f
  235. #define LENGTH_IS_SHORT_FORM(b) (((b)&0x80) == 0)
  236. #define LONG_FORM_LENGTH(b) ((b)&0x7f)
  237. #define HIGH_BITS(field, cnt) ((field) >> ((sizeof(field) * 8) - (cnt)))
  238. /*
  239. * An "outsider" will have an opaque pointer to this, created by calling
  240. * SEC_ASN1DecoderStart(). It will be passed back in to all subsequent
  241. * calls to SEC_ASN1DecoderUpdate(), and when done it is passed to
  242. * SEC_ASN1DecoderFinish().
  243. */
  244. struct sec_DecoderContext_struct {
  245. PLArenaPool *our_pool; /* for our internal allocs */
  246. PLArenaPool *their_pool; /* for destination structure allocs */
  247. #ifdef SEC_ASN1D_FREE_ON_ERROR /* \
  248. * XXX see comment below (by same \
  249. * ifdef) that explains why this \
  250. * does not work (need more smarts \
  251. * in order to free back to mark) \
  252. */
  253. /*
  254. * XXX how to make their_mark work in the case where they do NOT
  255. * give us a pool pointer?
  256. */
  257. void *their_mark; /* free on error */
  258. #endif
  259. sec_asn1d_state *current;
  260. sec_asn1d_parse_status status;
  261. /* The maximum size the caller is willing to allow a single element
  262. * to be before returning an error.
  263. *
  264. * In the case of an indefinite length element, this is the sum total
  265. * of all child elements.
  266. *
  267. * In the case of a definite length element, this represents the maximum
  268. * size of the top-level element.
  269. */
  270. unsigned long max_element_size;
  271. SEC_ASN1NotifyProc notify_proc; /* call before/after handling field */
  272. void *notify_arg; /* argument to notify_proc */
  273. PRBool during_notify; /* true during call to notify_proc */
  274. SEC_ASN1WriteProc filter_proc; /* pass field bytes to this */
  275. void *filter_arg; /* argument to that function */
  276. PRBool filter_only; /* do not allocate/store fields */
  277. };
  278. /*
  279. * XXX this is a fairly generic function that may belong elsewhere
  280. */
  281. static void *
  282. sec_asn1d_alloc(PLArenaPool *poolp, unsigned long len)
  283. {
  284. void *thing;
  285. if (poolp != NULL) {
  286. /*
  287. * Allocate from the pool.
  288. */
  289. thing = PORT_ArenaAlloc(poolp, len);
  290. } else {
  291. /*
  292. * Allocate generically.
  293. */
  294. thing = PORT_Alloc(len);
  295. }
  296. return thing;
  297. }
  298. /*
  299. * XXX this is a fairly generic function that may belong elsewhere
  300. */
  301. static void *
  302. sec_asn1d_zalloc(PLArenaPool *poolp, unsigned long len)
  303. {
  304. void *thing;
  305. thing = sec_asn1d_alloc(poolp, len);
  306. if (thing != NULL)
  307. PORT_Memset(thing, 0, len);
  308. return thing;
  309. }
  310. static sec_asn1d_state *
  311. sec_asn1d_push_state(SEC_ASN1DecoderContext *cx,
  312. const SEC_ASN1Template *theTemplate,
  313. void *dest, PRBool new_depth)
  314. {
  315. sec_asn1d_state *state, *new_state;
  316. state = cx->current;
  317. PORT_Assert(state == NULL || state->child == NULL);
  318. if (state != NULL) {
  319. PORT_Assert(state->our_mark == NULL);
  320. state->our_mark = PORT_ArenaMark(cx->our_pool);
  321. }
  322. new_state = (sec_asn1d_state *)sec_asn1d_zalloc(cx->our_pool,
  323. sizeof(*new_state));
  324. if (new_state == NULL) {
  325. goto loser;
  326. }
  327. new_state->top = cx;
  328. new_state->parent = state;
  329. new_state->theTemplate = theTemplate;
  330. new_state->place = notInUse;
  331. if (dest != NULL)
  332. new_state->dest = (char *)dest + theTemplate->offset;
  333. if (state != NULL) {
  334. new_state->depth = state->depth;
  335. if (new_depth) {
  336. if (++new_state->depth > SEC_ASN1D_MAX_DEPTH) {
  337. PORT_SetError(SEC_ERROR_BAD_DER);
  338. goto loser;
  339. }
  340. }
  341. state->child = new_state;
  342. }
  343. cx->current = new_state;
  344. return new_state;
  345. loser:
  346. cx->status = decodeError;
  347. if (state != NULL) {
  348. PORT_ArenaRelease(cx->our_pool, state->our_mark);
  349. state->our_mark = NULL;
  350. }
  351. return NULL;
  352. }
  353. static void
  354. sec_asn1d_scrub_state(sec_asn1d_state *state)
  355. {
  356. /*
  357. * Some default "scrubbing".
  358. * XXX right set of initializations?
  359. */
  360. state->place = beforeIdentifier;
  361. state->endofcontents = PR_FALSE;
  362. state->indefinite = PR_FALSE;
  363. state->missing = PR_FALSE;
  364. PORT_Assert(state->consumed == 0);
  365. }
  366. static void
  367. sec_asn1d_notify_before(SEC_ASN1DecoderContext *cx, void *dest, int depth)
  368. {
  369. if (cx->notify_proc == NULL)
  370. return;
  371. cx->during_notify = PR_TRUE;
  372. (*cx->notify_proc)(cx->notify_arg, PR_TRUE, dest, depth);
  373. cx->during_notify = PR_FALSE;
  374. }
  375. static void
  376. sec_asn1d_notify_after(SEC_ASN1DecoderContext *cx, void *dest, int depth)
  377. {
  378. if (cx->notify_proc == NULL)
  379. return;
  380. cx->during_notify = PR_TRUE;
  381. (*cx->notify_proc)(cx->notify_arg, PR_FALSE, dest, depth);
  382. cx->during_notify = PR_FALSE;
  383. }
  384. static sec_asn1d_state *
  385. sec_asn1d_init_state_based_on_template(sec_asn1d_state *state)
  386. {
  387. PRBool explicit, optional, universal;
  388. unsigned char expect_tag_modifiers;
  389. unsigned long encode_kind, under_kind;
  390. unsigned long check_tag_mask, expect_tag_number;
  391. /* XXX Check that both of these tests are really needed/appropriate. */
  392. if (state == NULL || state->top->status == decodeError)
  393. return state;
  394. encode_kind = state->theTemplate->kind;
  395. if (encode_kind & SEC_ASN1_SAVE) {
  396. /*
  397. * This is a "magic" field that saves away all bytes, allowing
  398. * the immediately following field to still be decoded from this
  399. * same spot -- sort of a fork.
  400. */
  401. /* check that there are no extraneous bits */
  402. PORT_Assert(encode_kind == SEC_ASN1_SAVE);
  403. if (state->top->filter_only) {
  404. /*
  405. * If we are not storing, then we do not do the SAVE field
  406. * at all. Just move ahead to the "real" field instead,
  407. * doing the appropriate notify calls before and after.
  408. */
  409. sec_asn1d_notify_after(state->top, state->dest, state->depth);
  410. /*
  411. * Since we are not storing, allow for our current dest value
  412. * to be NULL. (This might not actually occur, but right now I
  413. * cannot convince myself one way or the other.) If it is NULL,
  414. * assume that our parent dest can help us out.
  415. */
  416. if (state->dest == NULL)
  417. state->dest = state->parent->dest;
  418. else
  419. state->dest = (char *)state->dest - state->theTemplate->offset;
  420. state->theTemplate++;
  421. if (state->dest != NULL)
  422. state->dest = (char *)state->dest + state->theTemplate->offset;
  423. sec_asn1d_notify_before(state->top, state->dest, state->depth);
  424. encode_kind = state->theTemplate->kind;
  425. PORT_Assert((encode_kind & SEC_ASN1_SAVE) == 0);
  426. } else {
  427. sec_asn1d_scrub_state(state);
  428. state->place = duringSaveEncoding;
  429. state = sec_asn1d_push_state(state->top, SEC_AnyTemplate,
  430. state->dest, PR_FALSE);
  431. if (state != NULL)
  432. state = sec_asn1d_init_state_based_on_template(state);
  433. return state;
  434. }
  435. }
  436. universal = ((encode_kind & SEC_ASN1_CLASS_MASK) == SEC_ASN1_UNIVERSAL)
  437. ? PR_TRUE
  438. : PR_FALSE;
  439. explicit = (encode_kind & SEC_ASN1_EXPLICIT) ? PR_TRUE : PR_FALSE;
  440. encode_kind &= ~SEC_ASN1_EXPLICIT;
  441. optional = (encode_kind & SEC_ASN1_OPTIONAL) ? PR_TRUE : PR_FALSE;
  442. encode_kind &= ~SEC_ASN1_OPTIONAL;
  443. PORT_Assert(!(explicit && universal)); /* bad templates */
  444. encode_kind &= ~SEC_ASN1_DYNAMIC;
  445. encode_kind &= ~SEC_ASN1_MAY_STREAM;
  446. if (encode_kind & SEC_ASN1_CHOICE) {
  447. #if 0 /* XXX remove? */
  448. sec_asn1d_state *child = sec_asn1d_push_state(state->top, state->theTemplate, state->dest, PR_FALSE);
  449. if ((sec_asn1d_state *)NULL == child) {
  450. return (sec_asn1d_state *)NULL;
  451. }
  452. child->allocate = state->allocate;
  453. child->place = beforeChoice;
  454. return child;
  455. #else
  456. state->place = beforeChoice;
  457. return state;
  458. #endif
  459. }
  460. if ((encode_kind & (SEC_ASN1_POINTER | SEC_ASN1_INLINE)) || (!universal && !explicit)) {
  461. const SEC_ASN1Template *subt;
  462. void *dest;
  463. PRBool child_allocate;
  464. PORT_Assert((encode_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP)) == 0);
  465. sec_asn1d_scrub_state(state);
  466. child_allocate = PR_FALSE;
  467. if (encode_kind & SEC_ASN1_POINTER) {
  468. /*
  469. * A POINTER means we need to allocate the destination for
  470. * this field. But, since it may also be an optional field,
  471. * we defer the allocation until later; we just record that
  472. * it needs to be done.
  473. *
  474. * There are two possible scenarios here -- one is just a
  475. * plain POINTER (kind of like INLINE, except with allocation)
  476. * and the other is an implicitly-tagged POINTER. We don't
  477. * need to do anything special here for the two cases, but
  478. * since the template definition can be tricky, we do check
  479. * that there are no extraneous bits set in encode_kind.
  480. *
  481. * XXX The same conditions which assert should set an error.
  482. */
  483. if (universal) {
  484. /*
  485. * "universal" means this entry is a standalone POINTER;
  486. * there should be no other bits set in encode_kind.
  487. */
  488. PORT_Assert(encode_kind == SEC_ASN1_POINTER);
  489. } else {
  490. /*
  491. * If we get here we have an implicitly-tagged field
  492. * that needs to be put into a POINTER. The subtemplate
  493. * will determine how to decode the field, but encode_kind
  494. * describes the (implicit) tag we are looking for.
  495. * The non-tag bits of encode_kind will be ignored by
  496. * the code below; none of them should be set, however,
  497. * except for the POINTER bit itself -- so check that.
  498. */
  499. PORT_Assert((encode_kind & ~SEC_ASN1_TAG_MASK) == SEC_ASN1_POINTER);
  500. }
  501. if (!state->top->filter_only)
  502. child_allocate = PR_TRUE;
  503. dest = NULL;
  504. state->place = afterPointer;
  505. } else {
  506. dest = state->dest;
  507. if (encode_kind & SEC_ASN1_INLINE) {
  508. /* check that there are no extraneous bits */
  509. PORT_Assert(encode_kind == SEC_ASN1_INLINE && !optional);
  510. state->place = afterInline;
  511. } else {
  512. state->place = afterImplicit;
  513. }
  514. }
  515. state->optional = optional;
  516. subt = SEC_ASN1GetSubtemplate(state->theTemplate, state->dest, PR_FALSE);
  517. state = sec_asn1d_push_state(state->top, subt, dest, PR_FALSE);
  518. if (state == NULL)
  519. return NULL;
  520. state->allocate = child_allocate;
  521. if (universal) {
  522. state = sec_asn1d_init_state_based_on_template(state);
  523. if (state != NULL) {
  524. /*
  525. * If this field is optional, we need to record that on
  526. * the pushed child so it won't fail if the field isn't
  527. * found. I can't think of a way that this new state
  528. * could already have optional set (which we would wipe
  529. * out below if our local optional is not set) -- but
  530. * just to be sure, assert that it isn't set.
  531. */
  532. PORT_Assert(!state->optional);
  533. state->optional = optional;
  534. }
  535. return state;
  536. }
  537. under_kind = state->theTemplate->kind;
  538. under_kind &= ~SEC_ASN1_MAY_STREAM;
  539. } else if (explicit) {
  540. /*
  541. * For explicit, we only need to match the encoding tag next,
  542. * then we will push another state to handle the entire inner
  543. * part. In this case, there is no underlying kind which plays
  544. * any part in the determination of the outer, explicit tag.
  545. * So we just set under_kind to 0, which is not a valid tag,
  546. * and the rest of the tag matching stuff should be okay.
  547. */
  548. under_kind = 0;
  549. } else {
  550. /*
  551. * Nothing special; the underlying kind and the given encoding
  552. * information are the same.
  553. */
  554. under_kind = encode_kind;
  555. }
  556. /* XXX is this the right set of bits to test here? */
  557. PORT_Assert((under_kind & (SEC_ASN1_EXPLICIT | SEC_ASN1_OPTIONAL | SEC_ASN1_MAY_STREAM | SEC_ASN1_INLINE | SEC_ASN1_POINTER)) == 0);
  558. if (encode_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP)) {
  559. PORT_Assert(encode_kind == under_kind);
  560. if (encode_kind & SEC_ASN1_SKIP) {
  561. PORT_Assert(!optional);
  562. PORT_Assert(encode_kind == SEC_ASN1_SKIP);
  563. state->dest = NULL;
  564. }
  565. check_tag_mask = 0;
  566. expect_tag_modifiers = 0;
  567. expect_tag_number = 0;
  568. } else {
  569. check_tag_mask = SEC_ASN1_TAG_MASK;
  570. expect_tag_modifiers = (unsigned char)encode_kind & SEC_ASN1_TAG_MASK & ~SEC_ASN1_TAGNUM_MASK;
  571. /*
  572. * XXX This assumes only single-octet identifiers. To handle
  573. * the HIGH TAG form we would need to do some more work, especially
  574. * in how to specify them in the template, because right now we
  575. * do not provide a way to specify more *tag* bits in encode_kind.
  576. */
  577. expect_tag_number = encode_kind & SEC_ASN1_TAGNUM_MASK;
  578. switch (under_kind & SEC_ASN1_TAGNUM_MASK) {
  579. case SEC_ASN1_SET:
  580. /*
  581. * XXX A plain old SET (as opposed to a SET OF) is not implemented.
  582. * If it ever is, remove this assert...
  583. */
  584. PORT_Assert((under_kind & SEC_ASN1_GROUP) != 0);
  585. /* fallthru */
  586. case SEC_ASN1_SEQUENCE:
  587. expect_tag_modifiers |= SEC_ASN1_CONSTRUCTED;
  588. break;
  589. case SEC_ASN1_BIT_STRING:
  590. case SEC_ASN1_BMP_STRING:
  591. case SEC_ASN1_GENERALIZED_TIME:
  592. case SEC_ASN1_IA5_STRING:
  593. case SEC_ASN1_OCTET_STRING:
  594. case SEC_ASN1_PRINTABLE_STRING:
  595. case SEC_ASN1_T61_STRING:
  596. case SEC_ASN1_UNIVERSAL_STRING:
  597. case SEC_ASN1_UTC_TIME:
  598. case SEC_ASN1_UTF8_STRING:
  599. case SEC_ASN1_VISIBLE_STRING:
  600. check_tag_mask &= ~SEC_ASN1_CONSTRUCTED;
  601. break;
  602. }
  603. }
  604. state->check_tag_mask = check_tag_mask;
  605. state->expect_tag_modifiers = expect_tag_modifiers;
  606. state->expect_tag_number = expect_tag_number;
  607. state->underlying_kind = under_kind;
  608. state->explicit = explicit;
  609. state->optional = optional;
  610. sec_asn1d_scrub_state(state);
  611. return state;
  612. }
  613. static sec_asn1d_state *
  614. sec_asn1d_get_enclosing_construct(sec_asn1d_state *state)
  615. {
  616. for (state = state->parent; state; state = state->parent) {
  617. sec_asn1d_parse_place place = state->place;
  618. if (place != afterImplicit &&
  619. place != afterPointer &&
  620. place != afterInline &&
  621. place != afterSaveEncoding &&
  622. place != duringSaveEncoding &&
  623. place != duringChoice) {
  624. /* we've walked up the stack to a state that represents
  625. ** the enclosing construct.
  626. */
  627. break;
  628. }
  629. }
  630. return state;
  631. }
  632. static PRBool
  633. sec_asn1d_parent_allows_EOC(sec_asn1d_state *state)
  634. {
  635. /* get state of enclosing construct. */
  636. state = sec_asn1d_get_enclosing_construct(state);
  637. if (state) {
  638. sec_asn1d_parse_place place = state->place;
  639. /* Is it one of the types that permits an unexpected EOC? */
  640. int eoc_permitted =
  641. (place == duringGroup ||
  642. place == duringConstructedString ||
  643. state->child->optional);
  644. return (state->indefinite && eoc_permitted) ? PR_TRUE : PR_FALSE;
  645. }
  646. return PR_FALSE;
  647. }
  648. static unsigned long
  649. sec_asn1d_parse_identifier(sec_asn1d_state *state,
  650. const char *buf, unsigned long len)
  651. {
  652. unsigned char byte;
  653. unsigned char tag_number;
  654. PORT_Assert(state->place == beforeIdentifier);
  655. if (len == 0) {
  656. state->top->status = needBytes;
  657. return 0;
  658. }
  659. byte = (unsigned char)*buf;
  660. #ifdef DEBUG_ASN1D_STATES
  661. {
  662. char kindBuf[256];
  663. formatKind(byte, kindBuf);
  664. printf("Found tag %02x %s\n", byte, kindBuf);
  665. }
  666. #endif
  667. tag_number = byte & SEC_ASN1_TAGNUM_MASK;
  668. if (IS_HIGH_TAG_NUMBER(tag_number)) {
  669. state->place = duringIdentifier;
  670. state->found_tag_number = 0;
  671. /*
  672. * Actually, we have no idea how many bytes are pending, but we
  673. * do know that it is at least 1. That is all we know; we have
  674. * to look at each byte to know if there is another, etc.
  675. */
  676. state->pending = 1;
  677. } else {
  678. if (byte == 0 && sec_asn1d_parent_allows_EOC(state)) {
  679. /*
  680. * Our parent has indefinite-length encoding, and the
  681. * entire tag found is 0, so it seems that we have hit the
  682. * end-of-contents octets. To handle this, we just change
  683. * our state to that which expects to get the bytes of the
  684. * end-of-contents octets and let that code re-read this byte
  685. * so that our categorization of field types is correct.
  686. * After that, our parent will then deal with everything else.
  687. */
  688. state->place = duringEndOfContents;
  689. state->pending = 2;
  690. state->found_tag_number = 0;
  691. state->found_tag_modifiers = 0;
  692. /*
  693. * We might be an optional field that is, as we now find out,
  694. * missing. Give our parent a clue that this happened.
  695. */
  696. if (state->optional)
  697. state->missing = PR_TRUE;
  698. return 0;
  699. }
  700. state->place = afterIdentifier;
  701. state->found_tag_number = tag_number;
  702. }
  703. state->found_tag_modifiers = byte & ~SEC_ASN1_TAGNUM_MASK;
  704. return 1;
  705. }
  706. static unsigned long
  707. sec_asn1d_parse_more_identifier(sec_asn1d_state *state,
  708. const char *buf, unsigned long len)
  709. {
  710. unsigned char byte;
  711. int count;
  712. PORT_Assert(state->pending == 1);
  713. PORT_Assert(state->place == duringIdentifier);
  714. if (len == 0) {
  715. state->top->status = needBytes;
  716. return 0;
  717. }
  718. count = 0;
  719. while (len && state->pending) {
  720. if (HIGH_BITS(state->found_tag_number, TAG_NUMBER_BITS) != 0) {
  721. /*
  722. * The given high tag number overflows our container;
  723. * just give up. This is not likely to *ever* happen.
  724. */
  725. PORT_SetError(SEC_ERROR_BAD_DER);
  726. state->top->status = decodeError;
  727. return 0;
  728. }
  729. state->found_tag_number <<= TAG_NUMBER_BITS;
  730. byte = (unsigned char)buf[count++];
  731. state->found_tag_number |= (byte & TAG_NUMBER_MASK);
  732. len--;
  733. if (LAST_TAG_NUMBER_BYTE(byte))
  734. state->pending = 0;
  735. }
  736. if (state->pending == 0)
  737. state->place = afterIdentifier;
  738. return count;
  739. }
  740. static void
  741. sec_asn1d_confirm_identifier(sec_asn1d_state *state)
  742. {
  743. PRBool match;
  744. PORT_Assert(state->place == afterIdentifier);
  745. match = (PRBool)(((state->found_tag_modifiers & state->check_tag_mask) == state->expect_tag_modifiers) && ((state->found_tag_number & state->check_tag_mask) == state->expect_tag_number));
  746. if (match) {
  747. state->place = beforeLength;
  748. } else {
  749. if (state->optional) {
  750. state->missing = PR_TRUE;
  751. state->place = afterEndOfContents;
  752. } else {
  753. PORT_SetError(SEC_ERROR_BAD_DER);
  754. state->top->status = decodeError;
  755. }
  756. }
  757. }
  758. static unsigned long
  759. sec_asn1d_parse_length(sec_asn1d_state *state,
  760. const char *buf, unsigned long len)
  761. {
  762. unsigned char byte;
  763. PORT_Assert(state->place == beforeLength);
  764. if (len == 0) {
  765. state->top->status = needBytes;
  766. return 0;
  767. }
  768. /*
  769. * The default/likely outcome. It may get adjusted below.
  770. */
  771. state->place = afterLength;
  772. byte = (unsigned char)*buf;
  773. if (LENGTH_IS_SHORT_FORM(byte)) {
  774. state->contents_length = byte;
  775. } else {
  776. state->contents_length = 0;
  777. state->pending = LONG_FORM_LENGTH(byte);
  778. if (state->pending == 0) {
  779. state->indefinite = PR_TRUE;
  780. } else {
  781. state->place = duringLength;
  782. }
  783. }
  784. /* If we're parsing an ANY, SKIP, or SAVE template, and
  785. ** the object being saved is definite length encoded and constructed,
  786. ** there's no point in decoding that construct's members.
  787. ** So, just forget it's constructed and treat it as primitive.
  788. ** (SAVE appears as an ANY at this point)
  789. */
  790. if (!state->indefinite &&
  791. (state->underlying_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP))) {
  792. state->found_tag_modifiers &= ~SEC_ASN1_CONSTRUCTED;
  793. }
  794. return 1;
  795. }
  796. static unsigned long
  797. sec_asn1d_parse_more_length(sec_asn1d_state *state,
  798. const char *buf, unsigned long len)
  799. {
  800. int count;
  801. PORT_Assert(state->pending > 0);
  802. PORT_Assert(state->place == duringLength);
  803. if (len == 0) {
  804. state->top->status = needBytes;
  805. return 0;
  806. }
  807. count = 0;
  808. while (len && state->pending) {
  809. if (HIGH_BITS(state->contents_length, 9) != 0) {
  810. /*
  811. * The given full content length overflows our container;
  812. * just give up.
  813. */
  814. PORT_SetError(SEC_ERROR_BAD_DER);
  815. state->top->status = decodeError;
  816. return 0;
  817. }
  818. state->contents_length <<= 8;
  819. state->contents_length |= (unsigned char)buf[count++];
  820. len--;
  821. state->pending--;
  822. }
  823. if (state->pending == 0)
  824. state->place = afterLength;
  825. return count;
  826. }
  827. /*
  828. * Helper function for sec_asn1d_prepare_for_contents.
  829. * Checks that a value representing a number of bytes consumed can be
  830. * subtracted from a remaining length. If so, returns PR_TRUE.
  831. * Otherwise, sets the error SEC_ERROR_BAD_DER, indicates that there was a
  832. * decoding error in the given SEC_ASN1DecoderContext, and returns PR_FALSE.
  833. */
  834. static PRBool
  835. sec_asn1d_check_and_subtract_length(unsigned long *remaining,
  836. unsigned long consumed,
  837. SEC_ASN1DecoderContext *cx)
  838. {
  839. PORT_Assert(remaining);
  840. PORT_Assert(cx);
  841. if (!remaining || !cx) {
  842. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  843. cx->status = decodeError;
  844. return PR_FALSE;
  845. }
  846. if (*remaining < consumed) {
  847. PORT_SetError(SEC_ERROR_BAD_DER);
  848. cx->status = decodeError;
  849. return PR_FALSE;
  850. }
  851. *remaining -= consumed;
  852. return PR_TRUE;
  853. }
  854. static void
  855. sec_asn1d_prepare_for_contents(sec_asn1d_state *state)
  856. {
  857. SECItem *item;
  858. PLArenaPool *poolp;
  859. unsigned long alloc_len;
  860. sec_asn1d_state *parent;
  861. #ifdef DEBUG_ASN1D_STATES
  862. {
  863. printf("Found Length %lu %s\n", state->contents_length,
  864. state->indefinite ? "indefinite" : "");
  865. }
  866. #endif
  867. /**
  868. * The maximum length for a child element should be constrained to the
  869. * length remaining in the first definite length element in the ancestor
  870. * stack. If there is no definite length element in the ancestor stack,
  871. * there's nothing to constrain the length of the child, so there's no
  872. * further processing necessary.
  873. *
  874. * It's necessary to walk the ancestor stack, because it's possible to have
  875. * definite length children that are part of an indefinite length element,
  876. * which is itself part of an indefinite length element, and which is
  877. * ultimately part of a definite length element. A simple example of this
  878. * would be the handling of constructed OCTET STRINGs in BER encoding.
  879. *
  880. * This algorithm finds the first definite length element in the ancestor
  881. * stack, if any, and if so, ensures that the length of the child element
  882. * is consistent with the number of bytes remaining in the constraining
  883. * ancestor element (that is, after accounting for any other sibling
  884. * elements that may have been read).
  885. *
  886. * It's slightly complicated by the need to account both for integer
  887. * underflow and overflow, as well as ensure that for indefinite length
  888. * encodings, there's also enough space for the End-of-Contents (EOC)
  889. * octets (Tag = 0x00, Length = 0x00, or two bytes).
  890. */
  891. /* Determine the maximum length available for this element by finding the
  892. * first definite length ancestor, if any. */
  893. parent = sec_asn1d_get_enclosing_construct(state);
  894. while (parent && parent->indefinite) {
  895. parent = sec_asn1d_get_enclosing_construct(parent);
  896. }
  897. /* If parent is null, state is either the outermost state / at the top of
  898. * the stack, or the outermost state uses indefinite length encoding. In
  899. * these cases, there's nothing external to constrain this element, so
  900. * there's nothing to check. */
  901. if (parent) {
  902. unsigned long remaining = parent->pending;
  903. parent = state;
  904. do {
  905. if (!sec_asn1d_check_and_subtract_length(
  906. &remaining, parent->consumed, state->top) ||
  907. /* If parent->indefinite is true, parent->contents_length is
  908. * zero and this is a no-op. */
  909. !sec_asn1d_check_and_subtract_length(
  910. &remaining, parent->contents_length, state->top) ||
  911. /* If parent->indefinite is true, then ensure there is enough
  912. * space for an EOC tag of 2 bytes. */
  913. (parent->indefinite && !sec_asn1d_check_and_subtract_length(&remaining, 2, state->top))) {
  914. /* This element is larger than its enclosing element, which is
  915. * invalid. */
  916. return;
  917. }
  918. } while ((parent = sec_asn1d_get_enclosing_construct(parent)) &&
  919. parent->indefinite);
  920. }
  921. /*
  922. * XXX I cannot decide if this allocation should exclude the case
  923. * where state->endofcontents is true -- figure it out!
  924. */
  925. if (state->allocate) {
  926. void *dest;
  927. PORT_Assert(state->dest == NULL);
  928. /*
  929. * We are handling a POINTER or a member of a GROUP, and need to
  930. * allocate for the data structure.
  931. */
  932. dest = sec_asn1d_zalloc(state->top->their_pool,
  933. state->theTemplate->size);
  934. if (dest == NULL) {
  935. state->top->status = decodeError;
  936. return;
  937. }
  938. state->dest = (char *)dest + state->theTemplate->offset;
  939. /*
  940. * For a member of a GROUP, our parent will later put the
  941. * pointer wherever it belongs. But for a POINTER, we need
  942. * to record the destination now, in case notify or filter
  943. * procs need access to it -- they cannot find it otherwise,
  944. * until it is too late (for one-pass processing).
  945. */
  946. if (state->parent->place == afterPointer) {
  947. void **placep;
  948. placep = state->parent->dest;
  949. *placep = dest;
  950. }
  951. }
  952. /*
  953. * Remember, length may be indefinite here! In that case,
  954. * both contents_length and pending will be zero.
  955. */
  956. state->pending = state->contents_length;
  957. /*
  958. * An EXPLICIT is nothing but an outer header, which we have
  959. * already parsed and accepted. Now we need to do the inner
  960. * header and its contents.
  961. */
  962. if (state->explicit) {
  963. state->place = afterExplicit;
  964. state = sec_asn1d_push_state(state->top,
  965. SEC_ASN1GetSubtemplate(state->theTemplate,
  966. state->dest,
  967. PR_FALSE),
  968. state->dest, PR_TRUE);
  969. if (state != NULL) {
  970. (void)sec_asn1d_init_state_based_on_template(state);
  971. }
  972. return;
  973. }
  974. /*
  975. * For GROUP (SET OF, SEQUENCE OF), even if we know the length here
  976. * we cannot tell how many items we will end up with ... so push a
  977. * state that can keep track of "children" (the individual members
  978. * of the group; we will allocate as we go and put them all together
  979. * at the end.
  980. */
  981. if (state->underlying_kind & SEC_ASN1_GROUP) {
  982. /* XXX If this assertion holds (should be able to confirm it via
  983. * inspection, too) then move this code into the switch statement
  984. * below under cases SET_OF and SEQUENCE_OF; it will be cleaner.
  985. */
  986. PORT_Assert(state->underlying_kind == SEC_ASN1_SET_OF || state->underlying_kind == SEC_ASN1_SEQUENCE_OF || state->underlying_kind == (SEC_ASN1_SEQUENCE_OF | SEC_ASN1_DYNAMIC) || state->underlying_kind == (SEC_ASN1_SEQUENCE_OF | SEC_ASN1_DYNAMIC));
  987. if (state->contents_length != 0 || state->indefinite) {
  988. const SEC_ASN1Template *subt;
  989. state->place = duringGroup;
  990. subt = SEC_ASN1GetSubtemplate(state->theTemplate, state->dest,
  991. PR_FALSE);
  992. state = sec_asn1d_push_state(state->top, subt, NULL, PR_TRUE);
  993. if (state != NULL) {
  994. if (!state->top->filter_only)
  995. state->allocate = PR_TRUE; /* XXX propogate this? */
  996. /*
  997. * Do the "before" field notification for next in group.
  998. */
  999. sec_asn1d_notify_before(state->top, state->dest, state->depth);
  1000. (void)sec_asn1d_init_state_based_on_template(state);
  1001. }
  1002. } else {
  1003. /*
  1004. * A group of zero; we are done.
  1005. * Set state to afterGroup and let that code plant the NULL.
  1006. */
  1007. state->place = afterGroup;
  1008. }
  1009. return;
  1010. }
  1011. switch (state->underlying_kind) {
  1012. case SEC_ASN1_SEQUENCE:
  1013. /*
  1014. * We need to push a child to handle the individual fields.
  1015. */
  1016. state->place = duringSequence;
  1017. state = sec_asn1d_push_state(state->top, state->theTemplate + 1,
  1018. state->dest, PR_TRUE);
  1019. if (state != NULL) {
  1020. /*
  1021. * Do the "before" field notification.
  1022. */
  1023. sec_asn1d_notify_before(state->top, state->dest, state->depth);
  1024. (void)sec_asn1d_init_state_based_on_template(state);
  1025. }
  1026. break;
  1027. case SEC_ASN1_SET: /* XXX SET is not really implemented */
  1028. /*
  1029. * XXX A plain SET requires special handling; scanning of a
  1030. * template to see where a field should go (because by definition,
  1031. * they are not in any particular order, and you have to look at
  1032. * each tag to disambiguate what the field is). We may never
  1033. * implement this because in practice, it seems to be unused.
  1034. */
  1035. PORT_Assert(0);
  1036. PORT_SetError(SEC_ERROR_BAD_DER); /* XXX */
  1037. state->top->status = decodeError;
  1038. break;
  1039. case SEC_ASN1_NULL:
  1040. /*
  1041. * The NULL type, by definition, is "nothing", content length of zero.
  1042. * An indefinite-length encoding is not alloweed.
  1043. */
  1044. if (state->contents_length || state->indefinite) {
  1045. PORT_SetError(SEC_ERROR_BAD_DER);
  1046. state->top->status = decodeError;
  1047. break;
  1048. }
  1049. if (state->dest != NULL) {
  1050. item = (SECItem *)(state->dest);
  1051. item->data = NULL;
  1052. item->len = 0;
  1053. }
  1054. state->place = afterEndOfContents;
  1055. break;
  1056. case SEC_ASN1_BMP_STRING:
  1057. /* Error if length is not divisable by 2 */
  1058. if (state->contents_length % 2) {
  1059. PORT_SetError(SEC_ERROR_BAD_DER);
  1060. state->top->status = decodeError;
  1061. break;
  1062. }
  1063. /* otherwise, handle as other string types */
  1064. goto regular_string_type;
  1065. case SEC_ASN1_UNIVERSAL_STRING:
  1066. /* Error if length is not divisable by 4 */
  1067. if (state->contents_length % 4) {
  1068. PORT_SetError(SEC_ERROR_BAD_DER);
  1069. state->top->status = decodeError;
  1070. break;
  1071. }
  1072. /* otherwise, handle as other string types */
  1073. goto regular_string_type;
  1074. case SEC_ASN1_SKIP:
  1075. case SEC_ASN1_ANY:
  1076. case SEC_ASN1_ANY_CONTENTS:
  1077. /*
  1078. * These are not (necessarily) strings, but they need nearly
  1079. * identical handling (especially when we need to deal with
  1080. * constructed sub-pieces), so we pretend they are.
  1081. */
  1082. /* fallthru */
  1083. regular_string_type:
  1084. case SEC_ASN1_BIT_STRING:
  1085. case SEC_ASN1_IA5_STRING:
  1086. case SEC_ASN1_OCTET_STRING:
  1087. case SEC_ASN1_PRINTABLE_STRING:
  1088. case SEC_ASN1_T61_STRING:
  1089. case SEC_ASN1_UTC_TIME:
  1090. case SEC_ASN1_UTF8_STRING:
  1091. case SEC_ASN1_VISIBLE_STRING:
  1092. /*
  1093. * We are allocating for a primitive or a constructed string.
  1094. * If it is a constructed string, it may also be indefinite-length.
  1095. * If it is primitive, the length can (legally) be zero.
  1096. * Our first order of business is to allocate the memory for
  1097. * the string, if we can (if we know the length).
  1098. */
  1099. item = (SECItem *)(state->dest);
  1100. /*
  1101. * If the item is a definite-length constructed string, then
  1102. * the contents_length is actually larger than what we need
  1103. * (because it also counts each intermediate header which we
  1104. * will be throwing away as we go), but it is a perfectly good
  1105. * upper bound that we just allocate anyway, and then concat
  1106. * as we go; we end up wasting a few extra bytes but save a
  1107. * whole other copy.
  1108. */
  1109. alloc_len = state->contents_length;
  1110. poolp = NULL; /* quiet compiler warnings about unused... */
  1111. if (item == NULL || state->top->filter_only) {
  1112. if (item != NULL) {
  1113. item->data = NULL;
  1114. item->len = 0;
  1115. }
  1116. alloc_len = 0;
  1117. } else if (state->substring) {
  1118. /*
  1119. * If we are a substring of a constructed string, then we may
  1120. * not have to allocate anything (because our parent, the
  1121. * actual constructed string, did it for us). If we are a
  1122. * substring and we *do* have to allocate, that means our
  1123. * parent is an indefinite-length, so we allocate from our pool;
  1124. * later our parent will copy our string into the aggregated
  1125. * whole and free our pool allocation.
  1126. */
  1127. if (item->data == NULL) {
  1128. PORT_Assert(item->len == 0);
  1129. poolp = state->top->our_pool;
  1130. } else {
  1131. alloc_len = 0;
  1132. }
  1133. } else {
  1134. item->len = 0;
  1135. item->data = NULL;
  1136. poolp = state->top->their_pool;
  1137. }
  1138. if (alloc_len || ((!state->indefinite) && (state->subitems_head != NULL))) {
  1139. struct subitem *subitem;
  1140. int len;
  1141. PORT_Assert(item);
  1142. if (!item) {
  1143. PORT_SetError(SEC_ERROR_BAD_DER);
  1144. state->top->status = decodeError;
  1145. return;
  1146. }
  1147. PORT_Assert(item->len == 0 && item->data == NULL);
  1148. /*
  1149. * Check for and handle an ANY which has stashed aside the
  1150. * header (identifier and length) bytes for us to include
  1151. * in the saved contents.
  1152. */
  1153. if (state->subitems_head != NULL) {
  1154. PORT_Assert(state->underlying_kind == SEC_ASN1_ANY);
  1155. for (subitem = state->subitems_head;
  1156. subitem != NULL; subitem = subitem->next)
  1157. alloc_len += subitem->len;
  1158. }
  1159. if (state->top->max_element_size > 0 &&
  1160. alloc_len > state->top->max_element_size) {
  1161. PORT_SetError(SEC_ERROR_OUTPUT_LEN);
  1162. state->top->status = decodeError;
  1163. return;
  1164. }
  1165. item->data = (unsigned char *)sec_asn1d_zalloc(poolp, alloc_len);
  1166. if (item->data == NULL) {
  1167. state->top->status = decodeError;
  1168. break;
  1169. }
  1170. len = 0;
  1171. for (subitem = state->subitems_head;
  1172. subitem != NULL; subitem = subitem->next) {
  1173. PORT_Memcpy(item->data + len, subitem->data, subitem->len);
  1174. len += subitem->len;
  1175. }
  1176. item->len = len;
  1177. /*
  1178. * Because we use arenas and have a mark set, we later free
  1179. * everything we have allocated, so this does *not* present
  1180. * a memory leak (it is just temporarily left dangling).
  1181. */
  1182. state->subitems_head = state->subitems_tail = NULL;
  1183. }
  1184. if (state->contents_length == 0 && (!state->indefinite)) {
  1185. /*
  1186. * A zero-length simple or constructed string; we are done.
  1187. */
  1188. state->place = afterEndOfContents;
  1189. } else if (state->found_tag_modifiers & SEC_ASN1_CONSTRUCTED) {
  1190. const SEC_ASN1Template *sub;
  1191. switch (state->underlying_kind) {
  1192. case SEC_ASN1_ANY:
  1193. case SEC_ASN1_ANY_CONTENTS:
  1194. sub = SEC_AnyTemplate;
  1195. break;
  1196. case SEC_ASN1_BIT_STRING:
  1197. sub = SEC_BitStringTemplate;
  1198. break;
  1199. case SEC_ASN1_BMP_STRING:
  1200. sub = SEC_BMPStringTemplate;
  1201. break;
  1202. case SEC_ASN1_GENERALIZED_TIME:
  1203. sub = SEC_GeneralizedTimeTemplate;
  1204. break;
  1205. case SEC_ASN1_IA5_STRING:
  1206. sub = SEC_IA5StringTemplate;
  1207. break;
  1208. case SEC_ASN1_OCTET_STRING:
  1209. sub = SEC_OctetStringTemplate;
  1210. break;
  1211. case SEC_ASN1_PRINTABLE_STRING:
  1212. sub = SEC_PrintableStringTemplate;
  1213. break;
  1214. case SEC_ASN1_T61_STRING:
  1215. sub = SEC_T61StringTemplate;
  1216. break;
  1217. case SEC_ASN1_UNIVERSAL_STRING:
  1218. sub = SEC_UniversalStringTemplate;
  1219. break;
  1220. case SEC_ASN1_UTC_TIME:
  1221. sub = SEC_UTCTimeTemplate;
  1222. break;
  1223. case SEC_ASN1_UTF8_STRING:
  1224. sub = SEC_UTF8StringTemplate;
  1225. break;
  1226. case SEC_ASN1_VISIBLE_STRING:
  1227. sub = SEC_VisibleStringTemplate;
  1228. break;
  1229. case SEC_ASN1_SKIP:
  1230. sub = SEC_SkipTemplate;
  1231. break;
  1232. default: /* redundant given outer switch cases, but */
  1233. PORT_Assert(0); /* the compiler does not seem to know that, */
  1234. sub = NULL; /* so just do enough to quiet it. */
  1235. break;
  1236. }
  1237. state->place = duringConstructedString;
  1238. state = sec_asn1d_push_state(state->top, sub, item, PR_TRUE);
  1239. if (state != NULL) {
  1240. state->substring = PR_TRUE; /* XXX propogate? */
  1241. (void)sec_asn1d_init_state_based_on_template(state);
  1242. }
  1243. } else if (state->indefinite) {
  1244. /*
  1245. * An indefinite-length string *must* be constructed!
  1246. */
  1247. PORT_SetError(SEC_ERROR_BAD_DER);
  1248. state->top->status = decodeError;
  1249. } else {
  1250. /*
  1251. * A non-zero-length simple string.
  1252. */
  1253. if (state->underlying_kind == SEC_ASN1_BIT_STRING)
  1254. state->place = beforeBitString;
  1255. else
  1256. state->place = duringLeaf;
  1257. }
  1258. break;
  1259. default:
  1260. /*
  1261. * We are allocating for a simple leaf item.
  1262. */
  1263. if (state->contents_length) {
  1264. if (state->dest != NULL) {
  1265. item = (SECItem *)(state->dest);
  1266. item->len = 0;
  1267. if (state->top->max_element_size > 0 &&
  1268. state->contents_length > state->top->max_element_size) {
  1269. PORT_SetError(SEC_ERROR_OUTPUT_LEN);
  1270. state->top->status = decodeError;
  1271. return;
  1272. }
  1273. if (state->top->filter_only) {
  1274. item->data = NULL;
  1275. } else {
  1276. item->data = (unsigned char *)
  1277. sec_asn1d_zalloc(state->top->their_pool,
  1278. state->contents_length);
  1279. if (item->data == NULL) {
  1280. state->top->status = decodeError;
  1281. return;
  1282. }
  1283. }
  1284. }
  1285. state->place = duringLeaf;
  1286. } else {
  1287. /*
  1288. * An indefinite-length or zero-length item is not allowed.
  1289. * (All legal cases of such were handled above.)
  1290. */
  1291. PORT_SetError(SEC_ERROR_BAD_DER);
  1292. state->top->status = decodeError;
  1293. }
  1294. }
  1295. }
  1296. static void
  1297. sec_asn1d_free_child(sec_asn1d_state *state, PRBool error)
  1298. {
  1299. if (state->child != NULL) {
  1300. PORT_Assert(error || state->child->consumed == 0);
  1301. PORT_Assert(state->our_mark != NULL);
  1302. PORT_ArenaZRelease(state->top->our_pool, state->our_mark);
  1303. if (error && state->top->their_pool == NULL) {
  1304. /*
  1305. * XXX We need to free anything allocated.
  1306. * At this point, we failed in the middle of decoding. But we
  1307. * can't free the data we previously allocated with PR_Malloc
  1308. * unless we keep track of every pointer. So instead we have a
  1309. * memory leak when decoding fails half-way, unless an arena is
  1310. * used. See bug 95311 .
  1311. */
  1312. }
  1313. state->child = NULL;
  1314. state->our_mark = NULL;
  1315. } else {
  1316. /*
  1317. * It is important that we do not leave a mark unreleased/unmarked.
  1318. * But I do not think we should ever have one set in this case, only
  1319. * if we had a child (handled above). So check for that. If this
  1320. * assertion should ever get hit, then we probably need to add code
  1321. * here to release back to our_mark (and then set our_mark to NULL).
  1322. */
  1323. PORT_Assert(state->our_mark == NULL);
  1324. }
  1325. state->place = beforeEndOfContents;
  1326. }
  1327. /* We have just saved an entire encoded ASN.1 object (type) for a SAVE
  1328. ** template, and now in the next template, we are going to decode that
  1329. ** saved data by calling SEC_ASN1DecoderUpdate recursively.
  1330. ** If that recursive call fails with needBytes, it is a fatal error,
  1331. ** because the encoded object should have been complete.
  1332. ** If that recursive call fails with decodeError, it will have already
  1333. ** cleaned up the state stack, so we must bail out quickly.
  1334. **
  1335. ** These checks of the status returned by the recursive call are now
  1336. ** done in the caller of this function, immediately after it returns.
  1337. */
  1338. static void
  1339. sec_asn1d_reuse_encoding(sec_asn1d_state *state)
  1340. {
  1341. sec_asn1d_state *child;
  1342. unsigned long consumed;
  1343. SECItem *item;
  1344. void *dest;
  1345. child = state->child;
  1346. PORT_Assert(child != NULL);
  1347. consumed = child->consumed;
  1348. child->consumed = 0;
  1349. item = (SECItem *)(state->dest);
  1350. PORT_Assert(item != NULL);
  1351. PORT_Assert(item->len == consumed);
  1352. /*
  1353. * Free any grandchild.
  1354. */
  1355. sec_asn1d_free_child(child, PR_FALSE);
  1356. /*
  1357. * Notify after the SAVE field.
  1358. */
  1359. sec_asn1d_notify_after(state->top, state->dest, state->depth);
  1360. /*
  1361. * Adjust to get new dest and move forward.
  1362. */
  1363. dest = (char *)state->dest - state->theTemplate->offset;
  1364. state->theTemplate++;
  1365. child->dest = (char *)dest + state->theTemplate->offset;
  1366. child->theTemplate = state->theTemplate;
  1367. /*
  1368. * Notify before the "real" field.
  1369. */
  1370. PORT_Assert(state->depth == child->depth);
  1371. sec_asn1d_notify_before(state->top, child->dest, child->depth);
  1372. /*
  1373. * This will tell DecoderUpdate to return when it is done.
  1374. */
  1375. state->place = afterSaveEncoding;
  1376. /*
  1377. * We already have a child; "push" it by making it current.
  1378. */
  1379. state->top->current = child;
  1380. /*
  1381. * And initialize it so it is ready to parse.
  1382. */
  1383. (void)sec_asn1d_init_state_based_on_template(child);
  1384. /*
  1385. * Now parse that out of our data.
  1386. */
  1387. if (SEC_ASN1DecoderUpdate(state->top,
  1388. (char *)item->data, item->len) != SECSuccess)
  1389. return;
  1390. if (state->top->status == needBytes) {
  1391. return;
  1392. }
  1393. PORT_Assert(state->top->current == state);
  1394. PORT_Assert(state->child == child);
  1395. /*
  1396. * That should have consumed what we consumed before.
  1397. */
  1398. PORT_Assert(consumed == child->consumed);
  1399. child->consumed = 0;
  1400. /*
  1401. * Done.
  1402. */
  1403. state->consumed += consumed;
  1404. child->place = notInUse;
  1405. state->place = afterEndOfContents;
  1406. }
  1407. static unsigned long
  1408. sec_asn1d_parse_leaf(sec_asn1d_state *state,
  1409. const char *buf, unsigned long len)
  1410. {
  1411. SECItem *item;
  1412. unsigned long bufLen;
  1413. if (len == 0) {
  1414. state->top->status = needBytes;
  1415. return 0;
  1416. }
  1417. if (state->pending < len)
  1418. len = state->pending;
  1419. bufLen = len;
  1420. item = (SECItem *)(state->dest);
  1421. if (item != NULL && item->data != NULL) {
  1422. unsigned long offset;
  1423. /* Strip leading zeroes when target is unsigned integer */
  1424. if (state->underlying_kind == SEC_ASN1_INTEGER && /* INTEGER */
  1425. item->len == 0 && /* MSB */
  1426. item->type == siUnsignedInteger) /* unsigned */
  1427. {
  1428. while (len > 1 && buf[0] == 0) { /* leading 0 */
  1429. buf++;
  1430. len--;
  1431. }
  1432. }
  1433. offset = item->len;
  1434. if (state->underlying_kind == SEC_ASN1_BIT_STRING) {
  1435. // The previous bit string must have no unused bits.
  1436. if (item->len & 0x7) {
  1437. PORT_SetError(SEC_ERROR_BAD_DER);
  1438. state->top->status = decodeError;
  1439. return 0;
  1440. }
  1441. // If this is a bit string, the length is bits, not bytes.
  1442. offset = item->len >> 3;
  1443. }
  1444. if (state->underlying_kind == SEC_ASN1_BIT_STRING) {
  1445. unsigned long len_in_bits;
  1446. // Protect against overflow during the bytes-to-bits conversion.
  1447. if (len >= (ULONG_MAX >> 3) + 1) {
  1448. PORT_SetError(SEC_ERROR_BAD_DER);
  1449. state->top->status = decodeError;
  1450. return 0;
  1451. }
  1452. len_in_bits = (len << 3) - state->bit_string_unused_bits;
  1453. // Protect against overflow when computing the total length in bits.
  1454. if (UINT_MAX - item->len < len_in_bits) {
  1455. PORT_SetError(SEC_ERROR_BAD_DER);
  1456. state->top->status = decodeError;
  1457. return 0;
  1458. }
  1459. item->len += len_in_bits;
  1460. } else {
  1461. if (UINT_MAX - item->len < len) {
  1462. PORT_SetError(SEC_ERROR_BAD_DER);
  1463. state->top->status = decodeError;
  1464. return 0;
  1465. }
  1466. item->len += len;
  1467. }
  1468. PORT_Memcpy(item->data + offset, buf, len);
  1469. }
  1470. state->pending -= bufLen;
  1471. if (state->pending == 0)
  1472. state->place = beforeEndOfContents;
  1473. return bufLen;
  1474. }
  1475. static unsigned long
  1476. sec_asn1d_parse_bit_string(sec_asn1d_state *state,
  1477. const char *buf, unsigned long len)
  1478. {
  1479. unsigned char byte;
  1480. /*PORT_Assert (state->pending > 0); */
  1481. PORT_Assert(state->place == beforeBitString);
  1482. if (state->pending == 0) {
  1483. if (state->dest != NULL) {
  1484. SECItem *item = (SECItem *)(state->dest);
  1485. item->data = NULL;
  1486. item->len = 0;
  1487. state->place = beforeEndOfContents;
  1488. return 0;
  1489. }
  1490. }
  1491. if (len == 0) {
  1492. state->top->status = needBytes;
  1493. return 0;
  1494. }
  1495. byte = (unsigned char)*buf;
  1496. if (byte > 7) {
  1497. PORT_SetError(SEC_ERROR_BAD_DER);
  1498. state->top->status = decodeError;
  1499. return 0;
  1500. }
  1501. state->bit_string_unused_bits = byte;
  1502. state->place = duringBitString;
  1503. state->pending -= 1;
  1504. return 1;
  1505. }
  1506. static unsigned long
  1507. sec_asn1d_parse_more_bit_string(sec_asn1d_state *state,
  1508. const char *buf, unsigned long len)
  1509. {
  1510. PORT_Assert(state->place == duringBitString);
  1511. if (state->pending == 0) {
  1512. /* An empty bit string with some unused bits is invalid. */
  1513. if (state->bit_string_unused_bits) {
  1514. PORT_SetError(SEC_ERROR_BAD_DER);
  1515. state->top->status = decodeError;
  1516. } else {
  1517. /* An empty bit string with no unused bits is OK. */
  1518. state->place = beforeEndOfContents;
  1519. }
  1520. return 0;
  1521. }
  1522. len = sec_asn1d_parse_leaf(state, buf, len);
  1523. return len;
  1524. }
  1525. /*
  1526. * XXX All callers should be looking at return value to detect
  1527. * out-of-memory errors (and stop!).
  1528. */
  1529. static struct subitem *
  1530. sec_asn1d_add_to_subitems(sec_asn1d_state *state,
  1531. const void *data, unsigned long len,
  1532. PRBool copy_data)
  1533. {
  1534. struct subitem *thing;
  1535. thing = (struct subitem *)sec_asn1d_zalloc(state->top->our_pool,
  1536. sizeof(struct subitem));
  1537. if (thing == NULL) {
  1538. state->top->status = decodeError;
  1539. return NULL;
  1540. }
  1541. if (copy_data) {
  1542. void *copy;
  1543. copy = sec_asn1d_alloc(state->top->our_pool, len);
  1544. if (copy == NULL) {
  1545. state->top->status = decodeError;
  1546. if (!state->top->our_pool)
  1547. PORT_Free(thing);
  1548. return NULL;
  1549. }
  1550. PORT_Memcpy(copy, data, len);
  1551. thing->data = copy;
  1552. } else {
  1553. thing->data = data;
  1554. }
  1555. thing->len = len;
  1556. thing->next = NULL;
  1557. if (state->subitems_head == NULL) {
  1558. PORT_Assert(state->subitems_tail == NULL);
  1559. state->subitems_head = state->subitems_tail = thing;
  1560. } else {
  1561. state->subitems_tail->next = thing;
  1562. state->subitems_tail = thing;
  1563. }
  1564. return thing;
  1565. }
  1566. static void
  1567. sec_asn1d_record_any_header(sec_asn1d_state *state,
  1568. const char *buf,
  1569. unsigned long len)
  1570. {
  1571. SECItem *item;
  1572. item = (SECItem *)(state->dest);
  1573. if (item != NULL && item->data != NULL) {
  1574. PORT_Assert(state->substring);
  1575. PORT_Memcpy(item->data + item->len, buf, len);
  1576. item->len += len;
  1577. } else {
  1578. sec_asn1d_add_to_subitems(state, buf, len, PR_TRUE);
  1579. }
  1580. }
  1581. /*
  1582. * We are moving along through the substrings of a constructed string,
  1583. * and have just finished parsing one -- we need to save our child data
  1584. * (if the child was not already writing directly into the destination)
  1585. * and then move forward by one.
  1586. *
  1587. * We also have to detect when we are done:
  1588. * - a definite-length encoding stops when our pending value hits 0
  1589. * - an indefinite-length encoding stops when our child is empty
  1590. * (which means it was the end-of-contents octets)
  1591. */
  1592. static void
  1593. sec_asn1d_next_substring(sec_asn1d_state *state)
  1594. {
  1595. sec_asn1d_state *child;
  1596. SECItem *item;
  1597. unsigned long child_consumed;
  1598. PRBool done;
  1599. PORT_Assert(state->place == duringConstructedString);
  1600. PORT_Assert(state->child != NULL);
  1601. child = state->child;
  1602. child_consumed = child->consumed;
  1603. child->consumed = 0;
  1604. state->consumed += child_consumed;
  1605. done = PR_FALSE;
  1606. if (state->pending) {
  1607. PORT_Assert(!state->indefinite);
  1608. if (child_consumed > state->pending) {
  1609. PORT_SetError(SEC_ERROR_BAD_DER);
  1610. state->top->status = decodeError;
  1611. return;
  1612. }
  1613. state->pending -= child_consumed;
  1614. if (state->pending == 0)
  1615. done = PR_TRUE;
  1616. } else {
  1617. PRBool preallocatedString;
  1618. sec_asn1d_state *temp_state;
  1619. PORT_Assert(state->indefinite);
  1620. item = (SECItem *)(child->dest);
  1621. /**
  1622. * At this point, there's three states at play:
  1623. * child: The element that was just parsed
  1624. * state: The currently processed element
  1625. * 'parent' (aka state->parent): The enclosing construct
  1626. * of state, or NULL if this is the top-most element.
  1627. *
  1628. * This state handles both substrings of a constructed string AND
  1629. * child elements of items whose template type was that of
  1630. * SEC_ASN1_ANY, SEC_ASN1_SAVE, SEC_ASN1_ANY_CONTENTS, SEC_ASN1_SKIP
  1631. * template, as described in sec_asn1d_prepare_for_contents. For
  1632. * brevity, these will be referred to as 'string' and 'any' types.
  1633. *
  1634. * This leads to the following possibilities:
  1635. * 1: This element is an indefinite length string, part of a
  1636. * definite length string.
  1637. * 2: This element is an indefinite length string, part of an
  1638. * indefinite length string.
  1639. * 3: This element is an indefinite length any, part of a
  1640. * definite length any.
  1641. * 4: This element is an indefinite length any, part of an
  1642. * indefinite length any.
  1643. * 5: This element is an indefinite length any and does not
  1644. * meet any of the above criteria. Note that this would include
  1645. * an indefinite length string type matching an indefinite
  1646. * length any template.
  1647. *
  1648. * In Cases #1 and #3, the definite length 'parent' element will
  1649. * have allocated state->dest based on the parent elements definite
  1650. * size. During the processing of 'child', sec_asn1d_parse_leaf will
  1651. * have copied the (string, any) data directly into the offset of
  1652. * dest, as appropriate, so there's no need for this class to still
  1653. * store the child - it's already been processed.
  1654. *
  1655. * In Cases #2 and #4, dest will be set to the parent element's dest,
  1656. * but dest->data will not have been allocated yet, due to the
  1657. * indefinite length encoding. In this situation, it's necessary to
  1658. * hold onto child (and all other children) until the EOC, at which
  1659. * point, it becomes possible to compute 'state's overall length. Once
  1660. * 'state' has a computed length, this can then be fed to 'parent' (via
  1661. * this state), and then 'parent' can similarly compute the length of
  1662. * all of its children up to the EOC, which will ultimately transit to
  1663. * sec_asn1d_concat_substrings, determine the overall size needed,
  1664. * allocate, and copy the contents (of all of parent's children, which
  1665. * would include 'state', just as 'state' will have copied all of its
  1666. * children via sec_asn1d_concat_substrings)
  1667. *
  1668. * The final case, Case #5, will manifest in that item->data and
  1669. * item->len will be NULL/0, respectively, since this element was
  1670. * indefinite-length encoded. In that case, both the tag and length will
  1671. * already exist in state's subitems, via sec_asn1d_record_any_header,
  1672. * and so the contents (aka 'child') should be added to that list of
  1673. * items to concatenate in sec_asn1d_concat_substrings once the EOC
  1674. * is encountered.
  1675. *
  1676. * To distinguish #2/#4 from #1/#3, it's sufficient to walk the ancestor
  1677. * tree. If the current type is a string type, then the enclosing
  1678. * construct will be that same type (#1/#2). If the current type is an
  1679. * any type, then the enclosing construct is either an any type (#3/#4)
  1680. * or some other type (#5). Since this is BER, this nesting relationship
  1681. * between 'state' and 'parent' may go through several levels of
  1682. * constructed encoding, so continue walking the ancestor chain until a
  1683. * clear determination can be made.
  1684. *
  1685. * The variable preallocatedString is used to indicate Case #1/#3,
  1686. * indicating an in-place copy has already occurred, and Cases #2, #4,
  1687. * and #5 all have the same behaviour of adding a new substring.
  1688. */
  1689. preallocatedString = PR_FALSE;
  1690. temp_state = state;
  1691. while (temp_state && item == temp_state->dest && temp_state->indefinite) {
  1692. sec_asn1d_state *parent = sec_asn1d_get_enclosing_construct(temp_state);
  1693. if (!parent || parent->underlying_kind != temp_state->underlying_kind) {
  1694. /* Case #5 - Either this is a top-level construct or it is part
  1695. * of some other element (e.g. a SEQUENCE), in which case, a
  1696. * new item should be allocated. */
  1697. break;
  1698. }
  1699. if (!parent->indefinite) {
  1700. /* Cases #1 / #3 - A definite length ancestor exists, for which
  1701. * this is a substring that has already copied into dest. */
  1702. preallocatedString = PR_TRUE;
  1703. break;
  1704. }
  1705. if (!parent->substring) {
  1706. /* Cases #2 / #4 - If the parent is not a substring, but is
  1707. * indefinite, then there's nothing further up that may have
  1708. * preallocated dest, thus child will not have already
  1709. * been copied in place, therefore it's necessary to save child
  1710. * as a subitem. */
  1711. break;
  1712. }
  1713. temp_state = parent;
  1714. }
  1715. if (item != NULL && item->data != NULL && !preallocatedString) {
  1716. /*
  1717. * Save the string away for later concatenation.
  1718. */
  1719. PORT_Assert(item->data != NULL);
  1720. sec_asn1d_add_to_subitems(state, item->data, item->len, PR_FALSE);
  1721. /*
  1722. * Clear the child item for the next round.
  1723. */
  1724. item->data = NULL;
  1725. item->len = 0;
  1726. }
  1727. /*
  1728. * If our child was just our end-of-contents octets, we are done.
  1729. */
  1730. if (child->endofcontents)
  1731. done = PR_TRUE;
  1732. }
  1733. /*
  1734. * Stop or do the next one.
  1735. */
  1736. if (done) {
  1737. child->place = notInUse;
  1738. state->place = afterConstructedString;
  1739. } else {
  1740. sec_asn1d_scrub_state(child);
  1741. state->top->current = child;
  1742. }
  1743. }
  1744. /*
  1745. * We are doing a SET OF or SEQUENCE OF, and have just finished an item.
  1746. */
  1747. static void
  1748. sec_asn1d_next_in_group(sec_asn1d_state *state)
  1749. {
  1750. sec_asn1d_state *child;
  1751. unsigned long child_consumed;
  1752. PORT_Assert(state->place == duringGroup);
  1753. PORT_Assert(state->child != NULL);
  1754. child = state->child;
  1755. child_consumed = child->consumed;
  1756. child->consumed = 0;
  1757. state->consumed += child_consumed;
  1758. /*
  1759. * If our child was just our end-of-contents octets, we are done.
  1760. */
  1761. if (child->endofcontents) {
  1762. /* XXX I removed the PORT_Assert (child->dest == NULL) because there
  1763. * was a bug in that a template that was a sequence of which also had
  1764. * a child of a sequence of, in an indefinite group was not working
  1765. * properly. This fix seems to work, (added the if statement below),
  1766. * and nothing appears broken, but I am putting this note here just
  1767. * in case. */
  1768. /*
  1769. * XXX No matter how many times I read that comment,
  1770. * I cannot figure out what case he was fixing. I believe what he
  1771. * did was deliberate, so I am loathe to touch it. I need to
  1772. * understand how it could ever be that child->dest != NULL but
  1773. * child->endofcontents is true, and why it is important to check
  1774. * that state->subitems_head is NULL. This really needs to be
  1775. * figured out, as I am not sure if the following code should be
  1776. * compensating for "offset", as is done a little farther below
  1777. * in the more normal case.
  1778. */
  1779. PORT_Assert(state->indefinite);
  1780. PORT_Assert(state->pending == 0);
  1781. if (child->dest && !state->subitems_head) {
  1782. sec_asn1d_add_to_subitems(state, child->dest, 0, PR_FALSE);
  1783. child->dest = NULL;
  1784. }
  1785. child->place = notInUse;
  1786. state->place = afterGroup;
  1787. return;
  1788. }
  1789. /*
  1790. * Do the "after" field notification for next in group.
  1791. */
  1792. sec_asn1d_notify_after(state->top, child->dest, child->depth);
  1793. /*
  1794. * Save it away (unless we are not storing).
  1795. */
  1796. if (child->dest != NULL) {
  1797. void *dest;
  1798. dest = child->dest;
  1799. dest = (char *)dest - child->theTemplate->offset;
  1800. sec_asn1d_add_to_subitems(state, dest, 0, PR_FALSE);
  1801. child->dest = NULL;
  1802. }
  1803. /*
  1804. * Account for those bytes; see if we are done.
  1805. */
  1806. if (state->pending) {
  1807. PORT_Assert(!state->indefinite);
  1808. if (child_consumed > state->pending) {
  1809. PORT_SetError(SEC_ERROR_BAD_DER);
  1810. state->top->status = decodeError;
  1811. return;
  1812. }
  1813. state->pending -= child_consumed;
  1814. if (state->pending == 0) {
  1815. child->place = notInUse;
  1816. state->place = afterGroup;
  1817. return;
  1818. }
  1819. }
  1820. /*
  1821. * Do the "before" field notification for next item in group.
  1822. */
  1823. sec_asn1d_notify_before(state->top, child->dest, child->depth);
  1824. /*
  1825. * Now we do the next one.
  1826. */
  1827. sec_asn1d_scrub_state(child);
  1828. /* Initialize child state from the template */
  1829. sec_asn1d_init_state_based_on_template(child);
  1830. state->top->current = child;
  1831. }
  1832. /*
  1833. * We are moving along through a sequence; move forward by one,
  1834. * (detecting end-of-sequence when it happens).
  1835. * XXX The handling of "missing" is ugly. Fix it.
  1836. */
  1837. static void
  1838. sec_asn1d_next_in_sequence(sec_asn1d_state *state)
  1839. {
  1840. sec_asn1d_state *child;
  1841. unsigned long child_consumed;
  1842. PRBool child_missing;
  1843. PORT_Assert(state->place == duringSequence);
  1844. PORT_Assert(state->child != NULL);
  1845. child = state->child;
  1846. /*
  1847. * Do the "after" field notification.
  1848. */
  1849. sec_asn1d_notify_after(state->top, child->dest, child->depth);
  1850. child_missing = (PRBool)child->missing;
  1851. child_consumed = child->consumed;
  1852. child->consumed = 0;
  1853. /*
  1854. * Take care of accounting.
  1855. */
  1856. if (child_missing) {
  1857. PORT_Assert(child->optional);
  1858. } else {
  1859. state->consumed += child_consumed;
  1860. /*
  1861. * Free any grandchild.
  1862. */
  1863. sec_asn1d_free_child(child, PR_FALSE);
  1864. if (state->pending) {
  1865. PORT_Assert(!state->indefinite);
  1866. if (child_consumed > state->pending) {
  1867. PORT_SetError(SEC_ERROR_BAD_DER);
  1868. state->top->status = decodeError;
  1869. return;
  1870. }
  1871. state->pending -= child_consumed;
  1872. if (state->pending == 0) {
  1873. child->theTemplate++;
  1874. while (child->theTemplate->kind != 0) {
  1875. if ((child->theTemplate->kind & SEC_ASN1_OPTIONAL) == 0) {
  1876. PORT_SetError(SEC_ERROR_BAD_DER);
  1877. state->top->status = decodeError;
  1878. return;
  1879. }
  1880. child->theTemplate++;
  1881. }
  1882. child->place = notInUse;
  1883. state->place = afterEndOfContents;
  1884. return;
  1885. }
  1886. }
  1887. }
  1888. /*
  1889. * Move forward.
  1890. */
  1891. child->theTemplate++;
  1892. if (child->theTemplate->kind == 0) {
  1893. /*
  1894. * We are done with this sequence.
  1895. */
  1896. child->place = notInUse;
  1897. if (state->pending) {
  1898. PORT_SetError(SEC_ERROR_BAD_DER);
  1899. state->top->status = decodeError;
  1900. } else if (child_missing) {
  1901. /*
  1902. * We got to the end, but have a child that started parsing
  1903. * and ended up "missing". The only legitimate reason for
  1904. * this is that we had one or more optional fields at the
  1905. * end of our sequence, and we were encoded indefinite-length,
  1906. * so when we went looking for those optional fields we
  1907. * found our end-of-contents octets instead.
  1908. * (Yes, this is ugly; dunno a better way to handle it.)
  1909. * So, first confirm the situation, and then mark that we
  1910. * are done.
  1911. */
  1912. if (state->indefinite && child->endofcontents) {
  1913. PORT_Assert(child_consumed == 2);
  1914. if (child_consumed != 2) {
  1915. PORT_SetError(SEC_ERROR_BAD_DER);
  1916. state->top->status = decodeError;
  1917. } else {
  1918. state->consumed += child_consumed;
  1919. state->place = afterEndOfContents;
  1920. }
  1921. } else {
  1922. PORT_SetError(SEC_ERROR_BAD_DER);
  1923. state->top->status = decodeError;
  1924. }
  1925. } else {
  1926. /*
  1927. * We have to finish out, maybe reading end-of-contents octets;
  1928. * let the normal logic do the right thing.
  1929. */
  1930. state->place = beforeEndOfContents;
  1931. }
  1932. } else {
  1933. unsigned char child_found_tag_modifiers = 0;
  1934. unsigned long child_found_tag_number = 0;
  1935. /*
  1936. * Reset state and push.
  1937. */
  1938. if (state->dest != NULL)
  1939. child->dest = (char *)state->dest + child->theTemplate->offset;
  1940. /*
  1941. * Do the "before" field notification.
  1942. */
  1943. sec_asn1d_notify_before(state->top, child->dest, child->depth);
  1944. if (child_missing) { /* if previous child was missing, copy the tag data we already have */
  1945. child_found_tag_modifiers = child->found_tag_modifiers;
  1946. child_found_tag_number = child->found_tag_number;
  1947. }
  1948. state->top->current = child;
  1949. child = sec_asn1d_init_state_based_on_template(child);
  1950. if (child_missing && child) {
  1951. child->place = afterIdentifier;
  1952. child->found_tag_modifiers = child_found_tag_modifiers;
  1953. child->found_tag_number = child_found_tag_number;
  1954. child->consumed = child_consumed;
  1955. if (child->underlying_kind == SEC_ASN1_ANY && !child->top->filter_only) {
  1956. /*
  1957. * If the new field is an ANY, and we are storing, then
  1958. * we need to save the tag out. We would have done this
  1959. * already in the normal case, but since we were looking
  1960. * for an optional field, and we did not find it, we only
  1961. * now realize we need to save the tag.
  1962. */
  1963. unsigned char identifier;
  1964. /*
  1965. * Check that we did not end up with a high tag; for that
  1966. * we need to re-encode the tag into multiple bytes in order
  1967. * to store it back to look like what we parsed originally.
  1968. * In practice this does not happen, but for completeness
  1969. * sake it should probably be made to work at some point.
  1970. */
  1971. PORT_Assert(child_found_tag_number < SEC_ASN1_HIGH_TAG_NUMBER);
  1972. identifier = (unsigned char)(child_found_tag_modifiers | child_found_tag_number);
  1973. sec_asn1d_record_any_header(child, (char *)&identifier, 1);
  1974. }
  1975. }
  1976. }
  1977. }
  1978. static void
  1979. sec_asn1d_concat_substrings(sec_asn1d_state *state)
  1980. {
  1981. PORT_Assert(state->place == afterConstructedString);
  1982. if (state->subitems_head != NULL) {
  1983. struct subitem *substring;
  1984. unsigned long alloc_len, item_len;
  1985. unsigned char *where;
  1986. SECItem *item;
  1987. PRBool is_bit_string;
  1988. item_len = 0;
  1989. is_bit_string = (state->underlying_kind == SEC_ASN1_BIT_STRING)
  1990. ? PR_TRUE
  1991. : PR_FALSE;
  1992. substring = state->subitems_head;
  1993. while (substring != NULL) {
  1994. /*
  1995. * All bit-string substrings except the last one should be
  1996. * a clean multiple of 8 bits.
  1997. */
  1998. if (is_bit_string && (substring->next != NULL) && (substring->len & 0x7)) {
  1999. PORT_SetError(SEC_ERROR_BAD_DER);
  2000. state->top->status = decodeError;
  2001. return;
  2002. }
  2003. item_len += substring->len;
  2004. substring = substring->next;
  2005. }
  2006. if (is_bit_string) {
  2007. alloc_len = ((item_len + 7) >> 3);
  2008. } else {
  2009. /*
  2010. * Add 2 for the end-of-contents octets of an indefinite-length
  2011. * ANY that is *not* also an INNER. Because we zero-allocate
  2012. * below, all we need to do is increase the length here.
  2013. */
  2014. if (state->underlying_kind == SEC_ASN1_ANY && state->indefinite)
  2015. item_len += 2;
  2016. alloc_len = item_len;
  2017. }
  2018. if (state->top->max_element_size > 0 &&
  2019. alloc_len > state->top->max_element_size) {
  2020. PORT_SetError(SEC_ERROR_OUTPUT_LEN);
  2021. state->top->status = decodeError;
  2022. return;
  2023. }
  2024. item = (SECItem *)(state->dest);
  2025. PORT_Assert(item != NULL);
  2026. PORT_Assert(item->data == NULL);
  2027. item->data = (unsigned char *)sec_asn1d_zalloc(state->top->their_pool,
  2028. alloc_len);
  2029. if (item->data == NULL) {
  2030. state->top->status = decodeError;
  2031. return;
  2032. }
  2033. item->len = item_len;
  2034. where = item->data;
  2035. substring = state->subitems_head;
  2036. while (substring != NULL) {
  2037. if (is_bit_string)
  2038. item_len = (substring->len + 7) >> 3;
  2039. else
  2040. item_len = substring->len;
  2041. PORT_Memcpy(where, substring->data, item_len);
  2042. where += item_len;
  2043. substring = substring->next;
  2044. }
  2045. /*
  2046. * Because we use arenas and have a mark set, we later free
  2047. * everything we have allocated, so this does *not* present
  2048. * a memory leak (it is just temporarily left dangling).
  2049. */
  2050. state->subitems_head = state->subitems_tail = NULL;
  2051. }
  2052. state->place = afterEndOfContents;
  2053. }
  2054. static void
  2055. sec_asn1d_concat_group(sec_asn1d_state *state)
  2056. {
  2057. const void ***placep;
  2058. PORT_Assert(state->place == afterGroup);
  2059. placep = (const void ***)state->dest;
  2060. PORT_Assert(state->subitems_head == NULL || placep != NULL);
  2061. if (placep != NULL) {
  2062. struct subitem *item;
  2063. const void **group;
  2064. int count;
  2065. count = 0;
  2066. item = state->subitems_head;
  2067. while (item != NULL) {
  2068. PORT_Assert(item->next != NULL || item == state->subitems_tail);
  2069. count++;
  2070. item = item->next;
  2071. }
  2072. group = (const void **)sec_asn1d_zalloc(state->top->their_pool,
  2073. (count + 1) * (sizeof(void *)));
  2074. if (group == NULL) {
  2075. state->top->status = decodeError;
  2076. return;
  2077. }
  2078. *placep = group;
  2079. item = state->subitems_head;
  2080. while (item != NULL) {
  2081. *group++ = item->data;
  2082. item = item->next;
  2083. }
  2084. *group = NULL;
  2085. /*
  2086. * Because we use arenas and have a mark set, we later free
  2087. * everything we have allocated, so this does *not* present
  2088. * a memory leak (it is just temporarily left dangling).
  2089. */
  2090. state->subitems_head = state->subitems_tail = NULL;
  2091. }
  2092. state->place = afterEndOfContents;
  2093. }
  2094. /*
  2095. * For those states that push a child to handle a subtemplate,
  2096. * "absorb" that child (transfer necessary information).
  2097. */
  2098. static void
  2099. sec_asn1d_absorb_child(sec_asn1d_state *state)
  2100. {
  2101. /*
  2102. * There is absolutely supposed to be a child there.
  2103. */
  2104. PORT_Assert(state->child != NULL);
  2105. /*
  2106. * Inherit the missing status of our child, and do the ugly
  2107. * backing-up if necessary.
  2108. */
  2109. state->missing = state->child->missing;
  2110. if (state->missing) {
  2111. state->found_tag_number = state->child->found_tag_number;
  2112. state->found_tag_modifiers = state->child->found_tag_modifiers;
  2113. state->endofcontents = state->child->endofcontents;
  2114. }
  2115. /*
  2116. * Add in number of bytes consumed by child.
  2117. * (Only EXPLICIT should have already consumed bytes itself.)
  2118. */
  2119. PORT_Assert(state->place == afterExplicit || state->consumed == 0);
  2120. state->consumed += state->child->consumed;
  2121. /*
  2122. * Subtract from bytes pending; this only applies to a definite-length
  2123. * EXPLICIT field.
  2124. */
  2125. if (state->pending) {
  2126. PORT_Assert(!state->indefinite);
  2127. PORT_Assert(state->place == afterExplicit);
  2128. /*
  2129. * If we had a definite-length explicit, then what the child
  2130. * consumed should be what was left pending.
  2131. */
  2132. if (state->pending != state->child->consumed) {
  2133. if (state->pending < state->child->consumed) {
  2134. PORT_SetError(SEC_ERROR_BAD_DER);
  2135. state->top->status = decodeError;
  2136. return;
  2137. }
  2138. /*
  2139. * Okay, this is a hack. It *should* be an error whether
  2140. * pending is too big or too small, but it turns out that
  2141. * we had a bug in our *old* DER encoder that ended up
  2142. * counting an explicit header twice in the case where
  2143. * the underlying type was an ANY. So, because we cannot
  2144. * prevent receiving these (our own certificate server can
  2145. * send them to us), we need to be lenient and accept them.
  2146. * To do so, we need to pretend as if we read all of the
  2147. * bytes that the header said we would find, even though
  2148. * we actually came up short.
  2149. */
  2150. state->consumed += (state->pending - state->child->consumed);
  2151. }
  2152. state->pending = 0;
  2153. }
  2154. /*
  2155. * Indicate that we are done with child.
  2156. */
  2157. state->child->consumed = 0;
  2158. /*
  2159. * And move on to final state.
  2160. * (Technically everybody could move to afterEndOfContents except
  2161. * for an indefinite-length EXPLICIT; for simplicity though we assert
  2162. * that but let the end-of-contents code do the real determination.)
  2163. */
  2164. PORT_Assert(state->place == afterExplicit || (!state->indefinite));
  2165. state->place = beforeEndOfContents;
  2166. }
  2167. static void
  2168. sec_asn1d_prepare_for_end_of_contents(sec_asn1d_state *state)
  2169. {
  2170. PORT_Assert(state->place == beforeEndOfContents);
  2171. if (state->indefinite) {
  2172. state->place = duringEndOfContents;
  2173. state->pending = 2;
  2174. } else {
  2175. state->place = afterEndOfContents;
  2176. }
  2177. }
  2178. static unsigned long
  2179. sec_asn1d_parse_end_of_contents(sec_asn1d_state *state,
  2180. const char *buf, unsigned long len)
  2181. {
  2182. unsigned int i;
  2183. PORT_Assert(state->pending <= 2);
  2184. PORT_Assert(state->place == duringEndOfContents);
  2185. if (len == 0) {
  2186. state->top->status = needBytes;
  2187. return 0;
  2188. }
  2189. if (state->pending < len)
  2190. len = state->pending;
  2191. for (i = 0; i < len; i++) {
  2192. if (buf[i] != 0) {
  2193. /*
  2194. * We expect to find only zeros; if not, just give up.
  2195. */
  2196. PORT_SetError(SEC_ERROR_BAD_DER);
  2197. state->top->status = decodeError;
  2198. return 0;
  2199. }
  2200. }
  2201. state->pending -= len;
  2202. if (state->pending == 0) {
  2203. state->place = afterEndOfContents;
  2204. state->endofcontents = PR_TRUE;
  2205. }
  2206. return len;
  2207. }
  2208. static void
  2209. sec_asn1d_pop_state(sec_asn1d_state *state)
  2210. {
  2211. #if 0 /* XXX I think this should always be handled explicitly by parent? */
  2212. /*
  2213. * Account for our child.
  2214. */
  2215. if (state->child != NULL) {
  2216. state->consumed += state->child->consumed;
  2217. if (state->pending) {
  2218. PORT_Assert (!state->indefinite);
  2219. if (state->child->consumed > state->pending) {
  2220. PORT_SetError (SEC_ERROR_BAD_DER);
  2221. state->top->status = decodeError;
  2222. } else {
  2223. state->pending -= state->child->consumed;
  2224. }
  2225. }
  2226. state->child->consumed = 0;
  2227. }
  2228. #endif /* XXX */
  2229. /*
  2230. * Free our child.
  2231. */
  2232. sec_asn1d_free_child(state, PR_FALSE);
  2233. /*
  2234. * Just make my parent be the current state. It will then clean
  2235. * up after me and free me (or reuse me).
  2236. */
  2237. state->top->current = state->parent;
  2238. }
  2239. static sec_asn1d_state *
  2240. sec_asn1d_before_choice(sec_asn1d_state *state)
  2241. {
  2242. sec_asn1d_state *child;
  2243. if (state->allocate) {
  2244. void *dest;
  2245. dest = sec_asn1d_zalloc(state->top->their_pool, state->theTemplate->size);
  2246. if ((void *)NULL == dest) {
  2247. state->top->status = decodeError;
  2248. return (sec_asn1d_state *)NULL;
  2249. }
  2250. state->dest = (char *)dest + state->theTemplate->offset;
  2251. }
  2252. child = sec_asn1d_push_state(state->top, state->theTemplate + 1,
  2253. (char *)state->dest - state->theTemplate->offset,
  2254. PR_FALSE);
  2255. if ((sec_asn1d_state *)NULL == child) {
  2256. return (sec_asn1d_state *)NULL;
  2257. }
  2258. sec_asn1d_scrub_state(child);
  2259. child = sec_asn1d_init_state_based_on_template(child);
  2260. if ((sec_asn1d_state *)NULL == child) {
  2261. return (sec_asn1d_state *)NULL;
  2262. }
  2263. child->optional = PR_TRUE;
  2264. state->place = duringChoice;
  2265. return child;
  2266. }
  2267. static sec_asn1d_state *
  2268. sec_asn1d_during_choice(sec_asn1d_state *state)
  2269. {
  2270. sec_asn1d_state *child = state->child;
  2271. PORT_Assert((sec_asn1d_state *)NULL != child);
  2272. if (child->missing) {
  2273. unsigned char child_found_tag_modifiers = 0;
  2274. unsigned long child_found_tag_number = 0;
  2275. void *dest;
  2276. state->consumed += child->consumed;
  2277. if (child->endofcontents) {
  2278. /* This choice is probably the first item in a GROUP
  2279. ** (e.g. SET_OF) that was indefinite-length encoded.
  2280. ** We're actually at the end of that GROUP.
  2281. ** We look up the stack to be sure that we find
  2282. ** a state with indefinite length encoding before we
  2283. ** find a state (like a SEQUENCE) that is definite.
  2284. */
  2285. child->place = notInUse;
  2286. state->place = afterChoice;
  2287. state->endofcontents = PR_TRUE; /* propagate this up */
  2288. if (sec_asn1d_parent_allows_EOC(state))
  2289. return state;
  2290. PORT_SetError(SEC_ERROR_BAD_DER);
  2291. state->top->status = decodeError;
  2292. return NULL;
  2293. }
  2294. dest = (char *)child->dest - child->theTemplate->offset;
  2295. child->theTemplate++;
  2296. if (0 == child->theTemplate->kind) {
  2297. /* Ran out of choices */
  2298. PORT_SetError(SEC_ERROR_BAD_DER);
  2299. state->top->status = decodeError;
  2300. return (sec_asn1d_state *)NULL;
  2301. }
  2302. child->dest = (char *)dest + child->theTemplate->offset;
  2303. /* cargo'd from next_in_sequence innards */
  2304. if (state->pending) {
  2305. PORT_Assert(!state->indefinite);
  2306. if (child->consumed > state->pending) {
  2307. PORT_SetError(SEC_ERROR_BAD_DER);
  2308. state->top->status = decodeError;
  2309. return NULL;
  2310. }
  2311. state->pending -= child->consumed;
  2312. if (0 == state->pending) {
  2313. /* XXX uh.. not sure if I should have stopped this
  2314. * from happening before. */
  2315. PORT_Assert(0);
  2316. PORT_SetError(SEC_ERROR_BAD_DER);
  2317. state->top->status = decodeError;
  2318. return (sec_asn1d_state *)NULL;
  2319. }
  2320. }
  2321. child->consumed = 0;
  2322. sec_asn1d_scrub_state(child);
  2323. /* move it on top again */
  2324. state->top->current = child;
  2325. child_found_tag_modifiers = child->found_tag_modifiers;
  2326. child_found_tag_number = child->found_tag_number;
  2327. child = sec_asn1d_init_state_based_on_template(child);
  2328. if ((sec_asn1d_state *)NULL == child) {
  2329. return (sec_asn1d_state *)NULL;
  2330. }
  2331. /* copy our findings to the new top */
  2332. child->found_tag_modifiers = child_found_tag_modifiers;
  2333. child->found_tag_number = child_found_tag_number;
  2334. child->optional = PR_TRUE;
  2335. child->place = afterIdentifier;
  2336. return child;
  2337. }
  2338. if ((void *)NULL != state->dest) {
  2339. /* Store the enum */
  2340. int *which = (int *)state->dest;
  2341. *which = (int)child->theTemplate->size;
  2342. }
  2343. child->place = notInUse;
  2344. state->place = afterChoice;
  2345. return state;
  2346. }
  2347. static void
  2348. sec_asn1d_after_choice(sec_asn1d_state *state)
  2349. {
  2350. state->consumed += state->child->consumed;
  2351. state->child->consumed = 0;
  2352. state->place = afterEndOfContents;
  2353. sec_asn1d_pop_state(state);
  2354. }
  2355. unsigned long
  2356. sec_asn1d_uinteger(SECItem *src)
  2357. {
  2358. unsigned long value;
  2359. int len;
  2360. if (src->len > 5 || (src->len > 4 && src->data[0] == 0))
  2361. return 0;
  2362. value = 0;
  2363. len = src->len;
  2364. while (len) {
  2365. value <<= 8;
  2366. value |= src->data[--len];
  2367. }
  2368. return value;
  2369. }
  2370. SECStatus
  2371. SEC_ASN1DecodeInteger(SECItem *src, unsigned long *value)
  2372. {
  2373. unsigned long v;
  2374. unsigned int i;
  2375. if (src == NULL) {
  2376. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  2377. return SECFailure;
  2378. }
  2379. if (src->len > sizeof(unsigned long)) {
  2380. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  2381. return SECFailure;
  2382. }
  2383. if (src->data == NULL) {
  2384. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  2385. return SECFailure;
  2386. }
  2387. if (src->data[0] & 0x80)
  2388. v = -1; /* signed and negative - start with all 1's */
  2389. else
  2390. v = 0;
  2391. for (i = 0; i < src->len; i++) {
  2392. /* shift in next byte */
  2393. v <<= 8;
  2394. v |= src->data[i];
  2395. }
  2396. *value = v;
  2397. return SECSuccess;
  2398. }
  2399. #ifdef DEBUG_ASN1D_STATES
  2400. static void
  2401. dump_states(SEC_ASN1DecoderContext *cx)
  2402. {
  2403. sec_asn1d_state *state;
  2404. char kindBuf[256];
  2405. for (state = cx->current; state->parent; state = state->parent) {
  2406. ;
  2407. }
  2408. for (; state; state = state->child) {
  2409. int i;
  2410. for (i = 0; i < state->depth; i++) {
  2411. printf(" ");
  2412. }
  2413. i = formatKind(state->theTemplate->kind, kindBuf);
  2414. printf("%s: tmpl kind %s",
  2415. (state == cx->current) ? "STATE" : "State",
  2416. kindBuf);
  2417. printf(" %s", (state->place >= 0 && state->place <= notInUse) ? place_names[state->place] : "(undefined)");
  2418. if (!i)
  2419. printf(", expect 0x%02lx",
  2420. state->expect_tag_number | state->expect_tag_modifiers);
  2421. printf("%s%s%s %lu\n",
  2422. state->indefinite ? ", indef" : "",
  2423. state->missing ? ", miss" : "",
  2424. state->endofcontents ? ", EOC" : "",
  2425. state->pending);
  2426. }
  2427. return;
  2428. }
  2429. #endif /* DEBUG_ASN1D_STATES */
  2430. SECStatus
  2431. SEC_ASN1DecoderUpdate(SEC_ASN1DecoderContext *cx,
  2432. const char *buf, unsigned long len)
  2433. {
  2434. sec_asn1d_state *state = NULL;
  2435. unsigned long consumed;
  2436. SEC_ASN1EncodingPart what;
  2437. sec_asn1d_state *stateEnd = cx->current;
  2438. if (cx->status == needBytes)
  2439. cx->status = keepGoing;
  2440. while (cx->status == keepGoing) {
  2441. state = cx->current;
  2442. what = SEC_ASN1_Contents;
  2443. consumed = 0;
  2444. #ifdef DEBUG_ASN1D_STATES
  2445. printf("\nPLACE = %s, next byte = 0x%02x, %p[%lu]\n",
  2446. (state->place >= 0 && state->place <= notInUse) ? place_names[state->place] : "(undefined)",
  2447. len ? (unsigned int)((unsigned char *)buf)[consumed] : 0,
  2448. buf, consumed);
  2449. dump_states(cx);
  2450. #endif /* DEBUG_ASN1D_STATES */
  2451. switch (state->place) {
  2452. case beforeIdentifier:
  2453. consumed = sec_asn1d_parse_identifier(state, buf, len);
  2454. what = SEC_ASN1_Identifier;
  2455. break;
  2456. case duringIdentifier:
  2457. consumed = sec_asn1d_parse_more_identifier(state, buf, len);
  2458. what = SEC_ASN1_Identifier;
  2459. break;
  2460. case afterIdentifier:
  2461. sec_asn1d_confirm_identifier(state);
  2462. break;
  2463. case beforeLength:
  2464. consumed = sec_asn1d_parse_length(state, buf, len);
  2465. what = SEC_ASN1_Length;
  2466. break;
  2467. case duringLength:
  2468. consumed = sec_asn1d_parse_more_length(state, buf, len);
  2469. what = SEC_ASN1_Length;
  2470. break;
  2471. case afterLength:
  2472. sec_asn1d_prepare_for_contents(state);
  2473. break;
  2474. case beforeBitString:
  2475. consumed = sec_asn1d_parse_bit_string(state, buf, len);
  2476. break;
  2477. case duringBitString:
  2478. consumed = sec_asn1d_parse_more_bit_string(state, buf, len);
  2479. break;
  2480. case duringConstructedString:
  2481. sec_asn1d_next_substring(state);
  2482. break;
  2483. case duringGroup:
  2484. sec_asn1d_next_in_group(state);
  2485. break;
  2486. case duringLeaf:
  2487. consumed = sec_asn1d_parse_leaf(state, buf, len);
  2488. break;
  2489. case duringSaveEncoding:
  2490. sec_asn1d_reuse_encoding(state);
  2491. if (cx->status == decodeError) {
  2492. /* recursive call has already popped all states from stack.
  2493. ** Bail out quickly.
  2494. */
  2495. return SECFailure;
  2496. }
  2497. if (cx->status == needBytes) {
  2498. /* recursive call wanted more data. Fatal. Clean up below. */
  2499. PORT_SetError(SEC_ERROR_BAD_DER);
  2500. cx->status = decodeError;
  2501. }
  2502. break;
  2503. case duringSequence:
  2504. sec_asn1d_next_in_sequence(state);
  2505. break;
  2506. case afterConstructedString:
  2507. sec_asn1d_concat_substrings(state);
  2508. break;
  2509. case afterExplicit:
  2510. case afterImplicit:
  2511. case afterInline:
  2512. case afterPointer:
  2513. sec_asn1d_absorb_child(state);
  2514. break;
  2515. case afterGroup:
  2516. sec_asn1d_concat_group(state);
  2517. break;
  2518. case afterSaveEncoding:
  2519. /* SEC_ASN1DecoderUpdate has called itself recursively to
  2520. ** decode SAVEd encoded data, and now is done decoding that.
  2521. ** Return to the calling copy of SEC_ASN1DecoderUpdate.
  2522. */
  2523. return SECSuccess;
  2524. case beforeEndOfContents:
  2525. sec_asn1d_prepare_for_end_of_contents(state);
  2526. break;
  2527. case duringEndOfContents:
  2528. consumed = sec_asn1d_parse_end_of_contents(state, buf, len);
  2529. what = SEC_ASN1_EndOfContents;
  2530. break;
  2531. case afterEndOfContents:
  2532. sec_asn1d_pop_state(state);
  2533. break;
  2534. case beforeChoice:
  2535. state = sec_asn1d_before_choice(state);
  2536. break;
  2537. case duringChoice:
  2538. state = sec_asn1d_during_choice(state);
  2539. break;
  2540. case afterChoice:
  2541. sec_asn1d_after_choice(state);
  2542. break;
  2543. case notInUse:
  2544. default:
  2545. /* This is not an error, but rather a plain old BUG! */
  2546. PORT_Assert(0);
  2547. PORT_SetError(SEC_ERROR_BAD_DER);
  2548. cx->status = decodeError;
  2549. break;
  2550. }
  2551. if (cx->status == decodeError)
  2552. break;
  2553. /* We should not consume more than we have. */
  2554. PORT_Assert(consumed <= len);
  2555. if (consumed > len) {
  2556. PORT_SetError(SEC_ERROR_BAD_DER);
  2557. cx->status = decodeError;
  2558. break;
  2559. }
  2560. /* It might have changed, so we have to update our local copy. */
  2561. state = cx->current;
  2562. /* If it is NULL, we have popped all the way to the top. */
  2563. if (state == NULL) {
  2564. PORT_Assert(consumed == 0);
  2565. #if 0 /* XXX I want this here, but it seems that we have situations (like \
  2566. * downloading a pkcs7 cert chain from some issuers) that give us a \
  2567. * length which is greater than the entire encoding. So, we cannot \
  2568. * have this be an error. \
  2569. */
  2570. if (len > 0) {
  2571. PORT_SetError (SEC_ERROR_BAD_DER);
  2572. cx->status = decodeError;
  2573. } else
  2574. #endif
  2575. cx->status = allDone;
  2576. break;
  2577. } else if (state->theTemplate->kind == SEC_ASN1_SKIP_REST) {
  2578. cx->status = allDone;
  2579. break;
  2580. }
  2581. if (consumed == 0)
  2582. continue;
  2583. /*
  2584. * The following check is specifically looking for an ANY
  2585. * that is *not* also an INNER, because we need to save aside
  2586. * all bytes in that case -- the contents parts will get
  2587. * handled like all other contents, and the end-of-contents
  2588. * bytes are added by the concat code, but the outer header
  2589. * bytes need to get saved too, so we do them explicitly here.
  2590. */
  2591. if (state->underlying_kind == SEC_ASN1_ANY && !cx->filter_only && (what == SEC_ASN1_Identifier || what == SEC_ASN1_Length)) {
  2592. sec_asn1d_record_any_header(state, buf, consumed);
  2593. }
  2594. /*
  2595. * We had some number of good, accepted bytes. If the caller
  2596. * has registered to see them, pass them along.
  2597. */
  2598. if (state->top->filter_proc != NULL) {
  2599. int depth;
  2600. depth = state->depth;
  2601. if (what == SEC_ASN1_EndOfContents && !state->indefinite) {
  2602. PORT_Assert(state->parent != NULL && state->parent->indefinite);
  2603. depth--;
  2604. PORT_Assert(depth == state->parent->depth);
  2605. }
  2606. (*state->top->filter_proc)(state->top->filter_arg,
  2607. buf, consumed, depth, what);
  2608. }
  2609. state->consumed += consumed;
  2610. buf += consumed;
  2611. len -= consumed;
  2612. }
  2613. if (cx->status == decodeError) {
  2614. while (state != NULL && stateEnd->parent != state) {
  2615. sec_asn1d_free_child(state, PR_TRUE);
  2616. state = state->parent;
  2617. }
  2618. #ifdef SEC_ASN1D_FREE_ON_ERROR /* \
  2619. * XXX This does not work because we can \
  2620. * end up leaving behind dangling pointers \
  2621. * to stuff that was allocated. In order \
  2622. * to make this really work (which would \
  2623. * be a good thing, I think), we need to \
  2624. * keep track of every place/pointer that \
  2625. * was allocated and make sure to NULL it \
  2626. * out before we then free back to the mark. \
  2627. */
  2628. if (cx->their_pool != NULL) {
  2629. PORT_Assert(cx->their_mark != NULL);
  2630. PORT_ArenaRelease(cx->their_pool, cx->their_mark);
  2631. cx->their_mark = NULL;
  2632. }
  2633. #endif
  2634. return SECFailure;
  2635. }
  2636. #if 0 /* XXX This is what I want, but cannot have because it seems we \
  2637. * have situations (like when downloading a pkcs7 cert chain from \
  2638. * some issuers) that give us a total length which is greater than \
  2639. * the entire encoding. So, we have to allow allDone to have a \
  2640. * remaining length greater than zero. I wanted to catch internal \
  2641. * bugs with this, noticing when we do not have the right length. \
  2642. * Oh well. \
  2643. */
  2644. PORT_Assert (len == 0
  2645. && (cx->status == needBytes || cx->status == allDone));
  2646. #else
  2647. PORT_Assert((len == 0 && cx->status == needBytes) || cx->status == allDone);
  2648. #endif
  2649. return SECSuccess;
  2650. }
  2651. SECStatus
  2652. SEC_ASN1DecoderFinish(SEC_ASN1DecoderContext *cx)
  2653. {
  2654. SECStatus rv;
  2655. if (!cx || cx->status == needBytes) {
  2656. PORT_SetError(SEC_ERROR_BAD_DER);
  2657. rv = SECFailure;
  2658. } else {
  2659. rv = SECSuccess;
  2660. }
  2661. /*
  2662. * XXX anything else that needs to be finished?
  2663. */
  2664. if (cx) {
  2665. PORT_FreeArena(cx->our_pool, PR_TRUE);
  2666. }
  2667. return rv;
  2668. }
  2669. SEC_ASN1DecoderContext *
  2670. SEC_ASN1DecoderStart(PLArenaPool *their_pool, void *dest,
  2671. const SEC_ASN1Template *theTemplate)
  2672. {
  2673. PLArenaPool *our_pool;
  2674. SEC_ASN1DecoderContext *cx;
  2675. our_pool = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
  2676. if (our_pool == NULL)
  2677. return NULL;
  2678. cx = (SEC_ASN1DecoderContext *)PORT_ArenaZAlloc(our_pool, sizeof(*cx));
  2679. if (cx == NULL) {
  2680. PORT_FreeArena(our_pool, PR_FALSE);
  2681. return NULL;
  2682. }
  2683. cx->our_pool = our_pool;
  2684. if (their_pool != NULL) {
  2685. cx->their_pool = their_pool;
  2686. #ifdef SEC_ASN1D_FREE_ON_ERROR
  2687. cx->their_mark = PORT_ArenaMark(their_pool);
  2688. #endif
  2689. }
  2690. cx->status = needBytes;
  2691. if (sec_asn1d_push_state(cx, theTemplate, dest, PR_FALSE) == NULL || sec_asn1d_init_state_based_on_template(cx->current) == NULL) {
  2692. /*
  2693. * Trouble initializing (probably due to failed allocations)
  2694. * requires that we just give up.
  2695. */
  2696. PORT_FreeArena(our_pool, PR_FALSE);
  2697. return NULL;
  2698. }
  2699. return cx;
  2700. }
  2701. void
  2702. SEC_ASN1DecoderSetFilterProc(SEC_ASN1DecoderContext *cx,
  2703. SEC_ASN1WriteProc fn, void *arg,
  2704. PRBool only)
  2705. {
  2706. /* check that we are "between" fields here */
  2707. PORT_Assert(cx->during_notify);
  2708. cx->filter_proc = fn;
  2709. cx->filter_arg = arg;
  2710. cx->filter_only = only;
  2711. }
  2712. void
  2713. SEC_ASN1DecoderClearFilterProc(SEC_ASN1DecoderContext *cx)
  2714. {
  2715. /* check that we are "between" fields here */
  2716. PORT_Assert(cx->during_notify);
  2717. cx->filter_proc = NULL;
  2718. cx->filter_arg = NULL;
  2719. cx->filter_only = PR_FALSE;
  2720. }
  2721. void
  2722. SEC_ASN1DecoderSetNotifyProc(SEC_ASN1DecoderContext *cx,
  2723. SEC_ASN1NotifyProc fn, void *arg)
  2724. {
  2725. cx->notify_proc = fn;
  2726. cx->notify_arg = arg;
  2727. }
  2728. void
  2729. SEC_ASN1DecoderClearNotifyProc(SEC_ASN1DecoderContext *cx)
  2730. {
  2731. cx->notify_proc = NULL;
  2732. cx->notify_arg = NULL; /* not necessary; just being clean */
  2733. }
  2734. void
  2735. SEC_ASN1DecoderSetMaximumElementSize(SEC_ASN1DecoderContext *cx,
  2736. unsigned long max_size)
  2737. {
  2738. cx->max_element_size = max_size;
  2739. }
  2740. void
  2741. SEC_ASN1DecoderAbort(SEC_ASN1DecoderContext *cx, int error)
  2742. {
  2743. PORT_Assert(cx);
  2744. PORT_SetError(error);
  2745. cx->status = decodeError;
  2746. }
  2747. SECStatus
  2748. SEC_ASN1Decode(PLArenaPool *poolp, void *dest,
  2749. const SEC_ASN1Template *theTemplate,
  2750. const char *buf, long len)
  2751. {
  2752. SEC_ASN1DecoderContext *dcx;
  2753. SECStatus urv, frv;
  2754. dcx = SEC_ASN1DecoderStart(poolp, dest, theTemplate);
  2755. if (dcx == NULL)
  2756. return SECFailure;
  2757. /* In one-shot mode, there's no possibility of streaming data beyond the
  2758. * length of len */
  2759. SEC_ASN1DecoderSetMaximumElementSize(dcx, len);
  2760. urv = SEC_ASN1DecoderUpdate(dcx, buf, len);
  2761. frv = SEC_ASN1DecoderFinish(dcx);
  2762. if (urv != SECSuccess)
  2763. return urv;
  2764. return frv;
  2765. }
  2766. SECStatus
  2767. SEC_ASN1DecodeItem(PLArenaPool *poolp, void *dest,
  2768. const SEC_ASN1Template *theTemplate,
  2769. const SECItem *src)
  2770. {
  2771. return SEC_ASN1Decode(poolp, dest, theTemplate,
  2772. (const char *)src->data, src->len);
  2773. }
  2774. #ifdef DEBUG_ASN1D_STATES
  2775. void
  2776. sec_asn1d_Assert(const char *s, const char *file, PRIntn ln)
  2777. {
  2778. printf("Assertion failed, \"%s\", file %s, line %d\n", s, file, ln);
  2779. fflush(stdout);
  2780. }
  2781. #endif
  2782. /*
  2783. * Generic templates for individual/simple items and pointers to
  2784. * and sets of same.
  2785. *
  2786. * If you need to add a new one, please note the following:
  2787. * - For each new basic type you should add *four* templates:
  2788. * one plain, one PointerTo, one SequenceOf and one SetOf.
  2789. * - If the new type can be constructed (meaning, it is a
  2790. * *string* type according to BER/DER rules), then you should
  2791. * or-in SEC_ASN1_MAY_STREAM to the type in the basic template.
  2792. * See the definition of the OctetString template for an example.
  2793. * - It may not be obvious, but these are in *alphabetical*
  2794. * order based on the SEC_ASN1_XXX name; so put new ones in
  2795. * the appropriate place.
  2796. */
  2797. const SEC_ASN1Template SEC_SequenceOfAnyTemplate[] = {
  2798. { SEC_ASN1_SEQUENCE_OF, 0, SEC_AnyTemplate }
  2799. };
  2800. #if 0
  2801. const SEC_ASN1Template SEC_PointerToBitStringTemplate[] = {
  2802. { SEC_ASN1_POINTER, 0, SEC_BitStringTemplate }
  2803. };
  2804. const SEC_ASN1Template SEC_SequenceOfBitStringTemplate[] = {
  2805. { SEC_ASN1_SEQUENCE_OF, 0, SEC_BitStringTemplate }
  2806. };
  2807. const SEC_ASN1Template SEC_SetOfBitStringTemplate[] = {
  2808. { SEC_ASN1_SET_OF, 0, SEC_BitStringTemplate }
  2809. };
  2810. const SEC_ASN1Template SEC_PointerToBMPStringTemplate[] = {
  2811. { SEC_ASN1_POINTER, 0, SEC_BMPStringTemplate }
  2812. };
  2813. const SEC_ASN1Template SEC_SequenceOfBMPStringTemplate[] = {
  2814. { SEC_ASN1_SEQUENCE_OF, 0, SEC_BMPStringTemplate }
  2815. };
  2816. const SEC_ASN1Template SEC_SetOfBMPStringTemplate[] = {
  2817. { SEC_ASN1_SET_OF, 0, SEC_BMPStringTemplate }
  2818. };
  2819. const SEC_ASN1Template SEC_PointerToBooleanTemplate[] = {
  2820. { SEC_ASN1_POINTER, 0, SEC_BooleanTemplate }
  2821. };
  2822. const SEC_ASN1Template SEC_SequenceOfBooleanTemplate[] = {
  2823. { SEC_ASN1_SEQUENCE_OF, 0, SEC_BooleanTemplate }
  2824. };
  2825. const SEC_ASN1Template SEC_SetOfBooleanTemplate[] = {
  2826. { SEC_ASN1_SET_OF, 0, SEC_BooleanTemplate }
  2827. };
  2828. #endif
  2829. const SEC_ASN1Template SEC_EnumeratedTemplate[] = {
  2830. { SEC_ASN1_ENUMERATED, 0, NULL, sizeof(SECItem) }
  2831. };
  2832. const SEC_ASN1Template SEC_PointerToEnumeratedTemplate[] = {
  2833. { SEC_ASN1_POINTER, 0, SEC_EnumeratedTemplate }
  2834. };
  2835. #if 0
  2836. const SEC_ASN1Template SEC_SequenceOfEnumeratedTemplate[] = {
  2837. { SEC_ASN1_SEQUENCE_OF, 0, SEC_EnumeratedTemplate }
  2838. };
  2839. #endif
  2840. const SEC_ASN1Template SEC_SetOfEnumeratedTemplate[] = {
  2841. { SEC_ASN1_SET_OF, 0, SEC_EnumeratedTemplate }
  2842. };
  2843. const SEC_ASN1Template SEC_PointerToGeneralizedTimeTemplate[] = {
  2844. { SEC_ASN1_POINTER, 0, SEC_GeneralizedTimeTemplate }
  2845. };
  2846. #if 0
  2847. const SEC_ASN1Template SEC_SequenceOfGeneralizedTimeTemplate[] = {
  2848. { SEC_ASN1_SEQUENCE_OF, 0, SEC_GeneralizedTimeTemplate }
  2849. };
  2850. const SEC_ASN1Template SEC_SetOfGeneralizedTimeTemplate[] = {
  2851. { SEC_ASN1_SET_OF, 0, SEC_GeneralizedTimeTemplate }
  2852. };
  2853. const SEC_ASN1Template SEC_PointerToIA5StringTemplate[] = {
  2854. { SEC_ASN1_POINTER, 0, SEC_IA5StringTemplate }
  2855. };
  2856. const SEC_ASN1Template SEC_SequenceOfIA5StringTemplate[] = {
  2857. { SEC_ASN1_SEQUENCE_OF, 0, SEC_IA5StringTemplate }
  2858. };
  2859. const SEC_ASN1Template SEC_SetOfIA5StringTemplate[] = {
  2860. { SEC_ASN1_SET_OF, 0, SEC_IA5StringTemplate }
  2861. };
  2862. const SEC_ASN1Template SEC_PointerToIntegerTemplate[] = {
  2863. { SEC_ASN1_POINTER, 0, SEC_IntegerTemplate }
  2864. };
  2865. const SEC_ASN1Template SEC_SequenceOfIntegerTemplate[] = {
  2866. { SEC_ASN1_SEQUENCE_OF, 0, SEC_IntegerTemplate }
  2867. };
  2868. const SEC_ASN1Template SEC_SetOfIntegerTemplate[] = {
  2869. { SEC_ASN1_SET_OF, 0, SEC_IntegerTemplate }
  2870. };
  2871. const SEC_ASN1Template SEC_PointerToNullTemplate[] = {
  2872. { SEC_ASN1_POINTER, 0, SEC_NullTemplate }
  2873. };
  2874. const SEC_ASN1Template SEC_SequenceOfNullTemplate[] = {
  2875. { SEC_ASN1_SEQUENCE_OF, 0, SEC_NullTemplate }
  2876. };
  2877. const SEC_ASN1Template SEC_SetOfNullTemplate[] = {
  2878. { SEC_ASN1_SET_OF, 0, SEC_NullTemplate }
  2879. };
  2880. const SEC_ASN1Template SEC_PointerToObjectIDTemplate[] = {
  2881. { SEC_ASN1_POINTER, 0, SEC_ObjectIDTemplate }
  2882. };
  2883. #endif
  2884. const SEC_ASN1Template SEC_SequenceOfObjectIDTemplate[] = {
  2885. { SEC_ASN1_SEQUENCE_OF, 0, SEC_ObjectIDTemplate }
  2886. };
  2887. #if 0
  2888. const SEC_ASN1Template SEC_SetOfObjectIDTemplate[] = {
  2889. { SEC_ASN1_SET_OF, 0, SEC_ObjectIDTemplate }
  2890. };
  2891. const SEC_ASN1Template SEC_SequenceOfOctetStringTemplate[] = {
  2892. { SEC_ASN1_SEQUENCE_OF, 0, SEC_OctetStringTemplate }
  2893. };
  2894. const SEC_ASN1Template SEC_SetOfOctetStringTemplate[] = {
  2895. { SEC_ASN1_SET_OF, 0, SEC_OctetStringTemplate }
  2896. };
  2897. #endif
  2898. const SEC_ASN1Template SEC_PrintableStringTemplate[] = {
  2899. { SEC_ASN1_PRINTABLE_STRING | SEC_ASN1_MAY_STREAM, 0, NULL, sizeof(SECItem) }
  2900. };
  2901. #if 0
  2902. const SEC_ASN1Template SEC_PointerToPrintableStringTemplate[] = {
  2903. { SEC_ASN1_POINTER, 0, SEC_PrintableStringTemplate }
  2904. };
  2905. const SEC_ASN1Template SEC_SequenceOfPrintableStringTemplate[] = {
  2906. { SEC_ASN1_SEQUENCE_OF, 0, SEC_PrintableStringTemplate }
  2907. };
  2908. const SEC_ASN1Template SEC_SetOfPrintableStringTemplate[] = {
  2909. { SEC_ASN1_SET_OF, 0, SEC_PrintableStringTemplate }
  2910. };
  2911. #endif
  2912. const SEC_ASN1Template SEC_T61StringTemplate[] = {
  2913. { SEC_ASN1_T61_STRING | SEC_ASN1_MAY_STREAM, 0, NULL, sizeof(SECItem) }
  2914. };
  2915. #if 0
  2916. const SEC_ASN1Template SEC_PointerToT61StringTemplate[] = {
  2917. { SEC_ASN1_POINTER, 0, SEC_T61StringTemplate }
  2918. };
  2919. const SEC_ASN1Template SEC_SequenceOfT61StringTemplate[] = {
  2920. { SEC_ASN1_SEQUENCE_OF, 0, SEC_T61StringTemplate }
  2921. };
  2922. const SEC_ASN1Template SEC_SetOfT61StringTemplate[] = {
  2923. { SEC_ASN1_SET_OF, 0, SEC_T61StringTemplate }
  2924. };
  2925. #endif
  2926. const SEC_ASN1Template SEC_UniversalStringTemplate[] = {
  2927. { SEC_ASN1_UNIVERSAL_STRING | SEC_ASN1_MAY_STREAM, 0, NULL, sizeof(SECItem) }
  2928. };
  2929. #if 0
  2930. const SEC_ASN1Template SEC_PointerToUniversalStringTemplate[] = {
  2931. { SEC_ASN1_POINTER, 0, SEC_UniversalStringTemplate }
  2932. };
  2933. const SEC_ASN1Template SEC_SequenceOfUniversalStringTemplate[] = {
  2934. { SEC_ASN1_SEQUENCE_OF, 0, SEC_UniversalStringTemplate }
  2935. };
  2936. const SEC_ASN1Template SEC_SetOfUniversalStringTemplate[] = {
  2937. { SEC_ASN1_SET_OF, 0, SEC_UniversalStringTemplate }
  2938. };
  2939. const SEC_ASN1Template SEC_PointerToUTCTimeTemplate[] = {
  2940. { SEC_ASN1_POINTER, 0, SEC_UTCTimeTemplate }
  2941. };
  2942. const SEC_ASN1Template SEC_SequenceOfUTCTimeTemplate[] = {
  2943. { SEC_ASN1_SEQUENCE_OF, 0, SEC_UTCTimeTemplate }
  2944. };
  2945. const SEC_ASN1Template SEC_SetOfUTCTimeTemplate[] = {
  2946. { SEC_ASN1_SET_OF, 0, SEC_UTCTimeTemplate }
  2947. };
  2948. const SEC_ASN1Template SEC_PointerToUTF8StringTemplate[] = {
  2949. { SEC_ASN1_POINTER, 0, SEC_UTF8StringTemplate }
  2950. };
  2951. const SEC_ASN1Template SEC_SequenceOfUTF8StringTemplate[] = {
  2952. { SEC_ASN1_SEQUENCE_OF, 0, SEC_UTF8StringTemplate }
  2953. };
  2954. const SEC_ASN1Template SEC_SetOfUTF8StringTemplate[] = {
  2955. { SEC_ASN1_SET_OF, 0, SEC_UTF8StringTemplate }
  2956. };
  2957. #endif
  2958. const SEC_ASN1Template SEC_VisibleStringTemplate[] = {
  2959. { SEC_ASN1_VISIBLE_STRING | SEC_ASN1_MAY_STREAM, 0, NULL, sizeof(SECItem) }
  2960. };
  2961. #if 0
  2962. const SEC_ASN1Template SEC_PointerToVisibleStringTemplate[] = {
  2963. { SEC_ASN1_POINTER, 0, SEC_VisibleStringTemplate }
  2964. };
  2965. const SEC_ASN1Template SEC_SequenceOfVisibleStringTemplate[] = {
  2966. { SEC_ASN1_SEQUENCE_OF, 0, SEC_VisibleStringTemplate }
  2967. };
  2968. const SEC_ASN1Template SEC_SetOfVisibleStringTemplate[] = {
  2969. { SEC_ASN1_SET_OF, 0, SEC_VisibleStringTemplate }
  2970. };
  2971. #endif
  2972. /*
  2973. * Template for skipping a subitem.
  2974. *
  2975. * Note that it only makes sense to use this for decoding (when you want
  2976. * to decode something where you are only interested in one or two of
  2977. * the fields); you cannot encode a SKIP!
  2978. */
  2979. const SEC_ASN1Template SEC_SkipTemplate[] = {
  2980. { SEC_ASN1_SKIP }
  2981. };
  2982. /* These functions simply return the address of the above-declared templates.
  2983. ** This is necessary for Windows DLLs. Sigh.
  2984. */
  2985. SEC_ASN1_CHOOSER_IMPLEMENT(SEC_EnumeratedTemplate)
  2986. SEC_ASN1_CHOOSER_IMPLEMENT(SEC_PointerToEnumeratedTemplate)
  2987. SEC_ASN1_CHOOSER_IMPLEMENT(SEC_SequenceOfAnyTemplate)
  2988. SEC_ASN1_CHOOSER_IMPLEMENT(SEC_SequenceOfObjectIDTemplate)
  2989. SEC_ASN1_CHOOSER_IMPLEMENT(SEC_SkipTemplate)
  2990. SEC_ASN1_CHOOSER_IMPLEMENT(SEC_UniversalStringTemplate)
  2991. SEC_ASN1_CHOOSER_IMPLEMENT(SEC_PrintableStringTemplate)
  2992. SEC_ASN1_CHOOSER_IMPLEMENT(SEC_T61StringTemplate)
  2993. SEC_ASN1_CHOOSER_IMPLEMENT(SEC_PointerToGeneralizedTimeTemplate)