add: test for loading go-ipfs 0.4.23 rsa key

This commit is contained in:
Joonas Koivunen 2020-03-12 12:58:45 +02:00 committed by Joonas Koivunen
parent 6b780a767d
commit e0179d5ef4

View File

@ -139,37 +139,12 @@ pub enum LoadingError {
/// Returns only the [`ipfs::KeyPair`] or [`LoadingError`] but this should be extended to contain
/// the bootstrap nodes at least later when we need to support those for testing purposes.
pub fn load(config: File) -> Result<ipfs::Keypair, LoadingError> {
use keys_proto::KeyType;
use multibase::Base::Base64Pad;
use prost::Message;
use std::io::BufReader;
let CompatibleConfigFile { identity } = serde_json::from_reader(BufReader::new(config))
.map_err(LoadingError::ConfigurationFileFormat)?;
let bytes = Base64Pad
.decode(identity.private_key)
.map_err(|e| LoadingError::PrivateKeyLoadingFailed(Box::new(e)))?;
let private_key = keys_proto::PrivateKey::decode(bytes.as_slice())
.map_err(|e| LoadingError::PrivateKeyLoadingFailed(Box::new(e)))?;
let kp = match KeyType::from_i32(private_key.r#type) {
Some(KeyType::Rsa) => {
let pk = openssl::rsa::Rsa::private_key_from_der(&private_key.data)
.map_err(|e| LoadingError::PrivateKeyLoadingFailed(Box::new(e)))?;
let pkcs8 = openssl::pkey::PKey::from_rsa(pk)
.and_then(|pk| pk.private_key_to_pem_pkcs8())
.map_err(|e| LoadingError::PrivateKeyLoadingFailed(Box::new(e)))?;
let mut pkcs8 = pem_to_der(&pkcs8);
ipfs::Keypair::rsa_from_pkcs8(&mut pkcs8)
.map_err(|e| LoadingError::PrivateKeyLoadingFailed(Box::new(e)))?
}
_keytype => return Err(LoadingError::UnsupportedPrivateKeyType(private_key.r#type)),
};
let kp = identity.load_keypair()?;
let peer_id = kp.public().into_peer_id().to_string();
@ -262,6 +237,38 @@ struct Identity {
private_key: String,
}
impl Identity {
fn load_keypair(&self) -> Result<ipfs::Keypair, LoadingError> {
use keys_proto::KeyType;
use multibase::Base::Base64Pad;
use prost::Message;
let bytes = Base64Pad
.decode(&self.private_key)
.map_err(|e| LoadingError::PrivateKeyLoadingFailed(Box::new(e)))?;
let private_key = keys_proto::PrivateKey::decode(bytes.as_slice())
.map_err(|e| LoadingError::PrivateKeyLoadingFailed(Box::new(e)))?;
Ok(match KeyType::from_i32(private_key.r#type) {
Some(KeyType::Rsa) => {
let pk = openssl::rsa::Rsa::private_key_from_der(&private_key.data)
.map_err(|e| LoadingError::PrivateKeyLoadingFailed(Box::new(e)))?;
let pkcs8 = openssl::pkey::PKey::from_rsa(pk)
.and_then(|pk| pk.private_key_to_pem_pkcs8())
.map_err(|e| LoadingError::PrivateKeyLoadingFailed(Box::new(e)))?;
let mut pkcs8 = pem_to_der(&pkcs8);
ipfs::Keypair::rsa_from_pkcs8(&mut pkcs8)
.map_err(|e| LoadingError::PrivateKeyLoadingFailed(Box::new(e)))?
}
_keytype => return Err(LoadingError::UnsupportedPrivateKeyType(private_key.r#type)),
})
}
}
#[cfg(test)]
mod test {
#[test]
@ -286,4 +293,24 @@ aGVsbG8gd29ybGQ=
pem_to_der(input.as_bytes());
}
#[test]
fn read_private_key_from_goipfs() {
use super::Identity;
// generated with go-ipfs 0.4.23, init --bits 2048
let input = Identity {
peer_id: String::from("QmVNXj4TENKBjaUmQQzYMawDXu5LcEEzLyf4K6Ds3WgcF3"),
private_key: String::from("CAASqQkwggSlAgEAAoIBAQDVwj2MoXUccztSbZarmjQusB+7dZw1ZDycnGlOtLTjsc/Fl7keESwQB+nSXvt3DjV+ftTmK3nPODNVY2c+nooyX3k9svQogSmHxfQIwHkKe11VmrMNTdsYwfswcDq4PgNWrGX8/vUBtfvVb0qzgevBXwc4C9+SDIhRtjiHRNSexc2vFx59tQv03VTfj3sbxdBTwWN+ReeCTyf+7nE3Mg7NdHQ78mysMDFT3w1HDwZ+qt4dpyH5mZRm0anNWQUBtQue7IwzUsHzVCUzm+NeYXJf/miSNw2CCQUfA245+H6zu1F0SJFvTVTKCEmZ7D2ZkseRG73Srm0rdD1jajLiBhUtAgMBAAECggEBALy/mHOuOefWRGKDjBCYyE0Vjd+MeVOX4AF2B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+InoVC+KEAkRPjHU3gFGdBvlDif3x2a8eFsZl//RCd9QdpTToynhl+WNKqQH87kfjsBoFW1L5QYLTbKK558QUp9yR6siKW0viXDbOvB7lK8WaDdYX8lcA=="),
};
let peer_id = input
.load_keypair()
.unwrap()
.public()
.into_peer_id()
.to_string();
assert_eq!(peer_id, input.peer_id);
}
}