| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#ifndef HEADER_CRYPTO_MISC_H |
| 36 |
#define HEADER_CRYPTO_MISC_H |
| 37 |
|
| 38 |
#ifdef __cplusplus |
| 39 |
extern "C" { |
| 40 |
#endif |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#define bool unsigned int |
| 45 |
#define true 1 |
| 46 |
#define false 0 |
| 47 |
|
| 48 |
#include "crypto.h" |
| 49 |
#include "bigint.h" |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#define X509_OK 0 |
| 55 |
#define X509_NOT_OK -1 |
| 56 |
#define X509_VFY_ERROR_NO_TRUSTED_CERT -2 |
| 57 |
#define X509_VFY_ERROR_BAD_SIGNATURE -3 |
| 58 |
#define X509_VFY_ERROR_NOT_YET_VALID -4 |
| 59 |
#define X509_VFY_ERROR_EXPIRED -5 |
| 60 |
#define X509_VFY_ERROR_SELF_SIGNED -6 |
| 61 |
#define X509_VFY_ERROR_INVALID_CHAIN -7 |
| 62 |
#define X509_VFY_ERROR_UNSUPPORTED_DIGEST -8 |
| 63 |
#define X509_INVALID_PRIV_KEY -9 |
| 64 |
#define X509_INVALID_PUB_KEY -10 |
| 65 |
#define X509_MAX_CERTS -11 |
| 66 |
#define X509_VFY_ERROR_BASIC_CONSTRAINT -12 |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
#define X509_NUM_DN_TYPES 6 |
| 72 |
#define X509_COMMON_NAME 0 |
| 73 |
#define X509_ORGANIZATION 1 |
| 74 |
#define X509_ORGANIZATIONAL_UNIT 2 |
| 75 |
#define X509_LOCATION 3 |
| 76 |
#define X509_COUNTRY 4 |
| 77 |
#define X509_STATE 5 |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
#define IS_SET_KEY_USAGE_FLAG(A, B) (A->key_usage & B) |
| 83 |
|
| 84 |
#define KEY_USAGE_DIGITAL_SIGNATURE 0x0080 |
| 85 |
#define KEY_USAGE_NON_REPUDIATION 0x0040 |
| 86 |
#define KEY_USAGE_KEY_ENCIPHERMENT 0x0020 |
| 87 |
#define KEY_USAGE_DATA_ENCIPHERMENT 0x0010 |
| 88 |
#define KEY_USAGE_KEY_AGREEMENT 0x0008 |
| 89 |
#define KEY_USAGE_KEY_CERT_SIGN 0x0004 |
| 90 |
#define KEY_USAGE_CRL_SIGN 0x0002 |
| 91 |
#define KEY_USAGE_ENCIPHER_ONLY 0x0001 |
| 92 |
#define KEY_USAGE_DECIPHER_ONLY 0x8000 |
| 93 |
|
| 94 |
struct _x509_ctx |
| 95 |
{ |
| 96 |
char *ca_cert_dn[X509_NUM_DN_TYPES]; |
| 97 |
char *cert_dn[X509_NUM_DN_TYPES]; |
| 98 |
char **subject_alt_dnsnames; |
| 99 |
time_t not_before; |
| 100 |
time_t not_after; |
| 101 |
uint8_t *signature; |
| 102 |
RSA_CTX *rsa_ctx; |
| 103 |
bigint *digest; |
| 104 |
uint16_t sig_len; |
| 105 |
uint8_t sig_type; |
| 106 |
bool basic_constraint_present; |
| 107 |
bool basic_constraint_is_critical; |
| 108 |
bool key_usage_present; |
| 109 |
bool key_usage_is_critical; |
| 110 |
bool subject_alt_name_present; |
| 111 |
bool subject_alt_name_is_critical; |
| 112 |
bool basic_constraint_cA; |
| 113 |
int basic_constraint_pathLenConstraint; |
| 114 |
uint32_t key_usage; |
| 115 |
struct _x509_ctx *next; |
| 116 |
}; |
| 117 |
|
| 118 |
typedef struct _x509_ctx X509_CTX; |
| 119 |
|
| 120 |
#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_DEBUG) |
| 121 |
typedef struct |
| 122 |
{ |
| 123 |
X509_CTX *cert[CONFIG_X509_MAX_CA_CERTS]; |
| 124 |
} CA_CERT_CTX; |
| 125 |
#endif |
| 126 |
|
| 127 |
int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx); |
| 128 |
void x509_free(X509_CTX *x509_ctx); |
| 129 |
#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_DEBUG) |
| 130 |
int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert, |
| 131 |
int *pathLenConstraint); |
| 132 |
void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx); |
| 133 |
#endif |
| 134 |
const char * x509_display_error(int error); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
#define ASN1_BOOLEAN 0x01 |
| 140 |
#define ASN1_INTEGER 0x02 |
| 141 |
#define ASN1_BIT_STRING 0x03 |
| 142 |
#define ASN1_OCTET_STRING 0x04 |
| 143 |
#define ASN1_NULL 0x05 |
| 144 |
#define ASN1_PRINTABLE_STR2 0x0C |
| 145 |
#define ASN1_OID 0x06 |
| 146 |
#define ASN1_PRINTABLE_STR2 0x0C |
| 147 |
#define ASN1_PRINTABLE_STR 0x13 |
| 148 |
#define ASN1_TELETEX_STR 0x14 |
| 149 |
#define ASN1_IA5_STR 0x16 |
| 150 |
#define ASN1_UTC_TIME 0x17 |
| 151 |
#define ASN1_GENERALIZED_TIME 0x18 |
| 152 |
#define ASN1_UNICODE_STR 0x1e |
| 153 |
#define ASN1_SEQUENCE 0x30 |
| 154 |
#define ASN1_CONTEXT_DNSNAME 0x82 |
| 155 |
#define ASN1_SET 0x31 |
| 156 |
#define ASN1_V3_DATA 0xa3 |
| 157 |
#define ASN1_IMPLICIT_TAG 0x80 |
| 158 |
#define ASN1_CONTEXT_DNSNAME 0x82 |
| 159 |
#define ASN1_EXPLICIT_TAG 0xa0 |
| 160 |
#define ASN1_V3_DATA 0xa3 |
| 161 |
|
| 162 |
#define SIG_TYPE_MD5 0x04 |
| 163 |
#define SIG_TYPE_SHA1 0x05 |
| 164 |
#define SIG_TYPE_SHA256 0x0b |
| 165 |
#define SIG_TYPE_SHA384 0x0c |
| 166 |
#define SIG_TYPE_SHA512 0x0d |
| 167 |
|
| 168 |
uint32_t get_asn1_length(const uint8_t *buf, int *offset); |
| 169 |
int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx); |
| 170 |
int asn1_get_public_key(const uint8_t *cert, int *offset, RSA_CTX **rsa_ctx); |
| 171 |
int asn1_next_obj(const uint8_t *buf, int *offset, int obj_type); |
| 172 |
int asn1_skip_obj(const uint8_t *buf, int *offset, int obj_type); |
| 173 |
int asn1_get_big_int(const uint8_t *buf, int *offset, uint8_t **object); |
| 174 |
int asn1_get_int(const uint8_t *buf, int *offset, int32_t *val); |
| 175 |
int asn1_get_bool(const uint8_t *buf, int *offset, bool *val); |
| 176 |
int asn1_get_bit_string_as_int(const uint8_t *buf, int *offset, uint32_t *val); |
| 177 |
int asn1_version(const uint8_t *cert, int *offset, int *val); |
| 178 |
int asn1_validity(const uint8_t *cert, int *offset, X509_CTX *x509_ctx); |
| 179 |
int asn1_name(const uint8_t *cert, int *offset, char *dn[]); |
| 180 |
int asn1_public_key(const uint8_t *cert, int *offset, X509_CTX *x509_ctx); |
| 181 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 182 |
int asn1_signature(const uint8_t *cert, int *offset, X509_CTX *x509_ctx); |
| 183 |
int asn1_is_subject_alt_name(const uint8_t *cert, int offset); |
| 184 |
int asn1_is_basic_constraints(const uint8_t *cert, int offset); |
| 185 |
int asn1_is_key_usage(const uint8_t *cert, int offset); |
| 186 |
bool asn1_is_critical_ext(const uint8_t *buf, int *offset); |
| 187 |
#endif |
| 188 |
int asn1_compare_dn(char * const dn1[], char * const dn2[]); |
| 189 |
int asn1_signature_type(const uint8_t *cert, |
| 190 |
int *offset, X509_CTX *x509_ctx); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
#define SALT_SIZE 8 |
| 196 |
|
| 197 |
extern const char * const unsupported_str; |
| 198 |
|
| 199 |
typedef void (*crypt_func)(void *, const uint8_t *, uint8_t *, int); |
| 200 |
typedef void (*hmac_func)(const uint8_t *msg, int length, const uint8_t *key, |
| 201 |
int key_len, uint8_t *digest); |
| 202 |
|
| 203 |
int get_file(const char *filename, uint8_t **buf); |
| 204 |
|
| 205 |
#if defined(CONFIG_SSL_FULL_MODE) || defined(WIN32) || defined(CONFIG_DEBUG) |
| 206 |
EXP_FUNC void STDCALL print_blob(const char *format, const uint8_t *data, int size, ...); |
| 207 |
#else |
| 208 |
#define print_blob(...) |
| 209 |
#endif |
| 210 |
|
| 211 |
EXP_FUNC int STDCALL base64_decode(const char *in, int len, |
| 212 |
uint8_t *out, int *outlen); |
| 213 |
|
| 214 |
#ifdef __cplusplus |
| 215 |
} |
| 216 |
#endif |
| 217 |
|
| 218 |
#endif |