Vanilla.PDF  2.0.0
Cross-platform toolkit for creating and modifying PDF documents
sign_custom.c

Example use of custom interface callbacks for document signing.

#include "tools.h"
void print_sign_custom_help() {
printf("Usage: sign_custom -s [source file] -d [destination file] -l [license file]");
}
// Custom callbacks
typedef struct {
BufferHandle* data;
} SignatureData;
error_type sign_init(void* user_data, MessageDigestAlgorithmType algorithm) {
SignatureData* signature_data = (SignatureData*) user_data;
const char SAMPLE_DATA[] = "Hello world!";
UNUSED(algorithm);
RETURN_ERROR_IF_NOT_SUCCESS(Buffer_Create(&signature_data->data));
RETURN_ERROR_IF_NOT_SUCCESS(Buffer_SetData(signature_data->data, SAMPLE_DATA, sizeof(SAMPLE_DATA)));
}
error_type sign_update(void* user_data, const BufferHandle* data) {
SignatureData* signature_data = (SignatureData*) user_data;
string_type buffer_data = NULL;
size_type buffer_size = 0;
RETURN_ERROR_IF_NOT_SUCCESS(Buffer_GetData(data, &buffer_data, &buffer_size));
// Calculate signature
UNUSED(signature_data);
}
error_type sign_final(void* user_data, BufferHandle** result) {
SignatureData* signature_data = (SignatureData*) user_data;
*result = signature_data->data;
}
error_type sign_cleanup(void* user_data) {
SignatureData* signature_data = (SignatureData*) user_data;
RETURN_ERROR_IF_NOT_SUCCESS(Buffer_Release(signature_data->data));
}
int process_sign_custom(int argc, char *argv[]) {
string_type license_file = NULL;
string_type source_document_path = NULL;
string_type destination_file_path = NULL;
DocumentHandle* source_document = NULL;
FileHandle* destination_file = NULL;
DocumentSignatureSettingsHandle* signature_settings = NULL;
SigningKeyHandle* signing_key = NULL;
SignatureData user_data = {0};
integer_type i = 0;
for (i = 0; i < argc; ++i) {
// source document path
if (strcmp(argv[i], "-s") == 0 && (i + 1 < argc)) {
source_document_path = argv[i + 1];
i++;
// destination file path
} else if (strcmp(argv[i], "-d") == 0 && (i + 1 < argc)) {
destination_file_path = argv[i + 1];
i++;
// license
} else if (strcmp(argv[i], "-l") == 0 && (i + 1 < argc)) {
license_file = argv[i + 1];
i++;
} else {
print_sign_custom_help();
return VANILLAPDF_TOOLS_ERROR_INVALID_PARAMETERS;
}
}
if (source_document_path == NULL || destination_file_path == NULL) {
print_sign_custom_help();
return VANILLAPDF_TOOLS_ERROR_INVALID_PARAMETERS;
}
if (license_file != NULL) {
RETURN_ERROR_IF_NOT_SUCCESS(LicenseInfo_SetLicenseFile(license_file));
}
RETURN_ERROR_IF_NOT_SUCCESS(SigningKey_CreateCustom(&sign_init, &sign_update, &sign_final, &sign_cleanup, &user_data, &signing_key));
RETURN_ERROR_IF_NOT_SUCCESS(DocumentSignatureSettings_Create(&signature_settings));
RETURN_ERROR_IF_NOT_SUCCESS(DocumentSignatureSettings_SetSigningKey(signature_settings, signing_key));
RETURN_ERROR_IF_NOT_SUCCESS(DocumentSignatureSettings_SetDigest(signature_settings, MessageDigestAlgorithmType_SHA256));
RETURN_ERROR_IF_NOT_SUCCESS(File_Create(destination_file_path, &destination_file));
RETURN_ERROR_IF_NOT_SUCCESS(Document_Open(source_document_path, &source_document));
RETURN_ERROR_IF_NOT_SUCCESS(Document_Sign(source_document, destination_file, signature_settings));
RETURN_ERROR_IF_NOT_SUCCESS(Document_Release(source_document));
RETURN_ERROR_IF_NOT_SUCCESS(File_Release(destination_file));
RETURN_ERROR_IF_NOT_SUCCESS(DocumentSignatureSettings_Release(signature_settings));
RETURN_ERROR_IF_NOT_SUCCESS(SigningKey_Release(signing_key));
return VANILLAPDF_TOOLS_ERROR_SUCCESS;
}
Represents memory stored data.
Represents high-level file access handle.
Group of settings controlling document's digital signature.
Represents low-level file access handle.
Used for document signing.
const error_type VANILLAPDF_ERROR_SUCCESS
Indicates that the operation completed successfully.
uint32_t error_type
This is return value type of all API functions.
Definition c_types.h:25
int32_t integer_type
32-bit signed integer
Definition c_types.h:51
uint32_t size_type
Size type defined in standard library.
Definition c_types.h:62
const char * string_type
C-Style string.
Definition c_types.h:82
MessageDigestAlgorithmType
Supported digest algorithms used as hash functions.
Definition c_message_digest_algorithm.h:22
@ MessageDigestAlgorithmType_SHA256
SHA-2 (Secure Hash Algorithm 2) is a set of cryptographic hash functions designed by the United State...
Definition c_message_digest_algorithm.h:62