1
0

archive_read_add_passphrase.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*-
  2. * Copyright (c) 2014 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. #ifdef HAVE_ERRNO_H
  27. #include <errno.h>
  28. #endif
  29. #include "archive_read_private.h"
  30. static void
  31. add_passphrase_to_tail(struct archive_read *a,
  32. struct archive_read_passphrase *p)
  33. {
  34. *a->passphrases.last = p;
  35. a->passphrases.last = &p->next;
  36. p->next = NULL;
  37. }
  38. static struct archive_read_passphrase *
  39. remove_passphrases_from_head(struct archive_read *a)
  40. {
  41. struct archive_read_passphrase *p;
  42. p = a->passphrases.first;
  43. if (p != NULL)
  44. a->passphrases.first = p->next;
  45. return (p);
  46. }
  47. static void
  48. insert_passphrase_to_head(struct archive_read *a,
  49. struct archive_read_passphrase *p)
  50. {
  51. p->next = a->passphrases.first;
  52. a->passphrases.first = p;
  53. if (&a->passphrases.first == a->passphrases.last) {
  54. a->passphrases.last = &p->next;
  55. p->next = NULL;
  56. }
  57. }
  58. static struct archive_read_passphrase *
  59. new_read_passphrase(struct archive_read *a, const char *passphrase)
  60. {
  61. struct archive_read_passphrase *p;
  62. p = malloc(sizeof(*p));
  63. if (p == NULL) {
  64. archive_set_error(&a->archive, ENOMEM,
  65. "Can't allocate memory");
  66. return (NULL);
  67. }
  68. p->passphrase = strdup(passphrase);
  69. if (p->passphrase == NULL) {
  70. free(p);
  71. archive_set_error(&a->archive, ENOMEM,
  72. "Can't allocate memory");
  73. return (NULL);
  74. }
  75. return (p);
  76. }
  77. int
  78. archive_read_add_passphrase(struct archive *_a, const char *passphrase)
  79. {
  80. struct archive_read *a = (struct archive_read *)_a;
  81. struct archive_read_passphrase *p;
  82. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  83. "archive_read_add_passphrase");
  84. if (passphrase == NULL || passphrase[0] == '\0') {
  85. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  86. "Empty passphrase is unacceptable");
  87. return (ARCHIVE_FAILED);
  88. }
  89. p = new_read_passphrase(a, passphrase);
  90. if (p == NULL)
  91. return (ARCHIVE_FATAL);
  92. add_passphrase_to_tail(a, p);
  93. return (ARCHIVE_OK);
  94. }
  95. int
  96. archive_read_set_passphrase_callback(struct archive *_a, void *client_data,
  97. archive_passphrase_callback *cb)
  98. {
  99. struct archive_read *a = (struct archive_read *)_a;
  100. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  101. "archive_read_set_passphrase_callback");
  102. a->passphrases.callback = cb;
  103. a->passphrases.client_data = client_data;
  104. return (ARCHIVE_OK);
  105. }
  106. /*
  107. * Call this in advance when you start to get a passphrase for decryption
  108. * for a entry.
  109. */
  110. void
  111. __archive_read_reset_passphrase(struct archive_read *a)
  112. {
  113. a->passphrases.candidate = -1;
  114. }
  115. /*
  116. * Get a passphrase for decryption.
  117. */
  118. const char *
  119. __archive_read_next_passphrase(struct archive_read *a)
  120. {
  121. struct archive_read_passphrase *p;
  122. const char *passphrase;
  123. if (a->passphrases.candidate < 0) {
  124. /* Count out how many passphrases we have. */
  125. int cnt = 0;
  126. for (p = a->passphrases.first; p != NULL; p = p->next)
  127. cnt++;
  128. a->passphrases.candidate = cnt;
  129. p = a->passphrases.first;
  130. } else if (a->passphrases.candidate > 1) {
  131. /* Rotate a passphrase list. */
  132. a->passphrases.candidate--;
  133. p = remove_passphrases_from_head(a);
  134. add_passphrase_to_tail(a, p);
  135. /* Pick a new passphrase candidate up. */
  136. p = a->passphrases.first;
  137. } else if (a->passphrases.candidate == 1) {
  138. /* This case is that all candidates failed to decrypt. */
  139. a->passphrases.candidate = 0;
  140. if (a->passphrases.first->next != NULL) {
  141. /* Rotate a passphrase list. */
  142. p = remove_passphrases_from_head(a);
  143. add_passphrase_to_tail(a, p);
  144. }
  145. p = NULL;
  146. } else /* There is no passphrase candidate. */
  147. p = NULL;
  148. if (p != NULL)
  149. passphrase = p->passphrase;
  150. else if (a->passphrases.callback != NULL) {
  151. /* Get a passphrase through a call-back function
  152. * since we tried all passphrases out or we don't
  153. * have it. */
  154. passphrase = a->passphrases.callback(&a->archive,
  155. a->passphrases.client_data);
  156. if (passphrase != NULL) {
  157. p = new_read_passphrase(a, passphrase);
  158. if (p == NULL)
  159. return (NULL);
  160. insert_passphrase_to_head(a, p);
  161. a->passphrases.candidate = 1;
  162. }
  163. } else
  164. passphrase = NULL;
  165. return (passphrase);
  166. }