64 lines
1.3 KiB
Text
64 lines
1.3 KiB
Text
|
|
OpenSSL_add_all_algorithms();
|
|
ERR_load_crypto_strings();
|
|
ERR_clear_error();
|
|
ENGINE_load_builtin_engines();
|
|
|
|
e = ENGINE_by_id("pkcs11");
|
|
if (!e) {
|
|
display_openssl_errors(__LINE__);
|
|
exit(1);
|
|
}
|
|
|
|
if (module_path) {
|
|
fprintf(stderr, "loading: %s\n", module_path);
|
|
if (!ENGINE_ctrl_cmd_string(e, "MODULE_PATH", module_path, 0)) {
|
|
display_openssl_errors(__LINE__);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
if (!ENGINE_init(e)) {
|
|
display_openssl_errors(__LINE__);
|
|
exit(1);
|
|
}
|
|
|
|
if (key_pass && !ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0)) {
|
|
display_openssl_errors(__LINE__);
|
|
exit(1);
|
|
}
|
|
|
|
private_key = ENGINE_load_private_key(e, private_key_name, NULL, NULL);
|
|
if (!private_key) {
|
|
fprintf(stderr, "cannot load: %s\n", private_key_name);
|
|
display_openssl_errors(__LINE__);
|
|
exit(1);
|
|
}
|
|
|
|
display_openssl_errors(__LINE__);
|
|
|
|
digest_algo = EVP_get_digestbyname("sha256");
|
|
|
|
EVP_MD_CTX_init(&ctx);
|
|
if (EVP_DigestInit(&ctx, digest_algo) <= 0) {
|
|
display_openssl_errors(__LINE__);
|
|
exit(1);
|
|
}
|
|
|
|
EVP_SignInit(&ctx, digest_algo);
|
|
|
|
#define TEST_DATA "test data"
|
|
if (EVP_SignUpdate(&ctx, TEST_DATA, sizeof(TEST_DATA) - 1) <= 0) {
|
|
display_openssl_errors(__LINE__);
|
|
exit(1);
|
|
}
|
|
|
|
n = sizeof(buf);
|
|
if (EVP_SignFinal(&ctx, buf, &n, private_key) <= 0) {
|
|
display_openssl_errors(__LINE__);
|
|
exit(1);
|
|
}
|
|
|
|
EVP_PKEY_free(private_key);
|
|
ENGINE_finish(e);
|
|
|