2012-02-17 09:58:30 +04:00
|
|
|
/**
|
2012-02-21 09:56:55 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2012-02-17 09:58:30 +04:00
|
|
|
* Certificate Handling
|
|
|
|
*
|
|
|
|
* Copyright 2011 Jiten Pathy
|
2023-02-03 13:40:35 +03:00
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2023-02-09 10:48:11 +03:00
|
|
|
* Copyright 2023 Armin Novak <anovak@thincast.com>
|
|
|
|
* Copyright 2023 Thincast Technologies GmbH
|
2012-02-17 09:58:30 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-02-17 09:58:30 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
#include <winpr/assert.h>
|
|
|
|
#include <winpr/wtypes.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-03-22 23:52:43 +04:00
|
|
|
#include <winpr/file.h>
|
2023-02-03 13:40:35 +03:00
|
|
|
#include <winpr/print.h>
|
|
|
|
#include <winpr/crypto.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
#include <freerdp/crypto/certificate.h>
|
|
|
|
|
|
|
|
#include <openssl/err.h>
|
2012-02-17 09:58:30 +04:00
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/rsa.h>
|
2023-12-08 07:48:36 +03:00
|
|
|
#include <openssl/bn.h>
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-04-25 09:44:55 +03:00
|
|
|
#if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
|
|
|
|
#include <openssl/core_names.h>
|
2023-05-17 14:29:28 +03:00
|
|
|
#include <openssl/param_build.h>
|
|
|
|
#include <openssl/evp.h>
|
2023-04-25 09:44:55 +03:00
|
|
|
#endif
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
#include "certificate.h"
|
|
|
|
#include "cert_common.h"
|
|
|
|
#include "crypto.h"
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
#include "x509_utils.h"
|
|
|
|
#include "privatekey.h"
|
|
|
|
#include "opensslcompat.h"
|
|
|
|
|
|
|
|
#define TAG FREERDP_TAG("core")
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
#define CERTIFICATE_TAG FREERDP_TAG("core.certificate")
|
|
|
|
#ifdef WITH_DEBUG_CERTIFICATE
|
|
|
|
#define DEBUG_CERTIFICATE(...) WLog_DBG(TAG, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define DEBUG_CERTIFICATE(...) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TSSK_KEY_LENGTH 64
|
|
|
|
|
|
|
|
struct rdp_CertBlob
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
UINT32 length;
|
|
|
|
BYTE* data;
|
2021-04-13 12:01:43 +03:00
|
|
|
};
|
2023-02-03 13:40:35 +03:00
|
|
|
typedef struct rdp_CertBlob rdpCertBlob;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
struct rdp_X509CertChain
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
UINT32 count;
|
|
|
|
rdpCertBlob* array;
|
2021-04-13 12:01:43 +03:00
|
|
|
};
|
2023-02-03 13:40:35 +03:00
|
|
|
typedef struct rdp_X509CertChain rdpX509CertChain;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
struct rdp_certificate
|
|
|
|
{
|
|
|
|
X509* x509;
|
|
|
|
STACK_OF(X509) * chain;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
rdpCertInfo cert_info;
|
|
|
|
rdpX509CertChain x509_cert_chain;
|
|
|
|
};
|
2014-09-12 16:36:29 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static const char rsa_magic[4] = "RSA1";
|
|
|
|
|
|
|
|
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" };
|
|
|
|
|
|
|
|
static const BYTE initial_signature[] = {
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01
|
|
|
|
};
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
#if defined(CERT_VALIDATE_RSA)
|
|
|
|
static const BYTE tssk_exponent[] = { 0x5b, 0x7b, 0x88, 0xc0 };
|
|
|
|
#endif
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static void certificate_free_int(rdpCertificate* certificate);
|
|
|
|
static BOOL cert_clone_int(rdpCertificate* dst, const rdpCertificate* src);
|
2018-05-11 12:09:54 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* [MS-RDPBCGR] 5.3.3.2 X.509 Certificate Chains:
|
|
|
|
*
|
|
|
|
* More detail[MS-RDPELE] section 2.2.1.4.2.
|
|
|
|
*/
|
|
|
|
static BOOL cert_blob_copy(rdpCertBlob* dst, const rdpCertBlob* src);
|
|
|
|
static void cert_blob_free(rdpCertBlob* blob);
|
|
|
|
static BOOL cert_blob_write(const rdpCertBlob* blob, wStream* s);
|
|
|
|
static BOOL cert_blob_read(rdpCertBlob* blob, wStream* s);
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
BOOL cert_blob_read(rdpCertBlob* blob, wStream* s)
|
2020-11-18 09:51:45 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
UINT32 certLength = 0;
|
|
|
|
WINPR_ASSERT(blob);
|
|
|
|
cert_blob_free(blob);
|
2021-10-04 09:19:53 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
|
2023-02-06 15:15:49 +03:00
|
|
|
goto fail;
|
2021-10-04 09:19:53 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Read_UINT32(s, certLength);
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, certLength))
|
2023-02-06 15:15:49 +03:00
|
|
|
goto fail;
|
2022-10-28 09:09:27 +03:00
|
|
|
|
2023-05-23 09:04:59 +03:00
|
|
|
DEBUG_CERTIFICATE("X.509 Certificate length:%" PRIu32 "", certLength);
|
2023-02-03 13:40:35 +03:00
|
|
|
blob->data = (BYTE*)malloc(certLength);
|
|
|
|
|
|
|
|
if (!blob->data)
|
2023-02-06 15:15:49 +03:00
|
|
|
goto fail;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Read(s, blob->data, certLength);
|
|
|
|
blob->length = certLength;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
return TRUE;
|
2023-02-06 15:15:49 +03:00
|
|
|
|
|
|
|
fail:
|
|
|
|
cert_blob_free(blob);
|
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
BOOL cert_blob_write(const rdpCertBlob* blob, wStream* s)
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(blob);
|
2021-10-04 09:19:53 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, 4 + blob->length))
|
2020-11-18 09:51:45 +03:00
|
|
|
return FALSE;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Write_UINT32(s, blob->length);
|
|
|
|
Stream_Write(s, blob->data, blob->length);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cert_blob_free(rdpCertBlob* blob)
|
|
|
|
{
|
|
|
|
if (!blob)
|
|
|
|
return;
|
|
|
|
free(blob->data);
|
|
|
|
blob->data = NULL;
|
|
|
|
blob->length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read X.509 Certificate
|
|
|
|
*/
|
|
|
|
|
2023-02-06 15:15:49 +03:00
|
|
|
static BOOL is_rsa_key(const X509* x509)
|
|
|
|
{
|
|
|
|
EVP_PKEY* evp = X509_get0_pubkey(x509);
|
|
|
|
if (!evp)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return (EVP_PKEY_id(evp) == EVP_PKEY_RSA);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL certificate_read_x509_certificate(const rdpCertBlob* cert, rdpCertInfo* info)
|
2023-02-03 13:40:35 +03:00
|
|
|
{
|
|
|
|
wStream sbuffer = { 0 };
|
|
|
|
wStream* s = NULL;
|
|
|
|
size_t length = 0;
|
|
|
|
BYTE padding = 0;
|
|
|
|
UINT32 version = 0;
|
|
|
|
size_t modulus_length = 0;
|
|
|
|
size_t exponent_length = 0;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
if (!cert || !info)
|
2020-11-18 09:51:45 +03:00
|
|
|
return FALSE;
|
2021-10-04 09:19:53 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
cert_info_free(info);
|
2023-02-06 15:15:49 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
s = Stream_StaticConstInit(&sbuffer, cert->data, cert->length);
|
|
|
|
|
|
|
|
if (!s)
|
2020-11-18 09:51:45 +03:00
|
|
|
return FALSE;
|
2021-10-04 09:19:53 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!ber_read_sequence_tag(s, &length)) /* Certificate (SEQUENCE) */
|
|
|
|
goto error;
|
2021-10-04 09:19:53 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2021-10-04 09:19:53 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!ber_read_sequence_tag(s, &length)) /* TBSCertificate (SEQUENCE) */
|
|
|
|
goto error;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2014-07-18 05:15:22 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!ber_read_contextual_tag(s, 0, &length, TRUE)) /* Explicit Contextual Tag [0] */
|
|
|
|
goto error;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!ber_read_integer(s, &version)) /* version (INTEGER) */
|
|
|
|
goto error;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
|
|
|
version++;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* serialNumber */
|
|
|
|
if (!ber_read_integer(s, NULL)) /* CertificateSerialNumber (INTEGER) */
|
|
|
|
goto error;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* signature */
|
|
|
|
if (!ber_read_sequence_tag(s, &length) ||
|
|
|
|
!Stream_SafeSeek(s, length)) /* AlgorithmIdentifier (SEQUENCE) */
|
|
|
|
goto error;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* issuer */
|
|
|
|
if (!ber_read_sequence_tag(s, &length) || !Stream_SafeSeek(s, length)) /* Name (SEQUENCE) */
|
|
|
|
goto error;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* validity */
|
|
|
|
if (!ber_read_sequence_tag(s, &length) || !Stream_SafeSeek(s, length)) /* Validity (SEQUENCE) */
|
|
|
|
goto error;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* subject */
|
|
|
|
if (!ber_read_sequence_tag(s, &length) || !Stream_SafeSeek(s, length)) /* Name (SEQUENCE) */
|
|
|
|
goto error;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* subjectPublicKeyInfo */
|
|
|
|
if (!ber_read_sequence_tag(s, &length)) /* SubjectPublicKeyInfo (SEQUENCE) */
|
|
|
|
goto error;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* subjectPublicKeyInfo::AlgorithmIdentifier */
|
|
|
|
if (!ber_read_sequence_tag(s, &length) ||
|
|
|
|
!Stream_SafeSeek(s, length)) /* AlgorithmIdentifier (SEQUENCE) */
|
|
|
|
goto error;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* subjectPublicKeyInfo::subjectPublicKey */
|
|
|
|
if (!ber_read_bit_string(s, &length, &padding)) /* BIT_STRING */
|
|
|
|
goto error;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2015-06-11 12:16:45 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* RSAPublicKey (SEQUENCE) */
|
|
|
|
if (!ber_read_sequence_tag(s, &length)) /* SEQUENCE */
|
|
|
|
goto error;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!ber_read_integer_length(s, &modulus_length)) /* modulus (INTEGER) */
|
|
|
|
goto error;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
|
|
|
|
|
|
|
/* skip zero padding, if any */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
|
|
|
|
goto error;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Peek_UINT8(s, padding);
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (padding == 0)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!Stream_SafeSeek(s, 1))
|
|
|
|
goto error;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
modulus_length--;
|
|
|
|
}
|
|
|
|
} while (padding == 0);
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!cert_info_read_modulus(info, modulus_length, s))
|
|
|
|
goto error;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!ber_read_integer_length(s, &exponent_length)) /* publicExponent (INTEGER) */
|
|
|
|
goto error;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
error++;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!cert_info_read_exponent(info, exponent_length, s))
|
|
|
|
goto error;
|
|
|
|
return TRUE;
|
|
|
|
error:
|
|
|
|
WLog_ERR(TAG, "error reading when reading certificate: part=%s error=%d",
|
|
|
|
certificate_read_errors[error], error);
|
|
|
|
cert_info_free(info);
|
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/**
|
|
|
|
* 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)
|
2015-06-11 00:33:58 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
rdpX509CertChain x509_cert_chain = { 0 };
|
2015-06-11 10:58:34 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
x509_cert_chain.array = (rdpCertBlob*)calloc(count, sizeof(rdpCertBlob));
|
|
|
|
|
|
|
|
if (x509_cert_chain.array)
|
|
|
|
x509_cert_chain.count = count;
|
|
|
|
|
|
|
|
return x509_cert_chain;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/**
|
|
|
|
* 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-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!x509_cert_chain)
|
|
|
|
return;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (x509_cert_chain->array)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
for (UINT32 i = 0; i < x509_cert_chain->count; i++)
|
|
|
|
{
|
|
|
|
rdpCertBlob* element = &x509_cert_chain->array[i];
|
|
|
|
cert_blob_free(element);
|
|
|
|
}
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
free(x509_cert_chain->array);
|
2015-06-11 00:33:58 +03:00
|
|
|
}
|
|
|
|
|
2023-05-17 14:29:28 +03:00
|
|
|
#if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
|
|
|
|
static OSSL_PARAM* get_params(const BIGNUM* e, const BIGNUM* mod)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(e);
|
|
|
|
WINPR_ASSERT(mod);
|
|
|
|
|
|
|
|
OSSL_PARAM* parameters = NULL;
|
|
|
|
OSSL_PARAM_BLD* param = OSSL_PARAM_BLD_new();
|
|
|
|
if (!param)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
const int bits = BN_num_bits(e);
|
|
|
|
if ((bits < 0) || (bits > 32))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
UINT ie = 0;
|
|
|
|
const int ne = BN_bn2nativepad(e, (BYTE*)&ie, sizeof(ie));
|
|
|
|
if ((ne < 0) || (ne > 4))
|
|
|
|
goto fail;
|
|
|
|
if (OSSL_PARAM_BLD_push_BN(param, OSSL_PKEY_PARAM_RSA_N, mod) != 1)
|
|
|
|
goto fail;
|
|
|
|
if (OSSL_PARAM_BLD_push_uint(param, OSSL_PKEY_PARAM_RSA_E, ie) != 1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
parameters = OSSL_PARAM_BLD_to_param(param);
|
|
|
|
fail:
|
|
|
|
OSSL_PARAM_BLD_free(param);
|
|
|
|
|
|
|
|
return parameters;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static BOOL update_x509_from_info(rdpCertificate* cert)
|
2015-06-11 00:33:58 +03:00
|
|
|
{
|
2021-04-13 12:01:43 +03:00
|
|
|
BOOL rc = FALSE;
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
X509_free(cert->x509);
|
|
|
|
cert->x509 = NULL;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
rdpCertInfo* info = &cert->cert_info;
|
|
|
|
|
|
|
|
BIGNUM* e = BN_new();
|
|
|
|
BIGNUM* mod = BN_new();
|
2023-04-25 09:44:55 +03:00
|
|
|
#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
|
|
|
|
RSA* rsa = RSA_new();
|
|
|
|
if (!rsa)
|
|
|
|
goto fail;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!mod || !e)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!BN_bin2bn(info->Modulus, info->ModulusLength, mod))
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!BN_bin2bn(info->exponent, sizeof(info->exponent), e))
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
|
|
|
|
2023-04-25 09:44:55 +03:00
|
|
|
#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
|
2023-02-03 13:40:35 +03:00
|
|
|
const int rec = RSA_set0_key(rsa, mod, e, NULL);
|
|
|
|
if (rec != 1)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
cert->x509 = x509_from_rsa(rsa);
|
2023-04-25 09:44:55 +03:00
|
|
|
#else
|
2023-05-17 14:29:28 +03:00
|
|
|
EVP_PKEY* pkey = NULL;
|
|
|
|
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
|
|
|
|
if (!ctx)
|
2023-04-25 09:44:55 +03:00
|
|
|
goto fail2;
|
2023-05-17 14:29:28 +03:00
|
|
|
const int xx = EVP_PKEY_fromdata_init(ctx);
|
|
|
|
if (xx != 1)
|
2023-04-25 09:44:55 +03:00
|
|
|
goto fail2;
|
2023-05-17 14:29:28 +03:00
|
|
|
OSSL_PARAM* parameters = get_params(e, mod);
|
|
|
|
if (!parameters)
|
|
|
|
goto fail2;
|
|
|
|
|
|
|
|
const int rc2 = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_PUBLIC_KEY, parameters);
|
|
|
|
OSSL_PARAM_free(parameters);
|
|
|
|
if (rc2 <= 0)
|
2023-04-25 09:44:55 +03:00
|
|
|
goto fail2;
|
|
|
|
|
|
|
|
cert->x509 = X509_new();
|
|
|
|
if (!cert->x509)
|
|
|
|
goto fail2;
|
|
|
|
if (X509_set_pubkey(cert->x509, pkey) != 1)
|
|
|
|
{
|
|
|
|
X509_free(cert->x509);
|
|
|
|
cert->x509 = NULL;
|
|
|
|
}
|
|
|
|
fail2:
|
|
|
|
EVP_PKEY_free(pkey);
|
2023-05-17 14:29:28 +03:00
|
|
|
EVP_PKEY_CTX_free(ctx);
|
2023-04-25 09:44:55 +03:00
|
|
|
#endif
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!cert->x509)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
rc = TRUE;
|
|
|
|
|
|
|
|
fail:
|
2023-04-25 09:44:55 +03:00
|
|
|
#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
|
2023-02-03 13:40:35 +03:00
|
|
|
if (rsa)
|
|
|
|
RSA_free(rsa);
|
2021-04-13 12:01:43 +03:00
|
|
|
else
|
2023-04-25 09:44:55 +03:00
|
|
|
#endif
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
BN_free(mod);
|
|
|
|
BN_free(e);
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static BOOL certificate_process_server_public_key(rdpCertificate* cert, wStream* s, UINT32 length)
|
2021-05-27 09:38:59 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
char magic[sizeof(rsa_magic)] = { 0 };
|
|
|
|
UINT32 keylen = 0;
|
|
|
|
UINT32 bitlen = 0;
|
|
|
|
UINT32 datalen = 0;
|
2021-05-27 09:38:59 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
WINPR_ASSERT(s);
|
2021-05-27 09:38:59 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 20))
|
2021-05-27 09:38:59 +03:00
|
|
|
return FALSE;
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
Stream_Read(s, magic, sizeof(magic));
|
|
|
|
|
|
|
|
if (memcmp(magic, rsa_magic, sizeof(magic)) != 0)
|
2021-05-27 09:38:59 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WLog_ERR(TAG, "magic error");
|
2021-05-27 09:38:59 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
rdpCertInfo* info = &cert->cert_info;
|
|
|
|
cert_info_free(info);
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Read_UINT32(s, keylen);
|
|
|
|
Stream_Read_UINT32(s, bitlen);
|
|
|
|
Stream_Read_UINT32(s, datalen);
|
|
|
|
Stream_Read(s, info->exponent, 4);
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if ((keylen <= 8) || (!Stream_CheckAndLogRequiredLength(TAG, s, keylen)))
|
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
info->ModulusLength = keylen - 8;
|
2023-06-05 13:16:57 +03:00
|
|
|
BYTE* tmp = realloc(info->Modulus, info->ModulusLength);
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
if (!tmp)
|
|
|
|
return FALSE;
|
|
|
|
info->Modulus = tmp;
|
|
|
|
|
|
|
|
Stream_Read(s, info->Modulus, info->ModulusLength);
|
|
|
|
Stream_Seek(s, 8); /* 8 bytes of zero padding */
|
|
|
|
return update_x509_from_info(cert);
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static BOOL certificate_process_server_public_signature(rdpCertificate* certificate,
|
|
|
|
const BYTE* sigdata, size_t sigdatalen,
|
|
|
|
wStream* s, UINT32 siglen)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(certificate);
|
|
|
|
#if defined(CERT_VALIDATE_PADDING) || defined(CERT_VALIDATE_RSA)
|
|
|
|
size_t i, sum;
|
|
|
|
#endif
|
|
|
|
#if defined(CERT_VALIDATE_RSA)
|
|
|
|
BYTE sig[TSSK_KEY_LENGTH];
|
|
|
|
#endif
|
|
|
|
BYTE encsig[TSSK_KEY_LENGTH + 8];
|
|
|
|
#if defined(CERT_VALIDATE_MD5) && defined(CERT_VALIDATE_RSA)
|
|
|
|
BYTE md5hash[WINPR_MD5_DIGEST_LENGTH];
|
|
|
|
#endif
|
|
|
|
#if !defined(CERT_VALIDATE_MD5) || !defined(CERT_VALIDATE_RSA)
|
|
|
|
(void)sigdata;
|
|
|
|
(void)sigdatalen;
|
|
|
|
#endif
|
|
|
|
(void)certificate;
|
|
|
|
/* 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. */
|
|
|
|
#if defined(CERT_VALIDATE_MD5)
|
|
|
|
|
|
|
|
if (!winpr_Digest(WINPR_MD_MD5, sigdata, sigdatalen, md5hash, sizeof(md5hash)))
|
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
#endif
|
|
|
|
Stream_Read(s, encsig, siglen);
|
|
|
|
|
|
|
|
if (siglen < 8)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Last 8 bytes shall be all zero. */
|
|
|
|
#if defined(CERT_VALIDATE_PADDING)
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
for (sum = 0, i = sizeof(encsig) - 8; i < sizeof(encsig); i++)
|
|
|
|
sum += encsig[i];
|
|
|
|
|
|
|
|
if (sum != 0)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WLog_ERR(TAG, "invalid signature");
|
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
#endif
|
|
|
|
#if defined(CERT_VALIDATE_RSA)
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40: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;
|
|
|
|
}
|
2021-05-27 09:38:59 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* Verify signature. */
|
|
|
|
/* Do not bother with validation of server proprietary certificate as described above. */
|
|
|
|
#if defined(CERT_VALIDATE_MD5)
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (memcmp(md5hash, sig, sizeof(md5hash)) != 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "invalid signature");
|
2021-04-13 12:01:43 +03:00
|
|
|
return FALSE;
|
2023-02-03 13:40:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#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];
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (sig[16] != 0x00 || sum != 0xFF * (62 - 17) || sig[62] != 0x01)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WLog_ERR(TAG, "invalid signature");
|
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
#endif
|
|
|
|
return TRUE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static BOOL certificate_read_server_proprietary_certificate(rdpCertificate* certificate, wStream* s)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
UINT32 dwSigAlgId = 0;
|
|
|
|
UINT32 dwKeyAlgId = 0;
|
|
|
|
UINT16 wPublicKeyBlobType = 0;
|
|
|
|
UINT16 wPublicKeyBlobLen = 0;
|
|
|
|
UINT16 wSignatureBlobType = 0;
|
|
|
|
UINT16 wSignatureBlobLen = 0;
|
|
|
|
size_t sigdatalen = 0;
|
|
|
|
|
|
|
|
WINPR_ASSERT(certificate);
|
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
|
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* -4, because we need to include dwVersion */
|
2023-06-07 16:38:21 +03:00
|
|
|
const BYTE* sigdata = Stream_PointerAs(s, const BYTE) - 4;
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Read_UINT32(s, dwSigAlgId);
|
|
|
|
Stream_Read_UINT32(s, dwKeyAlgId);
|
2012-06-29 05:05:10 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!((dwSigAlgId == SIGNATURE_ALG_RSA) && (dwKeyAlgId == KEY_EXCHANGE_ALG_RSA)))
|
2021-05-27 09:38:59 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"unsupported signature or key algorithm, dwSigAlgId=%" PRIu32
|
|
|
|
" dwKeyAlgId=%" PRIu32 "",
|
|
|
|
dwSigAlgId, dwKeyAlgId);
|
2015-07-13 15:16:04 +03:00
|
|
|
return FALSE;
|
2021-05-27 09:38:59 +03:00
|
|
|
}
|
2012-06-29 05:05:10 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Read_UINT16(s, wPublicKeyBlobType);
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (wPublicKeyBlobType != BB_RSA_KEY_BLOB)
|
2015-06-10 11:34:02 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WLog_ERR(TAG, "unsupported public key blob type %" PRIu16 "", wPublicKeyBlobType);
|
|
|
|
return FALSE;
|
2015-07-13 15:16:04 +03:00
|
|
|
}
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Read_UINT16(s, wPublicKeyBlobLen);
|
2021-05-27 09:38:59 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, wPublicKeyBlobLen))
|
|
|
|
return FALSE;
|
2012-06-29 05:05:10 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!certificate_process_server_public_key(certificate, s, wPublicKeyBlobLen))
|
|
|
|
return FALSE;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
|
|
|
|
return FALSE;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-06-07 16:38:21 +03:00
|
|
|
sigdatalen = Stream_PointerAs(s, const BYTE) - sigdata;
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Read_UINT16(s, wSignatureBlobType);
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (wSignatureBlobType != BB_RSA_SIGNATURE_BLOB)
|
2015-12-09 20:29:41 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WLog_ERR(TAG, "unsupported blob signature %" PRIu16 "", wSignatureBlobType);
|
|
|
|
return FALSE;
|
2015-12-09 20:29:41 +03:00
|
|
|
}
|
2015-06-11 10:23:32 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Read_UINT16(s, wSignatureBlobLen);
|
2015-06-11 10:23:32 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, wSignatureBlobLen))
|
|
|
|
return FALSE;
|
2012-06-29 05:05:10 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (wSignatureBlobLen != 72)
|
2012-06-29 05:05:10 +04:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WLog_ERR(TAG, "invalid signature length (got %" PRIu16 ", expected 72)", wSignatureBlobLen);
|
|
|
|
return FALSE;
|
2012-06-29 05:05:10 +04:00
|
|
|
}
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!certificate_process_server_public_signature(certificate, sigdata, sigdatalen, s,
|
|
|
|
wSignatureBlobLen))
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WLog_ERR(TAG, "unable to parse server public signature");
|
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
return TRUE;
|
2012-06-29 05:05:10 +04:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* [MS-RDPBCGR] 2.2.1.4.3.1.1.1 RSA Public Key (RSA_PUBLIC_KEY) */
|
|
|
|
static BOOL cert_write_rsa_public_key(wStream* s, const rdpCertificate* cert)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
2023-02-06 15:15:49 +03:00
|
|
|
WINPR_ASSERT(freerdp_certificate_is_rsa(cert));
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
const rdpCertInfo* info = &cert->cert_info;
|
|
|
|
|
|
|
|
const UINT32 keyLen = info->ModulusLength + 8;
|
|
|
|
const UINT32 bitLen = info->ModulusLength * 8;
|
|
|
|
const UINT32 dataLen = (bitLen / 8) - 1;
|
|
|
|
const size_t pubExpLen = sizeof(info->exponent);
|
|
|
|
const BYTE* pubExp = info->exponent;
|
|
|
|
const BYTE* modulus = info->Modulus;
|
|
|
|
|
2023-07-28 15:03:14 +03:00
|
|
|
const size_t wPublicKeyBlobLen = 16 + pubExpLen + keyLen;
|
|
|
|
WINPR_ASSERT(wPublicKeyBlobLen <= UINT16_MAX);
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, 2 + wPublicKeyBlobLen))
|
|
|
|
return FALSE;
|
2023-07-28 15:03:14 +03:00
|
|
|
Stream_Write_UINT16(s, (UINT16)wPublicKeyBlobLen);
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Write(s, rsa_magic, sizeof(rsa_magic));
|
|
|
|
Stream_Write_UINT32(s, keyLen);
|
|
|
|
Stream_Write_UINT32(s, bitLen);
|
|
|
|
Stream_Write_UINT32(s, dataLen);
|
|
|
|
Stream_Write(s, pubExp, pubExpLen);
|
|
|
|
Stream_Write(s, modulus, info->ModulusLength);
|
|
|
|
Stream_Zero(s, 8);
|
|
|
|
return TRUE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static BOOL cert_write_rsa_signature(wStream* s, const void* sigData, size_t sigDataLen)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
BYTE encryptedSignature[TSSK_KEY_LENGTH] = { 0 };
|
|
|
|
BYTE signature[sizeof(initial_signature)] = { 0 };
|
|
|
|
|
|
|
|
memcpy(signature, initial_signature, sizeof(initial_signature));
|
|
|
|
if (!winpr_Digest(WINPR_MD_MD5, sigData, sigDataLen, signature, sizeof(signature)))
|
2021-04-13 12:01:43 +03:00
|
|
|
return FALSE;
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
crypto_rsa_private_encrypt(signature, sizeof(signature), priv_key_tssk, encryptedSignature,
|
|
|
|
sizeof(encryptedSignature));
|
|
|
|
|
|
|
|
if (!Stream_EnsureRemainingCapacity(s, 2 * sizeof(UINT16) + sizeof(encryptedSignature) + 8))
|
|
|
|
return FALSE;
|
|
|
|
Stream_Write_UINT16(s, BB_RSA_SIGNATURE_BLOB);
|
|
|
|
Stream_Write_UINT16(s, sizeof(encryptedSignature) + 8); /* wSignatureBlobLen */
|
|
|
|
Stream_Write(s, encryptedSignature, sizeof(encryptedSignature));
|
|
|
|
Stream_Zero(s, 8);
|
|
|
|
return TRUE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* [MS-RDPBCGR] 2.2.1.4.3.1.1 Server Proprietary Certificate (PROPRIETARYSERVERCERTIFICATE) */
|
|
|
|
static BOOL cert_write_server_certificate_v1(wStream* s, const rdpCertificate* certificate)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
const size_t start = Stream_GetPosition(s);
|
2023-06-07 16:38:21 +03:00
|
|
|
const BYTE* sigData = Stream_PointerAs(s, const BYTE) - sizeof(UINT32);
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(start >= 4);
|
|
|
|
if (!Stream_EnsureRemainingCapacity(s, 10))
|
2021-05-27 09:38:59 +03:00
|
|
|
return FALSE;
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Write_UINT32(s, SIGNATURE_ALG_RSA);
|
|
|
|
Stream_Write_UINT32(s, KEY_EXCHANGE_ALG_RSA);
|
|
|
|
Stream_Write_UINT16(s, BB_RSA_KEY_BLOB);
|
|
|
|
if (!cert_write_rsa_public_key(s, certificate))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
const size_t end = Stream_GetPosition(s);
|
|
|
|
return cert_write_rsa_signature(s, sigData, end - start + sizeof(UINT32));
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL cert_write_server_certificate_v2(wStream* s, const rdpCertificate* certificate)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(certificate);
|
|
|
|
|
|
|
|
const rdpX509CertChain* chain = &certificate->x509_cert_chain;
|
|
|
|
const size_t padding = 8ull + 4ull * chain->count;
|
|
|
|
|
2023-04-24 10:28:11 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, sizeof(UINT32)))
|
2023-02-03 13:40:35 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Write_UINT32(s, chain->count);
|
|
|
|
for (UINT32 x = 0; x < chain->count; x++)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
const rdpCertBlob* cert = &chain->array[x];
|
|
|
|
if (!cert_blob_write(cert, s))
|
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
|
2023-04-24 10:28:11 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, padding))
|
2023-02-03 13:40:35 +03:00
|
|
|
return FALSE;
|
|
|
|
Stream_Zero(s, padding);
|
2023-04-24 10:28:11 +03:00
|
|
|
return TRUE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
SSIZE_T freerdp_certificate_write_server_cert(const rdpCertificate* certificate, UINT32 dwVersion,
|
|
|
|
wStream* s)
|
2015-06-09 16:33:13 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!certificate)
|
|
|
|
return -1;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
const size_t start = Stream_GetPosition(s);
|
|
|
|
if (!Stream_EnsureRemainingCapacity(s, 4))
|
|
|
|
return -1;
|
|
|
|
Stream_Write_UINT32(s, dwVersion);
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
switch (dwVersion & CERT_CHAIN_VERSION_MASK)
|
|
|
|
{
|
|
|
|
case CERT_CHAIN_VERSION_1:
|
|
|
|
if (!cert_write_server_certificate_v1(s, certificate))
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
case CERT_CHAIN_VERSION_2:
|
|
|
|
if (!cert_write_server_certificate_v2(s, certificate))
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WLog_ERR(TAG, "invalid certificate chain version:%" PRIu32 "",
|
|
|
|
dwVersion & CERT_CHAIN_VERSION_MASK);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t end = Stream_GetPosition(s);
|
|
|
|
return end - start;
|
|
|
|
}
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/**
|
|
|
|
* Read an X.509 Certificate Chain.
|
2023-10-11 16:05:05 +03:00
|
|
|
* @param cert certificate module
|
2023-02-03 13:40:35 +03:00
|
|
|
* @param s stream
|
2023-10-11 16:05:05 +03:00
|
|
|
* @return \b TRUE for success, \b FALSE otherwise.
|
2023-02-03 13:40:35 +03:00
|
|
|
*/
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static BOOL certificate_read_server_x509_certificate_chain(rdpCertificate* cert, wStream* s)
|
|
|
|
{
|
|
|
|
UINT32 numCertBlobs = 0;
|
|
|
|
DEBUG_CERTIFICATE("Server X.509 Certificate Chain");
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
|
|
|
|
return FALSE;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Read_UINT32(s, numCertBlobs); /* numCertBlobs */
|
2023-02-06 15:15:49 +03:00
|
|
|
certificate_free_x509_certificate_chain(&cert->x509_cert_chain);
|
2023-02-03 13:40:35 +03:00
|
|
|
cert->x509_cert_chain = certificate_new_x509_certificate_chain(numCertBlobs);
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
for (UINT32 i = 0; i < cert->x509_cert_chain.count; i++)
|
|
|
|
{
|
|
|
|
rdpCertBlob* blob = &cert->x509_cert_chain.array[i];
|
|
|
|
if (!cert_blob_read(blob, s))
|
|
|
|
return FALSE;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-02-06 15:15:49 +03:00
|
|
|
if (numCertBlobs - i == 1)
|
2023-02-03 13:40:35 +03:00
|
|
|
{
|
2023-02-06 15:15:49 +03:00
|
|
|
DEBUG_CERTIFICATE("Terminal Server Certificate");
|
2015-06-11 12:16:45 +03:00
|
|
|
|
2023-02-06 15:15:49 +03:00
|
|
|
BOOL res = certificate_read_x509_certificate(blob, &cert->cert_info);
|
|
|
|
|
|
|
|
if (res)
|
2023-02-03 13:40:35 +03:00
|
|
|
{
|
2023-02-06 15:15:49 +03:00
|
|
|
if (!update_x509_from_info(cert))
|
|
|
|
res = FALSE;
|
2023-02-03 13:40:35 +03:00
|
|
|
}
|
2015-06-11 12:16:45 +03:00
|
|
|
|
2023-02-06 15:15:49 +03:00
|
|
|
if (!res)
|
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
return FALSE;
|
2023-02-06 15:15:49 +03:00
|
|
|
}
|
2015-06-11 12:16:45 +03:00
|
|
|
|
2023-05-23 09:04:59 +03:00
|
|
|
DEBUG_CERTIFICATE("modulus length:%" PRIu32 "", cert->cert_info.ModulusLength);
|
2023-02-03 13:40:35 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-11 12:16:45 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
return update_x509_from_info(cert);
|
|
|
|
}
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static BOOL certificate_write_server_x509_certificate_chain(const rdpCertificate* certificate,
|
|
|
|
wStream* s)
|
|
|
|
{
|
|
|
|
UINT32 numCertBlobs = 0;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(certificate);
|
|
|
|
WINPR_ASSERT(s);
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
numCertBlobs = certificate->x509_cert_chain.count;
|
2015-06-09 16:33:13 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, 4))
|
|
|
|
return FALSE;
|
|
|
|
Stream_Write_UINT32(s, numCertBlobs); /* numCertBlobs */
|
|
|
|
|
|
|
|
for (UINT32 i = 0; i < numCertBlobs; i++)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
const rdpCertBlob* cert = &certificate->x509_cert_chain.array[i];
|
|
|
|
if (!cert_blob_write(cert, s))
|
|
|
|
return FALSE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
return TRUE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2015-07-13 15:16:04 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/**
|
|
|
|
* Read a Server Certificate.
|
|
|
|
* @param certificate certificate module
|
|
|
|
* @param server_cert server certificate
|
|
|
|
* @param length certificate length
|
|
|
|
*/
|
|
|
|
|
|
|
|
BOOL freerdp_certificate_read_server_cert(rdpCertificate* certificate, const BYTE* server_cert,
|
|
|
|
size_t length)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
BOOL ret = FALSE;
|
|
|
|
wStream *s, sbuffer;
|
|
|
|
UINT32 dwVersion = 0;
|
|
|
|
|
|
|
|
WINPR_ASSERT(certificate);
|
|
|
|
if (length < 4) /* NULL certificate is not an error see #1795 */
|
2024-01-11 13:12:12 +03:00
|
|
|
{
|
|
|
|
WLog_DBG(TAG, "Received empty certificate, ignoring...");
|
2023-02-03 13:40:35 +03:00
|
|
|
return TRUE;
|
2024-01-11 13:12:12 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(server_cert);
|
|
|
|
s = Stream_StaticConstInit(&sbuffer, server_cert, length);
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
2015-07-13 15:16:04 +03:00
|
|
|
return FALSE;
|
2023-02-03 13:40:35 +03:00
|
|
|
}
|
2021-05-27 09:38:59 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
Stream_Read_UINT32(s, dwVersion); /* dwVersion (4 bytes) */
|
2021-05-27 09:38:59 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
switch (dwVersion & CERT_CHAIN_VERSION_MASK)
|
|
|
|
{
|
|
|
|
case CERT_CHAIN_VERSION_1:
|
|
|
|
ret = certificate_read_server_proprietary_certificate(certificate, s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CERT_CHAIN_VERSION_2:
|
|
|
|
ret = certificate_read_server_x509_certificate_chain(certificate, s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
WLog_ERR(TAG, "invalid certificate chain version:%" PRIu32 "",
|
|
|
|
dwVersion & CERT_CHAIN_VERSION_MASK);
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static BOOL cert_blob_copy(rdpCertBlob* dst, const rdpCertBlob* src)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(dst);
|
|
|
|
WINPR_ASSERT(src);
|
|
|
|
|
2023-02-06 15:15:49 +03:00
|
|
|
cert_blob_free(dst);
|
2023-02-03 13:40:35 +03:00
|
|
|
if (src->length > 0)
|
|
|
|
{
|
|
|
|
dst->data = malloc(src->length);
|
|
|
|
if (!dst->data)
|
|
|
|
return FALSE;
|
|
|
|
dst->length = src->length;
|
|
|
|
memcpy(dst->data, src->data, src->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2019-02-07 16:18:02 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static BOOL cert_x509_chain_copy(rdpX509CertChain* cert, const rdpX509CertChain* src)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
|
2023-02-06 15:15:49 +03:00
|
|
|
certificate_free_x509_certificate_chain(cert);
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!src)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (src->count > 0)
|
2015-12-09 20:29:41 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
cert->array = calloc(src->count, sizeof(rdpCertBlob));
|
|
|
|
if (!cert->array)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
cert->count = src->count;
|
|
|
|
|
|
|
|
for (UINT32 x = 0; x < cert->count; x++)
|
|
|
|
{
|
|
|
|
const rdpCertBlob* srcblob = &src->array[x];
|
|
|
|
rdpCertBlob* dstblob = &cert->array[x];
|
|
|
|
|
|
|
|
if (!cert_blob_copy(dstblob, srcblob))
|
|
|
|
{
|
|
|
|
certificate_free_x509_certificate_chain(cert);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 20:29:41 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL cert_clone_int(rdpCertificate* dst, const rdpCertificate* src)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(dst);
|
|
|
|
WINPR_ASSERT(src);
|
|
|
|
|
|
|
|
if (src->x509)
|
2015-07-13 15:16:04 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
dst->x509 = X509_dup(src->x509);
|
|
|
|
if (!dst->x509)
|
|
|
|
return FALSE;
|
2015-07-13 15:16:04 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
if (!cert_info_clone(&dst->cert_info, &src->cert_info))
|
|
|
|
return FALSE;
|
|
|
|
return cert_x509_chain_copy(&dst->x509_cert_chain, &src->x509_cert_chain);
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
rdpCertificate* freerdp_certificate_clone(const rdpCertificate* certificate)
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!certificate)
|
|
|
|
return NULL;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
rdpCertificate* _certificate = freerdp_certificate_new();
|
|
|
|
|
|
|
|
if (!_certificate)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!cert_clone_int(_certificate, certificate))
|
|
|
|
goto out_fail;
|
|
|
|
|
|
|
|
return _certificate;
|
|
|
|
out_fail:
|
|
|
|
|
|
|
|
freerdp_certificate_free(_certificate);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate new certificate module.
|
|
|
|
* @return new certificate module
|
|
|
|
*/
|
|
|
|
|
|
|
|
rdpCertificate* freerdp_certificate_new(void)
|
|
|
|
{
|
|
|
|
return (rdpCertificate*)calloc(1, sizeof(rdpCertificate));
|
|
|
|
}
|
|
|
|
|
|
|
|
void certificate_free_int(rdpCertificate* cert)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
|
|
|
|
if (cert->x509)
|
|
|
|
X509_free(cert->x509);
|
|
|
|
if (cert->chain)
|
|
|
|
sk_X509_free(cert->chain);
|
|
|
|
|
|
|
|
certificate_free_x509_certificate_chain(&cert->x509_cert_chain);
|
|
|
|
cert_info_free(&cert->cert_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free certificate module.
|
2023-10-11 16:05:05 +03:00
|
|
|
* @param cert certificate module to be freed
|
2023-02-03 13:40:35 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
void freerdp_certificate_free(rdpCertificate* cert)
|
|
|
|
{
|
|
|
|
if (!cert)
|
|
|
|
return;
|
|
|
|
|
|
|
|
certificate_free_int(cert);
|
|
|
|
free(cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL freerdp_rsa_from_x509(rdpCertificate* cert)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
|
|
|
|
WINPR_ASSERT(cert);
|
2015-06-11 12:34:22 +03:00
|
|
|
|
2023-02-06 15:15:49 +03:00
|
|
|
if (!freerdp_certificate_is_rsa(cert))
|
|
|
|
return TRUE;
|
2018-11-30 11:55:10 +03:00
|
|
|
|
2023-06-07 11:20:43 +03:00
|
|
|
#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
|
2023-02-03 13:40:35 +03:00
|
|
|
RSA* rsa = NULL;
|
2023-06-07 11:20:43 +03:00
|
|
|
const BIGNUM* rsa_n = NULL;
|
|
|
|
const BIGNUM* rsa_e = NULL;
|
|
|
|
#else
|
|
|
|
BIGNUM* rsa_n = NULL;
|
|
|
|
BIGNUM* rsa_e = NULL;
|
|
|
|
#endif
|
2023-02-06 15:15:49 +03:00
|
|
|
EVP_PKEY* pubkey = X509_get0_pubkey(cert->x509);
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!pubkey)
|
2021-04-13 12:01:43 +03:00
|
|
|
goto fail;
|
2023-04-25 09:44:55 +03:00
|
|
|
|
|
|
|
#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
|
2023-02-03 13:40:35 +03:00
|
|
|
rsa = EVP_PKEY_get1_RSA(pubkey);
|
2014-03-26 02:13:08 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* If this is not a RSA key return success */
|
|
|
|
rc = TRUE;
|
|
|
|
if (!rsa)
|
2015-06-11 12:16:45 +03:00
|
|
|
goto fail;
|
2015-07-13 16:52:06 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
/* Now we return failure again if something is wrong. */
|
|
|
|
rc = FALSE;
|
|
|
|
|
|
|
|
RSA_get0_key(rsa, &rsa_n, &rsa_e, NULL);
|
2023-04-25 09:44:55 +03:00
|
|
|
#else
|
|
|
|
if (!EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_E, &rsa_e))
|
|
|
|
goto fail;
|
|
|
|
if (!EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_N, &rsa_n))
|
|
|
|
goto fail;
|
|
|
|
#endif
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!rsa_n || !rsa_e)
|
|
|
|
goto fail;
|
|
|
|
if (!cert_info_create(&cert->cert_info, rsa_n, rsa_e))
|
|
|
|
goto fail;
|
|
|
|
rc = TRUE;
|
|
|
|
fail:
|
2023-04-25 09:44:55 +03:00
|
|
|
#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
|
2023-02-03 13:40:35 +03:00
|
|
|
RSA_free(rsa);
|
2023-04-25 09:44:55 +03:00
|
|
|
#else
|
|
|
|
BN_free(rsa_n);
|
|
|
|
BN_free(rsa_e);
|
|
|
|
#endif
|
2023-02-03 13:40:35 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rdpCertificate* freerdp_certificate_new_from_der(const BYTE* data, size_t length)
|
|
|
|
{
|
|
|
|
rdpCertificate* cert = freerdp_certificate_new();
|
|
|
|
|
|
|
|
if (!cert || !data || (length == 0))
|
|
|
|
goto fail;
|
2023-04-25 09:44:55 +03:00
|
|
|
const BYTE* ptr = data;
|
2023-02-06 15:15:49 +03:00
|
|
|
cert->x509 = d2i_X509(NULL, &ptr, length);
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!cert->x509)
|
|
|
|
goto fail;
|
|
|
|
if (!freerdp_rsa_from_x509(cert))
|
|
|
|
goto fail;
|
|
|
|
return cert;
|
2015-06-11 12:16:45 +03:00
|
|
|
fail:
|
2023-02-03 13:40:35 +03:00
|
|
|
freerdp_certificate_free(cert);
|
2014-03-26 02:13:08 +04:00
|
|
|
return NULL;
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
rdpCertificate* freerdp_certificate_new_from_x509(const X509* xcert, const STACK_OF(X509) * chain)
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(xcert);
|
|
|
|
|
|
|
|
rdpCertificate* cert = freerdp_certificate_new();
|
|
|
|
if (!cert)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cert->x509 = X509_dup(xcert);
|
|
|
|
if (!cert->x509)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!freerdp_rsa_from_x509(cert))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (chain)
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
cert->chain = sk_X509_dup(chain);
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
return cert;
|
|
|
|
fail:
|
|
|
|
freerdp_certificate_free(cert);
|
|
|
|
return NULL;
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static rdpCertificate* freerdp_certificate_new_from(const char* file, BOOL isFile)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
X509* x509 = x509_utils_from_pem(file, strlen(file), isFile);
|
|
|
|
if (!x509)
|
2021-04-13 12:01:43 +03:00
|
|
|
return NULL;
|
2023-02-03 13:40:35 +03:00
|
|
|
rdpCertificate* cert = freerdp_certificate_new_from_x509(x509, NULL);
|
|
|
|
X509_free(x509);
|
|
|
|
return cert;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
rdpCertificate* freerdp_certificate_new_from_file(const char* file)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
return freerdp_certificate_new_from(file, TRUE);
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
rdpCertificate* freerdp_certificate_new_from_pem(const char* pem)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
return freerdp_certificate_new_from(pem, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
const rdpCertInfo* freerdp_certificate_get_info(const rdpCertificate* cert)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(cert);
|
2023-02-06 15:15:49 +03:00
|
|
|
if (!freerdp_certificate_is_rsa(cert))
|
2023-02-03 13:40:35 +03:00
|
|
|
return NULL;
|
|
|
|
return &cert->cert_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* freerdp_certificate_get_fingerprint(const rdpCertificate* cert)
|
|
|
|
{
|
|
|
|
return freerdp_certificate_get_fingerprint_by_hash(cert, "sha256");
|
|
|
|
}
|
|
|
|
|
|
|
|
char* freerdp_certificate_get_fingerprint_by_hash(const rdpCertificate* cert, const char* hash)
|
|
|
|
{
|
|
|
|
return freerdp_certificate_get_fingerprint_by_hash_ex(cert, hash, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* freerdp_certificate_get_fingerprint_by_hash_ex(const rdpCertificate* cert, const char* hash,
|
|
|
|
BOOL separator)
|
|
|
|
{
|
|
|
|
size_t fp_len = 0;
|
|
|
|
size_t pos = 0;
|
|
|
|
size_t size = 0;
|
|
|
|
BYTE* fp = NULL;
|
|
|
|
char* fp_buffer = NULL;
|
|
|
|
if (!cert || !cert->x509)
|
2021-05-27 09:38:59 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WLog_ERR(TAG, "Invalid certificate [%p, %p]", cert, cert ? cert->x509 : NULL);
|
|
|
|
return NULL;
|
2021-05-27 09:38:59 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!hash)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Invalid certificate hash %p", hash);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fp = x509_utils_get_hash(cert->x509, hash, &fp_len);
|
|
|
|
if (!fp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
size = fp_len * 3 + 1;
|
|
|
|
fp_buffer = calloc(size, sizeof(char));
|
|
|
|
if (!fp_buffer)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
pos = 0;
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
size_t i = 0;
|
|
|
|
for (i = 0; i < (fp_len - 1); i++)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char* p = &fp_buffer[pos];
|
|
|
|
if (separator)
|
|
|
|
rc = sprintf_s(p, size - pos, "%02" PRIx8 ":", fp[i]);
|
|
|
|
else
|
|
|
|
rc = sprintf_s(p, size - pos, "%02" PRIx8, fp[i]);
|
|
|
|
if (rc <= 0)
|
|
|
|
goto fail;
|
|
|
|
pos += (size_t)rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf_s(&fp_buffer[pos], size - pos, "%02" PRIx8 "", fp[i]);
|
|
|
|
|
|
|
|
free(fp);
|
|
|
|
|
|
|
|
return fp_buffer;
|
|
|
|
fail:
|
|
|
|
free(fp);
|
|
|
|
free(fp_buffer);
|
|
|
|
return NULL;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
static BOOL bio_read_pem(BIO* bio, char** ppem, size_t* plength)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
BOOL rc = FALSE;
|
|
|
|
|
|
|
|
WINPR_ASSERT(bio);
|
|
|
|
WINPR_ASSERT(ppem);
|
|
|
|
|
|
|
|
size_t offset = 0;
|
|
|
|
size_t length = 2048;
|
|
|
|
char* pem = NULL;
|
|
|
|
while (offset < length)
|
|
|
|
{
|
|
|
|
char* tmp = realloc(pem, length + 1);
|
|
|
|
if (!tmp)
|
|
|
|
goto fail;
|
|
|
|
pem = tmp;
|
|
|
|
|
|
|
|
ERR_clear_error();
|
|
|
|
|
|
|
|
const int status = BIO_read(bio, &pem[offset], length - offset);
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to read certificate");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
offset += (size_t)status;
|
|
|
|
if (length - offset > 0)
|
|
|
|
break;
|
|
|
|
length *= 2;
|
|
|
|
}
|
|
|
|
pem[offset] = '\0';
|
|
|
|
*ppem = pem;
|
|
|
|
if (plength)
|
|
|
|
*plength = offset;
|
|
|
|
rc = TRUE;
|
|
|
|
fail:
|
|
|
|
return rc;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
char* freerdp_certificate_get_pem(const rdpCertificate* cert, size_t* pLength)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
char* pem = NULL;
|
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
|
|
|
|
if (!cert->x509)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BIO* bio;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Don't manage certificates internally, leave it up entirely to the external client
|
|
|
|
* implementation
|
|
|
|
*/
|
|
|
|
bio = BIO_new(BIO_s_mem());
|
|
|
|
|
|
|
|
if (!bio)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "BIO_new() failure");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = PEM_write_bio_X509(bio, cert->x509);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "PEM_write_bio_X509 failure: %d", status);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (chain)
|
|
|
|
{
|
|
|
|
count = sk_X509_num(chain);
|
|
|
|
for (x = 0; x < count; x++)
|
|
|
|
{
|
|
|
|
X509* c = sk_X509_value(chain, x);
|
|
|
|
status = PEM_write_bio_X509(bio, c);
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "PEM_write_bio_X509 failure: %d", status);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!bio_read_pem(bio, &pem, pLength))
|
|
|
|
goto fail;
|
|
|
|
fail:
|
|
|
|
BIO_free_all(bio);
|
|
|
|
return pem;
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
char* freerdp_certificate_get_subject(const rdpCertificate* cert)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
return x509_utils_get_subject(cert->x509);
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
char* freerdp_certificate_get_issuer(const rdpCertificate* cert)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
return x509_utils_get_issuer(cert->x509);
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
char* freerdp_certificate_get_upn(const rdpCertificate* cert)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
return x509_utils_get_upn(cert->x509);
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
char* freerdp_certificate_get_email(const rdpCertificate* cert)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
return x509_utils_get_email(cert->x509);
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
BOOL freerdp_certificate_check_eku(const rdpCertificate* cert, int nid)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
return x509_utils_check_eku(cert->x509, nid);
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
BOOL freerdp_certificate_get_public_key(const rdpCertificate* cert, BYTE** PublicKey,
|
|
|
|
DWORD* PublicKeyLength)
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
2023-02-06 15:05:11 +03:00
|
|
|
BYTE* ptr = NULL;
|
|
|
|
BYTE* optr = NULL;
|
2023-02-03 13:40:35 +03:00
|
|
|
int length;
|
2023-02-06 15:05:11 +03:00
|
|
|
BOOL status = FALSE;
|
2023-02-03 13:40:35 +03:00
|
|
|
EVP_PKEY* pkey = NULL;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
2012-11-22 04:22:41 +04:00
|
|
|
|
2023-02-06 15:15:49 +03:00
|
|
|
pkey = X509_get0_pubkey(cert->x509);
|
2014-07-18 05:15:22 +04:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (!pkey)
|
2015-05-05 20:45:34 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WLog_ERR(TAG, "X509_get_pubkey() failed");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
length = i2d_PublicKey(pkey, NULL);
|
|
|
|
|
|
|
|
if (length < 1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "i2d_PublicKey() failed");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-02-06 15:05:11 +03:00
|
|
|
*PublicKey = optr = ptr = (BYTE*)calloc(length, sizeof(BYTE));
|
2023-02-03 13:40:35 +03:00
|
|
|
|
|
|
|
if (!ptr)
|
|
|
|
goto exit;
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2023-02-06 15:05:11 +03:00
|
|
|
const int length2 = i2d_PublicKey(pkey, &ptr);
|
|
|
|
if (length != length2)
|
|
|
|
goto exit;
|
|
|
|
*PublicKeyLength = (DWORD)length2;
|
|
|
|
status = TRUE;
|
2023-02-03 13:40:35 +03:00
|
|
|
exit:
|
|
|
|
|
2023-02-06 15:05:11 +03:00
|
|
|
if (!status)
|
|
|
|
free(optr);
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
return status;
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
BOOL freerdp_certificate_verify(const rdpCertificate* cert, const char* certificate_store_path)
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
return x509_utils_verify(cert->x509, cert->chain, certificate_store_path);
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
char** freerdp_certificate_get_dns_names(const rdpCertificate* cert, size_t* pcount,
|
|
|
|
size_t** pplengths)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
return x509_utils_get_dns_names(cert->x509, pcount, pplengths);
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
char* freerdp_certificate_get_common_name(const rdpCertificate* cert, size_t* plength)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
return x509_utils_get_common_name(cert->x509, plength);
|
|
|
|
}
|
|
|
|
|
|
|
|
WINPR_MD_TYPE freerdp_certificate_get_signature_alg(const rdpCertificate* cert)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
return x509_utils_get_signature_alg(cert->x509);
|
|
|
|
}
|
|
|
|
|
|
|
|
void freerdp_certificate_free_dns_names(size_t count, size_t* lengths, char** names)
|
|
|
|
{
|
|
|
|
x509_utils_dns_names_free(count, lengths, names);
|
2021-04-13 12:01:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
char* freerdp_certificate_get_hash(const rdpCertificate* cert, const char* hash, size_t* plength)
|
2021-04-13 12:01:43 +03:00
|
|
|
{
|
2023-02-03 13:40:35 +03:00
|
|
|
WINPR_ASSERT(cert);
|
2023-05-16 09:04:33 +03:00
|
|
|
return (char*)x509_utils_get_hash(cert->x509, hash, plength);
|
2023-02-03 13:40:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
X509* freerdp_certificate_get_x509(rdpCertificate* cert)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
return cert->x509;
|
|
|
|
}
|
2021-04-13 12:01:43 +03:00
|
|
|
|
2023-08-15 15:06:35 +03:00
|
|
|
BOOL freerdp_certificate_publickey_encrypt(const rdpCertificate* cert, const BYTE* input,
|
|
|
|
size_t cbInput, BYTE** poutput, size_t* pcbOutput)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
WINPR_ASSERT(input);
|
|
|
|
WINPR_ASSERT(poutput);
|
|
|
|
WINPR_ASSERT(pcbOutput);
|
|
|
|
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
BYTE* output = NULL;
|
|
|
|
EVP_PKEY* pkey = X509_get0_pubkey(cert->x509);
|
|
|
|
if (!pkey)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(pkey, NULL);
|
|
|
|
if (!ctx)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
size_t outputSize = EVP_PKEY_size(pkey);
|
|
|
|
output = malloc(outputSize);
|
|
|
|
*pcbOutput = outputSize;
|
|
|
|
|
|
|
|
if (EVP_PKEY_encrypt_init(ctx) != 1 ||
|
|
|
|
EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) != 1 ||
|
|
|
|
EVP_PKEY_encrypt(ctx, output, pcbOutput, input, cbInput) != 1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "error when setting up public key");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
*poutput = output;
|
|
|
|
output = NULL;
|
|
|
|
ret = TRUE;
|
|
|
|
out:
|
|
|
|
EVP_PKEY_CTX_free(ctx);
|
|
|
|
free(output);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-04-25 09:44:55 +03:00
|
|
|
#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
|
|
|
|
static RSA* freerdp_certificate_get_RSA(const rdpCertificate* cert)
|
2023-02-08 16:32:24 +03:00
|
|
|
{
|
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
|
|
|
|
if (!freerdp_certificate_is_rsa(cert))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
EVP_PKEY* pubkey = X509_get0_pubkey(cert->x509);
|
|
|
|
if (!pubkey)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return EVP_PKEY_get1_RSA(pubkey);
|
|
|
|
}
|
2023-04-25 09:44:55 +03:00
|
|
|
#endif
|
2023-02-08 16:32:24 +03:00
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
BYTE* freerdp_certificate_get_der(const rdpCertificate* cert, size_t* pLength)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
|
|
|
|
if (pLength)
|
|
|
|
*pLength = 0;
|
|
|
|
|
2023-02-08 14:00:01 +03:00
|
|
|
const int rc = i2d_X509(cert->x509, NULL);
|
|
|
|
if (rc <= 0)
|
2021-04-13 12:01:43 +03:00
|
|
|
return NULL;
|
2023-02-03 13:40:35 +03:00
|
|
|
|
2023-02-08 14:00:01 +03:00
|
|
|
BYTE* ptr = calloc(rc + 1, sizeof(BYTE));
|
|
|
|
if (!ptr)
|
|
|
|
return NULL;
|
|
|
|
BYTE* i2d_ptr = ptr;
|
|
|
|
|
|
|
|
const int rc2 = i2d_X509(cert->x509, &i2d_ptr);
|
|
|
|
if (rc2 <= 0)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-02-03 13:40:35 +03:00
|
|
|
if (pLength)
|
2023-02-08 14:00:01 +03:00
|
|
|
*pLength = (size_t)rc2;
|
2023-02-03 13:40:35 +03:00
|
|
|
return ptr;
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
2023-02-03 17:49:55 +03:00
|
|
|
|
|
|
|
BOOL freerdp_certificate_is_rsa(const rdpCertificate* cert)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(cert);
|
2023-02-06 15:15:49 +03:00
|
|
|
return is_rsa_key(cert->x509);
|
2023-02-03 17:49:55 +03:00
|
|
|
}
|
2023-03-25 16:36:27 +03:00
|
|
|
|
|
|
|
BOOL freerdp_certificate_is_rdp_security_compatible(const rdpCertificate* cert)
|
|
|
|
{
|
|
|
|
const rdpCertInfo* info = freerdp_certificate_get_info(cert);
|
|
|
|
if (!freerdp_certificate_is_rsa(cert) || !info || (info->ModulusLength != 2048 / 8))
|
|
|
|
{
|
|
|
|
WLog_INFO(TAG, "certificate is not RSA 2048, RDP security not supported.");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2023-04-25 09:44:55 +03:00
|
|
|
|
|
|
|
char* freerdp_certificate_get_param(const rdpCertificate* cert, enum FREERDP_CERT_PARAM what,
|
|
|
|
size_t* psize)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(cert);
|
|
|
|
WINPR_ASSERT(psize);
|
|
|
|
|
|
|
|
*psize = 0;
|
|
|
|
|
|
|
|
#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
|
|
|
|
const BIGNUM* bn = NULL;
|
|
|
|
RSA* rsa = freerdp_certificate_get_RSA(cert);
|
|
|
|
switch (what)
|
|
|
|
{
|
|
|
|
case FREERDP_CERT_RSA_E:
|
|
|
|
RSA_get0_key(rsa, NULL, &bn, NULL);
|
|
|
|
break;
|
|
|
|
case FREERDP_CERT_RSA_N:
|
|
|
|
RSA_get0_key(rsa, &bn, NULL, NULL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
RSA_free(rsa);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
RSA_free(rsa);
|
|
|
|
#else
|
|
|
|
EVP_PKEY* pkey = X509_get0_pubkey(cert->x509);
|
|
|
|
if (!pkey)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BIGNUM* bn = NULL;
|
|
|
|
switch (what)
|
|
|
|
{
|
|
|
|
case FREERDP_CERT_RSA_E:
|
|
|
|
if (!EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_E, &bn))
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
case FREERDP_CERT_RSA_N:
|
|
|
|
if (!EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_N, &bn))
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const size_t bnsize = BN_num_bytes(bn);
|
|
|
|
char* rc = calloc(bnsize + 1, sizeof(char));
|
|
|
|
if (!rc)
|
|
|
|
goto fail;
|
2023-05-16 09:04:33 +03:00
|
|
|
BN_bn2bin(bn, (BYTE*)rc);
|
2023-04-25 09:44:55 +03:00
|
|
|
*psize = bnsize;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
#if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR < 3)
|
|
|
|
BN_free(bn);
|
|
|
|
#endif
|
|
|
|
return rc;
|
|
|
|
}
|