crypto_misc.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2007-2017, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @file crypto_misc.h
  32. */
  33. #ifndef HEADER_CRYPTO_MISC_H
  34. #define HEADER_CRYPTO_MISC_H
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. #include <stdbool.h>
  39. #include "crypto.h"
  40. #include "bigint.h"
  41. /**************************************************************************
  42. * X509 declarations
  43. **************************************************************************/
  44. #define X509_OK 0
  45. #define X509_NOT_OK -1
  46. #define X509_VFY_ERROR_NO_TRUSTED_CERT -2
  47. #define X509_VFY_ERROR_BAD_SIGNATURE -3
  48. #define X509_VFY_ERROR_NOT_YET_VALID -4
  49. #define X509_VFY_ERROR_EXPIRED -5
  50. #define X509_VFY_ERROR_SELF_SIGNED -6
  51. #define X509_VFY_ERROR_INVALID_CHAIN -7
  52. #define X509_VFY_ERROR_UNSUPPORTED_DIGEST -8
  53. #define X509_INVALID_PRIV_KEY -9
  54. #define X509_MAX_CERTS -10
  55. #define X509_VFY_ERROR_BASIC_CONSTRAINT -11
  56. /*
  57. * The Distinguished Name
  58. */
  59. #define X509_NUM_DN_TYPES 6
  60. #define X509_COMMON_NAME 0
  61. #define X509_ORGANIZATION 1
  62. #define X509_ORGANIZATIONAL_UNIT 2
  63. #define X509_LOCATION 3
  64. #define X509_COUNTRY 4
  65. #define X509_STATE 5
  66. /*
  67. * Key Usage bits
  68. */
  69. #define IS_SET_KEY_USAGE_FLAG(A, B) (A->key_usage & B)
  70. #define KEY_USAGE_DIGITAL_SIGNATURE 0x0080
  71. #define KEY_USAGE_NON_REPUDIATION 0x0040
  72. #define KEY_USAGE_KEY_ENCIPHERMENT 0x0020
  73. #define KEY_USAGE_DATA_ENCIPHERMENT 0x0010
  74. #define KEY_USAGE_KEY_AGREEMENT 0x0008
  75. #define KEY_USAGE_KEY_CERT_SIGN 0x0004
  76. #define KEY_USAGE_CRL_SIGN 0x0002
  77. #define KEY_USAGE_ENCIPHER_ONLY 0x0001
  78. #define KEY_USAGE_DECIPHER_ONLY 0x8000
  79. struct _x509_ctx
  80. {
  81. char *ca_cert_dn[X509_NUM_DN_TYPES];
  82. char *cert_dn[X509_NUM_DN_TYPES];
  83. char **subject_alt_dnsnames;
  84. time_t not_before;
  85. time_t not_after;
  86. uint8_t *signature;
  87. RSA_CTX *rsa_ctx;
  88. bigint *digest;
  89. uint16_t sig_len;
  90. uint8_t sig_type;
  91. bool basic_constraint_present;
  92. bool basic_constraint_is_critical;
  93. bool key_usage_present;
  94. bool key_usage_is_critical;
  95. bool subject_alt_name_present;
  96. bool subject_alt_name_is_critical;
  97. bool basic_constraint_cA;
  98. int basic_constraint_pathLenConstraint;
  99. uint32_t key_usage;
  100. struct _x509_ctx *next;
  101. };
  102. typedef struct _x509_ctx X509_CTX;
  103. #ifdef CONFIG_SSL_CERT_VERIFICATION
  104. typedef struct
  105. {
  106. //X509_CTX *cert[CONFIG_X509_MAX_CA_CERTS];
  107. X509_CTX **cert;
  108. } CA_CERT_CTX;
  109. #endif
  110. int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx);
  111. void x509_free(X509_CTX *x509_ctx);
  112. #ifdef CONFIG_SSL_CERT_VERIFICATION
  113. int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert,
  114. int *pathLenConstraint);
  115. #endif
  116. #ifdef CONFIG_SSL_FULL_MODE
  117. void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx);
  118. const char * x509_display_error(int error);
  119. #endif
  120. /**************************************************************************
  121. * ASN1 declarations
  122. **************************************************************************/
  123. #define ASN1_BOOLEAN 0x01
  124. #define ASN1_INTEGER 0x02
  125. #define ASN1_BIT_STRING 0x03
  126. #define ASN1_OCTET_STRING 0x04
  127. #define ASN1_NULL 0x05
  128. #define ASN1_PRINTABLE_STR2 0x0C
  129. #define ASN1_OID 0x06
  130. #define ASN1_PRINTABLE_STR2 0x0C
  131. #define ASN1_PRINTABLE_STR 0x13
  132. #define ASN1_TELETEX_STR 0x14
  133. #define ASN1_IA5_STR 0x16
  134. #define ASN1_UTC_TIME 0x17
  135. #define ASN1_GENERALIZED_TIME 0x18
  136. #define ASN1_UNICODE_STR 0x1e
  137. #define ASN1_SEQUENCE 0x30
  138. #define ASN1_CONTEXT_DNSNAME 0x82
  139. #define ASN1_SET 0x31
  140. #define ASN1_V3_DATA 0xa3
  141. #define ASN1_IMPLICIT_TAG 0x80
  142. #define ASN1_CONTEXT_DNSNAME 0x82
  143. #define ASN1_EXPLICIT_TAG 0xa0
  144. #define ASN1_V3_DATA 0xa3
  145. #define SIG_TYPE_MD5 0x04
  146. #define SIG_TYPE_SHA1 0x05
  147. #define SIG_TYPE_SHA256 0x0b
  148. #define SIG_TYPE_SHA384 0x0c
  149. #define SIG_TYPE_SHA512 0x0d
  150. uint32_t get_asn1_length(const uint8_t *buf, int *offset);
  151. int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx);
  152. int asn1_next_obj(const uint8_t *buf, int *offset, int obj_type);
  153. int asn1_skip_obj(const uint8_t *buf, int *offset, int obj_type);
  154. int asn1_get_big_int(const uint8_t *buf, int *offset, uint8_t **object);
  155. int asn1_get_int(const uint8_t *buf, int *offset, int32_t *val);
  156. int asn1_get_bool(const uint8_t *buf, int *offset, bool *val);
  157. int asn1_get_bit_string_as_int(const uint8_t *buf, int *offset, uint32_t *val);
  158. int asn1_version(const uint8_t *cert, int *offset, int *val);
  159. int asn1_validity(const uint8_t *cert, int *offset, X509_CTX *x509_ctx);
  160. int asn1_name(const uint8_t *cert, int *offset, char *dn[]);
  161. int asn1_public_key(const uint8_t *cert, int *offset, X509_CTX *x509_ctx);
  162. #ifdef CONFIG_SSL_CERT_VERIFICATION
  163. int asn1_signature(const uint8_t *cert, int *offset, X509_CTX *x509_ctx);
  164. int asn1_compare_dn(char * const dn1[], char * const dn2[]);
  165. int asn1_is_subject_alt_name(const uint8_t *cert, int offset);
  166. int asn1_is_basic_constraints(const uint8_t *cert, int offset);
  167. int asn1_is_key_usage(const uint8_t *cert, int offset);
  168. bool asn1_is_critical_ext(const uint8_t *buf, int *offset);
  169. #endif /* CONFIG_SSL_CERT_VERIFICATION */
  170. int asn1_signature_type(const uint8_t *cert,
  171. int *offset, X509_CTX *x509_ctx);
  172. /**************************************************************************
  173. * MISC declarations
  174. **************************************************************************/
  175. #define SALT_SIZE 8
  176. extern const char * const unsupported_str;
  177. typedef void (*crypt_func)(void *, const uint8_t *, uint8_t *, int);
  178. typedef void (*hmac_func)(const uint8_t *msg, int length, const uint8_t *key,
  179. int key_len, uint8_t *digest);
  180. int get_file(const char *filename, uint8_t **buf);
  181. #if defined(CONFIG_SSL_FULL_MODE) || defined(WIN32) || defined(CONFIG_DEBUG)
  182. EXP_FUNC void STDCALL print_blob(const char *format, const uint8_t *data, int size, ...);
  183. #else
  184. #define print_blob(...)
  185. #endif
  186. EXP_FUNC int STDCALL base64_decode(const char *in, int len,
  187. uint8_t *out, int *outlen);
  188. #ifdef __cplusplus
  189. }
  190. #endif
  191. #endif