Skip to content

KeyPair::from_pem detects incorrect algorithm? #193

Open
@brocaar

Description

@brocaar

How to reproduce

Test certificates

Working certificates (-sha256 option)

openssl req -x509 \
                        -sha256 -days 356 \
                        -nodes \
                        -newkey rsa:4096 \
                        -subj "/CN=example.com" \
                        -keyout rootCA.key -out rootCA.crt

Failing certificates (-sha512 option)

openssl req -x509 \
                        -sha512 -days 356 \
                        -nodes \
                        -newkey rsa:4096 \
                        -subj "/CN=example.com" \
                        -keyout rootCA.key -out rootCA.crt

Code

use std::fs::read_to_string;

fn main() {
    let private_key = read_to_string("rootCA.key").unwrap();
    let private_key = rcgen::KeyPair::from_pem(&private_key).unwrap();
    println!("KeyPair alg: {:?}", private_key.algorithm());

    let cert = read_to_string("rootCA.crt").unwrap();
    let params = rcgen::CertificateParams::from_ca_cert_pem(&cert, private_key).unwrap();

    println!("Params alg: {:?}", params.alg);

    if let Err(e) = rcgen::Certificate::from_params(params) {
        println!("Error: {:#}", e);
    } else {
        println!("All good :-)")
    };
}

Test results

If using the -sha256 certificate files, the output is:

KeyPair alg: PKCS_RSA_SHA256
Params alg: PKCS_RSA_SHA256
All good :-)

If using the -sha512 certificate files, the output is:

KeyPair alg: PKCS_RSA_SHA256
Params alg: PKCS_RSA_SHA512
Error: The provided certificate's signature algorithm is incompatible with the given key pair

Is this expected?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions