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 /*
20  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22  */
23 
24 #include <config.h>
25 
26 #include <stdlib.h>
27 #if defined(HAVE_STDINT_H)
28 # include <stdint.h>
29 #elif defined(HAVE_INTTYPES_H)
30 # include <inttypes.h>
31 #endif
32 #include <string.h>
33 #include <arpa/inet.h>
34 
35 #include "sudo_compat.h"
36 #include "sudo_util.h"
37 #include "sudo_rand.h"
38 
39 struct uuid {
40     uint32_t time_low;
41     uint16_t time_mid;
42     uint16_t time_hi_and_version;
43     uint8_t clock_seq_hi_and_reserved;
44     uint8_t clock_seq_low;
45     uint8_t node[6];
46 };
47 
48 /*
49  * Create a type 4 (random), variant 1 universally unique identifier (UUID).
50  * As per RFC 4122 section 4.4.
51  */
52 void
sudo_uuid_create_v1(unsigned char uuid_out[16])53 sudo_uuid_create_v1(unsigned char uuid_out[16])
54 {
55     union {
56 	struct uuid id;
57 	unsigned char u8[16];
58     } uuid;
59 
60     arc4random_buf(&uuid, sizeof(uuid));
61 
62     /* Set version to 4 (random), 4 most significant bits (12-15) are 0010. */
63     uuid.id.time_hi_and_version &= 0x0fff;
64     uuid.id.time_hi_and_version |= 0x4000;
65 
66     /* Set variant to 1: two most significant bits (6 and 7) are 01. */
67     uuid.id.clock_seq_hi_and_reserved &= 0x3f;
68     uuid.id.clock_seq_hi_and_reserved |= 0x80;
69 
70     memcpy(uuid_out, &uuid, 16);
71 }
72 
73 /*
74  * Format a uuid as a 36-byte string (plus one for the NUL).
75  */
76 char *
sudo_uuid_to_string_v1(unsigned char uuid[16],char * dst,size_t dstsiz)77 sudo_uuid_to_string_v1(unsigned char uuid[16], char *dst, size_t dstsiz)
78 {
79     const char hex[] = "0123456789abcdef";
80     char *cp = dst;
81     int i;
82 
83     if (dstsiz < sizeof("123e4567-e89b-12d3-a456-426655440000"))
84 	return NULL;
85 
86     for (i = 0; i < 16; i++) {
87 	*cp++ = hex[uuid[i] >> 4];
88 	*cp++ = hex[uuid[i] & 0x0f];
89 
90 	switch (i) {
91 	case 4:
92 	case 6:
93 	case 8:
94 	case 10:
95 	    *cp++ = '-';
96 	    break;
97 	}
98     }
99     *cp = '\0';
100 
101     return dst;
102 }
103