]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/crypto.c
Support for FD-side file encryption.
[bacula/bacula] / bacula / src / lib / crypto.c
1 /*
2  * crypto.c Encryption support functions
3  *
4  * Author: Landon Fuller <landonf@opendarwin.org>
5  *
6  * Version $Id$
7  *
8  * Copyright (C) 2005 Kern Sibbald
9  *
10  * This file was contributed to the Bacula project by Landon Fuller.
11  *
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.
17  *
18  * If you wish to license these contributions under an alternate open source
19  * license please contact Landon Fuller <landonf@opendarwin.org>.
20  */
21 /*
22    Copyright (C) 2005 Kern Sibbald
23
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.
28
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.
33
34  */
35
36
37 #include "bacula.h"
38 #include <assert.h>
39
40 /*
41  * Bacula ASN.1 Syntax
42  *
43  * OID Allocation:
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
48  *
49  * Top Level Allocations - 500.2
50  * 1 - Published Allocations
51  *   1.1 - Bacula Encryption
52  *
53  * Bacula Encryption - 500.2.1.1
54  * 1 - ASN.1 Modules
55  *    1.1 - BaculaCrypto
56  * 2 - ASN.1 Object Identifiers
57  *    2.1 - SignatureData
58  *    2.2 - SignerInfo
59  *    2.3 - CryptoData
60  *    2.4 - RecipientInfo
61  *
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) }
66  *
67  * DEFINITIONS AUTOMATIC TAGS ::=
68  * BEGIN
69  *
70  * SignatureData ::= SEQUENCE {
71  *    version         Version DEFAULT v0,
72  *    signerInfo      SignerInfo }
73  *
74  * CryptoData ::= SEQUENCE {
75  *    version                     Version DEFAULT v0,
76  *    contentEncryptionAlgorithm  ContentEncryptionAlgorithmIdentifier,
77  *    iv                          InitializationVector,
78  *    recipientInfo               RecipientInfo
79  * }
80  *
81  * SignerInfo ::= SET OF SignerInfo
82  * RecipientInfo ::= SET OF RecipientInfo
83  *
84  * Version ::= INTEGER { v0(0) }
85  *
86  * SignerInfo ::= SEQUENCE {
87  *    version                 Version,
88  *    subjectKeyIdentifier    SubjectKeyIdentifier,
89  *    digestAlgorithm         DigestAlgorithmIdentifier,
90  *    signatureAlgorithm      SignatureAlgorithmIdentifier,
91  *    signature               SignatureValue }
92  *
93  * RecipientInfo ::= SEQUENCE {
94  *    version                 Version
95  *    subjectKeyIdentifier    SubjectKeyIdentifier
96  *    keyEncryptionAlgorithm  KeyEncryptionAlgorithmIdentifier
97  *    encryptedKey            EncryptedKey
98  * }
99  *
100  * SubjectKeyIdentifier ::= OCTET STRING
101  *
102  * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
103  *
104  * SignatureAlgorithmIdentifier ::= AlgorithmIdentifier
105  *
106  * KeyEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
107  *
108  * ContentEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
109  *
110  * InitializationVector ::= OCTET STRING
111  *
112  * SignatureValue ::= OCTET STRING
113  *
114  * EncryptedKey ::= OCTET STRING
115  *
116  * AlgorithmIdentifier ::= OBJECT IDENTIFIER
117  *
118  * END
119  */
120
121 #ifdef HAVE_CRYPTO /* Is encryption enabled? */
122 #ifdef HAVE_OPENSSL /* How about OpenSSL? */
123
124 /* Are we initialized? */
125 static int crypto_initialized = false;
126
127 /* ASN.1 Declarations */
128 #define BACULA_ASN1_VERSION 0
129
130 typedef struct {
131    ASN1_INTEGER *version;
132    ASN1_OCTET_STRING *subjectKeyIdentifier;
133    ASN1_OBJECT *digestAlgorithm;
134    ASN1_OBJECT *signatureAlgorithm;
135    ASN1_OCTET_STRING *signature;
136 } SignerInfo;
137
138 typedef struct {
139    ASN1_INTEGER *version;
140    ASN1_OCTET_STRING *subjectKeyIdentifier;
141    ASN1_OBJECT *keyEncryptionAlgorithm;
142    ASN1_OCTET_STRING *encryptedKey;
143 } RecipientInfo;
144
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);
152
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);
159
160 typedef struct {
161    ASN1_INTEGER *version;
162    STACK_OF(SignerInfo) *signerInfo;
163 } SignatureData;
164
165 typedef struct {
166    ASN1_INTEGER *version;
167    ASN1_OBJECT *contentEncryptionAlgorithm;
168    ASN1_OCTET_STRING *iv;
169    STACK_OF(RecipientInfo) *recipientInfo;
170 } CryptoData;
171
172 ASN1_SEQUENCE(SignatureData) = {
173    ASN1_SIMPLE(SignatureData, version, ASN1_INTEGER),
174    ASN1_SET_OF(SignatureData, signerInfo, SignerInfo),
175 } ASN1_SEQUENCE_END(SignatureData);
176
177 ASN1_SEQUENCE(CryptoData) = {
178    ASN1_SIMPLE(CryptoData, version, ASN1_INTEGER),
179    ASN1_SIMPLE(CryptoData, iv, ASN1_OCTET_STRING),
180    ASN1_SET_OF(CryptoData, recipientInfo, RecipientInfo)
181 } ASN1_SEQUENCE_END(CryptoData);
182
183 IMPLEMENT_ASN1_FUNCTIONS(SignerInfo)
184 IMPLEMENT_ASN1_FUNCTIONS(RecipientInfo)
185 IMPLEMENT_ASN1_FUNCTIONS(SignatureData)
186 IMPLEMENT_ASN1_FUNCTIONS(CryptoData)
187 IMPLEMENT_STACK_OF(SignerInfo)
188 IMPLEMENT_STACK_OF(RecipientInfo)
189
190 /*
191  * SignerInfo and RecipientInfo stack macros, generated by OpenSSL's util/mkstack.pl.
192  */
193 #define sk_SignerInfo_new(st) SKM_sk_new(SignerInfo, (st))
194 #define sk_SignerInfo_new_null() SKM_sk_new_null(SignerInfo)
195 #define sk_SignerInfo_free(st) SKM_sk_free(SignerInfo, (st))
196 #define sk_SignerInfo_num(st) SKM_sk_num(SignerInfo, (st))
197 #define sk_SignerInfo_value(st, i) SKM_sk_value(SignerInfo, (st), (i))
198 #define sk_SignerInfo_set(st, i, val) SKM_sk_set(SignerInfo, (st), (i), (val))
199 #define sk_SignerInfo_zero(st) SKM_sk_zero(SignerInfo, (st))
200 #define sk_SignerInfo_push(st, val) SKM_sk_push(SignerInfo, (st), (val))
201 #define sk_SignerInfo_unshift(st, val) SKM_sk_unshift(SignerInfo, (st), (val))
202 #define sk_SignerInfo_find(st, val) SKM_sk_find(SignerInfo, (st), (val))
203 #define sk_SignerInfo_delete(st, i) SKM_sk_delete(SignerInfo, (st), (i))
204 #define sk_SignerInfo_delete_ptr(st, ptr) SKM_sk_delete_ptr(SignerInfo, (st), (ptr))
205 #define sk_SignerInfo_insert(st, val, i) SKM_sk_insert(SignerInfo, (st), (val), (i))
206 #define sk_SignerInfo_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(SignerInfo, (st), (cmp))
207 #define sk_SignerInfo_dup(st) SKM_sk_dup(SignerInfo, st)
208 #define sk_SignerInfo_pop_free(st, free_func) SKM_sk_pop_free(SignerInfo, (st), (free_func))
209 #define sk_SignerInfo_shift(st) SKM_sk_shift(SignerInfo, (st))
210 #define sk_SignerInfo_pop(st) SKM_sk_pop(SignerInfo, (st))
211 #define sk_SignerInfo_sort(st) SKM_sk_sort(SignerInfo, (st))
212 #define sk_SignerInfo_is_sorted(st) SKM_sk_is_sorted(SignerInfo, (st))
213
214 #define d2i_ASN1_SET_OF_SignerInfo(st, pp, length, d2i_func, free_func, ex_tag, ex_class) \
215         SKM_ASN1_SET_OF_d2i(SignerInfo, (st), (pp), (length), (d2i_func), (free_func), (ex_tag), (ex_class)) 
216 #define i2d_ASN1_SET_OF_SignerInfo(st, pp, i2d_func, ex_tag, ex_class, is_set) \
217         SKM_ASN1_SET_OF_i2d(SignerInfo, (st), (pp), (i2d_func), (ex_tag), (ex_class), (is_set))
218 #define ASN1_seq_pack_SignerInfo(st, i2d_func, buf, len) \
219         SKM_ASN1_seq_pack(SignerInfo, (st), (i2d_func), (buf), (len))
220 #define ASN1_seq_unpack_SignerInfo(buf, len, d2i_func, free_func) \
221         SKM_ASN1_seq_unpack(SignerInfo, (buf), (len), (d2i_func), (free_func))
222
223 #define sk_RecipientInfo_new(st) SKM_sk_new(RecipientInfo, (st))
224 #define sk_RecipientInfo_new_null() SKM_sk_new_null(RecipientInfo)
225 #define sk_RecipientInfo_free(st) SKM_sk_free(RecipientInfo, (st))
226 #define sk_RecipientInfo_num(st) SKM_sk_num(RecipientInfo, (st))
227 #define sk_RecipientInfo_value(st, i) SKM_sk_value(RecipientInfo, (st), (i))
228 #define sk_RecipientInfo_set(st, i, val) SKM_sk_set(RecipientInfo, (st), (i), (val))
229 #define sk_RecipientInfo_zero(st) SKM_sk_zero(RecipientInfo, (st))
230 #define sk_RecipientInfo_push(st, val) SKM_sk_push(RecipientInfo, (st), (val))
231 #define sk_RecipientInfo_unshift(st, val) SKM_sk_unshift(RecipientInfo, (st), (val))
232 #define sk_RecipientInfo_find(st, val) SKM_sk_find(RecipientInfo, (st), (val))
233 #define sk_RecipientInfo_delete(st, i) SKM_sk_delete(RecipientInfo, (st), (i))
234 #define sk_RecipientInfo_delete_ptr(st, ptr) SKM_sk_delete_ptr(RecipientInfo, (st), (ptr))
235 #define sk_RecipientInfo_insert(st, val, i) SKM_sk_insert(RecipientInfo, (st), (val), (i))
236 #define sk_RecipientInfo_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(RecipientInfo, (st), (cmp))
237 #define sk_RecipientInfo_dup(st) SKM_sk_dup(RecipientInfo, st)
238 #define sk_RecipientInfo_pop_free(st, free_func) SKM_sk_pop_free(RecipientInfo, (st), (free_func))
239 #define sk_RecipientInfo_shift(st) SKM_sk_shift(RecipientInfo, (st))
240 #define sk_RecipientInfo_pop(st) SKM_sk_pop(RecipientInfo, (st))
241 #define sk_RecipientInfo_sort(st) SKM_sk_sort(RecipientInfo, (st))
242 #define sk_RecipientInfo_is_sorted(st) SKM_sk_is_sorted(RecipientInfo, (st))
243
244 #define d2i_ASN1_SET_OF_RecipientInfo(st, pp, length, d2i_func, free_func, ex_tag, ex_class) \
245         SKM_ASN1_SET_OF_d2i(RecipientInfo, (st), (pp), (length), (d2i_func), (free_func), (ex_tag), (ex_class)) 
246 #define i2d_ASN1_SET_OF_RecipientInfo(st, pp, i2d_func, ex_tag, ex_class, is_set) \
247         SKM_ASN1_SET_OF_i2d(RecipientInfo, (st), (pp), (i2d_func), (ex_tag), (ex_class), (is_set))
248 #define ASN1_seq_pack_RecipientInfo(st, i2d_func, buf, len) \
249         SKM_ASN1_seq_pack(RecipientInfo, (st), (i2d_func), (buf), (len))
250 #define ASN1_seq_unpack_RecipientInfo(buf, len, d2i_func, free_func) \
251         SKM_ASN1_seq_unpack(RecipientInfo, (buf), (len), (d2i_func), (free_func))
252 /* End of util/mkstack.pl block */
253
254 /* X509 Public/Private Key Pair Structure */
255 struct X509_Keypair {
256    ASN1_OCTET_STRING *keyid;
257    EVP_PKEY *pubkey;
258    EVP_PKEY *privkey;
259 };
260
261 /* Message Digest Structure */
262 struct Digest {
263    crypto_digest_t type;
264    EVP_MD_CTX ctx;
265 };
266
267 /* Message Signature Structure */
268 struct Signature {
269    SignatureData *sigData;
270 };
271
272 /* Encryption Session Data */
273 struct Crypto_Session {
274    CryptoData *cryptoData;                        /* ASN.1 Structure */
275    unsigned char *session_key;                    /* Private symmetric session key */
276    size_t session_key_len;                        /* Symmetric session key length */
277 };
278
279 /* Symmetric Cipher Context */
280 struct Cipher_Context {
281    EVP_CIPHER_CTX ctx;
282 };
283
284 /* PEM Password Dispatch Context */
285 typedef struct PEM_CB_Context {
286    CRYPTO_PEM_PASSWD_CB *pem_callback;
287    const void *pem_userdata;
288 } PEM_CB_CONTEXT;
289
290 /*
291  * Extract subjectKeyIdentifier from x509 certificate.
292  * Returns: On success, an ASN1_OCTET_STRING that must be freed via M_ASN1_OCTET_STRING_free().
293  *          NULL on failure.
294  */
295 static ASN1_OCTET_STRING *openssl_cert_keyid(X509 *cert){
296    X509_EXTENSION *ext;
297    X509V3_EXT_METHOD *method;
298    ASN1_OCTET_STRING *keyid;
299    int i;
300 #if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
301    const unsigned char *ext_value_data;
302 #else
303    unsigned char *ext_value_data;
304 #endif
305
306
307    /* Find the index to the subjectKeyIdentifier extension */
308    i = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1);
309    if (i < 0) {
310       /* Not found */
311       return NULL;
312    }
313
314    /* Grab the extension */
315    ext = X509_get_ext(cert, i);
316
317    /* Get x509 extension method structure */
318    if (!(method = X509V3_EXT_get(ext))) {
319       return NULL;
320    }
321
322    ext_value_data = ext->value->data;
323
324 #if (OPENSSL_VERSION_NUMBER > 0x00907000L)
325    if (method->it) {
326       /* New style ASN1 */
327
328       /* Decode ASN1 item in data */
329       keyid = (ASN1_OCTET_STRING *) ASN1_item_d2i(NULL, &ext_value_data, ext->value->length,
330                                                   ASN1_ITEM_ptr(method->it));
331    } else {
332       /* Old style ASN1 */
333
334       /* Decode ASN1 item in data */
335       keyid = (ASN1_OCTET_STRING *) method->d2i(NULL, &ext_value_data, ext->value->length);
336    }
337
338 #else
339    keyid = (ASN1_OCTET_STRING *) method->d2i(NULL, &ext_value_data, ext->value->length);
340 #endif
341
342    return keyid;
343 }
344
345 /*
346  * Create a new keypair object.
347  *  Returns: A pointer to a X509 KEYPAIR object on success.
348  *           NULL on failure.
349  */
350 X509_KEYPAIR *crypto_keypair_new (void) {
351    X509_KEYPAIR *keypair;
352
353    /* Allocate our keypair structure */
354    keypair = (X509_KEYPAIR *) malloc(sizeof(X509_KEYPAIR));
355    if (!keypair) {
356       return NULL;
357    }
358
359    /* Initialize our keypair structure */
360    keypair->keyid = NULL;
361    keypair->pubkey = NULL;
362    keypair->privkey = NULL;
363
364    return keypair;
365 }
366
367 /*
368  * Create a copy of a keypair object. The underlying
369  * EVP objects are not duplicated, as no EVP_PKEY_dup()
370  * API is available. Instead, the reference count is
371  * incremented.
372  */
373 X509_KEYPAIR *crypto_keypair_dup (X509_KEYPAIR *keypair)
374 {
375    X509_KEYPAIR *newpair;
376
377    newpair = crypto_keypair_new();
378
379    if (!newpair) {
380       /* Allocation failed */
381       return NULL;
382    }
383
384    /* Increment the public key ref count */
385    if (keypair->pubkey) {
386       CRYPTO_add(&(keypair->pubkey->references), 1, CRYPTO_LOCK_EVP_PKEY);
387       newpair->pubkey = keypair->pubkey;
388    }
389
390    /* Increment the private key ref count */
391    if (keypair->privkey) {
392       CRYPTO_add(&(keypair->privkey->references), 1, CRYPTO_LOCK_EVP_PKEY);
393       newpair->privkey = keypair->privkey;
394    }
395
396    /* Duplicate the keyid */
397    if (keypair->keyid) {
398       newpair->keyid = M_ASN1_OCTET_STRING_dup(keypair->keyid);
399       if (!newpair->keyid) {
400          /* Allocation failed */
401          crypto_keypair_free(newpair);
402          return NULL;
403       }
404    }
405
406    return newpair;
407 }
408
409
410 /*
411  * Load a public key from a PEM-encoded x509 certificate.
412  *  Returns: true on success
413  *           false on failure
414  */
415 int crypto_keypair_load_cert (X509_KEYPAIR *keypair, const char *file)
416 {
417    BIO *bio;
418    X509 *cert;
419
420    /* Open the file */
421    if (!(bio = BIO_new_file(file, "r"))) {
422       openssl_post_errors(M_ERROR, _("Unable to open certificate file"));
423       return false;
424    }
425
426    cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
427    BIO_free(bio);
428    if (!cert) {
429       openssl_post_errors(M_ERROR, _("Unable to read certificate from file"));
430       return false;
431    }
432
433    /* Extract the public key */
434    if (!(keypair->pubkey = X509_get_pubkey(cert))) {
435       openssl_post_errors(M_ERROR, _("Unable to extract public key from certificate"));
436       goto err;
437    }
438
439    /* Extract the subjectKeyIdentifier extension field */
440    if ((keypair->keyid = openssl_cert_keyid(cert)) == NULL) {
441       Emsg0(M_ERROR, 0, _("Provided certificate does not include the required subjectKeyIdentifier extension."));
442       goto err;
443    }
444
445    /* Validate the public key type (only RSA is supported) */
446    if (EVP_PKEY_type(keypair->pubkey->type) != EVP_PKEY_RSA) {
447        Emsg1(M_ERROR, 0, _("Unsupported key type provided: %d\n"), EVP_PKEY_type(keypair->pubkey->type));
448        goto err;
449    }
450
451    X509_free(cert);
452    return true;
453
454 err:
455    X509_free(cert);
456    if (keypair->pubkey) {
457       EVP_PKEY_free(keypair->pubkey);
458    }
459    return false;
460 }
461
462 /* Dispatch user PEM encryption callbacks */
463 static int crypto_pem_callback_dispatch (char *buf, int size, int rwflag, void *userdata)
464 {
465    PEM_CB_CONTEXT *ctx = (PEM_CB_CONTEXT *) userdata;
466    return (ctx->pem_callback(buf, size, ctx->pem_userdata));
467 }
468
469 /*
470  * Check a PEM-encoded file
471  * for the existence of a private key.
472  * Returns: true if a private key is found
473  *          false otherwise
474  */
475 bool crypto_keypair_has_key (const char *file) {
476    BIO *bio;
477    char *name = NULL;
478    char *header = NULL;
479    unsigned char *data = NULL;
480    bool retval = false;
481    long len;
482
483    if (!(bio = BIO_new_file(file, "r"))) {
484       openssl_post_errors(M_ERROR, _("Unable to open private key file"));
485       return false;
486    }
487
488    while (PEM_read_bio(bio, &name, &header, &data, &len)) {
489       /* We don't care what the data is, just that it's there */
490       OPENSSL_free(header);
491       OPENSSL_free(data);
492
493       /*
494        * PEM Header Found, check for a private key
495        * Due to OpenSSL limitations, we must specifically
496        * list supported PEM private key encodings.
497        */
498       if (strcmp(name, PEM_STRING_RSA) == 0
499             || strcmp(name, PEM_STRING_DSA) == 0
500             || strcmp(name, PEM_STRING_PKCS8) == 0
501             || strcmp(name, PEM_STRING_PKCS8INF) == 0) {
502          retval = true;
503          OPENSSL_free(name);
504          break;
505       } else {
506          OPENSSL_free(name);
507       }
508    }
509
510    /* Free our bio */
511    BIO_free(bio);
512
513    /* Post PEM-decoding error messages, if any */
514    openssl_post_errors(M_ERROR, _("Unable to read private key from file"));
515    return retval;
516 }
517
518 /*
519  * Load a PEM-encoded private key.
520  *  Returns: true on success
521  *           false on failure
522  */
523 int crypto_keypair_load_key (X509_KEYPAIR *keypair, const char *file,
524                              CRYPTO_PEM_PASSWD_CB *pem_callback,
525                              const void *pem_userdata)
526 {
527    BIO *bio;
528    PEM_CB_CONTEXT ctx;
529
530    /* Open the file */
531    if (!(bio = BIO_new_file(file, "r"))) {
532       openssl_post_errors(M_ERROR, _("Unable to open private key file"));
533       return false;
534    }
535
536    /* Set up PEM encryption callback */
537    if (pem_callback) {
538       ctx.pem_callback = pem_callback;
539       ctx.pem_userdata = pem_userdata;
540    } else {
541       ctx.pem_callback = crypto_default_pem_callback;
542       ctx.pem_userdata = NULL;
543    }
544
545    keypair->privkey = PEM_read_bio_PrivateKey(bio, NULL, crypto_pem_callback_dispatch, &ctx);
546    BIO_free(bio);
547    if (!keypair->privkey) {
548       openssl_post_errors(M_ERROR, _("Unable to read private key from file"));
549       return false;
550    }
551
552    return true;
553 }
554
555 /*
556  * Free memory associated with a keypair object.
557  */
558 void crypto_keypair_free (X509_KEYPAIR *keypair)
559 {
560    if (keypair->pubkey) {
561       EVP_PKEY_free(keypair->pubkey);
562    }
563    if (keypair->privkey) {
564       EVP_PKEY_free(keypair->privkey);
565    }
566    if (keypair->keyid) {
567       M_ASN1_OCTET_STRING_free(keypair->keyid);
568    }
569    free(keypair);
570 }
571
572 /*
573  * Create a new message digest context of the specified type
574  *  Returns: A pointer to a DIGEST object on success.
575  *           NULL on failure.
576  */
577 DIGEST *crypto_digest_new (crypto_digest_t type)
578 {
579    DIGEST *digest;
580    const EVP_MD *md = NULL; /* Quell invalid uninitialized warnings */
581
582    digest = (DIGEST *) malloc(sizeof(DIGEST));
583    digest->type = type;
584
585    /* Initialize the OpenSSL message digest context */
586    EVP_MD_CTX_init(&digest->ctx);
587
588    /* Determine the correct OpenSSL message digest type */
589    switch (type) {
590    case CRYPTO_DIGEST_MD5:
591       md = EVP_md5();
592       break;
593    case CRYPTO_DIGEST_SHA1:
594       md = EVP_sha1();
595       break;
596 #ifdef HAVE_SHA2
597    case CRYPTO_DIGEST_SHA256:
598       md = EVP_sha256();
599       break;
600    case CRYPTO_DIGEST_SHA512:
601       md = EVP_sha512();
602       break;
603 #endif
604    default:
605       Emsg1(M_ERROR, 0, _("Unsupported digest type: %d\n"), type);
606       goto err;
607    }
608
609    /* Initialize the backing OpenSSL context */
610    if (EVP_DigestInit_ex(&digest->ctx, md, NULL) == 0) {
611       goto err;
612    }
613
614    return digest;
615
616 err:
617    /* This should not happen, but never say never ... */
618    openssl_post_errors(M_ERROR, _("OpenSSL digest initialization failed"));
619    crypto_digest_free(digest);
620    return NULL;
621 }
622
623 /*
624  * Hash length bytes of data into the provided digest context.
625  * Returns: true on success
626  *          false on failure
627  */
628 bool crypto_digest_update (DIGEST *digest, const void *data, size_t length) {
629    if (EVP_DigestUpdate(&digest->ctx, data, length) == 0) {
630       return true;
631    } else { 
632       return false;
633    }
634 }
635
636 /*
637  * Finalize the data in digest, storing the result in dest and the result size
638  * in length. The result size can be determined with crypto_digest_size().
639  *
640  * Returns: true on success
641  *          false on failure
642  */
643 bool crypto_digest_finalize (DIGEST *digest, void *dest, size_t *length) {
644    if (!EVP_DigestFinal(&digest->ctx, (unsigned char *) dest, (unsigned int *) length)) {
645       return false;
646    } else {
647       return true;
648    }
649 }
650
651 /*
652  * Free memory associated with a digest object.
653  */
654 void crypto_digest_free (DIGEST *digest)
655 {
656   EVP_MD_CTX_cleanup(&digest->ctx);
657   free (digest);
658 }
659
660 /*
661  * Create a new message signature context.
662  *  Returns: A pointer to a SIGNATURE object on success.
663  *           NULL on failure.
664  */
665 SIGNATURE *crypto_sign_new (void)
666 {
667    SIGNATURE *sig;
668
669    sig = (SIGNATURE *) malloc(sizeof(SIGNATURE));
670    if (!sig) {
671       return NULL;
672    }
673
674    sig->sigData = SignatureData_new();
675
676    if (!sig->sigData) {
677       /* Allocation failed in OpenSSL */
678       free(sig);
679       return NULL;
680    }
681
682    /* Set the ASN.1 structure version number */
683    ASN1_INTEGER_set(sig->sigData->version, BACULA_ASN1_VERSION);
684
685    return sig;
686 }
687
688 /*
689  * For a given public key, find the associated SignatureInfo record
690  * and create a digest context for signature validation
691  * Returns: CRYPTO_ERROR_NONE on success, with the newly allocated DIGEST in digest.
692  *          A crypto_error_t value on failure.
693  */
694 crypto_error_t crypto_sign_get_digest(SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST **digest)
695 {
696    STACK_OF(SignerInfo) *signers;
697    SignerInfo *si;
698    int i;
699
700    signers = sig->sigData->signerInfo;
701
702    for (i = 0; i < sk_SignerInfo_num(signers); i++) {
703       si = sk_SignerInfo_value(signers, i);
704       if (M_ASN1_OCTET_STRING_cmp(keypair->keyid, si->subjectKeyIdentifier) == 0) {
705          /* Get the digest algorithm and allocate a digest context */
706          switch (OBJ_obj2nid(si->digestAlgorithm)) {
707          case NID_md5:
708             *digest = crypto_digest_new(CRYPTO_DIGEST_MD5);
709             break;
710          case NID_sha1:
711             *digest = crypto_digest_new(CRYPTO_DIGEST_SHA1);
712             break;
713 #ifdef HAVE_SHA2
714          case NID_sha256:
715             *digest = crypto_digest_new(CRYPTO_DIGEST_SHA256);
716             break;
717          case NID_sha512:
718             *digest = crypto_digest_new(CRYPTO_DIGEST_SHA512);
719             break;
720 #endif
721          default:
722             *digest = NULL;
723             return CRYPTO_ERROR_INVALID_DIGEST;
724          }
725
726          /* Shouldn't happen */
727          if (*digest == NULL) {
728             return CRYPTO_ERROR_INVALID_DIGEST;
729          } else {
730             return CRYPTO_ERROR_NONE;
731          }
732       }
733    }
734
735    return CRYPTO_ERROR_NOSIGNER;
736 }
737
738 /*
739  * For a given signature, public key, and digest, verify the SIGNATURE.
740  * Returns: CRYPTO_ERROR_NONE on success.
741  *          A crypto_error_t value on failure.
742  */
743 crypto_error_t crypto_sign_verify(SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST *digest)
744 {
745    STACK_OF(SignerInfo) *signers;
746    SignerInfo *si;
747    int ok, i;
748    unsigned int sigLen;
749 #if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
750    const unsigned char *sigData;
751 #else
752    unsigned char *sigData;
753 #endif
754
755    signers = sig->sigData->signerInfo;
756
757    /* Find the signer */
758    for (i = 0; i < sk_SignerInfo_num(signers); i++) {
759       si = sk_SignerInfo_value(signers, i);
760       if (M_ASN1_OCTET_STRING_cmp(keypair->keyid, si->subjectKeyIdentifier) == 0) {
761          /* Extract the signature data */
762          sigLen = M_ASN1_STRING_length(si->signature);
763          sigData = M_ASN1_STRING_data(si->signature);
764
765          ok = EVP_VerifyFinal(&digest->ctx, sigData, sigLen, keypair->pubkey);
766          if (ok >= 1) {
767             return CRYPTO_ERROR_NONE;
768          } else if (ok == 0) {
769             return CRYPTO_ERROR_BAD_SIGNATURE;
770          } else if (ok < 0) {
771             /* Shouldn't happen */
772             openssl_post_errors(M_ERROR, _("OpenSSL error occured"));
773             return CRYPTO_ERROR_INTERNAL;
774          }
775       }
776    }
777
778    /* Signer wasn't found. */
779    return CRYPTO_ERROR_NOSIGNER;
780 }
781
782
783 /*
784  * Add a new signer
785  *  Returns: true on success
786  *           false on failure
787  */
788 int crypto_sign_add_signer(SIGNATURE *sig, DIGEST *digest, X509_KEYPAIR *keypair)
789 {
790    SignerInfo *si = NULL;
791    unsigned char *buf = NULL;
792    unsigned int len;
793
794    si = SignerInfo_new();
795
796    if (!si) {
797       /* Allocation failed in OpenSSL */
798       return false;
799    }
800
801    /* Set the ASN.1 structure version number */
802    ASN1_INTEGER_set(si->version, BACULA_ASN1_VERSION);
803
804    /* Set the digest algorithm identifier */
805    switch (digest->type) {
806    case CRYPTO_DIGEST_MD5:
807       si->digestAlgorithm = OBJ_nid2obj(NID_md5);
808       break;
809    case CRYPTO_DIGEST_SHA1:
810       si->digestAlgorithm = OBJ_nid2obj(NID_sha1);
811       break;
812 #ifdef HAVE_SHA2
813    case CRYPTO_DIGEST_SHA256:
814       si->digestAlgorithm = OBJ_nid2obj(NID_sha256);
815       break;
816    case CRYPTO_DIGEST_SHA512:
817       si->digestAlgorithm = OBJ_nid2obj(NID_sha512);
818       break;
819 #endif
820    default:
821       /* This should never happen */
822       goto err;
823    }
824
825    /* Drop the string allocated by OpenSSL, and add our subjectKeyIdentifier */
826    M_ASN1_OCTET_STRING_free(si->subjectKeyIdentifier);
827    si->subjectKeyIdentifier = M_ASN1_OCTET_STRING_dup(keypair->keyid);
828
829    /* Set our signature algorithm. We currently require RSA */
830    assert(EVP_PKEY_type(keypair->pubkey->type) == EVP_PKEY_RSA);
831    /* This is slightly evil. Reach into the MD structure and grab the key type */
832    si->signatureAlgorithm = OBJ_nid2obj(digest->ctx.digest->pkey_type);
833
834    /* Finalize/Sign our Digest */
835    len = EVP_PKEY_size(keypair->privkey);
836    buf = (unsigned char *) malloc(len);
837    if (!EVP_SignFinal(&digest->ctx, buf, &len, keypair->privkey)) {
838       openssl_post_errors(M_ERROR, _("Signature creation failed"));
839       goto err;
840    }
841
842    /* Add the signature to the SignerInfo structure */
843    if (!M_ASN1_OCTET_STRING_set(si->signature, buf, len)) {
844       /* Allocation failed in OpenSSL */
845       goto err;
846    }
847
848    /* No longer needed */
849    free(buf);
850
851    /* Push the new SignerInfo structure onto the stack */
852    sk_SignerInfo_push(sig->sigData->signerInfo, si);
853
854    return true;
855
856 err:
857    if (si) {
858       SignerInfo_free(si);
859    }
860    if (buf) {
861       free(buf);
862    }
863
864    return false;
865 }
866
867 /*
868  * Encodes the SignatureData structure. The length argument is used to specify the
869  * size of dest. A length of 0 will cause no data to be written to dest, and the
870  * required length to be written to length. The caller can then allocate sufficient
871  * space for the output.
872  *
873  * Returns: true on success, stores the encoded data in dest, and the size in length.
874  *          false on failure.
875  */
876 int crypto_sign_encode(SIGNATURE *sig, void *dest, size_t *length)
877 {
878    if (*length == 0) {
879       *length = i2d_SignatureData(sig->sigData, NULL);
880       return true;
881    }
882
883    *length = i2d_SignatureData(sig->sigData, (unsigned char **) &dest);
884    return true;
885 }
886
887 /*
888  * Decodes the SignatureData structure. The length argument is used to specify the
889  * size of sigData.
890  *
891  * Returns: SIGNATURE instance on success.
892  *          NULL on failure.
893
894  */
895
896 SIGNATURE *crypto_sign_decode(const void *sigData, size_t length)
897 {
898    SIGNATURE *sig;
899 #if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
900    const unsigned char *p = (const unsigned char *) sigData;
901 #else
902    unsigned char *p = (unsigned char *) sigData;
903 #endif
904
905    sig = (SIGNATURE *) malloc(sizeof(SIGNATURE));
906    if (!sig) {
907       return NULL;
908    }
909
910    /* d2i_SignatureData modifies the supplied pointer */
911    sig->sigData  = d2i_SignatureData(NULL, &p, length);
912
913    if (!sig->sigData) {
914       /* Allocation / Decoding failed in OpenSSL */
915       openssl_post_errors(M_ERROR, _("Signature decoding failed"));
916       return NULL;
917    }
918
919    return sig;
920 }
921
922 /*
923  * Free memory associated with a signature object.
924  */
925 void crypto_sign_free(SIGNATURE *sig)
926 {
927    SignatureData_free(sig->sigData);
928    free (sig);
929 }
930
931 /*
932  * Create a new encryption session.
933  *  Returns: A pointer to a CRYPTO_SESSION object on success.
934  *           NULL on failure.
935  */
936 CRYPTO_SESSION *crypto_session_new (crypto_cipher_t cipher, alist *pubkeys)
937 {
938    CRYPTO_SESSION *cs;
939    X509_KEYPAIR *keypair;
940    const EVP_CIPHER *ec;
941    unsigned char *iv;
942    int iv_len;
943
944    /* Allocate our session description structures */
945    cs = (CRYPTO_SESSION *) malloc(sizeof(CRYPTO_SESSION));
946    if (!cs) {
947       return NULL;
948    }
949
950    /* Initialize required fields */
951    cs->session_key = NULL;
952
953    /* Allocate a CryptoData structure */
954    cs->cryptoData = CryptoData_new();
955
956    if (!cs->cryptoData) {
957       /* Allocation failed in OpenSSL */
958       free(cs);
959       return NULL;
960    }
961
962    /* Set the ASN.1 structure version number */
963    ASN1_INTEGER_set(cs->cryptoData->version, BACULA_ASN1_VERSION);
964
965    /*
966     * Acquire a cipher instance and set the ASN.1 cipher NID
967     */
968    switch (cipher) {
969    case CRYPTO_CIPHER_AES_128_CBC:
970       /* AES 128 bit CBC */
971       cs->cryptoData->contentEncryptionAlgorithm = OBJ_nid2obj(NID_aes_128_cbc);
972       ec = EVP_aes_128_cbc();
973       break;
974    case CRYPTO_CIPHER_AES_192_CBC:
975       /* AES 192 bit CBC */
976       cs->cryptoData->contentEncryptionAlgorithm = OBJ_nid2obj(NID_aes_192_cbc);
977       ec = EVP_aes_192_cbc();
978       break;
979    case CRYPTO_CIPHER_AES_256_CBC:
980       /* AES 256 bit CBC */
981       cs->cryptoData->contentEncryptionAlgorithm = OBJ_nid2obj(NID_aes_256_cbc);
982       ec = EVP_aes_256_cbc();
983       break;
984    case CRYPTO_CIPHER_BLOWFISH_CBC:
985       /* Blowfish CBC */
986       cs->cryptoData->contentEncryptionAlgorithm = OBJ_nid2obj(NID_bf_cbc);
987       ec = EVP_bf_cbc();
988       break;
989    default:
990       Emsg0(M_ERROR, 0, _("Unsupported cipher type specified\n"));
991       crypto_session_free(cs);
992       return NULL;
993    }
994
995    /* Generate a symmetric session key */
996    cs->session_key_len = EVP_CIPHER_key_length(ec);
997    cs->session_key = (unsigned char *) malloc(cs->session_key_len);
998    if (RAND_bytes(cs->session_key, cs->session_key_len) <= 0) {
999       /* OpenSSL failure */
1000       crypto_session_free(cs);
1001       return NULL;
1002    }
1003
1004    /* Generate an IV if possible */
1005    if ((iv_len = EVP_CIPHER_iv_length(ec))) {
1006       iv = (unsigned char *) malloc(iv_len);
1007       if (!iv) {
1008          /* Malloc failure */
1009          crypto_session_free(cs);
1010          return NULL;
1011       }
1012
1013       /* Generate random IV */
1014       if (RAND_bytes(iv, iv_len) <= 0) {
1015          /* OpenSSL failure */
1016          crypto_session_free(cs);
1017          free(iv);
1018          return NULL;
1019       }
1020
1021       /* Store it in our ASN.1 structure */
1022       if (!M_ASN1_OCTET_STRING_set(cs->cryptoData->iv, iv, iv_len)) {
1023          /* Allocation failed in OpenSSL */
1024          crypto_session_free(cs);
1025          free(iv);
1026          return NULL;
1027       }
1028       free(iv);
1029    }
1030
1031    /*
1032     * Create RecipientInfo structures for supplied
1033     * public keys.
1034     */
1035    foreach_alist(keypair, pubkeys) {
1036       RecipientInfo *ri;
1037       unsigned char *ekey;
1038       int ekey_len;
1039
1040       ri = RecipientInfo_new();
1041       if (!ri) {
1042          /* Allocation failed in OpenSSL */
1043          crypto_session_free(cs);
1044          return NULL;
1045       }
1046
1047       /* Set the ASN.1 structure version number */
1048       ASN1_INTEGER_set(ri->version, BACULA_ASN1_VERSION);
1049
1050       /* Drop the string allocated by OpenSSL, and add our subjectKeyIdentifier */
1051       M_ASN1_OCTET_STRING_free(ri->subjectKeyIdentifier);
1052       ri->subjectKeyIdentifier = M_ASN1_OCTET_STRING_dup(keypair->keyid);
1053
1054       /* Set our key encryption algorithm. We currently require RSA */
1055       assert(keypair->pubkey && EVP_PKEY_type(keypair->pubkey->type) == EVP_PKEY_RSA);
1056       ri->keyEncryptionAlgorithm = OBJ_nid2obj(NID_rsaEncryption);
1057
1058       /* Encrypt the session key */
1059       ekey = (unsigned char *) malloc(EVP_PKEY_size(keypair->pubkey));
1060       if (!ekey) {
1061          RecipientInfo_free(ri);
1062          crypto_session_free(cs);
1063          return NULL;
1064       }
1065
1066       if ((ekey_len = EVP_PKEY_encrypt(ekey, cs->session_key, cs->session_key_len, keypair->pubkey)) <= 0) {
1067          /* OpenSSL failure */
1068          RecipientInfo_free(ri);
1069          crypto_session_free(cs);
1070          free(ekey);
1071          return NULL;
1072       }
1073
1074       /* Store it in our ASN.1 structure */
1075       if (!M_ASN1_OCTET_STRING_set(ri->encryptedKey, ekey, ekey_len)) {
1076          /* Allocation failed in OpenSSL */
1077          RecipientInfo_free(ri);
1078          crypto_session_free(cs);
1079          free(ekey);
1080          return NULL;
1081       }
1082
1083       /* Free the encrypted key buffer */
1084       free(ekey);
1085
1086       /* Push the new RecipientInfo structure onto the stack */
1087       sk_RecipientInfo_push(cs->cryptoData->recipientInfo, ri);
1088    }
1089
1090    return cs;
1091 }
1092
1093 /*
1094  * Encodes the CryptoData structure. The length argument is used to specify the
1095  * size of dest. A length of 0 will cause no data to be written to dest, and the
1096  * required length to be written to length. The caller can then allocate sufficient
1097  * space for the output.
1098  *
1099  * Returns: true on success, stores the encoded data in dest, and the size in length.
1100  *          false on failure.
1101  */
1102 bool crypto_session_encode(CRYPTO_SESSION *cs, void *dest, size_t *length)
1103 {
1104    if (*length == 0) {
1105       *length = i2d_CryptoData(cs->cryptoData, NULL);
1106       return true;
1107    }
1108
1109    *length = i2d_CryptoData(cs->cryptoData, (unsigned char **) &dest);
1110    return true;
1111 }
1112
1113 /*
1114  * Decodes the CryptoData structure. The length argument is
1115  * used to specify the size of data.
1116  *
1117  * Returns: CRYPTO_SESSION instance on success.
1118  *          NULL on failure.
1119  * Returns: CRYPTO_ERROR_NONE and a pointer to a newly allocated CRYPTO_SESSION structure in *session on success.
1120  *          A crypto_error_t value on failure.
1121  */
1122 crypto_error_t crypto_session_decode(const void *data, size_t length, alist *keypairs, CRYPTO_SESSION **session)
1123 {
1124    CRYPTO_SESSION *cs;
1125    X509_KEYPAIR *keypair;
1126    STACK_OF(RecipientInfo) *recipients;
1127    crypto_error_t retval = CRYPTO_ERROR_NONE;
1128 #if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
1129    const unsigned char *p = (const unsigned char *) data;
1130 #else
1131    unsigned char *p = (unsigned char *) data;
1132 #endif
1133
1134    cs = (CRYPTO_SESSION *) malloc(sizeof(CRYPTO_SESSION));
1135    if (!cs) {
1136       return CRYPTO_ERROR_INTERNAL;
1137    }
1138
1139    /* d2i_CryptoData modifies the supplied pointer */
1140    cs->cryptoData = d2i_CryptoData(NULL, &p, length);
1141
1142    if (!cs->cryptoData) {
1143       /* Allocation / Decoding failed in OpenSSL */
1144       openssl_post_errors(M_ERROR, _("CryptoData decoding failed"));
1145       retval = CRYPTO_ERROR_INTERNAL;
1146       goto err;
1147    }
1148
1149    recipients = cs->cryptoData->recipientInfo;
1150
1151    /*
1152     * Find a matching RecipientInfo structure for a supplied
1153     * public key
1154     */
1155    foreach_alist(keypair, keypairs) {
1156       RecipientInfo *ri;
1157       int i;
1158
1159       /* Private key available? */
1160       if (keypair->privkey == NULL) {
1161          continue;
1162       }
1163
1164       for (i = 0; i < sk_RecipientInfo_num(recipients); i++) {
1165          ri = sk_RecipientInfo_value(recipients, i);
1166
1167          /* Match against the subjectKeyIdentifier */
1168          if (M_ASN1_OCTET_STRING_cmp(keypair->keyid, ri->subjectKeyIdentifier) == 0) {
1169             /* Match found, extract symmetric encryption session data */
1170             
1171             /* RSA is required. */
1172             assert(EVP_PKEY_type(keypair->privkey->type) == EVP_PKEY_RSA);
1173
1174             /* If we recieve a RecipientInfo structure that does not use
1175              * RSA, return an error */
1176             if (OBJ_obj2nid(ri->keyEncryptionAlgorithm) != NID_rsaEncryption) {
1177                retval = CRYPTO_ERROR_INVALID_CRYPTO;
1178                goto err;
1179             }
1180
1181             /* Decrypt the session key */
1182             /* Allocate sufficient space for the largest possible decrypted data */
1183             cs->session_key = (unsigned char *) malloc(EVP_PKEY_size(keypair->privkey));
1184             cs->session_key_len = EVP_PKEY_decrypt(cs->session_key, M_ASN1_STRING_data(ri->encryptedKey),
1185                                   M_ASN1_STRING_length(ri->encryptedKey), keypair->privkey);
1186
1187             if (cs->session_key_len <= 0) {
1188                openssl_post_errors(M_ERROR, _("Failure decrypting the session key"));
1189                retval = CRYPTO_ERROR_DECRYPTION;
1190                goto err;
1191             }
1192
1193             /* Session key successfully extracted, return the CRYPTO_SESSION structure */
1194             *session = cs;
1195             return CRYPTO_ERROR_NONE;
1196          }
1197       }
1198    }
1199
1200    /* No matching recipient found */
1201    return CRYPTO_ERROR_NORECIPIENT;
1202
1203 err:
1204    crypto_session_free(cs);
1205    return retval;
1206 }
1207
1208 /*
1209  * Free memory associated with a crypto session object.
1210  */
1211 void crypto_session_free (CRYPTO_SESSION *cs)
1212 {
1213    if (cs->cryptoData) {
1214       CryptoData_free(cs->cryptoData);
1215    }
1216    if (cs->session_key){
1217       free(cs->session_key);
1218    }
1219    free(cs);
1220 }
1221
1222 /*
1223  * Create a new crypto cipher context with the specified session object
1224  *  Returns: A pointer to a CIPHER_CONTEXT object on success. The cipher block size is returned in blocksize.
1225  *           NULL on failure.
1226  */
1227 CIPHER_CONTEXT *crypto_cipher_new (CRYPTO_SESSION *cs, bool encrypt, size_t *blocksize)
1228 {
1229    CIPHER_CONTEXT *cipher_ctx;
1230    const EVP_CIPHER *ec;
1231
1232    cipher_ctx = (CIPHER_CONTEXT *) malloc(sizeof(CIPHER_CONTEXT));
1233    if (!cipher_ctx) {
1234       return NULL;
1235    }
1236
1237    /*
1238     * Acquire a cipher instance for the given ASN.1 cipher NID
1239     */
1240    if ((ec = EVP_get_cipherbyobj(cs->cryptoData->contentEncryptionAlgorithm)) == NULL) {
1241       Emsg1(M_ERROR, 0, _("Unsupported contentEncryptionAlgorithm: %d\n"), OBJ_obj2nid(cs->cryptoData->contentEncryptionAlgorithm));
1242       crypto_cipher_free(cipher_ctx);
1243       return NULL;
1244    }
1245
1246    /* Initialize the OpenSSL cipher context */
1247    EVP_CIPHER_CTX_init(&cipher_ctx->ctx);
1248    if (encrypt) {
1249       /* Initialize for encryption */
1250       if (!EVP_CipherInit_ex(&cipher_ctx->ctx, ec, NULL, NULL, NULL, 1)) {
1251          openssl_post_errors(M_ERROR, _("OpenSSL cipher context initialization failed"));
1252          goto err;
1253       }
1254    } else {
1255       /* Initialize for decryption */
1256       if (!EVP_CipherInit_ex(&cipher_ctx->ctx, ec, NULL, NULL, NULL, 0)) {
1257          openssl_post_errors(M_ERROR, _("OpenSSL cipher context initialization failed"));
1258          goto err;
1259       }
1260    }
1261
1262    /* Set the key size */
1263    if (!EVP_CIPHER_CTX_set_key_length(&cipher_ctx->ctx, cs->session_key_len)) {
1264       openssl_post_errors(M_ERROR, _("Encryption session provided an invalid symmetric key"));
1265       goto err;
1266    }
1267
1268    /* Validate the IV length */
1269    if (EVP_CIPHER_iv_length(ec) != M_ASN1_STRING_length(cs->cryptoData->iv)) {
1270       openssl_post_errors(M_ERROR, _("Encryption session provided an invalid IV"));
1271       goto err;
1272    }
1273    
1274    /* Add the key and IV to the cipher context */
1275    if (!EVP_CipherInit_ex(&cipher_ctx->ctx, NULL, NULL, cs->session_key, M_ASN1_STRING_data(cs->cryptoData->iv), -1)) {
1276       openssl_post_errors(M_ERROR, _("OpenSSL cipher context key/IV initialization failed"));
1277       goto err;
1278    }
1279
1280    *blocksize = EVP_CIPHER_CTX_block_size(&cipher_ctx->ctx);
1281    return cipher_ctx;
1282
1283 err:
1284    crypto_cipher_free(cipher_ctx);
1285    return NULL;
1286 }
1287
1288
1289 /*
1290  * Encrypt/Decrypt length bytes of data using the provided cipher context
1291  * Returns: true on success, number of bytes output in written
1292  *          false on failure
1293  */
1294 bool crypto_cipher_update (CIPHER_CONTEXT *cipher_ctx, const void *data, size_t length, const void *dest, size_t *written) {
1295    if (!EVP_CipherUpdate(&cipher_ctx->ctx, (unsigned char *) dest, (int *) written, (const unsigned char *) data, length)) {
1296       /* This really shouldn't fail */
1297       return false;
1298    } else {
1299       return true;
1300    }
1301 }
1302
1303 /*
1304  * Finalize the cipher context, writing any remaining data and necessary padding
1305  * to dest, and the size in written.
1306  * The result size will either be one block of data or zero.
1307  *
1308  * Returns: true on success
1309  *          false on failure
1310  */
1311 bool crypto_cipher_finalize (CIPHER_CONTEXT *cipher_ctx, void *dest, size_t *written) {
1312    if (!EVP_CipherFinal_ex(&cipher_ctx->ctx, (unsigned char *) dest, (int *) written)) {
1313       /* This really shouldn't fail */
1314       return false;
1315    } else {
1316       return true;
1317    }
1318 }
1319
1320
1321 /*
1322  * Free memory associated with a cipher context.
1323  */
1324 void crypto_cipher_free (CIPHER_CONTEXT *cipher_ctx)
1325 {
1326    EVP_CIPHER_CTX_cleanup(&cipher_ctx->ctx);
1327    free (cipher_ctx);
1328 }
1329
1330
1331 /*
1332  * Perform global initialization of OpenSSL
1333  * This function is not thread safe.
1334  *  Returns: 0 on success
1335  *           errno on failure
1336  */
1337 int init_crypto (void)
1338 {
1339    int stat;
1340
1341    if ((stat = openssl_init_threads()) != 0) {
1342       Emsg1(M_ABORT, 0, _("Unable to init OpenSSL threading: ERR=%s\n"), strerror(stat));
1343    }
1344
1345    /* Load libssl and libcrypto human-readable error strings */
1346    SSL_load_error_strings();
1347
1348    /* Register OpenSSL ciphers */
1349    SSL_library_init();
1350
1351    if (!openssl_seed_prng()) {
1352       Emsg0(M_ERROR_TERM, 0, _("Failed to seed OpenSSL PRNG\n"));
1353    }
1354
1355    crypto_initialized = true;
1356
1357    return stat;
1358 }
1359
1360 /*
1361  * Perform global cleanup of OpenSSL
1362  * All cryptographic operations must be completed before calling this function.
1363  * This function is not thread safe.
1364  *  Returns: 0 on success
1365  *           errno on failure
1366  */
1367 int cleanup_crypto (void)
1368 {
1369    /*
1370     * Ensure that we've actually been initialized; Doing this here decreases the
1371     * complexity of client's termination/cleanup code.
1372     */
1373    if (!crypto_initialized) {
1374       return 0;
1375    }
1376
1377    if (!openssl_save_prng()) {
1378       Emsg0(M_ERROR, 0, _("Failed to save OpenSSL PRNG\n"));
1379    }
1380
1381    openssl_cleanup_threads();
1382
1383    /* Free libssl and libcrypto error strings */
1384    ERR_free_strings();
1385
1386    /* Free memory used by PRNG */
1387    RAND_cleanup();
1388
1389    crypto_initialized = false;
1390
1391    return 0;
1392 }
1393
1394
1395 #else /* HAVE_OPENSSL */
1396 # error No encryption library available
1397 #endif /* HAVE_OPENSSL */
1398
1399 #else /* HAVE_CRYPTO */
1400
1401 /*
1402  * Cryptography Support Disabled
1403  */
1404
1405 /* Message Digest Structure */
1406 struct Digest {
1407    crypto_digest_t type;
1408    union {
1409       SHA1Context sha1;
1410       MD5Context md5;
1411    };
1412 };
1413
1414 /* Dummy Signature Structure */
1415 struct Signature {
1416 };
1417
1418 DIGEST *crypto_digest_new (crypto_digest_t type)
1419 {
1420    DIGEST *digest;
1421
1422    digest = (DIGEST *) malloc(sizeof(DIGEST));
1423    digest->type = type;
1424
1425    switch (type) {
1426    case CRYPTO_DIGEST_MD5:
1427       MD5Init(&digest->md5);
1428       break;
1429    case CRYPTO_DIGEST_SHA1:
1430       SHA1Init(&digest->sha1);
1431       break;
1432    default:
1433       Emsg0(M_ERROR, 0, _("Unsupported digest type specified\n"));
1434       free(digest);
1435       return NULL;
1436    }
1437
1438    return (digest);
1439 }
1440
1441 bool crypto_digest_update (DIGEST *digest, const void *data, size_t length) {
1442    switch (digest->type) {
1443    case CRYPTO_DIGEST_MD5:
1444       /* Doesn't return anything ... */
1445       MD5Update(&digest->md5, (unsigned char *) data, length);
1446       return true;
1447    case CRYPTO_DIGEST_SHA1:
1448       int ret;
1449       if ((ret = SHA1Update(&digest->sha1, (const u_int8_t *) data, length)) == shaSuccess) {
1450          return true;
1451       } else {
1452          Emsg1(M_ERROR, 0, _("SHA1Update() returned an error: %d\n"), ret);
1453          return false;
1454       }
1455       break;
1456    default:
1457       return false;
1458    }
1459 }
1460
1461 bool crypto_digest_finalize (DIGEST *digest, void *dest, size_t *length) {
1462
1463    switch (digest->type) {
1464    case CRYPTO_DIGEST_MD5:
1465       /* Guard against programmer error by either the API client or
1466        * an out-of-sync CRYPTO_DIGEST_MAX_SIZE */
1467       assert(*length >= CRYPTO_DIGEST_MD5_SIZE);
1468       *length = CRYPTO_DIGEST_MD5_SIZE;
1469       /* Doesn't return anything ... */
1470       MD5Final((unsigned char *) dest, &digest->md5);
1471       return true;
1472    case CRYPTO_DIGEST_SHA1:
1473       /* Guard against programmer error by either the API client or
1474        * an out-of-sync CRYPTO_DIGEST_MAX_SIZE */
1475       assert(*length >= CRYPTO_DIGEST_SHA1_SIZE);
1476       *length = CRYPTO_DIGEST_SHA1_SIZE;
1477       if (SHA1Final(&digest->sha1, (u_int8_t *) dest) == shaSuccess) {
1478          return true;
1479       } else {
1480          return false;
1481       }
1482       break;
1483    default:
1484       return false;
1485    }
1486
1487    return false;
1488 }
1489
1490 void crypto_digest_free (DIGEST *digest)
1491 {
1492    free (digest);
1493 }
1494
1495 /* Dummy routines */
1496 int init_crypto (void) { return 0; }
1497 int cleanup_crypto (void) { return 0; }
1498
1499 SIGNATURE *crypto_sign_new (void) { return NULL; }
1500
1501 crypto_error_t crypto_sign_get_digest (SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST **digest) { return CRYPTO_ERROR_INTERNAL; }
1502 crypto_error_t crypto_sign_verify (SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST *digest) { return CRYPTO_ERROR_INTERNAL; }
1503
1504 int crypto_sign_add_signer (SIGNATURE *sig, DIGEST *digest, X509_KEYPAIR *keypair) { return false; }
1505 int crypto_sign_encode (SIGNATURE *sig, void *dest, size_t *length) { return false; }
1506
1507 SIGNATURE *crypto_sign_decode (const void *sigData, size_t length) { return NULL; }
1508 void crypto_sign_free (SIGNATURE *sig) { }
1509
1510
1511 X509_KEYPAIR *crypto_keypair_new (void) { return NULL; }
1512 X509_KEYPAIR *crypto_keypair_dup (X509_KEYPAIR *keypair) { return NULL; }
1513 int crypto_keypair_load_cert (X509_KEYPAIR *keypair, const char *file) { return false; }
1514 bool crypto_keypair_has_key (const char *file) { return false; }
1515 int crypto_keypair_load_key (X509_KEYPAIR *keypair, const char *file, CRYPTO_PEM_PASSWD_CB *pem_callback, const void *pem_userdata) { return false; }
1516 void crypto_keypair_free (X509_KEYPAIR *keypair) { }
1517
1518 CRYPTO_SESSION *crypto_session_new (crypto_cipher_t cipher, alist *pubkeys) { return NULL; }
1519 void crypto_session_free (CRYPTO_SESSION *cs) { }
1520 bool crypto_session_encode (CRYPTO_SESSION *cs, void *dest, size_t *length) { return false; }
1521 crypto_error_t crypto_session_decode (const void *data, size_t length, alist *keypairs, CRYPTO_SESSION **session) { return CRYPTO_ERROR_INTERNAL; }
1522
1523 CIPHER_CONTEXT *crypto_cipher_new (CRYPTO_SESSION *cs, bool encrypt, size_t *blocksize) { return NULL; }
1524 bool crypto_cipher_update (CIPHER_CONTEXT *cipher_ctx, const void *data, size_t length, const void *dest, size_t *written) { return false; }
1525 bool crypto_cipher_finalize (CIPHER_CONTEXT *cipher_ctx, void *dest, size_t *written) { return false; }
1526 void crypto_cipher_free (CIPHER_CONTEXT *cipher_ctx) { }
1527
1528 #endif /* HAVE_CRYPTO */
1529
1530 /* Shared Code */
1531
1532 /*
1533  * Default PEM encryption passphrase callback.
1534  * Returns an empty password.
1535  */
1536 int crypto_default_pem_callback(char *buf, int size, const void *userdata)
1537 {
1538    bstrncpy(buf, "", size);
1539    return (strlen(buf));
1540 }
1541
1542 /*
1543  * Returns the ASCII name of the digest type.
1544  * Returns: ASCII name of digest type.
1545  */
1546 const char *crypto_digest_name (DIGEST *digest) {
1547    switch (digest->type) {
1548    case CRYPTO_DIGEST_MD5:
1549       return "MD5";
1550    case CRYPTO_DIGEST_SHA1:
1551       return "SHA1";
1552    case CRYPTO_DIGEST_SHA256:
1553       return "SHA256";
1554    case CRYPTO_DIGEST_SHA512:
1555       return "SHA512";
1556    case CRYPTO_DIGEST_NONE:
1557       return "None";
1558    default:
1559       return "Invalid Digest Type";
1560    }
1561
1562 }
1563
1564 /*
1565  * Given a stream type, returns the associated
1566  * crypto_digest_t value.
1567  */
1568 crypto_digest_t crypto_digest_stream_type (int stream) {
1569    switch (stream) {
1570    case STREAM_MD5_DIGEST:
1571       return CRYPTO_DIGEST_MD5;
1572    case STREAM_SHA1_DIGEST:
1573       return CRYPTO_DIGEST_SHA1;
1574    case STREAM_SHA256_DIGEST:
1575       return CRYPTO_DIGEST_SHA256;
1576    case STREAM_SHA512_DIGEST:
1577       return CRYPTO_DIGEST_SHA512;
1578    default:
1579       return CRYPTO_DIGEST_NONE;
1580    }
1581 }
1582
1583 /*
1584  *  * Given a crypto_error_t value, return the associated
1585  *   * error string
1586  *    */
1587 const char *crypto_strerror(crypto_error_t error) {
1588    switch (error) {
1589    case CRYPTO_ERROR_NONE:
1590       return "No error";
1591    case CRYPTO_ERROR_NOSIGNER:
1592       return "Signer not found";
1593    case CRYPTO_ERROR_NORECIPIENT:
1594       return "Recipient not found";
1595    case CRYPTO_ERROR_INVALID_DIGEST:
1596       return "Unsupported digest algorithm";
1597    case CRYPTO_ERROR_INVALID_CRYPTO:
1598       return "Unsupported encryption algorithm";
1599    case CRYPTO_ERROR_BAD_SIGNATURE:
1600       return "Signature is invalid";
1601    case CRYPTO_ERROR_DECRYPTION:
1602       return "Decryption error";
1603    case CRYPTO_ERROR_INTERNAL:
1604       /* This shouldn't happen */
1605       return "Internal error";
1606    default:
1607       return "Unknown error";
1608    }
1609 }