fn verify_signature(signature_buf: &[u8], signed_bytes: &[u8], public_key: &PublicKey) -> Result<Vec<u8>> {
    let mut reader = Reader::new(&signature_buf);
    let sig_type = reader.read_string().and_then(|v| KeyType::from_name(&v))?;

    match &public_key.kind {
         PublicKeyKind::Ecdsa(key) => {
            let sig_reader = reader.read_bytes()?;
            let mut reader = Reader::new(&sig_reader);

            let (alg, len) = match sig_type.name {
                "ecdsa-sha2-nistp256" => (&ECDSA_P256_SHA256_FIXED, 32),
                "ecdsa-sha2-nistp384" => (&ECDSA_P384_SHA384_FIXED, 48),
                _ => return Err(Error::KeyTypeMismatch),
            };

            // Read the R value
            let r_bytes = reader.read_mpint()?;
            // Read the S value
            let s_bytes = reader.read_mpint()?;

            // *_bytes are user controlled so ensure maliciously signatures
            // can't cause integer underflow.
            if r_bytes.len() > len || s_bytes.len() > len {
                return Err(Error::InvalidFormat);
            }

            // Determine and create the padding required
            let mut r = vec![0; len - r_bytes.len()];
            let mut s = vec![0; len - s_bytes.len()];

            // Pad *_bytes
            r.extend(r_bytes);
            s.extend(s_bytes);

            // Build a properly padded signature
            let mut sig = r;
            sig.extend(s);

            UnparsedPublicKey::new(alg, &key.key).verify(&signed_bytes, &sig)?;
            Ok(signature_buf.to_vec())
        },
        PublicKeyKind::Rsa(key) => {
            let alg = match sig_type.name {
                "rsa-sha2-256" => &RSA_PKCS1_2048_8192_SHA256,
                "rsa-sha2-512" => &RSA_PKCS1_2048_8192_SHA512,
                "ssh-rsa" => &RSA_PKCS1_2048_8192_SHA1_FOR_LEGACY_USE_ONLY,
                _ => return Err(Error::KeyTypeMismatch),
            };
            let signature = reader.read_bytes()?;
            let public_key = RsaPublicKeyComponents { n: &key.n, e: &key.e };
            public_key.verify(alg, &signed_bytes, &signature)?;
            Ok(signature_buf.to_vec())
        },
        PublicKeyKind::Ed25519(key) => {
            let alg = &ED25519;
            let signature = reader.read_bytes()?;
            let peer_public_key = UnparsedPublicKey::new(alg, &key.key);
            peer_public_key.verify(&signed_bytes, &signature)?;
            Ok(signature_buf.to_vec())
        },
    }
}
// Verifies the certificate's signature is valid.
// Appended to the end of every SSH Cert is a signature for the preceding data,
// depending on the key, the signature could be any of the following:
//
// ECDSA
//  ecdsa-sha2-nistp256
//  ecdsa-sha2-nistp384
//  ecdsa-sha2-nistp521 (but this is unsupported in Ring so not supported here)
//
// RSA
//  rsa-sha2-256
//  rsa-sha2-512
//
// Ed25519
//
// We then take the public key of the CA (immiediately preceeding the signature and part of the signed data)
// and verify the signature accordingly. If the signature is not valid, this function errors.
Verifies the certificate's signature is valid.
Appended to the end of every SSH Cert is a signature for the preceding data,
depending on the key, the signature could be any of the following.







