2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2016 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * crypto.c Encryption support functions
22 * Author: Landon Fuller <landonf@opendarwin.org>
24 * This file was contributed to the Bacula project by Landon Fuller.
26 * Landon Fuller has been granted a perpetual, worldwide, non-exclusive,
27 * no-charge, royalty-free, irrevocable copyright license to reproduce,
28 * prepare derivative works of, publicly display, publicly perform,
29 * sublicense, and distribute the original work contributed by Landon Fuller
30 * to the Bacula project in source or object form.
32 * If you wish to license these contributions under an alternate open source
33 * license please contact Landon Fuller <landonf@opendarwin.org>.
42 * For OpenSSL version 1.x, EVP_PKEY_encrypt no longer
43 * exists. It was not an official API.
46 #define EVP_PKEY_encrypt EVP_PKEY_encrypt_old
47 #define EVP_PKEY_decrypt EVP_PKEY_decrypt_old
54 * Prefix: iso.org.dod.internet.private.enterprise.threerings.external.bacula (1.3.6.1.4.1.22054.500.2)
55 * Organization: Bacula Project
56 * Contact Name: Kern Sibbald
57 * Contact E-mail: kern@sibbald.com
59 * Top Level Allocations - 500.2
60 * 1 - Published Allocations
61 * 1.1 - Bacula Encryption
63 * Bacula Encryption - 500.2.1.1
66 * 2 - ASN.1 Object Identifiers
72 * BaculaCrypto { iso(1) identified-organization(3) usdod(6)
73 * internet(1) private(4) enterprises(1) three-rings(22054)
74 * external(500) bacula(2) published(1) bacula-encryption(1)
75 * asn1-modules(1) bacula-crypto(1) }
77 * DEFINITIONS AUTOMATIC TAGS ::=
80 * SignatureData ::= SEQUENCE {
81 * version Version DEFAULT v0,
82 * signerInfo SignerInfo }
84 * CryptoData ::= SEQUENCE {
85 * version Version DEFAULT v0,
86 * contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier,
87 * iv InitializationVector,
88 * recipientInfo RecipientInfo
91 * SignerInfo ::= SET OF SignerInfo
92 * RecipientInfo ::= SET OF RecipientInfo
94 * Version ::= INTEGER { v0(0) }
96 * SignerInfo ::= SEQUENCE {
98 * subjectKeyIdentifier SubjectKeyIdentifier,
99 * digestAlgorithm DigestAlgorithmIdentifier,
100 * signatureAlgorithm SignatureAlgorithmIdentifier,
101 * signature SignatureValue }
103 * RecipientInfo ::= SEQUENCE {
105 * subjectKeyIdentifier SubjectKeyIdentifier
106 * keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier
107 * encryptedKey EncryptedKey
110 * SubjectKeyIdentifier ::= OCTET STRING
112 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
114 * SignatureAlgorithmIdentifier ::= AlgorithmIdentifier
116 * KeyEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
118 * ContentEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
120 * InitializationVector ::= OCTET STRING
122 * SignatureValue ::= OCTET STRING
124 * EncryptedKey ::= OCTET STRING
126 * AlgorithmIdentifier ::= OBJECT IDENTIFIER
131 #ifdef HAVE_CRYPTO /* Is encryption enabled? */
132 #ifdef HAVE_OPENSSL /* How about OpenSSL? */
134 #include "openssl-compat.h"
136 /* ASN.1 Declarations */
137 #define BACULA_ASN1_VERSION 0
140 ASN1_INTEGER *version;
141 ASN1_OCTET_STRING *subjectKeyIdentifier;
142 ASN1_OBJECT *digestAlgorithm;
143 ASN1_OBJECT *signatureAlgorithm;
144 ASN1_OCTET_STRING *signature;
148 ASN1_INTEGER *version;
149 ASN1_OCTET_STRING *subjectKeyIdentifier;
150 ASN1_OBJECT *keyEncryptionAlgorithm;
151 ASN1_OCTET_STRING *encryptedKey;
154 ASN1_SEQUENCE(SignerInfo) = {
155 ASN1_SIMPLE(SignerInfo, version, ASN1_INTEGER),
156 ASN1_SIMPLE(SignerInfo, subjectKeyIdentifier, ASN1_OCTET_STRING),
157 ASN1_SIMPLE(SignerInfo, digestAlgorithm, ASN1_OBJECT),
158 ASN1_SIMPLE(SignerInfo, signatureAlgorithm, ASN1_OBJECT),
159 ASN1_SIMPLE(SignerInfo, signature, ASN1_OCTET_STRING)
160 } ASN1_SEQUENCE_END(SignerInfo);
162 ASN1_SEQUENCE(RecipientInfo) = {
163 ASN1_SIMPLE(RecipientInfo, version, ASN1_INTEGER),
164 ASN1_SIMPLE(RecipientInfo, subjectKeyIdentifier, ASN1_OCTET_STRING),
165 ASN1_SIMPLE(RecipientInfo, keyEncryptionAlgorithm, ASN1_OBJECT),
166 ASN1_SIMPLE(RecipientInfo, encryptedKey, ASN1_OCTET_STRING),
167 } ASN1_SEQUENCE_END(RecipientInfo);
170 ASN1_INTEGER *version;
171 STACK_OF(SignerInfo) *signerInfo;
175 ASN1_INTEGER *version;
176 ASN1_OBJECT *contentEncryptionAlgorithm;
177 ASN1_OCTET_STRING *iv;
178 STACK_OF(RecipientInfo) *recipientInfo;
181 ASN1_SEQUENCE(SignatureData) = {
182 ASN1_SIMPLE(SignatureData, version, ASN1_INTEGER),
183 ASN1_SET_OF(SignatureData, signerInfo, SignerInfo),
184 } ASN1_SEQUENCE_END(SignatureData);
186 ASN1_SEQUENCE(CryptoData) = {
187 ASN1_SIMPLE(CryptoData, version, ASN1_INTEGER),
188 ASN1_SIMPLE(CryptoData, contentEncryptionAlgorithm, ASN1_OBJECT),
189 ASN1_SIMPLE(CryptoData, iv, ASN1_OCTET_STRING),
190 ASN1_SET_OF(CryptoData, recipientInfo, RecipientInfo)
191 } ASN1_SEQUENCE_END(CryptoData);
193 IMPLEMENT_ASN1_FUNCTIONS(SignerInfo)
194 IMPLEMENT_ASN1_FUNCTIONS(RecipientInfo)
195 IMPLEMENT_ASN1_FUNCTIONS(SignatureData)
196 IMPLEMENT_ASN1_FUNCTIONS(CryptoData)
198 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
199 DEFINE_STACK_OF(SignerInfo);
200 DEFINE_STACK_OF(RecipientInfo);
203 * SignerInfo and RecipientInfo stack macros, generated by OpenSSL's util/mkstack.pl.
205 #define sk_SignerInfo_new(st) SKM_sk_new(SignerInfo, (st))
206 #define sk_SignerInfo_new_null() SKM_sk_new_null(SignerInfo)
207 #define sk_SignerInfo_free(st) SKM_sk_free(SignerInfo, (st))
208 #define sk_SignerInfo_num(st) SKM_sk_num(SignerInfo, (st))
209 #define sk_SignerInfo_value(st, i) SKM_sk_value(SignerInfo, (st), (i))
210 #define sk_SignerInfo_set(st, i, val) SKM_sk_set(SignerInfo, (st), (i), (val))
211 #define sk_SignerInfo_zero(st) SKM_sk_zero(SignerInfo, (st))
212 #define sk_SignerInfo_push(st, val) SKM_sk_push(SignerInfo, (st), (val))
213 #define sk_SignerInfo_unshift(st, val) SKM_sk_unshift(SignerInfo, (st), (val))
214 #define sk_SignerInfo_find(st, val) SKM_sk_find(SignerInfo, (st), (val))
215 #define sk_SignerInfo_delete(st, i) SKM_sk_delete(SignerInfo, (st), (i))
216 #define sk_SignerInfo_delete_ptr(st, ptr) SKM_sk_delete_ptr(SignerInfo, (st), (ptr))
217 #define sk_SignerInfo_insert(st, val, i) SKM_sk_insert(SignerInfo, (st), (val), (i))
218 #define sk_SignerInfo_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(SignerInfo, (st), (cmp))
219 #define sk_SignerInfo_dup(st) SKM_sk_dup(SignerInfo, st)
220 #define sk_SignerInfo_pop_free(st, free_func) SKM_sk_pop_free(SignerInfo, (st), (free_func))
221 #define sk_SignerInfo_shift(st) SKM_sk_shift(SignerInfo, (st))
222 #define sk_SignerInfo_pop(st) SKM_sk_pop(SignerInfo, (st))
223 #define sk_SignerInfo_sort(st) SKM_sk_sort(SignerInfo, (st))
224 #define sk_SignerInfo_is_sorted(st) SKM_sk_is_sorted(SignerInfo, (st))
226 #define d2i_ASN1_SET_OF_SignerInfo(st, pp, length, d2i_func, free_func, ex_tag, ex_class) \
227 SKM_ASN1_SET_OF_d2i(SignerInfo, (st), (pp), (length), (d2i_func), (free_func), (ex_tag), (ex_class))
228 #define i2d_ASN1_SET_OF_SignerInfo(st, pp, i2d_func, ex_tag, ex_class, is_set) \
229 SKM_ASN1_SET_OF_i2d(SignerInfo, (st), (pp), (i2d_func), (ex_tag), (ex_class), (is_set))
230 #define ASN1_seq_pack_SignerInfo(st, i2d_func, buf, len) \
231 SKM_ASN1_seq_pack(SignerInfo, (st), (i2d_func), (buf), (len))
232 #define ASN1_seq_unpack_SignerInfo(buf, len, d2i_func, free_func) \
233 SKM_ASN1_seq_unpack(SignerInfo, (buf), (len), (d2i_func), (free_func))
235 #define sk_RecipientInfo_new(st) SKM_sk_new(RecipientInfo, (st))
236 #define sk_RecipientInfo_new_null() SKM_sk_new_null(RecipientInfo)
237 #define sk_RecipientInfo_free(st) SKM_sk_free(RecipientInfo, (st))
238 #define sk_RecipientInfo_num(st) SKM_sk_num(RecipientInfo, (st))
239 #define sk_RecipientInfo_value(st, i) SKM_sk_value(RecipientInfo, (st), (i))
240 #define sk_RecipientInfo_set(st, i, val) SKM_sk_set(RecipientInfo, (st), (i), (val))
241 #define sk_RecipientInfo_zero(st) SKM_sk_zero(RecipientInfo, (st))
242 #define sk_RecipientInfo_push(st, val) SKM_sk_push(RecipientInfo, (st), (val))
243 #define sk_RecipientInfo_unshift(st, val) SKM_sk_unshift(RecipientInfo, (st), (val))
244 #define sk_RecipientInfo_find(st, val) SKM_sk_find(RecipientInfo, (st), (val))
245 #define sk_RecipientInfo_delete(st, i) SKM_sk_delete(RecipientInfo, (st), (i))
246 #define sk_RecipientInfo_delete_ptr(st, ptr) SKM_sk_delete_ptr(RecipientInfo, (st), (ptr))
247 #define sk_RecipientInfo_insert(st, val, i) SKM_sk_insert(RecipientInfo, (st), (val), (i))
248 #define sk_RecipientInfo_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(RecipientInfo, (st), (cmp))
249 #define sk_RecipientInfo_dup(st) SKM_sk_dup(RecipientInfo, st)
250 #define sk_RecipientInfo_pop_free(st, free_func) SKM_sk_pop_free(RecipientInfo, (st), (free_func))
251 #define sk_RecipientInfo_shift(st) SKM_sk_shift(RecipientInfo, (st))
252 #define sk_RecipientInfo_pop(st) SKM_sk_pop(RecipientInfo, (st))
253 #define sk_RecipientInfo_sort(st) SKM_sk_sort(RecipientInfo, (st))
254 #define sk_RecipientInfo_is_sorted(st) SKM_sk_is_sorted(RecipientInfo, (st))
256 #define d2i_ASN1_SET_OF_RecipientInfo(st, pp, length, d2i_func, free_func, ex_tag, ex_class) \
257 SKM_ASN1_SET_OF_d2i(RecipientInfo, (st), (pp), (length), (d2i_func), (free_func), (ex_tag), (ex_class))
258 #define i2d_ASN1_SET_OF_RecipientInfo(st, pp, i2d_func, ex_tag, ex_class, is_set) \
259 SKM_ASN1_SET_OF_i2d(RecipientInfo, (st), (pp), (i2d_func), (ex_tag), (ex_class), (is_set))
260 #define ASN1_seq_pack_RecipientInfo(st, i2d_func, buf, len) \
261 SKM_ASN1_seq_pack(RecipientInfo, (st), (i2d_func), (buf), (len))
262 #define ASN1_seq_unpack_RecipientInfo(buf, len, d2i_func, free_func) \
263 SKM_ASN1_seq_unpack(RecipientInfo, (buf), (len), (d2i_func), (free_func))
264 /* End of util/mkstack.pl block */
267 /* X509 Public/Private Key Pair Structure */
268 struct X509_Keypair {
269 ASN1_OCTET_STRING *keyid;
274 /* Message Digest Structure */
276 crypto_digest_t type;
281 /* Message Signature Structure */
283 SignatureData *sigData;
287 /* Encryption Session Data */
288 struct Crypto_Session {
289 CryptoData *cryptoData; /* ASN.1 Structure */
290 unsigned char *session_key; /* Private symmetric session key */
291 size_t session_key_len; /* Symmetric session key length */
294 /* Symmetric Cipher Context */
295 struct Cipher_Context {
299 /* PEM Password Dispatch Context */
300 typedef struct PEM_CB_Context {
301 CRYPTO_PEM_PASSWD_CB *pem_callback;
302 const void *pem_userdata;
306 * Extract subjectKeyIdentifier from x509 certificate.
307 * Returns: On success, an ASN1_OCTET_STRING that must be freed via M_ASN1_OCTET_STRING_free().
310 static ASN1_OCTET_STRING *openssl_cert_keyid(X509 *cert) {
312 const X509V3_EXT_METHOD *method;
313 ASN1_OCTET_STRING *keyid;
315 const unsigned char *ext_value_data;
317 /* Find the index to the subjectKeyIdentifier extension */
318 i = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1);
324 /* Grab the extension */
325 ext = X509_get_ext(cert, i);
327 /* Get x509 extension method structure */
328 if (!(method = X509V3_EXT_get(ext))) {
332 ext_value_data = ext->value->data;
337 /* Decode ASN1 item in data */
338 keyid = (ASN1_OCTET_STRING *) ASN1_item_d2i(NULL, &ext_value_data, ext->value->length,
339 ASN1_ITEM_ptr(method->it));
343 /* Decode ASN1 item in data */
344 keyid = (ASN1_OCTET_STRING *) method->d2i(NULL, &ext_value_data, ext->value->length);
351 * Create a new keypair object.
352 * Returns: A pointer to a X509 KEYPAIR object on success.
355 X509_KEYPAIR *crypto_keypair_new(void)
357 X509_KEYPAIR *keypair;
359 /* Allocate our keypair structure */
360 keypair = (X509_KEYPAIR *)malloc(sizeof(X509_KEYPAIR));
362 /* Initialize our keypair structure */
363 keypair->keyid = NULL;
364 keypair->pubkey = NULL;
365 keypair->privkey = NULL;
371 * Create a copy of a keypair object. The underlying
372 * EVP objects are not duplicated, as no EVP_PKEY_dup()
373 * API is available. Instead, the reference count is
376 X509_KEYPAIR *crypto_keypair_dup(X509_KEYPAIR *keypair)
378 X509_KEYPAIR *newpair;
380 newpair = crypto_keypair_new();
383 /* Allocation failed */
387 /* Increment the public key ref count */
388 if (keypair->pubkey) {
389 CRYPTO_add(&(keypair->pubkey->references), 1, CRYPTO_LOCK_EVP_PKEY);
390 newpair->pubkey = keypair->pubkey;
393 /* Increment the private key ref count */
394 if (keypair->privkey) {
395 CRYPTO_add(&(keypair->privkey->references), 1, CRYPTO_LOCK_EVP_PKEY);
396 newpair->privkey = keypair->privkey;
399 /* Duplicate the keyid */
400 if (keypair->keyid) {
401 newpair->keyid = M_ASN1_OCTET_STRING_dup(keypair->keyid);
402 if (!newpair->keyid) {
403 /* Allocation failed */
404 crypto_keypair_free(newpair);
414 * Load a public key from a PEM-encoded x509 certificate.
415 * Returns: true on success
418 int crypto_keypair_load_cert(X509_KEYPAIR *keypair, const char *file)
424 if (!(bio = BIO_new_file(file, "r"))) {
425 openssl_post_errors(M_ERROR, _("Unable to open certificate file"));
429 cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
432 openssl_post_errors(M_ERROR, _("Unable to read certificate from file"));
436 /* Extract the public key */
437 if (!(keypair->pubkey = X509_get_pubkey(cert))) {
438 openssl_post_errors(M_ERROR, _("Unable to extract public key from certificate"));
442 /* Extract the subjectKeyIdentifier extension field */
443 if ((keypair->keyid = openssl_cert_keyid(cert)) == NULL) {
444 Jmsg0(NULL, M_ERROR, 0,
445 _("Provided certificate does not include the required subjectKeyIdentifier extension."));
449 /* Validate the public key type (only RSA is supported) */
450 if (EVP_PKEY_type(keypair->pubkey->type) != EVP_PKEY_RSA) {
451 Jmsg1(NULL, M_ERROR, 0,
452 _("Unsupported key type provided: %d\n"), EVP_PKEY_type(keypair->pubkey->type));
461 if (keypair->pubkey) {
462 EVP_PKEY_free(keypair->pubkey);
467 /* Dispatch user PEM encryption callbacks */
468 static int crypto_pem_callback_dispatch (char *buf, int size, int rwflag, void *userdata)
470 PEM_CB_CONTEXT *ctx = (PEM_CB_CONTEXT *) userdata;
471 return (ctx->pem_callback(buf, size, ctx->pem_userdata));
475 * Check a PEM-encoded file
476 * for the existence of a private key.
477 * Returns: true if a private key is found
480 bool crypto_keypair_has_key(const char *file) {
484 unsigned char *data = NULL;
488 if (!(bio = BIO_new_file(file, "r"))) {
489 openssl_post_errors(M_ERROR, _("Unable to open private key file"));
493 while (PEM_read_bio(bio, &name, &header, &data, &len)) {
494 /* We don't care what the data is, just that it's there */
495 OPENSSL_free(header);
499 * PEM Header Found, check for a private key
500 * Due to OpenSSL limitations, we must specifically
501 * list supported PEM private key encodings.
503 if (strcmp(name, PEM_STRING_RSA) == 0
504 || strcmp(name, PEM_STRING_DSA) == 0
505 || strcmp(name, PEM_STRING_PKCS8) == 0
506 || strcmp(name, PEM_STRING_PKCS8INF) == 0) {
518 /* Post PEM-decoding error messages, if any */
519 openssl_post_errors(M_ERROR, _("Unable to read private key from file"));
524 * Load a PEM-encoded private key.
525 * Returns: true on success
528 int crypto_keypair_load_key(X509_KEYPAIR *keypair, const char *file,
529 CRYPTO_PEM_PASSWD_CB *pem_callback,
530 const void *pem_userdata)
536 if (!(bio = BIO_new_file(file, "r"))) {
537 openssl_post_errors(M_ERROR, _("Unable to open private key file"));
541 /* Set up PEM encryption callback */
543 ctx.pem_callback = pem_callback;
544 ctx.pem_userdata = pem_userdata;
546 ctx.pem_callback = crypto_default_pem_callback;
547 ctx.pem_userdata = NULL;
550 keypair->privkey = PEM_read_bio_PrivateKey(bio, NULL, crypto_pem_callback_dispatch, &ctx);
552 if (!keypair->privkey) {
553 openssl_post_errors(M_ERROR, _("Unable to read private key from file"));
561 * Free memory associated with a keypair object.
563 void crypto_keypair_free(X509_KEYPAIR *keypair)
565 if (keypair->pubkey) {
566 EVP_PKEY_free(keypair->pubkey);
568 if (keypair->privkey) {
569 EVP_PKEY_free(keypair->privkey);
571 if (keypair->keyid) {
572 M_ASN1_OCTET_STRING_free(keypair->keyid);
578 * Create a new message digest context of the specified type
579 * Returns: A pointer to a DIGEST object on success.
582 DIGEST *crypto_digest_new(JCR *jcr, crypto_digest_t type)
585 const EVP_MD *md = NULL; /* Quell invalid uninitialized warnings */
587 digest = (DIGEST *)malloc(sizeof(DIGEST));
590 Dmsg1(150, "crypto_digest_new jcr=%p\n", jcr);
592 /* Initialize the OpenSSL message digest context */
593 digest->ctx = EVP_MD_CTX_new();
596 EVP_MD_CTX_reset(digest->ctx);
598 /* Determine the correct OpenSSL message digest type */
600 case CRYPTO_DIGEST_MD5:
603 case CRYPTO_DIGEST_SHA1:
607 case CRYPTO_DIGEST_SHA256:
610 case CRYPTO_DIGEST_SHA512:
615 Jmsg1(jcr, M_ERROR, 0, _("Unsupported digest type: %d\n"), type);
619 /* Initialize the backing OpenSSL context */
620 if (EVP_DigestInit_ex(digest->ctx, md, NULL) == 0) {
627 /* This should not happen, but never say never ... */
628 Dmsg0(150, "Digest init failed.\n");
629 openssl_post_errors(jcr, M_ERROR, _("OpenSSL digest initialization failed"));
630 crypto_digest_free(digest);
635 * Hash length bytes of data into the provided digest context.
636 * Returns: true on success
639 bool crypto_digest_update(DIGEST *digest, const uint8_t *data, uint32_t length)
641 if (EVP_DigestUpdate(digest->ctx, data, length) == 0) {
642 Dmsg0(150, "digest update failed\n");
643 openssl_post_errors(digest->jcr, M_ERROR, _("OpenSSL digest update failed"));
651 * Finalize the data in digest, storing the result in dest and the result size
652 * in length. The result size can be determined with crypto_digest_size().
654 * Returns: true on success
657 bool crypto_digest_finalize(DIGEST *digest, uint8_t *dest, uint32_t *length)
659 if (!EVP_DigestFinal(digest->ctx, dest, (unsigned int *)length)) {
660 Dmsg0(150, "digest finalize failed\n");
661 openssl_post_errors(digest->jcr, M_ERROR, _("OpenSSL digest finalize failed"));
669 * Free memory associated with a digest object.
671 void crypto_digest_free(DIGEST *digest)
673 EVP_MD_CTX_free(digest->ctx);
678 * Create a new message signature context.
679 * Returns: A pointer to a SIGNATURE object on success.
682 SIGNATURE *crypto_sign_new(JCR *jcr)
686 sig = (SIGNATURE *)malloc(sizeof(SIGNATURE));
691 sig->sigData = SignatureData_new();
693 Dmsg1(150, "crypto_sign_new jcr=%p\n", jcr);
696 /* Allocation failed in OpenSSL */
701 /* Set the ASN.1 structure version number */
702 ASN1_INTEGER_set(sig->sigData->version, BACULA_ASN1_VERSION);
708 * For a given public key, find the associated SignatureInfo record
709 * and create a digest context for signature validation
711 * Returns: CRYPTO_ERROR_NONE on success, with the newly allocated DIGEST in digest.
712 * A crypto_error_t value on failure.
714 crypto_error_t crypto_sign_get_digest(SIGNATURE *sig, X509_KEYPAIR *keypair,
715 crypto_digest_t &type, DIGEST **digest)
717 STACK_OF(SignerInfo) *signers;
721 signers = sig->sigData->signerInfo;
723 for (i = 0; i < sk_SignerInfo_num(signers); i++) {
724 si = sk_SignerInfo_value(signers, i);
725 if (M_ASN1_OCTET_STRING_cmp(keypair->keyid, si->subjectKeyIdentifier) == 0) {
726 /* Get the digest algorithm and allocate a digest context */
727 Dmsg1(150, "crypto_sign_get_digest jcr=%p\n", sig->jcr);
728 switch (OBJ_obj2nid(si->digestAlgorithm)) {
730 Dmsg0(100, "sign digest algorithm is MD5\n");
731 type = CRYPTO_DIGEST_MD5;
732 *digest = crypto_digest_new(sig->jcr, CRYPTO_DIGEST_MD5);
735 Dmsg0(100, "sign digest algorithm is SHA1\n");
736 type = CRYPTO_DIGEST_SHA1;
737 *digest = crypto_digest_new(sig->jcr, CRYPTO_DIGEST_SHA1);
741 Dmsg0(100, "sign digest algorithm is SHA256\n");
742 type = CRYPTO_DIGEST_SHA256;
743 *digest = crypto_digest_new(sig->jcr, CRYPTO_DIGEST_SHA256);
746 Dmsg0(100, "sign digest algorithm is SHA512\n");
747 type = CRYPTO_DIGEST_SHA512;
748 *digest = crypto_digest_new(sig->jcr, CRYPTO_DIGEST_SHA512);
752 type = CRYPTO_DIGEST_NONE;
754 return CRYPTO_ERROR_INVALID_DIGEST;
757 /* Shouldn't happen */
758 if (*digest == NULL) {
759 openssl_post_errors(sig->jcr, M_ERROR, _("OpenSSL digest_new failed"));
760 return CRYPTO_ERROR_INVALID_DIGEST;
762 return CRYPTO_ERROR_NONE;
765 openssl_post_errors(sig->jcr, M_ERROR, _("OpenSSL sign get digest failed"));
770 return CRYPTO_ERROR_NOSIGNER;
774 * For a given signature, public key, and digest, verify the SIGNATURE.
775 * Returns: CRYPTO_ERROR_NONE on success.
776 * A crypto_error_t value on failure.
778 crypto_error_t crypto_sign_verify(SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST *digest)
780 STACK_OF(SignerInfo) *signers;
784 const unsigned char *sigData;
786 signers = sig->sigData->signerInfo;
788 /* Find the signer */
789 for (i = 0; i < sk_SignerInfo_num(signers); i++) {
790 si = sk_SignerInfo_value(signers, i);
791 if (M_ASN1_OCTET_STRING_cmp(keypair->keyid, si->subjectKeyIdentifier) == 0) {
792 /* Extract the signature data */
793 sigLen = M_ASN1_STRING_length(si->signature);
794 sigData = M_ASN1_STRING_data(si->signature);
796 ok = EVP_VerifyFinal(digest->ctx, sigData, sigLen, keypair->pubkey);
798 return CRYPTO_ERROR_NONE;
799 } else if (ok == 0) {
800 openssl_post_errors(sig->jcr, M_ERROR, _("OpenSSL digest Verify final failed"));
801 return CRYPTO_ERROR_BAD_SIGNATURE;
803 /* Shouldn't happen */
804 openssl_post_errors(sig->jcr, M_ERROR, _("OpenSSL digest Verify final failed"));
805 return CRYPTO_ERROR_INTERNAL;
809 Jmsg(sig->jcr, M_ERROR, 0, _("No signers found for crypto verify.\n"));
810 /* Signer wasn't found. */
811 return CRYPTO_ERROR_NOSIGNER;
817 * Returns: true on success
820 int crypto_sign_add_signer(SIGNATURE *sig, DIGEST *digest, X509_KEYPAIR *keypair)
822 SignerInfo *si = NULL;
823 unsigned char *buf = NULL;
826 si = SignerInfo_new();
829 /* Allocation failed in OpenSSL */
833 /* Set the ASN.1 structure version number */
834 ASN1_INTEGER_set(si->version, BACULA_ASN1_VERSION);
836 /* Set the digest algorithm identifier */
837 switch (digest->type) {
838 case CRYPTO_DIGEST_MD5:
839 si->digestAlgorithm = OBJ_nid2obj(NID_md5);
841 case CRYPTO_DIGEST_SHA1:
842 si->digestAlgorithm = OBJ_nid2obj(NID_sha1);
845 case CRYPTO_DIGEST_SHA256:
846 si->digestAlgorithm = OBJ_nid2obj(NID_sha256);
848 case CRYPTO_DIGEST_SHA512:
849 si->digestAlgorithm = OBJ_nid2obj(NID_sha512);
853 /* This should never happen */
857 /* Drop the string allocated by OpenSSL, and add our subjectKeyIdentifier */
858 M_ASN1_OCTET_STRING_free(si->subjectKeyIdentifier);
859 si->subjectKeyIdentifier = M_ASN1_OCTET_STRING_dup(keypair->keyid);
861 /* Set our signature algorithm. We currently require RSA */
862 assert(EVP_PKEY_type(keypair->pubkey->type) == EVP_PKEY_RSA);
863 /* This is slightly evil. Reach into the MD structure and grab the key type */
864 si->signatureAlgorithm = OBJ_nid2obj(EVP_MD_pkey_type(EVP_MD_CTX_md(digest->ctx)));
866 /* Finalize/Sign our Digest */
867 len = EVP_PKEY_size(keypair->privkey);
868 buf = (unsigned char *) malloc(len);
869 if (!EVP_SignFinal(digest->ctx, buf, &len, keypair->privkey)) {
870 openssl_post_errors(M_ERROR, _("Signature creation failed"));
874 /* Add the signature to the SignerInfo structure */
875 if (!M_ASN1_OCTET_STRING_set(si->signature, buf, len)) {
876 /* Allocation failed in OpenSSL */
880 /* No longer needed */
883 /* Push the new SignerInfo structure onto the stack */
884 sk_SignerInfo_push(sig->sigData->signerInfo, si);
900 * Encodes the SignatureData structure. The length argument is used to specify the
901 * size of dest. A length of 0 will cause no data to be written to dest, and the
902 * required length to be written to length. The caller can then allocate sufficient
903 * space for the output.
905 * Returns: true on success, stores the encoded data in dest, and the size in length.
908 int crypto_sign_encode(SIGNATURE *sig, uint8_t *dest, uint32_t *length)
911 *length = i2d_SignatureData(sig->sigData, NULL);
915 *length = i2d_SignatureData(sig->sigData, (unsigned char **)&dest);
920 * Decodes the SignatureData structure. The length argument is used to specify the
923 * Returns: SIGNATURE instance on success.
928 SIGNATURE *crypto_sign_decode(JCR *jcr, const uint8_t *sigData, uint32_t length)
931 const unsigned char *p = (const unsigned char *) sigData;
933 sig = (SIGNATURE *)malloc(sizeof(SIGNATURE));
939 /* d2i_SignatureData modifies the supplied pointer */
940 sig->sigData = d2i_SignatureData(NULL, &p, length);
943 /* Allocation / Decoding failed in OpenSSL */
944 openssl_post_errors(jcr, M_ERROR, _("Signature decoding failed"));
953 * Free memory associated with a signature object.
955 void crypto_sign_free(SIGNATURE *sig)
957 SignatureData_free(sig->sigData);
962 * Create a new encryption session.
963 * Returns: A pointer to a CRYPTO_SESSION object on success.
966 * Note! Bacula malloc() fails if out of memory.
968 CRYPTO_SESSION *crypto_session_new (crypto_cipher_t cipher, alist *pubkeys)
971 X509_KEYPAIR *keypair;
972 const EVP_CIPHER *ec;
976 /* Allocate our session description structures */
977 cs = (CRYPTO_SESSION *)malloc(sizeof(CRYPTO_SESSION));
979 /* Initialize required fields */
980 cs->session_key = NULL;
982 /* Allocate a CryptoData structure */
983 cs->cryptoData = CryptoData_new();
985 if (!cs->cryptoData) {
986 /* Allocation failed in OpenSSL */
991 /* Set the ASN.1 structure version number */
992 ASN1_INTEGER_set(cs->cryptoData->version, BACULA_ASN1_VERSION);
995 * Acquire a cipher instance and set the ASN.1 cipher NID
998 case CRYPTO_CIPHER_AES_128_CBC:
999 /* AES 128 bit CBC */
1000 cs->cryptoData->contentEncryptionAlgorithm = OBJ_nid2obj(NID_aes_128_cbc);
1001 ec = EVP_aes_128_cbc();
1003 #ifndef HAVE_OPENSSL_EXPORT_LIBRARY
1004 case CRYPTO_CIPHER_AES_192_CBC:
1005 /* AES 192 bit CBC */
1006 cs->cryptoData->contentEncryptionAlgorithm = OBJ_nid2obj(NID_aes_192_cbc);
1007 ec = EVP_aes_192_cbc();
1009 case CRYPTO_CIPHER_AES_256_CBC:
1010 /* AES 256 bit CBC */
1011 cs->cryptoData->contentEncryptionAlgorithm = OBJ_nid2obj(NID_aes_256_cbc);
1012 ec = EVP_aes_256_cbc();
1015 case CRYPTO_CIPHER_BLOWFISH_CBC:
1017 cs->cryptoData->contentEncryptionAlgorithm = OBJ_nid2obj(NID_bf_cbc);
1021 Jmsg0(NULL, M_ERROR, 0, _("Unsupported cipher type specified\n"));
1022 crypto_session_free(cs);
1026 /* Generate a symmetric session key */
1027 cs->session_key_len = EVP_CIPHER_key_length(ec);
1028 cs->session_key = (unsigned char *) malloc(cs->session_key_len);
1029 if (RAND_bytes(cs->session_key, cs->session_key_len) <= 0) {
1030 /* OpenSSL failure */
1031 crypto_session_free(cs);
1035 /* Generate an IV if possible */
1036 if ((iv_len = EVP_CIPHER_iv_length(ec))) {
1037 iv = (unsigned char *)malloc(iv_len);
1039 /* Generate random IV */
1040 if (RAND_bytes(iv, iv_len) <= 0) {
1041 /* OpenSSL failure */
1042 crypto_session_free(cs);
1047 /* Store it in our ASN.1 structure */
1048 if (!M_ASN1_OCTET_STRING_set(cs->cryptoData->iv, iv, iv_len)) {
1049 /* Allocation failed in OpenSSL */
1050 crypto_session_free(cs);
1058 * Create RecipientInfo structures for supplied
1061 foreach_alist(keypair, pubkeys) {
1063 unsigned char *ekey;
1066 ri = RecipientInfo_new();
1068 /* Allocation failed in OpenSSL */
1069 crypto_session_free(cs);
1073 /* Set the ASN.1 structure version number */
1074 ASN1_INTEGER_set(ri->version, BACULA_ASN1_VERSION);
1076 /* Drop the string allocated by OpenSSL, and add our subjectKeyIdentifier */
1077 M_ASN1_OCTET_STRING_free(ri->subjectKeyIdentifier);
1078 ri->subjectKeyIdentifier = M_ASN1_OCTET_STRING_dup(keypair->keyid);
1080 /* Set our key encryption algorithm. We currently require RSA */
1081 assert(keypair->pubkey && EVP_PKEY_type(keypair->pubkey->type) == EVP_PKEY_RSA);
1082 ri->keyEncryptionAlgorithm = OBJ_nid2obj(NID_rsaEncryption);
1084 /* Encrypt the session key */
1085 ekey = (unsigned char *)malloc(EVP_PKEY_size(keypair->pubkey));
1087 if ((ekey_len = EVP_PKEY_encrypt(ekey, cs->session_key, cs->session_key_len, keypair->pubkey)) <= 0) {
1088 /* OpenSSL failure */
1089 RecipientInfo_free(ri);
1090 crypto_session_free(cs);
1095 /* Store it in our ASN.1 structure */
1096 if (!M_ASN1_OCTET_STRING_set(ri->encryptedKey, ekey, ekey_len)) {
1097 /* Allocation failed in OpenSSL */
1098 RecipientInfo_free(ri);
1099 crypto_session_free(cs);
1104 /* Free the encrypted key buffer */
1107 /* Push the new RecipientInfo structure onto the stack */
1108 sk_RecipientInfo_push(cs->cryptoData->recipientInfo, ri);
1115 * Encodes the CryptoData structure. The length argument is used to specify the
1116 * size of dest. A length of 0 will cause no data to be written to dest, and the
1117 * required length to be written to length. The caller can then allocate sufficient
1118 * space for the output.
1120 * Returns: true on success, stores the encoded data in dest, and the size in length.
1123 bool crypto_session_encode(CRYPTO_SESSION *cs, uint8_t *dest, uint32_t *length)
1126 *length = i2d_CryptoData(cs->cryptoData, NULL);
1130 *length = i2d_CryptoData(cs->cryptoData, &dest);
1135 * Decodes the CryptoData structure. The length argument is
1136 * used to specify the size of data.
1138 * Returns: CRYPTO_SESSION instance on success.
1140 * Returns: CRYPTO_ERROR_NONE and a pointer to a newly allocated CRYPTO_SESSION structure in *session on success.
1141 * A crypto_error_t value on failure.
1143 crypto_error_t crypto_session_decode(const uint8_t *data, uint32_t length, alist *keypairs, CRYPTO_SESSION **session)
1146 X509_KEYPAIR *keypair;
1147 STACK_OF(RecipientInfo) *recipients;
1148 crypto_error_t retval = CRYPTO_ERROR_NONE;
1149 const unsigned char *p = (const unsigned char *)data;
1151 /* bacula-fd.conf doesn't contains any key */
1153 return CRYPTO_ERROR_NORECIPIENT;
1156 cs = (CRYPTO_SESSION *)malloc(sizeof(CRYPTO_SESSION));
1158 /* Initialize required fields */
1159 cs->session_key = NULL;
1161 /* d2i_CryptoData modifies the supplied pointer */
1162 cs->cryptoData = d2i_CryptoData(NULL, &p, length);
1164 if (!cs->cryptoData) {
1165 /* Allocation / Decoding failed in OpenSSL */
1166 openssl_post_errors(M_ERROR, _("CryptoData decoding failed"));
1167 retval = CRYPTO_ERROR_INTERNAL;
1171 recipients = cs->cryptoData->recipientInfo;
1174 * Find a matching RecipientInfo structure for a supplied
1177 foreach_alist(keypair, keypairs) {
1181 /* Private key available? */
1182 if (keypair->privkey == NULL) {
1186 for (i = 0; i < sk_RecipientInfo_num(recipients); i++) {
1187 ri = sk_RecipientInfo_value(recipients, i);
1189 /* Match against the subjectKeyIdentifier */
1190 if (M_ASN1_OCTET_STRING_cmp(keypair->keyid, ri->subjectKeyIdentifier) == 0) {
1191 /* Match found, extract symmetric encryption session data */
1193 /* RSA is required. */
1194 assert(EVP_PKEY_type(keypair->privkey->type) == EVP_PKEY_RSA);
1196 /* If we recieve a RecipientInfo structure that does not use
1197 * RSA, return an error */
1198 if (OBJ_obj2nid(ri->keyEncryptionAlgorithm) != NID_rsaEncryption) {
1199 retval = CRYPTO_ERROR_INVALID_CRYPTO;
1203 /* Decrypt the session key */
1204 /* Allocate sufficient space for the largest possible decrypted data */
1205 cs->session_key = (unsigned char *)malloc(EVP_PKEY_size(keypair->privkey));
1206 cs->session_key_len = EVP_PKEY_decrypt(cs->session_key, M_ASN1_STRING_data(ri->encryptedKey),
1207 M_ASN1_STRING_length(ri->encryptedKey), keypair->privkey);
1209 if (cs->session_key_len <= 0) {
1210 openssl_post_errors(M_ERROR, _("Failure decrypting the session key"));
1211 retval = CRYPTO_ERROR_DECRYPTION;
1215 /* Session key successfully extracted, return the CRYPTO_SESSION structure */
1217 return CRYPTO_ERROR_NONE;
1222 /* No matching recipient found */
1223 return CRYPTO_ERROR_NORECIPIENT;
1226 crypto_session_free(cs);
1231 * Free memory associated with a crypto session object.
1233 void crypto_session_free(CRYPTO_SESSION *cs)
1235 if (cs->cryptoData) {
1236 CryptoData_free(cs->cryptoData);
1238 if (cs->session_key){
1239 free(cs->session_key);
1245 * Create a new crypto cipher context with the specified session object
1246 * Returns: A pointer to a CIPHER_CONTEXT object on success. The cipher block size is returned in blocksize.
1249 CIPHER_CONTEXT *crypto_cipher_new(CRYPTO_SESSION *cs, bool encrypt, uint32_t *blocksize)
1251 CIPHER_CONTEXT *cipher_ctx;
1252 const EVP_CIPHER *ec;
1254 cipher_ctx = (CIPHER_CONTEXT *)malloc(sizeof(CIPHER_CONTEXT));
1258 cipher_ctx->ctx = EVP_CIPHER_CTX_new();
1259 if (!cipher_ctx->ctx)
1263 * Acquire a cipher instance for the given ASN.1 cipher NID
1265 if ((ec = EVP_get_cipherbyobj(cs->cryptoData->contentEncryptionAlgorithm)) == NULL) {
1266 Jmsg1(NULL, M_ERROR, 0,
1267 _("Unsupported contentEncryptionAlgorithm: %d\n"), OBJ_obj2nid(cs->cryptoData->contentEncryptionAlgorithm));
1272 /* Initialize the OpenSSL cipher context */
1273 EVP_CIPHER_CTX_reset(cipher_ctx->ctx);
1275 /* Initialize for encryption */
1276 if (!EVP_CipherInit_ex(cipher_ctx->ctx, ec, NULL, NULL, NULL, 1)) {
1277 openssl_post_errors(M_ERROR, _("OpenSSL cipher context initialization failed"));
1281 /* Initialize for decryption */
1282 if (!EVP_CipherInit_ex(cipher_ctx->ctx, ec, NULL, NULL, NULL, 0)) {
1283 openssl_post_errors(M_ERROR, _("OpenSSL cipher context initialization failed"));
1288 /* Set the key size */
1289 if (!EVP_CIPHER_CTX_set_key_length(cipher_ctx->ctx, cs->session_key_len)) {
1290 openssl_post_errors(M_ERROR, _("Encryption session provided an invalid symmetric key"));
1294 /* Validate the IV length */
1295 if (EVP_CIPHER_iv_length(ec) != ASN1_STRING_length(cs->cryptoData->iv)) {
1296 openssl_post_errors(M_ERROR, _("Encryption session provided an invalid IV"));
1300 /* Add the key and IV to the cipher context */
1301 if (!EVP_CipherInit_ex(cipher_ctx->ctx, NULL, NULL, cs->session_key, ASN1_STRING_get0_data(cs->cryptoData->iv), -1)) {
1302 openssl_post_errors(M_ERROR, _("OpenSSL cipher context key/IV initialization failed"));
1306 *blocksize = EVP_CIPHER_CTX_block_size(cipher_ctx->ctx);
1310 crypto_cipher_free(cipher_ctx);
1316 * Encrypt/Decrypt length bytes of data using the provided cipher context
1317 * Returns: true on success, number of bytes output in written
1320 bool crypto_cipher_update(CIPHER_CONTEXT *cipher_ctx, const uint8_t *data, uint32_t length, const uint8_t *dest, uint32_t *written)
1322 if (!EVP_CipherUpdate(cipher_ctx->ctx, (unsigned char *)dest, (int *)written, (const unsigned char *)data, length)) {
1323 /* This really shouldn't fail */
1331 * Finalize the cipher context, writing any remaining data and necessary padding
1332 * to dest, and the size in written.
1333 * The result size will either be one block of data or zero.
1335 * Returns: true on success
1338 bool crypto_cipher_finalize (CIPHER_CONTEXT *cipher_ctx, uint8_t *dest, uint32_t *written)
1340 if (!EVP_CipherFinal_ex(cipher_ctx->ctx, (unsigned char *)dest, (int *) written)) {
1341 /* This really shouldn't fail */
1350 * Free memory associated with a cipher context.
1352 void crypto_cipher_free (CIPHER_CONTEXT *cipher_ctx)
1354 EVP_CIPHER_CTX_free(cipher_ctx->ctx);
1358 #else /* HAVE_OPENSSL */
1359 # error No encryption library available
1360 #endif /* HAVE_OPENSSL */
1362 #else /* HAVE_CRYPTO */
1365 * Cryptography Support Disabled
1368 /* Message Digest Structure */
1370 crypto_digest_t type;
1378 /* Dummy Signature Structure */
1383 DIGEST *crypto_digest_new(JCR *jcr, crypto_digest_t type)
1387 digest = (DIGEST *)malloc(sizeof(DIGEST));
1388 digest->type = type;
1392 case CRYPTO_DIGEST_MD5:
1393 MD5Init(&digest->md5);
1395 case CRYPTO_DIGEST_SHA1:
1396 SHA1Init(&digest->sha1);
1399 Jmsg1(jcr, M_ERROR, 0, _("Unsupported digest type=%d specified\n"), type);
1407 bool crypto_digest_update(DIGEST *digest, const uint8_t *data, uint32_t length)
1409 switch (digest->type) {
1410 case CRYPTO_DIGEST_MD5:
1411 /* Doesn't return anything ... */
1412 MD5Update(&digest->md5, (unsigned char *) data, length);
1414 case CRYPTO_DIGEST_SHA1:
1416 if ((ret = SHA1Update(&digest->sha1, (const u_int8_t *) data, length)) == shaSuccess) {
1419 Jmsg1(NULL, M_ERROR, 0, _("SHA1Update() returned an error: %d\n"), ret);
1428 bool crypto_digest_finalize(DIGEST *digest, uint8_t *dest, uint32_t *length)
1430 switch (digest->type) {
1431 case CRYPTO_DIGEST_MD5:
1432 /* Guard against programmer error by either the API client or
1433 * an out-of-sync CRYPTO_DIGEST_MAX_SIZE */
1434 assert(*length >= CRYPTO_DIGEST_MD5_SIZE);
1435 *length = CRYPTO_DIGEST_MD5_SIZE;
1436 /* Doesn't return anything ... */
1437 MD5Final((unsigned char *)dest, &digest->md5);
1439 case CRYPTO_DIGEST_SHA1:
1440 /* Guard against programmer error by either the API client or
1441 * an out-of-sync CRYPTO_DIGEST_MAX_SIZE */
1442 assert(*length >= CRYPTO_DIGEST_SHA1_SIZE);
1443 *length = CRYPTO_DIGEST_SHA1_SIZE;
1444 if (SHA1Final(&digest->sha1, (u_int8_t *) dest) == shaSuccess) {
1457 void crypto_digest_free(DIGEST *digest)
1462 SIGNATURE *crypto_sign_new(JCR *jcr) { return NULL; }
1464 crypto_error_t crypto_sign_get_digest (SIGNATURE *sig, X509_KEYPAIR *keypair,
1465 crypto_digest_t &type, DIGEST **digest)
1466 { return CRYPTO_ERROR_INTERNAL; }
1468 crypto_error_t crypto_sign_verify (SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST *digest) { return CRYPTO_ERROR_INTERNAL; }
1470 int crypto_sign_add_signer (SIGNATURE *sig, DIGEST *digest, X509_KEYPAIR *keypair) { return false; }
1471 int crypto_sign_encode (SIGNATURE *sig, uint8_t *dest, uint32_t *length) { return false; }
1473 SIGNATURE *crypto_sign_decode (JCR *jcr, const uint8_t *sigData, uint32_t length) { return NULL; }
1474 void crypto_sign_free (SIGNATURE *sig) { }
1477 X509_KEYPAIR *crypto_keypair_new(void) { return NULL; }
1478 X509_KEYPAIR *crypto_keypair_dup (X509_KEYPAIR *keypair) { return NULL; }
1479 int crypto_keypair_load_cert (X509_KEYPAIR *keypair, const char *file) { return false; }
1480 bool crypto_keypair_has_key (const char *file) { return false; }
1481 int crypto_keypair_load_key (X509_KEYPAIR *keypair, const char *file, CRYPTO_PEM_PASSWD_CB *pem_callback, const void *pem_userdata) { return false; }
1482 void crypto_keypair_free (X509_KEYPAIR *keypair) { }
1484 CRYPTO_SESSION *crypto_session_new (crypto_cipher_t cipher, alist *pubkeys) { return NULL; }
1485 void crypto_session_free (CRYPTO_SESSION *cs) { }
1486 bool crypto_session_encode (CRYPTO_SESSION *cs, uint8_t *dest, uint32_t *length) { return false; }
1487 crypto_error_t crypto_session_decode(const uint8_t *data, uint32_t length, alist *keypairs, CRYPTO_SESSION **session) { return CRYPTO_ERROR_INTERNAL; }
1489 CIPHER_CONTEXT *crypto_cipher_new (CRYPTO_SESSION *cs, bool encrypt, uint32_t *blocksize) { return NULL; }
1490 bool crypto_cipher_update (CIPHER_CONTEXT *cipher_ctx, const uint8_t *data, uint32_t length, const uint8_t *dest, uint32_t *written) { return false; }
1491 bool crypto_cipher_finalize (CIPHER_CONTEXT *cipher_ctx, uint8_t *dest, uint32_t *written) { return false; }
1492 void crypto_cipher_free (CIPHER_CONTEXT *cipher_ctx) { }
1494 #endif /* HAVE_CRYPTO */
1499 * Default PEM encryption passphrase callback.
1500 * Returns an empty password.
1502 int crypto_default_pem_callback(char *buf, int size, const void *userdata)
1504 bstrncpy(buf, "", size);
1505 return (strlen(buf));
1509 * Returns the ASCII name of the digest type.
1510 * Returns: ASCII name of digest type.
1512 const char *crypto_digest_name(DIGEST *digest)
1514 switch (digest->type) {
1515 case CRYPTO_DIGEST_MD5:
1517 case CRYPTO_DIGEST_SHA1:
1519 case CRYPTO_DIGEST_SHA256:
1521 case CRYPTO_DIGEST_SHA512:
1523 case CRYPTO_DIGEST_NONE:
1526 return "Invalid Digest Type";
1532 * Given a stream type, returns the associated
1533 * crypto_digest_t value.
1535 crypto_digest_t crypto_digest_stream_type(int stream)
1538 case STREAM_MD5_DIGEST:
1539 return CRYPTO_DIGEST_MD5;
1540 case STREAM_SHA1_DIGEST:
1541 return CRYPTO_DIGEST_SHA1;
1542 case STREAM_SHA256_DIGEST:
1543 return CRYPTO_DIGEST_SHA256;
1544 case STREAM_SHA512_DIGEST:
1545 return CRYPTO_DIGEST_SHA512;
1547 return CRYPTO_DIGEST_NONE;
1552 * * Given a crypto_error_t value, return the associated
1555 const char *crypto_strerror(crypto_error_t error) {
1557 case CRYPTO_ERROR_NONE:
1558 return _("No error");
1559 case CRYPTO_ERROR_NOSIGNER:
1560 return _("Signer not found");
1561 case CRYPTO_ERROR_NORECIPIENT:
1562 return _("Recipient not found");
1563 case CRYPTO_ERROR_INVALID_DIGEST:
1564 return _("Unsupported digest algorithm");
1565 case CRYPTO_ERROR_INVALID_CRYPTO:
1566 return _("Unsupported encryption algorithm");
1567 case CRYPTO_ERROR_BAD_SIGNATURE:
1568 return _("Signature is invalid");
1569 case CRYPTO_ERROR_DECRYPTION:
1570 return _("Decryption error");
1571 case CRYPTO_ERROR_INTERNAL:
1572 /* This shouldn't happen */
1573 return _("Internal error");
1575 return _("Unknown error");