FreeRDP/libfreerdp/core/certificate.c

979 lines
24 KiB
C
Raw Normal View History

/**
2012-10-09 07:02:04 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
* Certificate Handling
*
* Copyright 2011 Jiten Pathy
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2015 Thincast Technologies GmbH
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <winpr/wtypes.h>
#include <winpr/crt.h>
#include <winpr/file.h>
2016-02-24 18:46:25 +03:00
#include <winpr/crypto.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include "certificate.h"
2016-11-21 19:28:54 +03:00
#include "../crypto/opensslcompat.h"
#define TAG "com.freerdp.core"
/**
*
* X.509 Certificate Structure
*
* Certificate ::= SEQUENCE
* {
* tbsCertificate TBSCertificate,
* signatureAlgorithm AlgorithmIdentifier,
* signatureValue BIT_STRING
* }
*
* TBSCertificate ::= SEQUENCE
* {
* version [0] EXPLICIT Version DEFAULT v1,
* serialNumber CertificateSerialNumber,
* signature AlgorithmIdentifier,
* issuer Name,
* validity Validity,
* subject Name,
* subjectPublicKeyInfo SubjectPublicKeyInfo,
* issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
* subjectUniqueId [2] IMPLICIT UniqueIdentifier OPTIONAL,
* extensions [3] EXPLICIT Extensions OPTIONAL
* }
*
* Version ::= INTEGER { v1(0), v2(1), v3(2) }
*
* CertificateSerialNumber ::= INTEGER
*
* AlgorithmIdentifier ::= SEQUENCE
* {
* algorithm OBJECT_IDENTIFIER,
* parameters ANY DEFINED BY algorithm OPTIONAL
* }
*
* Name ::= CHOICE { RDNSequence }
*
* RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
*
* RelativeDistinguishedName ::= SET OF AttributeTypeAndValue
*
* AttributeTypeAndValue ::= SEQUENCE
* {
* type AttributeType,
* value AttributeValue
* }
*
* AttributeType ::= OBJECT_IDENTIFIER
*
* AttributeValue ::= ANY DEFINED BY AttributeType
*
* Validity ::= SEQUENCE
* {
* notBefore Time,
* notAfter Time
* }
*
* Time ::= CHOICE
* {
* utcTime UTCTime,
* generalTime GeneralizedTime
* }
*
* UniqueIdentifier ::= BIT_STRING
*
* SubjectPublicKeyInfo ::= SEQUENCE
* {
* algorithm AlgorithmIdentifier,
* subjectPublicKey BIT_STRING
* }
*
* RSAPublicKey ::= SEQUENCE
* {
* modulus INTEGER
* publicExponent INTEGER
* }
*
* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
*
* Extension ::= SEQUENCE
* {
* extnID OBJECT_IDENTIFIER
* critical BOOLEAN DEFAULT FALSE,
* extnValue OCTET_STRING
* }
*
*/
2019-11-06 17:24:51 +03:00
static const char* certificate_read_errors[] = { "Certificate tag",
"TBSCertificate",
"Explicit Contextual Tag [0]",
"version",
"CertificateSerialNumber",
"AlgorithmIdentifier",
"Issuer Name",
"Validity",
"Subject Name",
"SubjectPublicKeyInfo Tag",
"subjectPublicKeyInfo::AlgorithmIdentifier",
"subjectPublicKeyInfo::subjectPublicKey",
"RSAPublicKey Tag",
"modulusLength",
"zero padding",
"modulusLength",
"modulus",
"publicExponent length",
"publicExponent" };
/**
* Read X.509 Certificate
* @param certificate certificate module
* @param cert X.509 certificate
*/
static BOOL certificate_read_x509_certificate(rdpCertBlob* cert, rdpCertInfo* info)
{
2014-08-18 21:34:47 +04:00
wStream* s;
size_t length;
BYTE padding;
2012-10-09 11:26:39 +04:00
UINT32 version;
size_t modulus_length;
size_t exponent_length;
int error = 0;
2017-07-13 13:19:10 +03:00
if (!cert || !info)
return FALSE;
memset(info, 0, sizeof(rdpCertInfo));
2013-05-09 01:48:30 +04:00
s = Stream_New(cert->data, cert->length);
if (!s)
return FALSE;
info->Modulus = 0;
2013-05-09 01:48:30 +04:00
if (!ber_read_sequence_tag(s, &length)) /* Certificate (SEQUENCE) */
goto error1;
error++;
2013-05-09 01:48:30 +04:00
if (!ber_read_sequence_tag(s, &length)) /* TBSCertificate (SEQUENCE) */
goto error1;
error++;
2019-11-06 17:24:51 +03:00
if (!ber_read_contextual_tag(s, 0, &length, TRUE)) /* Explicit Contextual Tag [0] */
goto error1;
error++;
2013-05-09 01:48:30 +04:00
if (!ber_read_integer(s, &version)) /* version (INTEGER) */
goto error1;
error++;
version++;
/* serialNumber */
2013-05-09 01:48:30 +04:00
if (!ber_read_integer(s, NULL)) /* CertificateSerialNumber (INTEGER) */
goto error1;
error++;
/* signature */
2017-11-21 13:37:42 +03:00
if (!ber_read_sequence_tag(s, &length) ||
!Stream_SafeSeek(s, length)) /* AlgorithmIdentifier (SEQUENCE) */
goto error1;
error++;
/* issuer */
2013-05-09 01:48:30 +04:00
if (!ber_read_sequence_tag(s, &length) || !Stream_SafeSeek(s, length)) /* Name (SEQUENCE) */
goto error1;
error++;
/* validity */
2013-05-09 01:48:30 +04:00
if (!ber_read_sequence_tag(s, &length) || !Stream_SafeSeek(s, length)) /* Validity (SEQUENCE) */
goto error1;
error++;
/* subject */
2013-05-09 01:48:30 +04:00
if (!ber_read_sequence_tag(s, &length) || !Stream_SafeSeek(s, length)) /* Name (SEQUENCE) */
goto error1;
error++;
/* subjectPublicKeyInfo */
2013-05-09 01:48:30 +04:00
if (!ber_read_sequence_tag(s, &length)) /* SubjectPublicKeyInfo (SEQUENCE) */
goto error1;
error++;
/* subjectPublicKeyInfo::AlgorithmIdentifier */
2017-11-21 13:37:42 +03:00
if (!ber_read_sequence_tag(s, &length) ||
!Stream_SafeSeek(s, length)) /* AlgorithmIdentifier (SEQUENCE) */
goto error1;
error++;
/* subjectPublicKeyInfo::subjectPublicKey */
2013-05-09 01:48:30 +04:00
if (!ber_read_bit_string(s, &length, &padding)) /* BIT_STRING */
goto error1;
error++;
/* RSAPublicKey (SEQUENCE) */
2013-05-09 01:48:30 +04:00
if (!ber_read_sequence_tag(s, &length)) /* SEQUENCE */
goto error1;
error++;
2013-05-09 01:48:30 +04:00
if (!ber_read_integer_length(s, &modulus_length)) /* modulus (INTEGER) */
goto error1;
error++;
/* skip zero padding, if any */
do
{
2013-05-09 01:48:30 +04:00
if (Stream_GetRemainingLength(s) < 1)
goto error1;
2013-05-09 01:48:30 +04:00
2013-05-09 00:27:21 +04:00
Stream_Peek_UINT8(s, padding);
if (padding == 0)
{
2013-05-09 01:48:30 +04:00
if (!Stream_SafeSeek(s, 1))
goto error1;
2013-05-09 01:48:30 +04:00
modulus_length--;
}
2019-11-06 17:24:51 +03:00
} while (padding == 0);
2013-05-09 01:48:30 +04:00
error++;
2019-02-07 16:22:28 +03:00
if (modulus_length > UINT32_MAX)
goto error1;
2013-05-09 01:48:30 +04:00
2019-02-07 16:22:28 +03:00
if ((Stream_GetRemainingLength(s)) < modulus_length)
goto error1;
info->ModulusLength = (UINT32)modulus_length;
2019-11-06 17:24:51 +03:00
info->Modulus = (BYTE*)malloc(info->ModulusLength);
if (!info->Modulus)
goto error1;
2013-05-09 00:09:16 +04:00
Stream_Read(s, info->Modulus, info->ModulusLength);
error++;
2013-05-09 01:48:30 +04:00
if (!ber_read_integer_length(s, &exponent_length)) /* publicExponent (INTEGER) */
goto error2;
2013-05-09 01:48:30 +04:00
error++;
2013-05-09 01:48:30 +04:00
2019-02-07 16:22:28 +03:00
if (((Stream_GetRemainingLength(s)) < exponent_length) || (exponent_length > 4))
goto error2;
2013-05-09 01:48:30 +04:00
2013-05-09 00:09:16 +04:00
Stream_Read(s, &info->exponent[4 - exponent_length], exponent_length);
2012-09-24 12:40:32 +04:00
crypto_reverse(info->Modulus, info->ModulusLength);
crypto_reverse(info->exponent, 4);
2013-05-09 01:48:30 +04:00
Stream_Free(s, FALSE);
return TRUE;
error2:
free(info->Modulus);
2013-01-14 02:37:50 +04:00
info->Modulus = 0;
error1:
2017-11-21 13:37:42 +03:00
WLog_ERR(TAG, "error reading when reading certificate: part=%s error=%d",
certificate_read_errors[error], error);
2013-05-09 01:48:30 +04:00
Stream_Free(s, FALSE);
return FALSE;
}
/**
* Instantiate new X.509 Certificate Chain.
* @param count certificate chain count
* @return new X.509 certificate chain
*/
static rdpX509CertChain* certificate_new_x509_certificate_chain(UINT32 count)
{
2014-08-18 21:34:47 +04:00
rdpX509CertChain* x509_cert_chain;
2019-11-06 17:24:51 +03:00
x509_cert_chain = (rdpX509CertChain*)malloc(sizeof(rdpX509CertChain));
if (!x509_cert_chain)
return NULL;
x509_cert_chain->count = count;
2019-11-06 17:24:51 +03:00
x509_cert_chain->array = (rdpCertBlob*)calloc(count, sizeof(rdpCertBlob));
if (!x509_cert_chain->array)
{
free(x509_cert_chain);
return NULL;
}
return x509_cert_chain;
}
/**
* Free X.509 Certificate Chain.
* @param x509_cert_chain X.509 certificate chain to be freed
*/
static void certificate_free_x509_certificate_chain(rdpX509CertChain* x509_cert_chain)
{
2021-09-17 10:10:25 +03:00
UINT32 i;
if (!x509_cert_chain)
return;
2021-09-17 10:10:25 +03:00
for (i = 0; i < x509_cert_chain->count; i++)
{
2021-09-17 10:10:25 +03:00
struct rdp_CertBlob* element = &x509_cert_chain->array[i];
free(element->data);
2012-02-02 06:11:46 +04:00
}
free(x509_cert_chain->array);
free(x509_cert_chain);
}
2017-11-21 13:37:42 +03:00
static BOOL certificate_process_server_public_key(rdpCertificate* certificate, wStream* s,
2019-11-06 17:24:51 +03:00
UINT32 length)
2011-09-05 22:02:52 +04:00
{
BYTE magic[4];
2012-10-09 11:26:39 +04:00
UINT32 keylen;
UINT32 bitlen;
UINT32 datalen;
BYTE* tmp;
2011-09-05 22:02:52 +04:00
2013-05-09 01:48:30 +04:00
if (Stream_GetRemainingLength(s) < 20)
return FALSE;
2013-05-09 00:09:16 +04:00
Stream_Read(s, magic, 4);
2012-09-24 12:40:32 +04:00
2011-09-05 22:02:52 +04:00
if (memcmp(magic, "RSA1", 4) != 0)
{
WLog_ERR(TAG, "magic error");
return FALSE;
2011-09-05 22:02:52 +04:00
}
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(s, keylen);
Stream_Read_UINT32(s, bitlen);
Stream_Read_UINT32(s, datalen);
Stream_Read(s, certificate->cert_info.exponent, 4);
2012-09-24 12:40:32 +04:00
if ((keylen <= 8) || (Stream_GetRemainingLength(s) < keylen))
return FALSE;
certificate->cert_info.ModulusLength = keylen - 8;
tmp = realloc(certificate->cert_info.Modulus, certificate->cert_info.ModulusLength);
if (!tmp)
return FALSE;
certificate->cert_info.Modulus = tmp;
2013-05-09 00:09:16 +04:00
Stream_Read(s, certificate->cert_info.Modulus, certificate->cert_info.ModulusLength);
Stream_Seek(s, 8); /* 8 bytes of zero padding */
return TRUE;
2011-09-05 22:02:52 +04:00
}
2014-08-18 21:34:47 +04:00
static BOOL certificate_process_server_public_signature(rdpCertificate* certificate,
2019-11-06 17:24:51 +03:00
const BYTE* sigdata, size_t sigdatalen,
wStream* s, UINT32 siglen)
2011-09-05 22:02:52 +04:00
{
2017-11-21 13:37:42 +03:00
#if defined(CERT_VALIDATE_PADDING) || defined(CERT_VALIDATE_RSA)
size_t i, sum;
2017-11-21 13:37:42 +03:00
#endif
#if defined(CERT_VALIDATE_RSA)
BYTE sig[TSSK_KEY_LENGTH];
2017-11-21 13:37:42 +03:00
#endif
BYTE encsig[TSSK_KEY_LENGTH + 8];
2017-11-21 13:37:42 +03:00
#if defined(CERT_VALIDATE_MD5) && defined(CERT_VALIDATE_RSA)
2016-02-24 18:46:25 +03:00
BYTE md5hash[WINPR_MD5_DIGEST_LENGTH];
#endif
2017-11-21 13:37:42 +03:00
#if !defined(CERT_VALIDATE_MD5) || !defined(CERT_VALIDATE_RSA)
(void)sigdata;
(void)sigdatalen;
#endif
(void)certificate;
2019-11-06 17:24:51 +03:00
/* Do not bother with validation of server proprietary certificate. The use of MD5 here is not
* allowed under FIPS. Since the validation is not protecting against anything since the
* private/public keys are well known and documented in MS-RDPBCGR section 5.3.3.1, we are not
* gaining any security by using MD5 for signature comparison. Rather then use MD5
* here we just dont do the validation to avoid its use. Historically, freerdp has been ignoring
* a failed validation anyways. */
2017-11-21 13:37:42 +03:00
#if defined(CERT_VALIDATE_MD5)
if (!winpr_Digest(WINPR_MD_MD5, sigdata, sigdatalen, md5hash, sizeof(md5hash)))
return FALSE;
#endif
2013-05-09 00:09:16 +04:00
Stream_Read(s, encsig, siglen);
2017-12-21 11:34:35 +03:00
if (siglen < 8)
return FALSE;
2019-11-06 17:24:51 +03:00
/* Last 8 bytes shall be all zero. */
2017-11-21 13:37:42 +03:00
#if defined(CERT_VALIDATE_PADDING)
for (sum = 0, i = sizeof(encsig) - 8; i < sizeof(encsig); i++)
sum += encsig[i];
if (sum != 0)
{
WLog_ERR(TAG, "invalid signature");
return FALSE;
}
2017-11-21 13:37:42 +03:00
#endif
#if defined(CERT_VALIDATE_RSA)
2017-12-21 11:34:35 +03:00
if (crypto_rsa_public_decrypt(encsig, siglen - 8, TSSK_KEY_LENGTH, tssk_modulus, tssk_exponent,
sig) <= 0)
{
WLog_ERR(TAG, "invalid RSA decrypt");
return FALSE;
}
/* Verify signature. */
/* Do not bother with validation of server proprietary certificate as described above. */
2017-11-21 13:37:42 +03:00
#if defined(CERT_VALIDATE_MD5)
if (memcmp(md5hash, sig, sizeof(md5hash)) != 0)
{
WLog_ERR(TAG, "invalid signature");
return FALSE;
}
#endif
/*
* Verify rest of decrypted data:
* The 17th byte is 0x00.
* The 18th through 62nd bytes are each 0xFF.
* The 63rd byte is 0x01.
*/
for (sum = 0, i = 17; i < 62; i++)
sum += sig[i];
if (sig[16] != 0x00 || sum != 0xFF * (62 - 17) || sig[62] != 0x01)
{
WLog_ERR(TAG, "invalid signature");
return FALSE;
}
2017-11-21 13:37:42 +03:00
#endif
return TRUE;
2011-09-05 22:02:52 +04:00
}
/**
* Read a Server Proprietary Certificate.\n
* @param certificate certificate module
* @param s stream
*/
static BOOL certificate_read_server_proprietary_certificate(rdpCertificate* certificate, wStream* s)
{
2012-10-09 11:26:39 +04:00
UINT32 dwSigAlgId;
UINT32 dwKeyAlgId;
UINT16 wPublicKeyBlobType;
UINT16 wPublicKeyBlobLen;
UINT16 wSignatureBlobType;
UINT16 wSignatureBlobLen;
2014-08-18 21:34:47 +04:00
BYTE* sigdata;
2017-11-21 13:37:42 +03:00
size_t sigdatalen;
2011-09-05 22:02:52 +04:00
2013-05-09 01:48:30 +04:00
if (Stream_GetRemainingLength(s) < 12)
return FALSE;
/* -4, because we need to include dwVersion */
sigdata = Stream_Pointer(s) - 4;
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(s, dwSigAlgId);
Stream_Read_UINT32(s, dwKeyAlgId);
2012-09-24 12:40:32 +04:00
if (!((dwSigAlgId == SIGNATURE_ALG_RSA) && (dwKeyAlgId == KEY_EXCHANGE_ALG_RSA)))
2011-09-05 22:02:52 +04:00
{
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG,
"unsupported signature or key algorithm, dwSigAlgId=%" PRIu32
" dwKeyAlgId=%" PRIu32 "",
2017-11-21 13:37:42 +03:00
dwSigAlgId, dwKeyAlgId);
return FALSE;
2011-09-05 22:02:52 +04:00
}
2012-09-24 12:40:32 +04:00
2013-05-09 00:09:16 +04:00
Stream_Read_UINT16(s, wPublicKeyBlobType);
2012-09-24 12:40:32 +04:00
2011-09-05 22:02:52 +04:00
if (wPublicKeyBlobType != BB_RSA_KEY_BLOB)
{
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG, "unsupported public key blob type %" PRIu16 "", wPublicKeyBlobType);
return FALSE;
2011-09-05 22:02:52 +04:00
}
2012-09-24 12:40:32 +04:00
2013-05-09 00:09:16 +04:00
Stream_Read_UINT16(s, wPublicKeyBlobLen);
2013-05-09 01:48:30 +04:00
if (Stream_GetRemainingLength(s) < wPublicKeyBlobLen)
{
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG, "not enough bytes for public key(len=%" PRIu16 ")", wPublicKeyBlobLen);
return FALSE;
}
2012-09-24 12:40:32 +04:00
2011-09-05 22:02:52 +04:00
if (!certificate_process_server_public_key(certificate, s, wPublicKeyBlobLen))
{
WLog_ERR(TAG, "error in server public key");
return FALSE;
2011-09-05 22:02:52 +04:00
}
2012-09-24 12:40:32 +04:00
2013-05-09 01:48:30 +04:00
if (Stream_GetRemainingLength(s) < 4)
return FALSE;
sigdatalen = Stream_Pointer(s) - sigdata;
2013-05-09 00:09:16 +04:00
Stream_Read_UINT16(s, wSignatureBlobType);
2012-09-24 12:40:32 +04:00
2011-09-05 22:02:52 +04:00
if (wSignatureBlobType != BB_RSA_SIGNATURE_BLOB)
{
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG, "unsupported blob signature %" PRIu16 "", wSignatureBlobType);
return FALSE;
2011-09-05 22:02:52 +04:00
}
2012-09-24 12:40:32 +04:00
2013-05-09 00:09:16 +04:00
Stream_Read_UINT16(s, wSignatureBlobLen);
2013-05-09 01:48:30 +04:00
if (Stream_GetRemainingLength(s) < wSignatureBlobLen)
{
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG, "not enough bytes for signature(len=%" PRIu16 ")", wSignatureBlobLen);
return FALSE;
}
2012-09-24 12:40:32 +04:00
if (wSignatureBlobLen != 72)
{
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG, "invalid signature length (got %" PRIu16 ", expected 72)", wSignatureBlobLen);
return FALSE;
}
2012-09-24 12:40:32 +04:00
2017-11-21 13:37:42 +03:00
if (!certificate_process_server_public_signature(certificate, sigdata, sigdatalen, s,
2019-11-06 17:24:51 +03:00
wSignatureBlobLen))
2011-09-05 22:02:52 +04:00
{
WLog_ERR(TAG, "unable to parse server public signature");
return FALSE;
2011-09-05 22:02:52 +04:00
}
return TRUE;
}
/**
* Read an X.509 Certificate Chain.\n
* @param certificate certificate module
* @param s stream
*/
static BOOL certificate_read_server_x509_certificate_chain(rdpCertificate* certificate, wStream* s)
{
UINT32 i;
BOOL ret;
2012-10-09 11:26:39 +04:00
UINT32 certLength;
UINT32 numCertBlobs;
DEBUG_CERTIFICATE("Server X.509 Certificate Chain");
2013-05-09 01:48:30 +04:00
if (Stream_GetRemainingLength(s) < 4)
return FALSE;
Stream_Read_UINT32(s, numCertBlobs); /* numCertBlobs */
certificate->x509_cert_chain = certificate_new_x509_certificate_chain(numCertBlobs);
if (!certificate->x509_cert_chain)
return FALSE;
for (i = 0; i < numCertBlobs; i++)
{
2013-05-09 01:48:30 +04:00
if (Stream_GetRemainingLength(s) < 4)
return FALSE;
2013-05-09 01:48:30 +04:00
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(s, certLength);
2013-05-09 01:48:30 +04:00
if (Stream_GetRemainingLength(s) < certLength)
return FALSE;
2019-11-06 17:24:51 +03:00
DEBUG_CERTIFICATE("X.509 Certificate #%d, length:%" PRIu32 "", i + 1, certLength);
certificate->x509_cert_chain->array[i].data = (BYTE*)malloc(certLength);
if (!certificate->x509_cert_chain->array[i].data)
return FALSE;
2013-05-09 00:09:16 +04:00
Stream_Read(s, certificate->x509_cert_chain->array[i].data, certLength);
certificate->x509_cert_chain->array[i].length = certLength;
if ((numCertBlobs - i) == 2)
{
2017-07-18 13:43:55 +03:00
rdpCertInfo cert_info = { 0 };
DEBUG_CERTIFICATE("License Server Certificate");
2019-11-06 17:24:51 +03:00
ret = certificate_read_x509_certificate(&certificate->x509_cert_chain->array[i],
&cert_info);
DEBUG_LICENSE("modulus length:%" PRIu32 "", cert_info.ModulusLength);
2015-05-11 10:07:39 +03:00
free(cert_info.Modulus);
if (!ret)
{
WLog_ERR(TAG, "failed to read License Server, content follows:");
2017-11-21 13:37:42 +03:00
winpr_HexDump(TAG, WLOG_ERROR, certificate->x509_cert_chain->array[i].data,
certificate->x509_cert_chain->array[i].length);
2013-01-14 02:37:50 +04:00
return FALSE;
}
}
else if (numCertBlobs - i == 1)
{
DEBUG_CERTIFICATE("Terminal Server Certificate");
2017-11-21 13:37:42 +03:00
if (!certificate_read_x509_certificate(&certificate->x509_cert_chain->array[i],
&certificate->cert_info))
2013-01-14 02:37:50 +04:00
return FALSE;
2019-11-06 17:24:51 +03:00
DEBUG_CERTIFICATE("modulus length:%" PRIu32 "", certificate->cert_info.ModulusLength);
}
}
return TRUE;
}
/**
* Read a Server Certificate.\n
* @param certificate certificate module
* @param server_cert server certificate
* @param length certificate length
*/
2020-11-18 09:51:45 +03:00
BOOL certificate_read_server_certificate(rdpCertificate* certificate, const BYTE* server_cert,
2019-11-06 17:24:51 +03:00
size_t length)
{
BOOL ret;
wStream* s, sbuffer;
2012-10-09 11:26:39 +04:00
UINT32 dwVersion;
2019-11-06 17:24:51 +03:00
if (length < 4) /* NULL certificate is not an error see #1795 */
return TRUE;
s = Stream_StaticConstInit(&sbuffer, server_cert, length);
if (!s)
{
WLog_ERR(TAG, "Stream_New failed!");
return FALSE;
}
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(s, dwVersion); /* dwVersion (4 bytes) */
switch (dwVersion & CERT_CHAIN_VERSION_MASK)
{
case CERT_CHAIN_VERSION_1:
ret = certificate_read_server_proprietary_certificate(certificate, s);
break;
2014-08-18 21:34:47 +04:00
case CERT_CHAIN_VERSION_2:
ret = certificate_read_server_x509_certificate_chain(certificate, s);
break;
2014-08-18 21:34:47 +04:00
default:
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG, "invalid certificate chain version:%" PRIu32 "",
dwVersion & CERT_CHAIN_VERSION_MASK);
ret = FALSE;
break;
}
2011-09-05 22:02:52 +04:00
return ret;
}
2017-11-21 13:37:42 +03:00
rdpRsaKey* key_new_from_content(const char* keycontent, const char* keyfile)
{
2014-11-17 03:07:07 +03:00
BIO* bio = NULL;
RSA* rsa = NULL;
rdpRsaKey* key = NULL;
2017-11-21 13:37:42 +03:00
const BIGNUM* rsa_e = NULL;
const BIGNUM* rsa_n = NULL;
const BIGNUM* rsa_d = NULL;
2019-11-06 17:24:51 +03:00
key = (rdpRsaKey*)calloc(1, sizeof(rdpRsaKey));
if (!key)
return NULL;
2021-08-02 13:13:34 +03:00
bio = BIO_new_mem_buf((const void*)keycontent, strlen(keycontent));
if (!bio)
goto out_free;
rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
BIO_free_all(bio);
if (!rsa)
{
WLog_ERR(TAG, "unable to load RSA key from %s: %s.", keyfile, strerror(errno));
goto out_free;
}
switch (RSA_check_key(rsa))
{
case 0:
WLog_ERR(TAG, "invalid RSA key in %s", keyfile);
goto out_free_rsa;
2014-08-18 21:34:47 +04:00
case 1:
/* Valid key. */
break;
2014-08-18 21:34:47 +04:00
default:
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG, "unexpected error when checking RSA key from %s: %s.", keyfile,
strerror(errno));
goto out_free_rsa;
}
2016-11-21 19:28:54 +03:00
RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
if (BN_num_bytes(rsa_e) > 4)
{
WLog_ERR(TAG, "RSA public exponent too large in %s", keyfile);
goto out_free_rsa;
}
2016-11-21 19:28:54 +03:00
key->ModulusLength = BN_num_bytes(rsa_n);
2019-11-06 17:24:51 +03:00
key->Modulus = (BYTE*)malloc(key->ModulusLength);
if (!key->Modulus)
goto out_free_rsa;
2016-11-21 19:28:54 +03:00
BN_bn2bin(rsa_n, key->Modulus);
2012-09-24 12:40:32 +04:00
crypto_reverse(key->Modulus, key->ModulusLength);
2016-11-21 19:28:54 +03:00
key->PrivateExponentLength = BN_num_bytes(rsa_d);
2019-11-06 17:24:51 +03:00
key->PrivateExponent = (BYTE*)malloc(key->PrivateExponentLength);
if (!key->PrivateExponent)
goto out_free_modulus;
2016-11-21 19:28:54 +03:00
BN_bn2bin(rsa_d, key->PrivateExponent);
2012-09-24 12:40:32 +04:00
crypto_reverse(key->PrivateExponent, key->PrivateExponentLength);
memset(key->exponent, 0, sizeof(key->exponent));
2016-11-21 19:28:54 +03:00
BN_bn2bin(rsa_e, key->exponent + sizeof(key->exponent) - BN_num_bytes(rsa_e));
crypto_reverse(key->exponent, sizeof(key->exponent));
RSA_free(rsa);
return key;
out_free_modulus:
free(key->Modulus);
out_free_rsa:
RSA_free(rsa);
out_free:
free(key);
return NULL;
}
rdpRsaKey* key_new(const char* keyfile)
{
FILE* fp = NULL;
INT64 length;
char* buffer = NULL;
rdpRsaKey* key = NULL;
fp = winpr_fopen(keyfile, "rb");
if (!fp)
{
WLog_ERR(TAG, "unable to open RSA key file %s: %s.", keyfile, strerror(errno));
goto out_free;
}
if (_fseeki64(fp, 0, SEEK_END) < 0)
goto out_free;
if ((length = _ftelli64(fp)) < 0)
goto out_free;
if (_fseeki64(fp, 0, SEEK_SET) < 0)
goto out_free;
2017-11-21 13:37:42 +03:00
buffer = (char*)malloc(length + 1);
if (!buffer)
goto out_free;
2019-11-06 17:24:51 +03:00
if (fread((void*)buffer, length, 1, fp) != 1)
goto out_free;
fclose(fp);
buffer[length] = '\0';
key = key_new_from_content(buffer, keyfile);
free(buffer);
return key;
out_free:
2014-11-17 03:07:07 +03:00
if (fp)
fclose(fp);
2015-05-11 10:07:39 +03:00
free(buffer);
return NULL;
}
2019-07-16 13:26:26 +03:00
rdpRsaKey* key_clone(const rdpRsaKey* key)
{
2019-11-06 17:24:51 +03:00
rdpRsaKey* _key = (rdpRsaKey*)calloc(1, sizeof(rdpRsaKey));
2019-07-16 13:26:26 +03:00
if (!_key)
return NULL;
CopyMemory(_key, key, sizeof(rdpRsaKey));
if (key->Modulus)
{
2019-11-06 17:24:51 +03:00
_key->Modulus = (BYTE*)malloc(key->ModulusLength);
2019-07-16 13:26:26 +03:00
if (!_key->Modulus)
goto out_fail;
CopyMemory(_key->Modulus, key->Modulus, key->ModulusLength);
}
if (key->PrivateExponent)
{
2019-11-06 17:24:51 +03:00
_key->PrivateExponent = (BYTE*)malloc(key->PrivateExponentLength);
2019-07-16 13:26:26 +03:00
if (!_key->PrivateExponent)
goto out_fail;
CopyMemory(_key->PrivateExponent, key->PrivateExponent, key->PrivateExponentLength);
}
return _key;
out_fail:
2020-11-18 09:51:45 +03:00
key_free(_key);
2019-07-16 13:26:26 +03:00
return NULL;
}
2014-08-18 21:34:47 +04:00
void key_free(rdpRsaKey* key)
{
if (!key)
return;
2015-05-11 10:07:39 +03:00
free(key->Modulus);
free(key->PrivateExponent);
free(key);
}
2015-02-06 22:21:26 +03:00
rdpCertificate* certificate_clone(rdpCertificate* certificate)
{
2019-02-07 16:22:28 +03:00
UINT32 index;
2019-11-06 17:24:51 +03:00
rdpCertificate* _certificate = (rdpCertificate*)calloc(1, sizeof(rdpCertificate));
2015-02-06 22:21:26 +03:00
if (!_certificate)
return NULL;
CopyMemory(_certificate, certificate, sizeof(rdpCertificate));
if (certificate->cert_info.ModulusLength)
{
2019-11-06 17:24:51 +03:00
_certificate->cert_info.Modulus = (BYTE*)malloc(certificate->cert_info.ModulusLength);
if (!_certificate->cert_info.Modulus)
goto out_fail;
2017-11-21 13:37:42 +03:00
CopyMemory(_certificate->cert_info.Modulus, certificate->cert_info.Modulus,
certificate->cert_info.ModulusLength);
2015-02-06 22:21:26 +03:00
_certificate->cert_info.ModulusLength = certificate->cert_info.ModulusLength;
}
if (certificate->x509_cert_chain)
{
2019-11-06 17:24:51 +03:00
_certificate->x509_cert_chain = (rdpX509CertChain*)malloc(sizeof(rdpX509CertChain));
if (!_certificate->x509_cert_chain)
goto out_fail;
2019-11-06 17:24:51 +03:00
CopyMemory(_certificate->x509_cert_chain, certificate->x509_cert_chain,
sizeof(rdpX509CertChain));
2015-02-06 22:21:26 +03:00
if (certificate->x509_cert_chain->count)
{
2019-11-06 17:24:51 +03:00
_certificate->x509_cert_chain->array =
(rdpCertBlob*)calloc(certificate->x509_cert_chain->count, sizeof(rdpCertBlob));
if (!_certificate->x509_cert_chain->array)
goto out_fail;
2015-02-06 22:21:26 +03:00
for (index = 0; index < certificate->x509_cert_chain->count; index++)
{
2017-11-21 13:37:42 +03:00
_certificate->x509_cert_chain->array[index].length =
certificate->x509_cert_chain->array[index].length;
2015-02-06 22:21:26 +03:00
if (certificate->x509_cert_chain->array[index].length)
{
2019-11-06 17:24:51 +03:00
_certificate->x509_cert_chain->array[index].data =
(BYTE*)malloc(certificate->x509_cert_chain->array[index].length);
if (!_certificate->x509_cert_chain->array[index].data)
{
2019-02-07 16:22:28 +03:00
for (; index > 0; --index)
{
2019-11-06 17:24:51 +03:00
if (certificate->x509_cert_chain->array[index - 1].length)
free(_certificate->x509_cert_chain->array[index - 1].data);
}
goto out_fail;
}
2017-11-21 13:37:42 +03:00
CopyMemory(_certificate->x509_cert_chain->array[index].data,
certificate->x509_cert_chain->array[index].data,
_certificate->x509_cert_chain->array[index].length);
2015-02-06 22:21:26 +03:00
}
}
}
}
return _certificate;
out_fail:
2020-11-18 09:51:45 +03:00
certificate_free(_certificate);
return NULL;
2015-02-06 22:21:26 +03:00
}
/**
* Instantiate new certificate module.\n
* @param rdp RDP module
* @return new certificate module
*/
rdpCertificate* certificate_new(void)
{
2019-11-06 17:24:51 +03:00
return (rdpCertificate*)calloc(1, sizeof(rdpCertificate));
}
/**
* Free certificate module.
* @param certificate certificate module to be freed
*/
2014-08-18 21:34:47 +04:00
void certificate_free(rdpCertificate* certificate)
{
if (!certificate)
return;
certificate_free_x509_certificate_chain(certificate->x509_cert_chain);
2015-05-11 10:07:39 +03:00
free(certificate->cert_info.Modulus);
free(certificate);
}