/src/sudo/lib/util/uuid.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2020 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 | 20.4k | { |
50 | 20.4k | struct uuid uuid; |
51 | | |
52 | 20.4k | arc4random_buf(&uuid, sizeof(uuid)); |
53 | | |
54 | | /* Set version to 4 (random), 4 most significant bits (12-15) are 0010. */ |
55 | 20.4k | uuid.time_hi_and_version &= 0x0fff; |
56 | 20.4k | uuid.time_hi_and_version |= 0x4000; |
57 | | |
58 | | /* Set variant to 1: two most significant bits (6 and 7) are 01. */ |
59 | 20.4k | uuid.clock_seq_hi_and_reserved &= 0x3f; |
60 | 20.4k | uuid.clock_seq_hi_and_reserved |= 0x80; |
61 | | |
62 | 20.4k | memcpy(uuid_out, &uuid, 16); |
63 | 20.4k | } |
64 | | |
65 | | /* |
66 | | * Format a uuid as a 36-byte string (plus one for the NUL). |
67 | | */ |
68 | | char * |
69 | | sudo_uuid_to_string_v1(const unsigned char uuid[restrict static 16], char * restrict dst, size_t dstsiz) |
70 | 20.4k | { |
71 | 20.4k | const char hex[] = "0123456789abcdef"; |
72 | 20.4k | char *cp = dst; |
73 | 20.4k | unsigned int i; |
74 | | |
75 | 20.4k | if (dstsiz < sizeof("123e4567-e89b-12d3-a456-426655440000")) |
76 | 0 | return NULL; |
77 | | |
78 | 348k | for (i = 0; i < 16; i++) { |
79 | 327k | *cp++ = hex[uuid[i] >> 4]; |
80 | 327k | *cp++ = hex[uuid[i] & 0x0f]; |
81 | | |
82 | 327k | switch (i) { |
83 | 20.4k | case 4: |
84 | 40.9k | case 6: |
85 | 61.4k | case 8: |
86 | 81.9k | case 10: |
87 | 81.9k | *cp++ = '-'; |
88 | 81.9k | break; |
89 | 327k | } |
90 | 327k | } |
91 | 20.4k | *cp = '\0'; |
92 | | |
93 | 20.4k | return dst; |
94 | 20.4k | } |