Vanilla.PDF  1.6.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.
error_type CALLING_CONVENTION Buffer_Release(BufferHandle *handle)
Decrement the internal reference counter.
error_type CALLING_CONVENTION Buffer_SetData(BufferHandle *handle, string_type data, size_type size)
Set new data content.
error_type CALLING_CONVENTION Buffer_Create(BufferHandle **result)
Creates a new buffer instance.
error_type CALLING_CONVENTION Buffer_GetData(const BufferHandle *handle, string_type *data, size_type *size)
Get data content from buffer.
Represents high-level file access handle.
error_type CALLING_CONVENTION Document_Release(DocumentHandle *handle)
Decrement the internal reference counter.
error_type CALLING_CONVENTION Document_Open(string_type filename, DocumentHandle **result)
Opens a new document.
error_type CALLING_CONVENTION Document_Sign(DocumentHandle *handle, FileHandle *destination, DocumentSignatureSettingsHandle *settings)
Digitally signs the document using specified settings.
Group of settings controlling document's digital signature.
error_type CALLING_CONVENTION DocumentSignatureSettings_SetDigest(DocumentSignatureSettingsHandle *handle, MessageDigestAlgorithmType value)
Message digest algorithm for calculation hash of the data to be signed.
error_type CALLING_CONVENTION DocumentSignatureSettings_Release(DocumentSignatureSettingsHandle *handle)
Decrement the internal reference counter.
error_type CALLING_CONVENTION DocumentSignatureSettings_SetSigningKey(DocumentSignatureSettingsHandle *handle, SigningKeyHandle *value)
Get handle to key used for calculating document signature value.
error_type CALLING_CONVENTION DocumentSignatureSettings_Create(DocumentSignatureSettingsHandle **result)
Creates a new DocumentSignatureSettings instance.
Represents low-level file access handle.
error_type CALLING_CONVENTION File_Create(string_type filename, FileHandle **result)
Creates a file for writing.
error_type CALLING_CONVENTION File_Release(FileHandle *handle)
Decrement the internal reference counter.
error_type CALLING_CONVENTION LicenseInfo_SetLicenseFile(string_type filename)
Set path to license file.
Used for document signing.
error_type CALLING_CONVENTION SigningKey_CreateCustom(SigningKey_Initialize_Function sign_init, SigningKey_Update_Function sign_update, SigningKey_Final_Function sign_final, SigningKey_Cleanup_Function sign_cleanup, void *user_data, SigningKeyHandle **result)
Creates a custom SigningKeyHandle to provide custom sign operation.
error_type CALLING_CONVENTION SigningKey_Release(SigningKeyHandle *handle)
Decrement the internal reference counter.
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