Skip to content

error: An error in the OpenSSL library: PEM lib, no start line, no start line #205

Closed

Description

@ghmlee
An error in the OpenSSL library: PEM lib, no start line, no start line
An error in the OpenSSL library: PEM lib

sometimes I got these errors after set_CA_file() of openssl::ssl::SslContext is called even if CA file is right.

Activity

  1. sfackler commented on Apr 27, 2015

    @sfackler
    Collaborator

    Are you sure the CA file is in the right format? It should look something like

    -----BEGIN CERTIFICATE-----
    MIIDXTCCAkWgAwIBAgIJAMWJMG/NYWQyMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
    BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
    aWRnaXRzIFB0eSBMdGQwHhcNMTQxMTIwMDU0MjIxWhcNMTcxMTE5MDU0MjIxWjBF
    MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
    ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
    CgKCAQEA2E9uYoLpX4eMGz6+l+1ZdD11Y1PQjNjqSA7/nq0Q6gogPLds99a+Ca7B
    2w6mWGMpCQbJTf0tOkdF6Td/gwIBNYtHuCIAiPh2Gbm6oZErVIXWwWuTWC2r7myB
    0ePga5ZAE9SqsFqMEuhWikEK1+ae1CCfmbogsQSXyl4+EVN7xAwdi6yUtRL/92nn
    ImKdwDBhuqzdBpBODQW/VCn0KG54CvWdwT0iqg7CvLuXQGyxM8K17SAiBtn6N38S
    0jeL4D9IrBfi9PCYGpAn3jKr4oBEJVRCNvvni3Q9ikJ+GEF5nvLLVM+I98/o0vCu
    Y2o7+1LiwViGONJ1BfRgdFWMjz0BfQIDAQABo1AwTjAdBgNVHQ4EFgQU509vewJT
    k6qdvjT8e52gTE4+V9EwHwYDVR0jBBgwFoAU509vewJTk6qdvjT8e52gTE4+V9Ew
    DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAnLxMvrhgQrdKUbd+RSl3
    i6tEfexDepO5QOMIL/m7w/OAkXddu7Y/IuLSgAztp7cu8tQeeBTqbO9T9XKRd1ZD
    hMN3QPZwX1ftREPd1mhe3LQgJ/Q3AbrtlW+0XcANBCTiYi19X193sU2a3Z2nzd5v
    vSvrP1W3Yxzvu7Jwvhgl20BQ0fREpPVfZTrJRhg8/jdLA8TmKZA7012LKh59BCEX
    dXgXc3KYb+Fn8usj79P1nP1YhYmX0Lp0IuC0i1z+FoP/NxNMe+ebXPjM9KlkvQK1
    Vy9sIxV9MAlXpGsMvBJAte94JRikYkQTPjMT1DGQQYGpHIYb3a8jcWvmZPEEaqhB
    Ag==
    -----END CERTIFICATE-----
    
  2. ghmlee commented on Apr 27, 2015

    @ghmlee
    Author

    Yes, I am sure. This is my CA file example.

    -----BEGIN CERTIFICATE-----
    MIIC1jCCAcCgAwIBAgIRAJt/ZX1Py7FTjvJNyihkDskwCwYJKoZIhvcNAQELMBYx
    FDASBgNVBAoTC0Jvb3QyRG9ja2VyMB4XDTE1MDMyOTAzMjY0MloXDTE4MDMxMzAz
    MjY0MlowFjEUMBIGA1UEChMLQm9vdDJEb2NrZXIwggEiMA0GCSqGSIb3DQEBAQUA
    A4IBDwAwggEKAoIBAQCiZjZPSJmNdwOx/A5Gewb4lxq0ji6ioHRsa9eA5uTg3v3u
    W0LcHMgwPJQBEZXxTOTBqQBXfd1MFrKE8jw80vWtUbmhD6WyMUGLLcOAFKeR2Pdl
    mdDkjDmai9DV/JWrJQytpWSHXOYcuuTZSN4a83vHG8ElteAwZw1ypUTl0XKpmq7e
    EnpY5WclKGm0m0aCcwfPD44hWzDVZbKFX+MiTJObCEUi0mlLvw7fEkmzzWsX7XxG
    Fy/HfWEB8ynHlTFawDDe4fBT2KfLtFsvsEKO2jvlluEXg2qREbGbwaQZ4Ob7R1we
    Sabf3ZgbinBOeOq/QhErIky7uiC4r7eGlVyzoSXDAgMBAAGjIzAhMA4GA1UdDwEB
    /wQEAwIApDAPBgNVHRMBAf8EBTADAQH/MAsGCSqGSIb3DQEBCwOCAQEAh+WDPSPS
    gY0AmTcKX2jrwuz7xt21blv4Uc/zdTxnpM4gS6Gv3PfYcVHVNEsBe3z9q9vnb7R0
    x6jBXc9wrKk9eJaeWg8rerBBfR9IoeQSGPS3OESQVmwYvu5n8Br3M8TEd1ywn8hS
    8K9ihWHkKg7BPES7Lwv59jRhF+oCJyDudyggoZ2OrOYN4cCjPG7wWsMdnNIMQcb8
    I8kDQvzQ+LdgxWMXMQVAmSXaqxE+D6lZpvx2M0pKixQ/iP8U3gLE48OrlR1UTjry
    CICbbUuwAZxQx1g6R4wsuU/0sRmJVyZBbej+Zof1oWqUZDrYcERiDGC1cTXJIOZk
    M8iyKqyfwTgiuQ==
    -----END CERTIFICATE-----
    
  3. sfackler commented on Apr 27, 2015

    @sfackler
    Collaborator

    Could you post a small chunk of code that reproduces the issue? set_CA_file seems to work fine with that cert for me with a small program:

    extern crate openssl;
    
    use openssl::ssl::{SslContext, SslMethod};
    use std::path::Path;
    
    fn main() {
        let mut ctx = SslContext::new(SslMethod::Sslv23).unwrap();
        ctx.set_CA_file(&Path::new("cert.pem")).unwrap();
    }
  4. ghmlee commented on Apr 28, 2015

    @ghmlee
    Author

    I think my problem is a multithreaded bug. there are functions as a variable for a private key and a certificate but for CA there are not. It needs to read from a file.

  5. ghmlee commented on Apr 28, 2015

    @ghmlee
    Author

    Do I need to make a Mutex for every SslStream? In the openssl code, I think it caches just only path as a string and when stream is created and sent, it uses CA file from a file.

    extern crate openssl;
    
    use std::thread;
    use openssl::ssl::{SslContext, SslMethod};
    use std::path::Path;
    
    fn main() {
        thread::spawn(move||{
            loop {
                let key = Path::new("/Users/iMac/.boot2docker/certs/boot2docker-vm/key.pem");
                let cert = Path::new("/Users/iMac/.boot2docker/certs/boot2docker-vm/cert.pem");
                let ca = Path::new("/Users/iMac/.boot2docker/certs/boot2docker-vm/ca.pem");
    
                let mut ctx = SslContext::new(SslMethod::Tlsv1).unwrap();
    
                match ctx.set_private_key_file(&key, openssl::x509::X509FileType::PEM) {
                    Ok(_) => {}
                    Err(e) => {
                        println!("1. {}", e);
                        continue;
                    }
                }
    
                match ctx.set_certificate_file(&cert, openssl::x509::X509FileType::PEM) {
                    Ok(_) => {}
                    Err(e) => {
                        println!("2. {}", e);
                        continue;
                    }
                }
    
                match ctx.set_CA_file(&ca) {
                    Ok(_) => {}
                    Err(e) => {
                        println!("3. {}", e);
                        continue;
                    }
                }
            }
        });
        thread::spawn(move||{
            loop {
                let key = Path::new("/Users/iMac/.boot2docker/certs/boot2docker-vm/key.pem");
                let cert = Path::new("/Users/iMac/.boot2docker/certs/boot2docker-vm/cert.pem");
                let ca = Path::new("/Users/iMac/.boot2docker/certs/boot2docker-vm/ca.pem");
    
                let mut ctx = SslContext::new(SslMethod::Tlsv1).unwrap();
    
                match ctx.set_private_key_file(&key, openssl::x509::X509FileType::PEM) {
                    Ok(_) => {}
                    Err(e) => {
                        println!("1. {}", e);
                        continue;
                    }
                }
    
                match ctx.set_certificate_file(&cert, openssl::x509::X509FileType::PEM) {
                    Ok(_) => {}
                    Err(e) => {
                        println!("2. {}", e);
                        continue;
                    }
                }
    
                match ctx.set_CA_file(&ca) {
                    Ok(_) => {}
                    Err(e) => {
                        println!("3. {}", e);
                        continue;
                    }
                }
            }
        });
        loop {
            thread::sleep_ms(100);
        }
    }
  6. sfackler commented on Apr 28, 2015

    @sfackler
    Collaborator

    You shouldn't need to do any manual locking at all. I can reproduce this on OSX's default OpenSSL install (version 0.9.8), but not with version 1.0.2 on either Linux or OSX. This could either be because there was a race condition in OpenSSL that was fixed or some part of the API behaves differently between 0.9.8 and 1.0.2 in a way that rust-openssl doesn't handle right now.

    As a work around, you can grab 1.0.2 (I pulled it in via homebrew) and override the location rust-openssl will look with some environment variables that are documented in the README:

    env OPENSSL_LIB_DIR=/usr/local/Cellar/openssl/1.0.2/lib/ OPENSSL_INCLUDE_DIR=/usr/local/Cellar/openssl/1.0.2/include/ DYLD_LIBRARY_PATH=/usr/local/Cellar/openssl/1.0.2/lib/ cargo run
    
  7. ghmlee commented on Apr 28, 2015

    @ghmlee
    Author

    @sfackler thank you so much. I appreciate your help. :)

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