archive_read_add_passphrase.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. __FBSDID("$FreeBSD$");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #include "archive_read_private.h"
  31. static void
  32. add_passphrase_to_tail(struct archive_read *a,
  33. struct archive_read_passphrase *p)
  34. {
  35. *a->passphrases.last = p;
  36. a->passphrases.last = &p->next;
  37. p->next = NULL;
  38. }
  39. static struct archive_read_passphrase *
  40. remove_passphrases_from_head(struct archive_read *a)
  41. {
  42. struct archive_read_passphrase *p;
  43. p = a->passphrases.first;
  44. if (p != NULL)
  45. a->passphrases.first = p->next;
  46. return (p);
  47. }
  48. static void
  49. insert_passphrase_to_head(struct archive_read *a,
  50. struct archive_read_passphrase *p)
  51. {
  52. p->next = a->passphrases.first;
  53. a->passphrases.first = p;
  54. }
  55. static struct archive_read_passphrase *
  56. new_read_passphrase(struct archive_read *a, const char *passphrase)
  57. {
  58. struct archive_read_passphrase *p;
  59. p = malloc(sizeof(*p));
  60. if (p == NULL) {
  61. archive_set_error(&a->archive, ENOMEM,
  62. "Can't allocate memory");
  63. return (NULL);
  64. }
  65. p->passphrase = strdup(passphrase);
  66. if (p->passphrase == NULL) {
  67. free(p);
  68. archive_set_error(&a->archive, ENOMEM,
  69. "Can't allocate memory");
  70. return (NULL);
  71. }
  72. return (p);
  73. }
  74. int
  75. archive_read_add_passphrase(struct archive *_a, const char *passphrase)
  76. {
  77. struct archive_read *a = (struct archive_read *)_a;
  78. struct archive_read_passphrase *p;
  79. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  80. "archive_read_add_passphrase");
  81. if (passphrase == NULL || passphrase[0] == '\0') {
  82. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  83. "Empty passphrase is unacceptable");
  84. return (ARCHIVE_FAILED);
  85. }
  86. p = new_read_passphrase(a, passphrase);
  87. if (p == NULL)
  88. return (ARCHIVE_FATAL);
  89. add_passphrase_to_tail(a, p);
  90. return (ARCHIVE_OK);
  91. }
  92. int
  93. archive_read_set_passphrase_callback(struct archive *_a, void *client_data,
  94. archive_passphrase_callback *cb)
  95. {
  96. struct archive_read *a = (struct archive_read *)_a;
  97. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  98. "archive_read_set_passphrase_callback");
  99. a->passphrases.callback = cb;
  100. a->passphrases.client_data = client_data;
  101. return (ARCHIVE_OK);
  102. }
  103. /*
  104. * Call this in advance when you start to get a passphrase for decryption
  105. * for a entry.
  106. */
  107. void
  108. __archive_read_reset_passphrase(struct archive_read *a)
  109. {
  110. a->passphrases.candidate = -1;
  111. }
  112. /*
  113. * Get a passphrase for decryption.
  114. */
  115. const char *
  116. __archive_read_next_passphrase(struct archive_read *a)
  117. {
  118. struct archive_read_passphrase *p;
  119. const char *passphrase;
  120. if (a->passphrases.candidate < 0) {
  121. /* Count out how many passphrases we have. */
  122. int cnt = 0;
  123. for (p = a->passphrases.first; p != NULL; p = p->next)
  124. cnt++;
  125. a->passphrases.candidate = cnt;
  126. p = a->passphrases.first;
  127. } else if (a->passphrases.candidate > 1) {
  128. /* Rotate a passphrase list. */
  129. a->passphrases.candidate--;
  130. p = remove_passphrases_from_head(a);
  131. add_passphrase_to_tail(a, p);
  132. /* Pick a new passphrase candidate up. */
  133. p = a->passphrases.first;
  134. } else if (a->passphrases.candidate == 1) {
  135. /* This case is that all candidates failed to decrypt. */
  136. a->passphrases.candidate = 0;
  137. if (a->passphrases.first->next != NULL) {
  138. /* Rotate a passphrase list. */
  139. p = remove_passphrases_from_head(a);
  140. add_passphrase_to_tail(a, p);
  141. }
  142. p = NULL;
  143. } else /* There is no passphrase candidate. */
  144. p = NULL;
  145. if (p != NULL)
  146. passphrase = p->passphrase;
  147. else if (a->passphrases.callback != NULL) {
  148. /* Get a passphrase through a call-back function
  149. * since we tried all passphrases out or we don't
  150. * have it. */
  151. passphrase = a->passphrases.callback(&a->archive,
  152. a->passphrases.client_data);
  153. if (passphrase != NULL) {
  154. p = new_read_passphrase(a, passphrase);
  155. if (p == NULL)
  156. return (NULL);
  157. insert_passphrase_to_head(a, p);
  158. a->passphrases.candidate = 1;
  159. }
  160. } else
  161. passphrase = NULL;
  162. return (passphrase);
  163. }