asn1.h 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. #ifndef HEADER_ASN1_H
  58. #define HEADER_ASN1_H
  59. #include <openssl/base.h>
  60. #include <time.h>
  61. #include <openssl/bio.h>
  62. #include <openssl/bn.h>
  63. #include <openssl/stack.h>
  64. #if defined(__cplusplus)
  65. extern "C" {
  66. #endif
  67. // Legacy ASN.1 library.
  68. //
  69. // This header is part of OpenSSL's ASN.1 implementation. It is retained for
  70. // compatibility but should not be used by new code. The functions are difficult
  71. // to use correctly, and have buggy or non-standard behaviors. They are thus
  72. // particularly prone to behavior changes and API removals, as BoringSSL
  73. // iterates on these issues.
  74. //
  75. // Use the new |CBS| and |CBB| library in <openssl/bytestring.h> instead.
  76. // Tag constants.
  77. //
  78. // These constants are used in various APIs to specify ASN.1 types and tag
  79. // components. See the specific API's documentation for details on which values
  80. // are used and how.
  81. // The following constants are tag classes.
  82. #define V_ASN1_UNIVERSAL 0x00
  83. #define V_ASN1_APPLICATION 0x40
  84. #define V_ASN1_CONTEXT_SPECIFIC 0x80
  85. #define V_ASN1_PRIVATE 0xc0
  86. // V_ASN1_CONSTRUCTED indicates an element is constructed, rather than
  87. // primitive.
  88. #define V_ASN1_CONSTRUCTED 0x20
  89. // V_ASN1_PRIMITIVE_TAG is the highest tag number which can be encoded in a
  90. // single byte. Note this is unrelated to whether an element is constructed or
  91. // primitive.
  92. //
  93. // TODO(davidben): Make this private.
  94. #define V_ASN1_PRIMITIVE_TAG 0x1f
  95. // V_ASN1_MAX_UNIVERSAL is the highest supported universal tag number. It is
  96. // necessary to avoid ambiguity with |V_ASN1_NEG| and |MBSTRING_FLAG|.
  97. //
  98. // TODO(davidben): Make this private.
  99. #define V_ASN1_MAX_UNIVERSAL 0xff
  100. // V_ASN1_UNDEF is used in some APIs to indicate an ASN.1 element is omitted.
  101. #define V_ASN1_UNDEF (-1)
  102. // V_ASN1_OTHER is used in |ASN1_TYPE| to indicate a non-universal ASN.1 type.
  103. #define V_ASN1_OTHER (-3)
  104. // V_ASN1_ANY is used by the ASN.1 templates to indicate an ANY type.
  105. #define V_ASN1_ANY (-4)
  106. // The following constants are tag numbers for universal types.
  107. #define V_ASN1_EOC 0
  108. #define V_ASN1_BOOLEAN 1
  109. #define V_ASN1_INTEGER 2
  110. #define V_ASN1_BIT_STRING 3
  111. #define V_ASN1_OCTET_STRING 4
  112. #define V_ASN1_NULL 5
  113. #define V_ASN1_OBJECT 6
  114. #define V_ASN1_OBJECT_DESCRIPTOR 7
  115. #define V_ASN1_EXTERNAL 8
  116. #define V_ASN1_REAL 9
  117. #define V_ASN1_ENUMERATED 10
  118. #define V_ASN1_UTF8STRING 12
  119. #define V_ASN1_SEQUENCE 16
  120. #define V_ASN1_SET 17
  121. #define V_ASN1_NUMERICSTRING 18
  122. #define V_ASN1_PRINTABLESTRING 19
  123. #define V_ASN1_T61STRING 20
  124. #define V_ASN1_TELETEXSTRING 20
  125. #define V_ASN1_VIDEOTEXSTRING 21
  126. #define V_ASN1_IA5STRING 22
  127. #define V_ASN1_UTCTIME 23
  128. #define V_ASN1_GENERALIZEDTIME 24
  129. #define V_ASN1_GRAPHICSTRING 25
  130. #define V_ASN1_ISO64STRING 26
  131. #define V_ASN1_VISIBLESTRING 26
  132. #define V_ASN1_GENERALSTRING 27
  133. #define V_ASN1_UNIVERSALSTRING 28
  134. #define V_ASN1_BMPSTRING 30
  135. // The following constants are used for |ASN1_STRING| values that represent
  136. // negative INTEGER and ENUMERATED values. See |ASN1_STRING| for more details.
  137. #define V_ASN1_NEG 0x100
  138. #define V_ASN1_NEG_INTEGER (V_ASN1_INTEGER | V_ASN1_NEG)
  139. #define V_ASN1_NEG_ENUMERATED (V_ASN1_ENUMERATED | V_ASN1_NEG)
  140. // The following constants are bitmask representations of ASN.1 types.
  141. #define B_ASN1_NUMERICSTRING 0x0001
  142. #define B_ASN1_PRINTABLESTRING 0x0002
  143. #define B_ASN1_T61STRING 0x0004
  144. #define B_ASN1_TELETEXSTRING 0x0004
  145. #define B_ASN1_VIDEOTEXSTRING 0x0008
  146. #define B_ASN1_IA5STRING 0x0010
  147. #define B_ASN1_GRAPHICSTRING 0x0020
  148. #define B_ASN1_ISO64STRING 0x0040
  149. #define B_ASN1_VISIBLESTRING 0x0040
  150. #define B_ASN1_GENERALSTRING 0x0080
  151. #define B_ASN1_UNIVERSALSTRING 0x0100
  152. #define B_ASN1_OCTET_STRING 0x0200
  153. #define B_ASN1_BIT_STRING 0x0400
  154. #define B_ASN1_BMPSTRING 0x0800
  155. #define B_ASN1_UNKNOWN 0x1000
  156. #define B_ASN1_UTF8STRING 0x2000
  157. #define B_ASN1_UTCTIME 0x4000
  158. #define B_ASN1_GENERALIZEDTIME 0x8000
  159. #define B_ASN1_SEQUENCE 0x10000
  160. // ASN1_tag2bit converts |tag| from the tag number of a universal type to a
  161. // corresponding |B_ASN1_*| constant, |B_ASN1_UNKNOWN|, or zero. If the
  162. // |B_ASN1_*| constant above is defined, it will map the corresponding
  163. // |V_ASN1_*| constant to it. Otherwise, whether it returns |B_ASN1_UNKNOWN| or
  164. // zero is ill-defined and callers should not rely on it.
  165. //
  166. // TODO(https://crbug.com/boringssl/412): Figure out what |B_ASN1_UNNOWN| vs
  167. // zero is meant to be. The main impact is what values go in |B_ASN1_PRINTABLE|.
  168. // To that end, we must return zero on types that can't go in |ASN1_STRING|.
  169. OPENSSL_EXPORT unsigned long ASN1_tag2bit(int tag);
  170. // ASN1_tag2str returns a string representation of |tag|, interpret as a tag
  171. // number for a universal type, or |V_ASN1_NEG_*|.
  172. OPENSSL_EXPORT const char *ASN1_tag2str(int tag);
  173. // API conventions.
  174. //
  175. // The following sample functions document the calling conventions used by
  176. // legacy ASN.1 APIs.
  177. #if 0 // Sample functions
  178. // d2i_SAMPLE parses a structure from up to |len| bytes at |*inp|. On success,
  179. // it advances |*inp| by the number of bytes read and returns a newly-allocated
  180. // |SAMPLE| object containing the parsed structure. If |out| is non-NULL, it
  181. // additionally frees the previous value at |*out| and updates |*out| to the
  182. // result. If parsing or allocating the result fails, it returns NULL.
  183. //
  184. // This function does not reject trailing data in the input. This allows the
  185. // caller to parse a sequence of concatenated structures. Callers parsing only
  186. // one structure should check for trailing data by comparing the updated |*inp|
  187. // with the end of the input.
  188. //
  189. // Note: If |out| and |*out| are both non-NULL, the object at |*out| is not
  190. // updated in-place. Instead, it is freed, and the pointer is updated to the
  191. // new object. This differs from OpenSSL, which behaves more like
  192. // |d2i_SAMPLE_with_reuse|. Callers are recommended to set |out| to NULL and
  193. // instead use the return value.
  194. SAMPLE *d2i_SAMPLE(SAMPLE **out, const uint8_t **inp, long len);
  195. // d2i_SAMPLE_with_reuse parses a structure from up to |len| bytes at |*inp|. On
  196. // success, it advances |*inp| by the number of bytes read and returns a
  197. // non-NULL pointer to an object containing the parsed structure. The object is
  198. // determined from |out| as follows:
  199. //
  200. // If |out| is NULL, the function places the result in a newly-allocated
  201. // |SAMPLE| object and returns it. This mode is recommended.
  202. //
  203. // If |out| is non-NULL, but |*out| is NULL, the function also places the result
  204. // in a newly-allocated |SAMPLE| object. It sets |*out| to this object and also
  205. // returns it.
  206. //
  207. // If |out| and |*out| are both non-NULL, the function updates the object at
  208. // |*out| in-place with the result and returns |*out|.
  209. //
  210. // If any of the above fail, the function returns NULL.
  211. //
  212. // This function does not reject trailing data in the input. This allows the
  213. // caller to parse a sequence of concatenated structures. Callers parsing only
  214. // one structure should check for trailing data by comparing the updated |*inp|
  215. // with the end of the input.
  216. //
  217. // WARNING: Callers should not rely on the in-place update mode. It often
  218. // produces the wrong result or breaks the type's internal invariants. Future
  219. // revisions of BoringSSL may standardize on the |d2i_SAMPLE| behavior.
  220. SAMPLE *d2i_SAMPLE_with_reuse(SAMPLE **out, const uint8_t **inp, long len);
  221. // i2d_SAMPLE marshals |in|. On error, it returns a negative value. On success,
  222. // it returns the length of the result and outputs it via |outp| as follows:
  223. //
  224. // If |outp| is NULL, the function writes nothing. This mode can be used to size
  225. // buffers.
  226. //
  227. // If |outp| is non-NULL but |*outp| is NULL, the function sets |*outp| to a
  228. // newly-allocated buffer containing the result. The caller is responsible for
  229. // releasing |*outp| with |OPENSSL_free|. This mode is recommended for most
  230. // callers.
  231. //
  232. // If |outp| and |*outp| are non-NULL, the function writes the result to
  233. // |*outp|, which must have enough space available, and advances |*outp| just
  234. // past the output.
  235. //
  236. // WARNING: In the third mode, the function does not internally check output
  237. // bounds. Failing to correctly size the buffer will result in a potentially
  238. // exploitable memory error.
  239. int i2d_SAMPLE(const SAMPLE *in, uint8_t **outp);
  240. #endif // Sample functions
  241. // The following typedefs are sometimes used for pointers to functions like
  242. // |d2i_SAMPLE| and |i2d_SAMPLE|. Note, however, that these act on |void*|.
  243. // Calling a function with a different pointer type is undefined in C, so this
  244. // is only valid with a wrapper.
  245. typedef void *d2i_of_void(void **, const unsigned char **, long);
  246. typedef int i2d_of_void(const void *, unsigned char **);
  247. // ASN.1 types.
  248. //
  249. // An |ASN1_ITEM| represents an ASN.1 type and allows working with ASN.1 types
  250. // generically.
  251. //
  252. // |ASN1_ITEM|s use a different namespace from C types and are accessed via
  253. // |ASN1_ITEM_*| macros. So, for example, |ASN1_OCTET_STRING| is both a C type
  254. // and the name of an |ASN1_ITEM|, referenced as
  255. // |ASN1_ITEM_rptr(ASN1_OCTET_STRING)|.
  256. //
  257. // Each |ASN1_ITEM| has a corresponding C type, typically with the same name,
  258. // which represents values in the ASN.1 type. This type is either a pointer type
  259. // or |ASN1_BOOLEAN|. When it is a pointer, NULL pointers represent omitted
  260. // values. For example, an OCTET STRING value is declared with the C type
  261. // |ASN1_OCTET_STRING*| and uses the |ASN1_ITEM| named |ASN1_OCTET_STRING|. An
  262. // OPTIONAL OCTET STRING uses the same C type and represents an omitted value
  263. // with a NULL pointer. |ASN1_BOOLEAN| is described in a later section.
  264. // DECLARE_ASN1_ITEM declares an |ASN1_ITEM| with name |name|. The |ASN1_ITEM|
  265. // may be referenced with |ASN1_ITEM_rptr|. Uses of this macro should document
  266. // the corresponding ASN.1 and C types.
  267. #define DECLARE_ASN1_ITEM(name) extern OPENSSL_EXPORT const ASN1_ITEM name##_it;
  268. // ASN1_ITEM_rptr returns the |const ASN1_ITEM *| named |name|.
  269. #define ASN1_ITEM_rptr(name) (&(name##_it))
  270. // ASN1_ITEM_EXP is an abstraction for referencing an |ASN1_ITEM| in a
  271. // constant-initialized structure, such as a method table. It exists because, on
  272. // some OpenSSL platforms, |ASN1_ITEM| references are indirected through
  273. // functions. Structures reference the |ASN1_ITEM| by declaring a field like
  274. // |ASN1_ITEM_EXP *item| and initializing it with |ASN1_ITEM_ref|.
  275. typedef const ASN1_ITEM ASN1_ITEM_EXP;
  276. // ASN1_ITEM_ref returns an |ASN1_ITEM_EXP*| for the |ASN1_ITEM| named |name|.
  277. #define ASN1_ITEM_ref(name) (&(name##_it))
  278. // ASN1_ITEM_ptr converts |iptr|, which must be an |ASN1_ITEM_EXP*| to a
  279. // |const ASN1_ITEM*|.
  280. #define ASN1_ITEM_ptr(iptr) (iptr)
  281. // ASN1_VALUE_st (aka |ASN1_VALUE|) is an opaque type used as a placeholder for
  282. // the C type corresponding to an |ASN1_ITEM|.
  283. typedef struct ASN1_VALUE_st ASN1_VALUE;
  284. // ASN1_item_new allocates a new value of the C type corresponding to |it|, or
  285. // NULL on error. On success, the caller must release the value with
  286. // |ASN1_item_free|, or the corresponding C type's free function, when done. The
  287. // new value will initialize fields of the value to some default state, such as
  288. // an empty string. Note, however, that this default state sometimes omits
  289. // required values, such as with CHOICE types.
  290. //
  291. // This function may not be used with |ASN1_ITEM|s whose C type is
  292. // |ASN1_BOOLEAN|.
  293. //
  294. // WARNING: Casting the result of this function to the wrong type is a
  295. // potentially exploitable memory error. Callers must ensure the value is used
  296. // consistently with |it|. Prefer using type-specific functions such as
  297. // |ASN1_OCTET_STRING_new|.
  298. OPENSSL_EXPORT ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it);
  299. // ASN1_item_free releases memory associated with |val|, which must be an object
  300. // of the C type corresponding to |it|.
  301. //
  302. // This function may not be used with |ASN1_ITEM|s whose C type is
  303. // |ASN1_BOOLEAN|.
  304. //
  305. // WARNING: Passing a pointer of the wrong type into this function is a
  306. // potentially exploitable memory error. Callers must ensure |val| is consistent
  307. // with |it|. Prefer using type-specific functions such as
  308. // |ASN1_OCTET_STRING_free|.
  309. OPENSSL_EXPORT void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it);
  310. // ASN1_item_d2i parses the ASN.1 type |it| from up to |len| bytes at |*inp|.
  311. // It behaves like |d2i_SAMPLE_with_reuse|, except that |out| and the return
  312. // value are cast to |ASN1_VALUE| pointers.
  313. //
  314. // TODO(https://crbug.com/boringssl/444): C strict aliasing forbids type-punning
  315. // |T*| and |ASN1_VALUE*| the way this function signature does. When that bug is
  316. // resolved, we will need to pick which type |*out| is (probably |T*|). Do not
  317. // use a non-NULL |out| to avoid ending up on the wrong side of this question.
  318. //
  319. // This function may not be used with |ASN1_ITEM|s whose C type is
  320. // |ASN1_BOOLEAN|.
  321. //
  322. // WARNING: Casting the result of this function to the wrong type, or passing a
  323. // pointer of the wrong type into this function, are potentially exploitable
  324. // memory errors. Callers must ensure |out| is consistent with |it|. Prefer
  325. // using type-specific functions such as |d2i_ASN1_OCTET_STRING|.
  326. OPENSSL_EXPORT ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **out,
  327. const unsigned char **inp, long len,
  328. const ASN1_ITEM *it);
  329. // ASN1_item_i2d marshals |val| as the ASN.1 type associated with |it|, as
  330. // described in |i2d_SAMPLE|.
  331. //
  332. // This function may not be used with |ASN1_ITEM|s whose C type is
  333. // |ASN1_BOOLEAN|.
  334. //
  335. // WARNING: Passing a pointer of the wrong type into this function is a
  336. // potentially exploitable memory error. Callers must ensure |val| is consistent
  337. // with |it|. Prefer using type-specific functions such as
  338. // |i2d_ASN1_OCTET_STRING|.
  339. OPENSSL_EXPORT int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **outp,
  340. const ASN1_ITEM *it);
  341. // ASN1_item_dup returns a newly-allocated copy of |x|, or NULL on error. |x|
  342. // must be an object of |it|'s C type.
  343. //
  344. // This function may not be used with |ASN1_ITEM|s whose C type is
  345. // |ASN1_BOOLEAN|.
  346. //
  347. // WARNING: Casting the result of this function to the wrong type, or passing a
  348. // pointer of the wrong type into this function, are potentially exploitable
  349. // memory errors. Prefer using type-specific functions such as
  350. // |ASN1_STRING_dup|.
  351. OPENSSL_EXPORT void *ASN1_item_dup(const ASN1_ITEM *it, void *x);
  352. // The following functions behave like |ASN1_item_d2i| but read from |in|
  353. // instead. |out| is the same parameter as in |ASN1_item_d2i|, but written with
  354. // |void*| instead. The return values similarly match.
  355. //
  356. // These functions may not be used with |ASN1_ITEM|s whose C type is
  357. // |ASN1_BOOLEAN|.
  358. //
  359. // WARNING: These functions do not bound how much data is read from |in|.
  360. // Parsing an untrusted input could consume unbounded memory.
  361. OPENSSL_EXPORT void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *out);
  362. OPENSSL_EXPORT void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *out);
  363. // The following functions behave like |ASN1_item_i2d| but write to |out|
  364. // instead. |in| is the same parameter as in |ASN1_item_i2d|, but written with
  365. // |void*| instead.
  366. //
  367. // These functions may not be used with |ASN1_ITEM|s whose C type is
  368. // |ASN1_BOOLEAN|.
  369. OPENSSL_EXPORT int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, void *in);
  370. OPENSSL_EXPORT int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *in);
  371. // ASN1_item_unpack parses |oct|'s contents as |it|'s ASN.1 type. It returns a
  372. // newly-allocated instance of |it|'s C type on success, or NULL on error.
  373. //
  374. // This function may not be used with |ASN1_ITEM|s whose C type is
  375. // |ASN1_BOOLEAN|.
  376. //
  377. // WARNING: Casting the result of this function to the wrong type is a
  378. // potentially exploitable memory error. Callers must ensure the value is used
  379. // consistently with |it|.
  380. OPENSSL_EXPORT void *ASN1_item_unpack(const ASN1_STRING *oct,
  381. const ASN1_ITEM *it);
  382. // ASN1_item_pack marshals |obj| as |it|'s ASN.1 type. If |out| is NULL, it
  383. // returns a newly-allocated |ASN1_STRING| with the result, or NULL on error.
  384. // If |out| is non-NULL, but |*out| is NULL, it does the same but additionally
  385. // sets |*out| to the result. If both |out| and |*out| are non-NULL, it writes
  386. // the result to |*out| and returns |*out| on success or NULL on error.
  387. //
  388. // This function may not be used with |ASN1_ITEM|s whose C type is
  389. // |ASN1_BOOLEAN|.
  390. //
  391. // WARNING: Passing a pointer of the wrong type into this function is a
  392. // potentially exploitable memory error. Callers must ensure |val| is consistent
  393. // with |it|.
  394. OPENSSL_EXPORT ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it,
  395. ASN1_STRING **out);
  396. // Booleans.
  397. //
  398. // This library represents ASN.1 BOOLEAN values with |ASN1_BOOLEAN|, which is an
  399. // integer type. FALSE is zero, TRUE is 0xff, and an omitted OPTIONAL BOOLEAN is
  400. // -1.
  401. // d2i_ASN1_BOOLEAN parses a DER-encoded ASN.1 BOOLEAN from up to |len| bytes at
  402. // |*inp|. On success, it advances |*inp| by the number of bytes read and
  403. // returns the result. If |out| is non-NULL, it additionally writes the result
  404. // to |*out|. On error, it returns -1.
  405. //
  406. // This function does not reject trailing data in the input. This allows the
  407. // caller to parse a sequence of concatenated structures. Callers parsing only
  408. // one structure should check for trailing data by comparing the updated |*inp|
  409. // with the end of the input.
  410. //
  411. // WARNING: This function's is slightly different from other |d2i_*| functions
  412. // because |ASN1_BOOLEAN| is not a pointer type.
  413. //
  414. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  415. // BER, but this will be removed in the future.
  416. OPENSSL_EXPORT ASN1_BOOLEAN d2i_ASN1_BOOLEAN(ASN1_BOOLEAN *out,
  417. const unsigned char **inp,
  418. long len);
  419. // i2d_ASN1_BOOLEAN marshals |a| as a DER-encoded ASN.1 BOOLEAN, as described in
  420. // |i2d_SAMPLE|.
  421. OPENSSL_EXPORT int i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a, unsigned char **outp);
  422. // The following |ASN1_ITEM|s have ASN.1 type BOOLEAN and C type |ASN1_BOOLEAN|.
  423. // |ASN1_TBOOLEAN| and |ASN1_FBOOLEAN| must be marked OPTIONAL. When omitted,
  424. // they are parsed as TRUE and FALSE, respectively, rather than -1.
  425. DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
  426. DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
  427. DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
  428. // Strings.
  429. //
  430. // ASN.1 contains a myriad of string types, as well as types that contain data
  431. // that may be encoded into a string. This library uses a single type,
  432. // |ASN1_STRING|, to represent most values.
  433. // An asn1_string_st (aka |ASN1_STRING|) represents a value of a string-like
  434. // ASN.1 type. It contains a type field, and a byte string data field with a
  435. // type-specific representation.
  436. //
  437. // When representing a string value, the type field is one of
  438. // |V_ASN1_OCTET_STRING|, |V_ASN1_UTF8STRING|, |V_ASN1_NUMERICSTRING|,
  439. // |V_ASN1_PRINTABLESTRING|, |V_ASN1_T61STRING|, |V_ASN1_VIDEOTEXSTRING|,
  440. // |V_ASN1_IA5STRING|, |V_ASN1_GRAPHICSTRING|, |V_ASN1_ISO64STRING|,
  441. // |V_ASN1_VISIBLESTRING|, |V_ASN1_GENERALSTRING|, |V_ASN1_UNIVERSALSTRING|, or
  442. // |V_ASN1_BMPSTRING|. The data contains the byte representation of of the
  443. // string.
  444. //
  445. // When representing a BIT STRING value, the type field is |V_ASN1_BIT_STRING|.
  446. // See bit string documentation below for how the data and flags are used.
  447. //
  448. // When representing an INTEGER or ENUMERATED value, the type field is one of
  449. // |V_ASN1_INTEGER|, |V_ASN1_NEG_INTEGER|, |V_ASN1_ENUMERATED|, or
  450. // |V_ASN1_NEG_ENUMERATED|. See integer documentation below for details.
  451. //
  452. // When representing a GeneralizedTime or UTCTime value, the type field is
  453. // |V_ASN1_GENERALIZEDTIME| or |V_ASN1_UTCTIME|, respectively. The data contains
  454. // the DER encoding of the value. For example, the UNIX epoch would be
  455. // "19700101000000Z" for a GeneralizedTime and "700101000000Z" for a UTCTime.
  456. //
  457. // |ASN1_STRING|, when stored in an |ASN1_TYPE|, may also represent an element
  458. // with tag not directly supported by this library. See |ASN1_TYPE| for details.
  459. //
  460. // |ASN1_STRING| additionally has the following typedefs: |ASN1_BIT_STRING|,
  461. // |ASN1_BMPSTRING|, |ASN1_ENUMERATED|, |ASN1_GENERALIZEDTIME|,
  462. // |ASN1_GENERALSTRING|, |ASN1_IA5STRING|, |ASN1_INTEGER|, |ASN1_OCTET_STRING|,
  463. // |ASN1_PRINTABLESTRING|, |ASN1_T61STRING|, |ASN1_TIME|,
  464. // |ASN1_UNIVERSALSTRING|, |ASN1_UTCTIME|, |ASN1_UTF8STRING|, and
  465. // |ASN1_VISIBLESTRING|. Other than |ASN1_TIME|, these correspond to universal
  466. // ASN.1 types. |ASN1_TIME| represents a CHOICE of UTCTime and GeneralizedTime,
  467. // with a cutoff of 2049, as used in Section 4.1.2.5 of RFC 5280.
  468. //
  469. // For clarity, callers are encouraged to use the appropriate typedef when
  470. // available. They are the same type as |ASN1_STRING|, so a caller may freely
  471. // pass them into functions expecting |ASN1_STRING|, such as
  472. // |ASN1_STRING_length|.
  473. //
  474. // If a function returns an |ASN1_STRING| where the typedef or ASN.1 structure
  475. // implies constraints on the type field, callers may assume that the type field
  476. // is correct. However, if a function takes an |ASN1_STRING| as input, callers
  477. // must ensure the type field matches. These invariants are not captured by the
  478. // C type system and may not be checked at runtime. For example, callers may
  479. // assume the output of |X509_get0_serialNumber| has type |V_ASN1_INTEGER| or
  480. // |V_ASN1_NEG_INTEGER|. Callers must not pass a string of type
  481. // |V_ASN1_OCTET_STRING| to |X509_set_serialNumber|. Doing so may break
  482. // invariants on the |X509| object and break the |X509_get0_serialNumber|
  483. // invariant.
  484. //
  485. // TODO(https://crbug.com/boringssl/445): This is very unfriendly. Getting the
  486. // type field wrong should not cause memory errors, but it may do strange
  487. // things. We should add runtime checks to anything that consumes |ASN1_STRING|s
  488. // from the caller.
  489. struct asn1_string_st {
  490. int length;
  491. int type;
  492. unsigned char *data;
  493. long flags;
  494. };
  495. // ASN1_STRING_FLAG_BITS_LEFT indicates, in a BIT STRING |ASN1_STRING|, that
  496. // flags & 0x7 contains the number of padding bits added to the BIT STRING
  497. // value. When not set, all trailing zero bits in the last byte are implicitly
  498. // treated as padding. This behavior is deprecated and should not be used.
  499. #define ASN1_STRING_FLAG_BITS_LEFT 0x08
  500. // ASN1_STRING_type_new returns a newly-allocated empty |ASN1_STRING| object of
  501. // type |type|, or NULL on error.
  502. OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_type_new(int type);
  503. // ASN1_STRING_new returns a newly-allocated empty |ASN1_STRING| object with an
  504. // arbitrary type. Prefer one of the type-specific constructors, such as
  505. // |ASN1_OCTET_STRING_new|, or |ASN1_STRING_type_new|.
  506. OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_new(void);
  507. // ASN1_STRING_free releases memory associated with |str|.
  508. OPENSSL_EXPORT void ASN1_STRING_free(ASN1_STRING *str);
  509. // ASN1_STRING_copy sets |dst| to a copy of |str|. It returns one on success and
  510. // zero on error.
  511. OPENSSL_EXPORT int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str);
  512. // ASN1_STRING_dup returns a newly-allocated copy of |str|, or NULL on error.
  513. OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str);
  514. // ASN1_STRING_type returns the type of |str|. This value will be one of the
  515. // |V_ASN1_*| constants.
  516. OPENSSL_EXPORT int ASN1_STRING_type(const ASN1_STRING *str);
  517. // ASN1_STRING_get0_data returns a pointer to |str|'s contents. Callers should
  518. // use |ASN1_STRING_length| to determine the length of the string. The string
  519. // may have embedded NUL bytes and may not be NUL-terminated.
  520. OPENSSL_EXPORT const unsigned char *ASN1_STRING_get0_data(
  521. const ASN1_STRING *str);
  522. // ASN1_STRING_data returns a mutable pointer to |str|'s contents. Callers
  523. // should use |ASN1_STRING_length| to determine the length of the string. The
  524. // string may have embedded NUL bytes and may not be NUL-terminated.
  525. //
  526. // Prefer |ASN1_STRING_get0_data|.
  527. OPENSSL_EXPORT unsigned char *ASN1_STRING_data(ASN1_STRING *str);
  528. // ASN1_STRING_length returns the length of |str|, in bytes.
  529. OPENSSL_EXPORT int ASN1_STRING_length(const ASN1_STRING *str);
  530. // ASN1_STRING_cmp compares |a| and |b|'s type and contents. It returns an
  531. // integer equal to, less than, or greater than zero if |a| is equal to, less
  532. // than, or greater than |b|, respectively. This function compares by length,
  533. // then data, then type. Note the data compared is the |ASN1_STRING| internal
  534. // representation and the type order is arbitrary. While this comparison is
  535. // suitable for sorting, callers should not rely on the exact order when |a|
  536. // and |b| are different types.
  537. //
  538. // Note that, if |a| and |b| are INTEGERs, this comparison does not order the
  539. // values numerically. For a numerical comparison, use |ASN1_INTEGER_cmp|.
  540. OPENSSL_EXPORT int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b);
  541. // ASN1_STRING_set sets the contents of |str| to a copy of |len| bytes from
  542. // |data|. It returns one on success and zero on error. If |data| is NULL, it
  543. // updates the length and allocates the buffer as needed, but does not
  544. // initialize the contents.
  545. OPENSSL_EXPORT int ASN1_STRING_set(ASN1_STRING *str, const void *data, int len);
  546. // ASN1_STRING_set0 sets the contents of |str| to |len| bytes from |data|. It
  547. // takes ownership of |data|, which must have been allocated with
  548. // |OPENSSL_malloc|.
  549. OPENSSL_EXPORT void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len);
  550. // The following functions call |ASN1_STRING_type_new| with the corresponding
  551. // |V_ASN1_*| constant.
  552. OPENSSL_EXPORT ASN1_BMPSTRING *ASN1_BMPSTRING_new(void);
  553. OPENSSL_EXPORT ASN1_GENERALSTRING *ASN1_GENERALSTRING_new(void);
  554. OPENSSL_EXPORT ASN1_IA5STRING *ASN1_IA5STRING_new(void);
  555. OPENSSL_EXPORT ASN1_OCTET_STRING *ASN1_OCTET_STRING_new(void);
  556. OPENSSL_EXPORT ASN1_PRINTABLESTRING *ASN1_PRINTABLESTRING_new(void);
  557. OPENSSL_EXPORT ASN1_T61STRING *ASN1_T61STRING_new(void);
  558. OPENSSL_EXPORT ASN1_UNIVERSALSTRING *ASN1_UNIVERSALSTRING_new(void);
  559. OPENSSL_EXPORT ASN1_UTF8STRING *ASN1_UTF8STRING_new(void);
  560. OPENSSL_EXPORT ASN1_VISIBLESTRING *ASN1_VISIBLESTRING_new(void);
  561. // The following functions call |ASN1_STRING_free|.
  562. OPENSSL_EXPORT void ASN1_BMPSTRING_free(ASN1_BMPSTRING *str);
  563. OPENSSL_EXPORT void ASN1_GENERALSTRING_free(ASN1_GENERALSTRING *str);
  564. OPENSSL_EXPORT void ASN1_IA5STRING_free(ASN1_IA5STRING *str);
  565. OPENSSL_EXPORT void ASN1_OCTET_STRING_free(ASN1_OCTET_STRING *str);
  566. OPENSSL_EXPORT void ASN1_PRINTABLESTRING_free(ASN1_PRINTABLESTRING *str);
  567. OPENSSL_EXPORT void ASN1_T61STRING_free(ASN1_T61STRING *str);
  568. OPENSSL_EXPORT void ASN1_UNIVERSALSTRING_free(ASN1_UNIVERSALSTRING *str);
  569. OPENSSL_EXPORT void ASN1_UTF8STRING_free(ASN1_UTF8STRING *str);
  570. OPENSSL_EXPORT void ASN1_VISIBLESTRING_free(ASN1_VISIBLESTRING *str);
  571. // The following functions parse up to |len| bytes from |*inp| as a
  572. // DER-encoded ASN.1 value of the corresponding type, as described in
  573. // |d2i_SAMPLE_with_reuse|.
  574. //
  575. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  576. // BER, but this will be removed in the future.
  577. OPENSSL_EXPORT ASN1_BMPSTRING *d2i_ASN1_BMPSTRING(ASN1_BMPSTRING **out,
  578. const uint8_t **inp,
  579. long len);
  580. OPENSSL_EXPORT ASN1_GENERALSTRING *d2i_ASN1_GENERALSTRING(
  581. ASN1_GENERALSTRING **out, const uint8_t **inp, long len);
  582. OPENSSL_EXPORT ASN1_IA5STRING *d2i_ASN1_IA5STRING(ASN1_IA5STRING **out,
  583. const uint8_t **inp,
  584. long len);
  585. OPENSSL_EXPORT ASN1_OCTET_STRING *d2i_ASN1_OCTET_STRING(ASN1_OCTET_STRING **out,
  586. const uint8_t **inp,
  587. long len);
  588. OPENSSL_EXPORT ASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING(
  589. ASN1_PRINTABLESTRING **out, const uint8_t **inp, long len);
  590. OPENSSL_EXPORT ASN1_T61STRING *d2i_ASN1_T61STRING(ASN1_T61STRING **out,
  591. const uint8_t **inp,
  592. long len);
  593. OPENSSL_EXPORT ASN1_UNIVERSALSTRING *d2i_ASN1_UNIVERSALSTRING(
  594. ASN1_UNIVERSALSTRING **out, const uint8_t **inp, long len);
  595. OPENSSL_EXPORT ASN1_UTF8STRING *d2i_ASN1_UTF8STRING(ASN1_UTF8STRING **out,
  596. const uint8_t **inp,
  597. long len);
  598. OPENSSL_EXPORT ASN1_VISIBLESTRING *d2i_ASN1_VISIBLESTRING(
  599. ASN1_VISIBLESTRING **out, const uint8_t **inp, long len);
  600. // The following functions marshal |in| as a DER-encoded ASN.1 value of the
  601. // corresponding type, as described in |i2d_SAMPLE|.
  602. OPENSSL_EXPORT int i2d_ASN1_BMPSTRING(const ASN1_BMPSTRING *in, uint8_t **outp);
  603. OPENSSL_EXPORT int i2d_ASN1_GENERALSTRING(const ASN1_GENERALSTRING *in,
  604. uint8_t **outp);
  605. OPENSSL_EXPORT int i2d_ASN1_IA5STRING(const ASN1_IA5STRING *in, uint8_t **outp);
  606. OPENSSL_EXPORT int i2d_ASN1_OCTET_STRING(const ASN1_OCTET_STRING *in,
  607. uint8_t **outp);
  608. OPENSSL_EXPORT int i2d_ASN1_PRINTABLESTRING(const ASN1_PRINTABLESTRING *in,
  609. uint8_t **outp);
  610. OPENSSL_EXPORT int i2d_ASN1_T61STRING(const ASN1_T61STRING *in, uint8_t **outp);
  611. OPENSSL_EXPORT int i2d_ASN1_UNIVERSALSTRING(const ASN1_UNIVERSALSTRING *in,
  612. uint8_t **outp);
  613. OPENSSL_EXPORT int i2d_ASN1_UTF8STRING(const ASN1_UTF8STRING *in,
  614. uint8_t **outp);
  615. OPENSSL_EXPORT int i2d_ASN1_VISIBLESTRING(const ASN1_VISIBLESTRING *in,
  616. uint8_t **outp);
  617. // The following |ASN1_ITEM|s have the ASN.1 type referred to in their name and
  618. // C type |ASN1_STRING*|. The C type may also be written as the corresponding
  619. // typedef.
  620. DECLARE_ASN1_ITEM(ASN1_BMPSTRING)
  621. DECLARE_ASN1_ITEM(ASN1_GENERALSTRING)
  622. DECLARE_ASN1_ITEM(ASN1_IA5STRING)
  623. DECLARE_ASN1_ITEM(ASN1_OCTET_STRING)
  624. DECLARE_ASN1_ITEM(ASN1_PRINTABLESTRING)
  625. DECLARE_ASN1_ITEM(ASN1_T61STRING)
  626. DECLARE_ASN1_ITEM(ASN1_UNIVERSALSTRING)
  627. DECLARE_ASN1_ITEM(ASN1_UTF8STRING)
  628. DECLARE_ASN1_ITEM(ASN1_VISIBLESTRING)
  629. // ASN1_OCTET_STRING_dup calls |ASN1_STRING_dup|.
  630. OPENSSL_EXPORT ASN1_OCTET_STRING *ASN1_OCTET_STRING_dup(
  631. const ASN1_OCTET_STRING *a);
  632. // ASN1_OCTET_STRING_cmp calls |ASN1_STRING_cmp|.
  633. OPENSSL_EXPORT int ASN1_OCTET_STRING_cmp(const ASN1_OCTET_STRING *a,
  634. const ASN1_OCTET_STRING *b);
  635. // ASN1_OCTET_STRING_set calls |ASN1_STRING_set|.
  636. OPENSSL_EXPORT int ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *str,
  637. const unsigned char *data, int len);
  638. // ASN1_STRING_to_UTF8 converts |in| to UTF-8. On success, sets |*out| to a
  639. // newly-allocated buffer containing the resulting string and returns the length
  640. // of the string. The caller must call |OPENSSL_free| to release |*out| when
  641. // done. On error, it returns a negative number.
  642. OPENSSL_EXPORT int ASN1_STRING_to_UTF8(unsigned char **out,
  643. const ASN1_STRING *in);
  644. // The following formats define encodings for use with functions like
  645. // |ASN1_mbstring_copy|. Note |MBSTRING_ASC| refers to Latin-1, not ASCII.
  646. #define MBSTRING_FLAG 0x1000
  647. #define MBSTRING_UTF8 (MBSTRING_FLAG)
  648. #define MBSTRING_ASC (MBSTRING_FLAG | 1)
  649. #define MBSTRING_BMP (MBSTRING_FLAG | 2)
  650. #define MBSTRING_UNIV (MBSTRING_FLAG | 4)
  651. // DIRSTRING_TYPE contains the valid string types in an X.509 DirectoryString.
  652. #define DIRSTRING_TYPE \
  653. (B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_BMPSTRING | \
  654. B_ASN1_UTF8STRING)
  655. // PKCS9STRING_TYPE contains the valid string types in a PKCS9String.
  656. #define PKCS9STRING_TYPE (DIRSTRING_TYPE | B_ASN1_IA5STRING)
  657. // ASN1_mbstring_copy converts |len| bytes from |in| to an ASN.1 string. If
  658. // |len| is -1, |in| must be NUL-terminated and the length is determined by
  659. // |strlen|. |in| is decoded according to |inform|, which must be one of
  660. // |MBSTRING_*|. |mask| determines the set of valid output types and is a
  661. // bitmask containing a subset of |B_ASN1_PRINTABLESTRING|, |B_ASN1_IA5STRING|,
  662. // |B_ASN1_T61STRING|, |B_ASN1_BMPSTRING|, |B_ASN1_UNIVERSALSTRING|, and
  663. // |B_ASN1_UTF8STRING|, in that preference order. This function chooses the
  664. // first output type in |mask| which can represent |in|. It interprets T61String
  665. // as Latin-1, rather than T.61.
  666. //
  667. // If |mask| is zero, |DIRSTRING_TYPE| is used by default.
  668. //
  669. // On success, this function returns the |V_ASN1_*| constant corresponding to
  670. // the selected output type and, if |out| and |*out| are both non-NULL, updates
  671. // the object at |*out| with the result. If |out| is non-NULL and |*out| is
  672. // NULL, it instead sets |*out| to a newly-allocated |ASN1_STRING| containing
  673. // the result. If |out| is NULL, it returns the selected output type without
  674. // constructing an |ASN1_STRING|. On error, this function returns -1.
  675. OPENSSL_EXPORT int ASN1_mbstring_copy(ASN1_STRING **out, const uint8_t *in,
  676. int len, int inform, unsigned long mask);
  677. // ASN1_mbstring_ncopy behaves like |ASN1_mbstring_copy| but returns an error if
  678. // the input is less than |minsize| or greater than |maxsize| codepoints long. A
  679. // |maxsize| value of zero is ignored. Note the sizes are measured in
  680. // codepoints, not output bytes.
  681. OPENSSL_EXPORT int ASN1_mbstring_ncopy(ASN1_STRING **out, const uint8_t *in,
  682. int len, int inform, unsigned long mask,
  683. long minsize, long maxsize);
  684. // ASN1_STRING_set_by_NID behaves like |ASN1_mbstring_ncopy|, but determines
  685. // |mask|, |minsize|, and |maxsize| based on |nid|. When |nid| is a recognized
  686. // X.509 attribute type, it will pick a suitable ASN.1 string type and bounds.
  687. // For most attribute types, it preferentially chooses UTF8String. If |nid| is
  688. // unrecognized, it uses UTF8String by default.
  689. //
  690. // Slightly unlike |ASN1_mbstring_ncopy|, this function interprets |out| and
  691. // returns its result as follows: If |out| is NULL, it returns a newly-allocated
  692. // |ASN1_STRING| containing the result. If |out| is non-NULL and
  693. // |*out| is NULL, it additionally sets |*out| to the result. If both |out| and
  694. // |*out| are non-NULL, it instead updates the object at |*out| and returns
  695. // |*out|. In all cases, it returns NULL on error.
  696. //
  697. // This function supports the following NIDs: |NID_countryName|,
  698. // |NID_dnQualifier|, |NID_domainComponent|, |NID_friendlyName|,
  699. // |NID_givenName|, |NID_initials|, |NID_localityName|, |NID_ms_csp_name|,
  700. // |NID_name|, |NID_organizationalUnitName|, |NID_organizationName|,
  701. // |NID_pkcs9_challengePassword|, |NID_pkcs9_emailAddress|,
  702. // |NID_pkcs9_unstructuredAddress|, |NID_pkcs9_unstructuredName|,
  703. // |NID_serialNumber|, |NID_stateOrProvinceName|, and |NID_surname|. Additional
  704. // NIDs may be registered with |ASN1_STRING_set_by_NID|, but it is recommended
  705. // to call |ASN1_mbstring_ncopy| directly instead.
  706. OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
  707. const unsigned char *in,
  708. int len, int inform,
  709. int nid);
  710. // STABLE_NO_MASK causes |ASN1_STRING_TABLE_add| to allow types other than
  711. // UTF8String.
  712. #define STABLE_NO_MASK 0x02
  713. // ASN1_STRING_TABLE_add registers the corresponding parameters with |nid|, for
  714. // use with |ASN1_STRING_set_by_NID|. It returns one on success and zero on
  715. // error. It is an error to call this function if |nid| is a built-in NID, or
  716. // was already registered by a previous call.
  717. //
  718. // WARNING: This function affects global state in the library. If two libraries
  719. // in the same address space register information for the same OID, one call
  720. // will fail. Prefer directly passing the desired parametrs to
  721. // |ASN1_mbstring_copy| or |ASN1_mbstring_ncopy| instead.
  722. OPENSSL_EXPORT int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize,
  723. unsigned long mask,
  724. unsigned long flags);
  725. // Multi-strings.
  726. //
  727. // A multi-string, or "MSTRING", is an |ASN1_STRING| that represents a CHOICE of
  728. // several string or string-like types, such as X.509's DirectoryString. The
  729. // |ASN1_STRING|'s type field determines which type is used.
  730. //
  731. // Multi-string types are associated with a bitmask, using the |B_ASN1_*|
  732. // constants, which defines which types are valid.
  733. // B_ASN1_DIRECTORYSTRING is a bitmask of types allowed in an X.509
  734. // DirectoryString (RFC 5280).
  735. #define B_ASN1_DIRECTORYSTRING \
  736. (B_ASN1_PRINTABLESTRING | B_ASN1_TELETEXSTRING | B_ASN1_BMPSTRING | \
  737. B_ASN1_UNIVERSALSTRING | B_ASN1_UTF8STRING)
  738. // DIRECTORYSTRING_new returns a newly-allocated |ASN1_STRING| with type -1, or
  739. // NULL on error. The resulting |ASN1_STRING| is not a valid X.509
  740. // DirectoryString until initialized with a value.
  741. OPENSSL_EXPORT ASN1_STRING *DIRECTORYSTRING_new(void);
  742. // DIRECTORYSTRING_free calls |ASN1_STRING_free|.
  743. OPENSSL_EXPORT void DIRECTORYSTRING_free(ASN1_STRING *str);
  744. // d2i_DIRECTORYSTRING parses up to |len| bytes from |*inp| as a DER-encoded
  745. // X.509 DirectoryString (RFC 5280), as described in |d2i_SAMPLE_with_reuse|.
  746. //
  747. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  748. // BER, but this will be removed in the future.
  749. //
  750. // TODO(https://crbug.com/boringssl/449): DirectoryString's non-empty string
  751. // requirement is not currently enforced.
  752. OPENSSL_EXPORT ASN1_STRING *d2i_DIRECTORYSTRING(ASN1_STRING **out,
  753. const uint8_t **inp, long len);
  754. // i2d_DIRECTORYSTRING marshals |in| as a DER-encoded X.509 DirectoryString (RFC
  755. // 5280), as described in |i2d_SAMPLE|.
  756. OPENSSL_EXPORT int i2d_DIRECTORYSTRING(const ASN1_STRING *in, uint8_t **outp);
  757. // DIRECTORYSTRING is an |ASN1_ITEM| whose ASN.1 type is X.509 DirectoryString
  758. // (RFC 5280) and C type is |ASN1_STRING*|.
  759. DECLARE_ASN1_ITEM(DIRECTORYSTRING)
  760. // B_ASN1_DISPLAYTEXT is a bitmask of types allowed in an X.509 DisplayText (RFC
  761. // 5280).
  762. #define B_ASN1_DISPLAYTEXT \
  763. (B_ASN1_IA5STRING | B_ASN1_VISIBLESTRING | B_ASN1_BMPSTRING | \
  764. B_ASN1_UTF8STRING)
  765. // DISPLAYTEXT_new returns a newly-allocated |ASN1_STRING| with type -1, or NULL
  766. // on error. The resulting |ASN1_STRING| is not a valid X.509 DisplayText until
  767. // initialized with a value.
  768. OPENSSL_EXPORT ASN1_STRING *DISPLAYTEXT_new(void);
  769. // DISPLAYTEXT_free calls |ASN1_STRING_free|.
  770. OPENSSL_EXPORT void DISPLAYTEXT_free(ASN1_STRING *str);
  771. // d2i_DISPLAYTEXT parses up to |len| bytes from |*inp| as a DER-encoded X.509
  772. // DisplayText (RFC 5280), as described in |d2i_SAMPLE_with_reuse|.
  773. //
  774. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  775. // BER, but this will be removed in the future.
  776. //
  777. // TODO(https://crbug.com/boringssl/449): DisplayText's size limits are not
  778. // currently enforced.
  779. OPENSSL_EXPORT ASN1_STRING *d2i_DISPLAYTEXT(ASN1_STRING **out,
  780. const uint8_t **inp, long len);
  781. // i2d_DISPLAYTEXT marshals |in| as a DER-encoded X.509 DisplayText (RFC 5280),
  782. // as described in |i2d_SAMPLE|.
  783. OPENSSL_EXPORT int i2d_DISPLAYTEXT(const ASN1_STRING *in, uint8_t **outp);
  784. // DISPLAYTEXT is an |ASN1_ITEM| whose ASN.1 type is X.509 DisplayText (RFC
  785. // 5280) and C type is |ASN1_STRING*|.
  786. DECLARE_ASN1_ITEM(DISPLAYTEXT)
  787. // Bit strings.
  788. //
  789. // An ASN.1 BIT STRING type represents a string of bits. The string may not
  790. // necessarily be a whole number of bytes. BIT STRINGs occur in ASN.1 structures
  791. // in several forms:
  792. //
  793. // Some BIT STRINGs represent a bitmask of named bits, such as the X.509 key
  794. // usage extension in RFC 5280, section 4.2.1.3. For such bit strings, DER
  795. // imposes an additional restriction that trailing zero bits are removed. Some
  796. // functions like |ASN1_BIT_STRING_set_bit| help in maintaining this.
  797. //
  798. // Other BIT STRINGs are arbitrary strings of bits used as identifiers and do
  799. // not have this constraint, such as the X.509 issuerUniqueID field.
  800. //
  801. // Finally, some structures use BIT STRINGs as a container for byte strings. For
  802. // example, the signatureValue field in X.509 and the subjectPublicKey field in
  803. // SubjectPublicKeyInfo are defined as BIT STRINGs with a value specific to the
  804. // AlgorithmIdentifier. While some unknown algorithm could choose to store
  805. // arbitrary bit strings, all supported algorithms use a byte string, with bit
  806. // order matching the DER encoding. Callers interpreting a BIT STRING as a byte
  807. // string should use |ASN1_BIT_STRING_num_bytes| instead of |ASN1_STRING_length|
  808. // and reject bit strings that are not a whole number of bytes.
  809. //
  810. // This library represents BIT STRINGs as |ASN1_STRING|s with type
  811. // |V_ASN1_BIT_STRING|. The data contains the encoded form of the BIT STRING,
  812. // including any padding bits added to round to a whole number of bytes, but
  813. // excluding the leading byte containing the number of padding bits. If
  814. // |ASN1_STRING_FLAG_BITS_LEFT| is set, the bottom three bits contains the
  815. // number of padding bits. For example, DER encodes the BIT STRING {1, 0} as
  816. // {0x06, 0x80 = 0b10_000000}. The |ASN1_STRING| representation has data of
  817. // {0x80} and flags of ASN1_STRING_FLAG_BITS_LEFT | 6. If
  818. // |ASN1_STRING_FLAG_BITS_LEFT| is unset, trailing zero bits are implicitly
  819. // removed. Callers should not rely this representation when constructing bit
  820. // strings. The padding bits in the |ASN1_STRING| data must be zero.
  821. // ASN1_BIT_STRING_new calls |ASN1_STRING_type_new| with |V_ASN1_BIT_STRING|.
  822. OPENSSL_EXPORT ASN1_BIT_STRING *ASN1_BIT_STRING_new(void);
  823. // ASN1_BIT_STRING_free calls |ASN1_STRING_free|.
  824. OPENSSL_EXPORT void ASN1_BIT_STRING_free(ASN1_BIT_STRING *str);
  825. // d2i_ASN1_BIT_STRING parses up to |len| bytes from |*inp| as a DER-encoded
  826. // ASN.1 BIT STRING, as described in |d2i_SAMPLE_with_reuse|.
  827. //
  828. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  829. // BER, but this will be removed in the future.
  830. OPENSSL_EXPORT ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **out,
  831. const uint8_t **inp,
  832. long len);
  833. // i2d_ASN1_BIT_STRING marshals |in| as a DER-encoded ASN.1 BIT STRING, as
  834. // described in |i2d_SAMPLE|.
  835. OPENSSL_EXPORT int i2d_ASN1_BIT_STRING(const ASN1_BIT_STRING *in,
  836. uint8_t **outp);
  837. // c2i_ASN1_BIT_STRING decodes |len| bytes from |*inp| as the contents of a
  838. // DER-encoded BIT STRING, excluding the tag and length. It behaves like
  839. // |d2i_SAMPLE_with_reuse| except, on success, it always consumes all |len|
  840. // bytes.
  841. //
  842. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  843. // BER, but this will be removed in the future.
  844. OPENSSL_EXPORT ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **out,
  845. const uint8_t **inp,
  846. long len);
  847. // i2c_ASN1_BIT_STRING encodes |in| as the contents of a DER-encoded BIT STRING,
  848. // excluding the tag and length. If |outp| is non-NULL, it writes the result to
  849. // |*outp|, advances |*outp| just past the output, and returns the number of
  850. // bytes written. |*outp| must have space available for the result. If |outp| is
  851. // NULL, it returns the number of bytes without writing anything. On error, it
  852. // returns a value <= 0.
  853. //
  854. // Note this function differs slightly from |i2d_SAMPLE|. If |outp| is non-NULL
  855. // and |*outp| is NULL, it does not allocate a new buffer.
  856. //
  857. // TODO(davidben): This function currently returns zero on error instead of -1,
  858. // but it is also mostly infallible. I've currently documented <= 0 to suggest
  859. // callers work with both.
  860. OPENSSL_EXPORT int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *in,
  861. uint8_t **outp);
  862. // ASN1_BIT_STRING is an |ASN1_ITEM| with ASN.1 type BIT STRING and C type
  863. // |ASN1_BIT_STRING*|.
  864. DECLARE_ASN1_ITEM(ASN1_BIT_STRING)
  865. // ASN1_BIT_STRING_num_bytes computes the length of |str| in bytes. If |str|'s
  866. // bit length is a multiple of 8, it sets |*out| to the byte length and returns
  867. // one. Otherwise, it returns zero.
  868. //
  869. // This function may be used with |ASN1_STRING_get0_data| to interpret |str| as
  870. // a byte string.
  871. OPENSSL_EXPORT int ASN1_BIT_STRING_num_bytes(const ASN1_BIT_STRING *str,
  872. size_t *out);
  873. // ASN1_BIT_STRING_set calls |ASN1_STRING_set|. It leaves flags unchanged, so
  874. // the caller must set the number of unused bits.
  875. //
  876. // TODO(davidben): Maybe it should? Wrapping a byte string in a bit string is a
  877. // common use case.
  878. OPENSSL_EXPORT int ASN1_BIT_STRING_set(ASN1_BIT_STRING *str,
  879. const unsigned char *d, int length);
  880. // ASN1_BIT_STRING_set_bit sets bit |n| of |str| to one if |value| is non-zero
  881. // and zero if |value| is zero, resizing |str| as needed. It then truncates
  882. // trailing zeros in |str| to align with the DER represention for a bit string
  883. // with named bits. It returns one on success and zero on error. |n| is indexed
  884. // beginning from zero.
  885. OPENSSL_EXPORT int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *str, int n,
  886. int value);
  887. // ASN1_BIT_STRING_get_bit returns one if bit |n| of |a| is in bounds and set,
  888. // and zero otherwise. |n| is indexed beginning from zero.
  889. OPENSSL_EXPORT int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *str, int n);
  890. // ASN1_BIT_STRING_check returns one if |str| only contains bits that are set in
  891. // the |flags_len| bytes pointed by |flags|. Otherwise it returns zero. Bits in
  892. // |flags| are arranged according to the DER representation, so bit 0
  893. // corresponds to the MSB of |flags[0]|.
  894. OPENSSL_EXPORT int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *str,
  895. const unsigned char *flags,
  896. int flags_len);
  897. // Integers and enumerated values.
  898. //
  899. // INTEGER and ENUMERATED values are represented as |ASN1_STRING|s where the
  900. // data contains the big-endian encoding of the absolute value of the integer.
  901. // The sign bit is encoded in the type: non-negative values have a type of
  902. // |V_ASN1_INTEGER| or |V_ASN1_ENUMERATED|, while negative values have a type of
  903. // |V_ASN1_NEG_INTEGER| or |V_ASN1_NEG_ENUMERATED|. Note this differs from DER's
  904. // two's complement representation.
  905. //
  906. // The data in the |ASN1_STRING| may not have leading zeros. Note this means
  907. // zero is represented as the empty string. Parsing functions will never return
  908. // invalid representations. If an invalid input is constructed, the marshaling
  909. // functions will skip leading zeros, however other functions, such as
  910. // |ASN1_INTEGER_cmp| or |ASN1_INTEGER_get|, may not return the correct result.
  911. DEFINE_STACK_OF(ASN1_INTEGER)
  912. // ASN1_INTEGER_new calls |ASN1_STRING_type_new| with |V_ASN1_INTEGER|. The
  913. // resulting object has value zero.
  914. OPENSSL_EXPORT ASN1_INTEGER *ASN1_INTEGER_new(void);
  915. // ASN1_INTEGER_free calls |ASN1_STRING_free|.
  916. OPENSSL_EXPORT void ASN1_INTEGER_free(ASN1_INTEGER *str);
  917. // ASN1_INTEGER_dup calls |ASN1_STRING_dup|.
  918. OPENSSL_EXPORT ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x);
  919. // d2i_ASN1_INTEGER parses up to |len| bytes from |*inp| as a DER-encoded
  920. // ASN.1 INTEGER, as described in |d2i_SAMPLE_with_reuse|.
  921. //
  922. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  923. // BER, but this will be removed in the future.
  924. OPENSSL_EXPORT ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **out,
  925. const uint8_t **inp, long len);
  926. // i2d_ASN1_INTEGER marshals |in| as a DER-encoded ASN.1 INTEGER, as
  927. // described in |i2d_SAMPLE|.
  928. OPENSSL_EXPORT int i2d_ASN1_INTEGER(const ASN1_INTEGER *in, uint8_t **outp);
  929. // c2i_ASN1_INTEGER decodes |len| bytes from |*inp| as the contents of a
  930. // DER-encoded INTEGER, excluding the tag and length. It behaves like
  931. // |d2i_SAMPLE_with_reuse| except, on success, it always consumes all |len|
  932. // bytes.
  933. //
  934. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  935. // some invalid inputs, but this will be removed in the future.
  936. OPENSSL_EXPORT ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **in,
  937. const uint8_t **outp, long len);
  938. // i2c_ASN1_INTEGER encodes |in| as the contents of a DER-encoded INTEGER,
  939. // excluding the tag and length. If |outp| is non-NULL, it writes the result to
  940. // |*outp|, advances |*outp| just past the output, and returns the number of
  941. // bytes written. |*outp| must have space available for the result. If |outp| is
  942. // NULL, it returns the number of bytes without writing anything. On error, it
  943. // returns a value <= 0.
  944. //
  945. // Note this function differs slightly from |i2d_SAMPLE|. If |outp| is non-NULL
  946. // and |*outp| is NULL, it does not allocate a new buffer.
  947. //
  948. // TODO(davidben): This function currently returns zero on error instead of -1,
  949. // but it is also mostly infallible. I've currently documented <= 0 to suggest
  950. // callers work with both.
  951. OPENSSL_EXPORT int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, uint8_t **outp);
  952. // ASN1_INTEGER is an |ASN1_ITEM| with ASN.1 type INTEGER and C type
  953. // |ASN1_INTEGER*|.
  954. DECLARE_ASN1_ITEM(ASN1_INTEGER)
  955. // ASN1_INTEGER_set_uint64 sets |a| to an INTEGER with value |v|. It returns one
  956. // on success and zero on error.
  957. OPENSSL_EXPORT int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v);
  958. // ASN1_INTEGER_set sets |a| to an INTEGER with value |v|. It returns one on
  959. // success and zero on error.
  960. OPENSSL_EXPORT int ASN1_INTEGER_set(ASN1_INTEGER *a, long v);
  961. // ASN1_INTEGER_get_uint64 converts |a| to a |uint64_t|. On success, it returns
  962. // one and sets |*out| to the result. If |a| did not fit or has the wrong type,
  963. // it returns zero.
  964. OPENSSL_EXPORT int ASN1_INTEGER_get_uint64(uint64_t *out,
  965. const ASN1_INTEGER *a);
  966. // ASN1_INTEGER_get returns the value of |a| as a |long|, or -1 if |a| is out of
  967. // range or the wrong type.
  968. //
  969. // WARNING: This function's return value cannot distinguish errors from -1.
  970. // Prefer |ASN1_INTEGER_get_uint64|.
  971. OPENSSL_EXPORT long ASN1_INTEGER_get(const ASN1_INTEGER *a);
  972. // BN_to_ASN1_INTEGER sets |ai| to an INTEGER with value |bn| and returns |ai|
  973. // on success or NULL or error. If |ai| is NULL, it returns a newly-allocated
  974. // |ASN1_INTEGER| on success instead, which the caller must release with
  975. // |ASN1_INTEGER_free|.
  976. OPENSSL_EXPORT ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn,
  977. ASN1_INTEGER *ai);
  978. // ASN1_INTEGER_to_BN sets |bn| to the value of |ai| and returns |bn| on success
  979. // or NULL or error. If |bn| is NULL, it returns a newly-allocated |BIGNUM| on
  980. // success instead, which the caller must release with |BN_free|.
  981. OPENSSL_EXPORT BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn);
  982. // ASN1_INTEGER_cmp compares the values of |x| and |y|. It returns an integer
  983. // equal to, less than, or greater than zero if |x| is equal to, less than, or
  984. // greater than |y|, respectively.
  985. OPENSSL_EXPORT int ASN1_INTEGER_cmp(const ASN1_INTEGER *x,
  986. const ASN1_INTEGER *y);
  987. // ASN1_ENUMERATED_new calls |ASN1_STRING_type_new| with |V_ASN1_ENUMERATED|.
  988. // The resulting object has value zero.
  989. OPENSSL_EXPORT ASN1_ENUMERATED *ASN1_ENUMERATED_new(void);
  990. // ASN1_ENUMERATED_free calls |ASN1_STRING_free|.
  991. OPENSSL_EXPORT void ASN1_ENUMERATED_free(ASN1_ENUMERATED *str);
  992. // d2i_ASN1_ENUMERATED parses up to |len| bytes from |*inp| as a DER-encoded
  993. // ASN.1 ENUMERATED, as described in |d2i_SAMPLE_with_reuse|.
  994. //
  995. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  996. // BER, but this will be removed in the future.
  997. OPENSSL_EXPORT ASN1_ENUMERATED *d2i_ASN1_ENUMERATED(ASN1_ENUMERATED **out,
  998. const uint8_t **inp,
  999. long len);
  1000. // i2d_ASN1_ENUMERATED marshals |in| as a DER-encoded ASN.1 ENUMERATED, as
  1001. // described in |i2d_SAMPLE|.
  1002. OPENSSL_EXPORT int i2d_ASN1_ENUMERATED(const ASN1_ENUMERATED *in,
  1003. uint8_t **outp);
  1004. // ASN1_ENUMERATED is an |ASN1_ITEM| with ASN.1 type ENUMERATED and C type
  1005. // |ASN1_ENUMERATED*|.
  1006. DECLARE_ASN1_ITEM(ASN1_ENUMERATED)
  1007. // ASN1_ENUMERATED_set_uint64 sets |a| to an ENUMERATED with value |v|. It
  1008. // returns one on success and zero on error.
  1009. OPENSSL_EXPORT int ASN1_ENUMERATED_set_uint64(ASN1_ENUMERATED *out, uint64_t v);
  1010. // ASN1_ENUMERATED_set sets |a| to an ENUMERATED with value |v|. It returns one
  1011. // on success and zero on error.
  1012. OPENSSL_EXPORT int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v);
  1013. // ASN1_ENUMERATED_get_uint64 converts |a| to a |uint64_t|. On success, it
  1014. // returns one and sets |*out| to the result. If |a| did not fit or has the
  1015. // wrong type, it returns zero.
  1016. OPENSSL_EXPORT int ASN1_ENUMERATED_get_uint64(uint64_t *out,
  1017. const ASN1_ENUMERATED *a);
  1018. // ASN1_ENUMERATED_get returns the value of |a| as a |long|, or -1 if |a| is out
  1019. // of range or the wrong type.
  1020. //
  1021. // WARNING: This function's return value cannot distinguish errors from -1.
  1022. // Prefer |ASN1_ENUMERATED_get_uint64|.
  1023. OPENSSL_EXPORT long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a);
  1024. // BN_to_ASN1_ENUMERATED sets |ai| to an ENUMERATED with value |bn| and returns
  1025. // |ai| on success or NULL or error. If |ai| is NULL, it returns a
  1026. // newly-allocated |ASN1_ENUMERATED| on success instead, which the caller must
  1027. // release with |ASN1_ENUMERATED_free|.
  1028. OPENSSL_EXPORT ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn,
  1029. ASN1_ENUMERATED *ai);
  1030. // ASN1_ENUMERATED_to_BN sets |bn| to the value of |ai| and returns |bn| on
  1031. // success or NULL or error. If |bn| is NULL, it returns a newly-allocated
  1032. // |BIGNUM| on success instead, which the caller must release with |BN_free|.
  1033. OPENSSL_EXPORT BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai,
  1034. BIGNUM *bn);
  1035. // Time.
  1036. //
  1037. // GeneralizedTime and UTCTime values are represented as |ASN1_STRING|s. The
  1038. // type field is |V_ASN1_GENERALIZEDTIME| or |V_ASN1_UTCTIME|, respectively. The
  1039. // data field contains the DER encoding of the value. For example, the UNIX
  1040. // epoch would be "19700101000000Z" for a GeneralizedTime and "700101000000Z"
  1041. // for a UTCTime.
  1042. //
  1043. // ASN.1 does not define how to interpret UTCTime's two-digit year. RFC 5280
  1044. // defines it as a range from 1950 to 2049 for X.509. The library uses the
  1045. // RFC 5280 interpretation. It does not currently enforce the restrictions from
  1046. // BER, and the additional restrictions from RFC 5280, but future versions may.
  1047. // Callers should not rely on fractional seconds and non-UTC time zones.
  1048. //
  1049. // The |ASN1_TIME| typedef is a multi-string representing the X.509 Time type,
  1050. // which is a CHOICE of GeneralizedTime and UTCTime, using UTCTime when the
  1051. // value is in range.
  1052. // ASN1_UTCTIME_new calls |ASN1_STRING_type_new| with |V_ASN1_UTCTIME|. The
  1053. // resulting object contains empty contents and must be initialized to be a
  1054. // valid UTCTime.
  1055. OPENSSL_EXPORT ASN1_UTCTIME *ASN1_UTCTIME_new(void);
  1056. // ASN1_UTCTIME_free calls |ASN1_STRING_free|.
  1057. OPENSSL_EXPORT void ASN1_UTCTIME_free(ASN1_UTCTIME *str);
  1058. // d2i_ASN1_UTCTIME parses up to |len| bytes from |*inp| as a DER-encoded
  1059. // ASN.1 UTCTime, as described in |d2i_SAMPLE_with_reuse|.
  1060. //
  1061. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  1062. // BER, but this will be removed in the future.
  1063. OPENSSL_EXPORT ASN1_UTCTIME *d2i_ASN1_UTCTIME(ASN1_UTCTIME **out,
  1064. const uint8_t **inp, long len);
  1065. // i2d_ASN1_UTCTIME marshals |in| as a DER-encoded ASN.1 UTCTime, as
  1066. // described in |i2d_SAMPLE|.
  1067. OPENSSL_EXPORT int i2d_ASN1_UTCTIME(const ASN1_UTCTIME *in, uint8_t **outp);
  1068. // ASN1_UTCTIME is an |ASN1_ITEM| with ASN.1 type UTCTime and C type
  1069. // |ASN1_UTCTIME*|.
  1070. DECLARE_ASN1_ITEM(ASN1_UTCTIME)
  1071. // ASN1_UTCTIME_check returns one if |a| is a valid UTCTime and zero otherwise.
  1072. OPENSSL_EXPORT int ASN1_UTCTIME_check(const ASN1_UTCTIME *a);
  1073. // ASN1_UTCTIME_set represents |t| as a UTCTime and writes the result to |s|. It
  1074. // returns |s| on success and NULL on error. If |s| is NULL, it returns a
  1075. // newly-allocated |ASN1_UTCTIME| instead.
  1076. //
  1077. // Note this function may fail if the time is out of range for UTCTime.
  1078. OPENSSL_EXPORT ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t);
  1079. // ASN1_UTCTIME_adj adds |offset_day| days and |offset_sec| seconds to |t| and
  1080. // writes the result to |s| as a UTCTime. It returns |s| on success and NULL on
  1081. // error. If |s| is NULL, it returns a newly-allocated |ASN1_UTCTIME| instead.
  1082. //
  1083. // Note this function may fail if the time overflows or is out of range for
  1084. // UTCTime.
  1085. OPENSSL_EXPORT ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
  1086. int offset_day, long offset_sec);
  1087. // ASN1_UTCTIME_set_string sets |s| to a UTCTime whose contents are a copy of
  1088. // |str|. It returns one on success and zero on error or if |str| is not a valid
  1089. // UTCTime.
  1090. //
  1091. // If |s| is NULL, this function validates |str| without copying it.
  1092. OPENSSL_EXPORT int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str);
  1093. // ASN1_UTCTIME_cmp_time_t compares |s| to |t|. It returns -1 if |s| < |t|, 0 if
  1094. // they are equal, 1 if |s| > |t|, and -2 on error.
  1095. OPENSSL_EXPORT int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t);
  1096. // ASN1_GENERALIZEDTIME_new calls |ASN1_STRING_type_new| with
  1097. // |V_ASN1_GENERALIZEDTIME|. The resulting object contains empty contents and
  1098. // must be initialized to be a valid GeneralizedTime.
  1099. OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_new(void);
  1100. // ASN1_GENERALIZEDTIME_free calls |ASN1_STRING_free|.
  1101. OPENSSL_EXPORT void ASN1_GENERALIZEDTIME_free(ASN1_GENERALIZEDTIME *str);
  1102. // d2i_ASN1_GENERALIZEDTIME parses up to |len| bytes from |*inp| as a
  1103. // DER-encoded ASN.1 GeneralizedTime, as described in |d2i_SAMPLE_with_reuse|.
  1104. //
  1105. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  1106. // BER, but this will be removed in the future.
  1107. OPENSSL_EXPORT ASN1_GENERALIZEDTIME *d2i_ASN1_GENERALIZEDTIME(
  1108. ASN1_GENERALIZEDTIME **out, const uint8_t **inp, long len);
  1109. // i2d_ASN1_GENERALIZEDTIME marshals |in| as a DER-encoded ASN.1
  1110. // GeneralizedTime, as described in |i2d_SAMPLE|.
  1111. OPENSSL_EXPORT int i2d_ASN1_GENERALIZEDTIME(const ASN1_GENERALIZEDTIME *in,
  1112. uint8_t **outp);
  1113. // ASN1_GENERALIZEDTIME is an |ASN1_ITEM| with ASN.1 type GeneralizedTime and C
  1114. // type |ASN1_GENERALIZEDTIME*|.
  1115. DECLARE_ASN1_ITEM(ASN1_GENERALIZEDTIME)
  1116. // ASN1_GENERALIZEDTIME_check returns one if |a| is a valid GeneralizedTime and
  1117. // zero otherwise.
  1118. OPENSSL_EXPORT int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *a);
  1119. // ASN1_GENERALIZEDTIME_set represents |t| as a GeneralizedTime and writes the
  1120. // result to |s|. It returns |s| on success and NULL on error. If |s| is NULL,
  1121. // it returns a newly-allocated |ASN1_GENERALIZEDTIME| instead.
  1122. //
  1123. // Note this function may fail if the time is out of range for GeneralizedTime.
  1124. OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(
  1125. ASN1_GENERALIZEDTIME *s, time_t t);
  1126. // ASN1_GENERALIZEDTIME_adj adds |offset_day| days and |offset_sec| seconds to
  1127. // |t| and writes the result to |s| as a GeneralizedTime. It returns |s| on
  1128. // success and NULL on error. If |s| is NULL, it returns a newly-allocated
  1129. // |ASN1_GENERALIZEDTIME| instead.
  1130. //
  1131. // Note this function may fail if the time overflows or is out of range for
  1132. // GeneralizedTime.
  1133. OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(
  1134. ASN1_GENERALIZEDTIME *s, time_t t, int offset_day, long offset_sec);
  1135. // ASN1_GENERALIZEDTIME_set_string sets |s| to a GeneralizedTime whose contents
  1136. // are a copy of |str|. It returns one on success and zero on error or if |str|
  1137. // is not a valid GeneralizedTime.
  1138. //
  1139. // If |s| is NULL, this function validates |str| without copying it.
  1140. OPENSSL_EXPORT int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s,
  1141. const char *str);
  1142. // B_ASN1_TIME is a bitmask of types allowed in an X.509 Time.
  1143. #define B_ASN1_TIME (B_ASN1_UTCTIME | B_ASN1_GENERALIZEDTIME)
  1144. // ASN1_TIME_new returns a newly-allocated |ASN1_TIME| with type -1, or NULL on
  1145. // error. The resulting |ASN1_TIME| is not a valid X.509 Time until initialized
  1146. // with a value.
  1147. OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_new(void);
  1148. // ASN1_TIME_free releases memory associated with |str|.
  1149. OPENSSL_EXPORT void ASN1_TIME_free(ASN1_TIME *str);
  1150. // d2i_ASN1_TIME parses up to |len| bytes from |*inp| as a DER-encoded X.509
  1151. // Time (RFC 5280), as described in |d2i_SAMPLE_with_reuse|.
  1152. //
  1153. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  1154. // BER, but this will be removed in the future.
  1155. OPENSSL_EXPORT ASN1_TIME *d2i_ASN1_TIME(ASN1_TIME **out, const uint8_t **inp,
  1156. long len);
  1157. // i2d_ASN1_TIME marshals |in| as a DER-encoded X.509 Time (RFC 5280), as
  1158. // described in |i2d_SAMPLE|.
  1159. OPENSSL_EXPORT int i2d_ASN1_TIME(const ASN1_TIME *in, uint8_t **outp);
  1160. // ASN1_TIME is an |ASN1_ITEM| whose ASN.1 type is X.509 Time (RFC 5280) and C
  1161. // type is |ASN1_TIME*|.
  1162. DECLARE_ASN1_ITEM(ASN1_TIME)
  1163. // ASN1_TIME_diff computes |to| - |from|. On success, it sets |*out_days| to the
  1164. // difference in days, rounded towards zero, sets |*out_seconds| to the
  1165. // remainder, and returns one. On error, it returns zero.
  1166. //
  1167. // If |from| is before |to|, both outputs will be <= 0, with at least one
  1168. // negative. If |from| is after |to|, both will be >= 0, with at least one
  1169. // positive. If they are equal, ignoring fractional seconds, both will be zero.
  1170. //
  1171. // Note this function may fail on overflow, or if |from| or |to| cannot be
  1172. // decoded.
  1173. OPENSSL_EXPORT int ASN1_TIME_diff(int *out_days, int *out_seconds,
  1174. const ASN1_TIME *from, const ASN1_TIME *to);
  1175. // ASN1_TIME_set represents |t| as a GeneralizedTime or UTCTime and writes
  1176. // the result to |s|. As in RFC 5280, section 4.1.2.5, it uses UTCTime when the
  1177. // time fits and GeneralizedTime otherwise. It returns |s| on success and NULL
  1178. // on error. If |s| is NULL, it returns a newly-allocated |ASN1_TIME| instead.
  1179. //
  1180. // Note this function may fail if the time is out of range for GeneralizedTime.
  1181. OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t);
  1182. // ASN1_TIME_adj adds |offset_day| days and |offset_sec| seconds to
  1183. // |t| and writes the result to |s|. As in RFC 5280, section 4.1.2.5, it uses
  1184. // UTCTime when the time fits and GeneralizedTime otherwise. It returns |s| on
  1185. // success and NULL on error. If |s| is NULL, it returns a newly-allocated
  1186. // |ASN1_GENERALIZEDTIME| instead.
  1187. //
  1188. // Note this function may fail if the time overflows or is out of range for
  1189. // GeneralizedTime.
  1190. OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, int offset_day,
  1191. long offset_sec);
  1192. // ASN1_TIME_check returns one if |t| is a valid UTCTime or GeneralizedTime, and
  1193. // zero otherwise. |t|'s type determines which check is performed. This
  1194. // function does not enforce that UTCTime was used when possible.
  1195. OPENSSL_EXPORT int ASN1_TIME_check(const ASN1_TIME *t);
  1196. // ASN1_TIME_to_generalizedtime converts |t| to a GeneralizedTime. If |out| is
  1197. // NULL, it returns a newly-allocated |ASN1_GENERALIZEDTIME| on success, or NULL
  1198. // on error. If |out| is non-NULL and |*out| is NULL, it additionally sets
  1199. // |*out| to the result. If |out| and |*out| are non-NULL, it instead updates
  1200. // the object pointed by |*out| and returns |*out| on success or NULL on error.
  1201. OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(
  1202. const ASN1_TIME *t, ASN1_GENERALIZEDTIME **out);
  1203. // ASN1_TIME_set_string behaves like |ASN1_UTCTIME_set_string| if |str| is a
  1204. // valid UTCTime, and |ASN1_GENERALIZEDTIME_set_string| if |str| is a valid
  1205. // GeneralizedTime. If |str| is neither, it returns zero.
  1206. OPENSSL_EXPORT int ASN1_TIME_set_string(ASN1_TIME *s, const char *str);
  1207. // TODO(davidben): Expand and document function prototypes generated in macros.
  1208. // NULL values.
  1209. //
  1210. // This library represents the ASN.1 NULL value by a non-NULL pointer to the
  1211. // opaque type |ASN1_NULL|. An omitted OPTIONAL ASN.1 NULL value is a NULL
  1212. // pointer. Unlike other pointer types, it is not necessary to free |ASN1_NULL|
  1213. // pointers, but it is safe to do so.
  1214. // ASN1_NULL_new returns an opaque, non-NULL pointer. It is safe to call
  1215. // |ASN1_NULL_free| on the result, but not necessary.
  1216. OPENSSL_EXPORT ASN1_NULL *ASN1_NULL_new(void);
  1217. // ASN1_NULL_free does nothing.
  1218. OPENSSL_EXPORT void ASN1_NULL_free(ASN1_NULL *null);
  1219. // d2i_ASN1_NULL parses a DER-encoded ASN.1 NULL value from up to |len| bytes
  1220. // at |*inp|, as described in |d2i_SAMPLE|.
  1221. //
  1222. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  1223. // BER, but this will be removed in the future.
  1224. OPENSSL_EXPORT ASN1_NULL *d2i_ASN1_NULL(ASN1_NULL **out, const uint8_t **inp,
  1225. long len);
  1226. // i2d_ASN1_NULL marshals |in| as a DER-encoded ASN.1 NULL value, as described
  1227. // in |i2d_SAMPLE|.
  1228. OPENSSL_EXPORT int i2d_ASN1_NULL(const ASN1_NULL *in, uint8_t **outp);
  1229. // ASN1_NULL is an |ASN1_ITEM| with ASN.1 type NULL and C type |ASN1_NULL*|.
  1230. DECLARE_ASN1_ITEM(ASN1_NULL)
  1231. // Object identifiers.
  1232. //
  1233. // An |ASN1_OBJECT| represents a ASN.1 OBJECT IDENTIFIER. See also obj.h for
  1234. // additional functions relating to |ASN1_OBJECT|.
  1235. //
  1236. // TODO(davidben): What's the relationship between asn1.h and obj.h? Most of
  1237. // obj.h deals with the large NID table, but then functions like |OBJ_get0_data|
  1238. // or |OBJ_dup| are general |ASN1_OBJECT| functions.
  1239. DEFINE_STACK_OF(ASN1_OBJECT)
  1240. // ASN1_OBJECT_create returns a newly-allocated |ASN1_OBJECT| with |len| bytes
  1241. // from |data| as the encoded OID, or NULL on error. |data| should contain the
  1242. // DER-encoded identifier, excluding the tag and length.
  1243. //
  1244. // |nid| should be |NID_undef|. Passing a NID value that does not match |data|
  1245. // will cause some functions to misbehave. |sn| and |ln| should be NULL. If
  1246. // non-NULL, they are stored as short and long names, respectively, but these
  1247. // values have no effect for |ASN1_OBJECT|s created through this function.
  1248. //
  1249. // TODO(davidben): Should we just ignore all those parameters? NIDs and names
  1250. // are only relevant for |ASN1_OBJECT|s in the obj.h table.
  1251. OPENSSL_EXPORT ASN1_OBJECT *ASN1_OBJECT_create(int nid, const uint8_t *data,
  1252. int len, const char *sn,
  1253. const char *ln);
  1254. // ASN1_OBJECT_free releases memory associated with |a|. If |a| is a static
  1255. // |ASN1_OBJECT|, returned from |OBJ_nid2obj|, this function does nothing.
  1256. OPENSSL_EXPORT void ASN1_OBJECT_free(ASN1_OBJECT *a);
  1257. // d2i_ASN1_OBJECT parses a DER-encoded ASN.1 OBJECT IDENTIFIER from up to |len|
  1258. // bytes at |*inp|, as described in |d2i_SAMPLE_with_reuse|.
  1259. //
  1260. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  1261. // BER, but this will be removed in the future.
  1262. OPENSSL_EXPORT ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **out,
  1263. const uint8_t **inp, long len);
  1264. // i2d_ASN1_OBJECT marshals |in| as a DER-encoded ASN.1 OBJECT IDENTIFIER, as
  1265. // described in |i2d_SAMPLE|.
  1266. OPENSSL_EXPORT int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, uint8_t **outp);
  1267. // c2i_ASN1_OBJECT decodes |len| bytes from |*inp| as the contents of a
  1268. // DER-encoded OBJECT IDENTIFIER, excluding the tag and length. It behaves like
  1269. // |d2i_SAMPLE_with_reuse| except, on success, it always consumes all |len|
  1270. // bytes.
  1271. OPENSSL_EXPORT ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **out,
  1272. const uint8_t **inp, long len);
  1273. // ASN1_OBJECT is an |ASN1_ITEM| with ASN.1 type OBJECT IDENTIFIER and C type
  1274. // |ASN1_OBJECT*|.
  1275. DECLARE_ASN1_ITEM(ASN1_OBJECT)
  1276. // Arbitrary elements.
  1277. // An asn1_type_st (aka |ASN1_TYPE|) represents an arbitrary ASN.1 element,
  1278. // typically used for ANY types. It contains a |type| field and a |value| union
  1279. // dependent on |type|.
  1280. //
  1281. // WARNING: This struct has a complex representation. Callers must not construct
  1282. // |ASN1_TYPE| values manually. Use |ASN1_TYPE_set| and |ASN1_TYPE_set1|
  1283. // instead. Additionally, callers performing non-trivial operations on this type
  1284. // are encouraged to use |CBS| and |CBB| from <openssl/bytestring.h>, and
  1285. // convert to or from |ASN1_TYPE| with |d2i_ASN1_TYPE| or |i2d_ASN1_TYPE|.
  1286. //
  1287. // The |type| field corresponds to the tag of the ASN.1 element being
  1288. // represented:
  1289. //
  1290. // If |type| is a |V_ASN1_*| constant for an ASN.1 string-like type, as defined
  1291. // by |ASN1_STRING|, the tag matches the constant. |value| contains an
  1292. // |ASN1_STRING| pointer (equivalently, one of the more specific typedefs). See
  1293. // |ASN1_STRING| for details on the representation. Unlike |ASN1_STRING|,
  1294. // |ASN1_TYPE| does not use the |V_ASN1_NEG| flag for negative INTEGER and
  1295. // ENUMERATE values. For a negative value, the |ASN1_TYPE|'s |type| will be
  1296. // |V_ASN1_INTEGER| or |V_ASN1_ENUMERATED|, but |value| will an |ASN1_STRING|
  1297. // whose |type| is |V_ASN1_NEG_INTEGER| or |V_ASN1_NEG_ENUMERATED|.
  1298. //
  1299. // If |type| is |V_ASN1_OBJECT|, the tag is OBJECT IDENTIFIER and |value|
  1300. // contains an |ASN1_OBJECT| pointer.
  1301. //
  1302. // If |type| is |V_ASN1_NULL|, the tag is NULL. |value| contains a NULL pointer.
  1303. //
  1304. // If |type| is |V_ASN1_BOOLEAN|, the tag is BOOLEAN. |value| contains an
  1305. // |ASN1_BOOLEAN|.
  1306. //
  1307. // If |type| is |V_ASN1_SEQUENCE|, |V_ASN1_SET|, or |V_ASN1_OTHER|, the tag is
  1308. // SEQUENCE, SET, or some non-universal tag, respectively. |value| is an
  1309. // |ASN1_STRING| containing the entire element, including the tag and length.
  1310. // The |ASN1_STRING|'s |type| field matches the containing |ASN1_TYPE|'s |type|.
  1311. //
  1312. // Other positive values of |type|, up to |V_ASN1_MAX_UNIVERSAL|, correspond to
  1313. // universal primitive tags not directly supported by this library. |value| is
  1314. // an |ASN1_STRING| containing the body of the element, excluding the tag
  1315. // and length. The |ASN1_STRING|'s |type| field matches the containing
  1316. // |ASN1_TYPE|'s |type|.
  1317. struct asn1_type_st {
  1318. int type;
  1319. union {
  1320. char *ptr;
  1321. ASN1_BOOLEAN boolean;
  1322. ASN1_STRING *asn1_string;
  1323. ASN1_OBJECT *object;
  1324. ASN1_INTEGER *integer;
  1325. ASN1_ENUMERATED *enumerated;
  1326. ASN1_BIT_STRING *bit_string;
  1327. ASN1_OCTET_STRING *octet_string;
  1328. ASN1_PRINTABLESTRING *printablestring;
  1329. ASN1_T61STRING *t61string;
  1330. ASN1_IA5STRING *ia5string;
  1331. ASN1_GENERALSTRING *generalstring;
  1332. ASN1_BMPSTRING *bmpstring;
  1333. ASN1_UNIVERSALSTRING *universalstring;
  1334. ASN1_UTCTIME *utctime;
  1335. ASN1_GENERALIZEDTIME *generalizedtime;
  1336. ASN1_VISIBLESTRING *visiblestring;
  1337. ASN1_UTF8STRING *utf8string;
  1338. // set and sequence are left complete and still contain the entire element.
  1339. ASN1_STRING *set;
  1340. ASN1_STRING *sequence;
  1341. ASN1_VALUE *asn1_value;
  1342. } value;
  1343. };
  1344. DEFINE_STACK_OF(ASN1_TYPE)
  1345. // ASN1_TYPE_new returns a newly-allocated |ASN1_TYPE|, or NULL on allocation
  1346. // failure. The resulting object has type -1 and must be initialized to be
  1347. // a valid ANY value.
  1348. OPENSSL_EXPORT ASN1_TYPE *ASN1_TYPE_new(void);
  1349. // ASN1_TYPE_free releases memory associated with |a|.
  1350. OPENSSL_EXPORT void ASN1_TYPE_free(ASN1_TYPE *a);
  1351. // d2i_ASN1_TYPE parses up to |len| bytes from |*inp| as an ASN.1 value of any
  1352. // type, as described in |d2i_SAMPLE_with_reuse|. Note this function only
  1353. // validates primitive, universal types supported by this library. Values of
  1354. // type |V_ASN1_SEQUENCE|, |V_ASN1_SET|, |V_ASN1_OTHER|, or an unsupported
  1355. // primitive type must be validated by the caller when interpreting.
  1356. //
  1357. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  1358. // BER, but this will be removed in the future.
  1359. OPENSSL_EXPORT ASN1_TYPE *d2i_ASN1_TYPE(ASN1_TYPE **out, const uint8_t **inp,
  1360. long len);
  1361. // i2d_ASN1_TYPE marshals |in| as DER, as described in |i2d_SAMPLE|.
  1362. OPENSSL_EXPORT int i2d_ASN1_TYPE(const ASN1_TYPE *in, uint8_t **outp);
  1363. // ASN1_ANY is an |ASN1_ITEM| with ASN.1 type ANY and C type |ASN1_TYPE*|. Note
  1364. // the |ASN1_ITEM| name and C type do not match.
  1365. DECLARE_ASN1_ITEM(ASN1_ANY)
  1366. // ASN1_TYPE_get returns the type of |a|, which will be one of the |V_ASN1_*|
  1367. // constants, or zero if |a| is not fully initialized.
  1368. OPENSSL_EXPORT int ASN1_TYPE_get(const ASN1_TYPE *a);
  1369. // ASN1_TYPE_set sets |a| to an |ASN1_TYPE| of type |type| and value |value|,
  1370. // releasing the previous contents of |a|.
  1371. //
  1372. // If |type| is |V_ASN1_BOOLEAN|, |a| is set to FALSE if |value| is NULL and
  1373. // TRUE otherwise. If setting |a| to TRUE, |value| may be an invalid pointer,
  1374. // such as (void*)1.
  1375. //
  1376. // If |type| is |V_ASN1_NULL|, |value| must be NULL.
  1377. //
  1378. // For other values of |type|, this function takes ownership of |value|, which
  1379. // must point to an object of the corresponding type. See |ASN1_TYPE| for
  1380. // details.
  1381. OPENSSL_EXPORT void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value);
  1382. // ASN1_TYPE_set1 behaves like |ASN1_TYPE_set| except it does not take ownership
  1383. // of |value|. It returns one on success and zero on error.
  1384. OPENSSL_EXPORT int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value);
  1385. // ASN1_TYPE_cmp returns zero if |a| and |b| are equal and some non-zero value
  1386. // otherwise. Note this function can only be used for equality checks, not an
  1387. // ordering.
  1388. OPENSSL_EXPORT int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b);
  1389. typedef STACK_OF(ASN1_TYPE) ASN1_SEQUENCE_ANY;
  1390. // d2i_ASN1_SEQUENCE_ANY parses up to |len| bytes from |*inp| as a DER-encoded
  1391. // ASN.1 SEQUENCE OF ANY structure, as described in |d2i_SAMPLE_with_reuse|. The
  1392. // resulting |ASN1_SEQUENCE_ANY| owns its contents and thus must be released
  1393. // with |sk_ASN1_TYPE_pop_free| and |ASN1_TYPE_free|, not |sk_ASN1_TYPE_free|.
  1394. //
  1395. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  1396. // BER, but this will be removed in the future.
  1397. OPENSSL_EXPORT ASN1_SEQUENCE_ANY *d2i_ASN1_SEQUENCE_ANY(ASN1_SEQUENCE_ANY **out,
  1398. const uint8_t **inp,
  1399. long len);
  1400. // i2d_ASN1_SEQUENCE_ANY marshals |in| as a DER-encoded SEQUENCE OF ANY
  1401. // structure, as described in |i2d_SAMPLE|.
  1402. OPENSSL_EXPORT int i2d_ASN1_SEQUENCE_ANY(const ASN1_SEQUENCE_ANY *in,
  1403. uint8_t **outp);
  1404. // d2i_ASN1_SET_ANY parses up to |len| bytes from |*inp| as a DER-encoded ASN.1
  1405. // SET OF ANY structure, as described in |d2i_SAMPLE_with_reuse|. The resulting
  1406. // |ASN1_SEQUENCE_ANY| owns its contents and thus must be released with
  1407. // |sk_ASN1_TYPE_pop_free| and |ASN1_TYPE_free|, not |sk_ASN1_TYPE_free|.
  1408. //
  1409. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  1410. // BER, but this will be removed in the future.
  1411. OPENSSL_EXPORT ASN1_SEQUENCE_ANY *d2i_ASN1_SET_ANY(ASN1_SEQUENCE_ANY **out,
  1412. const uint8_t **inp,
  1413. long len);
  1414. // i2d_ASN1_SET_ANY marshals |in| as a DER-encoded SET OF ANY structure, as
  1415. // described in |i2d_SAMPLE|.
  1416. OPENSSL_EXPORT int i2d_ASN1_SET_ANY(const ASN1_SEQUENCE_ANY *in,
  1417. uint8_t **outp);
  1418. // Human-readable output.
  1419. //
  1420. // The following functions output types in some human-readable format. These
  1421. // functions may be used for debugging and logging. However, the output should
  1422. // not be consumed programmatically. They may be ambiguous or lose information.
  1423. // ASN1_UTCTIME_print writes a human-readable representation of |a| to |out|. It
  1424. // returns one on success and zero on error.
  1425. OPENSSL_EXPORT int ASN1_UTCTIME_print(BIO *out, const ASN1_UTCTIME *a);
  1426. // ASN1_GENERALIZEDTIME_print writes a human-readable representation of |a| to
  1427. // |out|. It returns one on success and zero on error.
  1428. OPENSSL_EXPORT int ASN1_GENERALIZEDTIME_print(BIO *out,
  1429. const ASN1_GENERALIZEDTIME *a);
  1430. // ASN1_TIME_print writes a human-readable representation of |a| to |out|. It
  1431. // returns one on success and zero on error.
  1432. OPENSSL_EXPORT int ASN1_TIME_print(BIO *out, const ASN1_TIME *a);
  1433. // ASN1_STRING_print writes a human-readable representation of |str| to |out|.
  1434. // It returns one on success and zero on error. Unprintable characters are
  1435. // replaced with '.'.
  1436. OPENSSL_EXPORT int ASN1_STRING_print(BIO *out, const ASN1_STRING *str);
  1437. // The following flags must not collide with |XN_FLAG_*|.
  1438. // ASN1_STRFLGS_ESC_2253 causes characters to be escaped as in RFC 2253, section
  1439. // 2.4.
  1440. #define ASN1_STRFLGS_ESC_2253 1
  1441. // ASN1_STRFLGS_ESC_CTRL causes all control characters to be escaped.
  1442. #define ASN1_STRFLGS_ESC_CTRL 2
  1443. // ASN1_STRFLGS_ESC_MSB causes all characters above 127 to be escaped.
  1444. #define ASN1_STRFLGS_ESC_MSB 4
  1445. // ASN1_STRFLGS_ESC_QUOTE causes the string to be surrounded by quotes, rather
  1446. // than using backslashes, when characters are escaped. Fewer characters will
  1447. // require escapes in this case.
  1448. #define ASN1_STRFLGS_ESC_QUOTE 8
  1449. // ASN1_STRFLGS_UTF8_CONVERT causes the string to be encoded as UTF-8, with each
  1450. // byte in the UTF-8 encoding treated as an individual character for purposes of
  1451. // escape sequences. If not set, each Unicode codepoint in the string is treated
  1452. // as a character, with wide characters escaped as "\Uxxxx" or "\Wxxxxxxxx".
  1453. // Note this can be ambiguous if |ASN1_STRFLGS_ESC_*| are all unset. In that
  1454. // case, backslashes are not escaped, but wide characters are.
  1455. #define ASN1_STRFLGS_UTF8_CONVERT 0x10
  1456. // ASN1_STRFLGS_IGNORE_TYPE causes the string type to be ignored. The
  1457. // |ASN1_STRING| in-memory representation will be printed directly.
  1458. #define ASN1_STRFLGS_IGNORE_TYPE 0x20
  1459. // ASN1_STRFLGS_SHOW_TYPE causes the string type to be included in the output.
  1460. #define ASN1_STRFLGS_SHOW_TYPE 0x40
  1461. // ASN1_STRFLGS_DUMP_ALL causes all strings to be printed as a hexdump, using
  1462. // RFC 2253 hexstring notation, such as "#0123456789ABCDEF".
  1463. #define ASN1_STRFLGS_DUMP_ALL 0x80
  1464. // ASN1_STRFLGS_DUMP_UNKNOWN behaves like |ASN1_STRFLGS_DUMP_ALL| but only
  1465. // applies to values of unknown type. If unset, unknown values will print
  1466. // their contents as single-byte characters with escape sequences.
  1467. #define ASN1_STRFLGS_DUMP_UNKNOWN 0x100
  1468. // ASN1_STRFLGS_DUMP_DER causes hexdumped strings (as determined by
  1469. // |ASN1_STRFLGS_DUMP_ALL| or |ASN1_STRFLGS_DUMP_UNKNOWN|) to print the entire
  1470. // DER element as in RFC 2253, rather than only the contents of the
  1471. // |ASN1_STRING|.
  1472. #define ASN1_STRFLGS_DUMP_DER 0x200
  1473. // ASN1_STRFLGS_RFC2253 causes the string to be escaped as in RFC 2253,
  1474. // additionally escaping control characters.
  1475. #define ASN1_STRFLGS_RFC2253 \
  1476. (ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB | \
  1477. ASN1_STRFLGS_UTF8_CONVERT | ASN1_STRFLGS_DUMP_UNKNOWN | \
  1478. ASN1_STRFLGS_DUMP_DER)
  1479. // ASN1_STRING_print_ex writes a human-readable representation of |str| to
  1480. // |out|. It returns the number of bytes written on success and -1 on error. If
  1481. // |out| is NULL, it returns the number of bytes it would have written, without
  1482. // writing anything.
  1483. //
  1484. // The |flags| should be a combination of combination of |ASN1_STRFLGS_*|
  1485. // constants. See the documentation for each flag for how it controls the
  1486. // output. If unsure, use |ASN1_STRFLGS_RFC2253|.
  1487. OPENSSL_EXPORT int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str,
  1488. unsigned long flags);
  1489. // ASN1_STRING_print_ex_fp behaves like |ASN1_STRING_print_ex| but writes to a
  1490. // |FILE| rather than a |BIO|.
  1491. OPENSSL_EXPORT int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str,
  1492. unsigned long flags);
  1493. // i2a_ASN1_INTEGER writes a human-readable representation of |a| to |bp|. It
  1494. // returns the number of bytes written on success, or a negative number on
  1495. // error. On error, this function may have written a partial output to |bp|.
  1496. OPENSSL_EXPORT int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a);
  1497. // i2a_ASN1_ENUMERATED writes a human-readable representation of |a| to |bp|. It
  1498. // returns the number of bytes written on success, or a negative number on
  1499. // error. On error, this function may have written a partial output to |bp|.
  1500. OPENSSL_EXPORT int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a);
  1501. // i2a_ASN1_OBJECT writes a human-readable representation of |a| to |bp|. It
  1502. // returns the number of bytes written on success, or a negative number on
  1503. // error. On error, this function may have written a partial output to |bp|.
  1504. OPENSSL_EXPORT int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a);
  1505. // i2a_ASN1_STRING writes a text representation of |a|'s contents to |bp|. It
  1506. // returns the number of bytes written on success, or a negative number on
  1507. // error. On error, this function may have written a partial output to |bp|.
  1508. // |type| is ignored.
  1509. //
  1510. // This function does not decode |a| into a Unicode string. It only hex-encodes
  1511. // the internal representation of |a|. This is suitable for printing an OCTET
  1512. // STRING, but may not be human-readable for any other string type.
  1513. OPENSSL_EXPORT int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type);
  1514. // i2t_ASN1_OBJECT calls |OBJ_obj2txt| with |always_return_oid| set to zero.
  1515. OPENSSL_EXPORT int i2t_ASN1_OBJECT(char *buf, int buf_len,
  1516. const ASN1_OBJECT *a);
  1517. // Low-level encoding functions.
  1518. // ASN1_get_object parses a BER element from up to |max_len| bytes at |*inp|. It
  1519. // returns |V_ASN1_CONSTRUCTED| if it successfully parsed a constructed element,
  1520. // zero if it successfully parsed a primitive element, and 0x80 on error. On
  1521. // success, it additionally advances |*inp| to the element body, sets
  1522. // |*out_length|, |*out_tag|, and |*out_class| to the element's length, tag
  1523. // number, and tag class, respectively,
  1524. //
  1525. // Unlike OpenSSL, this function does not support indefinite-length elements.
  1526. //
  1527. // This function is difficult to use correctly. Use |CBS_get_asn1| and related
  1528. // functions from bytestring.h.
  1529. //
  1530. // TODO(https://crbug.com/boringssl/354): Remove support for non-minimal
  1531. // lengths.
  1532. OPENSSL_EXPORT int ASN1_get_object(const unsigned char **inp, long *out_length,
  1533. int *out_tag, int *out_class, long max_len);
  1534. // ASN1_put_object writes the header for a DER or BER element to |*outp| and
  1535. // advances |*outp| by the number of bytes written. The caller is responsible
  1536. // for ensuring |*outp| has enough space for the output. The header describes an
  1537. // element with length |length|, tag number |tag|, and class |xclass|. |xclass|
  1538. // should be one of the |V_ASN1_*| tag class constants. The element is primitive
  1539. // if |constructed| is zero and constructed if it is one or two. If
  1540. // |constructed| is two, |length| is ignored and the element uses
  1541. // indefinite-length encoding.
  1542. //
  1543. // Use |CBB_add_asn1| instead.
  1544. OPENSSL_EXPORT void ASN1_put_object(unsigned char **outp, int constructed,
  1545. int length, int tag, int xclass);
  1546. // ASN1_put_eoc writes two zero bytes to |*outp|, advances |*outp| to point past
  1547. // those bytes, and returns two.
  1548. //
  1549. // Use definite-length encoding instead.
  1550. OPENSSL_EXPORT int ASN1_put_eoc(unsigned char **outp);
  1551. // ASN1_object_size returns the number of bytes needed to encode a DER or BER
  1552. // value with length |length| and tag number |tag|, or -1 on error. |tag| should
  1553. // not include the constructed bit or tag class. If |constructed| is zero or
  1554. // one, the result uses a definite-length encoding with minimally-encoded
  1555. // length, as in DER. If |constructed| is two, the result uses BER
  1556. // indefinite-length encoding.
  1557. //
  1558. // Use |CBB_add_asn1| instead.
  1559. OPENSSL_EXPORT int ASN1_object_size(int constructed, int length, int tag);
  1560. // Function declaration macros.
  1561. //
  1562. // The following macros declare functions for ASN.1 types. Prefer writing the
  1563. // prototypes directly. Particularly when |type|, |itname|, or |name| differ,
  1564. // the macros can be difficult to understand.
  1565. #define DECLARE_ASN1_FUNCTIONS(type) DECLARE_ASN1_FUNCTIONS_name(type, type)
  1566. #define DECLARE_ASN1_ALLOC_FUNCTIONS(type) \
  1567. DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, type)
  1568. #define DECLARE_ASN1_FUNCTIONS_name(type, name) \
  1569. DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \
  1570. DECLARE_ASN1_ENCODE_FUNCTIONS(type, name, name)
  1571. #define DECLARE_ASN1_FUNCTIONS_fname(type, itname, name) \
  1572. DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \
  1573. DECLARE_ASN1_ENCODE_FUNCTIONS(type, itname, name)
  1574. #define DECLARE_ASN1_ENCODE_FUNCTIONS(type, itname, name) \
  1575. OPENSSL_EXPORT type *d2i_##name(type **a, const unsigned char **in, \
  1576. long len); \
  1577. OPENSSL_EXPORT int i2d_##name(type *a, unsigned char **out); \
  1578. DECLARE_ASN1_ITEM(itname)
  1579. #define DECLARE_ASN1_ENCODE_FUNCTIONS_const(type, name) \
  1580. OPENSSL_EXPORT type *d2i_##name(type **a, const unsigned char **in, \
  1581. long len); \
  1582. OPENSSL_EXPORT int i2d_##name(const type *a, unsigned char **out); \
  1583. DECLARE_ASN1_ITEM(name)
  1584. #define DECLARE_ASN1_FUNCTIONS_const(name) \
  1585. DECLARE_ASN1_ALLOC_FUNCTIONS(name) \
  1586. DECLARE_ASN1_ENCODE_FUNCTIONS_const(name, name)
  1587. #define DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \
  1588. OPENSSL_EXPORT type *name##_new(void); \
  1589. OPENSSL_EXPORT void name##_free(type *a);
  1590. // Deprecated functions.
  1591. // ASN1_PRINTABLE_type interprets |len| bytes from |s| as a Latin-1 string. It
  1592. // returns the first of |V_ASN1_PRINTABLESTRING|, |V_ASN1_IA5STRING|, or
  1593. // |V_ASN1_T61STRING| that can represent every character. If |len| is negative,
  1594. // |strlen(s)| is used instead.
  1595. //
  1596. // TODO(davidben): Remove this once all copies of Conscrypt have been updated
  1597. // past https://github.com/google/conscrypt/pull/1032.
  1598. OPENSSL_EXPORT int ASN1_PRINTABLE_type(const unsigned char *s, int len);
  1599. // ASN1_STRING_set_default_mask does nothing.
  1600. OPENSSL_EXPORT void ASN1_STRING_set_default_mask(unsigned long mask);
  1601. // ASN1_STRING_set_default_mask_asc returns one.
  1602. OPENSSL_EXPORT int ASN1_STRING_set_default_mask_asc(const char *p);
  1603. // ASN1_STRING_get_default_mask returns |B_ASN1_UTF8STRING|.
  1604. OPENSSL_EXPORT unsigned long ASN1_STRING_get_default_mask(void);
  1605. // ASN1_STRING_TABLE_cleanup does nothing.
  1606. OPENSSL_EXPORT void ASN1_STRING_TABLE_cleanup(void);
  1607. // M_ASN1_* are legacy aliases for various |ASN1_STRING| functions. Use the
  1608. // functions themselves.
  1609. #define M_ASN1_STRING_length(x) ASN1_STRING_length(x)
  1610. #define M_ASN1_STRING_type(x) ASN1_STRING_type(x)
  1611. #define M_ASN1_STRING_data(x) ASN1_STRING_data(x)
  1612. #define M_ASN1_BIT_STRING_new() ASN1_BIT_STRING_new()
  1613. #define M_ASN1_BIT_STRING_free(a) ASN1_BIT_STRING_free(a)
  1614. #define M_ASN1_BIT_STRING_dup(a) ASN1_STRING_dup(a)
  1615. #define M_ASN1_BIT_STRING_cmp(a, b) ASN1_STRING_cmp(a, b)
  1616. #define M_ASN1_BIT_STRING_set(a, b, c) ASN1_BIT_STRING_set(a, b, c)
  1617. #define M_ASN1_INTEGER_new() ASN1_INTEGER_new()
  1618. #define M_ASN1_INTEGER_free(a) ASN1_INTEGER_free(a)
  1619. #define M_ASN1_INTEGER_dup(a) ASN1_INTEGER_dup(a)
  1620. #define M_ASN1_INTEGER_cmp(a, b) ASN1_INTEGER_cmp(a, b)
  1621. #define M_ASN1_ENUMERATED_new() ASN1_ENUMERATED_new()
  1622. #define M_ASN1_ENUMERATED_free(a) ASN1_ENUMERATED_free(a)
  1623. #define M_ASN1_ENUMERATED_dup(a) ASN1_STRING_dup(a)
  1624. #define M_ASN1_ENUMERATED_cmp(a, b) ASN1_STRING_cmp(a, b)
  1625. #define M_ASN1_OCTET_STRING_new() ASN1_OCTET_STRING_new()
  1626. #define M_ASN1_OCTET_STRING_free(a) ASN1_OCTET_STRING_free()
  1627. #define M_ASN1_OCTET_STRING_dup(a) ASN1_OCTET_STRING_dup(a)
  1628. #define M_ASN1_OCTET_STRING_cmp(a, b) ASN1_OCTET_STRING_cmp(a, b)
  1629. #define M_ASN1_OCTET_STRING_set(a, b, c) ASN1_OCTET_STRING_set(a, b, c)
  1630. #define M_ASN1_OCTET_STRING_print(a, b) ASN1_STRING_print(a, b)
  1631. #define M_ASN1_PRINTABLESTRING_new() ASN1_PRINTABLESTRING_new()
  1632. #define M_ASN1_PRINTABLESTRING_free(a) ASN1_PRINTABLESTRING_free(a)
  1633. #define M_ASN1_IA5STRING_new() ASN1_IA5STRING_new()
  1634. #define M_ASN1_IA5STRING_free(a) ASN1_IA5STRING_free(a)
  1635. #define M_ASN1_IA5STRING_dup(a) ASN1_STRING_dup(a)
  1636. #define M_ASN1_UTCTIME_new() ASN1_UTCTIME_new()
  1637. #define M_ASN1_UTCTIME_free(a) ASN1_UTCTIME_free(a)
  1638. #define M_ASN1_UTCTIME_dup(a) ASN1_STRING_dup(a)
  1639. #define M_ASN1_T61STRING_new() ASN1_T61STRING_new()
  1640. #define M_ASN1_T61STRING_free(a) ASN1_T61STRING_free(a)
  1641. #define M_ASN1_GENERALIZEDTIME_new() ASN1_GENERALIZEDTIME_new()
  1642. #define M_ASN1_GENERALIZEDTIME_free(a) ASN1_GENERALIZEDTIME_free(a)
  1643. #define M_ASN1_GENERALIZEDTIME_dup(a) ASN1_STRING_dup(a)
  1644. #define M_ASN1_GENERALSTRING_new() ASN1_GENERALSTRING_new()
  1645. #define M_ASN1_GENERALSTRING_free(a) ASN1_GENERALSTRING_free(a)
  1646. #define M_ASN1_UNIVERSALSTRING_new() ASN1_UNIVERSALSTRING_new()
  1647. #define M_ASN1_UNIVERSALSTRING_free(a) ASN1_UNIVERSALSTRING_free(a)
  1648. #define M_ASN1_BMPSTRING_new() ASN1_BMPSTRING_new()
  1649. #define M_ASN1_BMPSTRING_free(a) ASN1_BMPSTRING_free(a)
  1650. #define M_ASN1_VISIBLESTRING_new() ASN1_VISIBLESTRING_new()
  1651. #define M_ASN1_VISIBLESTRING_free(a) ASN1_VISIBLESTRING_free(a)
  1652. #define M_ASN1_UTF8STRING_new() ASN1_UTF8STRING_new()
  1653. #define M_ASN1_UTF8STRING_free(a) ASN1_UTF8STRING_free(a)
  1654. // B_ASN1_PRINTABLE is a bitmask for an ad-hoc subset of string-like types. Note
  1655. // the presence of |B_ASN1_UNKNOWN| means it includes types which |ASN1_tag2bit|
  1656. // maps to |B_ASN1_UNKNOWN|.
  1657. //
  1658. // Do not use this. Despite the name, it has no connection to PrintableString or
  1659. // printable characters. See https://crbug.com/boringssl/412.
  1660. #define B_ASN1_PRINTABLE \
  1661. (B_ASN1_NUMERICSTRING | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | \
  1662. B_ASN1_IA5STRING | B_ASN1_BIT_STRING | B_ASN1_UNIVERSALSTRING | \
  1663. B_ASN1_BMPSTRING | B_ASN1_UTF8STRING | B_ASN1_SEQUENCE | B_ASN1_UNKNOWN)
  1664. // ASN1_PRINTABLE_new returns a newly-allocated |ASN1_STRING| with type -1, or
  1665. // NULL on error. The resulting |ASN1_STRING| is not a valid ASN.1 value until
  1666. // initialized with a value.
  1667. OPENSSL_EXPORT ASN1_STRING *ASN1_PRINTABLE_new(void);
  1668. // ASN1_PRINTABLE_free calls |ASN1_STRING_free|.
  1669. OPENSSL_EXPORT void ASN1_PRINTABLE_free(ASN1_STRING *str);
  1670. // d2i_ASN1_PRINTABLE parses up to |len| bytes from |*inp| as a DER-encoded
  1671. // CHOICE of an ad-hoc subset of string-like types, as described in
  1672. // |d2i_SAMPLE_with_reuse|.
  1673. //
  1674. // Do not use this. Despite, the name it has no connection to PrintableString or
  1675. // printable characters. See https://crbug.com/boringssl/412.
  1676. //
  1677. // TODO(https://crbug.com/boringssl/354): This function currently also accepts
  1678. // BER, but this will be removed in the future.
  1679. OPENSSL_EXPORT ASN1_STRING *d2i_ASN1_PRINTABLE(ASN1_STRING **out,
  1680. const uint8_t **inp, long len);
  1681. // i2d_ASN1_PRINTABLE marshals |in| as DER, as described in |i2d_SAMPLE|.
  1682. //
  1683. // Do not use this. Despite the name, it has no connection to PrintableString or
  1684. // printable characters. See https://crbug.com/boringssl/412.
  1685. OPENSSL_EXPORT int i2d_ASN1_PRINTABLE(const ASN1_STRING *in, uint8_t **outp);
  1686. // ASN1_PRINTABLE is an |ASN1_ITEM| whose ASN.1 type is a CHOICE of an ad-hoc
  1687. // subset of string-like types, and whose C type is |ASN1_STRING*|.
  1688. //
  1689. // Do not use this. Despite the name, it has no connection to PrintableString or
  1690. // printable characters. See https://crbug.com/boringssl/412.
  1691. DECLARE_ASN1_ITEM(ASN1_PRINTABLE)
  1692. #if defined(__cplusplus)
  1693. } // extern C
  1694. extern "C++" {
  1695. BSSL_NAMESPACE_BEGIN
  1696. BORINGSSL_MAKE_DELETER(ASN1_OBJECT, ASN1_OBJECT_free)
  1697. BORINGSSL_MAKE_DELETER(ASN1_STRING, ASN1_STRING_free)
  1698. BORINGSSL_MAKE_DELETER(ASN1_TYPE, ASN1_TYPE_free)
  1699. BSSL_NAMESPACE_END
  1700. } // extern C++
  1701. #endif
  1702. #define ASN1_R_ASN1_LENGTH_MISMATCH 100
  1703. #define ASN1_R_AUX_ERROR 101
  1704. #define ASN1_R_BAD_GET_ASN1_OBJECT_CALL 102
  1705. #define ASN1_R_BAD_OBJECT_HEADER 103
  1706. #define ASN1_R_BMPSTRING_IS_WRONG_LENGTH 104
  1707. #define ASN1_R_BN_LIB 105
  1708. #define ASN1_R_BOOLEAN_IS_WRONG_LENGTH 106
  1709. #define ASN1_R_BUFFER_TOO_SMALL 107
  1710. #define ASN1_R_CONTEXT_NOT_INITIALISED 108
  1711. #define ASN1_R_DECODE_ERROR 109
  1712. #define ASN1_R_DEPTH_EXCEEDED 110
  1713. #define ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED 111
  1714. #define ASN1_R_ENCODE_ERROR 112
  1715. #define ASN1_R_ERROR_GETTING_TIME 113
  1716. #define ASN1_R_EXPECTING_AN_ASN1_SEQUENCE 114
  1717. #define ASN1_R_EXPECTING_AN_INTEGER 115
  1718. #define ASN1_R_EXPECTING_AN_OBJECT 116
  1719. #define ASN1_R_EXPECTING_A_BOOLEAN 117
  1720. #define ASN1_R_EXPECTING_A_TIME 118
  1721. #define ASN1_R_EXPLICIT_LENGTH_MISMATCH 119
  1722. #define ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED 120
  1723. #define ASN1_R_FIELD_MISSING 121
  1724. #define ASN1_R_FIRST_NUM_TOO_LARGE 122
  1725. #define ASN1_R_HEADER_TOO_LONG 123
  1726. #define ASN1_R_ILLEGAL_BITSTRING_FORMAT 124
  1727. #define ASN1_R_ILLEGAL_BOOLEAN 125
  1728. #define ASN1_R_ILLEGAL_CHARACTERS 126
  1729. #define ASN1_R_ILLEGAL_FORMAT 127
  1730. #define ASN1_R_ILLEGAL_HEX 128
  1731. #define ASN1_R_ILLEGAL_IMPLICIT_TAG 129
  1732. #define ASN1_R_ILLEGAL_INTEGER 130
  1733. #define ASN1_R_ILLEGAL_NESTED_TAGGING 131
  1734. #define ASN1_R_ILLEGAL_NULL 132
  1735. #define ASN1_R_ILLEGAL_NULL_VALUE 133
  1736. #define ASN1_R_ILLEGAL_OBJECT 134
  1737. #define ASN1_R_ILLEGAL_OPTIONAL_ANY 135
  1738. #define ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE 136
  1739. #define ASN1_R_ILLEGAL_TAGGED_ANY 137
  1740. #define ASN1_R_ILLEGAL_TIME_VALUE 138
  1741. #define ASN1_R_INTEGER_NOT_ASCII_FORMAT 139
  1742. #define ASN1_R_INTEGER_TOO_LARGE_FOR_LONG 140
  1743. #define ASN1_R_INVALID_BIT_STRING_BITS_LEFT 141
  1744. #define ASN1_R_INVALID_BMPSTRING 142
  1745. #define ASN1_R_INVALID_DIGIT 143
  1746. #define ASN1_R_INVALID_MODIFIER 144
  1747. #define ASN1_R_INVALID_NUMBER 145
  1748. #define ASN1_R_INVALID_OBJECT_ENCODING 146
  1749. #define ASN1_R_INVALID_SEPARATOR 147
  1750. #define ASN1_R_INVALID_TIME_FORMAT 148
  1751. #define ASN1_R_INVALID_UNIVERSALSTRING 149
  1752. #define ASN1_R_INVALID_UTF8STRING 150
  1753. #define ASN1_R_LIST_ERROR 151
  1754. #define ASN1_R_MISSING_ASN1_EOS 152
  1755. #define ASN1_R_MISSING_EOC 153
  1756. #define ASN1_R_MISSING_SECOND_NUMBER 154
  1757. #define ASN1_R_MISSING_VALUE 155
  1758. #define ASN1_R_MSTRING_NOT_UNIVERSAL 156
  1759. #define ASN1_R_MSTRING_WRONG_TAG 157
  1760. #define ASN1_R_NESTED_ASN1_ERROR 158
  1761. #define ASN1_R_NESTED_ASN1_STRING 159
  1762. #define ASN1_R_NON_HEX_CHARACTERS 160
  1763. #define ASN1_R_NOT_ASCII_FORMAT 161
  1764. #define ASN1_R_NOT_ENOUGH_DATA 162
  1765. #define ASN1_R_NO_MATCHING_CHOICE_TYPE 163
  1766. #define ASN1_R_NULL_IS_WRONG_LENGTH 164
  1767. #define ASN1_R_OBJECT_NOT_ASCII_FORMAT 165
  1768. #define ASN1_R_ODD_NUMBER_OF_CHARS 166
  1769. #define ASN1_R_SECOND_NUMBER_TOO_LARGE 167
  1770. #define ASN1_R_SEQUENCE_LENGTH_MISMATCH 168
  1771. #define ASN1_R_SEQUENCE_NOT_CONSTRUCTED 169
  1772. #define ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG 170
  1773. #define ASN1_R_SHORT_LINE 171
  1774. #define ASN1_R_STREAMING_NOT_SUPPORTED 172
  1775. #define ASN1_R_STRING_TOO_LONG 173
  1776. #define ASN1_R_STRING_TOO_SHORT 174
  1777. #define ASN1_R_TAG_VALUE_TOO_HIGH 175
  1778. #define ASN1_R_TIME_NOT_ASCII_FORMAT 176
  1779. #define ASN1_R_TOO_LONG 177
  1780. #define ASN1_R_TYPE_NOT_CONSTRUCTED 178
  1781. #define ASN1_R_TYPE_NOT_PRIMITIVE 179
  1782. #define ASN1_R_UNEXPECTED_EOC 180
  1783. #define ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH 181
  1784. #define ASN1_R_UNKNOWN_FORMAT 182
  1785. #define ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM 183
  1786. #define ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM 184
  1787. #define ASN1_R_UNKNOWN_TAG 185
  1788. #define ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE 186
  1789. #define ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE 187
  1790. #define ASN1_R_UNSUPPORTED_TYPE 188
  1791. #define ASN1_R_WRONG_PUBLIC_KEY_TYPE 189
  1792. #define ASN1_R_WRONG_TAG 190
  1793. #define ASN1_R_WRONG_TYPE 191
  1794. #define ASN1_R_NESTED_TOO_DEEP 192
  1795. #define ASN1_R_BAD_TEMPLATE 193
  1796. #define ASN1_R_INVALID_BIT_STRING_PADDING 194
  1797. #define ASN1_R_WRONG_INTEGER_TYPE 195
  1798. #define ASN1_R_INVALID_INTEGER 196
  1799. #endif