~apreiml/hare-ssh

bf27f211c2b768c912d7bdf052f9a23105c29828 — Armin Preiml 1 year, 7 months ago 6b0b478
format::ssh: factor out newkey, free keys on err
1 files changed, 21 insertions(+), 11 deletions(-)

M format/ssh/key.ha
M format/ssh/key.ha => format/ssh/key.ha +21 -11
@@ 57,15 57,24 @@ export fn decodeprivate(src: *sshprivkey) (*key | error) = {
	const keytype = readstr(&buf)?;
	defer free(keytype);

	let key: *key = switch (keytype) {
	let key = newkey(keytype)?;
	match (key.vtable.decodepriv(key, &buf)) {
	case void =>
		yield;
	case let e: error =>
		key_free(key);
		return e;
	};
	return key;
};

fn newkey(keytype: const str) (*key | error) = {
	switch (keytype) {
	case "ssh-ed25519" =>
		yield newed25519key();
		return newed25519key();
	case =>
		return badcipher;
	};

	key.vtable.decodepriv(key, &buf)?;
	return key;
};

// Decodes a public key in the SSH public key format.


@@ 73,13 82,14 @@ export fn decodepublic(src: io::handle) (*key | error) = {
	const keytype = readstr(src)?;
	defer free(keytype);

	let key: *key = switch (keytype) {
	case "ssh-ed25519" =>
		yield newed25519key();
	case =>
		return badcipher;
	let key = newkey(keytype)?;
	match (key.vtable.decodepub(key, src)) {
	case void =>
		yield;
	case let e: error =>
		key_free(key);
		return e;
	};
	key.vtable.decodepub(key, src)?;
	return key;
};