internal.h.grpc_back 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2006.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #ifndef OPENSSL_HEADER_ASN1_ASN1_LOCL_H
  59. #define OPENSSL_HEADER_ASN1_ASN1_LOCL_H
  60. #include <time.h>
  61. #include <openssl_grpc/asn1.h>
  62. #include <openssl_grpc/asn1t.h>
  63. #if defined(__cplusplus)
  64. extern "C" {
  65. #endif
  66. /* Wrapper functions for time functions. */
  67. /* OPENSSL_gmtime wraps |gmtime_r|. See the manual page for that function. */
  68. struct tm *OPENSSL_gmtime(const time_t *time, struct tm *result);
  69. /* OPENSSL_gmtime_adj updates |tm| by adding |offset_day| days and |offset_sec|
  70. * seconds. */
  71. int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, long offset_sec);
  72. /* OPENSSL_gmtime_diff calculates the difference between |from| and |to| and
  73. * outputs the difference as a number of days and seconds in |*out_days| and
  74. * |*out_secs|. */
  75. int OPENSSL_gmtime_diff(int *out_days, int *out_secs, const struct tm *from,
  76. const struct tm *to);
  77. /* Internal ASN1 structures and functions: not for application use */
  78. /* These are used internally in the ASN1_OBJECT to keep track of
  79. * whether the names and data need to be free()ed */
  80. #define ASN1_OBJECT_FLAG_DYNAMIC 0x01 /* internal use */
  81. #define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04 /* internal use */
  82. #define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08 /* internal use */
  83. /* An asn1_object_st (aka |ASN1_OBJECT|) represents an ASN.1 OBJECT IDENTIFIER.
  84. * Note: Mutating an |ASN1_OBJECT| is only permitted when initializing it. The
  85. * library maintains a table of static |ASN1_OBJECT|s, which may be referenced
  86. * by non-const |ASN1_OBJECT| pointers. Code which receives an |ASN1_OBJECT|
  87. * pointer externally must assume it is immutable, even if the pointer is not
  88. * const. */
  89. struct asn1_object_st {
  90. const char *sn, *ln;
  91. int nid;
  92. int length;
  93. const unsigned char *data; /* data remains const after init */
  94. int flags; /* Should we free this one */
  95. };
  96. ASN1_OBJECT *ASN1_OBJECT_new(void);
  97. // ASN1_ENCODING structure: this is used to save the received
  98. // encoding of an ASN1 type. This is useful to get round
  99. // problems with invalid encodings which can break signatures.
  100. typedef struct ASN1_ENCODING_st {
  101. unsigned char *enc; // DER encoding
  102. long len; // Length of encoding
  103. int modified; // set to 1 if 'enc' is invalid
  104. // alias_only is zero if |enc| owns the buffer that it points to
  105. // (although |enc| may still be NULL). If one, |enc| points into a
  106. // buffer that is owned elsewhere.
  107. unsigned alias_only : 1;
  108. // alias_only_on_next_parse is one iff the next parsing operation
  109. // should avoid taking a copy of the input and rather set
  110. // |alias_only|.
  111. unsigned alias_only_on_next_parse : 1;
  112. } ASN1_ENCODING;
  113. int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d);
  114. int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d);
  115. void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
  116. int combine);
  117. int UTF8_getc(const unsigned char *str, int len, uint32_t *val);
  118. int UTF8_putc(unsigned char *str, int len, uint32_t value);
  119. int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
  120. void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
  121. void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  122. int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
  123. const ASN1_ITEM *it, int tag, int aclass, char opt,
  124. ASN1_TLC *ctx);
  125. /* ASN1_item_ex_i2d encodes |*pval| as a value of type |it| to |out| under the
  126. * i2d output convention. It returns a non-zero length on success and -1 on
  127. * error. If |tag| is -1. the tag and class come from |it|. Otherwise, the tag
  128. * number is |tag| and the class is |aclass|. This is used for implicit tagging.
  129. * This function treats a missing value as an error, not an optional field. */
  130. int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
  131. const ASN1_ITEM *it, int tag, int aclass);
  132. void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
  133. /* asn1_get_choice_selector returns the CHOICE selector value for |*pval|, which
  134. * must of type |it|. */
  135. int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
  136. int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
  137. /* asn1_get_field_ptr returns a pointer to the field in |*pval| corresponding to
  138. * |tt|. */
  139. ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  140. /* asn1_do_adb returns the |ASN1_TEMPLATE| for the ANY DEFINED BY field |tt|,
  141. * based on the selector INTEGER or OID in |*pval|. If |tt| is not an ADB field,
  142. * it returns |tt|. If the selector does not match any value, it returns NULL.
  143. * If |nullerr| is non-zero, it will additionally push an error to the error
  144. * queue when there is no match. */
  145. const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
  146. int nullerr);
  147. void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it);
  148. int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it);
  149. void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
  150. void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
  151. /* asn1_enc_restore, if |*pval| has a saved encoding, writes it to |out| under
  152. * the i2d output convention, sets |*len| to the length, and returns one. If it
  153. * has no saved encoding, it returns zero. */
  154. int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
  155. const ASN1_ITEM *it);
  156. int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
  157. const ASN1_ITEM *it);
  158. /* asn1_type_value_as_pointer returns |a|'s value in pointer form. This is
  159. * usually the value object but, for BOOLEAN values, is 0 or 0xff cast to
  160. * a pointer. */
  161. const void *asn1_type_value_as_pointer(const ASN1_TYPE *a);
  162. /* asn1_is_printable returns one if |value| is a valid Unicode codepoint for an
  163. * ASN.1 PrintableString, and zero otherwise. */
  164. int asn1_is_printable(uint32_t value);
  165. /* asn1_bit_string_length returns the number of bytes in |str| and sets
  166. * |*out_padding_bits| to the number of padding bits.
  167. *
  168. * This function should be used instead of |ASN1_STRING_length| to correctly
  169. * handle the non-|ASN1_STRING_FLAG_BITS_LEFT| case. */
  170. int asn1_bit_string_length(const ASN1_BIT_STRING *str,
  171. uint8_t *out_padding_bits);
  172. typedef struct {
  173. int nid;
  174. long minsize;
  175. long maxsize;
  176. unsigned long mask;
  177. unsigned long flags;
  178. } ASN1_STRING_TABLE;
  179. /* asn1_get_string_table_for_testing sets |*out_ptr| and |*out_len| to the table
  180. * of built-in |ASN1_STRING_TABLE| values. It is exported for testing. */
  181. OPENSSL_EXPORT void asn1_get_string_table_for_testing(
  182. const ASN1_STRING_TABLE **out_ptr, size_t *out_len);
  183. #if defined(__cplusplus)
  184. } /* extern C */
  185. #endif
  186. #endif /* OPENSSL_HEADER_ASN1_ASN1_LOCL_H */