2 * crypto.c Encryption support functions
4 * Author: Landon Fuller <landonf@opendarwin.org>
8 * Copyright (C) 2005 Kern Sibbald
10 * This file was contributed to the Bacula project by Landon Fuller.
12 * Landon Fuller has been granted a perpetual, worldwide, non-exclusive,
13 * no-charge, royalty-free, irrevocable copyright license to reproduce,
14 * prepare derivative works of, publicly display, publicly perform,
15 * sublicense, and distribute the original work contributed by Landon Fuller
16 * to the Bacula project in source or object form.
18 * If you wish to license these contributions under an alternate open source
19 * license please contact Landon Fuller <landonf@opendarwin.org>.
22 Copyright (C) 2005 Kern Sibbald
24 This program is free software; you can redistribute it and/or
25 modify it under the terms of the GNU General Public License
26 version 2 as amended with additional clauses defined in the
27 file LICENSE in the main source directory.
29 This program is distributed in the hope that it will be useful,
30 but WITHOUT ANY WARRANTY; without even the implied warranty of
31 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 the file LICENSE for additional details.
44 * Prefix: iso.org.dod.internet.private.enterprise.threerings.external.bacula (1.3.6.1.4.1.22054.500.2)
45 * Organization: Bacula Project
46 * Contact Name: Kern Sibbald
47 * Contact E-mail: kern@sibbald.com
49 * Top Level Allocations - 500.2
50 * 1 - Published Allocations
51 * 1.1 - Bacula Encryption
53 * Bacula Encryption - 500.2.1.1
56 * 2 - ASN.1 Object Identifiers
62 * BaculaCrypto { iso(1) identified-organization(3) usdod(6)
63 * internet(1) private(4) enterprises(1) three-rings(22054)
64 * external(500) bacula(2) published(1) bacula-encryption(1)
65 * asn1-modules(1) bacula-crypto(1) }
67 * DEFINITIONS AUTOMATIC TAGS ::=
70 * SignatureData ::= SEQUENCE {
71 * version Version DEFAULT v0,
72 * signerInfo SignerInfo }
74 * CryptoData ::= SEQUENCE {
75 * version Version DEFAULT v0,
76 * contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier,
77 * iv InitializationVector,
78 * recipientInfo RecipientInfo
81 * SignerInfo ::= SET OF SignerInfo
82 * RecipientInfo ::= SET OF RecipientInfo
84 * Version ::= INTEGER { v0(0) }
86 * SignerInfo ::= SEQUENCE {
88 * subjectKeyIdentifier SubjectKeyIdentifier,
89 * digestAlgorithm DigestAlgorithmIdentifier,
90 * signatureAlgorithm SignatureAlgorithmIdentifier,
91 * signature SignatureValue }
93 * RecipientInfo ::= SEQUENCE {
95 * subjectKeyIdentifier SubjectKeyIdentifier
96 * keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier
97 * encryptedKey EncryptedKey
100 * SubjectKeyIdentifier ::= OCTET STRING
102 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
104 * SignatureAlgorithmIdentifier ::= AlgorithmIdentifier
106 * KeyEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
108 * ContentEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
110 * InitializationVector ::= OCTET STRING
112 * SignatureValue ::= OCTET STRING
114 * EncryptedKey ::= OCTET STRING
116 * AlgorithmIdentifier ::= OBJECT IDENTIFIER
121 #ifdef HAVE_CRYPTO /* Is encryption enabled? */
122 #ifdef HAVE_OPENSSL /* How about OpenSSL? */
124 /* Are we initialized? */
125 static int crypto_initialized = false;
127 /* ASN.1 Declarations */
128 #define BACULA_ASN1_VERSION 0
131 ASN1_INTEGER *version;
132 ASN1_OCTET_STRING *subjectKeyIdentifier;
133 ASN1_OBJECT *digestAlgorithm;
134 ASN1_OBJECT *signatureAlgorithm;
135 ASN1_OCTET_STRING *signature;
139 ASN1_INTEGER *version;
140 ASN1_OCTET_STRING *subjectKeyIdentifier;
141 ASN1_OBJECT *keyEncryptionAlgorithm;
142 ASN1_OCTET_STRING *encryptedKey;
145 ASN1_SEQUENCE(SignerInfo) = {
146 ASN1_SIMPLE(SignerInfo, version, ASN1_INTEGER),
147 ASN1_SIMPLE(SignerInfo, subjectKeyIdentifier, ASN1_OCTET_STRING),
148 ASN1_SIMPLE(SignerInfo, digestAlgorithm, ASN1_OBJECT),
149 ASN1_SIMPLE(SignerInfo, signatureAlgorithm, ASN1_OBJECT),
150 ASN1_SIMPLE(SignerInfo, signature, ASN1_OCTET_STRING)
151 } ASN1_SEQUENCE_END(SignerInfo);
153 ASN1_SEQUENCE(RecipientInfo) = {
154 ASN1_SIMPLE(RecipientInfo, version, ASN1_INTEGER),
155 ASN1_SIMPLE(RecipientInfo, subjectKeyIdentifier, ASN1_OCTET_STRING),
156 ASN1_SIMPLE(RecipientInfo, keyEncryptionAlgorithm, ASN1_OBJECT),
157 ASN1_SIMPLE(RecipientInfo, encryptedKey, ASN1_OCTET_STRING),
158 } ASN1_SEQUENCE_END(RecipientInfo);
161 ASN1_INTEGER *version;
162 STACK_OF(SignerInfo) *signerInfo;
166 ASN1_INTEGER *version;
167 ASN1_OBJECT *contentEncryptionAlgorithm;
168 ASN1_OCTET_STRING *iv;
169 STACK_OF(RecipientInfo) *recipientInfo;
172 ASN1_SEQUENCE(SignatureData) = {
173 ASN1_SIMPLE(SignatureData, version, ASN1_INTEGER),
174 ASN1_SET_OF(SignatureData, signerInfo, SignerInfo),
175 } ASN1_SEQUENCE_END(SignatureData);
177 ASN1_SEQUENCE(CryptoData) = {
178 ASN1_SIMPLE(CryptoData, version, ASN1_INTEGER),
179 ASN1_SIMPLE(CryptoData, contentEncryptionAlgorithm, ASN1_OBJECT),
180 ASN1_SIMPLE(CryptoData, iv, ASN1_OCTET_STRING),
181 ASN1_SET_OF(CryptoData, recipientInfo, RecipientInfo)
182 } ASN1_SEQUENCE_END(CryptoData);
184 IMPLEMENT_ASN1_FUNCTIONS(SignerInfo)
185 IMPLEMENT_ASN1_FUNCTIONS(RecipientInfo)
186 IMPLEMENT_ASN1_FUNCTIONS(SignatureData)
187 IMPLEMENT_ASN1_FUNCTIONS(CryptoData)
188 IMPLEMENT_STACK_OF(SignerInfo)
189 IMPLEMENT_STACK_OF(RecipientInfo)
192 * SignerInfo and RecipientInfo stack macros, generated by OpenSSL's util/mkstack.pl.
194 #define sk_SignerInfo_new(st) SKM_sk_new(SignerInfo, (st))
195 #define sk_SignerInfo_new_null() SKM_sk_new_null(SignerInfo)
196 #define sk_SignerInfo_free(st) SKM_sk_free(SignerInfo, (st))
197 #define sk_SignerInfo_num(st) SKM_sk_num(SignerInfo, (st))
198 #define sk_SignerInfo_value(st, i) SKM_sk_value(SignerInfo, (st), (i))
199 #define sk_SignerInfo_set(st, i, val) SKM_sk_set(SignerInfo, (st), (i), (val))
200 #define sk_SignerInfo_zero(st) SKM_sk_zero(SignerInfo, (st))
201 #define sk_SignerInfo_push(st, val) SKM_sk_push(SignerInfo, (st), (val))
202 #define sk_SignerInfo_unshift(st, val) SKM_sk_unshift(SignerInfo, (st), (val))
203 #define sk_SignerInfo_find(st, val) SKM_sk_find(SignerInfo, (st), (val))
204 #define sk_SignerInfo_delete(st, i) SKM_sk_delete(SignerInfo, (st), (i))
205 #define sk_SignerInfo_delete_ptr(st, ptr) SKM_sk_delete_ptr(SignerInfo, (st), (ptr))
206 #define sk_SignerInfo_insert(st, val, i) SKM_sk_insert(SignerInfo, (st), (val), (i))
207 #define sk_SignerInfo_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(SignerInfo, (st), (cmp))
208 #define sk_SignerInfo_dup(st) SKM_sk_dup(SignerInfo, st)
209 #define sk_SignerInfo_pop_free(st, free_func) SKM_sk_pop_free(SignerInfo, (st), (free_func))
210 #define sk_SignerInfo_shift(st) SKM_sk_shift(SignerInfo, (st))
211 #define sk_SignerInfo_pop(st) SKM_sk_pop(SignerInfo, (st))
212 #define sk_SignerInfo_sort(st) SKM_sk_sort(SignerInfo, (st))
213 #define sk_SignerInfo_is_sorted(st) SKM_sk_is_sorted(SignerInfo, (st))
215 #define d2i_ASN1_SET_OF_SignerInfo(st, pp, length, d2i_func, free_func, ex_tag, ex_class) \
216 SKM_ASN1_SET_OF_d2i(SignerInfo, (st), (pp), (length), (d2i_func), (free_func), (ex_tag), (ex_class))
217 #define i2d_ASN1_SET_OF_SignerInfo(st, pp, i2d_func, ex_tag, ex_class, is_set) \
218 SKM_ASN1_SET_OF_i2d(SignerInfo, (st), (pp), (i2d_func), (ex_tag), (ex_class), (is_set))
219 #define ASN1_seq_pack_SignerInfo(st, i2d_func, buf, len) \
220 SKM_ASN1_seq_pack(SignerInfo, (st), (i2d_func), (buf), (len))
221 #define ASN1_seq_unpack_SignerInfo(buf, len, d2i_func, free_func) \
222 SKM_ASN1_seq_unpack(SignerInfo, (buf), (len), (d2i_func), (free_func))
224 #define sk_RecipientInfo_new(st) SKM_sk_new(RecipientInfo, (st))
225 #define sk_RecipientInfo_new_null() SKM_sk_new_null(RecipientInfo)
226 #define sk_RecipientInfo_free(st) SKM_sk_free(RecipientInfo, (st))
227 #define sk_RecipientInfo_num(st) SKM_sk_num(RecipientInfo, (st))
228 #define sk_RecipientInfo_value(st, i) SKM_sk_value(RecipientInfo, (st), (i))
229 #define sk_RecipientInfo_set(st, i, val) SKM_sk_set(RecipientInfo, (st), (i), (val))
230 #define sk_RecipientInfo_zero(st) SKM_sk_zero(RecipientInfo, (st))
231 #define sk_RecipientInfo_push(st, val) SKM_sk_push(RecipientInfo, (st), (val))
232 #define sk_RecipientInfo_unshift(st, val) SKM_sk_unshift(RecipientInfo, (st), (val))
233 #define sk_RecipientInfo_find(st, val) SKM_sk_find(RecipientInfo, (st), (val))
234 #define sk_RecipientInfo_delete(st, i) SKM_sk_delete(RecipientInfo, (st), (i))
235 #define sk_RecipientInfo_delete_ptr(st, ptr) SKM_sk_delete_ptr(RecipientInfo, (st), (ptr))
236 #define sk_RecipientInfo_insert(st, val, i) SKM_sk_insert(RecipientInfo, (st), (val), (i))
237 #define sk_RecipientInfo_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(RecipientInfo, (st), (cmp))
238 #define sk_RecipientInfo_dup(st) SKM_sk_dup(RecipientInfo, st)
239 #define sk_RecipientInfo_pop_free(st, free_func) SKM_sk_pop_free(RecipientInfo, (st), (free_func))
240 #define sk_RecipientInfo_shift(st) SKM_sk_shift(RecipientInfo, (st))
241 #define sk_RecipientInfo_pop(st) SKM_sk_pop(RecipientInfo, (st))
242 #define sk_RecipientInfo_sort(st) SKM_sk_sort(RecipientInfo, (st))
243 #define sk_RecipientInfo_is_sorted(st) SKM_sk_is_sorted(RecipientInfo, (st))
245 #define d2i_ASN1_SET_OF_RecipientInfo(st, pp, length, d2i_func, free_func, ex_tag, ex_class) \
246 SKM_ASN1_SET_OF_d2i(RecipientInfo, (st), (pp), (length), (d2i_func), (free_func), (ex_tag), (ex_class))
247 #define i2d_ASN1_SET_OF_RecipientInfo(st, pp, i2d_func, ex_tag, ex_class, is_set) \
248 SKM_ASN1_SET_OF_i2d(RecipientInfo, (st), (pp), (i2d_func), (ex_tag), (ex_class), (is_set))
249 #define ASN1_seq_pack_RecipientInfo(st, i2d_func, buf, len) \
250 SKM_ASN1_seq_pack(RecipientInfo, (st), (i2d_func), (buf), (len))
251 #define ASN1_seq_unpack_RecipientInfo(buf, len, d2i_func, free_func) \
252 SKM_ASN1_seq_unpack(RecipientInfo, (buf), (len), (d2i_func), (free_func))
253 /* End of util/mkstack.pl block */
255 /* X509 Public/Private Key Pair Structure */
256 struct X509_Keypair {
257 ASN1_OCTET_STRING *keyid;
262 /* Message Digest Structure */
264 crypto_digest_t type;
268 /* Message Signature Structure */
270 SignatureData *sigData;
273 /* Encryption Session Data */
274 struct Crypto_Session {
275 CryptoData *cryptoData; /* ASN.1 Structure */
276 unsigned char *session_key; /* Private symmetric session key */
277 size_t session_key_len; /* Symmetric session key length */
280 /* Symmetric Cipher Context */
281 struct Cipher_Context {
285 /* PEM Password Dispatch Context */
286 typedef struct PEM_CB_Context {
287 CRYPTO_PEM_PASSWD_CB *pem_callback;
288 const void *pem_userdata;
292 * Extract subjectKeyIdentifier from x509 certificate.
293 * Returns: On success, an ASN1_OCTET_STRING that must be freed via M_ASN1_OCTET_STRING_free().
296 static ASN1_OCTET_STRING *openssl_cert_keyid(X509 *cert){
298 X509V3_EXT_METHOD *method;
299 ASN1_OCTET_STRING *keyid;
301 #if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
302 const unsigned char *ext_value_data;
304 unsigned char *ext_value_data;
308 /* Find the index to the subjectKeyIdentifier extension */
309 i = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1);
315 /* Grab the extension */
316 ext = X509_get_ext(cert, i);
318 /* Get x509 extension method structure */
319 if (!(method = X509V3_EXT_get(ext))) {
323 ext_value_data = ext->value->data;
325 #if (OPENSSL_VERSION_NUMBER > 0x00907000L)
329 /* Decode ASN1 item in data */
330 keyid = (ASN1_OCTET_STRING *) ASN1_item_d2i(NULL, &ext_value_data, ext->value->length,
331 ASN1_ITEM_ptr(method->it));
335 /* Decode ASN1 item in data */
336 keyid = (ASN1_OCTET_STRING *) method->d2i(NULL, &ext_value_data, ext->value->length);
340 keyid = (ASN1_OCTET_STRING *) method->d2i(NULL, &ext_value_data, ext->value->length);
347 * Create a new keypair object.
348 * Returns: A pointer to a X509 KEYPAIR object on success.
351 X509_KEYPAIR *crypto_keypair_new (void) {
352 X509_KEYPAIR *keypair;
354 /* Allocate our keypair structure */
355 keypair = (X509_KEYPAIR *) malloc(sizeof(X509_KEYPAIR));
360 /* Initialize our keypair structure */
361 keypair->keyid = NULL;
362 keypair->pubkey = NULL;
363 keypair->privkey = NULL;
369 * Create a copy of a keypair object. The underlying
370 * EVP objects are not duplicated, as no EVP_PKEY_dup()
371 * API is available. Instead, the reference count is
374 X509_KEYPAIR *crypto_keypair_dup (X509_KEYPAIR *keypair)
376 X509_KEYPAIR *newpair;
378 newpair = crypto_keypair_new();
381 /* Allocation failed */
385 /* Increment the public key ref count */
386 if (keypair->pubkey) {
387 CRYPTO_add(&(keypair->pubkey->references), 1, CRYPTO_LOCK_EVP_PKEY);
388 newpair->pubkey = keypair->pubkey;
391 /* Increment the private key ref count */
392 if (keypair->privkey) {
393 CRYPTO_add(&(keypair->privkey->references), 1, CRYPTO_LOCK_EVP_PKEY);
394 newpair->privkey = keypair->privkey;
397 /* Duplicate the keyid */
398 if (keypair->keyid) {
399 newpair->keyid = M_ASN1_OCTET_STRING_dup(keypair->keyid);
400 if (!newpair->keyid) {
401 /* Allocation failed */
402 crypto_keypair_free(newpair);
412 * Load a public key from a PEM-encoded x509 certificate.
413 * Returns: true on success
416 int crypto_keypair_load_cert (X509_KEYPAIR *keypair, const char *file)
422 if (!(bio = BIO_new_file(file, "r"))) {
423 openssl_post_errors(M_ERROR, _("Unable to open certificate file"));
427 cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
430 openssl_post_errors(M_ERROR, _("Unable to read certificate from file"));
434 /* Extract the public key */
435 if (!(keypair->pubkey = X509_get_pubkey(cert))) {
436 openssl_post_errors(M_ERROR, _("Unable to extract public key from certificate"));
440 /* Extract the subjectKeyIdentifier extension field */
441 if ((keypair->keyid = openssl_cert_keyid(cert)) == NULL) {
442 Emsg0(M_ERROR, 0, _("Provided certificate does not include the required subjectKeyIdentifier extension."));
446 /* Validate the public key type (only RSA is supported) */
447 if (EVP_PKEY_type(keypair->pubkey->type) != EVP_PKEY_RSA) {
448 Emsg1(M_ERROR, 0, _("Unsupported key type provided: %d\n"), EVP_PKEY_type(keypair->pubkey->type));
457 if (keypair->pubkey) {
458 EVP_PKEY_free(keypair->pubkey);
463 /* Dispatch user PEM encryption callbacks */
464 static int crypto_pem_callback_dispatch (char *buf, int size, int rwflag, void *userdata)
466 PEM_CB_CONTEXT *ctx = (PEM_CB_CONTEXT *) userdata;
467 return (ctx->pem_callback(buf, size, ctx->pem_userdata));
471 * Check a PEM-encoded file
472 * for the existence of a private key.
473 * Returns: true if a private key is found
476 bool crypto_keypair_has_key (const char *file) {
480 unsigned char *data = NULL;
484 if (!(bio = BIO_new_file(file, "r"))) {
485 openssl_post_errors(M_ERROR, _("Unable to open private key file"));
489 while (PEM_read_bio(bio, &name, &header, &data, &len)) {
490 /* We don't care what the data is, just that it's there */
491 OPENSSL_free(header);
495 * PEM Header Found, check for a private key
496 * Due to OpenSSL limitations, we must specifically
497 * list supported PEM private key encodings.
499 if (strcmp(name, PEM_STRING_RSA) == 0
500 || strcmp(name, PEM_STRING_DSA) == 0
501 || strcmp(name, PEM_STRING_PKCS8) == 0
502 || strcmp(name, PEM_STRING_PKCS8INF) == 0) {
514 /* Post PEM-decoding error messages, if any */
515 openssl_post_errors(M_ERROR, _("Unable to read private key from file"));
520 * Load a PEM-encoded private key.
521 * Returns: true on success
524 int crypto_keypair_load_key (X509_KEYPAIR *keypair, const char *file,
525 CRYPTO_PEM_PASSWD_CB *pem_callback,
526 const void *pem_userdata)
532 if (!(bio = BIO_new_file(file, "r"))) {
533 openssl_post_errors(M_ERROR, _("Unable to open private key file"));
537 /* Set up PEM encryption callback */
539 ctx.pem_callback = pem_callback;
540 ctx.pem_userdata = pem_userdata;
542 ctx.pem_callback = crypto_default_pem_callback;
543 ctx.pem_userdata = NULL;
546 keypair->privkey = PEM_read_bio_PrivateKey(bio, NULL, crypto_pem_callback_dispatch, &ctx);
548 if (!keypair->privkey) {
549 openssl_post_errors(M_ERROR, _("Unable to read private key from file"));
557 * Free memory associated with a keypair object.
559 void crypto_keypair_free (X509_KEYPAIR *keypair)
561 if (keypair->pubkey) {
562 EVP_PKEY_free(keypair->pubkey);
564 if (keypair->privkey) {
565 EVP_PKEY_free(keypair->privkey);
567 if (keypair->keyid) {
568 M_ASN1_OCTET_STRING_free(keypair->keyid);
574 * Create a new message digest context of the specified type
575 * Returns: A pointer to a DIGEST object on success.
578 DIGEST *crypto_digest_new (crypto_digest_t type)
581 const EVP_MD *md = NULL; /* Quell invalid uninitialized warnings */
583 digest = (DIGEST *) malloc(sizeof(DIGEST));
586 /* Initialize the OpenSSL message digest context */
587 EVP_MD_CTX_init(&digest->ctx);
589 /* Determine the correct OpenSSL message digest type */
591 case CRYPTO_DIGEST_MD5:
594 case CRYPTO_DIGEST_SHA1:
598 case CRYPTO_DIGEST_SHA256:
601 case CRYPTO_DIGEST_SHA512:
606 Emsg1(M_ERROR, 0, _("Unsupported digest type: %d\n"), type);
610 /* Initialize the backing OpenSSL context */
611 if (EVP_DigestInit_ex(&digest->ctx, md, NULL) == 0) {
618 /* This should not happen, but never say never ... */
619 openssl_post_errors(M_ERROR, _("OpenSSL digest initialization failed"));
620 crypto_digest_free(digest);
625 * Hash length bytes of data into the provided digest context.
626 * Returns: true on success
629 bool crypto_digest_update (DIGEST *digest, const void *data, size_t length) {
630 if (EVP_DigestUpdate(&digest->ctx, data, length) == 0) {
638 * Finalize the data in digest, storing the result in dest and the result size
639 * in length. The result size can be determined with crypto_digest_size().
641 * Returns: true on success
644 bool crypto_digest_finalize (DIGEST *digest, void *dest, size_t *length) {
645 if (!EVP_DigestFinal(&digest->ctx, (unsigned char *) dest, (unsigned int *) length)) {
653 * Free memory associated with a digest object.
655 void crypto_digest_free (DIGEST *digest)
657 EVP_MD_CTX_cleanup(&digest->ctx);
662 * Create a new message signature context.
663 * Returns: A pointer to a SIGNATURE object on success.
666 SIGNATURE *crypto_sign_new (void)
670 sig = (SIGNATURE *) malloc(sizeof(SIGNATURE));
675 sig->sigData = SignatureData_new();
678 /* Allocation failed in OpenSSL */
683 /* Set the ASN.1 structure version number */
684 ASN1_INTEGER_set(sig->sigData->version, BACULA_ASN1_VERSION);
690 * For a given public key, find the associated SignatureInfo record
691 * and create a digest context for signature validation
692 * Returns: CRYPTO_ERROR_NONE on success, with the newly allocated DIGEST in digest.
693 * A crypto_error_t value on failure.
695 crypto_error_t crypto_sign_get_digest(SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST **digest)
697 STACK_OF(SignerInfo) *signers;
701 signers = sig->sigData->signerInfo;
703 for (i = 0; i < sk_SignerInfo_num(signers); i++) {
704 si = sk_SignerInfo_value(signers, i);
705 if (M_ASN1_OCTET_STRING_cmp(keypair->keyid, si->subjectKeyIdentifier) == 0) {
706 /* Get the digest algorithm and allocate a digest context */
707 switch (OBJ_obj2nid(si->digestAlgorithm)) {
709 *digest = crypto_digest_new(CRYPTO_DIGEST_MD5);
712 *digest = crypto_digest_new(CRYPTO_DIGEST_SHA1);
716 *digest = crypto_digest_new(CRYPTO_DIGEST_SHA256);
719 *digest = crypto_digest_new(CRYPTO_DIGEST_SHA512);
724 return CRYPTO_ERROR_INVALID_DIGEST;
727 /* Shouldn't happen */
728 if (*digest == NULL) {
729 return CRYPTO_ERROR_INVALID_DIGEST;
731 return CRYPTO_ERROR_NONE;
736 return CRYPTO_ERROR_NOSIGNER;
740 * For a given signature, public key, and digest, verify the SIGNATURE.
741 * Returns: CRYPTO_ERROR_NONE on success.
742 * A crypto_error_t value on failure.
744 crypto_error_t crypto_sign_verify(SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST *digest)
746 STACK_OF(SignerInfo) *signers;
750 #if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
751 const unsigned char *sigData;
753 unsigned char *sigData;
756 signers = sig->sigData->signerInfo;
758 /* Find the signer */
759 for (i = 0; i < sk_SignerInfo_num(signers); i++) {
760 si = sk_SignerInfo_value(signers, i);
761 if (M_ASN1_OCTET_STRING_cmp(keypair->keyid, si->subjectKeyIdentifier) == 0) {
762 /* Extract the signature data */
763 sigLen = M_ASN1_STRING_length(si->signature);
764 sigData = M_ASN1_STRING_data(si->signature);
766 ok = EVP_VerifyFinal(&digest->ctx, sigData, sigLen, keypair->pubkey);
768 return CRYPTO_ERROR_NONE;
769 } else if (ok == 0) {
770 return CRYPTO_ERROR_BAD_SIGNATURE;
772 /* Shouldn't happen */
773 openssl_post_errors(M_ERROR, _("OpenSSL error occured"));
774 return CRYPTO_ERROR_INTERNAL;
779 /* Signer wasn't found. */
780 return CRYPTO_ERROR_NOSIGNER;
786 * Returns: true on success
789 int crypto_sign_add_signer(SIGNATURE *sig, DIGEST *digest, X509_KEYPAIR *keypair)
791 SignerInfo *si = NULL;
792 unsigned char *buf = NULL;
795 si = SignerInfo_new();
798 /* Allocation failed in OpenSSL */
802 /* Set the ASN.1 structure version number */
803 ASN1_INTEGER_set(si->version, BACULA_ASN1_VERSION);
805 /* Set the digest algorithm identifier */
806 switch (digest->type) {
807 case CRYPTO_DIGEST_MD5:
808 si->digestAlgorithm = OBJ_nid2obj(NID_md5);
810 case CRYPTO_DIGEST_SHA1:
811 si->digestAlgorithm = OBJ_nid2obj(NID_sha1);
814 case CRYPTO_DIGEST_SHA256:
815 si->digestAlgorithm = OBJ_nid2obj(NID_sha256);
817 case CRYPTO_DIGEST_SHA512:
818 si->digestAlgorithm = OBJ_nid2obj(NID_sha512);
822 /* This should never happen */
826 /* Drop the string allocated by OpenSSL, and add our subjectKeyIdentifier */
827 M_ASN1_OCTET_STRING_free(si->subjectKeyIdentifier);
828 si->subjectKeyIdentifier = M_ASN1_OCTET_STRING_dup(keypair->keyid);
830 /* Set our signature algorithm. We currently require RSA */
831 assert(EVP_PKEY_type(keypair->pubkey->type) == EVP_PKEY_RSA);
832 /* This is slightly evil. Reach into the MD structure and grab the key type */
833 si->signatureAlgorithm = OBJ_nid2obj(digest->ctx.digest->pkey_type);
835 /* Finalize/Sign our Digest */
836 len = EVP_PKEY_size(keypair->privkey);
837 buf = (unsigned char *) malloc(len);
838 if (!EVP_SignFinal(&digest->ctx, buf, &len, keypair->privkey)) {
839 openssl_post_errors(M_ERROR, _("Signature creation failed"));
843 /* Add the signature to the SignerInfo structure */
844 if (!M_ASN1_OCTET_STRING_set(si->signature, buf, len)) {
845 /* Allocation failed in OpenSSL */
849 /* No longer needed */
852 /* Push the new SignerInfo structure onto the stack */
853 sk_SignerInfo_push(sig->sigData->signerInfo, si);
869 * Encodes the SignatureData structure. The length argument is used to specify the
870 * size of dest. A length of 0 will cause no data to be written to dest, and the
871 * required length to be written to length. The caller can then allocate sufficient
872 * space for the output.
874 * Returns: true on success, stores the encoded data in dest, and the size in length.
877 int crypto_sign_encode(SIGNATURE *sig, void *dest, size_t *length)
880 *length = i2d_SignatureData(sig->sigData, NULL);
884 *length = i2d_SignatureData(sig->sigData, (unsigned char **) &dest);
889 * Decodes the SignatureData structure. The length argument is used to specify the
892 * Returns: SIGNATURE instance on success.
897 SIGNATURE *crypto_sign_decode(const void *sigData, size_t length)
900 #if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
901 const unsigned char *p = (const unsigned char *) sigData;
903 unsigned char *p = (unsigned char *) sigData;
906 sig = (SIGNATURE *) malloc(sizeof(SIGNATURE));
911 /* d2i_SignatureData modifies the supplied pointer */
912 sig->sigData = d2i_SignatureData(NULL, &p, length);
915 /* Allocation / Decoding failed in OpenSSL */
916 openssl_post_errors(M_ERROR, _("Signature decoding failed"));
925 * Free memory associated with a signature object.
927 void crypto_sign_free(SIGNATURE *sig)
929 SignatureData_free(sig->sigData);
934 * Create a new encryption session.
935 * Returns: A pointer to a CRYPTO_SESSION object on success.
938 CRYPTO_SESSION *crypto_session_new (crypto_cipher_t cipher, alist *pubkeys)
941 X509_KEYPAIR *keypair;
942 const EVP_CIPHER *ec;
946 /* Allocate our session description structures */
947 cs = (CRYPTO_SESSION *) malloc(sizeof(CRYPTO_SESSION));
952 /* Initialize required fields */
953 cs->session_key = NULL;
955 /* Allocate a CryptoData structure */
956 cs->cryptoData = CryptoData_new();
958 if (!cs->cryptoData) {
959 /* Allocation failed in OpenSSL */
964 /* Set the ASN.1 structure version number */
965 ASN1_INTEGER_set(cs->cryptoData->version, BACULA_ASN1_VERSION);
968 * Acquire a cipher instance and set the ASN.1 cipher NID
971 case CRYPTO_CIPHER_AES_128_CBC:
972 /* AES 128 bit CBC */
973 cs->cryptoData->contentEncryptionAlgorithm = OBJ_nid2obj(NID_aes_128_cbc);
974 ec = EVP_aes_128_cbc();
976 case CRYPTO_CIPHER_AES_192_CBC:
977 /* AES 192 bit CBC */
978 cs->cryptoData->contentEncryptionAlgorithm = OBJ_nid2obj(NID_aes_192_cbc);
979 ec = EVP_aes_192_cbc();
981 case CRYPTO_CIPHER_AES_256_CBC:
982 /* AES 256 bit CBC */
983 cs->cryptoData->contentEncryptionAlgorithm = OBJ_nid2obj(NID_aes_256_cbc);
984 ec = EVP_aes_256_cbc();
986 case CRYPTO_CIPHER_BLOWFISH_CBC:
988 cs->cryptoData->contentEncryptionAlgorithm = OBJ_nid2obj(NID_bf_cbc);
992 Emsg0(M_ERROR, 0, _("Unsupported cipher type specified\n"));
993 crypto_session_free(cs);
997 /* Generate a symmetric session key */
998 cs->session_key_len = EVP_CIPHER_key_length(ec);
999 cs->session_key = (unsigned char *) malloc(cs->session_key_len);
1000 if (RAND_bytes(cs->session_key, cs->session_key_len) <= 0) {
1001 /* OpenSSL failure */
1002 crypto_session_free(cs);
1006 /* Generate an IV if possible */
1007 if ((iv_len = EVP_CIPHER_iv_length(ec))) {
1008 iv = (unsigned char *) malloc(iv_len);
1010 /* Malloc failure */
1011 crypto_session_free(cs);
1015 /* Generate random IV */
1016 if (RAND_bytes(iv, iv_len) <= 0) {
1017 /* OpenSSL failure */
1018 crypto_session_free(cs);
1023 /* Store it in our ASN.1 structure */
1024 if (!M_ASN1_OCTET_STRING_set(cs->cryptoData->iv, iv, iv_len)) {
1025 /* Allocation failed in OpenSSL */
1026 crypto_session_free(cs);
1034 * Create RecipientInfo structures for supplied
1037 foreach_alist(keypair, pubkeys) {
1039 unsigned char *ekey;
1042 ri = RecipientInfo_new();
1044 /* Allocation failed in OpenSSL */
1045 crypto_session_free(cs);
1049 /* Set the ASN.1 structure version number */
1050 ASN1_INTEGER_set(ri->version, BACULA_ASN1_VERSION);
1052 /* Drop the string allocated by OpenSSL, and add our subjectKeyIdentifier */
1053 M_ASN1_OCTET_STRING_free(ri->subjectKeyIdentifier);
1054 ri->subjectKeyIdentifier = M_ASN1_OCTET_STRING_dup(keypair->keyid);
1056 /* Set our key encryption algorithm. We currently require RSA */
1057 assert(keypair->pubkey && EVP_PKEY_type(keypair->pubkey->type) == EVP_PKEY_RSA);
1058 ri->keyEncryptionAlgorithm = OBJ_nid2obj(NID_rsaEncryption);
1060 /* Encrypt the session key */
1061 ekey = (unsigned char *) malloc(EVP_PKEY_size(keypair->pubkey));
1063 RecipientInfo_free(ri);
1064 crypto_session_free(cs);
1068 if ((ekey_len = EVP_PKEY_encrypt(ekey, cs->session_key, cs->session_key_len, keypair->pubkey)) <= 0) {
1069 /* OpenSSL failure */
1070 RecipientInfo_free(ri);
1071 crypto_session_free(cs);
1076 /* Store it in our ASN.1 structure */
1077 if (!M_ASN1_OCTET_STRING_set(ri->encryptedKey, ekey, ekey_len)) {
1078 /* Allocation failed in OpenSSL */
1079 RecipientInfo_free(ri);
1080 crypto_session_free(cs);
1085 /* Free the encrypted key buffer */
1088 /* Push the new RecipientInfo structure onto the stack */
1089 sk_RecipientInfo_push(cs->cryptoData->recipientInfo, ri);
1096 * Encodes the CryptoData structure. The length argument is used to specify the
1097 * size of dest. A length of 0 will cause no data to be written to dest, and the
1098 * required length to be written to length. The caller can then allocate sufficient
1099 * space for the output.
1101 * Returns: true on success, stores the encoded data in dest, and the size in length.
1104 bool crypto_session_encode(CRYPTO_SESSION *cs, void *dest, size_t *length)
1107 *length = i2d_CryptoData(cs->cryptoData, NULL);
1111 *length = i2d_CryptoData(cs->cryptoData, (unsigned char **) &dest);
1116 * Decodes the CryptoData structure. The length argument is
1117 * used to specify the size of data.
1119 * Returns: CRYPTO_SESSION instance on success.
1121 * Returns: CRYPTO_ERROR_NONE and a pointer to a newly allocated CRYPTO_SESSION structure in *session on success.
1122 * A crypto_error_t value on failure.
1124 crypto_error_t crypto_session_decode(const void *data, size_t length, alist *keypairs, CRYPTO_SESSION **session)
1127 X509_KEYPAIR *keypair;
1128 STACK_OF(RecipientInfo) *recipients;
1129 crypto_error_t retval = CRYPTO_ERROR_NONE;
1130 #if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
1131 const unsigned char *p = (const unsigned char *) data;
1133 unsigned char *p = (unsigned char *) data;
1136 cs = (CRYPTO_SESSION *) malloc(sizeof(CRYPTO_SESSION));
1138 return CRYPTO_ERROR_INTERNAL;
1141 /* Initialize required fields */
1142 cs->session_key = NULL;
1144 /* d2i_CryptoData modifies the supplied pointer */
1145 cs->cryptoData = d2i_CryptoData(NULL, &p, length);
1147 if (!cs->cryptoData) {
1148 /* Allocation / Decoding failed in OpenSSL */
1149 openssl_post_errors(M_ERROR, _("CryptoData decoding failed"));
1150 retval = CRYPTO_ERROR_INTERNAL;
1154 recipients = cs->cryptoData->recipientInfo;
1157 * Find a matching RecipientInfo structure for a supplied
1160 foreach_alist(keypair, keypairs) {
1164 /* Private key available? */
1165 if (keypair->privkey == NULL) {
1169 for (i = 0; i < sk_RecipientInfo_num(recipients); i++) {
1170 ri = sk_RecipientInfo_value(recipients, i);
1172 /* Match against the subjectKeyIdentifier */
1173 if (M_ASN1_OCTET_STRING_cmp(keypair->keyid, ri->subjectKeyIdentifier) == 0) {
1174 /* Match found, extract symmetric encryption session data */
1176 /* RSA is required. */
1177 assert(EVP_PKEY_type(keypair->privkey->type) == EVP_PKEY_RSA);
1179 /* If we recieve a RecipientInfo structure that does not use
1180 * RSA, return an error */
1181 if (OBJ_obj2nid(ri->keyEncryptionAlgorithm) != NID_rsaEncryption) {
1182 retval = CRYPTO_ERROR_INVALID_CRYPTO;
1186 /* Decrypt the session key */
1187 /* Allocate sufficient space for the largest possible decrypted data */
1188 cs->session_key = (unsigned char *) malloc(EVP_PKEY_size(keypair->privkey));
1189 cs->session_key_len = EVP_PKEY_decrypt(cs->session_key, M_ASN1_STRING_data(ri->encryptedKey),
1190 M_ASN1_STRING_length(ri->encryptedKey), keypair->privkey);
1192 if (cs->session_key_len <= 0) {
1193 openssl_post_errors(M_ERROR, _("Failure decrypting the session key"));
1194 retval = CRYPTO_ERROR_DECRYPTION;
1198 /* Session key successfully extracted, return the CRYPTO_SESSION structure */
1200 return CRYPTO_ERROR_NONE;
1205 /* No matching recipient found */
1206 return CRYPTO_ERROR_NORECIPIENT;
1209 crypto_session_free(cs);
1214 * Free memory associated with a crypto session object.
1216 void crypto_session_free (CRYPTO_SESSION *cs)
1218 if (cs->cryptoData) {
1219 CryptoData_free(cs->cryptoData);
1221 if (cs->session_key){
1222 free(cs->session_key);
1228 * Create a new crypto cipher context with the specified session object
1229 * Returns: A pointer to a CIPHER_CONTEXT object on success. The cipher block size is returned in blocksize.
1232 CIPHER_CONTEXT *crypto_cipher_new (CRYPTO_SESSION *cs, bool encrypt, size_t *blocksize)
1234 CIPHER_CONTEXT *cipher_ctx;
1235 const EVP_CIPHER *ec;
1237 cipher_ctx = (CIPHER_CONTEXT *) malloc(sizeof(CIPHER_CONTEXT));
1243 * Acquire a cipher instance for the given ASN.1 cipher NID
1245 if ((ec = EVP_get_cipherbyobj(cs->cryptoData->contentEncryptionAlgorithm)) == NULL) {
1246 Emsg1(M_ERROR, 0, _("Unsupported contentEncryptionAlgorithm: %d\n"), OBJ_obj2nid(cs->cryptoData->contentEncryptionAlgorithm));
1251 /* Initialize the OpenSSL cipher context */
1252 EVP_CIPHER_CTX_init(&cipher_ctx->ctx);
1254 /* Initialize for encryption */
1255 if (!EVP_CipherInit_ex(&cipher_ctx->ctx, ec, NULL, NULL, NULL, 1)) {
1256 openssl_post_errors(M_ERROR, _("OpenSSL cipher context initialization failed"));
1260 /* Initialize for decryption */
1261 if (!EVP_CipherInit_ex(&cipher_ctx->ctx, ec, NULL, NULL, NULL, 0)) {
1262 openssl_post_errors(M_ERROR, _("OpenSSL cipher context initialization failed"));
1267 /* Set the key size */
1268 if (!EVP_CIPHER_CTX_set_key_length(&cipher_ctx->ctx, cs->session_key_len)) {
1269 openssl_post_errors(M_ERROR, _("Encryption session provided an invalid symmetric key"));
1273 /* Validate the IV length */
1274 if (EVP_CIPHER_iv_length(ec) != M_ASN1_STRING_length(cs->cryptoData->iv)) {
1275 openssl_post_errors(M_ERROR, _("Encryption session provided an invalid IV"));
1279 /* Add the key and IV to the cipher context */
1280 if (!EVP_CipherInit_ex(&cipher_ctx->ctx, NULL, NULL, cs->session_key, M_ASN1_STRING_data(cs->cryptoData->iv), -1)) {
1281 openssl_post_errors(M_ERROR, _("OpenSSL cipher context key/IV initialization failed"));
1285 *blocksize = EVP_CIPHER_CTX_block_size(&cipher_ctx->ctx);
1289 crypto_cipher_free(cipher_ctx);
1295 * Encrypt/Decrypt length bytes of data using the provided cipher context
1296 * Returns: true on success, number of bytes output in written
1299 bool crypto_cipher_update (CIPHER_CONTEXT *cipher_ctx, const void *data, size_t length, const void *dest, size_t *written) {
1300 if (!EVP_CipherUpdate(&cipher_ctx->ctx, (unsigned char *) dest, (int *) written, (const unsigned char *) data, length)) {
1301 /* This really shouldn't fail */
1309 * Finalize the cipher context, writing any remaining data and necessary padding
1310 * to dest, and the size in written.
1311 * The result size will either be one block of data or zero.
1313 * Returns: true on success
1316 bool crypto_cipher_finalize (CIPHER_CONTEXT *cipher_ctx, void *dest, size_t *written) {
1317 if (!EVP_CipherFinal_ex(&cipher_ctx->ctx, (unsigned char *) dest, (int *) written)) {
1318 /* This really shouldn't fail */
1327 * Free memory associated with a cipher context.
1329 void crypto_cipher_free (CIPHER_CONTEXT *cipher_ctx)
1331 EVP_CIPHER_CTX_cleanup(&cipher_ctx->ctx);
1337 * Perform global initialization of OpenSSL
1338 * This function is not thread safe.
1339 * Returns: 0 on success
1342 int init_crypto (void)
1346 if ((stat = openssl_init_threads()) != 0) {
1347 Emsg1(M_ABORT, 0, _("Unable to init OpenSSL threading: ERR=%s\n"), strerror(stat));
1350 /* Load libssl and libcrypto human-readable error strings */
1351 SSL_load_error_strings();
1353 /* Initialize OpenSSL SSL library */
1356 /* Register OpenSSL ciphers and digests */
1357 OpenSSL_add_all_algorithms();
1359 if (!openssl_seed_prng()) {
1360 Emsg0(M_ERROR_TERM, 0, _("Failed to seed OpenSSL PRNG\n"));
1363 crypto_initialized = true;
1369 * Perform global cleanup of OpenSSL
1370 * All cryptographic operations must be completed before calling this function.
1371 * This function is not thread safe.
1372 * Returns: 0 on success
1375 int cleanup_crypto (void)
1378 * Ensure that we've actually been initialized; Doing this here decreases the
1379 * complexity of client's termination/cleanup code.
1381 if (!crypto_initialized) {
1385 if (!openssl_save_prng()) {
1386 Emsg0(M_ERROR, 0, _("Failed to save OpenSSL PRNG\n"));
1389 openssl_cleanup_threads();
1391 /* Free libssl and libcrypto error strings */
1394 /* Free all ciphers and digests */
1397 /* Free memory used by PRNG */
1400 crypto_initialized = false;
1406 #else /* HAVE_OPENSSL */
1407 # error No encryption library available
1408 #endif /* HAVE_OPENSSL */
1410 #else /* HAVE_CRYPTO */
1413 * Cryptography Support Disabled
1416 /* Message Digest Structure */
1418 crypto_digest_t type;
1425 /* Dummy Signature Structure */
1429 DIGEST *crypto_digest_new (crypto_digest_t type)
1433 digest = (DIGEST *) malloc(sizeof(DIGEST));
1434 digest->type = type;
1437 case CRYPTO_DIGEST_MD5:
1438 MD5Init(&digest->md5);
1440 case CRYPTO_DIGEST_SHA1:
1441 SHA1Init(&digest->sha1);
1444 Emsg0(M_ERROR, 0, _("Unsupported digest type specified\n"));
1452 bool crypto_digest_update (DIGEST *digest, const void *data, size_t length) {
1453 switch (digest->type) {
1454 case CRYPTO_DIGEST_MD5:
1455 /* Doesn't return anything ... */
1456 MD5Update(&digest->md5, (unsigned char *) data, length);
1458 case CRYPTO_DIGEST_SHA1:
1460 if ((ret = SHA1Update(&digest->sha1, (const u_int8_t *) data, length)) == shaSuccess) {
1463 Emsg1(M_ERROR, 0, _("SHA1Update() returned an error: %d\n"), ret);
1472 bool crypto_digest_finalize (DIGEST *digest, void *dest, size_t *length) {
1474 switch (digest->type) {
1475 case CRYPTO_DIGEST_MD5:
1476 /* Guard against programmer error by either the API client or
1477 * an out-of-sync CRYPTO_DIGEST_MAX_SIZE */
1478 assert(*length >= CRYPTO_DIGEST_MD5_SIZE);
1479 *length = CRYPTO_DIGEST_MD5_SIZE;
1480 /* Doesn't return anything ... */
1481 MD5Final((unsigned char *) dest, &digest->md5);
1483 case CRYPTO_DIGEST_SHA1:
1484 /* Guard against programmer error by either the API client or
1485 * an out-of-sync CRYPTO_DIGEST_MAX_SIZE */
1486 assert(*length >= CRYPTO_DIGEST_SHA1_SIZE);
1487 *length = CRYPTO_DIGEST_SHA1_SIZE;
1488 if (SHA1Final(&digest->sha1, (u_int8_t *) dest) == shaSuccess) {
1501 void crypto_digest_free (DIGEST *digest)
1506 /* Dummy routines */
1507 int init_crypto (void) { return 0; }
1508 int cleanup_crypto (void) { return 0; }
1510 SIGNATURE *crypto_sign_new (void) { return NULL; }
1512 crypto_error_t crypto_sign_get_digest (SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST **digest) { return CRYPTO_ERROR_INTERNAL; }
1513 crypto_error_t crypto_sign_verify (SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST *digest) { return CRYPTO_ERROR_INTERNAL; }
1515 int crypto_sign_add_signer (SIGNATURE *sig, DIGEST *digest, X509_KEYPAIR *keypair) { return false; }
1516 int crypto_sign_encode (SIGNATURE *sig, void *dest, size_t *length) { return false; }
1518 SIGNATURE *crypto_sign_decode (const void *sigData, size_t length) { return NULL; }
1519 void crypto_sign_free (SIGNATURE *sig) { }
1522 X509_KEYPAIR *crypto_keypair_new (void) { return NULL; }
1523 X509_KEYPAIR *crypto_keypair_dup (X509_KEYPAIR *keypair) { return NULL; }
1524 int crypto_keypair_load_cert (X509_KEYPAIR *keypair, const char *file) { return false; }
1525 bool crypto_keypair_has_key (const char *file) { return false; }
1526 int crypto_keypair_load_key (X509_KEYPAIR *keypair, const char *file, CRYPTO_PEM_PASSWD_CB *pem_callback, const void *pem_userdata) { return false; }
1527 void crypto_keypair_free (X509_KEYPAIR *keypair) { }
1529 CRYPTO_SESSION *crypto_session_new (crypto_cipher_t cipher, alist *pubkeys) { return NULL; }
1530 void crypto_session_free (CRYPTO_SESSION *cs) { }
1531 bool crypto_session_encode (CRYPTO_SESSION *cs, void *dest, size_t *length) { return false; }
1532 crypto_error_t crypto_session_decode (const void *data, size_t length, alist *keypairs, CRYPTO_SESSION **session) { return CRYPTO_ERROR_INTERNAL; }
1534 CIPHER_CONTEXT *crypto_cipher_new (CRYPTO_SESSION *cs, bool encrypt, size_t *blocksize) { return NULL; }
1535 bool crypto_cipher_update (CIPHER_CONTEXT *cipher_ctx, const void *data, size_t length, const void *dest, size_t *written) { return false; }
1536 bool crypto_cipher_finalize (CIPHER_CONTEXT *cipher_ctx, void *dest, size_t *written) { return false; }
1537 void crypto_cipher_free (CIPHER_CONTEXT *cipher_ctx) { }
1539 #endif /* HAVE_CRYPTO */
1544 * Default PEM encryption passphrase callback.
1545 * Returns an empty password.
1547 int crypto_default_pem_callback(char *buf, int size, const void *userdata)
1549 bstrncpy(buf, "", size);
1550 return (strlen(buf));
1554 * Returns the ASCII name of the digest type.
1555 * Returns: ASCII name of digest type.
1557 const char *crypto_digest_name (DIGEST *digest) {
1558 switch (digest->type) {
1559 case CRYPTO_DIGEST_MD5:
1561 case CRYPTO_DIGEST_SHA1:
1563 case CRYPTO_DIGEST_SHA256:
1565 case CRYPTO_DIGEST_SHA512:
1567 case CRYPTO_DIGEST_NONE:
1570 return "Invalid Digest Type";
1576 * Given a stream type, returns the associated
1577 * crypto_digest_t value.
1579 crypto_digest_t crypto_digest_stream_type (int stream) {
1581 case STREAM_MD5_DIGEST:
1582 return CRYPTO_DIGEST_MD5;
1583 case STREAM_SHA1_DIGEST:
1584 return CRYPTO_DIGEST_SHA1;
1585 case STREAM_SHA256_DIGEST:
1586 return CRYPTO_DIGEST_SHA256;
1587 case STREAM_SHA512_DIGEST:
1588 return CRYPTO_DIGEST_SHA512;
1590 return CRYPTO_DIGEST_NONE;
1595 * * Given a crypto_error_t value, return the associated
1598 const char *crypto_strerror(crypto_error_t error) {
1600 case CRYPTO_ERROR_NONE:
1602 case CRYPTO_ERROR_NOSIGNER:
1603 return "Signer not found";
1604 case CRYPTO_ERROR_NORECIPIENT:
1605 return "Recipient not found";
1606 case CRYPTO_ERROR_INVALID_DIGEST:
1607 return "Unsupported digest algorithm";
1608 case CRYPTO_ERROR_INVALID_CRYPTO:
1609 return "Unsupported encryption algorithm";
1610 case CRYPTO_ERROR_BAD_SIGNATURE:
1611 return "Signature is invalid";
1612 case CRYPTO_ERROR_DECRYPTION:
1613 return "Decryption error";
1614 case CRYPTO_ERROR_INTERNAL:
1615 /* This shouldn't happen */
1616 return "Internal error";
1618 return "Unknown error";