Skip to content

Commit 2496979

Browse files
authored
Fix build for AWS-LC and BoringSSL (#894)
1 parent d1030e6 commit 2496979

File tree

6 files changed

+119
-114
lines changed

6 files changed

+119
-114
lines changed

src/openssl/app.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ xmlSecOpenSSLAppCheckCertMatchesKey(EVP_PKEY * pKey, X509 * cert) {
760760
static X509 *
761761
xmlSecOpenSSLAppFindKeyCert(EVP_PKEY * pKey, STACK_OF(X509) * certs) {
762762
X509 * cert;
763-
int ii, size;
763+
xmlSecOpenSSLSizeT ii, size;
764764
int ret;
765765

766766
xmlSecAssert2(pKey != NULL, NULL);

src/openssl/ciphers.c

-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ xmlSecOpenSSLEvpBlockCipherCtxInit(xmlSecOpenSSLEvpBlockCipherCtxPtr ctx,
127127
xmlSecInternalError2("xmlSecBufferAppend", cipherName, "size=%d", ivLen);
128128
return(-1);
129129
}
130-
131130
} else {
132131
/* if we don't have enough data, exit and hope that
133132
* we'll have iv next time */

src/openssl/globals.h

+15-28
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "config.h"
1919
#endif /* HAVE_CONFIG_H */
2020

21+
#include <openssl/crypto.h>
22+
2123
#define IN_XMLSEC_CRYPTO
2224
#define XMLSEC_PRIVATE
2325

@@ -31,17 +33,24 @@
3133
*/
3234
#define XMLSEC_OPENSSL_ERROR_BUFFER_SIZE 1024
3335

36+
/** AWS LC and OpenSSL have different types for error code type */
37+
#ifdef OPENSSL_IS_AWSLC
38+
typedef uint32_t xmlSecOpenSSLErrorType;
39+
#else /* OPENSSL_IS_AWSLC */
40+
typedef unsigned long xmlSecOpenSSLErrorType;
41+
#endif /* ! OPENSSL_IS_AWSLC */
42+
3443
/**
3544
* xmlSecOpenSSLError:
3645
* @errorFunction: the failed function name.
3746
* @errorObject: the error specific error object (e.g. transform, key data, etc).
3847
*
3948
* Macro. The XMLSec library macro for reporting OpenSSL crypro errors.
4049
*/
41-
#define __xmlSecOpenSSLError(errorType, errorFunction, errorObject) \
50+
#define xmlSecOpenSSLError(errorFunction, errorObject) \
4251
{ \
4352
char _openssl_error_buf[XMLSEC_OPENSSL_ERROR_BUFFER_SIZE]; \
44-
errorType _openssl_error_code = ERR_peek_last_error(); \
53+
xmlSecOpenSSLErrorType _openssl_error_code = ERR_peek_last_error(); \
4554
ERR_error_string_n(_openssl_error_code, _openssl_error_buf, sizeof(_openssl_error_buf)); \
4655
xmlSecError(XMLSEC_ERRORS_HERE, \
4756
(const char*)(errorObject), \
@@ -62,9 +71,9 @@
6271
*
6372
* Macro. The XMLSec library macro for reporting OpenSSL crypro errors.
6473
*/
65-
#define __xmlSecOpenSSLError2(errorType, errorFunction, errorObject, msg, param) \
74+
#define xmlSecOpenSSLError2(errorFunction, errorObject, msg, param) \
6675
char _openssl_error_buf[XMLSEC_OPENSSL_ERROR_BUFFER_SIZE]; \
67-
errorType _openssl_error_code = ERR_peek_last_error(); \
76+
xmlSecOpenSSLErrorType _openssl_error_code = ERR_peek_last_error(); \
6877
ERR_error_string_n(_openssl_error_code, _openssl_error_buf, sizeof(_openssl_error_buf)); \
6978
xmlSecError(XMLSEC_ERRORS_HERE, \
7079
(const char*)(errorObject), \
@@ -85,9 +94,9 @@
8594
*
8695
* Macro. The XMLSec library macro for reporting OpenSSL crypro errors.
8796
*/
88-
#define __xmlSecOpenSSLError3(errorType, errorFunction, errorObject, msg, param1, param2) \
97+
#define xmlSecOpenSSLError3(errorFunction, errorObject, msg, param1, param2) \
8998
char _openssl_error_buf[XMLSEC_OPENSSL_ERROR_BUFFER_SIZE]; \
90-
errorType _openssl_error_code = ERR_peek_last_error(); \
99+
xmlSecOpenSSLErrorType _openssl_error_code = ERR_peek_last_error(); \
91100
ERR_error_string_n(_openssl_error_code, _openssl_error_buf, sizeof(_openssl_error_buf)); \
92101
xmlSecError(XMLSEC_ERRORS_HERE, \
93102
(const char*)(errorObject), \
@@ -99,26 +108,4 @@
99108
xmlSecErrorsSafeString(_openssl_error_buf) \
100109
); \
101110

102-
103-
104-
#ifdef OPENSSL_IS_BORINGSSL
105-
106-
#define xmlSecOpenSSLError(errorFunction, errorObject) \
107-
__xmlSecOpenSSLError(uint32_t, errorFunction, errorObject)
108-
#define xmlSecOpenSSLError2(errorFunction, errorObject, msg, param) \
109-
__xmlSecOpenSSLError2(uint32_t, errorFunction, errorObject, msg, param)
110-
#define xmlSecOpenSSLError3(errorFunction, errorObject, msg, param1, param2) \
111-
__xmlSecOpenSSLError3(uint32_t, errorFunction, errorObject, msg, param1, param2)
112-
113-
#else /* OPENSSL_IS_BORINGSSL */
114-
115-
#define xmlSecOpenSSLError(errorFunction, errorObject) \
116-
__xmlSecOpenSSLError(unsigned long, errorFunction, errorObject)
117-
#define xmlSecOpenSSLError2(errorFunction, errorObject, msg, param) \
118-
__xmlSecOpenSSLError2(unsigned long, errorFunction, errorObject, msg, param)
119-
#define xmlSecOpenSSLError3(errorFunction, errorObject, msg, param1, param2) \
120-
__xmlSecOpenSSLError3(unsigned long, errorFunction, errorObject, msg, param1, param2)
121-
122-
#endif /* ! OPENSSL_IS_BORINGSSL */
123-
124111
#endif /* ! __XMLSEC_GLOBALS_H__ */

src/openssl/openssl_compat.h

+57-32
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,35 @@
77
#ifndef __XMLSEC_OPENSSL_OPENSSL_COMPAT_H__
88
#define __XMLSEC_OPENSSL_OPENSSL_COMPAT_H__
99

10+
#include <openssl/crypto.h>
1011
#include <openssl/rand.h>
1112

1213
#include "../cast_helpers.h"
1314

1415

1516
/******************************************************************************
1617
*
17-
* boringssl compatibility
18+
* AWS LC compatibility (based on BoringSSL)
1819
*
1920
*****************************************************************************/
20-
#ifdef OPENSSL_IS_BORINGSSL
21-
22-
/* Not implemented by LibreSSL (yet?) */
23-
#define XMLSEC_OPENSSL_NO_ASN1_TIME_TO_TM 1
24-
#define XMLSEC_OPENSSL_NO_STORE 1
25-
#define XMLSEC_OPENSSL_NO_DEEP_COPY 1
26-
#define XMLSEC_OPENSSL_NO_CRL_VERIFICATION 1
27-
#define XMLSEC_NO_RSA_OAEP 1
28-
#define XMLSEC_NO_DH 1
29-
#define XMLSEC_NO_DSA 1
30-
#define XMLSEC_NO_SHA3 1
21+
#ifdef OPENSSL_IS_AWSLC
3122

23+
#ifndef OPENSSL_IS_BORINGSSL
24+
#define OPENSSL_IS_BORINGSSL
25+
#endif /* OPENSSL_IS_BORINGSSL */
3226

3327
#define EVP_CIPHER_key_length (int)EVP_CIPHER_key_length
3428
#define EVP_CIPHER_iv_length (int)EVP_CIPHER_iv_length
3529
#define EVP_CIPHER_block_size (int)EVP_CIPHER_block_size
3630

3731
#define ECDSA_do_verify(digest, digest_len, sig, key) \
38-
ECDSA_do_verify(digest, (size_t)(digest_len), sig, key)
32+
ECDSA_do_verify(digest, (size_t)(digest_len), sig, key)
3933
#define ECDSA_do_sign(digest, digest_len, key) \
40-
ECDSA_do_sign(digest, (size_t)(digest_len), key)
34+
ECDSA_do_sign(digest, (size_t)(digest_len), key)
4135

4236
#define HMAC_Init_ex(ctx, key, key_len, md, impl) \
4337
HMAC_Init_ex(ctx, key, (size_t)(key_len), md, impl)
38+
4439
#define AES_set_encrypt_key(user_key, bits, aes_key) \
4540
AES_set_encrypt_key(user_key, (unsigned)(bits), aes_key)
4641
#define AES_set_decrypt_key(user_key, bits, aes_key) \
@@ -51,7 +46,6 @@
5146
#define RSA_private_decrypt(flen, from, to, rsa, padding) \
5247
RSA_private_decrypt((size_t)(flen), from, to, rsa, padding)
5348

54-
5549
#define EVP_MD_size (int)EVP_MD_size
5650
#define RSA_size (int)RSA_size
5751

@@ -60,29 +54,37 @@
6054
#define BN_bn2bin (int)BN_bn2bin
6155
#define BN_bin2bn(in, len, ret) BN_bin2bn(in, (size_t)(len), ret)
6256

63-
#define sk_X509_insert (int)sk_X509_insert
64-
#define sk_X509_push (int)sk_X509_push
65-
#define sk_X509_num (int)sk_X509_num
66-
#define sk_X509_CRL_num (int)sk_X509_CRL_num
67-
#define sk_X509_CRL_push (int)sk_X509_CRL_push
68-
#define sk_X509_CRL_value(sk, idx) sk_X509_CRL_value(sk, (size_t)(idx))
69-
#define sk_X509_value(sk, idx) sk_X509_value(sk, (size_t)(idx))
70-
#define sk_X509_NAME_ENTRY_value(sk, idx) sk_X509_NAME_ENTRY_value(sk, (size_t)(idx))
71-
#define sk_X509_REVOKED_value(sk, idx) sk_X509_REVOKED_value(sk, (size_t)(idx))
72-
7357
#define BIO_pending (int)BIO_pending
7458

75-
#define sk_X509_NAME_ENTRY_num (int)sk_X509_NAME_ENTRY_num
76-
#define sk_X509_NAME_ENTRY_push (int)sk_X509_NAME_ENTRY_push
59+
#endif /* ! OPENSSL_IS_AWSLC */
60+
61+
62+
/******************************************************************************
63+
*
64+
* boringssl compatibility
65+
*
66+
*****************************************************************************/
67+
#ifdef OPENSSL_IS_BORINGSSL
68+
69+
/* Not implemented by LibreSSL (yet?) */
70+
#define XMLSEC_OPENSSL_NO_ASN1_TIME_TO_TM 1
71+
#define XMLSEC_OPENSSL_NO_STORE 1
72+
#define XMLSEC_OPENSSL_NO_DEEP_COPY 1
73+
#define XMLSEC_OPENSSL_NO_CRL_VERIFICATION 1
74+
#define XMLSEC_NO_RSA_OAEP 1
75+
#define XMLSEC_NO_DH 1
76+
#define XMLSEC_NO_DSA 1
77+
#define XMLSEC_NO_SHA3 1
78+
7779

7880
#define ENGINE_cleanup(...) {}
7981
#define CONF_modules_unload(...) {}
8082

81-
#define RAND_priv_bytes(buf,len) RAND_bytes((buf), (size_t)(len))
83+
#define RAND_priv_bytes(buf,len) RAND_bytes((buf), (len))
8284
#define RAND_write_file(file) (0)
8385

8486
#define EVP_PKEY_base_id(pkey) EVP_PKEY_id(pkey)
85-
#define EVP_CipherFinal(ctx, out, out_len) EVP_CipherFinal_ex(ctx, out, out_len)
87+
#define EVP_CipherFinal(ctx, out, out_len) EVP_CipherFinal_ex((ctx), (out), (out_len))
8688
#define EVP_read_pw_string(...) (-1)
8789

8890
#define X509_get0_pubkey(cert) X509_get_pubkey((cert))
@@ -94,6 +96,30 @@
9496

9597
#endif /* OPENSSL_IS_BORINGSSL */
9698

99+
100+
/* BoringSSL redefines int->size_t for bunch of x509 functions */
101+
#if defined(OPENSSL_IS_BORINGSSL)
102+
103+
typedef size_t xmlSecOpenSSLSizeT;
104+
105+
#define XMLSEC_OPENSSL_SAFE_CAST_SIZE_T_TO_SIZE(srcVal, dstVal, errorAction, errorObject) \
106+
(dstVal) = (srcVal)
107+
#define XMLSEC_OPENSSL_SAFE_CAST_SIZE_TO_SIZE_T(srcVal, dstVal, errorAction, errorObject) \
108+
(dstVal) = (srcVal)
109+
110+
#else /* defined(OPENSSL_IS_BORINGSSL) */
111+
112+
typedef int xmlSecOpenSSLSizeT;
113+
114+
#define XMLSEC_OPENSSL_SAFE_CAST_SIZE_T_TO_SIZE(srcVal, dstVal, errorAction, errorObject) \
115+
XMLSEC_SAFE_CAST_INT_TO_SIZE((srcVal), (dstVal), errorAction, (errorObject))
116+
117+
#define XMLSEC_OPENSSL_SAFE_CAST_SIZE_TO_SIZE_T(srcVal, dstVal, errorAction, errorObject) \
118+
XMLSEC_SAFE_CAST_SIZE_TO_INT((srcVal), (dstVal), errorAction, (errorObject))
119+
120+
#endif /* defined(OPENSSL_IS_BORINGSSL) */
121+
122+
97123
/******************************************************************************
98124
*
99125
* LibreSSL compatibility (implements most of OpenSSL 1.1 API)
@@ -124,7 +150,6 @@
124150

125151
#endif /* defined(LIBRESSL_VERSION_NUMBER) */
126152

127-
128153
/******************************************************************************
129154
*
130155
* OpenSSL 3.0.0 compatibility
@@ -158,8 +183,8 @@
158183

159184
#define RAND_priv_bytes_ex(ctx,buf,num,strength) xmlSecOpenSSLCompatRand((buf),(num))
160185
static inline int xmlSecOpenSSLCompatRand(unsigned char *buf, xmlSecSize size) {
161-
int num;
162-
XMLSEC_SAFE_CAST_SIZE_TO_INT(size, num, return(0), NULL);
186+
xmlSecOpenSSLSizeT num;
187+
XMLSEC_OPENSSL_SAFE_CAST_SIZE_TO_SIZE_T(size, num, return(0), NULL);
163188
return(RAND_priv_bytes(buf, num));
164189
}
165190

0 commit comments

Comments
 (0)