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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #ifndef __BEGIN_DECLS
  21. #define __BEGIN_DECLS
  22. #define __END_DECLS
  23. #endif
  24. __BEGIN_DECLS
  25. #define SRS_VERSION_MAJOR 1
  26. #define SRS_VERSION_MINOR 0
  27. #define SRS_VERSION_PATCHLEVEL 14
  28. #define SRS_VERSION_FROM(m, n, p) (((m) << 16) + ((n) << 8) + (p))
  29. #define SRS_VERSION SRS_VERSION_FROM(SRS_VERSION_MAJOR, \
  30. SRS_VERSION_MINOR, \
  31. SRS_VERSION_PATCHLEVEL)
  32. /* This is ugly, but reasonably safe. */
  33. #undef TRUE
  34. #define TRUE 1
  35. #undef FALSE
  36. #define FALSE 0
  37. #define SRSSEP '='
  38. #define SRS0TAG "SRS0"
  39. #define SRS1TAG "SRS1"
  40. /* Error codes */
  41. #define SRS_ERRTYPE_MASK 0xF000
  42. #define SRS_ERRTYPE_NONE 0x0000
  43. #define SRS_ERRTYPE_CONFIG 0x1000
  44. #define SRS_ERRTYPE_INPUT 0x2000
  45. #define SRS_ERRTYPE_SYNTAX 0x4000
  46. #define SRS_ERRTYPE_SRS 0x8000
  47. #define SRS_SUCCESS (0)
  48. #define SRS_ENOTSRSADDRESS (1)
  49. #define SRS_ENOTREWRITTEN (2)
  50. #define SRS_ENOSECRETS (SRS_ERRTYPE_CONFIG | 1)
  51. #define SRS_ESEPARATORINVALID (SRS_ERRTYPE_CONFIG | 2)
  52. #define SRS_ENOSENDERATSIGN (SRS_ERRTYPE_INPUT | 1)
  53. #define SRS_EBUFTOOSMALL (SRS_ERRTYPE_INPUT | 2)
  54. #define SRS_ENOSRS0HOST (SRS_ERRTYPE_SYNTAX | 1)
  55. #define SRS_ENOSRS0USER (SRS_ERRTYPE_SYNTAX | 2)
  56. #define SRS_ENOSRS0HASH (SRS_ERRTYPE_SYNTAX | 3)
  57. #define SRS_ENOSRS0STAMP (SRS_ERRTYPE_SYNTAX | 4)
  58. #define SRS_ENOSRS1HOST (SRS_ERRTYPE_SYNTAX | 5)
  59. #define SRS_ENOSRS1USER (SRS_ERRTYPE_SYNTAX | 6)
  60. #define SRS_ENOSRS1HASH (SRS_ERRTYPE_SYNTAX | 7)
  61. #define SRS_EBADTIMESTAMPCHAR (SRS_ERRTYPE_SYNTAX | 8)
  62. #define SRS_EHASHTOOSHORT (SRS_ERRTYPE_SYNTAX | 9)
  63. #define SRS_ETIMESTAMPOUTOFDATE (SRS_ERRTYPE_SRS | 1)
  64. #define SRS_EHASHINVALID (SRS_ERRTYPE_SRS | 2)
  65. #define SRS_ERROR_TYPE(x) ((x) & SRS_ERRTYPE_MASK)
  66. /* SRS implementation */
  67. #define SRS_IS_SRS_ADDRESS(x) ( \
  68. (strncasecmp((x), "SRS", 3) == 0) && \
  69. (strchr("01", (x)[3]) != NULL) && \
  70. (strchr("-+=", (x)[4]) != NULL) \
  71. )
  72. typedef void *(*srs_malloc_t)(size_t);
  73. typedef void *(*srs_realloc_t)(void *, size_t);
  74. typedef void (*srs_free_t)(void *);
  75. typedef int srs_bool;
  76. typedef
  77. struct _srs_t {
  78. /* Rewriting parameters */
  79. char **secrets;
  80. int numsecrets;
  81. char separator;
  82. /* Security parameters */
  83. int maxage; /* Maximum allowed age in seconds */
  84. int hashlength;
  85. int hashmin;
  86. /* Behaviour parameters */
  87. srs_bool alwaysrewrite; /* Rewrite even into same domain? */
  88. srs_bool noforward; /* Never perform forwards rewriting */
  89. srs_bool noreverse; /* Never perform reverse rewriting */
  90. char **neverrewrite; /* A list of non-rewritten domains */
  91. } srs_t;
  92. /* Interface */
  93. int srs_set_malloc(srs_malloc_t m, srs_realloc_t r, srs_free_t f);
  94. srs_t *srs_new();
  95. void srs_init(srs_t *srs);
  96. void srs_free(srs_t *srs);
  97. int srs_forward(srs_t *srs, char *buf, int buflen,
  98. const char *sender, const char *alias);
  99. int srs_forward_alloc(srs_t *srs, char **sptr,
  100. const char *sender, const char *alias);
  101. int srs_reverse(srs_t *srs, char *buf, int buflen,
  102. const char *sender);
  103. int srs_reverse_alloc(srs_t *srs, char **sptr, const char *sender);
  104. const char *
  105. srs_strerror(int code);
  106. int srs_add_secret(srs_t *srs, const char *secret);
  107. const char *
  108. srs_get_secret(srs_t *srs, int idx);
  109. /* You probably shouldn't call these. */
  110. int srs_timestamp_create(srs_t *srs, char *buf, time_t now);
  111. int srs_timestamp_check(srs_t *srs, const char *stamp);
  112. #define SRS_PARAM_DECLARE(n, t) \
  113. int srs_set_ ## n (srs_t *srs, t value); \
  114. t srs_get_ ## n (srs_t *srs);
  115. SRS_PARAM_DECLARE(alwaysrewrite, srs_bool)
  116. SRS_PARAM_DECLARE(separator, char)
  117. SRS_PARAM_DECLARE(maxage, int)
  118. SRS_PARAM_DECLARE(hashlength, int)
  119. SRS_PARAM_DECLARE(hashmin, int)
  120. SRS_PARAM_DECLARE(noforward, srs_bool)
  121. SRS_PARAM_DECLARE(noreverse, srs_bool)
  122. /* SHA1 implementation */
  123. typedef unsigned long ULONG; /* 32-or-more-bit quantity */
  124. typedef unsigned char sha_byte;
  125. #define SHA_BLOCKSIZE 64
  126. #define SHA_DIGESTSIZE 20
  127. typedef struct {
  128. ULONG digest[5]; /* message digest */
  129. ULONG count_lo, count_hi; /* 64-bit bit count */
  130. sha_byte data[SHA_BLOCKSIZE]; /* SHA data buffer */
  131. int local; /* unprocessed amount in data */
  132. } SHA_INFO;
  133. typedef
  134. struct _srs_hmac_ctx_t {
  135. SHA_INFO sctx;
  136. char ipad[SHA_BLOCKSIZE + 1];
  137. char opad[SHA_BLOCKSIZE + 1];
  138. } srs_hmac_ctx_t;
  139. void srs_hmac_init(srs_hmac_ctx_t *ctx, char *secret, int len);
  140. void srs_hmac_update(srs_hmac_ctx_t *ctx, char *data, int len);
  141. void srs_hmac_fini(srs_hmac_ctx_t *ctx, char *out);
  142. __END_DECLS
  143. #endif