tls_misc.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2007, 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 <cfg/crypto.h>
  39. #include <cfg/tls.h>
  40. #include <crypto/crypto.h>
  41. #include <crypto/bigint.h>
  42. #include <time.h>
  43. /* enable features based on a 'super-set' capbaility. */
  44. #if defined(TLS_SSL_FULL_MODE)
  45. #define TLS_SSL_ENABLE_CLIENT
  46. #define TLS_SSL_CERT_VERIFICATION
  47. #elif defined(TLS_SSL_ENABLE_CLIENT)
  48. #define TLS_SSL_CERT_VERIFICATION
  49. #endif
  50. /* Mutexing definitions */
  51. #if defined(TLS_SSL_CTX_MUTEXING)
  52. #include <sys/event.h>
  53. #define SSL_CTX_MUTEX_TYPE HANDLE
  54. #define SSL_CTX_MUTEX_INIT(A) NutEventPost(&A)
  55. #define SSL_CTX_MUTEX_DESTROY(A)
  56. #define SSL_CTX_LOCK(A) NutEventWait(&A, NUT_WAIT_INFINITE)
  57. #define SSL_CTX_UNLOCK(A) NutEventPost(&A)
  58. #else /* no mutexing */
  59. #define SSL_CTX_MUTEX_INIT(A)
  60. #define SSL_CTX_MUTEX_DESTROY(A)
  61. #define SSL_CTX_LOCK(A)
  62. #define SSL_CTX_UNLOCK(A)
  63. #endif
  64. /**************************************************************************
  65. * X509 declarations
  66. **************************************************************************/
  67. #define X509_OK 0
  68. #define X509_NOT_OK -1
  69. #define X509_VFY_ERROR_NO_TRUSTED_CERT -2
  70. #define X509_VFY_ERROR_BAD_SIGNATURE -3
  71. #define X509_VFY_ERROR_NOT_YET_VALID -4
  72. #define X509_VFY_ERROR_EXPIRED -5
  73. #define X509_VFY_ERROR_SELF_SIGNED -6
  74. #define X509_VFY_ERROR_INVALID_CHAIN -7
  75. #define X509_VFY_ERROR_UNSUPPORTED_DIGEST -8
  76. #define X509_INVALID_PRIV_KEY -9
  77. /*
  78. * The Distinguished Name
  79. */
  80. #define X509_NUM_DN_TYPES 3
  81. #define X509_COMMON_NAME 0
  82. #define X509_ORGANIZATION 1
  83. #define X509_ORGANIZATIONAL_UNIT 2
  84. struct _x509_ctx
  85. {
  86. char *ca_cert_dn[X509_NUM_DN_TYPES];
  87. char *cert_dn[X509_NUM_DN_TYPES];
  88. char **subject_alt_dnsnames;
  89. time_t not_before;
  90. time_t not_after;
  91. uint8_t *signature;
  92. uint16_t sig_len;
  93. uint8_t sig_type;
  94. RSA_CTX *rsa_ctx;
  95. bigint *digest;
  96. struct _x509_ctx *next;
  97. };
  98. typedef struct _x509_ctx X509_CTX;
  99. #ifdef TLS_SSL_CERT_VERIFICATION
  100. typedef struct
  101. {
  102. X509_CTX *cert[TLS_X509_MAX_CA_CERTS];
  103. } CA_CERT_CTX;
  104. #endif
  105. int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx);
  106. void x509_free(X509_CTX *x509_ctx);
  107. #ifdef TLS_SSL_CERT_VERIFICATION
  108. int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert);
  109. #endif
  110. #ifdef TLS_SSL_FULL_MODE
  111. void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx);
  112. const char * x509_display_error(int error);
  113. #endif
  114. /**************************************************************************
  115. * ASN1 declarations
  116. **************************************************************************/
  117. #define ASN1_INTEGER 0x02
  118. #define ASN1_BIT_STRING 0x03
  119. #define ASN1_OCTET_STRING 0x04
  120. #define ASN1_NULL 0x05
  121. #define ASN1_PRINTABLE_STR2 0x0C
  122. #define ASN1_OID 0x06
  123. #define ASN1_PRINTABLE_STR2 0x0C
  124. #define ASN1_PRINTABLE_STR 0x13
  125. #define ASN1_TELETEX_STR 0x14
  126. #define ASN1_IA5_STR 0x16
  127. #define ASN1_UTC_TIME 0x17
  128. #define ASN1_UNICODE_STR 0x1e
  129. #define ASN1_SEQUENCE 0x30
  130. #define ASN1_CONTEXT_DNSNAME 0x82
  131. #define ASN1_SET 0x31
  132. #define ASN1_V3_DATA 0xa3
  133. #define ASN1_IMPLICIT_TAG 0x80
  134. #define ASN1_CONTEXT_DNSNAME 0x82
  135. #define ASN1_EXPLICIT_TAG 0xa0
  136. #define ASN1_V3_DATA 0xa3
  137. #define SIG_TYPE_MD2 0x02
  138. #define SIG_TYPE_MD5 0x04
  139. #define SIG_TYPE_SHA1 0x05
  140. int get_asn1_length(const uint8_t *buf, int *offset);
  141. int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx);
  142. int asn1_next_obj(const uint8_t *buf, int *offset, int obj_type);
  143. int asn1_skip_obj(const uint8_t *buf, int *offset, int obj_type);
  144. int asn1_get_int(const uint8_t *buf, int *offset, uint8_t **object);
  145. int asn1_version(const uint8_t *cert, int *offset, X509_CTX *x509_ctx);
  146. int asn1_validity(const uint8_t *cert, int *offset, X509_CTX *x509_ctx);
  147. int asn1_name(const uint8_t *cert, int *offset, char *dn[]);
  148. int asn1_public_key(const uint8_t *cert, int *offset, X509_CTX *x509_ctx);
  149. #ifdef TLS_SSL_CERT_VERIFICATION
  150. int asn1_signature(const uint8_t *cert, int *offset, X509_CTX *x509_ctx);
  151. int asn1_find_subjectaltname(const uint8_t* cert, int offset);
  152. int asn1_compare_dn(char * const dn1[], char * const dn2[]);
  153. #endif /* TLS_SSL_CERT_VERIFICATION */
  154. int asn1_signature_type(const uint8_t *cert,
  155. int *offset, X509_CTX *x509_ctx);
  156. /**************************************************************************
  157. * MISC declarations
  158. **************************************************************************/
  159. #define SALT_SIZE 8
  160. extern const char * const unsupported_str;
  161. typedef void (*crypt_func)(void *, const uint8_t *, uint8_t *, int);
  162. typedef void (*hmac_func)(const uint8_t *msg, int length, const uint8_t *key,
  163. int key_len, uint8_t *digest);
  164. int get_file(const char *filename, uint8_t **buf);
  165. #if defined(TLS_SSL_FULL_MODE) || defined(TLS_DEBUG)
  166. void print_blob(const char *format, const uint8_t *data, int size, ...);
  167. #else
  168. #define print_blob(...)
  169. #endif
  170. int base64_decode(const char *in, int len,
  171. uint8_t *out, int *outlen);
  172. #ifdef __cplusplus
  173. }
  174. #endif
  175. #endif