25
25
#define RSA_KEY_SIZE 4096
26
26
#define BUFFER_SIZE 16000
27
27
28
+ static bool read_id_file (const char * filename , char * buffer , size_t buffer_size )
29
+ {
30
+ FILE * f = fopen (filename , "r" );
31
+ if (f == NULL )
32
+ return false;
33
+
34
+ if (fread (buffer , 1 , buffer_size , f ) != buffer_size )
35
+ {
36
+ fclose (f );
37
+ return false;
38
+ }
39
+
40
+ fclose (f );
41
+ return true;
42
+ }
43
+
28
44
static mbedtls_entropy_context entropy = { 0 };
29
45
static mbedtls_ctr_drbg_context ctr_drbg = { 0 };
30
46
/**
@@ -47,28 +63,23 @@ bool init_entropy(void)
47
63
mbedtls_ctr_drbg_init (& ctr_drbg );
48
64
mbedtls_entropy_init (& entropy );
49
65
50
- char machine_id [33 ] = { 0 };
51
- // Get machine-id
66
+ // Get machine-id (this may fail in containers)
52
67
// https://www.freedesktop.org/software/systemd/man/latest/machine-id.html
53
- FILE * f = fopen ("/etc/machine-id" , "r" );
54
- if (f == NULL )
55
- {
56
- log_warn ("Could not open /etc/machine-id, using fallback" );
57
- strcpy (machine_id , "c7bde55876876987accc913546f3bcc" );
58
- }
59
- else
68
+ char machine_id [128 ] = { 0 };
69
+ read_id_file ("/etc/machine-id" , machine_id , sizeof (machine_id ));
70
+
71
+ // The boot_id random ID that is regenerated on each boot. As such it
72
+ // can be used to identify the local machine’s current boot. It’s
73
+ // universally available on any recent Linux kernel. It’s a good and
74
+ // safe choice if you need to identify a specific boot on a specific
75
+ // booted kernel.
76
+ // Read /proc/sys/kernel/random/boot_id and append it to machine_id
77
+ // The UUID is in format 8-4-4-4-12 and, hence, 36 characters long
78
+ char boot_id [37 ] = { 0 };
79
+ if (read_id_file ("/proc/sys/kernel/random/boot_id" , boot_id , sizeof (boot_id )))
60
80
{
61
- if (fread (machine_id , 1 , 32 , f ) != 32 )
62
- {
63
- log_warn ("Could not read /etc/machine-id, using fallback" );
64
- strcpy (machine_id , "c7bde55876876987accc913546f3bcc" );
65
- }
66
- else
67
- {
68
- // Ensure null-termination
69
- machine_id [32 ] = '\0' ;
70
- }
71
- fclose (f );
81
+ boot_id [36 ] = '\0' ;
82
+ strncat (machine_id , boot_id , sizeof (machine_id ) - strlen (machine_id ) - 1 );
72
83
}
73
84
74
85
// Initialize random number generator
@@ -79,7 +90,6 @@ bool init_entropy(void)
79
90
return false;
80
91
}
81
92
82
- log_info ("Random number generator initialized successfully" );
83
93
initialized = true;
84
94
return true;
85
95
}
0 commit comments