Coverage Report

Created: 2023-06-07 06:46

/src/sudo/lib/util/uuid.c
Line
Count
Source (jump to first uncovered line)
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
53
sudo_uuid_create_v1(unsigned char uuid_out[16])
54
21.7k
{
55
21.7k
    struct uuid uuid;
56
57
21.7k
    arc4random_buf(&uuid, sizeof(uuid));
58
59
    /* Set version to 4 (random), 4 most significant bits (12-15) are 0010. */
60
21.7k
    uuid.time_hi_and_version &= 0x0fff;
61
21.7k
    uuid.time_hi_and_version |= 0x4000;
62
63
    /* Set variant to 1: two most significant bits (6 and 7) are 01. */
64
21.7k
    uuid.clock_seq_hi_and_reserved &= 0x3f;
65
21.7k
    uuid.clock_seq_hi_and_reserved |= 0x80;
66
67
21.7k
    memcpy(uuid_out, &uuid, 16);
68
21.7k
}
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(unsigned char uuid[16], char *dst, size_t dstsiz)
75
21.7k
{
76
21.7k
    const char hex[] = "0123456789abcdef";
77
21.7k
    char *cp = dst;
78
21.7k
    int i;
79
80
21.7k
    if (dstsiz < sizeof("123e4567-e89b-12d3-a456-426655440000"))
81
0
  return NULL;
82
83
370k
    for (i = 0; i < 16; i++) {
84
348k
  *cp++ = hex[uuid[i] >> 4];
85
348k
  *cp++ = hex[uuid[i] & 0x0f];
86
87
348k
  switch (i) {
88
21.7k
  case 4:
89
43.5k
  case 6:
90
65.3k
  case 8:
91
87.1k
  case 10:
92
87.1k
      *cp++ = '-';
93
87.1k
      break;
94
348k
  }
95
348k
    }
96
21.7k
    *cp = '\0';
97
98
21.7k
    return dst;
99
21.7k
}