|
| 1 | +/** |
| 2 | + * @file uuid.cc |
| 3 | + * |
| 4 | + * @section LICENSE |
| 5 | + * |
| 6 | + * The MIT License |
| 7 | + * |
| 8 | + * @copyright Copyright (c) 2018-2023 TileDB, Inc. |
| 9 | + * |
| 10 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | + * of this software and associated documentation files (the "Software"), to deal |
| 12 | + * in the Software without restriction, including without limitation the rights |
| 13 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | + * copies of the Software, and to permit persons to whom the Software is |
| 15 | + * furnished to do so, subject to the following conditions: |
| 16 | + * |
| 17 | + * The above copyright notice and this permission notice shall be included in |
| 18 | + * all copies or substantial portions of the Software. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | + * THE SOFTWARE. |
| 27 | + * |
| 28 | + * @section DESCRIPTION |
| 29 | + * |
| 30 | + * This file defines a platform-independent UUID generator. |
| 31 | + */ |
| 32 | + |
| 33 | +#include <mutex> |
| 34 | +#include <vector> |
| 35 | + |
| 36 | +#include "tiledb/sm/misc/uuid.h" |
| 37 | + |
| 38 | +#ifdef _WIN32 |
| 39 | +#include <Rpc.h> |
| 40 | +#else |
| 41 | +#include <openssl/err.h> |
| 42 | +#include <openssl/rand.h> |
| 43 | +#include <cstdio> |
| 44 | +#endif |
| 45 | + |
| 46 | +using namespace tiledb::common; |
| 47 | + |
| 48 | +namespace tiledb::sm::uuid { |
| 49 | + |
| 50 | +/** Mutex to guard UUID generation. */ |
| 51 | +static std::mutex uuid_mtx; |
| 52 | + |
| 53 | +#ifdef _WIN32 |
| 54 | + |
| 55 | +/** |
| 56 | + * Generate a UUID using Win32 RPC API. |
| 57 | + */ |
| 58 | +Status generate_uuid_win32(std::string* uuid_str) { |
| 59 | + if (uuid_str == nullptr) |
| 60 | + return Status_UtilsError("Null UUID string argument"); |
| 61 | + |
| 62 | + UUID uuid; |
| 63 | + RPC_STATUS rc = UuidCreate(&uuid); |
| 64 | + if (rc != RPC_S_OK) |
| 65 | + return Status_UtilsError("Unable to generate Win32 UUID: creation error"); |
| 66 | + |
| 67 | + char* buf = nullptr; |
| 68 | + rc = UuidToStringA(&uuid, reinterpret_cast<RPC_CSTR*>(&buf)); |
| 69 | + if (rc != RPC_S_OK) |
| 70 | + return Status_UtilsError( |
| 71 | + "Unable to generate Win32 UUID: string conversion error"); |
| 72 | + |
| 73 | + *uuid_str = std::string(buf); |
| 74 | + |
| 75 | + rc = RpcStringFreeA(reinterpret_cast<RPC_CSTR*>(&buf)); |
| 76 | + if (rc != RPC_S_OK) |
| 77 | + return Status_UtilsError("Unable to generate Win32 UUID: free error"); |
| 78 | + |
| 79 | + return Status::Ok(); |
| 80 | +} |
| 81 | + |
| 82 | +#else |
| 83 | + |
| 84 | +/** |
| 85 | + * Generate a UUID using OpenSSL. |
| 86 | + * |
| 87 | + * Initially from: https://gist.github.com/kvelakur/9069c9896577c3040030 |
| 88 | + * "Generating a Version 4 UUID using OpenSSL" |
| 89 | + */ |
| 90 | +Status generate_uuid_openssl(std::string* uuid_str) { |
| 91 | + if (uuid_str == nullptr) |
| 92 | + return Status_UtilsError("Null UUID string argument"); |
| 93 | + |
| 94 | + union { |
| 95 | + struct { |
| 96 | + uint32_t time_low; |
| 97 | + uint16_t time_mid; |
| 98 | + uint16_t time_hi_and_version; |
| 99 | + uint8_t clk_seq_hi_res; |
| 100 | + uint8_t clk_seq_low; |
| 101 | + uint8_t node[6]; |
| 102 | + }; |
| 103 | + uint8_t __rnd[16]; |
| 104 | + } uuid; |
| 105 | + |
| 106 | + int rc = RAND_bytes(uuid.__rnd, sizeof(uuid)); |
| 107 | + if (rc < 1) { |
| 108 | + char err_msg[256]; |
| 109 | + ERR_error_string_n(ERR_get_error(), err_msg, sizeof(err_msg)); |
| 110 | + return Status_UtilsError( |
| 111 | + "Cannot generate random bytes with OpenSSL: " + std::string(err_msg)); |
| 112 | + } |
| 113 | + |
| 114 | + // Refer Section 4.2 of RFC-4122 |
| 115 | + // https://tools.ietf.org/html/rfc4122#section-4.2 |
| 116 | + uuid.clk_seq_hi_res = (uint8_t)((uuid.clk_seq_hi_res & 0x3F) | 0x80); |
| 117 | + uuid.time_hi_and_version = |
| 118 | + (uint16_t)((uuid.time_hi_and_version & 0x0FFF) | 0x4000); |
| 119 | + |
| 120 | + // Format the UUID as a string. |
| 121 | + char buf[128]; |
| 122 | + rc = snprintf( |
| 123 | + buf, |
| 124 | + sizeof(buf), |
| 125 | + "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 126 | + uuid.time_low, |
| 127 | + uuid.time_mid, |
| 128 | + uuid.time_hi_and_version, |
| 129 | + uuid.clk_seq_hi_res, |
| 130 | + uuid.clk_seq_low, |
| 131 | + uuid.node[0], |
| 132 | + uuid.node[1], |
| 133 | + uuid.node[2], |
| 134 | + uuid.node[3], |
| 135 | + uuid.node[4], |
| 136 | + uuid.node[5]); |
| 137 | + |
| 138 | + if (rc < 0) |
| 139 | + return Status_UtilsError("Error formatting UUID string"); |
| 140 | + |
| 141 | + *uuid_str = std::string(buf); |
| 142 | + |
| 143 | + return Status::Ok(); |
| 144 | +} |
| 145 | + |
| 146 | +#endif |
| 147 | + |
| 148 | +Status generate_uuid(std::string* uuid, bool hyphenate) { |
| 149 | + if (uuid == nullptr) |
| 150 | + return Status_UtilsError("Null UUID string argument"); |
| 151 | + |
| 152 | + std::string uuid_str; |
| 153 | + { |
| 154 | + // OpenSSL is not threadsafe, so grab a lock here. We are locking in the |
| 155 | + // Windows case as well just to be careful. |
| 156 | + std::unique_lock<std::mutex> lck(uuid_mtx); |
| 157 | +#ifdef _WIN32 |
| 158 | + RETURN_NOT_OK(generate_uuid_win32(&uuid_str)); |
| 159 | +#else |
| 160 | + RETURN_NOT_OK(generate_uuid_openssl(&uuid_str)); |
| 161 | +#endif |
| 162 | + } |
| 163 | + |
| 164 | + uuid->clear(); |
| 165 | + for (unsigned i = 0; i < uuid_str.length(); i++) { |
| 166 | + if (uuid_str[i] == '-' && !hyphenate) |
| 167 | + continue; |
| 168 | + uuid->push_back(uuid_str[i]); |
| 169 | + } |
| 170 | + |
| 171 | + return Status::Ok(); |
| 172 | +} |
| 173 | + |
| 174 | +} // namespace tiledb::sm::uuid |
0 commit comments