signatures: Upgrade pkcs8

This commit is contained in:
Jonas Platte 2021-06-21 17:28:55 +02:00
parent 55a7dd4828
commit fc99c32c3d
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
3 changed files with 9 additions and 9 deletions

View File

@ -21,7 +21,7 @@ unstable-exhaustive-types = []
[dependencies] [dependencies]
base64 = "0.13.0" base64 = "0.13.0"
ed25519-dalek = "1.0.1" ed25519-dalek = "1.0.1"
pkcs8 = { version = "0.6.1", features = ["alloc"] } pkcs8 = { version = "0.7.0", features = ["alloc"] }
# because dalek uses an older version of rand_core # because dalek uses an older version of rand_core
rand = { version = "0.7", features = ["getrandom"] } rand = { version = "0.7", features = ["getrandom"] }
ruma-identifiers = { version = "0.19.3", path = "../ruma-identifiers" } ruma-identifiers = { version = "0.19.3", path = "../ruma-identifiers" }

View File

@ -9,7 +9,7 @@ use ed25519_dalek::{ExpandedSecretKey, PublicKey, SecretKey};
use pkcs8::{ use pkcs8::{
der::{Decodable, Encodable}, der::{Decodable, Encodable},
AlgorithmIdentifier, ObjectIdentifier, OneAsymmetricKey, PrivateKeyInfo, AlgorithmIdentifier, ObjectIdentifier, PrivateKeyInfo,
}; };
use crate::{signatures::Signature, Algorithm, Error, ParseError}; use crate::{signatures::Signature, Algorithm, Error, ParseError};
@ -90,13 +90,13 @@ impl Ed25519KeyPair {
/// generated from the private key. This is a fallback and extra validation against /// generated from the private key. This is a fallback and extra validation against
/// corruption or /// corruption or
pub fn from_der(document: &[u8], version: String) -> Result<Self, Error> { pub fn from_der(document: &[u8], version: String) -> Result<Self, Error> {
let oak = OneAsymmetricKey::from_der(document).map_err(Error::DerParse)?; let oak = PrivateKeyInfo::from_der(document).map_err(Error::DerParse)?;
Self::from_pkcs8_oak(oak, version) Self::from_pkcs8_oak(oak, version)
} }
/// Constructs a key pair from [`pkcs8::OneAsymmetricKey`]. /// Constructs a key pair from [`pkcs8::PrivateKeyInfo`].
pub fn from_pkcs8_oak(oak: OneAsymmetricKey<'_>, version: String) -> Result<Self, Error> { pub fn from_pkcs8_oak(oak: PrivateKeyInfo<'_>, version: String) -> Result<Self, Error> {
Self::new(oak.algorithm.oid, oak.private_key, oak.public_key, version) Self::new(oak.algorithm.oid, oak.private_key, oak.public_key, version)
} }
@ -136,14 +136,14 @@ impl Ed25519KeyPair {
let mut private: Vec<u8> = vec![0x04, 0x20]; let mut private: Vec<u8> = vec![0x04, 0x20];
private.extend_from_slice(secret.as_bytes()); private.extend_from_slice(secret.as_bytes());
let oak = OneAsymmetricKey { let pkinfo = PrivateKeyInfo {
algorithm: AlgorithmIdentifier { oid: ED25519_OID, parameters: None }, algorithm: AlgorithmIdentifier { oid: ED25519_OID, parameters: None },
private_key: private.as_ref(), private_key: private.as_ref(),
attributes: None, attributes: None,
public_key: Some(public.as_bytes()), public_key: Some(public.as_bytes()),
}; };
oak.to_vec().map_err(Error::DerParse) pkinfo.to_vec().map_err(Error::DerParse)
} }
/// Returns the version string for this keypair. /// Returns the version string for this keypair.

View File

@ -105,7 +105,7 @@ mod tests {
use std::collections::BTreeMap; use std::collections::BTreeMap;
use base64::{decode_config, encode_config, STANDARD_NO_PAD}; use base64::{decode_config, encode_config, STANDARD_NO_PAD};
use pkcs8::{der::Decodable, OneAsymmetricKey}; use pkcs8::{der::Decodable, PrivateKeyInfo};
use ruma_identifiers::RoomVersionId; use ruma_identifiers::RoomVersionId;
use serde_json::{from_str as from_json_str, to_string as to_json_string}; use serde_json::{from_str as from_json_str, to_string as to_json_string};
@ -121,7 +121,7 @@ mod tests {
/// Convenience method for getting the public key as a string /// Convenience method for getting the public key as a string
fn public_key_string() -> String { fn public_key_string() -> String {
encode_config( encode_config(
&OneAsymmetricKey::from_der(&decode_config(PKCS8, STANDARD_NO_PAD).unwrap()) &PrivateKeyInfo::from_der(&decode_config(PKCS8, STANDARD_NO_PAD).unwrap())
.unwrap() .unwrap()
.public_key .public_key
.unwrap(), .unwrap(),