asn1.h 32 KB

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