2014-03-05 12:34:10 +00:00
|
|
|
/*
|
|
|
|
* vircrypto.c: cryptographic helper APIs
|
|
|
|
*
|
2016-05-18 15:29:06 +00:00
|
|
|
* Copyright (C) 2014, 2016 Red Hat, Inc.
|
2014-03-05 12:34:10 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "vircrypto.h"
|
2016-05-18 15:29:06 +00:00
|
|
|
#include "virlog.h"
|
2014-03-05 12:34:10 +00:00
|
|
|
#include "virerror.h"
|
|
|
|
#include "viralloc.h"
|
2016-05-21 14:22:06 +00:00
|
|
|
#include "virrandom.h"
|
2021-02-01 13:13:53 +00:00
|
|
|
#include "virsecureerase.h"
|
2014-03-05 12:34:10 +00:00
|
|
|
|
2018-06-04 09:12:29 +00:00
|
|
|
#include <gnutls/gnutls.h>
|
|
|
|
#include <gnutls/crypto.h>
|
2016-05-18 15:29:06 +00:00
|
|
|
|
|
|
|
VIR_LOG_INIT("util.crypto");
|
2014-03-05 12:34:10 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_CRYPTO
|
|
|
|
|
|
|
|
static const char hex[] = "0123456789abcdef";
|
|
|
|
|
2018-05-11 14:18:17 +00:00
|
|
|
#define VIR_CRYPTO_LARGEST_DIGEST_SIZE VIR_CRYPTO_HASH_SIZE_SHA256
|
|
|
|
|
|
|
|
|
2014-03-05 12:34:10 +00:00
|
|
|
struct virHashInfo {
|
2018-05-11 14:18:17 +00:00
|
|
|
gnutls_digest_algorithm_t algorithm;
|
2014-03-05 12:34:10 +00:00
|
|
|
size_t hashlen;
|
|
|
|
} hashinfo[] = {
|
2018-05-11 14:18:17 +00:00
|
|
|
{ GNUTLS_DIG_MD5, VIR_CRYPTO_HASH_SIZE_MD5 },
|
|
|
|
{ GNUTLS_DIG_SHA256, VIR_CRYPTO_HASH_SIZE_SHA256 },
|
2014-03-05 12:34:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-01-09 10:39:55 +00:00
|
|
|
G_STATIC_ASSERT(G_N_ELEMENTS(hashinfo) == VIR_CRYPTO_HASH_LAST);
|
2014-03-05 12:34:10 +00:00
|
|
|
|
2018-05-15 07:58:50 +00:00
|
|
|
ssize_t
|
2018-05-11 14:31:10 +00:00
|
|
|
virCryptoHashBuf(virCryptoHash hash,
|
|
|
|
const char *input,
|
|
|
|
unsigned char *output)
|
2014-03-05 12:34:10 +00:00
|
|
|
{
|
2018-05-11 14:18:17 +00:00
|
|
|
int rc;
|
2014-03-05 12:34:10 +00:00
|
|
|
if (hash >= VIR_CRYPTO_HASH_LAST) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("Unknown crypto hash %d"), hash);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:18:17 +00:00
|
|
|
rc = gnutls_hash_fast(hashinfo[hash].algorithm, input, strlen(input), output);
|
|
|
|
if (rc < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to compute hash of data: %s"),
|
|
|
|
gnutls_strerror(rc));
|
2014-03-05 12:34:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-05-15 07:58:50 +00:00
|
|
|
return hashinfo[hash].hashlen;
|
2018-05-11 14:31:10 +00:00
|
|
|
}
|
2018-06-04 09:12:29 +00:00
|
|
|
|
2018-05-11 14:31:10 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virCryptoHashString(virCryptoHash hash,
|
|
|
|
const char *input,
|
|
|
|
char **output)
|
|
|
|
{
|
|
|
|
unsigned char buf[VIR_CRYPTO_LARGEST_DIGEST_SIZE];
|
2018-05-15 07:58:50 +00:00
|
|
|
ssize_t rc;
|
2018-05-11 14:31:10 +00:00
|
|
|
size_t hashstrlen;
|
|
|
|
size_t i;
|
|
|
|
|
2018-05-15 07:58:50 +00:00
|
|
|
if ((rc = virCryptoHashBuf(hash, input, buf)) < 0)
|
2018-05-11 14:31:10 +00:00
|
|
|
return -1;
|
|
|
|
|
2018-05-15 07:58:50 +00:00
|
|
|
hashstrlen = (rc * 2) + 1;
|
2018-05-11 14:31:10 +00:00
|
|
|
|
2020-10-05 17:12:37 +00:00
|
|
|
*output = g_new0(char, hashstrlen);
|
2014-03-05 12:34:10 +00:00
|
|
|
|
2018-05-15 07:58:50 +00:00
|
|
|
for (i = 0; i < rc; i++) {
|
2014-03-05 12:34:10 +00:00
|
|
|
(*output)[i * 2] = hex[(buf[i] >> 4) & 0xf];
|
|
|
|
(*output)[(i * 2) + 1] = hex[buf[i] & 0xf];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-05-18 15:29:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* virCryptoEncryptDataAESgntuls:
|
|
|
|
*
|
|
|
|
* Performs the AES gnutls encryption
|
|
|
|
*
|
2016-11-15 14:00:08 +00:00
|
|
|
* Same input as virCryptoEncryptData, except the algorithm is replaced
|
2016-05-18 15:29:06 +00:00
|
|
|
* by the specific gnutls algorithm.
|
|
|
|
*
|
|
|
|
* Encrypts the @data buffer using the @enckey and if available the @iv
|
|
|
|
*
|
|
|
|
* Returns 0 on success with the ciphertext being filled. It is the
|
|
|
|
* caller's responsibility to clear and free it. Returns -1 on failure
|
|
|
|
* w/ error set.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virCryptoEncryptDataAESgnutls(gnutls_cipher_algorithm_t gnutls_enc_alg,
|
|
|
|
uint8_t *enckey,
|
|
|
|
size_t enckeylen,
|
|
|
|
uint8_t *iv,
|
|
|
|
size_t ivlen,
|
|
|
|
uint8_t *data,
|
|
|
|
size_t datalen,
|
|
|
|
uint8_t **ciphertextret,
|
|
|
|
size_t *ciphertextlenret)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
size_t i;
|
|
|
|
gnutls_cipher_hd_t handle = NULL;
|
|
|
|
gnutls_datum_t enc_key;
|
|
|
|
gnutls_datum_t iv_buf;
|
|
|
|
uint8_t *ciphertext;
|
|
|
|
size_t ciphertextlen;
|
|
|
|
|
Fix padding of encrypted data
If we are encoding a block of data that is 16 bytes in length,
we cannot leave it as 16 bytes, we must pad it out to the next
block boundary, 32 bytes. Without this padding, the decoder will
incorrectly treat the last byte of plain text as the padding
length, as it can't distinguish padded from non-padded data.
The problem exhibited itself when using a 16 byte passphrase
for a LUKS volume
$ virsh secret-set-value 55806c7d-8e93-456f-829b-607d8c198367 \
$(echo -n 1234567812345678 | base64)
Secret value set
$ virsh start demo
error: Failed to start domain demo
error: internal error: process exited while connecting to monitor: >>>>>>>>>>Len 16
2017-05-02T10:35:40.016390Z qemu-system-x86_64: -object \
secret,id=virtio-disk1-luks-secret0,data=SEtNi5vDUeyseMKHwc1c1Q==,\
keyid=masterKey0,iv=zm7apUB1A6dPcH53VW960Q==,format=base64: \
Incorrect number of padding bytes (56) found on decrypted data
Notice how the padding '56' corresponds to the ordinal value of
the character '8'.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-05-02 10:32:43 +00:00
|
|
|
/* Allocate a padded buffer, copy in the data.
|
|
|
|
*
|
|
|
|
* NB, we must *always* have at least 1 byte of
|
|
|
|
* padding - we can't skip it on multiples of
|
|
|
|
* 16, otherwise decoder can't distinguish padded
|
|
|
|
* data from non-padded data. Hence datalen + 1
|
|
|
|
*/
|
|
|
|
ciphertextlen = VIR_ROUND_UP(datalen + 1, 16);
|
2020-10-05 17:12:37 +00:00
|
|
|
ciphertext = g_new0(uint8_t, ciphertextlen);
|
2016-05-18 15:29:06 +00:00
|
|
|
memcpy(ciphertext, data, datalen);
|
|
|
|
|
|
|
|
/* Fill in the padding of the buffer with the size of the padding
|
|
|
|
* which is required for decryption. */
|
|
|
|
for (i = datalen; i < ciphertextlen; i++)
|
|
|
|
ciphertext[i] = ciphertextlen - datalen;
|
|
|
|
|
|
|
|
/* Initialize the gnutls cipher */
|
|
|
|
enc_key.size = enckeylen;
|
|
|
|
enc_key.data = enckey;
|
|
|
|
if (iv) {
|
|
|
|
iv_buf.size = ivlen;
|
|
|
|
iv_buf.data = iv;
|
|
|
|
}
|
|
|
|
if ((rc = gnutls_cipher_init(&handle, gnutls_enc_alg,
|
|
|
|
&enc_key, &iv_buf)) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to initialize cipher: '%s'"),
|
|
|
|
gnutls_strerror(rc));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encrypt the data and free the memory for cipher operations */
|
|
|
|
rc = gnutls_cipher_encrypt(handle, ciphertext, ciphertextlen);
|
|
|
|
gnutls_cipher_deinit(handle);
|
2021-02-02 14:44:55 +00:00
|
|
|
virSecureErase(&enc_key, sizeof(gnutls_datum_t));
|
|
|
|
virSecureErase(&iv_buf, sizeof(gnutls_datum_t));
|
2016-05-18 15:29:06 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to encrypt the data: '%s'"),
|
|
|
|
gnutls_strerror(rc));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ciphertextret = ciphertext;
|
|
|
|
*ciphertextlenret = ciphertextlen;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
2021-02-01 13:13:53 +00:00
|
|
|
virSecureErase(ciphertext, ciphertextlen);
|
|
|
|
g_free(ciphertext);
|
2021-02-02 14:44:55 +00:00
|
|
|
virSecureErase(&enc_key, sizeof(gnutls_datum_t));
|
|
|
|
virSecureErase(&iv_buf, sizeof(gnutls_datum_t));
|
2016-05-18 15:29:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* virCryptoEncryptData:
|
2018-12-04 17:08:14 +00:00
|
|
|
* @algorithm: algorithm desired for encryption
|
2016-05-18 15:29:06 +00:00
|
|
|
* @enckey: encryption key
|
2018-12-04 17:08:14 +00:00
|
|
|
* @enckeylen: encryption key length
|
2016-05-18 15:29:06 +00:00
|
|
|
* @iv: initialization vector
|
|
|
|
* @ivlen: length of initialization vector
|
|
|
|
* @data: data to encrypt
|
|
|
|
* @datalen: length of data
|
|
|
|
* @ciphertext: stream of bytes allocated to store ciphertext
|
|
|
|
* @ciphertextlen: size of the stream of bytes
|
|
|
|
*
|
|
|
|
* If available, attempt and return the requested encryption type
|
|
|
|
* using the parameters passed.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure with error set
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virCryptoEncryptData(virCryptoCipher algorithm,
|
2016-05-22 14:07:47 +00:00
|
|
|
uint8_t *enckey,
|
2016-05-18 15:29:06 +00:00
|
|
|
size_t enckeylen,
|
2016-05-22 14:07:47 +00:00
|
|
|
uint8_t *iv,
|
2016-05-18 15:29:06 +00:00
|
|
|
size_t ivlen,
|
2016-05-22 14:07:47 +00:00
|
|
|
uint8_t *data,
|
|
|
|
size_t datalen,
|
|
|
|
uint8_t **ciphertext,
|
|
|
|
size_t *ciphertextlen)
|
2016-05-18 15:29:06 +00:00
|
|
|
{
|
|
|
|
switch (algorithm) {
|
|
|
|
case VIR_CRYPTO_CIPHER_AES256CBC:
|
|
|
|
if (enckeylen != 32) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("AES256CBC encryption invalid keylen=%zu"),
|
|
|
|
enckeylen);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ivlen != 16) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("AES256CBC initialization vector invalid len=%zu"),
|
|
|
|
ivlen);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Encrypt the data buffer using an encryption key and
|
|
|
|
* initialization vector via the gnutls_cipher_encrypt API
|
|
|
|
* for GNUTLS_CIPHER_AES_256_CBC.
|
|
|
|
*/
|
|
|
|
return virCryptoEncryptDataAESgnutls(GNUTLS_CIPHER_AES_256_CBC,
|
|
|
|
enckey, enckeylen, iv, ivlen,
|
|
|
|
data, datalen,
|
|
|
|
ciphertext, ciphertextlen);
|
|
|
|
|
|
|
|
case VIR_CRYPTO_CIPHER_NONE:
|
|
|
|
case VIR_CRYPTO_CIPHER_LAST:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("algorithm=%d is not supported"), algorithm);
|
|
|
|
return -1;
|
|
|
|
}
|