|
| 1 | +// This file is dual licensed under the terms of the Apache License, Version |
| 2 | +// 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | +// for complete details. |
| 4 | + |
| 5 | +use cryptography_x509::common::{AlgorithmParameters, EcParameters, SubjectPublicKeyInfo}; |
| 6 | + |
| 7 | +use crate::{KeyParsingError, KeyParsingResult}; |
| 8 | + |
| 9 | +pub fn parse_public_key( |
| 10 | + data: &[u8], |
| 11 | +) -> KeyParsingResult<openssl::pkey::PKey<openssl::pkey::Public>> { |
| 12 | + let k = asn1::parse_single::<SubjectPublicKeyInfo>(data)?; |
| 13 | + |
| 14 | + match k.algorithm.params { |
| 15 | + AlgorithmParameters::Ec(ec_params) => match ec_params { |
| 16 | + EcParameters::NamedCurve(curve_oid) => { |
| 17 | + let curve_nid = match curve_oid { |
| 18 | + cryptography_x509::oid::EC_SECP192R1 => openssl::nid::Nid::X9_62_PRIME192V1, |
| 19 | + cryptography_x509::oid::EC_SECP224R1 => openssl::nid::Nid::SECP224R1, |
| 20 | + cryptography_x509::oid::EC_SECP256R1 => openssl::nid::Nid::X9_62_PRIME256V1, |
| 21 | + cryptography_x509::oid::EC_SECP384R1 => openssl::nid::Nid::SECP384R1, |
| 22 | + cryptography_x509::oid::EC_SECP521R1 => openssl::nid::Nid::SECP521R1, |
| 23 | + |
| 24 | + cryptography_x509::oid::EC_SECP256K1 => openssl::nid::Nid::SECP256K1, |
| 25 | + |
| 26 | + cryptography_x509::oid::EC_SECT233R1 => openssl::nid::Nid::SECT233R1, |
| 27 | + cryptography_x509::oid::EC_SECT283R1 => openssl::nid::Nid::SECT283R1, |
| 28 | + cryptography_x509::oid::EC_SECT409R1 => openssl::nid::Nid::SECT409R1, |
| 29 | + cryptography_x509::oid::EC_SECT571R1 => openssl::nid::Nid::SECT571R1, |
| 30 | + |
| 31 | + cryptography_x509::oid::EC_SECT163R2 => openssl::nid::Nid::SECT163R2, |
| 32 | + |
| 33 | + cryptography_x509::oid::EC_SECT163K1 => openssl::nid::Nid::SECT163K1, |
| 34 | + cryptography_x509::oid::EC_SECT233K1 => openssl::nid::Nid::SECT233K1, |
| 35 | + cryptography_x509::oid::EC_SECT283K1 => openssl::nid::Nid::SECT283K1, |
| 36 | + cryptography_x509::oid::EC_SECT409K1 => openssl::nid::Nid::SECT409K1, |
| 37 | + cryptography_x509::oid::EC_SECT571K1 => openssl::nid::Nid::SECT571K1, |
| 38 | + |
| 39 | + #[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))] |
| 40 | + cryptography_x509::oid::EC_BRAINPOOLP256R1 => { |
| 41 | + openssl::nid::Nid::BRAINPOOL_P256R1 |
| 42 | + } |
| 43 | + #[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))] |
| 44 | + cryptography_x509::oid::EC_BRAINPOOLP384R1 => { |
| 45 | + openssl::nid::Nid::BRAINPOOL_P384R1 |
| 46 | + } |
| 47 | + #[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))] |
| 48 | + cryptography_x509::oid::EC_BRAINPOOLP512R1 => { |
| 49 | + openssl::nid::Nid::BRAINPOOL_P512R1 |
| 50 | + } |
| 51 | + |
| 52 | + _ => return Err(KeyParsingError::UnsupportedEllipticCurve(curve_oid)), |
| 53 | + }; |
| 54 | + |
| 55 | + let group = openssl::ec::EcGroup::from_curve_name(curve_nid) |
| 56 | + .map_err(|_| KeyParsingError::UnsupportedEllipticCurve(curve_oid))?; |
| 57 | + let mut bn_ctx = openssl::bn::BigNumContext::new()?; |
| 58 | + let ec_point = openssl::ec::EcPoint::from_bytes( |
| 59 | + &group, |
| 60 | + k.subject_public_key.as_bytes(), |
| 61 | + &mut bn_ctx, |
| 62 | + ) |
| 63 | + .map_err(|_| KeyParsingError::InvalidKey)?; |
| 64 | + let ec_key = openssl::ec::EcKey::from_public_key(&group, &ec_point)?; |
| 65 | + Ok(openssl::pkey::PKey::from_ec_key(ec_key)?) |
| 66 | + } |
| 67 | + EcParameters::ImplicitCurve(_) | EcParameters::SpecifiedCurve(_) => { |
| 68 | + Err(KeyParsingError::ExplicitCurveUnsupported) |
| 69 | + } |
| 70 | + }, |
| 71 | + AlgorithmParameters::Ed25519 => Ok(openssl::pkey::PKey::public_key_from_raw_bytes( |
| 72 | + k.subject_public_key.as_bytes(), |
| 73 | + openssl::pkey::Id::ED25519, |
| 74 | + )?), |
| 75 | + #[cfg(all(not(CRYPTOGRAPHY_IS_LIBRESSL), not(CRYPTOGRAPHY_IS_BORINGSSL)))] |
| 76 | + AlgorithmParameters::Ed448 => Ok(openssl::pkey::PKey::public_key_from_raw_bytes( |
| 77 | + k.subject_public_key.as_bytes(), |
| 78 | + openssl::pkey::Id::ED448, |
| 79 | + )?), |
| 80 | + AlgorithmParameters::X25519 => Ok(openssl::pkey::PKey::public_key_from_raw_bytes( |
| 81 | + k.subject_public_key.as_bytes(), |
| 82 | + openssl::pkey::Id::X25519, |
| 83 | + )?), |
| 84 | + #[cfg(all(not(CRYPTOGRAPHY_IS_LIBRESSL), not(CRYPTOGRAPHY_IS_BORINGSSL)))] |
| 85 | + AlgorithmParameters::X448 => Ok(openssl::pkey::PKey::public_key_from_raw_bytes( |
| 86 | + k.subject_public_key.as_bytes(), |
| 87 | + openssl::pkey::Id::X448, |
| 88 | + )?), |
| 89 | + AlgorithmParameters::Rsa(_) | AlgorithmParameters::RsaPss(_) => { |
| 90 | + // RSA-PSS keys are treated the same as bare RSA keys. |
| 91 | + crate::rsa::parse_pkcs1_public_key(k.subject_public_key.as_bytes()) |
| 92 | + } |
| 93 | + AlgorithmParameters::Dsa(dsa_params) => { |
| 94 | + let p = openssl::bn::BigNum::from_slice(dsa_params.p.as_bytes())?; |
| 95 | + let q = openssl::bn::BigNum::from_slice(dsa_params.q.as_bytes())?; |
| 96 | + let g = openssl::bn::BigNum::from_slice(dsa_params.g.as_bytes())?; |
| 97 | + |
| 98 | + let pub_key_int = |
| 99 | + asn1::parse_single::<asn1::BigUint<'_>>(k.subject_public_key.as_bytes())?; |
| 100 | + let pub_key = openssl::bn::BigNum::from_slice(pub_key_int.as_bytes())?; |
| 101 | + |
| 102 | + let dsa = openssl::dsa::Dsa::from_public_components(p, q, g, pub_key)?; |
| 103 | + Ok(openssl::pkey::PKey::from_dsa(dsa)?) |
| 104 | + } |
| 105 | + #[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))] |
| 106 | + AlgorithmParameters::Dh(dh_params) => { |
| 107 | + let p = openssl::bn::BigNum::from_slice(dh_params.p.as_bytes())?; |
| 108 | + let q = openssl::bn::BigNum::from_slice(dh_params.q.as_bytes())?; |
| 109 | + let g = openssl::bn::BigNum::from_slice(dh_params.g.as_bytes())?; |
| 110 | + let dh = openssl::dh::Dh::from_pqg(p, Some(q), g)?; |
| 111 | + |
| 112 | + let pub_key_int = |
| 113 | + asn1::parse_single::<asn1::BigUint<'_>>(k.subject_public_key.as_bytes())?; |
| 114 | + let pub_key = openssl::bn::BigNum::from_slice(pub_key_int.as_bytes())?; |
| 115 | + let dh = dh.set_public_key(pub_key)?; |
| 116 | + |
| 117 | + cfg_if::cfg_if! { |
| 118 | + if #[cfg(CRYPTOGRAPHY_IS_LIBRESSL)] { |
| 119 | + Ok(openssl::pkey::PKey::from_dh(dh)?) |
| 120 | + } else { |
| 121 | + Ok(openssl::pkey::PKey::from_dhx(dh)?) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + #[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))] |
| 126 | + AlgorithmParameters::DhKeyAgreement(dh_params) => { |
| 127 | + let p = openssl::bn::BigNum::from_slice(dh_params.p.as_bytes())?; |
| 128 | + let g = openssl::bn::BigNum::from_slice(dh_params.g.as_bytes())?; |
| 129 | + let dh = openssl::dh::Dh::from_pqg(p, None, g)?; |
| 130 | + |
| 131 | + let pub_key_int = |
| 132 | + asn1::parse_single::<asn1::BigUint<'_>>(k.subject_public_key.as_bytes())?; |
| 133 | + let pub_key = openssl::bn::BigNum::from_slice(pub_key_int.as_bytes())?; |
| 134 | + let dh = dh.set_public_key(pub_key)?; |
| 135 | + |
| 136 | + Ok(openssl::pkey::PKey::from_dh(dh)?) |
| 137 | + } |
| 138 | + _ => Err(KeyParsingError::UnsupportedKeyType( |
| 139 | + k.algorithm.oid().clone(), |
| 140 | + )), |
| 141 | + } |
| 142 | +} |
0 commit comments