/* Copyright (c) 2026 Juhapekka Piiroinen * All Rights Reserved. * AmoosedWeb project: https://amoosed.com * * Source-free SDK helper example: write a new 32-byte key as 64 hex digits. * The output path belongs outside the published site and source control. */ #ifndef _WIN32 # define _POSIX_C_SOURCE 200809L #endif #include #include #include #include #ifdef _WIN32 # include # include # include # include #else # include # include # include # include # include #endif static void clear_bytes(void* memory, size_t length) { volatile uint8_t* bytes = (volatile uint8_t*)memory; while (length--) *bytes++ = 0; } static void hex_key(const uint8_t key[32], char text[65]) { static const char digits[] = "0123456789abcdef"; for (size_t i = 0; i < 32; ++i) { text[i * 2] = digits[key[i] >> 4]; text[i * 2 + 1] = digits[key[i] & 15]; } text[64] = '\n'; } #ifdef _WIN32 static int private_output(const wchar_t* path, const char text[65]) { HANDLE token = NULL; DWORD length = 0; uint8_t* token_user = NULL; PACL acl = NULL; HANDLE file = INVALID_HANDLE_VALUE; int result = 0; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) goto done; GetTokenInformation(token, TokenUser, NULL, 0, &length); if (!length || !(token_user = (uint8_t*)malloc(length)) || !GetTokenInformation(token, TokenUser, token_user, length, &length)) goto done; EXPLICIT_ACCESSW access = {0}; access.grfAccessPermissions = GENERIC_ALL; access.grfAccessMode = SET_ACCESS; access.grfInheritance = NO_INHERITANCE; access.Trustee.TrusteeForm = TRUSTEE_IS_SID; access.Trustee.TrusteeType = TRUSTEE_IS_USER; access.Trustee.ptstrName = (LPWSTR)((TOKEN_USER*)token_user)->User.Sid; if (SetEntriesInAclW(1, &access, NULL, &acl) != ERROR_SUCCESS) goto done; SECURITY_DESCRIPTOR descriptor; if (!InitializeSecurityDescriptor(&descriptor, SECURITY_DESCRIPTOR_REVISION) || !SetSecurityDescriptorDacl(&descriptor, TRUE, acl, FALSE) || !SetSecurityDescriptorControl(&descriptor, SE_DACL_PROTECTED, SE_DACL_PROTECTED)) goto done; SECURITY_ATTRIBUTES attributes = {sizeof(attributes), &descriptor, FALSE}; file = CreateFileW(path, GENERIC_WRITE, 0, &attributes, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); if (file == INVALID_HANDLE_VALUE) goto done; DWORD written = 0; if (!WriteFile(file, text, 65, &written, NULL) || written != 65 || !FlushFileBuffers(file)) { CloseHandle(file); file = INVALID_HANDLE_VALUE; DeleteFileW(path); goto done; } result = 1; done: if (file != INVALID_HANDLE_VALUE) CloseHandle(file); if (acl) LocalFree(acl); if (token_user) free(token_user); if (token) CloseHandle(token); return result; } int wmain(int argc, wchar_t** argv) { if (argc != 2) { fputs("Usage: native-keygen ABSOLUTE_OUTPUT_KEY_FILE\n", stderr); return 2; } uint8_t key[32] = {0}; char text[65] = {0}; int result = 1; if (!BCRYPT_SUCCESS(BCryptGenRandom(NULL, key, sizeof(key), BCRYPT_USE_SYSTEM_PREFERRED_RNG))) fputs("OS random source failed.\n", stderr); else { hex_key(key, text); if (private_output(argv[1], text)) result = 0; else fputs("Cannot create a private key file at that new path.\n", stderr); } clear_bytes(text, sizeof(text)); clear_bytes(key, sizeof(key)); if (!result) puts("Created a private 256-bit key file."); return result; } #else static int random_key(uint8_t key[32]) { size_t offset = 0; while (offset < 32) { const ssize_t got = getrandom(key + offset, 32 - offset, 0); if (got < 0 && errno == EINTR) continue; if (got <= 0) return 0; offset += (size_t)got; } return 1; } static int private_output(const char* path, const char text[65]) { const int file = open(path, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0600); if (file < 0) return 0; size_t offset = 0; while (offset < 65) { const ssize_t wrote = write(file, text + offset, 65 - offset); if (wrote < 0 && errno == EINTR) continue; if (wrote <= 0) break; offset += (size_t)wrote; } struct stat status; const int private_mode = fstat(file, &status) == 0 && S_ISREG(status.st_mode) && status.st_uid == geteuid() && (status.st_mode & 0400) != 0 && (status.st_mode & 0077) == 0; const int complete = offset == 65 && private_mode && fsync(file) == 0; const int closed = close(file) == 0; if (!complete || !closed) { unlink(path); return 0; } return 1; } int main(int argc, char** argv) { if (argc != 2) { fputs("Usage: native-keygen ABSOLUTE_OUTPUT_KEY_FILE\n", stderr); return 2; } uint8_t key[32] = {0}; char text[65] = {0}; int result = 1; if (!random_key(key)) fputs("OS random source failed.\n", stderr); else { hex_key(key, text); if (private_output(argv[1], text)) result = 0; else fputs("Cannot create a private key file at that new path.\n", stderr); } clear_bytes(text, sizeof(text)); clear_bytes(key, sizeof(key)); if (!result) puts("Created a private 256-bit key file."); return result; } #endif