/src/sudo/lib/util/uuid.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2020-2021, 2025 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | #include <config.h> |
20 | | |
21 | | #include <stdlib.h> |
22 | | #if defined(HAVE_STDINT_H) |
23 | | # include <stdint.h> |
24 | | #elif defined(HAVE_INTTYPES_H) |
25 | | # include <inttypes.h> |
26 | | #endif |
27 | | #include <string.h> |
28 | | #include <arpa/inet.h> |
29 | | |
30 | | #include <sudo_compat.h> |
31 | | #include <sudo_util.h> |
32 | | #include <sudo_rand.h> |
33 | | |
34 | | struct uuid { |
35 | | uint32_t time_low; |
36 | | uint16_t time_mid; |
37 | | uint16_t time_hi_and_version; |
38 | | uint8_t clock_seq_hi_and_reserved; |
39 | | uint8_t clock_seq_low; |
40 | | uint8_t node[6]; |
41 | | }; |
42 | | |
43 | | /* |
44 | | * Create a type 4 (random), variant 1 universally unique identifier (UUID). |
45 | | * As per RFC 4122 section 4.4. |
46 | | */ |
47 | | void |
48 | | sudo_uuid_create_v1(unsigned char uuid_out[restrict static 16]) |
49 | 19.9k | { |
50 | 19.9k | struct uuid uuid; |
51 | | |
52 | 19.9k | arc4random_buf(&uuid, sizeof(uuid)); |
53 | | |
54 | | /* Set version to 4 (random), 4 most significant bits (12-15) are 0010. */ |
55 | 19.9k | uuid.time_hi_and_version &= 0x0fff; |
56 | 19.9k | uuid.time_hi_and_version |= 0x4000; |
57 | | |
58 | | /* Set variant to 1: two most significant bits (6 and 7) are 01. */ |
59 | 19.9k | uuid.clock_seq_hi_and_reserved &= 0x3f; |
60 | 19.9k | uuid.clock_seq_hi_and_reserved |= 0x80; |
61 | | |
62 | | /* Convert 16 and 32-bit fields to network byte order. */ |
63 | 19.9k | uuid.time_low = ntohl(uuid.time_low); |
64 | 19.9k | uuid.time_mid = ntohs(uuid.time_mid); |
65 | 19.9k | uuid.time_hi_and_version = ntohs(uuid.time_hi_and_version); |
66 | | |
67 | 19.9k | memcpy(uuid_out, &uuid, 16); |
68 | 19.9k | } |
69 | | |
70 | | /* |
71 | | * Format a uuid as a 36-byte string (plus one for the NUL). |
72 | | */ |
73 | | char * |
74 | | sudo_uuid_to_string_v1(const unsigned char uuid[restrict static 16], char * restrict dst, size_t dstsiz) |
75 | 19.9k | { |
76 | 19.9k | const char hex[] = "0123456789abcdef"; |
77 | 19.9k | char *cp = dst; |
78 | 19.9k | unsigned int i; |
79 | | |
80 | 19.9k | if (dstsiz < sizeof("123e4567-e89b-12d3-a456-426655440000")) |
81 | 0 | return NULL; |
82 | | |
83 | 338k | for (i = 0; i < 16; i++) { |
84 | 318k | *cp++ = hex[uuid[i] >> 4]; |
85 | 318k | *cp++ = hex[uuid[i] & 0x0f]; |
86 | | |
87 | 318k | switch (i) { |
88 | 79.6k | case 3: case 5: case 7: case 9: |
89 | 79.6k | *cp++ = '-'; |
90 | 79.6k | break; |
91 | 318k | } |
92 | 318k | } |
93 | 19.9k | *cp = '\0'; |
94 | | |
95 | 19.9k | return dst; |
96 | 19.9k | } |
97 | | |
98 | | /* |
99 | | * Parse 36-byte uuid string into a 16-byte binary uuid. |
100 | | * Returns 0 on success, -1 if str is not a valid uuid. |
101 | | */ |
102 | | int |
103 | | sudo_uuid_from_string_v1(const char *str, unsigned char uuid[static 16]) |
104 | 0 | { |
105 | 0 | unsigned int i = 0, j = 0; |
106 | 0 | int ch; |
107 | |
|
108 | 0 | if (strlen(str) != 36) |
109 | 0 | return -1; |
110 | | |
111 | | /* Parse a uuid in the format 123e4567-e89b-12d3-a456-426655440000 */ |
112 | 0 | while (i < 36) { |
113 | 0 | switch (i) { |
114 | 0 | case 8: case 13: case 18: case 23: |
115 | 0 | if (str[i] != '-') |
116 | 0 | return -1; |
117 | 0 | i++; |
118 | 0 | FALLTHROUGH; |
119 | 0 | default: |
120 | 0 | ch = sudo_hexchar(str + i); |
121 | 0 | if (ch == -1) |
122 | 0 | return -1; |
123 | 0 | uuid[j++] = ch; |
124 | 0 | i += 2; |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | 0 | return 0; |
129 | 0 | } |