Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

srs2.h 5.1KB

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