Waleed Elmelegy 255db80910 Improve & test legacy mbedtls_pkcs12_pbe
* Prevent pkcs12_pbe encryption when PKCS7 padding has been
  disabled since this not part of the specs.
* Allow decryption when PKCS7 padding is disabled for legacy
  reasons, However, invalid padding is not checked.
* Document new behaviour, known limitations and possible
  security concerns.
* Add tests to check these scenarios. Test data has been
  generated by the below code using OpenSSL as a reference:

#include <openssl/pkcs12.h>
#include <openssl/evp.h>
#include <openssl/des.h>
#include <openssl/asn1.h>
#include "crypto/asn1.h"
#include <string.h>

int main()
{
    char pass[] = "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB";
    unsigned char salt[] = "\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC";
    unsigned char plaintext[] = "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
    unsigned char *ciphertext = NULL;
    int iter = 10;
    X509_ALGOR *alg =  X509_ALGOR_new();
    int ciphertext_len = 0;
    int alg_nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
    alg->parameter = ASN1_TYPE_new();
    struct asn1_object_st * aobj;
    PKCS5_pbe_set0_algor(alg, alg_nid, iter,
                         salt, sizeof(salt)-1);

    aobj = alg->algorithm;
    printf("\"30%.2X", 2 + aobj->length + alg->parameter->value.asn1_string->length);
    printf("06%.2X", aobj->length);
    for (int i = 0; i < aobj->length; i++) {
        printf("%.2X", aobj->data[i]);
    }

    for (int i = 0; i < alg->parameter->value.asn1_string->length; i++) {
        printf("%.2X", alg->parameter->value.asn1_string->data[i]);
    }
    printf("\":\"");

    for (int i = 0; i < sizeof(pass)-1; i++) {
        printf("%.2X", pass[i] & 0xFF);
    }
    printf("\":\"");
    for (int i = 0; i < sizeof(plaintext)-1; i++) {
        printf("%.2X", plaintext[i]);
    }
    printf("\":");
    printf("0");
    printf(":\"");

    unsigned char * res = PKCS12_pbe_crypt(alg, pass, sizeof(pass)-1, plaintext, sizeof(plaintext)-1, &ciphertext, &ciphertext_len, 1);

    if (res == NULL)
        printf("Encryption failed!\n");
    for (int i = 0; i < ciphertext_len; i++) {
        printf("%.2X", res[i]);
    }
    printf("\"\n");

    return 0;
}

Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
#
2023-09-05 15:45:55 +01:00
..
2023-08-20 20:20:12 +01:00
2023-08-18 17:35:59 +08:00
2023-07-11 14:06:37 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-23 19:10:26 +01:00
2023-08-17 14:36:59 +01:00
2023-01-11 14:50:10 +01:00
2023-06-24 13:37:28 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-08-14 15:26:28 +01:00
2023-06-24 13:37:28 +01:00
2023-08-16 12:48:33 +01:00
2023-01-11 14:50:10 +01:00
2023-04-12 06:07:23 +00:00
2023-08-01 22:41:17 +08:00
2023-06-06 10:33:54 +02:00
2023-08-02 10:32:21 +01:00
2023-01-11 14:50:10 +01:00
2023-03-24 10:43:40 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-06-24 13:37:28 +01:00
2023-08-10 13:36:32 +08:00
2023-08-17 11:20:09 +08:00
2023-07-06 12:42:33 +02:00
2023-07-06 12:42:33 +02:00
2023-03-13 12:15:49 +00:00
2023-08-10 06:43:23 +02:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-02-02 12:40:50 +00:00
2023-01-11 14:50:10 +01:00
2023-08-10 12:11:31 +01:00
2023-01-11 14:50:10 +01:00
2023-06-07 19:59:05 +01:00
2023-07-13 10:40:29 +08:00
2023-07-13 10:40:29 +08:00
2023-01-11 14:50:10 +01:00
2023-08-10 14:01:51 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-01-11 14:50:10 +01:00
2023-08-16 12:48:33 +01:00