We then take the public key of the CA (immiediately preceeding the signature and part of the signed data)
and verify the signature accordingly. If the signature is not valid, this function errors.
Verifies the certificate ' s signature is valid . Appended to the end of every SSH Cert is a signature for the preceding data depending on the key the signature could be any of the following . We then take the public key of the CA ( immiediately preceeding the signature and part of the signed data ) and verify the signature accordingly . If the signature is not valid this function errors .
fn verify_signature(signature_buf: &[u8], signed_bytes: &[u8], public_key: &PublicKey) -> Result<Vec<u8>> {
    let mut reader = Reader::new(&signature_buf);
    let sig_type = reader.read_string().and_then(|v| KeyType::from_name(&v))?;
    match &public_key.kind {
         PublicKeyKind::Ecdsa(key) => {
            let sig_reader = reader.read_bytes()?;
            let mut reader = Reader::new(&sig_reader);
            let (alg, len) = match sig_type.name {
                "ecdsa-sha2-nistp256" => (&ECDSA_P256_SHA256_FIXED, 32),
                "ecdsa-sha2-nistp384" => (&ECDSA_P384_SHA384_FIXED, 48),
                _ => return Err(Error::KeyTypeMismatch),
            };
            // Read the R value
            let r_bytes = reader.read_mpint()?;
            // Read the S value
            let s_bytes = reader.read_mpint()?;
            // *_bytes are user controlled so ensure maliciously signatures
            // can't cause integer underflow.
            if r_bytes.len() > len || s_bytes.len() > len {
                return Err(Error::InvalidFormat);
            }
            // Determine and create the padding required
            let mut r = vec![0; len - r_bytes.len()];
            let mut s = vec![0; len - s_bytes.len()];
            // Pad *_bytes
            r.extend(r_bytes);
            s.extend(s_bytes);
            // Build a properly padded signature
            let mut sig = r;
            sig.extend(s);
            UnparsedPublicKey::new(alg, &key.key).verify(&signed_bytes, &sig)?;
            Ok(signature_buf.to_vec())
        },
        PublicKeyKind::Rsa(key) => {
            let alg = match sig_type.name {
                "rsa-sha2-256" => &RSA_PKCS1_2048_8192_SHA256,
                "rsa-sha2-512" => &RSA_PKCS1_2048_8192_SHA512,
                "ssh-rsa" => &RSA_PKCS1_2048_8192_SHA1_FOR_LEGACY_USE_ONLY,
                _ => return Err(Error::KeyTypeMismatch),
            };
            let signature = reader.read_bytes()?;
            let public_key = RsaPublicKeyComponents { n: &key.n, e: &key.e };
            public_key.verify(alg, &signed_bytes, &signature)?;
            Ok(signature_buf.to_vec())
        },
        PublicKeyKind::Ed25519(key) => {
            let alg = &ED25519;
            let signature = reader.read_bytes()?;
            let peer_public_key = UnparsedPublicKey::new(alg, &key.key);
            peer_public_key.verify(&signed_bytes, &signature)?;
            Ok(signature_buf.to_vec())
        },
    }
}
fn verify_signature ( signature_buf : & [ u8 ] , signed_bytes : & [ u8 ] , public_key : & PublicKey ) -> Result < Vec < u8 > > { let mut reader = Reader :: new ( & signature_buf ) ; let sig_type = reader . read_string ( ) . and_then ( | v | KeyType :: from_name ( & v ) ) ? ; match & public_key . kind { PublicKeyKind :: Ecdsa ( key ) => { let sig_reader = reader . read_bytes ( ) ? ; let mut reader = Reader :: new ( & sig_reader ) ; let ( alg , len ) = match sig_type . name { " " => ( & ECDSA_P256_SHA256_FIXED , 32 ) , " " => ( & ECDSA_P384_SHA384_FIXED , 48 ) , _ => return Err ( Error :: KeyTypeMismatch ) , } ; // Read the R value let r_bytes = reader . read_mpint ( ) ? ; // Read the S value let s_bytes = reader . read_mpint ( ) ? ; // *_bytes are user controlled so ensure maliciously signatures // can't cause integer underflow. if r_bytes . len ( ) > len || s_bytes . len ( ) > len { return Err ( Error :: InvalidFormat ) ; } // Determine and create the padding required let mut r = vec ! [ 0 len r_bytes len ( ) ] ; let mut s = vec ! [ 0 len s_bytes len ( ) ] ; // Pad *_bytes r . extend ( r_bytes ) ; s . extend ( s_bytes ) ; // Build a properly padded signature let mut sig = r ; sig . extend ( s ) ; UnparsedPublicKey :: new ( alg , & key . key ) . verify ( & signed_bytes , & sig ) ? ; Ok ( signature_buf . to_vec ( ) ) } , PublicKeyKind :: Rsa ( key ) => { let alg = match sig_type . name { " " => & RSA_PKCS1_2048_8192_SHA256 , " " => & RSA_PKCS1_2048_8192_SHA512 , " " => & RSA_PKCS1_2048_8192_SHA1_FOR_LEGACY_USE_ONLY , _ => return Err ( Error :: KeyTypeMismatch ) , } ; let signature = reader . read_bytes ( ) ? ; let public_key = RsaPublicKeyComponents { n : & key . n , e : & key . e } ; public_key . verify ( alg , & signed_bytes , & signature ) ? ; Ok ( signature_buf . to_vec ( ) ) } , PublicKeyKind :: Ed25519 ( key ) => { let alg = & ED25519 ; let signature = reader . read_bytes ( ) ? ; let peer_public_key = UnparsedPublicKey :: new ( alg , & key . key ) ; peer_public_key . verify ( & signed_bytes , & signature ) ? ; Ok ( signature_buf . to_vec ( ) ) } , } }
Verifies the certificate's signature is valid.
Verifies the certificate ' s signature is valid .
signature_buf
&[u8]
signed_bytes
&[u8]
public_key
&PublicKey