Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
git-server: Move function where it's used
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Sellier <[email protected]>
  • Loading branch information
cloudhead committed Feb 13, 2022
1 parent 0d5f19d commit ce93b4b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
21 changes: 20 additions & 1 deletion git-server/src/hooks/pre_receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
//!
//! The `pre-receive` git hook provides access to GPG certificates for a signed push, useful for authorizing an
//! update the repository.
use std::io;
use std::io::prelude::*;
use std::io::stdin;
use std::str::FromStr;

use envconfig::Envconfig;
use git2::{Oid, Repository};
use librad::PeerId;

use super::{
types::{CertNonceStatus, CertStatus, ReceivePackEnv},
Expand Down Expand Up @@ -122,7 +124,7 @@ impl PreReceive {
// key fingerpint.
let (peer_id, _) = crate::parse_ref(refname)
.map_err(|_| Error::InvalidRefPushed(refname.to_owned()))?;
let peer_fingerprint = crate::to_ssh_fingerprint(&peer_id)?;
let peer_fingerprint = to_ssh_fingerprint(&peer_id)?;

if key_fingerprint[..] != peer_fingerprint[..] {
return Err(Error::Unauthorized("signer does not match remote ref"));
Expand Down Expand Up @@ -184,3 +186,20 @@ impl PreReceive {
Err(Error::Unauthorized("key is not authorized to push"))
}
}

/// Get the SSH key fingerprint from a peer id.
fn to_ssh_fingerprint(peer_id: &PeerId) -> Result<Vec<u8>, io::Error> {
use byteorder::{BigEndian, WriteBytesExt};
use sha2::Digest;

let mut buf = Vec::new();
let name = b"ssh-ed25519";
let key = peer_id.as_public_key().as_ref();

buf.write_u32::<BigEndian>(name.len() as u32)?;
buf.extend_from_slice(name);
buf.write_u32::<BigEndian>(key.len() as u32)?;
buf.extend_from_slice(key);

Ok(sha2::Sha256::digest(&buf).to_vec())
}
17 changes: 0 additions & 17 deletions git-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,23 +595,6 @@ fn gen_random_string() -> String {
out
}

/// Get the SSH key fingerprint from a peer id.
fn to_ssh_fingerprint(peer_id: &PeerId) -> Result<Vec<u8>, io::Error> {
use byteorder::{BigEndian, WriteBytesExt};
use sha2::Digest;

let mut buf = Vec::new();
let name = b"ssh-ed25519";
let key = peer_id.as_public_key().as_ref();

buf.write_u32::<BigEndian>(name.len() as u32)?;
buf.extend_from_slice(name);
buf.write_u32::<BigEndian>(key.len() as u32)?;
buf.extend_from_slice(key);

Ok(sha2::Sha256::digest(&buf).to_vec())
}

/// Parse a remote git ref into a peer id and return the remaining input.
///
/// Eg. `refs/remotes/<peer>/heads/master`
Expand Down

0 comments on commit ce93b4b

Please sign in to comment.