Skip to content

Commit 3c774cf

Browse files
authored
Merge pull request #25 from deemru/1.x
1.0.5
2 parents e05d173 + cfbf43d commit 3c774cf

File tree

5 files changed

+47
-56
lines changed

5 files changed

+47
-56
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## 1.0.5
4+
5+
- Improved certificate and PFX format probing via `CRYPT_STRING_ANY`
6+
- Fixed `CHECK_HANDLE` macro for `MSSPI_CERT_HANDLE`
7+
- Added `static-cert` build target (`libmsspi-cert.a`) to Linux Makefile
8+
9+
---
10+
311
## 1.0.4
412

513
- Added DTLS-SRTP support: [`msspi_set_srtp_profiles()`](MSSPI.md#msspi_set_srtp_profiles), [`msspi_get_srtp_profile()`](MSSPI.md#msspi_get_srtp_profile)

build_linux/Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
all: static static-capix
1+
all: static static-capix static-cert
22

33
static: libmsspi.a
44

55
static-capix: libmsspi-capix.a
66

7+
static-cert: libmsspi-cert.a
8+
79
shared: libmsspi.so
810

911
msspi.o:
@@ -21,8 +23,11 @@ libmsspi.a: msspi.o
2123
libmsspi-capix.a: msspi-capix.o
2224
ar cr libmsspi-capix.a msspi-capix.o
2325

26+
libmsspi-cert.a: msspi-cert.o
27+
ar cr libmsspi-cert.a msspi-cert.o
28+
2429
libmsspi.so: msspi.o
2530
g++ -shared -o libmsspi.so msspi.o
2631

2732
clean:
28-
rm -f libmsspi.so libmsspi.a libmsspi-capix.a msspi.o msspi-capix.o
33+
rm -f libmsspi.so libmsspi.a libmsspi-capix.a libmsspi-cert.a msspi.o msspi-capix.o msspi-cert.o

src/msspi.cpp

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ namespace _detail { template< typename T > struct _alignof_trick { char _; T _te
3838

3939
#define IS_ALIGNED_PTR( x, type ) ( !( (uintptr_t)( x ) % ALIGNOF( type ) ) )
4040

41-
#define CHECK_HANDLE( h, type ) if( !h || !IS_ALIGNED_PTR( h, type ) || h->magic != MSSPI_MAGIC_VERSION ){ SetLastError( ERROR_INVALID_HANDLE ); return 0; }
41+
#define CHECK_HANDLE( h, type, alive ) if( !h || !IS_ALIGNED_PTR( h, type ) || h->magic != alive ){ SetLastError( ERROR_INVALID_HANDLE ); return 0; }
4242
#if defined( QT_NO_EXCEPTIONS ) || defined( NO_EXCEPTIONS ) || ( defined( __clang__ ) && !defined( __EXCEPTIONS ) )
4343
#define MSSPIEHTRY_0
44-
#define MSSPIEHTRY_h CHECK_HANDLE( h, MSSPI_HANDLE )
45-
#define MSSPIEHTRY_ch CHECK_HANDLE( ch, MSSPI_CERT_HANDLE )
44+
#define MSSPIEHTRY_h CHECK_HANDLE( h, MSSPI_HANDLE, MSSPI_MAGIC_VERSION )
45+
#define MSSPIEHTRY_ch CHECK_HANDLE( ch, MSSPI_CERT_HANDLE, MSSPI_CERT_MAGIC_VERSION )
4646
#define MSSPIEHCATCH
4747
#define MSSPIEHCATCH_HRET( ret )
4848
#define MSSPIEHCATCH_RET( ret )
4949
#define MSSPIEHCATCH_0 MSSPIEHCATCH
5050
#else // no EXCEPTIONS
5151
#define MSSPIEHTRY_0 try {
52-
#define MSSPIEHTRY_h try { CHECK_HANDLE( h, MSSPI_HANDLE )
53-
#define MSSPIEHTRY_ch try { CHECK_HANDLE( ch, MSSPI_CERT_HANDLE )
52+
#define MSSPIEHTRY_h try { CHECK_HANDLE( h, MSSPI_HANDLE, MSSPI_MAGIC_VERSION )
53+
#define MSSPIEHTRY_ch try { CHECK_HANDLE( ch, MSSPI_CERT_HANDLE, MSSPI_CERT_MAGIC_VERSION )
5454
#define MSSPIEHCATCH } catch( ... ) {
5555
#define MSSPIEHCATCH_HRET( ret ) MSSPIEHCATCH; h->state |= MSSPI_ERROR; SetLastError( ERROR_INTERNAL_ERROR ); return ret; }
5656
#define MSSPIEHCATCH_RET( ret ) MSSPIEHCATCH; return ret; }
@@ -2525,26 +2525,16 @@ static PCCERT_CONTEXT pfx2cert( const uint8_t * pfx, size_t len, const uint8_t *
25252525
for( size_t i = 0; i < wpassword.length(); ++i )
25262526
wpassword[i] = (WCHAR)password[i];
25272527

2528-
CRYPT_DATA_BLOB pfxBlob = { (DWORD)len, (BYTE *)pfx };
2528+
DWORD dwData;
2529+
if( !CryptStringToBinaryA( (LPCSTR)pfx, (DWORD)len, CRYPT_STRING_ANY, NULL, &dwData, NULL, NULL ) )
2530+
return NULL;
2531+
std::vector<BYTE> der( dwData );
2532+
if( !CryptStringToBinaryA( (LPCSTR)pfx, (DWORD)len, CRYPT_STRING_ANY, der.data(), &dwData, NULL, NULL ) )
2533+
return NULL;
2534+
CRYPT_DATA_BLOB pfxBlob = { dwData, der.data() };
25292535
HCERTSTORE hStore = PFXImportCertStore( &pfxBlob, wpassword.data(), PKCS12_NO_PERSIST_KEY );
25302536
if( !hStore )
2531-
{
2532-
std::vector<BYTE> PFXDer;
2533-
DWORD dwData;
2534-
if( CryptStringToBinaryA( (LPCSTR)pfx, (DWORD)len, CRYPT_STRING_BASE64_ANY, NULL, &dwData, NULL, NULL ) )
2535-
{
2536-
PFXDer.resize( dwData );
2537-
if( CryptStringToBinaryA( (LPCSTR)pfx, (DWORD)len, CRYPT_STRING_BASE64_ANY, PFXDer.data(), &dwData, NULL, NULL ) )
2538-
{
2539-
pfxBlob.cbData = dwData;
2540-
pfxBlob.pbData = PFXDer.data();
2541-
hStore = PFXImportCertStore( &pfxBlob, wpassword.data(), PKCS12_NO_PERSIST_KEY );
2542-
}
2543-
}
2544-
2545-
if( !hStore )
2546-
return NULL;
2547-
}
2537+
return NULL;
25482538

25492539
PCCERT_CONTEXT pfxcert, prevcert = NULL;
25502540
for( ;; )
@@ -2644,22 +2634,24 @@ static bool msspi_set_mycert_finalize( MSSPI_HANDLE h, PCCERT_CONTEXT certfound,
26442634
return isOK;
26452635
}
26462636

2647-
static PCCERT_CONTEXT findcert( const uint8_t * certData, size_t len, const char * certstore )
2637+
#define CERTIFICATE_THRESHOLD 200
2638+
2639+
static PCCERT_CONTEXT msspi_cert_create_context( const uint8_t * certbuf, size_t len )
26482640
{
2649-
PCCERT_CONTEXT certprobe = NULL;
2641+
if( !certbuf || !len || len < CERTIFICATE_THRESHOLD )
2642+
return NULL;
2643+
DWORD dwData;
2644+
if( !CryptStringToBinaryA( (const char *)certbuf, (DWORD)len, CRYPT_STRING_ANY, NULL, &dwData, NULL, NULL ) )
2645+
return NULL;
2646+
std::vector<BYTE> der( dwData );
2647+
if( !CryptStringToBinaryA( (const char *)certbuf, (DWORD)len, CRYPT_STRING_ANY, der.data(), &dwData, NULL, NULL ) )
2648+
return NULL;
2649+
return CertCreateCertificateContext( X509_ASN_ENCODING, der.data(), dwData );
2650+
}
26502651

2651-
certprobe = CertCreateCertificateContext( X509_ASN_ENCODING, certData, (DWORD)len ); // DER format
2652-
if( !certprobe )
2653-
{
2654-
std::vector<BYTE> clientCertDer;
2655-
DWORD dwData;
2656-
if( CryptStringToBinaryA( (const char *)certData, (DWORD)len, CRYPT_STRING_BASE64_ANY, NULL, &dwData, NULL, NULL ) )
2657-
{
2658-
clientCertDer.resize( dwData );
2659-
if( CryptStringToBinaryA( (const char *)certData, (DWORD)len, CRYPT_STRING_BASE64_ANY, clientCertDer.data(), &dwData, NULL, NULL ) )
2660-
certprobe = CertCreateCertificateContext( X509_ASN_ENCODING, clientCertDer.data(), dwData ); // PEM format
2661-
}
2662-
}
2652+
static PCCERT_CONTEXT findcert( const uint8_t * certData, size_t len, const char * certstore )
2653+
{
2654+
PCCERT_CONTEXT certprobe = msspi_cert_create_context( certData, len );
26632655

26642656
PCCERT_CONTEXT certfound = NULL;
26652657
HCERTSTORE hStore = 0;

src/msspi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#define MSSPI_VERSION_MAJOR 1
88
#define MSSPI_VERSION_MINOR 0
9-
#define MSSPI_VERSION_PATCH 4
9+
#define MSSPI_VERSION_PATCH 5
1010

1111
#define MSSPI_VERSION \
1212
( ( MSSPI_VERSION_MAJOR << 16 ) | ( MSSPI_VERSION_MINOR << 8 ) | MSSPI_VERSION_PATCH )

src/msspi_cert.hpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,29 +120,15 @@ MSSPI_CERT_HANDLE msspi_cert_open( const uint8_t * certbuf, size_t len )
120120
{
121121
MSSPIEHTRY_0;
122122

123-
PCCERT_CONTEXT cert = NULL;
124-
125123
if( !certbuf || !len )
126124
{
127125
SetLastError( ERROR_BAD_ARGUMENTS );
128126
return NULL;
129127
}
130128

131-
cert = CertCreateCertificateContext( X509_ASN_ENCODING, (const BYTE *)certbuf, (DWORD)len );
129+
PCCERT_CONTEXT cert = msspi_cert_create_context( certbuf, len );
132130
if( !cert )
133-
{
134-
std::vector<BYTE> certbufder;
135-
DWORD dwData;
136-
if( CryptStringToBinaryA( (const char *)certbuf, (DWORD)len, CRYPT_STRING_BASE64_ANY, NULL, &dwData, NULL, NULL ) )
137-
{
138-
certbufder.resize( dwData );
139-
if( CryptStringToBinaryA( (const char *)certbuf, (DWORD)len, CRYPT_STRING_BASE64_ANY, certbufder.data(), &dwData, NULL, NULL ) )
140-
cert = CertCreateCertificateContext( X509_ASN_ENCODING, certbufder.data(), dwData );
141-
}
142-
143-
if( !cert )
144-
return NULL; // last error included
145-
}
131+
return NULL; // last error included
146132

147133
return msspi_cert_handle( new MSSPI_CERT( cert ) );
148134

0 commit comments

Comments
 (0)