]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
print errors and fail, don't abort
authorAlan T. DeKok <aland@freeradius.org>
Sat, 12 Jun 2021 12:20:30 +0000 (08:20 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Sat, 12 Jun 2021 12:23:52 +0000 (08:23 -0400)
src/modules/rlm_eap/types/rlm_eap_fast/eap_fast.c
src/modules/rlm_eap/types/rlm_eap_fast/eap_fast_crypto.c
src/modules/rlm_eap/types/rlm_eap_fast/rlm_eap_fast.c

index fa2d6ff1eb60bfaa021bfc4dc27e50691e3c45c0..beba44de3c68e9717b4cf80a455a0ddcf304188e 100644 (file)
@@ -271,6 +271,7 @@ static void eap_fast_send_pac_tunnel(REQUEST *request, tls_session_t *tls_sessio
        dlen = eap_fast_encrypt((unsigned const char *)&opaque_plaintext, sizeof(opaque_plaintext),
                                    t->a_id, PAC_A_ID_LENGTH, t->pac_opaque_key, pac.opaque.iv,
                                    pac.opaque.data, pac.opaque.tag);
+       if (dlen < 0) return;
 
        pac.opaque.hdr.type = htons(EAP_FAST_TLV_MANDATORY | PAC_INFO_PAC_OPAQUE);
        pac.opaque.hdr.length = htons(sizeof(pac.opaque) - sizeof(pac.opaque.hdr) - sizeof(pac.opaque.data) + dlen);
index 6cc9852c15a1fa325a32a50d84caac97b345aeec..190c02df624525b4d555b7e2d6562b393f46bf07 100644 (file)
@@ -33,18 +33,15 @@ USES_APPLE_DEPRECATED_API   /* OpenSSL API has been deprecated by Apple */
 
 #include "eap_fast_crypto.h"
 
-// http://stackoverflow.com/a/29838852
-static void NEVER_RETURNS handleErrors(void)
+static void debug_errors(void)
 {
        unsigned long errCode;
 
-       fprintf(stderr, "An error occurred\n");
        while((errCode = ERR_get_error()))
        {
                char *err = ERR_error_string(errCode, NULL);
-               fprintf(stderr, "%s\n", err);
+               DEBUG("EAP-FAST error in OpenSSL - %s", err);
        }
-       abort();
 }
 
 // https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption#Authenticated_Encryption_using_GCM_mode
@@ -61,41 +58,65 @@ int eap_fast_encrypt(uint8_t const *plaintext, size_t plaintext_len,
 
 
        /* Create and initialise the context */
-       if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
+       if (!(ctx = EVP_CIPHER_CTX_new())) {
+               debug_errors();
+               return -1;
+       };
 
        /* Initialise the encryption operation. */
        if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
-               handleErrors();
+       {
+               debug_errors();
+               return -1;
+       };
 
        /* Set IV length if default 12 bytes (96 bits) is not appropriate */
        if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL))
-               handleErrors();
+       {
+               debug_errors();
+               return -1;
+       };
 
        /* Initialise key and IV */
-       if (1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) handleErrors();
+       if (1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) {
+               debug_errors();
+               return -1;
+       };
 
        /* Provide any AAD data. This can be called zero or more times as
         * required
         */
        if (1 != EVP_EncryptUpdate(ctx, NULL, &len, aad, aad_len))
-               handleErrors();
+       {
+               debug_errors();
+               return -1;
+       };
 
        /* Provide the message to be encrypted, and obtain the encrypted output.
         * EVP_EncryptUpdate can be called multiple times if necessary
         */
        if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
-               handleErrors();
+       {
+               debug_errors();
+               return -1;
+       };
        ciphertext_len = len;
 
        /* Finalise the encryption. Normally ciphertext bytes may be written at
         * this stage, but this does not occur in GCM mode
         */
-       if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
+       if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) {
+               debug_errors();
+               return -1;
+       };
        ciphertext_len += len;
 
        /* Get the tag */
        if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
-               handleErrors();
+       {
+               debug_errors();
+               return -1;
+       };
 
        /* Clean up */
        EVP_CIPHER_CTX_free(ctx);
@@ -113,30 +134,48 @@ int eap_fast_decrypt(uint8_t const *ciphertext, size_t ciphertext_len,
        int ret;
 
        /* Create and initialise the context */
-       if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
+       if (!(ctx = EVP_CIPHER_CTX_new())) {
+               debug_errors();
+               return -1;
+       };
 
        /* Initialise the decryption operation. */
        if (!EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
-               handleErrors();
+       {
+               debug_errors();
+               return -1;
+       };
 
        /* Set IV length. Not necessary if this is 12 bytes (96 bits) */
        if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL))
-               handleErrors();
+       {
+               debug_errors();
+               return -1;
+       };
 
        /* Initialise key and IV */
-       if (!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv)) handleErrors();
+       if (!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv)) {
+               debug_errors();
+               return -1;
+       };
 
        /* Provide any AAD data. This can be called zero or more times as
         * required
         */
        if (!EVP_DecryptUpdate(ctx, NULL, &len, aad, aad_len))
-               handleErrors();
+       {
+               debug_errors();
+               return -1;
+       };
 
        /* Provide the message to be decrypted, and obtain the plaintext output.
         * EVP_DecryptUpdate can be called multiple times if necessary
         */
        if (!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
-               handleErrors();
+       {
+               debug_errors();
+               return -1;
+       };
        plaintext_len = len;
 
        {
@@ -145,7 +184,10 @@ int eap_fast_decrypt(uint8_t const *ciphertext, size_t ciphertext_len,
                memcpy(&tmp, &tag, sizeof(tmp));
 
                /* Set expected tag value. Works in OpenSSL 1.0.1d and later */
-               if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tmp)) handleErrors();
+               if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tmp)) {
+                       debug_errors();
+                       return -1;
+               };
        }
 
        /* Finalise the decryption. A positive return value indicates success,
index 02ea77f3c220daf7284e8d3e970d8ce9f1acad64..528a143d47f9b63e1669eac79abb7a6c18280c40 100644 (file)
@@ -260,7 +260,7 @@ static int _session_ticket(SSL *s, uint8_t const *data, int len, void *arg)
        DICT_ATTR const *fast_da;
        char const              *errmsg;
        int                     dlen, plen;
-       uint16_t                length;
+       int                     length;
        eap_fast_attr_pac_opaque_t const        *opaque = (eap_fast_attr_pac_opaque_t const *) data;
        eap_fast_attr_pac_opaque_t              opaque_plaintext;
 
@@ -293,7 +293,7 @@ error:
         * so we have to use the length in the PAC-Opaque header
         */
        length = ntohs(opaque->hdr.length);
-       if (len - sizeof(opaque->hdr) < length) {
+       if (len < (int) (length + sizeof(opaque->hdr))) {
                errmsg = "PAC has bad length in header";
                goto error;
        }
@@ -312,7 +312,7 @@ error:
        plen = eap_fast_decrypt(opaque->data, dlen, opaque->aad, PAC_A_ID_LENGTH,
                                        (uint8_t const *) opaque->tag, t->pac_opaque_key, opaque->iv,
                                        (uint8_t *)&opaque_plaintext);
-       if (plen == -1) {
+       if (plen < 0) {
                errmsg = "PAC failed to decrypt";
                goto error;
        }