s2n_rfc5952.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "utils/s2n_rfc5952.h"
  16. #include <stdio.h>
  17. #include <sys/socket.h>
  18. #include <sys/types.h>
  19. #include "error/s2n_errno.h"
  20. #include "utils/s2n_safety.h"
  21. static uint8_t dec[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  22. static uint8_t hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  23. S2N_RESULT s2n_inet_ntop(int af, const void *addr, struct s2n_blob *dst)
  24. {
  25. const uint8_t *bytes = addr;
  26. uint8_t *cursor = dst->data;
  27. if (af == AF_INET) {
  28. RESULT_ENSURE(dst->size >= sizeof("111.222.333.444"), S2N_ERR_SIZE_MISMATCH);
  29. for (int i = 0; i < 4; i++) {
  30. if (bytes[i] / 100) {
  31. *cursor++ = dec[bytes[i] / 100];
  32. }
  33. if (bytes[i] >= 10) {
  34. *cursor++ = dec[(bytes[i] % 100) / 10];
  35. }
  36. *cursor++ = dec[(bytes[i] % 10)];
  37. *cursor++ = '.';
  38. }
  39. *--cursor = '\0';
  40. return S2N_RESULT_OK;
  41. }
  42. if (af == AF_INET6) {
  43. RESULT_ENSURE(dst->size >= sizeof("1111:2222:3333:4444:5555:6666:7777:8888"), S2N_ERR_SIZE_MISMATCH);
  44. /* See Section 4 of RFC5952 for the rules we are going to follow here
  45. *
  46. * Here's the general algorithm:
  47. *
  48. * 1/ Treat the bytes as 8 16-bit fields
  49. * 2/ Find the longest run of 16-bit fields.
  50. * 3/ or if there are two or more equal length longest runs, go with the left-most run
  51. * 4/ Make that run ::
  52. * 5/ Print the remaining 16-bit fields in lowercase hex, no leading zeroes
  53. */
  54. uint16_t octets[8] = { 0 };
  55. int longest_run_start = 0;
  56. int longest_run_length = 0;
  57. int current_run_length = 0;
  58. /* 2001:db8::1:0:0:1 */
  59. /* Find the longest run of zeroes */
  60. for (int i = 0; i < 8; i++) {
  61. octets[i] = (bytes[i * 2] << 8) + bytes[(i * 2) + 1];
  62. if (octets[i]) {
  63. current_run_length = 0;
  64. } else {
  65. current_run_length++;
  66. }
  67. if (current_run_length > longest_run_length) {
  68. longest_run_length = current_run_length;
  69. longest_run_start = (i - current_run_length) + 1;
  70. }
  71. }
  72. for (int i = 0; i < 8; i++) {
  73. if (i == longest_run_start && longest_run_length > 1) {
  74. if (i == 0) {
  75. *cursor++ = ':';
  76. }
  77. if (longest_run_length == 8) {
  78. *cursor++ = ':';
  79. }
  80. i += longest_run_length - 1;
  81. } else {
  82. uint8_t nibbles[4] = { (octets[i] & 0xF000) >> 12,
  83. (octets[i] & 0x0F00) >> 8,
  84. (octets[i] & 0x00F0) >> 4,
  85. (octets[i] & 0x000F) };
  86. /* Skip up to three leading zeroes */
  87. int j;
  88. for (j = 0; j < 3; j++) {
  89. if (nibbles[j]) {
  90. break;
  91. }
  92. }
  93. for (; j < 4; j++) {
  94. *cursor++ = hex[nibbles[j]];
  95. }
  96. }
  97. *cursor++ = ':';
  98. }
  99. *--cursor = '\0';
  100. return S2N_RESULT_OK;
  101. }
  102. RESULT_BAIL(S2N_ERR_INVALID_ARGUMENT);
  103. }