2
0

asn1.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef HEADER_ASN1_H
  10. # define HEADER_ASN1_H
  11. # include <time.h>
  12. # include <openssl/e_os2.h>
  13. # include <openssl/opensslconf.h>
  14. # include <openssl/bio.h>
  15. # include <openssl/safestack.h>
  16. # include <openssl/asn1err.h>
  17. # include <openssl/symhacks.h>
  18. # include <openssl/ossl_typ.h>
  19. # if OPENSSL_API_COMPAT < 0x10100000L
  20. # include <openssl/bn.h>
  21. # endif
  22. # ifdef OPENSSL_BUILD_SHLIBCRYPTO
  23. # undef OPENSSL_EXTERN
  24. # define OPENSSL_EXTERN OPENSSL_EXPORT
  25. # endif
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. # define V_ASN1_UNIVERSAL 0x00
  30. # define V_ASN1_APPLICATION 0x40
  31. # define V_ASN1_CONTEXT_SPECIFIC 0x80
  32. # define V_ASN1_PRIVATE 0xc0
  33. # define V_ASN1_CONSTRUCTED 0x20
  34. # define V_ASN1_PRIMITIVE_TAG 0x1f
  35. # define V_ASN1_PRIMATIVE_TAG /*compat*/ V_ASN1_PRIMITIVE_TAG
  36. # define V_ASN1_APP_CHOOSE -2/* let the recipient choose */
  37. # define V_ASN1_OTHER -3/* used in ASN1_TYPE */
  38. # define V_ASN1_ANY -4/* used in ASN1 template code */
  39. # define V_ASN1_UNDEF -1
  40. /* ASN.1 tag values */
  41. # define V_ASN1_EOC 0
  42. # define V_ASN1_BOOLEAN 1 /**/
  43. # define V_ASN1_INTEGER 2
  44. # define V_ASN1_BIT_STRING 3
  45. # define V_ASN1_OCTET_STRING 4
  46. # define V_ASN1_NULL 5
  47. # define V_ASN1_OBJECT 6
  48. # define V_ASN1_OBJECT_DESCRIPTOR 7
  49. # define V_ASN1_EXTERNAL 8
  50. # define V_ASN1_REAL 9
  51. # define V_ASN1_ENUMERATED 10
  52. # define V_ASN1_UTF8STRING 12
  53. # define V_ASN1_SEQUENCE 16
  54. # define V_ASN1_SET 17
  55. # define V_ASN1_NUMERICSTRING 18 /**/
  56. # define V_ASN1_PRINTABLESTRING 19
  57. # define V_ASN1_T61STRING 20
  58. # define V_ASN1_TELETEXSTRING 20/* alias */
  59. # define V_ASN1_VIDEOTEXSTRING 21 /**/
  60. # define V_ASN1_IA5STRING 22
  61. # define V_ASN1_UTCTIME 23
  62. # define V_ASN1_GENERALIZEDTIME 24 /**/
  63. # define V_ASN1_GRAPHICSTRING 25 /**/
  64. # define V_ASN1_ISO64STRING 26 /**/
  65. # define V_ASN1_VISIBLESTRING 26/* alias */
  66. # define V_ASN1_GENERALSTRING 27 /**/
  67. # define V_ASN1_UNIVERSALSTRING 28 /**/
  68. # define V_ASN1_BMPSTRING 30
  69. /*
  70. * NB the constants below are used internally by ASN1_INTEGER
  71. * and ASN1_ENUMERATED to indicate the sign. They are *not* on
  72. * the wire tag values.
  73. */
  74. # define V_ASN1_NEG 0x100
  75. # define V_ASN1_NEG_INTEGER (2 | V_ASN1_NEG)
  76. # define V_ASN1_NEG_ENUMERATED (10 | V_ASN1_NEG)
  77. /* For use with d2i_ASN1_type_bytes() */
  78. # define B_ASN1_NUMERICSTRING 0x0001
  79. # define B_ASN1_PRINTABLESTRING 0x0002
  80. # define B_ASN1_T61STRING 0x0004
  81. # define B_ASN1_TELETEXSTRING 0x0004
  82. # define B_ASN1_VIDEOTEXSTRING 0x0008
  83. # define B_ASN1_IA5STRING 0x0010
  84. # define B_ASN1_GRAPHICSTRING 0x0020
  85. # define B_ASN1_ISO64STRING 0x0040
  86. # define B_ASN1_VISIBLESTRING 0x0040
  87. # define B_ASN1_GENERALSTRING 0x0080
  88. # define B_ASN1_UNIVERSALSTRING 0x0100
  89. # define B_ASN1_OCTET_STRING 0x0200
  90. # define B_ASN1_BIT_STRING 0x0400
  91. # define B_ASN1_BMPSTRING 0x0800
  92. # define B_ASN1_UNKNOWN 0x1000
  93. # define B_ASN1_UTF8STRING 0x2000
  94. # define B_ASN1_UTCTIME 0x4000
  95. # define B_ASN1_GENERALIZEDTIME 0x8000
  96. # define B_ASN1_SEQUENCE 0x10000
  97. /* For use with ASN1_mbstring_copy() */
  98. # define MBSTRING_FLAG 0x1000
  99. # define MBSTRING_UTF8 (MBSTRING_FLAG)
  100. # define MBSTRING_ASC (MBSTRING_FLAG|1)
  101. # define MBSTRING_BMP (MBSTRING_FLAG|2)
  102. # define MBSTRING_UNIV (MBSTRING_FLAG|4)
  103. # define SMIME_OLDMIME 0x400
  104. # define SMIME_CRLFEOL 0x800
  105. # define SMIME_STREAM 0x1000
  106. struct X509_algor_st;
  107. DEFINE_STACK_OF(X509_ALGOR)
  108. # define ASN1_STRING_FLAG_BITS_LEFT 0x08/* Set if 0x07 has bits left value */
  109. /*
  110. * This indicates that the ASN1_STRING is not a real value but just a place
  111. * holder for the location where indefinite length constructed data should be
  112. * inserted in the memory buffer
  113. */
  114. # define ASN1_STRING_FLAG_NDEF 0x010
  115. /*
  116. * This flag is used by the CMS code to indicate that a string is not
  117. * complete and is a place holder for content when it had all been accessed.
  118. * The flag will be reset when content has been written to it.
  119. */
  120. # define ASN1_STRING_FLAG_CONT 0x020
  121. /*
  122. * This flag is used by ASN1 code to indicate an ASN1_STRING is an MSTRING
  123. * type.
  124. */
  125. # define ASN1_STRING_FLAG_MSTRING 0x040
  126. /* String is embedded and only content should be freed */
  127. # define ASN1_STRING_FLAG_EMBED 0x080
  128. /* String should be parsed in RFC 5280's time format */
  129. # define ASN1_STRING_FLAG_X509_TIME 0x100
  130. /* This is the base type that holds just about everything :-) */
  131. struct asn1_string_st {
  132. int length;
  133. int type;
  134. unsigned char *data;
  135. /*
  136. * The value of the following field depends on the type being held. It
  137. * is mostly being used for BIT_STRING so if the input data has a
  138. * non-zero 'unused bits' value, it will be handled correctly
  139. */
  140. long flags;
  141. };
  142. /*
  143. * ASN1_ENCODING structure: this is used to save the received encoding of an
  144. * ASN1 type. This is useful to get round problems with invalid encodings
  145. * which can break signatures.
  146. */
  147. typedef struct ASN1_ENCODING_st {
  148. unsigned char *enc; /* DER encoding */
  149. long len; /* Length of encoding */
  150. int modified; /* set to 1 if 'enc' is invalid */
  151. } ASN1_ENCODING;
  152. /* Used with ASN1 LONG type: if a long is set to this it is omitted */
  153. # define ASN1_LONG_UNDEF 0x7fffffffL
  154. # define STABLE_FLAGS_MALLOC 0x01
  155. /*
  156. * A zero passed to ASN1_STRING_TABLE_new_add for the flags is interpreted
  157. * as "don't change" and STABLE_FLAGS_MALLOC is always set. By setting
  158. * STABLE_FLAGS_MALLOC only we can clear the existing value. Use the alias
  159. * STABLE_FLAGS_CLEAR to reflect this.
  160. */
  161. # define STABLE_FLAGS_CLEAR STABLE_FLAGS_MALLOC
  162. # define STABLE_NO_MASK 0x02
  163. # define DIRSTRING_TYPE \
  164. (B_ASN1_PRINTABLESTRING|B_ASN1_T61STRING|B_ASN1_BMPSTRING|B_ASN1_UTF8STRING)
  165. # define PKCS9STRING_TYPE (DIRSTRING_TYPE|B_ASN1_IA5STRING)
  166. typedef struct asn1_string_table_st {
  167. int nid;
  168. long minsize;
  169. long maxsize;
  170. unsigned long mask;
  171. unsigned long flags;
  172. } ASN1_STRING_TABLE;
  173. DEFINE_STACK_OF(ASN1_STRING_TABLE)
  174. /* size limits: this stuff is taken straight from RFC2459 */
  175. # define ub_name 32768
  176. # define ub_common_name 64
  177. # define ub_locality_name 128
  178. # define ub_state_name 128
  179. # define ub_organization_name 64
  180. # define ub_organization_unit_name 64
  181. # define ub_title 64
  182. # define ub_email_address 128
  183. /*
  184. * Declarations for template structures: for full definitions see asn1t.h
  185. */
  186. typedef struct ASN1_TEMPLATE_st ASN1_TEMPLATE;
  187. typedef struct ASN1_TLC_st ASN1_TLC;
  188. /* This is just an opaque pointer */
  189. typedef struct ASN1_VALUE_st ASN1_VALUE;
  190. /* Declare ASN1 functions: the implement macro in in asn1t.h */
  191. # define DECLARE_ASN1_FUNCTIONS(type) DECLARE_ASN1_FUNCTIONS_name(type, type)
  192. # define DECLARE_ASN1_ALLOC_FUNCTIONS(type) \
  193. DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, type)
  194. # define DECLARE_ASN1_FUNCTIONS_name(type, name) \
  195. DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \
  196. DECLARE_ASN1_ENCODE_FUNCTIONS(type, name, name)
  197. # define DECLARE_ASN1_FUNCTIONS_fname(type, itname, name) \
  198. DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \
  199. DECLARE_ASN1_ENCODE_FUNCTIONS(type, itname, name)
  200. # define DECLARE_ASN1_ENCODE_FUNCTIONS(type, itname, name) \
  201. type *d2i_##name(type **a, const unsigned char **in, long len); \
  202. int i2d_##name(type *a, unsigned char **out); \
  203. DECLARE_ASN1_ITEM(itname)
  204. # define DECLARE_ASN1_ENCODE_FUNCTIONS_const(type, name) \
  205. type *d2i_##name(type **a, const unsigned char **in, long len); \
  206. int i2d_##name(const type *a, unsigned char **out); \
  207. DECLARE_ASN1_ITEM(name)
  208. # define DECLARE_ASN1_NDEF_FUNCTION(name) \
  209. int i2d_##name##_NDEF(name *a, unsigned char **out);
  210. # define DECLARE_ASN1_FUNCTIONS_const(name) \
  211. DECLARE_ASN1_ALLOC_FUNCTIONS(name) \
  212. DECLARE_ASN1_ENCODE_FUNCTIONS_const(name, name)
  213. # define DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \
  214. type *name##_new(void); \
  215. void name##_free(type *a);
  216. # define DECLARE_ASN1_PRINT_FUNCTION(stname) \
  217. DECLARE_ASN1_PRINT_FUNCTION_fname(stname, stname)
  218. # define DECLARE_ASN1_PRINT_FUNCTION_fname(stname, fname) \
  219. int fname##_print_ctx(BIO *out, stname *x, int indent, \
  220. const ASN1_PCTX *pctx);
  221. # define D2I_OF(type) type *(*)(type **,const unsigned char **,long)
  222. # define I2D_OF(type) int (*)(type *,unsigned char **)
  223. # define I2D_OF_const(type) int (*)(const type *,unsigned char **)
  224. # define CHECKED_D2I_OF(type, d2i) \
  225. ((d2i_of_void*) (1 ? d2i : ((D2I_OF(type))0)))
  226. # define CHECKED_I2D_OF(type, i2d) \
  227. ((i2d_of_void*) (1 ? i2d : ((I2D_OF(type))0)))
  228. # define CHECKED_NEW_OF(type, xnew) \
  229. ((void *(*)(void)) (1 ? xnew : ((type *(*)(void))0)))
  230. # define CHECKED_PTR_OF(type, p) \
  231. ((void*) (1 ? p : (type*)0))
  232. # define CHECKED_PPTR_OF(type, p) \
  233. ((void**) (1 ? p : (type**)0))
  234. # define TYPEDEF_D2I_OF(type) typedef type *d2i_of_##type(type **,const unsigned char **,long)
  235. # define TYPEDEF_I2D_OF(type) typedef int i2d_of_##type(type *,unsigned char **)
  236. # define TYPEDEF_D2I2D_OF(type) TYPEDEF_D2I_OF(type); TYPEDEF_I2D_OF(type)
  237. TYPEDEF_D2I2D_OF(void);
  238. /*-
  239. * The following macros and typedefs allow an ASN1_ITEM
  240. * to be embedded in a structure and referenced. Since
  241. * the ASN1_ITEM pointers need to be globally accessible
  242. * (possibly from shared libraries) they may exist in
  243. * different forms. On platforms that support it the
  244. * ASN1_ITEM structure itself will be globally exported.
  245. * Other platforms will export a function that returns
  246. * an ASN1_ITEM pointer.
  247. *
  248. * To handle both cases transparently the macros below
  249. * should be used instead of hard coding an ASN1_ITEM
  250. * pointer in a structure.
  251. *
  252. * The structure will look like this:
  253. *
  254. * typedef struct SOMETHING_st {
  255. * ...
  256. * ASN1_ITEM_EXP *iptr;
  257. * ...
  258. * } SOMETHING;
  259. *
  260. * It would be initialised as e.g.:
  261. *
  262. * SOMETHING somevar = {...,ASN1_ITEM_ref(X509),...};
  263. *
  264. * and the actual pointer extracted with:
  265. *
  266. * const ASN1_ITEM *it = ASN1_ITEM_ptr(somevar.iptr);
  267. *
  268. * Finally an ASN1_ITEM pointer can be extracted from an
  269. * appropriate reference with: ASN1_ITEM_rptr(X509). This
  270. * would be used when a function takes an ASN1_ITEM * argument.
  271. *
  272. */
  273. # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
  274. /* ASN1_ITEM pointer exported type */
  275. typedef const ASN1_ITEM ASN1_ITEM_EXP;
  276. /* Macro to obtain ASN1_ITEM pointer from exported type */
  277. # define ASN1_ITEM_ptr(iptr) (iptr)
  278. /* Macro to include ASN1_ITEM pointer from base type */
  279. # define ASN1_ITEM_ref(iptr) (&(iptr##_it))
  280. # define ASN1_ITEM_rptr(ref) (&(ref##_it))
  281. # define DECLARE_ASN1_ITEM(name) \
  282. OPENSSL_EXTERN const ASN1_ITEM name##_it;
  283. # else
  284. /*
  285. * Platforms that can't easily handle shared global variables are declared as
  286. * functions returning ASN1_ITEM pointers.
  287. */
  288. /* ASN1_ITEM pointer exported type */
  289. typedef const ASN1_ITEM *ASN1_ITEM_EXP (void);
  290. /* Macro to obtain ASN1_ITEM pointer from exported type */
  291. # define ASN1_ITEM_ptr(iptr) (iptr())
  292. /* Macro to include ASN1_ITEM pointer from base type */
  293. # define ASN1_ITEM_ref(iptr) (iptr##_it)
  294. # define ASN1_ITEM_rptr(ref) (ref##_it())
  295. # define DECLARE_ASN1_ITEM(name) \
  296. const ASN1_ITEM * name##_it(void);
  297. # endif
  298. /* Parameters used by ASN1_STRING_print_ex() */
  299. /*
  300. * These determine which characters to escape: RFC2253 special characters,
  301. * control characters and MSB set characters
  302. */
  303. # define ASN1_STRFLGS_ESC_2253 1
  304. # define ASN1_STRFLGS_ESC_CTRL 2
  305. # define ASN1_STRFLGS_ESC_MSB 4
  306. /*
  307. * This flag determines how we do escaping: normally RC2253 backslash only,
  308. * set this to use backslash and quote.
  309. */
  310. # define ASN1_STRFLGS_ESC_QUOTE 8
  311. /* These three flags are internal use only. */
  312. /* Character is a valid PrintableString character */
  313. # define CHARTYPE_PRINTABLESTRING 0x10
  314. /* Character needs escaping if it is the first character */
  315. # define CHARTYPE_FIRST_ESC_2253 0x20
  316. /* Character needs escaping if it is the last character */
  317. # define CHARTYPE_LAST_ESC_2253 0x40
  318. /*
  319. * NB the internal flags are safely reused below by flags handled at the top
  320. * level.
  321. */
  322. /*
  323. * If this is set we convert all character strings to UTF8 first
  324. */
  325. # define ASN1_STRFLGS_UTF8_CONVERT 0x10
  326. /*
  327. * If this is set we don't attempt to interpret content: just assume all
  328. * strings are 1 byte per character. This will produce some pretty odd
  329. * looking output!
  330. */
  331. # define ASN1_STRFLGS_IGNORE_TYPE 0x20
  332. /* If this is set we include the string type in the output */
  333. # define ASN1_STRFLGS_SHOW_TYPE 0x40
  334. /*
  335. * This determines which strings to display and which to 'dump' (hex dump of
  336. * content octets or DER encoding). We can only dump non character strings or
  337. * everything. If we don't dump 'unknown' they are interpreted as character
  338. * strings with 1 octet per character and are subject to the usual escaping
  339. * options.
  340. */
  341. # define ASN1_STRFLGS_DUMP_ALL 0x80
  342. # define ASN1_STRFLGS_DUMP_UNKNOWN 0x100
  343. /*
  344. * These determine what 'dumping' does, we can dump the content octets or the
  345. * DER encoding: both use the RFC2253 #XXXXX notation.
  346. */
  347. # define ASN1_STRFLGS_DUMP_DER 0x200
  348. /*
  349. * This flag specifies that RC2254 escaping shall be performed.
  350. */
  351. #define ASN1_STRFLGS_ESC_2254 0x400
  352. /*
  353. * All the string flags consistent with RFC2253, escaping control characters
  354. * isn't essential in RFC2253 but it is advisable anyway.
  355. */
  356. # define ASN1_STRFLGS_RFC2253 (ASN1_STRFLGS_ESC_2253 | \
  357. ASN1_STRFLGS_ESC_CTRL | \
  358. ASN1_STRFLGS_ESC_MSB | \
  359. ASN1_STRFLGS_UTF8_CONVERT | \
  360. ASN1_STRFLGS_DUMP_UNKNOWN | \
  361. ASN1_STRFLGS_DUMP_DER)
  362. DEFINE_STACK_OF(ASN1_INTEGER)
  363. DEFINE_STACK_OF(ASN1_GENERALSTRING)
  364. DEFINE_STACK_OF(ASN1_UTF8STRING)
  365. typedef struct asn1_type_st {
  366. int type;
  367. union {
  368. char *ptr;
  369. ASN1_BOOLEAN boolean;
  370. ASN1_STRING *asn1_string;
  371. ASN1_OBJECT *object;
  372. ASN1_INTEGER *integer;
  373. ASN1_ENUMERATED *enumerated;
  374. ASN1_BIT_STRING *bit_string;
  375. ASN1_OCTET_STRING *octet_string;
  376. ASN1_PRINTABLESTRING *printablestring;
  377. ASN1_T61STRING *t61string;
  378. ASN1_IA5STRING *ia5string;
  379. ASN1_GENERALSTRING *generalstring;
  380. ASN1_BMPSTRING *bmpstring;
  381. ASN1_UNIVERSALSTRING *universalstring;
  382. ASN1_UTCTIME *utctime;
  383. ASN1_GENERALIZEDTIME *generalizedtime;
  384. ASN1_VISIBLESTRING *visiblestring;
  385. ASN1_UTF8STRING *utf8string;
  386. /*
  387. * set and sequence are left complete and still contain the set or
  388. * sequence bytes
  389. */
  390. ASN1_STRING *set;
  391. ASN1_STRING *sequence;
  392. ASN1_VALUE *asn1_value;
  393. } value;
  394. } ASN1_TYPE;
  395. DEFINE_STACK_OF(ASN1_TYPE)
  396. typedef STACK_OF(ASN1_TYPE) ASN1_SEQUENCE_ANY;
  397. DECLARE_ASN1_ENCODE_FUNCTIONS_const(ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY)
  398. DECLARE_ASN1_ENCODE_FUNCTIONS_const(ASN1_SEQUENCE_ANY, ASN1_SET_ANY)
  399. /* This is used to contain a list of bit names */
  400. typedef struct BIT_STRING_BITNAME_st {
  401. int bitnum;
  402. const char *lname;
  403. const char *sname;
  404. } BIT_STRING_BITNAME;
  405. # define B_ASN1_TIME \
  406. B_ASN1_UTCTIME | \
  407. B_ASN1_GENERALIZEDTIME
  408. # define B_ASN1_PRINTABLE \
  409. B_ASN1_NUMERICSTRING| \
  410. B_ASN1_PRINTABLESTRING| \
  411. B_ASN1_T61STRING| \
  412. B_ASN1_IA5STRING| \
  413. B_ASN1_BIT_STRING| \
  414. B_ASN1_UNIVERSALSTRING|\
  415. B_ASN1_BMPSTRING|\
  416. B_ASN1_UTF8STRING|\
  417. B_ASN1_SEQUENCE|\
  418. B_ASN1_UNKNOWN
  419. # define B_ASN1_DIRECTORYSTRING \
  420. B_ASN1_PRINTABLESTRING| \
  421. B_ASN1_TELETEXSTRING|\
  422. B_ASN1_BMPSTRING|\
  423. B_ASN1_UNIVERSALSTRING|\
  424. B_ASN1_UTF8STRING
  425. # define B_ASN1_DISPLAYTEXT \
  426. B_ASN1_IA5STRING| \
  427. B_ASN1_VISIBLESTRING| \
  428. B_ASN1_BMPSTRING|\
  429. B_ASN1_UTF8STRING
  430. DECLARE_ASN1_FUNCTIONS_fname(ASN1_TYPE, ASN1_ANY, ASN1_TYPE)
  431. int ASN1_TYPE_get(const ASN1_TYPE *a);
  432. void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value);
  433. int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value);
  434. int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b);
  435. ASN1_TYPE *ASN1_TYPE_pack_sequence(const ASN1_ITEM *it, void *s, ASN1_TYPE **t);
  436. void *ASN1_TYPE_unpack_sequence(const ASN1_ITEM *it, const ASN1_TYPE *t);
  437. ASN1_OBJECT *ASN1_OBJECT_new(void);
  438. void ASN1_OBJECT_free(ASN1_OBJECT *a);
  439. int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp);
  440. ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
  441. long length);
  442. DECLARE_ASN1_ITEM(ASN1_OBJECT)
  443. DEFINE_STACK_OF(ASN1_OBJECT)
  444. ASN1_STRING *ASN1_STRING_new(void);
  445. void ASN1_STRING_free(ASN1_STRING *a);
  446. void ASN1_STRING_clear_free(ASN1_STRING *a);
  447. int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str);
  448. ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *a);
  449. ASN1_STRING *ASN1_STRING_type_new(int type);
  450. int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b);
  451. /*
  452. * Since this is used to store all sorts of things, via macros, for now,
  453. * make its data void *
  454. */
  455. int ASN1_STRING_set(ASN1_STRING *str, const void *data, int len);
  456. void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len);
  457. int ASN1_STRING_length(const ASN1_STRING *x);
  458. void ASN1_STRING_length_set(ASN1_STRING *x, int n);
  459. int ASN1_STRING_type(const ASN1_STRING *x);
  460. DEPRECATEDIN_1_1_0(unsigned char *ASN1_STRING_data(ASN1_STRING *x))
  461. const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x);
  462. DECLARE_ASN1_FUNCTIONS(ASN1_BIT_STRING)
  463. int ASN1_BIT_STRING_set(ASN1_BIT_STRING *a, unsigned char *d, int length);
  464. int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value);
  465. int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n);
  466. int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
  467. const unsigned char *flags, int flags_len);
  468. int ASN1_BIT_STRING_name_print(BIO *out, ASN1_BIT_STRING *bs,
  469. BIT_STRING_BITNAME *tbl, int indent);
  470. int ASN1_BIT_STRING_num_asc(const char *name, BIT_STRING_BITNAME *tbl);
  471. int ASN1_BIT_STRING_set_asc(ASN1_BIT_STRING *bs, const char *name, int value,
  472. BIT_STRING_BITNAME *tbl);
  473. DECLARE_ASN1_FUNCTIONS(ASN1_INTEGER)
  474. ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  475. long length);
  476. ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x);
  477. int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y);
  478. DECLARE_ASN1_FUNCTIONS(ASN1_ENUMERATED)
  479. int ASN1_UTCTIME_check(const ASN1_UTCTIME *a);
  480. ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t);
  481. ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
  482. int offset_day, long offset_sec);
  483. int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str);
  484. int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t);
  485. int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *a);
  486. ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
  487. time_t t);
  488. ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
  489. time_t t, int offset_day,
  490. long offset_sec);
  491. int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str);
  492. int ASN1_TIME_diff(int *pday, int *psec,
  493. const ASN1_TIME *from, const ASN1_TIME *to);
  494. DECLARE_ASN1_FUNCTIONS(ASN1_OCTET_STRING)
  495. ASN1_OCTET_STRING *ASN1_OCTET_STRING_dup(const ASN1_OCTET_STRING *a);
  496. int ASN1_OCTET_STRING_cmp(const ASN1_OCTET_STRING *a,
  497. const ASN1_OCTET_STRING *b);
  498. int ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *str, const unsigned char *data,
  499. int len);
  500. DECLARE_ASN1_FUNCTIONS(ASN1_VISIBLESTRING)
  501. DECLARE_ASN1_FUNCTIONS(ASN1_UNIVERSALSTRING)
  502. DECLARE_ASN1_FUNCTIONS(ASN1_UTF8STRING)
  503. DECLARE_ASN1_FUNCTIONS(ASN1_NULL)
  504. DECLARE_ASN1_FUNCTIONS(ASN1_BMPSTRING)
  505. int UTF8_getc(const unsigned char *str, int len, unsigned long *val);
  506. int UTF8_putc(unsigned char *str, int len, unsigned long value);
  507. DECLARE_ASN1_FUNCTIONS_name(ASN1_STRING, ASN1_PRINTABLE)
  508. DECLARE_ASN1_FUNCTIONS_name(ASN1_STRING, DIRECTORYSTRING)
  509. DECLARE_ASN1_FUNCTIONS_name(ASN1_STRING, DISPLAYTEXT)
  510. DECLARE_ASN1_FUNCTIONS(ASN1_PRINTABLESTRING)
  511. DECLARE_ASN1_FUNCTIONS(ASN1_T61STRING)
  512. DECLARE_ASN1_FUNCTIONS(ASN1_IA5STRING)
  513. DECLARE_ASN1_FUNCTIONS(ASN1_GENERALSTRING)
  514. DECLARE_ASN1_FUNCTIONS(ASN1_UTCTIME)
  515. DECLARE_ASN1_FUNCTIONS(ASN1_GENERALIZEDTIME)
  516. DECLARE_ASN1_FUNCTIONS(ASN1_TIME)
  517. DECLARE_ASN1_ITEM(ASN1_OCTET_STRING_NDEF)
  518. ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t);
  519. ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
  520. int offset_day, long offset_sec);
  521. int ASN1_TIME_check(const ASN1_TIME *t);
  522. ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
  523. ASN1_GENERALIZEDTIME **out);
  524. int ASN1_TIME_set_string(ASN1_TIME *s, const char *str);
  525. int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str);
  526. int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm);
  527. int ASN1_TIME_normalize(ASN1_TIME *s);
  528. int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t);
  529. int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b);
  530. int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a);
  531. int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size);
  532. int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a);
  533. int a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size);
  534. int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a);
  535. int a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size);
  536. int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type);
  537. int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a);
  538. int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num);
  539. ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
  540. const char *sn, const char *ln);
  541. int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a);
  542. int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r);
  543. int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a);
  544. int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r);
  545. int ASN1_INTEGER_set(ASN1_INTEGER *a, long v);
  546. long ASN1_INTEGER_get(const ASN1_INTEGER *a);
  547. ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai);
  548. BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn);
  549. int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a);
  550. int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r);
  551. int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v);
  552. long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a);
  553. ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai);
  554. BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn);
  555. /* General */
  556. /* given a string, return the correct type, max is the maximum length */
  557. int ASN1_PRINTABLE_type(const unsigned char *s, int max);
  558. unsigned long ASN1_tag2bit(int tag);
  559. /* SPECIALS */
  560. int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
  561. int *pclass, long omax);
  562. int ASN1_check_infinite_end(unsigned char **p, long len);
  563. int ASN1_const_check_infinite_end(const unsigned char **p, long len);
  564. void ASN1_put_object(unsigned char **pp, int constructed, int length,
  565. int tag, int xclass);
  566. int ASN1_put_eoc(unsigned char **pp);
  567. int ASN1_object_size(int constructed, int length, int tag);
  568. /* Used to implement other functions */
  569. void *ASN1_dup(i2d_of_void *i2d, d2i_of_void *d2i, void *x);
  570. # define ASN1_dup_of(type,i2d,d2i,x) \
  571. ((type*)ASN1_dup(CHECKED_I2D_OF(type, i2d), \
  572. CHECKED_D2I_OF(type, d2i), \
  573. CHECKED_PTR_OF(type, x)))
  574. # define ASN1_dup_of_const(type,i2d,d2i,x) \
  575. ((type*)ASN1_dup(CHECKED_I2D_OF(const type, i2d), \
  576. CHECKED_D2I_OF(type, d2i), \
  577. CHECKED_PTR_OF(const type, x)))
  578. void *ASN1_item_dup(const ASN1_ITEM *it, void *x);
  579. /* ASN1 alloc/free macros for when a type is only used internally */
  580. # define M_ASN1_new_of(type) (type *)ASN1_item_new(ASN1_ITEM_rptr(type))
  581. # define M_ASN1_free_of(x, type) \
  582. ASN1_item_free(CHECKED_PTR_OF(type, x), ASN1_ITEM_rptr(type))
  583. # ifndef OPENSSL_NO_STDIO
  584. void *ASN1_d2i_fp(void *(*xnew) (void), d2i_of_void *d2i, FILE *in, void **x);
  585. # define ASN1_d2i_fp_of(type,xnew,d2i,in,x) \
  586. ((type*)ASN1_d2i_fp(CHECKED_NEW_OF(type, xnew), \
  587. CHECKED_D2I_OF(type, d2i), \
  588. in, \
  589. CHECKED_PPTR_OF(type, x)))
  590. void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x);
  591. int ASN1_i2d_fp(i2d_of_void *i2d, FILE *out, void *x);
  592. # define ASN1_i2d_fp_of(type,i2d,out,x) \
  593. (ASN1_i2d_fp(CHECKED_I2D_OF(type, i2d), \
  594. out, \
  595. CHECKED_PTR_OF(type, x)))
  596. # define ASN1_i2d_fp_of_const(type,i2d,out,x) \
  597. (ASN1_i2d_fp(CHECKED_I2D_OF(const type, i2d), \
  598. out, \
  599. CHECKED_PTR_OF(const type, x)))
  600. int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, void *x);
  601. int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags);
  602. # endif
  603. int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in);
  604. void *ASN1_d2i_bio(void *(*xnew) (void), d2i_of_void *d2i, BIO *in, void **x);
  605. # define ASN1_d2i_bio_of(type,xnew,d2i,in,x) \
  606. ((type*)ASN1_d2i_bio( CHECKED_NEW_OF(type, xnew), \
  607. CHECKED_D2I_OF(type, d2i), \
  608. in, \
  609. CHECKED_PPTR_OF(type, x)))
  610. void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x);
  611. int ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, unsigned char *x);
  612. # define ASN1_i2d_bio_of(type,i2d,out,x) \
  613. (ASN1_i2d_bio(CHECKED_I2D_OF(type, i2d), \
  614. out, \
  615. CHECKED_PTR_OF(type, x)))
  616. # define ASN1_i2d_bio_of_const(type,i2d,out,x) \
  617. (ASN1_i2d_bio(CHECKED_I2D_OF(const type, i2d), \
  618. out, \
  619. CHECKED_PTR_OF(const type, x)))
  620. int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *x);
  621. int ASN1_UTCTIME_print(BIO *fp, const ASN1_UTCTIME *a);
  622. int ASN1_GENERALIZEDTIME_print(BIO *fp, const ASN1_GENERALIZEDTIME *a);
  623. int ASN1_TIME_print(BIO *fp, const ASN1_TIME *a);
  624. int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v);
  625. int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags);
  626. int ASN1_buf_print(BIO *bp, const unsigned char *buf, size_t buflen, int off);
  627. int ASN1_bn_print(BIO *bp, const char *number, const BIGNUM *num,
  628. unsigned char *buf, int off);
  629. int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent);
  630. int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent,
  631. int dump);
  632. const char *ASN1_tag2str(int tag);
  633. /* Used to load and write Netscape format cert */
  634. int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s);
  635. int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len);
  636. int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len);
  637. int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num,
  638. unsigned char *data, int len);
  639. int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num,
  640. unsigned char *data, int max_len);
  641. void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it);
  642. ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it,
  643. ASN1_OCTET_STRING **oct);
  644. void ASN1_STRING_set_default_mask(unsigned long mask);
  645. int ASN1_STRING_set_default_mask_asc(const char *p);
  646. unsigned long ASN1_STRING_get_default_mask(void);
  647. int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
  648. int inform, unsigned long mask);
  649. int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
  650. int inform, unsigned long mask,
  651. long minsize, long maxsize);
  652. ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
  653. const unsigned char *in, int inlen,
  654. int inform, int nid);
  655. ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid);
  656. int ASN1_STRING_TABLE_add(int, long, long, unsigned long, unsigned long);
  657. void ASN1_STRING_TABLE_cleanup(void);
  658. /* ASN1 template functions */
  659. /* Old API compatible functions */
  660. ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it);
  661. void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it);
  662. ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **val, const unsigned char **in,
  663. long len, const ASN1_ITEM *it);
  664. int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it);
  665. int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
  666. const ASN1_ITEM *it);
  667. void ASN1_add_oid_module(void);
  668. void ASN1_add_stable_module(void);
  669. ASN1_TYPE *ASN1_generate_nconf(const char *str, CONF *nconf);
  670. ASN1_TYPE *ASN1_generate_v3(const char *str, X509V3_CTX *cnf);
  671. int ASN1_str2mask(const char *str, unsigned long *pmask);
  672. /* ASN1 Print flags */
  673. /* Indicate missing OPTIONAL fields */
  674. # define ASN1_PCTX_FLAGS_SHOW_ABSENT 0x001
  675. /* Mark start and end of SEQUENCE */
  676. # define ASN1_PCTX_FLAGS_SHOW_SEQUENCE 0x002
  677. /* Mark start and end of SEQUENCE/SET OF */
  678. # define ASN1_PCTX_FLAGS_SHOW_SSOF 0x004
  679. /* Show the ASN1 type of primitives */
  680. # define ASN1_PCTX_FLAGS_SHOW_TYPE 0x008
  681. /* Don't show ASN1 type of ANY */
  682. # define ASN1_PCTX_FLAGS_NO_ANY_TYPE 0x010
  683. /* Don't show ASN1 type of MSTRINGs */
  684. # define ASN1_PCTX_FLAGS_NO_MSTRING_TYPE 0x020
  685. /* Don't show field names in SEQUENCE */
  686. # define ASN1_PCTX_FLAGS_NO_FIELD_NAME 0x040
  687. /* Show structure names of each SEQUENCE field */
  688. # define ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME 0x080
  689. /* Don't show structure name even at top level */
  690. # define ASN1_PCTX_FLAGS_NO_STRUCT_NAME 0x100
  691. int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
  692. const ASN1_ITEM *it, const ASN1_PCTX *pctx);
  693. ASN1_PCTX *ASN1_PCTX_new(void);
  694. void ASN1_PCTX_free(ASN1_PCTX *p);
  695. unsigned long ASN1_PCTX_get_flags(const ASN1_PCTX *p);
  696. void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags);
  697. unsigned long ASN1_PCTX_get_nm_flags(const ASN1_PCTX *p);
  698. void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags);
  699. unsigned long ASN1_PCTX_get_cert_flags(const ASN1_PCTX *p);
  700. void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags);
  701. unsigned long ASN1_PCTX_get_oid_flags(const ASN1_PCTX *p);
  702. void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags);
  703. unsigned long ASN1_PCTX_get_str_flags(const ASN1_PCTX *p);
  704. void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags);
  705. ASN1_SCTX *ASN1_SCTX_new(int (*scan_cb) (ASN1_SCTX *ctx));
  706. void ASN1_SCTX_free(ASN1_SCTX *p);
  707. const ASN1_ITEM *ASN1_SCTX_get_item(ASN1_SCTX *p);
  708. const ASN1_TEMPLATE *ASN1_SCTX_get_template(ASN1_SCTX *p);
  709. unsigned long ASN1_SCTX_get_flags(ASN1_SCTX *p);
  710. void ASN1_SCTX_set_app_data(ASN1_SCTX *p, void *data);
  711. void *ASN1_SCTX_get_app_data(ASN1_SCTX *p);
  712. const BIO_METHOD *BIO_f_asn1(void);
  713. BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it);
  714. int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
  715. const ASN1_ITEM *it);
  716. int PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
  717. const char *hdr, const ASN1_ITEM *it);
  718. int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
  719. int ctype_nid, int econt_nid,
  720. STACK_OF(X509_ALGOR) *mdalgs, const ASN1_ITEM *it);
  721. ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it);
  722. int SMIME_crlf_copy(BIO *in, BIO *out, int flags);
  723. int SMIME_text(BIO *in, BIO *out);
  724. const ASN1_ITEM *ASN1_ITEM_lookup(const char *name);
  725. const ASN1_ITEM *ASN1_ITEM_get(size_t i);
  726. # ifdef __cplusplus
  727. }
  728. # endif
  729. #endif