You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

srs2.h 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* This file is copied from the libsrs2 sources */
  2. /* Modified by Timo Röhling <timo.roehling@gmx.de> */
  3. /* Copyright (c) 2004 Shevek (srs@anarres.org)
  4. * All rights reserved.
  5. *
  6. * This file is a part of libsrs2 from http://www.libsrs2.org/
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, under the terms of either the GNU General Public
  10. * License version 2 or the BSD license, at the discretion of the
  11. * user. Copies of these licenses have been included in the libsrs2
  12. * distribution. See the the file called LICENSE for more
  13. * information.
  14. */
  15. #ifndef __SRS2_H__
  16. #define __SRS2_H__
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <ctype.h>
  20. #ifdef HAVE_SYS_TYPES_H
  21. #include <sys/types.h>
  22. #endif
  23. #ifdef HAVE_SYS_TIME_H
  24. #include <sys/time.h>
  25. #endif
  26. #ifdef HAVE_TIME_H
  27. #include <time.h>
  28. #endif
  29. #ifndef __BEGIN_DECLS
  30. #define __BEGIN_DECLS
  31. #define __END_DECLS
  32. #endif
  33. __BEGIN_DECLS
  34. #define SRS_VERSION_MAJOR 1
  35. #define SRS_VERSION_MINOR 0
  36. #define SRS_VERSION_PATCHLEVEL 14
  37. #define SRS_VERSION_FROM(m, n, p) (((m) << 16) + ((n) << 8) + (p))
  38. #define SRS_VERSION SRS_VERSION_FROM(SRS_VERSION_MAJOR, \
  39. SRS_VERSION_MINOR, \
  40. SRS_VERSION_PATCHLEVEL)
  41. /* This is ugly, but reasonably safe. */
  42. #undef TRUE
  43. #define TRUE 1
  44. #undef FALSE
  45. #define FALSE 0
  46. #define SRSSEP '='
  47. #define SRS0TAG "SRS0"
  48. #define SRS1TAG "SRS1"
  49. /* Error codes */
  50. #define SRS_ERRTYPE_MASK 0xF000
  51. #define SRS_ERRTYPE_NONE 0x0000
  52. #define SRS_ERRTYPE_CONFIG 0x1000
  53. #define SRS_ERRTYPE_INPUT 0x2000
  54. #define SRS_ERRTYPE_SYNTAX 0x4000
  55. #define SRS_ERRTYPE_SRS 0x8000
  56. #define SRS_SUCCESS (0)
  57. #define SRS_ENOTSRSADDRESS (1)
  58. #define SRS_ENOTREWRITTEN (2)
  59. #define SRS_ENOSECRETS (SRS_ERRTYPE_CONFIG | 1)
  60. #define SRS_ESEPARATORINVALID (SRS_ERRTYPE_CONFIG | 2)
  61. #define SRS_ENOSENDERATSIGN (SRS_ERRTYPE_INPUT | 1)
  62. #define SRS_EBUFTOOSMALL (SRS_ERRTYPE_INPUT | 2)
  63. #define SRS_ENOSRS0HOST (SRS_ERRTYPE_SYNTAX | 1)
  64. #define SRS_ENOSRS0USER (SRS_ERRTYPE_SYNTAX | 2)
  65. #define SRS_ENOSRS0HASH (SRS_ERRTYPE_SYNTAX | 3)
  66. #define SRS_ENOSRS0STAMP (SRS_ERRTYPE_SYNTAX | 4)
  67. #define SRS_ENOSRS1HOST (SRS_ERRTYPE_SYNTAX | 5)
  68. #define SRS_ENOSRS1USER (SRS_ERRTYPE_SYNTAX | 6)
  69. #define SRS_ENOSRS1HASH (SRS_ERRTYPE_SYNTAX | 7)
  70. #define SRS_EBADTIMESTAMPCHAR (SRS_ERRTYPE_SYNTAX | 8)
  71. #define SRS_EHASHTOOSHORT (SRS_ERRTYPE_SYNTAX | 9)
  72. #define SRS_ETIMESTAMPOUTOFDATE (SRS_ERRTYPE_SRS | 1)
  73. #define SRS_EHASHINVALID (SRS_ERRTYPE_SRS | 2)
  74. #define SRS_ERROR_TYPE(x) ((x) & SRS_ERRTYPE_MASK)
  75. /* SRS implementation */
  76. #define SRS_IS_SRS_ADDRESS(x) ( \
  77. (strncasecmp((x), "SRS", 3) == 0) && \
  78. (strchr("01", (x)[3]) != NULL) && \
  79. (strchr("-+=", (x)[4]) != NULL) \
  80. )
  81. typedef void *(*srs_malloc_t)(size_t);
  82. typedef void *(*srs_realloc_t)(void *, size_t);
  83. typedef void (*srs_free_t)(void *);
  84. typedef int srs_bool;
  85. typedef
  86. struct _srs_t {
  87. /* Rewriting parameters */
  88. char **secrets;
  89. int numsecrets;
  90. char separator;
  91. /* Security parameters */
  92. int maxage; /* Maximum allowed age in seconds */
  93. int hashlength;
  94. int hashmin;
  95. /* Behaviour parameters */
  96. srs_bool alwaysrewrite; /* Rewrite even into same domain? */
  97. srs_bool noforward; /* Never perform forwards rewriting */
  98. srs_bool noreverse; /* Never perform reverse rewriting */
  99. char **neverrewrite; /* A list of non-rewritten domains */
  100. } srs_t;
  101. /* Interface */
  102. int srs_set_malloc(srs_malloc_t m, srs_realloc_t r, srs_free_t f);
  103. srs_t *srs_new();
  104. void srs_init(srs_t *srs);
  105. void srs_free(srs_t *srs);
  106. int srs_forward(srs_t *srs, char *buf, unsigned buflen,
  107. const char *sender, const char *alias);
  108. int srs_forward_alloc(srs_t *srs, char **sptr,
  109. const char *sender, const char *alias);
  110. int srs_reverse(srs_t *srs, char *buf, unsigned buflen,
  111. const char *sender);
  112. int srs_reverse_alloc(srs_t *srs, char **sptr, const char *sender);
  113. const char *
  114. srs_strerror(int code);
  115. int srs_add_secret(srs_t *srs, const char *secret);
  116. const char *
  117. srs_get_secret(srs_t *srs, int idx);
  118. /* You probably shouldn't call these. */
  119. int srs_timestamp_create(srs_t *srs, char *buf, time_t now);
  120. int srs_timestamp_check(srs_t *srs, const char *stamp);
  121. #define SRS_PARAM_DECLARE(n, t) \
  122. int srs_set_ ## n (srs_t *srs, t value); \
  123. t srs_get_ ## n (srs_t *srs);
  124. SRS_PARAM_DECLARE(alwaysrewrite, srs_bool)
  125. SRS_PARAM_DECLARE(separator, char)
  126. SRS_PARAM_DECLARE(maxage, int)
  127. SRS_PARAM_DECLARE(hashlength, int)
  128. SRS_PARAM_DECLARE(hashmin, int)
  129. SRS_PARAM_DECLARE(noforward, srs_bool)
  130. SRS_PARAM_DECLARE(noreverse, srs_bool)
  131. /* SHA1 implementation */
  132. typedef unsigned long ULONG; /* 32-or-more-bit quantity */
  133. typedef unsigned char sha_byte;
  134. #define SHA_BLOCKSIZE 64
  135. #define SHA_DIGESTSIZE 20
  136. typedef struct {
  137. ULONG digest[5]; /* message digest */
  138. ULONG count_lo, count_hi; /* 64-bit bit count */
  139. sha_byte data[SHA_BLOCKSIZE]; /* SHA data buffer */
  140. int local; /* unprocessed amount in data */
  141. } SHA_INFO;
  142. typedef
  143. struct _srs_hmac_ctx_t {
  144. SHA_INFO sctx;
  145. char ipad[SHA_BLOCKSIZE + 1];
  146. char opad[SHA_BLOCKSIZE + 1];
  147. } srs_hmac_ctx_t;
  148. void srs_hmac_init(srs_hmac_ctx_t *ctx, char *secret, unsigned len);
  149. void srs_hmac_update(srs_hmac_ctx_t *ctx, char *data, unsigned len);
  150. void srs_hmac_fini(srs_hmac_ctx_t *ctx, char *out);
  151. __END_DECLS
  152. #endif