| 1 |
// Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 2006. |
| 2 |
// Copyright (c) 2006 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#ifndef OPENSSL_HEADER_ASN1_INTERNAL_H |
| 6 |
#define OPENSSL_HEADER_ASN1_INTERNAL_H |
| 7 |
|
| 8 |
#include <time.h> |
| 9 |
|
| 10 |
#include <openssl/asn1.h> |
| 11 |
#include <openssl/asn1t.h> |
| 12 |
#include <openssl/posix_time.h> |
| 13 |
|
| 14 |
#include "../internal.h" |
| 15 |
|
| 16 |
#if defined(__cplusplus) |
| 17 |
extern "C" { |
| 18 |
#endif |
| 19 |
|
| 20 |
|
| 21 |
// Wrapper functions for time functions. |
| 22 |
|
| 23 |
// OPENSSL_gmtime converts a time_t value in |time| which must be in the range |
| 24 |
// of year 0000 to 9999 to a broken out time value in |tm|. On success |tm| is |
| 25 |
// returned. On failure NULL is returned. |
| 26 |
OPENSSL_EXPORT struct tm *OPENSSL_gmtime(const time_t *time, struct tm *result); |
| 27 |
|
| 28 |
// OPENSSL_gmtime_adj returns one on success, and updates |tm| by adding |
| 29 |
// |offset_day| days and |offset_sec| seconds. It returns zero on failure. |tm| |
| 30 |
// must be in the range of year 0000 to 9999 both before and after the update or |
| 31 |
// a failure will be returned. |
| 32 |
OPENSSL_EXPORT int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, |
| 33 |
int64_t offset_sec); |
| 34 |
|
| 35 |
// OPENSSL_gmtime_diff calculates the difference between |from| and |to|. It |
| 36 |
// returns one, and outputs the difference as a number of days and seconds in |
| 37 |
// |*out_days| and |*out_secs| on success. It returns zero on failure. Both |
| 38 |
// |from| and |to| must be in the range of year 0000 to 9999 or a failure will |
| 39 |
// be returned. |
| 40 |
OPENSSL_EXPORT int OPENSSL_gmtime_diff(int *out_days, int *out_secs, |
| 41 |
const struct tm *from, |
| 42 |
const struct tm *to); |
| 43 |
|
| 44 |
// Internal ASN1 structures and functions: not for application use |
| 45 |
|
| 46 |
// These are used internally in the ASN1_OBJECT to keep track of |
| 47 |
// whether the names and data need to be free()ed |
| 48 |
#define ASN1_OBJECT_FLAG_DYNAMIC 0x01 // internal use |
| 49 |
#define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04 // internal use |
| 50 |
#define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08 // internal use |
| 51 |
|
| 52 |
// An asn1_object_st (aka |ASN1_OBJECT|) represents an ASN.1 OBJECT IDENTIFIER. |
| 53 |
// Note: Mutating an |ASN1_OBJECT| is only permitted when initializing it. The |
| 54 |
// library maintains a table of static |ASN1_OBJECT|s, which may be referenced |
| 55 |
// by non-const |ASN1_OBJECT| pointers. Code which receives an |ASN1_OBJECT| |
| 56 |
// pointer externally must assume it is immutable, even if the pointer is not |
| 57 |
// const. |
| 58 |
struct asn1_object_st { |
| 59 |
const char *sn, *ln; |
| 60 |
int nid; |
| 61 |
int length; |
| 62 |
const unsigned char *data; // data remains const after init |
| 63 |
int flags; // Should we free this one |
| 64 |
}; |
| 65 |
|
| 66 |
ASN1_OBJECT *ASN1_OBJECT_new(void); |
| 67 |
|
| 68 |
// ASN1_ENCODING structure: this is used to save the received |
| 69 |
// encoding of an ASN1 type. This is useful to get round |
| 70 |
// problems with invalid encodings which can break signatures. |
| 71 |
typedef struct ASN1_ENCODING_st { |
| 72 |
unsigned char *enc; // DER encoding |
| 73 |
long len; // Length of encoding, or zero if not present. |
| 74 |
// alias_only is zero if |enc| owns the buffer that it points to |
| 75 |
// (although |enc| may still be NULL). If one, |enc| points into a |
| 76 |
// buffer that is owned elsewhere. |
| 77 |
unsigned alias_only : 1; |
| 78 |
// alias_only_on_next_parse is one iff the next parsing operation |
| 79 |
// should avoid taking a copy of the input and rather set |
| 80 |
// |alias_only|. |
| 81 |
unsigned alias_only_on_next_parse : 1; |
| 82 |
} ASN1_ENCODING; |
| 83 |
|
| 84 |
OPENSSL_EXPORT int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d, |
| 85 |
int allow_timezone_offset); |
| 86 |
OPENSSL_EXPORT int asn1_generalizedtime_to_tm(struct tm *tm, |
| 87 |
const ASN1_GENERALIZEDTIME *d); |
| 88 |
|
| 89 |
void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 90 |
int combine); |
| 91 |
|
| 92 |
int UTF8_getc(const unsigned char *str, int len, uint32_t *val); |
| 93 |
int UTF8_putc(unsigned char *str, int len, uint32_t value); |
| 94 |
|
| 95 |
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it); |
| 96 |
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it); |
| 97 |
|
| 98 |
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); |
| 99 |
int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
| 100 |
const ASN1_ITEM *it, int tag, int aclass, char opt, |
| 101 |
ASN1_TLC *ctx); |
| 102 |
|
| 103 |
// ASN1_item_ex_i2d encodes |*pval| as a value of type |it| to |out| under the |
| 104 |
// i2d output convention. It returns a non-zero length on success and -1 on |
| 105 |
// error. If |tag| is -1. the tag and class come from |it|. Otherwise, the tag |
| 106 |
// number is |tag| and the class is |aclass|. This is used for implicit tagging. |
| 107 |
// This function treats a missing value as an error, not an optional field. |
| 108 |
int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, |
| 109 |
const ASN1_ITEM *it, int tag, int aclass); |
| 110 |
|
| 111 |
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it); |
| 112 |
|
| 113 |
// asn1_get_choice_selector returns the CHOICE selector value for |*pval|, which |
| 114 |
// must of type |it|. |
| 115 |
int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it); |
| 116 |
|
| 117 |
int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it); |
| 118 |
|
| 119 |
// asn1_get_field_ptr returns a pointer to the field in |*pval| corresponding to |
| 120 |
// |tt|. |
| 121 |
ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); |
| 122 |
|
| 123 |
// asn1_do_adb returns the |ASN1_TEMPLATE| for the ANY DEFINED BY field |tt|, |
| 124 |
// based on the selector INTEGER or OID in |*pval|. If |tt| is not an ADB field, |
| 125 |
// it returns |tt|. If the selector does not match any value, it returns NULL. |
| 126 |
// If |nullerr| is non-zero, it will additionally push an error to the error |
| 127 |
// queue when there is no match. |
| 128 |
const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, |
| 129 |
int nullerr); |
| 130 |
|
| 131 |
void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it); |
| 132 |
int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it); |
| 133 |
|
| 134 |
void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it); |
| 135 |
void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it); |
| 136 |
|
| 137 |
// asn1_enc_restore, if |*pval| has a saved encoding, writes it to |out| under |
| 138 |
// the i2d output convention, sets |*len| to the length, and returns one. If it |
| 139 |
// has no saved encoding, it returns zero. |
| 140 |
int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, |
| 141 |
const ASN1_ITEM *it); |
| 142 |
|
| 143 |
int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, |
| 144 |
const ASN1_ITEM *it); |
| 145 |
|
| 146 |
// asn1_encoding_clear clears the cached encoding in |enc|. |
| 147 |
void asn1_encoding_clear(ASN1_ENCODING *enc); |
| 148 |
|
| 149 |
// asn1_type_value_as_pointer returns |a|'s value in pointer form. This is |
| 150 |
// usually the value object but, for BOOLEAN values, is 0 or 0xff cast to |
| 151 |
// a pointer. |
| 152 |
const void *asn1_type_value_as_pointer(const ASN1_TYPE *a); |
| 153 |
|
| 154 |
// asn1_type_set0_string sets |a|'s value to the object represented by |str| and |
| 155 |
// takes ownership of |str|. |
| 156 |
void asn1_type_set0_string(ASN1_TYPE *a, ASN1_STRING *str); |
| 157 |
|
| 158 |
// asn1_type_cleanup releases memory associated with |a|'s value, without |
| 159 |
// freeing |a| itself. |
| 160 |
void asn1_type_cleanup(ASN1_TYPE *a); |
| 161 |
|
| 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 |
|
| 166 |
// asn1_bit_string_length returns the number of bytes in |str| and sets |
| 167 |
// |*out_padding_bits| to the number of padding bits. |
| 168 |
// |
| 169 |
// This function should be used instead of |ASN1_STRING_length| to correctly |
| 170 |
// handle the non-|ASN1_STRING_FLAG_BITS_LEFT| case. |
| 171 |
int asn1_bit_string_length(const ASN1_BIT_STRING *str, |
| 172 |
uint8_t *out_padding_bits); |
| 173 |
|
| 174 |
typedef struct { |
| 175 |
int nid; |
| 176 |
long minsize; |
| 177 |
long maxsize; |
| 178 |
unsigned long mask; |
| 179 |
unsigned long flags; |
| 180 |
} ASN1_STRING_TABLE; |
| 181 |
|
| 182 |
// asn1_get_string_table_for_testing sets |*out_ptr| and |*out_len| to the table |
| 183 |
// of built-in |ASN1_STRING_TABLE| values. It is exported for testing. |
| 184 |
OPENSSL_EXPORT void asn1_get_string_table_for_testing( |
| 185 |
const ASN1_STRING_TABLE **out_ptr, size_t *out_len); |
| 186 |
|
| 187 |
|
| 188 |
#if defined(__cplusplus) |
| 189 |
} // extern C |
| 190 |
#endif |
| 191 |
|
| 192 |
#endif // OPENSSL_HEADER_ASN1_INTERNAL_H |