]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/crypto.c
Fix Win32 built to work with new crypto code.
[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  *    recipientInfo               RecipientInfo
78  * }
79  *
80  * SignerInfo ::= SET OF SignerInfo
81  * RecipientInfo ::= SET OF RecipientInfo
82  *
83  * Version ::= INTEGER { v0(0) }
84  *
85  * SignerInfo ::= SEQUENCE {
86  *    version                 Version,
87  *    subjectKeyIdentifier    SubjectKeyIdentifier,
88  *    digestAlgorithm         DigestAlgorithmIdentifier,
89  *    signatureAlgorithm      SignatureAlgorithmIdentifier,
90  *    signature               SignatureValue }
91  *
92  * RecipientInfo ::= SEQUENCE {
93  *    version                 Version
94  *    subjectKeyIdentifier    SubjectKeyIdentifier
95  *    keyEncryptionAlgorithm  KeyEncryptionAlgorithmIdentifier
96  *    encryptedKey            EncryptedKey
97  * }
98  *
99  * SubjectKeyIdentifier ::= OCTET STRING
100  *
101  * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
102  *
103  * SignatureAlgorithmIdentifier ::= AlgorithmIdentifier
104  *
105  * KeyEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
106  *
107  * SignatureValue ::= OCTET STRING
108  *
109  * EncryptedKey ::= OCTET STRING
110  *
111  * AlgorithmIdentifier ::= OBJECT IDENTIFIER
112  *
113  * END
114  */
115
116 #ifdef HAVE_CRYPTO /* Is encryption enabled? */
117 #ifdef HAVE_OPENSSL /* How about OpenSSL? */
118
119 /* Are we initialized? */
120 static int crypto_initialized = false;
121
122 /* ASN.1 Declarations */
123 #define BACULA_ASN1_VERSION 0
124
125 typedef struct {
126    ASN1_INTEGER *version;
127    ASN1_OCTET_STRING *subjectKeyIdentifier;
128    ASN1_OBJECT *digestAlgorithm;
129    ASN1_OBJECT *signatureAlgorithm;
130    ASN1_OCTET_STRING *signature;
131 } SignerInfo;
132
133 typedef struct {
134    ASN1_INTEGER *version;
135    ASN1_OCTET_STRING *subjectKeyIdentifier;
136    ASN1_OBJECT *keyEncryptionAlgorithm;
137    ASN1_OCTET_STRING *encryptedKey;
138 } RecipientInfo;
139
140 ASN1_SEQUENCE(SignerInfo) = {
141    ASN1_SIMPLE(SignerInfo, version, ASN1_INTEGER),
142    ASN1_SIMPLE(SignerInfo, subjectKeyIdentifier, ASN1_OCTET_STRING),
143    ASN1_SIMPLE(SignerInfo, digestAlgorithm, ASN1_OBJECT),
144    ASN1_SIMPLE(SignerInfo, signatureAlgorithm, ASN1_OBJECT),
145    ASN1_SIMPLE(SignerInfo, signature, ASN1_OCTET_STRING)
146 } ASN1_SEQUENCE_END(SignerInfo);
147
148 ASN1_SEQUENCE(RecipientInfo) = {
149    ASN1_SIMPLE(RecipientInfo, version, ASN1_INTEGER),
150    ASN1_SIMPLE(RecipientInfo, subjectKeyIdentifier, ASN1_OCTET_STRING),
151    ASN1_SIMPLE(RecipientInfo, keyEncryptionAlgorithm, ASN1_OBJECT),
152    ASN1_SIMPLE(RecipientInfo, encryptedKey, ASN1_OCTET_STRING),
153 } ASN1_SEQUENCE_END(RecipientInfo);
154
155 typedef struct {
156    ASN1_INTEGER *version;
157    STACK_OF(SignerInfo) *signerInfo;
158 } SignatureData;
159
160 typedef struct {
161    ASN1_INTEGER *version;
162    ASN1_OBJECT *contentEncryptionAlgorithm;
163    STACK_OF(RecipientInfo) *recipientInfo;
164 } CryptoData;
165
166 ASN1_SEQUENCE(SignatureData) = {
167    ASN1_SIMPLE(SignatureData, version, ASN1_INTEGER),
168    ASN1_SET_OF(SignatureData, signerInfo, SignerInfo),
169 } ASN1_SEQUENCE_END(SignatureData);
170
171 ASN1_SEQUENCE(CryptoData) = {
172    ASN1_SIMPLE(CryptoData, version, ASN1_INTEGER),
173    ASN1_SET_OF(CryptoData, recipientInfo, RecipientInfo)
174 } ASN1_SEQUENCE_END(CryptoData);
175
176 IMPLEMENT_ASN1_FUNCTIONS(SignerInfo)
177 IMPLEMENT_ASN1_FUNCTIONS(SignatureData)
178 IMPLEMENT_ASN1_FUNCTIONS(CryptoData)
179 IMPLEMENT_STACK_OF(SignerInfo)
180 IMPLEMENT_STACK_OF(RecipientInfo)
181
182 /*
183  * SignerInfo and RecipientInfo stack macros, generated by OpenSSL's util/mkstack.pl.
184  */
185 #define sk_SignerInfo_new(st) SKM_sk_new(SignerInfo, (st))
186 #define sk_SignerInfo_new_null() SKM_sk_new_null(SignerInfo)
187 #define sk_SignerInfo_free(st) SKM_sk_free(SignerInfo, (st))
188 #define sk_SignerInfo_num(st) SKM_sk_num(SignerInfo, (st))
189 #define sk_SignerInfo_value(st, i) SKM_sk_value(SignerInfo, (st), (i))
190 #define sk_SignerInfo_set(st, i, val) SKM_sk_set(SignerInfo, (st), (i), (val))
191 #define sk_SignerInfo_zero(st) SKM_sk_zero(SignerInfo, (st))
192 #define sk_SignerInfo_push(st, val) SKM_sk_push(SignerInfo, (st), (val))
193 #define sk_SignerInfo_unshift(st, val) SKM_sk_unshift(SignerInfo, (st), (val))
194 #define sk_SignerInfo_find(st, val) SKM_sk_find(SignerInfo, (st), (val))
195 #define sk_SignerInfo_delete(st, i) SKM_sk_delete(SignerInfo, (st), (i))
196 #define sk_SignerInfo_delete_ptr(st, ptr) SKM_sk_delete_ptr(SignerInfo, (st), (ptr))
197 #define sk_SignerInfo_insert(st, val, i) SKM_sk_insert(SignerInfo, (st), (val), (i))
198 #define sk_SignerInfo_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(SignerInfo, (st), (cmp))
199 #define sk_SignerInfo_dup(st) SKM_sk_dup(SignerInfo, st)
200 #define sk_SignerInfo_pop_free(st, free_func) SKM_sk_pop_free(SignerInfo, (st), (free_func))
201 #define sk_SignerInfo_shift(st) SKM_sk_shift(SignerInfo, (st))
202 #define sk_SignerInfo_pop(st) SKM_sk_pop(SignerInfo, (st))
203 #define sk_SignerInfo_sort(st) SKM_sk_sort(SignerInfo, (st))
204 #define sk_SignerInfo_is_sorted(st) SKM_sk_is_sorted(SignerInfo, (st))
205
206 #define d2i_ASN1_SET_OF_SignerInfo(st, pp, length, d2i_func, free_func, ex_tag, ex_class) \
207         SKM_ASN1_SET_OF_d2i(SignerInfo, (st), (pp), (length), (d2i_func), (free_func), (ex_tag), (ex_class)) 
208 #define i2d_ASN1_SET_OF_SignerInfo(st, pp, i2d_func, ex_tag, ex_class, is_set) \
209         SKM_ASN1_SET_OF_i2d(SignerInfo, (st), (pp), (i2d_func), (ex_tag), (ex_class), (is_set))
210 #define ASN1_seq_pack_SignerInfo(st, i2d_func, buf, len) \
211         SKM_ASN1_seq_pack(SignerInfo, (st), (i2d_func), (buf), (len))
212 #define ASN1_seq_unpack_SignerInfo(buf, len, d2i_func, free_func) \
213         SKM_ASN1_seq_unpack(SignerInfo, (buf), (len), (d2i_func), (free_func))
214
215 #define sk_RecipientInfo_new(st) SKM_sk_new(RecipientInfo, (st))
216 #define sk_RecipientInfo_new_null() SKM_sk_new_null(RecipientInfo)
217 #define sk_RecipientInfo_free(st) SKM_sk_free(RecipientInfo, (st))
218 #define sk_RecipientInfo_num(st) SKM_sk_num(RecipientInfo, (st))
219 #define sk_RecipientInfo_value(st, i) SKM_sk_value(RecipientInfo, (st), (i))
220 #define sk_RecipientInfo_set(st, i, val) SKM_sk_set(RecipientInfo, (st), (i), (val))
221 #define sk_RecipientInfo_zero(st) SKM_sk_zero(RecipientInfo, (st))
222 #define sk_RecipientInfo_push(st, val) SKM_sk_push(RecipientInfo, (st), (val))
223 #define sk_RecipientInfo_unshift(st, val) SKM_sk_unshift(RecipientInfo, (st), (val))
224 #define sk_RecipientInfo_find(st, val) SKM_sk_find(RecipientInfo, (st), (val))
225 #define sk_RecipientInfo_delete(st, i) SKM_sk_delete(RecipientInfo, (st), (i))
226 #define sk_RecipientInfo_delete_ptr(st, ptr) SKM_sk_delete_ptr(RecipientInfo, (st), (ptr))
227 #define sk_RecipientInfo_insert(st, val, i) SKM_sk_insert(RecipientInfo, (st), (val), (i))
228 #define sk_RecipientInfo_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(RecipientInfo, (st), (cmp))
229 #define sk_RecipientInfo_dup(st) SKM_sk_dup(RecipientInfo, st)
230 #define sk_RecipientInfo_pop_free(st, free_func) SKM_sk_pop_free(RecipientInfo, (st), (free_func))
231 #define sk_RecipientInfo_shift(st) SKM_sk_shift(RecipientInfo, (st))
232 #define sk_RecipientInfo_pop(st) SKM_sk_pop(RecipientInfo, (st))
233 #define sk_RecipientInfo_sort(st) SKM_sk_sort(RecipientInfo, (st))
234 #define sk_RecipientInfo_is_sorted(st) SKM_sk_is_sorted(RecipientInfo, (st))
235
236 #define d2i_ASN1_SET_OF_RecipientInfo(st, pp, length, d2i_func, free_func, ex_tag, ex_class) \
237         SKM_ASN1_SET_OF_d2i(RecipientInfo, (st), (pp), (length), (d2i_func), (free_func), (ex_tag), (ex_class)) 
238 #define i2d_ASN1_SET_OF_RecipientInfo(st, pp, i2d_func, ex_tag, ex_class, is_set) \
239         SKM_ASN1_SET_OF_i2d(RecipientInfo, (st), (pp), (i2d_func), (ex_tag), (ex_class), (is_set))
240 #define ASN1_seq_pack_RecipientInfo(st, i2d_func, buf, len) \
241         SKM_ASN1_seq_pack(RecipientInfo, (st), (i2d_func), (buf), (len))
242 #define ASN1_seq_unpack_RecipientInfo(buf, len, d2i_func, free_func) \
243         SKM_ASN1_seq_unpack(RecipientInfo, (buf), (len), (d2i_func), (free_func))
244 /* End of util/mkstack.pl block */
245
246 /* X509 Public/Private Key Pair Structure */
247 struct X509_Keypair {
248    ASN1_OCTET_STRING *keyid;
249    EVP_PKEY *pubkey;
250    EVP_PKEY *privkey;
251 };
252
253 /* Message Digest Structure */
254 struct Digest {
255    crypto_digest_t type;
256    EVP_MD_CTX ctx;
257 };
258
259 /* Message Signature Structure */
260 struct Signature {
261    SignatureData *sigData;
262 };
263
264 /* PEM Password Dispatch Context */
265 typedef struct PEM_CB_Context {
266    CRYPTO_PEM_PASSWD_CB *pem_callback;
267    const void *pem_userdata;
268 } PEM_CB_CONTEXT;
269
270 /*
271  * Extract subjectKeyIdentifier from x509 certificate.
272  * Returns: On success, an ASN1_OCTET_STRING that must be freed via M_ASN1_OCTET_STRING_free().
273  *          NULL on failure.
274  */
275 static ASN1_OCTET_STRING *openssl_cert_keyid(X509 *cert){
276    X509_EXTENSION *ext;
277    X509V3_EXT_METHOD *method;
278    ASN1_OCTET_STRING *keyid;
279    int i;
280 #if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
281    const unsigned char *ext_value_data;
282 #else
283    unsigned char *ext_value_data;
284 #endif
285
286
287    /* Find the index to the subjectKeyIdentifier extension */
288    i = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1);
289    if (i < 0) {
290       /* Not found */
291       return NULL;
292    }
293
294    /* Grab the extension */
295    ext = X509_get_ext(cert, i);
296
297    /* Get x509 extension method structure */
298    if (!(method = X509V3_EXT_get(ext))) {
299       return NULL;
300    }
301
302    ext_value_data = ext->value->data;
303
304 #if (OPENSSL_VERSION_NUMBER > 0x00907000L)
305    if (method->it) {
306       /* New style ASN1 */
307
308       /* Decode ASN1 item in data */
309       keyid = (ASN1_OCTET_STRING *) ASN1_item_d2i(NULL, &ext_value_data, ext->value->length,
310                                                   ASN1_ITEM_ptr(method->it));
311    } else {
312       /* Old style ASN1 */
313
314       /* Decode ASN1 item in data */
315       keyid = (ASN1_OCTET_STRING *) method->d2i(NULL, &ext_value_data, ext->value->length);
316    }
317
318 #else
319    keyid = (ASN1_OCTET_STRING *) method->d2i(NULL, &ext_value_data, ext->value->length);
320 #endif
321
322    return keyid;
323 }
324
325 /*
326  * Create a new keypair object.
327  *  Returns: A pointer to a X509 KEYPAIR object on success.
328  *           NULL on failure.
329  */
330 X509_KEYPAIR *crypto_keypair_new (void) {
331    X509_KEYPAIR *keypair;
332
333    /* Allocate our keypair structure */
334    keypair = (X509_KEYPAIR *) malloc(sizeof(X509_KEYPAIR));
335    if (!keypair) {
336       return NULL;
337    }
338
339    /* Initialize our keypair structure */
340    keypair->keyid = NULL;
341    keypair->pubkey = NULL;
342    keypair->privkey = NULL;
343
344    return keypair;
345 }
346
347 /*
348  * Load a public key from a PEM-encoded x509 certificate.
349  *  Returns: true on success
350  *           false on failure
351  */
352 int crypto_keypair_load_cert (X509_KEYPAIR *keypair, const char *file)
353 {
354    BIO *bio;
355    X509 *cert;
356
357    /* Open the file */
358    if (!(bio = BIO_new_file(file, "r"))) {
359       openssl_post_errors(M_ERROR, _("Unable to open certificate file"));
360       return false;
361    }
362
363    cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
364    BIO_free(bio);
365    if (!cert) {
366       openssl_post_errors(M_ERROR, _("Unable to read certificate from file"));
367       return false;
368    }
369
370    /* Extract the public key */
371    if (!(keypair->pubkey = X509_get_pubkey(cert))) {
372       openssl_post_errors(M_ERROR, _("Unable to extract public key from certificate"));
373       goto err;
374    }
375
376    /* Extract the subjectKeyIdentifier extension field */
377    if ((keypair->keyid = openssl_cert_keyid(cert)) == NULL) {
378       Emsg0(M_ERROR, 0, _("Provided certificate does not include the required subjectKeyIdentifier extension."));
379       goto err;
380    }
381
382    /* Validate the public key type (only RSA is supported) */
383    if (EVP_PKEY_type(keypair->pubkey->type) != EVP_PKEY_RSA) {
384        Emsg1(M_ERROR, 0, _("Unsupported key type provided: %d\n"), EVP_PKEY_type(keypair->pubkey->type));
385        goto err;
386    }
387
388    return true;
389
390 err:
391    X509_free(cert);
392    if (keypair->pubkey) {
393       EVP_PKEY_free(keypair->pubkey);
394    }
395    return false;
396 }
397
398 /* Dispatch user PEM encryption callbacks */
399 static int crypto_pem_callback_dispatch (char *buf, int size, int rwflag, void *userdata)
400 {
401    PEM_CB_CONTEXT *ctx = (PEM_CB_CONTEXT *) userdata;
402    return (ctx->pem_callback(buf, size, ctx->pem_userdata));
403 }
404
405 /*
406  * Load a PEM-encoded private key.
407  *  Returns: true on success
408  *           false on failure
409  */
410 int crypto_keypair_load_key (X509_KEYPAIR *keypair, const char *file,
411                              CRYPTO_PEM_PASSWD_CB *pem_callback,
412                              const void *pem_userdata)
413 {
414    BIO *bio;
415    PEM_CB_CONTEXT ctx;
416
417    /* Open the file */
418    if (!(bio = BIO_new_file(file, "r"))) {
419       openssl_post_errors(M_ERROR, _("Unable to open private key file"));
420       return false;
421    }
422
423    /* Set up PEM encryption callback */
424    if (pem_callback) {
425       ctx.pem_callback = pem_callback;
426       ctx.pem_userdata = pem_userdata;
427    } else {
428       ctx.pem_callback = crypto_default_pem_callback;
429       ctx.pem_userdata = NULL;
430    }
431
432    keypair->privkey = PEM_read_bio_PrivateKey(bio, NULL, crypto_pem_callback_dispatch, &ctx);
433    BIO_free(bio);
434    if (!keypair->privkey) {
435       openssl_post_errors(M_ERROR, _("Unable to read private key from file"));
436       return false;
437    }
438
439    return true;
440 }
441
442 /*
443  * Free memory associated with a keypair object.
444  */
445 void crypto_keypair_free (X509_KEYPAIR *keypair)
446 {
447    if (keypair->pubkey) {
448       EVP_PKEY_free(keypair->pubkey);
449    }
450    if (keypair->privkey) {
451       EVP_PKEY_free(keypair->privkey);
452    }
453    if (keypair->keyid) {
454       M_ASN1_OCTET_STRING_free(keypair->keyid);
455    }
456    free(keypair);
457 }
458
459 /*
460  * Create a new message digest context of the specified type
461  *  Returns: A pointer to a DIGEST object on success.
462  *           NULL on failure.
463  */
464 DIGEST *crypto_digest_new (crypto_digest_t type)
465 {
466    DIGEST *digest;
467    const EVP_MD *md = NULL; /* Quell invalid uninitialized warnings */
468
469    digest = (DIGEST *) malloc(sizeof(DIGEST));
470    digest->type = type;
471
472    /* Initialize the OpenSSL message digest context */
473    EVP_MD_CTX_init(&digest->ctx);
474
475    /* Determine the correct OpenSSL message digest type */
476    switch (type) {
477    case CRYPTO_DIGEST_MD5:
478       md = EVP_md5();
479       break;
480    case CRYPTO_DIGEST_SHA1:
481       md = EVP_sha1();
482       break;
483 #ifdef HAVE_SHA2
484    case CRYPTO_DIGEST_SHA256:
485       md = EVP_sha256();
486       break;
487    case CRYPTO_DIGEST_SHA512:
488       md = EVP_sha512();
489       break;
490 #endif
491    default:
492       Emsg1(M_ERROR, 0, _("Unsupported digest type: %d\n"), type);
493       goto err;
494    }
495
496    /* Initialize the backing OpenSSL context */
497    if (EVP_DigestInit_ex(&digest->ctx, md, NULL) == 0) {
498       goto err;
499    }
500
501    return digest;
502
503 err:
504    /* This should not happen, but never say never ... */
505    openssl_post_errors(M_ERROR, _("OpenSSL digest initialization failed"));
506    crypto_digest_free(digest);
507    return NULL;
508 }
509
510 /*
511  * Hash length bytes of data into the provided digest context.
512  * Returns: true on success
513  *          false on failure
514  */
515 bool crypto_digest_update (DIGEST *digest, const void *data, size_t length) {
516    if (EVP_DigestUpdate(&digest->ctx, data, length) == 0) {
517       return true;
518    } else { 
519       return false;
520    }
521 }
522
523 /*
524  * Finalize the data in digest, storing the result in dest and the result size
525  * in length. The result size can be determined with crypto_digest_size().
526  *
527  * Returns: true on success
528  *          false on failure
529  */
530 bool crypto_digest_finalize (DIGEST *digest, void *dest, size_t *length) {
531    if (!EVP_DigestFinal(&digest->ctx, (unsigned char *) dest, length)) {
532       return false;
533    } else {
534       return true;
535    }
536 }
537
538 /*
539  * Free memory associated with a digest object.
540  */
541 void crypto_digest_free (DIGEST *digest)
542 {
543   EVP_MD_CTX_cleanup(&digest->ctx);
544   free (digest);
545 }
546
547 /*
548  * Create a new message signature context.
549  *  Returns: A pointer to a SIGNATURE object on success.
550  *           NULL on failure.
551  */
552 SIGNATURE *crypto_sign_new (void)
553 {
554    SIGNATURE *sig;
555
556    sig = (SIGNATURE *) malloc(sizeof(SIGNATURE));
557    if (!sig) {
558       return NULL;
559    }
560
561    sig->sigData = SignatureData_new();
562
563    if (!sig->sigData) {
564       /* Allocation failed in OpenSSL */
565       free(sig);
566       return NULL;
567    }
568
569    /* Set the ASN.1 structure version number */
570    ASN1_INTEGER_set(sig->sigData->version, BACULA_ASN1_VERSION);
571
572    return sig;
573 }
574
575 /*
576  * For a given public key, find the associated SignatureInfo record
577  * and create a digest context for signature validation
578  * Returns: CRYPTO_ERROR_NONE on success, with the newly allocated DIGEST in digest.
579  *          A crypto_error_t value on failure.
580  */
581 crypto_error_t crypto_sign_get_digest(SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST **digest)
582 {
583    STACK_OF(SignerInfo) *signers;
584    SignerInfo *si;
585    int i;
586
587    signers = sig->sigData->signerInfo;
588
589    for (i = 0; i < sk_SignerInfo_num(signers); i++) {
590       si = sk_SignerInfo_value(signers, i);
591       if (M_ASN1_OCTET_STRING_cmp(keypair->keyid, si->subjectKeyIdentifier) == 0) {
592          /* Get the digest algorithm and allocate a digest context */
593          switch (OBJ_obj2nid(si->digestAlgorithm)) {
594          case NID_md5:
595             *digest = crypto_digest_new(CRYPTO_DIGEST_MD5);
596             break;
597          case NID_sha1:
598             *digest = crypto_digest_new(CRYPTO_DIGEST_SHA1);
599             break;
600 #ifdef HAVE_SHA2
601          case NID_sha256:
602             *digest = crypto_digest_new(CRYPTO_DIGEST_SHA256);
603             break;
604          case NID_sha512:
605             *digest = crypto_digest_new(CRYPTO_DIGEST_SHA512);
606             break;
607 #endif
608          default:
609             *digest = NULL;
610             return CRYPTO_ERROR_INVALID_DIGEST;
611          }
612
613          /* Shouldn't happen */
614          if (*digest == NULL) {
615             return CRYPTO_ERROR_INVALID_DIGEST;
616          } else {
617             return CRYPTO_ERROR_NONE;
618          }
619       }
620    }
621
622    return CRYPTO_ERROR_NOSIGNER;
623 }
624
625 /*
626  * For a given signature, public key, and digest, verify the SIGNATURE.
627  * Returns: CRYPTO_ERROR_NONE on success.
628  *          A crypto_error_t value on failure.
629  */
630 crypto_error_t crypto_sign_verify(SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST *digest)
631 {
632    STACK_OF(SignerInfo) *signers;
633    SignerInfo *si;
634    int ok, i;
635    unsigned int sigLen;
636 #if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
637    const unsigned char *sigData;
638 #else
639    unsigned char *sigData;
640 #endif
641
642    signers = sig->sigData->signerInfo;
643
644    /* Find the signer */
645    for (i = 0; i < sk_SignerInfo_num(signers); i++) {
646       si = sk_SignerInfo_value(signers, i);
647       if (M_ASN1_OCTET_STRING_cmp(keypair->keyid, si->subjectKeyIdentifier) == 0) {
648          /* Extract the signature data */
649          sigLen = M_ASN1_STRING_length(si->signature);
650          sigData = M_ASN1_STRING_data(si->signature);
651
652          ok = EVP_VerifyFinal(&digest->ctx, sigData, sigLen, keypair->pubkey);
653          if (ok >= 1) {
654             return CRYPTO_ERROR_NONE;
655          } else if (ok == 0) {
656             return CRYPTO_ERROR_BAD_SIGNATURE;
657          } else if (ok < 0) {
658             /* Shouldn't happen */
659             openssl_post_errors(M_ERROR, _("OpenSSL error occured"));
660             return CRYPTO_ERROR_INTERNAL;
661          }
662       }
663    }
664
665    /* Signer wasn't found. */
666    return CRYPTO_ERROR_NOSIGNER;
667 }
668
669
670 /*
671  * Add a new signer
672  *  Returns: true on success
673  *           false on failure
674  */
675 int crypto_sign_add_signer(SIGNATURE *sig, DIGEST *digest, X509_KEYPAIR *keypair)
676 {
677    SignerInfo *si = NULL;
678    unsigned char *buf = NULL;
679    unsigned int len;
680
681    si = SignerInfo_new();
682
683    if (!si) {
684       /* Allocation failed in OpenSSL */
685       return false;
686    }
687
688    /* Set the ASN.1 structure version number */
689    ASN1_INTEGER_set(si->version, BACULA_ASN1_VERSION);
690
691    /* Set the digest algorithm identifier */
692    switch (digest->type) {
693    case CRYPTO_DIGEST_MD5:
694       si->digestAlgorithm = OBJ_nid2obj(NID_md5);
695       break;
696    case CRYPTO_DIGEST_SHA1:
697       si->digestAlgorithm = OBJ_nid2obj(NID_sha1);
698       break;
699 #ifdef HAVE_SHA2
700    case CRYPTO_DIGEST_SHA256:
701       si->digestAlgorithm = OBJ_nid2obj(NID_sha256);
702       break;
703    case CRYPTO_DIGEST_SHA512:
704       si->digestAlgorithm = OBJ_nid2obj(NID_sha512);
705       break;
706 #endif
707    default:
708       /* This should never happen */
709       goto err;
710    }
711
712    /* Drop the string allocated by OpenSSL, and add our subjectKeyIdentifier */
713    M_ASN1_OCTET_STRING_free(si->subjectKeyIdentifier);
714    si->subjectKeyIdentifier = M_ASN1_OCTET_STRING_dup(keypair->keyid);
715
716    /* Set our signature algorithm. We currently require RSA */
717    assert(EVP_PKEY_type(keypair->pubkey->type) == EVP_PKEY_RSA);
718    /* This is slightly evil. Reach into the MD structure and grab the key type */
719    si->signatureAlgorithm = OBJ_nid2obj(digest->ctx.digest->pkey_type);
720
721    /* Finalize/Sign our Digest */
722    len = EVP_PKEY_size(keypair->privkey);
723    buf = (unsigned char *) malloc(len);
724    if (!EVP_SignFinal(&digest->ctx, buf, &len, keypair->privkey)) {
725       openssl_post_errors(M_ERROR, _("Signature creation failed"));
726       goto err;
727    }
728
729    /* Add the signature to the SignerInfo structure */
730    if (!M_ASN1_OCTET_STRING_set(si->signature, buf, len)) {
731       /* Allocation failed in OpenSSL */
732       goto err;
733    }
734
735    /* No longer needed */
736    free(buf);
737
738    /* Push the new SignerInfo structure onto the stack */
739    sk_SignerInfo_push(sig->sigData->signerInfo, si);
740
741    return true;
742
743 err:
744    if (si) {
745       SignerInfo_free(si);
746    }
747    if (buf) {
748       free(buf);
749    }
750
751    return false;
752 }
753
754 /*
755  * Encodes the SignatureData structure. The length argument is used to specify the
756  * size of dest. A length of 0 will cause no data to be written to dest, and the
757  * required length to be written to length. The caller can then allocate sufficient
758  * space for the output.
759  *
760  * Returns: true on success, stores the encoded data in dest, and the size in length.
761  *          false on failure.
762  */
763 int crypto_sign_encode(SIGNATURE *sig, void *dest, size_t *length)
764 {
765    if (*length == 0) {
766       *length = i2d_SignatureData(sig->sigData, NULL);
767       return true;
768    }
769
770    *length = i2d_SignatureData(sig->sigData, (unsigned char **) &dest);
771    return true;
772 }
773
774 /*
775  * Decodes the SignatureData structure. The length argument is used to specify the
776  * size of sigData.
777  *
778  * Returns: SIGNATURE instance on success.
779  *          NULL on failure.
780
781  */
782
783 SIGNATURE *crypto_sign_decode(const void *sigData, size_t length)
784 {
785    SIGNATURE *sig;
786 #if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
787    const unsigned char *p = (const unsigned char *) sigData;
788 #else
789    unsigned char *p = (unsigned char *) sigData;
790 #endif
791
792    sig = (SIGNATURE *) malloc(sizeof(SIGNATURE));
793    if (!sig) {
794       return NULL;
795    }
796
797    /* d2i_SignatureData modifies the supplied pointer */
798    sig->sigData  = d2i_SignatureData(NULL, &p, length);
799
800    if (!sig->sigData) {
801       /* Allocation / Decoding failed in OpenSSL */
802       openssl_post_errors(M_ERROR, _("Signature decoding failed"));
803       return NULL;
804    }
805
806    return sig;
807 }
808
809 /*
810  * Free memory associated with a signature object.
811  */
812 void crypto_sign_free(SIGNATURE *sig)
813 {
814    SignatureData_free(sig->sigData);
815    free (sig);
816 }
817
818 /*
819  * Perform global initialization of OpenSSL
820  * This function is not thread safe.
821  *  Returns: 0 on success
822  *           errno on failure
823  */
824 int init_crypto (void)
825 {
826    int stat;
827
828    if ((stat = openssl_init_threads()) != 0) {
829       Emsg1(M_ABORT, 0, _("Unable to init OpenSSL threading: ERR=%s\n"), strerror(stat));
830    }
831
832    /* Load libssl and libcrypto human-readable error strings */
833    SSL_load_error_strings();
834
835    /* Register OpenSSL ciphers */
836    SSL_library_init();
837
838    if (!openssl_seed_prng()) {
839       Emsg0(M_ERROR_TERM, 0, _("Failed to seed OpenSSL PRNG\n"));
840    }
841
842    crypto_initialized = true;
843
844    return stat;
845 }
846
847 /*
848  * Perform global cleanup of OpenSSL
849  * All cryptographic operations must be completed before calling this function.
850  * This function is not thread safe.
851  *  Returns: 0 on success
852  *           errno on failure
853  */
854 int cleanup_crypto (void)
855 {
856    /*
857     * Ensure that we've actually been initialized; Doing this here decreases the
858     * complexity of client's termination/cleanup code.
859     */
860    if (!crypto_initialized) {
861       return 0;
862    }
863
864    if (!openssl_save_prng()) {
865       Emsg0(M_ERROR, 0, _("Failed to save OpenSSL PRNG\n"));
866    }
867
868    openssl_cleanup_threads();
869
870    /* Free libssl and libcrypto error strings */
871    ERR_free_strings();
872
873    /* Free memory used by PRNG */
874    RAND_cleanup();
875
876    crypto_initialized = false;
877
878    return 0;
879 }
880
881
882 #else /* HAVE_OPENSSL */
883 # error No encryption library available
884 #endif /* HAVE_OPENSSL */
885
886 #else /* HAVE_CRYPTO */
887
888 /*
889  * Cryptography Support Disabled
890  */
891
892 /* Message Digest Structure */
893 struct Digest {
894    crypto_digest_t type;
895    union {
896       SHA1Context sha1;
897       MD5Context md5;
898    };
899 };
900
901 /* Dummy Signature Structure */
902 struct Signature {
903 };
904
905 DIGEST *crypto_digest_new (crypto_digest_t type)
906 {
907    DIGEST *digest;
908
909    digest = (DIGEST *) malloc(sizeof(DIGEST));
910    digest->type = type;
911
912    switch (type) {
913    case CRYPTO_DIGEST_MD5:
914       MD5Init(&digest->md5);
915       break;
916    case CRYPTO_DIGEST_SHA1:
917       SHA1Init(&digest->sha1);
918       break;
919    default:
920       Emsg0(M_ERROR, 0, _("Unsupported digest type specified\n"));
921       free(digest);
922       return NULL;
923    }
924
925    return (digest);
926 }
927
928 bool crypto_digest_update (DIGEST *digest, const void *data, size_t length) {
929    switch (digest->type) {
930    case CRYPTO_DIGEST_MD5:
931       /* Doesn't return anything ... */
932       MD5Update(&digest->md5, (unsigned char *) data, length);
933       return true;
934    case CRYPTO_DIGEST_SHA1:
935       int ret;
936       if ((ret = SHA1Update(&digest->sha1, (const u_int8_t *) data, length)) == shaSuccess) {
937          return true;
938       } else {
939          Emsg1(M_ERROR, 0, _("SHA1Update() returned an error: %d\n"), ret);
940          return false;
941       }
942       break;
943    default:
944       return false;
945    }
946 }
947
948 bool crypto_digest_finalize (DIGEST *digest, void *dest, size_t *length) {
949
950    switch (digest->type) {
951    case CRYPTO_DIGEST_MD5:
952       /* Guard against programmer error by either the API client or
953        * an out-of-sync CRYPTO_DIGEST_MAX_SIZE */
954       assert(*length >= CRYPTO_DIGEST_MD5_SIZE);
955       *length = CRYPTO_DIGEST_MD5_SIZE;
956       /* Doesn't return anything ... */
957       MD5Final((unsigned char *) dest, &digest->md5);
958       return true;
959    case CRYPTO_DIGEST_SHA1:
960       /* Guard against programmer error by either the API client or
961        * an out-of-sync CRYPTO_DIGEST_MAX_SIZE */
962       assert(*length >= CRYPTO_DIGEST_SHA1_SIZE);
963       *length = CRYPTO_DIGEST_SHA1_SIZE;
964       if (SHA1Final(&digest->sha1, (u_int8_t *) dest) == shaSuccess) {
965          return true;
966       } else {
967          return false;
968       }
969       break;
970    default:
971       return false;
972    }
973
974    return false;
975 }
976
977 void crypto_digest_free (DIGEST *digest)
978 {
979    free (digest);
980 }
981
982 /* Dummy routines */
983 int init_crypto (void) { return 0; }
984 int cleanup_crypto (void) { return 0; }
985
986 SIGNATURE *crypto_sign_new (void) { return NULL; }
987
988 crypto_error_t crypto_sign_get_digest (SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST **digest) { return CRYPTO_ERROR_INTERNAL; }
989 crypto_error_t crypto_sign_verify (SIGNATURE *sig, X509_KEYPAIR *keypair, DIGEST *digest) { return CRYPTO_ERROR_INTERNAL; }
990
991 int crypto_sign_add_signer (SIGNATURE *sig, DIGEST *digest, X509_KEYPAIR *keypair) { return false; }
992 int crypto_sign_encode (SIGNATURE *sig, void *dest, size_t *length) { return false; }
993
994 SIGNATURE *crypto_sign_decode (const void *sigData, size_t length) { return false; }
995 void crypto_sign_free (SIGNATURE *sig) { }
996
997
998 X509_KEYPAIR *crypto_keypair_new (void) { return NULL; }
999 int crypto_keypair_load_cert (X509_KEYPAIR *keypair, const char *file) { return false; }
1000 int crypto_keypair_load_key (X509_KEYPAIR *keypair, const char *file, CRYPTO_PEM_PASSWD_CB *pem_callback, const void *pem_userdata) { return false; }
1001 void crypto_keypair_free (X509_KEYPAIR *keypair) { }
1002
1003 #endif /* HAVE_CRYPTO */
1004
1005 /* Shared Code */
1006
1007 /*
1008  * Default PEM encryption passphrase callback.
1009  * Returns an empty password.
1010  */
1011 int crypto_default_pem_callback(char *buf, int size, const void *userdata)
1012 {
1013    bstrncpy(buf, "", size);
1014    return (strlen(buf));
1015 }
1016
1017 /*
1018  * Returns the ASCII name of the digest type.
1019  * Returns: ASCII name of digest type.
1020  */
1021 const char *crypto_digest_name (DIGEST *digest) {
1022    switch (digest->type) {
1023    case CRYPTO_DIGEST_MD5:
1024       return "MD5";
1025    case CRYPTO_DIGEST_SHA1:
1026       return "SHA1";
1027    case CRYPTO_DIGEST_SHA256:
1028       return "SHA256";
1029    case CRYPTO_DIGEST_SHA512:
1030       return "SHA512";
1031    case CRYPTO_DIGEST_NONE:
1032       return "None";
1033    default:
1034       return "Invalid Digest Type";
1035    }
1036
1037 }
1038
1039 /*
1040  * Given a stream type, returns the associated
1041  * crypto_digest_t value.
1042  */
1043 crypto_digest_t crypto_digest_stream_type (int stream) {
1044    switch (stream) {
1045    case STREAM_MD5_DIGEST:
1046       return CRYPTO_DIGEST_MD5;
1047    case STREAM_SHA1_DIGEST:
1048       return CRYPTO_DIGEST_SHA1;
1049    case STREAM_SHA256_DIGEST:
1050       return CRYPTO_DIGEST_SHA256;
1051    case STREAM_SHA512_DIGEST:
1052       return CRYPTO_DIGEST_SHA512;
1053    default:
1054       return CRYPTO_DIGEST_NONE;
1055    }
1056 }
1057
1058 /*
1059  *  * Given a crypto_error_t value, return the associated
1060  *   * error string
1061  *    */
1062 const char *crypto_strerror(crypto_error_t error) {
1063    switch (error) {
1064    case CRYPTO_ERROR_NONE:
1065       return "No error";
1066    case CRYPTO_ERROR_NOSIGNER:
1067       return "Signer not found";
1068    case CRYPTO_ERROR_INVALID_DIGEST:
1069       return "Unsupported digest algorithm";
1070    case CRYPTO_ERROR_BAD_SIGNATURE:
1071       return "Signature is invalid";
1072    case CRYPTO_ERROR_INTERNAL:
1073       /* This shouldn't happen */
1074       return "Internal error";
1075    default:
1076       return "Unknown error";
1077    }
1078 }