s2n_stuffer_base64.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <string.h>
  16. #include "error/s2n_errno.h"
  17. #include "stuffer/s2n_stuffer.h"
  18. #include "utils/s2n_safety.h"
  19. static const uint8_t b64[64] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
  20. 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  21. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+',
  22. '/' };
  23. /* Generated with this python:
  24. *
  25. * b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  26. *
  27. * for i in range(0, 256):
  28. * if chr(i) in b64:
  29. * print str(b64.index(chr(i))) + ", ",
  30. * else:
  31. * print "255, ",
  32. *
  33. * if (i + 1) % 16 == 0:
  34. * print
  35. *
  36. * Note that '=' maps to 64.
  37. */
  38. static const uint8_t b64_inverse[256] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  39. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  40. 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 64, 255, 255,
  41. 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255,
  42. 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  43. 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  44. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  45. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  46. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  47. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  48. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
  49. bool s2n_is_base64_char(unsigned char c)
  50. {
  51. return (b64_inverse[*((uint8_t *) (&c))] != 255);
  52. }
  53. /**
  54. * NOTE:
  55. * In general, shift before masking. This avoids needing to worry about how the
  56. * signed bit may be handled.
  57. */
  58. int s2n_stuffer_read_base64(struct s2n_stuffer *stuffer, struct s2n_stuffer *out)
  59. {
  60. POSIX_PRECONDITION(s2n_stuffer_validate(stuffer));
  61. POSIX_PRECONDITION(s2n_stuffer_validate(out));
  62. int bytes_this_round = 3;
  63. s2n_stack_blob(o, 4, 4);
  64. do {
  65. if (s2n_stuffer_data_available(stuffer) < o.size) {
  66. break;
  67. }
  68. POSIX_GUARD(s2n_stuffer_read(stuffer, &o));
  69. uint8_t value1 = b64_inverse[o.data[0]];
  70. uint8_t value2 = b64_inverse[o.data[1]];
  71. uint8_t value3 = b64_inverse[o.data[2]];
  72. uint8_t value4 = b64_inverse[o.data[3]];
  73. /* We assume the entire thing is base64 data, thus, terminate cleanly if we encounter a non-base64 character */
  74. if (value1 == 255) {
  75. /* Undo the read */
  76. stuffer->read_cursor -= o.size;
  77. POSIX_BAIL(S2N_ERR_INVALID_BASE64);
  78. }
  79. /* The first two characters can never be '=' and in general
  80. * everything has to be a valid character.
  81. */
  82. POSIX_ENSURE(!(value1 == 64 || value2 == 64 || value2 == 255 || value3 == 255 || value4 == 255),
  83. S2N_ERR_INVALID_BASE64);
  84. if (o.data[2] == '=') {
  85. /* If there is only one output byte, then the second value
  86. * should have none of its bottom four bits set.
  87. */
  88. POSIX_ENSURE(!(o.data[3] != '=' || value2 & 0x0f), S2N_ERR_INVALID_BASE64);
  89. bytes_this_round = 1;
  90. value3 = 0;
  91. value4 = 0;
  92. } else if (o.data[3] == '=') {
  93. /* The last two bits of the final value should be unset */
  94. POSIX_ENSURE(!(value3 & 0x03), S2N_ERR_INVALID_BASE64);
  95. bytes_this_round = 2;
  96. value4 = 0;
  97. }
  98. /* Advance by bytes_this_round, and then fill in the data */
  99. POSIX_GUARD(s2n_stuffer_skip_write(out, bytes_this_round));
  100. uint8_t *ptr = out->blob.data + out->write_cursor - bytes_this_round;
  101. /* value1 maps to the first 6 bits of the first data byte */
  102. /* value2's top two bits are the rest */
  103. *ptr = ((value1 << 2) & 0xfc) | ((value2 >> 4) & 0x03);
  104. if (bytes_this_round > 1) {
  105. /* Put the next four bits in the second data byte */
  106. /* Put the next four bits in the third data byte */
  107. ptr++;
  108. *ptr = ((value2 << 4) & 0xf0) | ((value3 >> 2) & 0x0f);
  109. }
  110. if (bytes_this_round > 2) {
  111. /* Put the next two bits in the third data byte */
  112. /* Put the next six bits in the fourth data byte */
  113. ptr++;
  114. *ptr = ((value3 << 6) & 0xc0) | (value4 & 0x3f);
  115. }
  116. } while (bytes_this_round == 3);
  117. return S2N_SUCCESS;
  118. }
  119. int s2n_stuffer_write_base64(struct s2n_stuffer *stuffer, struct s2n_stuffer *in)
  120. {
  121. POSIX_PRECONDITION(s2n_stuffer_validate(stuffer));
  122. POSIX_PRECONDITION(s2n_stuffer_validate(in));
  123. s2n_stack_blob(o, 4, 4);
  124. s2n_stack_blob(i, 3, 3);
  125. while (s2n_stuffer_data_available(in) > 2) {
  126. POSIX_GUARD(s2n_stuffer_read(in, &i));
  127. /* Take the top 6-bits of the first data byte */
  128. o.data[0] = b64[(i.data[0] >> 2) & 0x3f];
  129. /* Take the bottom 2-bits of the first data byte - 0b00110000 = 0x30
  130. * and take the top 4-bits of the second data byte - 0b00001111 = 0x0f
  131. */
  132. o.data[1] = b64[((i.data[0] << 4) & 0x30) | ((i.data[1] >> 4) & 0x0f)];
  133. /* Take the bottom 4-bits of the second data byte - 0b00111100 = 0x3c
  134. * and take the top 2-bits of the third data byte - 0b00000011 = 0x03
  135. */
  136. o.data[2] = b64[((i.data[1] << 2) & 0x3c) | ((i.data[2] >> 6) & 0x03)];
  137. /* Take the bottom 6-bits of the second data byte - 0b00111111 = 0x3f
  138. */
  139. o.data[3] = b64[i.data[2] & 0x3f];
  140. POSIX_GUARD(s2n_stuffer_write(stuffer, &o));
  141. }
  142. if (s2n_stuffer_data_available(in)) {
  143. /* Read just one byte */
  144. i.size = 1;
  145. POSIX_GUARD(s2n_stuffer_read(in, &i));
  146. uint8_t c = i.data[0];
  147. /* We at least one data byte left to encode, encode
  148. * its first six bits
  149. */
  150. o.data[0] = b64[(c >> 2) & 0x3f];
  151. /* And our end has to be an equals */
  152. o.data[3] = '=';
  153. /* How many bytes are actually left? */
  154. if (s2n_stuffer_data_available(in) == 0) {
  155. /* We just have the last two bits to deal with */
  156. o.data[1] = b64[(c << 4) & 0x30];
  157. o.data[2] = '=';
  158. } else {
  159. /* Read the last byte */
  160. POSIX_GUARD(s2n_stuffer_read(in, &i));
  161. o.data[1] = b64[((c << 4) & 0x30) | ((i.data[0] >> 4) & 0x0f)];
  162. o.data[2] = b64[((i.data[0] << 2) & 0x3c)];
  163. }
  164. POSIX_GUARD(s2n_stuffer_write(stuffer, &o));
  165. }
  166. return S2N_SUCCESS;
  167. }