Debian Codegraph @

crt/s2n/utils/s2n_rfc5952.c

raw symbols SCIP
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
16 #include "utils/s2n_rfc5952.h"
17
18 #include <stdio.h>
19 /* <winsock2.h> internally includes core elements from <windows.h>. For historical
20 * reasons, <windows.h> defaults to including <winsock.h> (Winsock 1.1), whose
21 * declarations conflict with <winsock2.h> (Winsock 2). Defining WIN32_LEAN_AND_MEAN
22 * before including <winsock2.h> prevents this transitive <winsock.h> inclusion.
23 * https://learn.microsoft.com/en-us/windows/win32/winsock/include-files-2
24 */
25 #ifdef _WIN32
26 #define WIN32_LEAN_AND_MEAN
27 #include <winsock2.h>
28 #else
29 #include <sys/socket.h>
30 #endif
31 #include <sys/types.h>
32
33 #include "error/s2n_errno.h"
34 #include "utils/s2n_safety.h"
35
36 static uint8_t dec[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
37 static uint8_t hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
38
39 S2N_RESULT s2n_inet_ntop(int af, const void *addr, struct s2n_blob *dst)
40 {
41 const uint8_t *bytes = addr;
42 uint8_t *cursor = dst->data;
43
44 if (af == AF_INET) {
45 RESULT_ENSURE(dst->size >= sizeof("111.222.333.444"), S2N_ERR_SIZE_MISMATCH);
46
47 for (int i = 0; i < 4; i++) {
48 if (bytes[i] / 100) {
49 *cursor++ = dec[bytes[i] / 100];
50 }
51 if (bytes[i] >= 10) {
52 *cursor++ = dec[(bytes[i] % 100) / 10];
53 }
54 *cursor++ = dec[(bytes[i] % 10)];
55 *cursor++ = '.';
56 }
57
58 *--cursor = '\0';
59
60 return S2N_RESULT_OK;
61 }
62
63 if (af == AF_INET6) {
64 RESULT_ENSURE(dst->size >= sizeof("1111:2222:3333:4444:5555:6666:7777:8888"), S2N_ERR_SIZE_MISMATCH);
65
66 /* See Section 4 of RFC5952 for the rules we are going to follow here
67 *
68 * Here's the general algorithm:
69 *
70 * 1/ Treat the bytes as 8 16-bit fields
71 * 2/ Find the longest run of 16-bit fields.
72 * 3/ or if there are two or more equal length longest runs, go with the left-most run
73 * 4/ Make that run ::
74 * 5/ Print the remaining 16-bit fields in lowercase hex, no leading zeroes
75 */
76
77 uint16_t octets[8] = { 0 };
78
79 int longest_run_start = 0;
80 int longest_run_length = 0;
81 int current_run_length = 0;
82
83 /* 2001:db8::1:0:0:1 */
84
85 /* Find the longest run of zeroes */
86 for (int i = 0; i < 8; i++) {
87 octets[i] = (bytes[i * 2] << 8) + bytes[(i * 2) + 1];
88
89 if (octets[i]) {
90 current_run_length = 0;
91 } else {
92 current_run_length++;
93 }
94
95 if (current_run_length > longest_run_length) {
96 longest_run_length = current_run_length;
97 longest_run_start = (i - current_run_length) + 1;
98 }
99 }
100
101 for (int i = 0; i < 8; i++) {
102 if (i == longest_run_start && longest_run_length > 1) {
103 if (i == 0) {
104 *cursor++ = ':';
105 }
106
107 if (longest_run_length == 8) {
108 *cursor++ = ':';
109 }
110
111 i += longest_run_length - 1;
112
113 } else {
114 uint8_t nibbles[4] = { (octets[i] & 0xF000) >> 12,
115 (octets[i] & 0x0F00) >> 8,
116 (octets[i] & 0x00F0) >> 4,
117 (octets[i] & 0x000F) };
118
119 /* Skip up to three leading zeroes */
120 int j = 0;
121 for (j = 0; j < 3; j++) {
122 if (nibbles[j]) {
123 break;
124 }
125 }
126
127 for (; j < 4; j++) {
128 *cursor++ = hex[nibbles[j]];
129 }
130 }
131
132 *cursor++ = ':';
133 }
134
135 *--cursor = '\0';
136
137 return S2N_RESULT_OK;
138 }
139
140 RESULT_BAIL(S2N_ERR_INVALID_ARGUMENT);
141 }