Envelope problem

Robert Burner Schadek realburner at gmx.de
Tue Mar 9 17:36:34 UTC 2021


Hello all,

I'm trying to follow the guide shown in
https://wiki.openssl.org/index.php/EVP_Asymmetric_Encryption_and_Decryption_of_an_Envelope

But for the life of me I can't figure out how to do the following:

```
struct Data {
    unsigned char* data;
    unsigned long length;
};

struct KeyArray {
    EVP_PKEY** keys;
    unsigned long numberKeys;
};

/* When Data is written to disk the openssl cli should be
 * able to decrypt the resulting file with something like
 * $ openssl rsautl -decrypt -in ENCRYPTED -out PLAINTEXT -inkey
keys/privkey.pem
 */
Data encryptWithMultiplePublicKeys(Data input, KeyArray keys) {
    // WHAT GOES HERE?
}

Data decryptWithSinglePublicKey(Data input, EVP_PKEY* key) {
    // WHAT GOES HERE?
}

int main() {
    unsigned char* dataToEncrypt = (unsigned char*)"whatever";
    Data data;
    data.data = dataToEncrypt;
    dtat.length = strlen(dataToEncrypt);

    KeyArray ka = fillKeyArrayFromSomewhere();
   
    Data encryptedData = encryptWithMultiplePublicKeys(data, kArray);

    // see the requirement for this file in the comment above
    FILE* f = fopen("ENCRYPTED", "w");
    fwrite(encryptedData, 1, encryptedData.length, f);
    fclose(f);

    Data decryptedData = decryptWithSinglePublicKey(encryptedData,
ka.keys[rand() % ka.numberKeys]);

    // this assert should be true
    assert(strcmp(dataToEncrypt, decryptedData.data));

    return 0;
}

```

The parts I can't figure out are the contents of the two empty functions
in the example.

I tried working out how the openssl cli does this, but I couldn't.

Help would be much appreciated.

Best regards
Robert


More information about the openssl-users mailing list