~aritra1911/openssl_madness

d56ad0a6bd333d13cdbe2f3da432db4aa4b07de5 — Aritra Sarkar 2 years ago a82d7c3
Check key id for RSA key validation
2 files changed, 36 insertions(+), 3 deletions(-)

M .gitignore
M madness.c
M .gitignore => .gitignore +1 -0
@@ 1,2 1,3 @@
*.o
*.swp
madness

M madness.c => madness.c +35 -3
@@ 36,7 36,7 @@ void print_usage(const char* bin) {
}

int main(int argc, char* argv[]) {
    EVP_PKEY *private_key = NULL, *public_key = NULL;
    EVP_PKEY *private_key, *public_key;
    FILE *fp, *fpin, *fpout;
    char *input_filename = NULL, *key_filename = NULL, *output_filename = NULL;
    int decrypt = -1, verbose = 0;


@@ 137,6 137,10 @@ int main(int argc, char* argv[]) {

    // Fetch key from PEM formattted key in key file
    if (!decrypt) {
        /* Set the key to NULL, so that `PEM_read_PUBKEY()` allocates a new
         * EVP_PKEY structure to it */
        public_key = NULL;

        if (!PEM_read_PUBKEY(fp, &public_key, NULL, NULL)) {
            fprintf(stderr, "Not a valid public key!!!\n");
            putchar('\n');


@@ 145,9 149,25 @@ int main(int argc, char* argv[]) {
            return EXIT_FAILURE;
        }

        if (verbose) printf("RSA %d\n", EVP_PKEY_get_bits(public_key));
        if (verbose) {
            printf("INFO : %s\n"
                   "     : Key size = %i bits (%i bytes)\n",
                   EVP_PKEY_get0_description(public_key),
                   EVP_PKEY_get_bits(public_key),
                   EVP_PKEY_get_size(public_key));
        }

        if ( EVP_PKEY_get_id(public_key) != EVP_PKEY_RSA ) {
            fprintf(stderr, "Not a valid RSA public key!!!\n");
            fclose(fp);
            return EXIT_FAILURE;
        }

    } else {
        /* Set the key to NULL, so that `PEM_read_PrivateKey()` allocates a new
         * EVP_PKEY structure to it */
        private_key = NULL;

        if (!PEM_read_PrivateKey(fp, &private_key, NULL, NULL)) {
            fprintf(stderr, "Not a valid private key!!!\n");
            putchar('\n');


@@ 156,7 176,19 @@ int main(int argc, char* argv[]) {
            return EXIT_FAILURE;
        }

        if (verbose) printf("RSA %d\n", EVP_PKEY_get_bits(private_key));
        if (verbose) {
            printf("INFO : %s\n"
                   "     : Key size = %i bits (%i bytes)\n",
                   EVP_PKEY_get0_description(private_key),
                   EVP_PKEY_get_bits(private_key),
                   EVP_PKEY_get_size(private_key));
        }

        if ( EVP_PKEY_get_id(public_key) != EVP_PKEY_RSA ) {
            fprintf(stderr, "Not a valid RSA private key!!!\n");
            fclose(fp);
            return EXIT_FAILURE;
        }
    }
    fclose(fp);  // Close key file