|
| 1 | +/* Copyright (C) 2025 Jakub Zelenka <jakub.openssl@gmail.com> |
| 2 | + SPDX-License-Identifier: Apache-2.0 */ |
| 3 | + |
| 4 | +#define _GNU_SOURCE |
| 5 | +#include <stdlib.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <string.h> |
| 8 | +#include <openssl/err.h> |
| 9 | +#include <openssl/evp.h> |
| 10 | +#include <openssl/store.h> |
| 11 | +#include <sys/wait.h> |
| 12 | +#include <stdio.h> |
| 13 | +#include "util.h" |
| 14 | + |
| 15 | +static unsigned char *read_file(const char *filename, size_t *len) { |
| 16 | + FILE *fp = fopen(filename, "rb"); |
| 17 | + if (!fp) { |
| 18 | + PRINTERR("Failed to open file: %s\n", filename); |
| 19 | + return NULL; |
| 20 | + } |
| 21 | + |
| 22 | + fseek(fp, 0, SEEK_END); |
| 23 | + *len = ftell(fp); |
| 24 | + fseek(fp, 0, SEEK_SET); |
| 25 | + |
| 26 | + unsigned char *data = malloc(*len); |
| 27 | + if (!data) { |
| 28 | + PRINTERR("Failed to allocate memory for file\n"); |
| 29 | + fclose(fp); |
| 30 | + return NULL; |
| 31 | + } |
| 32 | + |
| 33 | + if (fread(data, 1, *len, fp) != *len) { |
| 34 | + PRINTERR("Failed to read file\n"); |
| 35 | + free(data); |
| 36 | + fclose(fp); |
| 37 | + return NULL; |
| 38 | + } |
| 39 | + |
| 40 | + fclose(fp); |
| 41 | + return data; |
| 42 | +} |
| 43 | + |
| 44 | +static void verify_op(EVP_PKEY *key, const char *input_file, |
| 45 | + const unsigned char *sig, size_t sig_len, |
| 46 | + pid_t pid, const char *stage) { |
| 47 | + EVP_MD_CTX *mdctx = NULL; |
| 48 | + unsigned char *input_data = NULL; |
| 49 | + size_t input_len; |
| 50 | + int ret; |
| 51 | + |
| 52 | + input_data = read_file(input_file, &input_len); |
| 53 | + if (!input_data) { |
| 54 | + PRINTERR("Failed to read input file (pid = %d, stage = %s)\n", pid, |
| 55 | + stage); |
| 56 | + exit(EXIT_FAILURE); |
| 57 | + } |
| 58 | + |
| 59 | + mdctx = EVP_MD_CTX_new(); |
| 60 | + if (!mdctx) { |
| 61 | + PRINTERR("Failed to create MD_CTX (pid = %d, stage = %s)\n", pid, |
| 62 | + stage); |
| 63 | + free(input_data); |
| 64 | + exit(EXIT_FAILURE); |
| 65 | + } |
| 66 | + |
| 67 | + ret = EVP_DigestVerifyInit_ex(mdctx, NULL, "sha256", NULL, |
| 68 | + "provider=pkcs11", key, NULL); |
| 69 | + if (ret != 1) { |
| 70 | + PRINTERROSSL("Failed to init digest verify (pid = %d, stage = %s)\n", |
| 71 | + pid, stage); |
| 72 | + EVP_MD_CTX_free(mdctx); |
| 73 | + free(input_data); |
| 74 | + exit(EXIT_FAILURE); |
| 75 | + } |
| 76 | + |
| 77 | + ret = EVP_DigestVerifyUpdate(mdctx, input_data, input_len); |
| 78 | + if (ret != 1) { |
| 79 | + PRINTERROSSL("Failed to update digest verify (pid = %d, stage = %s)\n", |
| 80 | + pid, stage); |
| 81 | + EVP_MD_CTX_free(mdctx); |
| 82 | + free(input_data); |
| 83 | + exit(EXIT_FAILURE); |
| 84 | + } |
| 85 | + |
| 86 | + ret = EVP_DigestVerifyFinal(mdctx, sig, sig_len); |
| 87 | + if (ret != 1) { |
| 88 | + PRINTERROSSL("Failed to verify signature (pid = %d, stage = %s)\n", |
| 89 | + pid, stage); |
| 90 | + EVP_MD_CTX_free(mdctx); |
| 91 | + free(input_data); |
| 92 | + exit(EXIT_FAILURE); |
| 93 | + } |
| 94 | + |
| 95 | + EVP_MD_CTX_free(mdctx); |
| 96 | + free(input_data); |
| 97 | +} |
| 98 | + |
| 99 | +int main(int argc, char *argv[]) { |
| 100 | + EVP_PKEY *pubkey, *pubkey_main, *privkey; |
| 101 | + unsigned char *sig; |
| 102 | + size_t sig_len; |
| 103 | + pid_t pid = 0; |
| 104 | + int status; |
| 105 | + |
| 106 | + if (argc != 5) { |
| 107 | + fprintf(stderr, "Usage: %s <privkey_uri> <pubkey_uri> <input_file> <signature_file>\n", argv[0]); |
| 108 | + exit(EXIT_FAILURE); |
| 109 | + } |
| 110 | + |
| 111 | + const char *privkey_uri = argv[1]; |
| 112 | + const char *pubkey_uri = argv[2]; |
| 113 | + const char *input_file = argv[3]; |
| 114 | + const char *sig_file = argv[4]; |
| 115 | + |
| 116 | + OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); |
| 117 | + |
| 118 | + sig = read_file(sig_file, &sig_len); |
| 119 | + if (!sig) { |
| 120 | + exit(EXIT_FAILURE); |
| 121 | + } |
| 122 | + |
| 123 | + privkey = load_key(privkey_uri); |
| 124 | + if (!privkey) { |
| 125 | + free(sig); |
| 126 | + exit(EXIT_FAILURE); |
| 127 | + } |
| 128 | + |
| 129 | + pubkey_main = load_key_ex(pubkey_uri, "provider=pkcs11"); |
| 130 | + if (!pubkey_main) { |
| 131 | + EVP_PKEY_free(privkey); |
| 132 | + free(sig); |
| 133 | + exit(EXIT_FAILURE); |
| 134 | + } |
| 135 | + |
| 136 | + /* Duplication to do import */ |
| 137 | + pubkey = EVP_PKEY_dup(pubkey_main); |
| 138 | + EVP_PKEY_free(pubkey_main); |
| 139 | + if (!pubkey) { |
| 140 | + EVP_PKEY_free(privkey); |
| 141 | + free(sig); |
| 142 | + exit(EXIT_FAILURE); |
| 143 | + } |
| 144 | + |
| 145 | + /* verify and pub encrypt in child as refresh after for happens there */ |
| 146 | + verify_op(pubkey, input_file, sig, sig_len, pid, "post-fork"); |
| 147 | + EVP_PKEY_free(privkey); |
| 148 | + EVP_PKEY_free(pubkey); |
| 149 | + |
| 150 | + free(sig); |
| 151 | + |
| 152 | + exit(EXIT_SUCCESS); |
| 153 | +} |
0 commit comments