mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 10:38:33 -04:00
refactor(ssh): Cleaned up.
This commit is contained in:
2
TODOS.md
2
TODOS.md
@@ -56,6 +56,8 @@
|
|||||||
|
|
||||||
28. Pass allocator to findr?
|
28. Pass allocator to findr?
|
||||||
|
|
||||||
|
29. Update `read_wire_string` to use a slice.
|
||||||
|
|
||||||
## Double-check AI output
|
## Double-check AI output
|
||||||
|
|
||||||
- [ ] cli.odin
|
- [ ] cli.odin
|
||||||
|
|||||||
56
ssh.odin
56
ssh.odin
@@ -2,7 +2,9 @@ package main
|
|||||||
|
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
import "core:encoding/base64"
|
import "core:encoding/base64"
|
||||||
|
import "core:encoding/endian"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
|
import "core:mem"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
|
|
||||||
@@ -44,9 +46,7 @@ parse_ssh_public_key :: proc(pub_path: string) -> (pub: [32]u8, ok: bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for i in 0 ..< 32 {
|
mem.copy_non_overlapping(&pub[0], raw_data(pk_data), 32)
|
||||||
pub[i] = pk_data[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
ok = true
|
ok = true
|
||||||
return
|
return
|
||||||
@@ -86,15 +86,10 @@ parse_ssh_private_key :: proc(priv_path: string) -> (kp: Ed25519Keypair, ok: boo
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
magic := "openssh-key-v1\x00"
|
magic :: "openssh-key-v1\x00"
|
||||||
if len(decoded) < len(magic) {
|
if !strings.has_prefix(string(decoded), magic) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for i in 0 ..< len(magic) {
|
|
||||||
if decoded[i] != u8(magic[i]) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
offset := len(magic)
|
offset := len(magic)
|
||||||
|
|
||||||
@@ -116,11 +111,8 @@ parse_ssh_private_key :: proc(priv_path: string) -> (kp: Ed25519Keypair, ok: boo
|
|||||||
if offset + 4 > len(decoded) {
|
if offset + 4 > len(decoded) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
num_keys :=
|
|
||||||
u32(decoded[offset]) << 24 |
|
num_keys := endian.get_u32(decoded[offset:offset + 4], .Big) or_return
|
||||||
u32(decoded[offset + 1]) << 16 |
|
|
||||||
u32(decoded[offset + 2]) << 8 |
|
|
||||||
u32(decoded[offset + 3])
|
|
||||||
offset += 4
|
offset += 4
|
||||||
|
|
||||||
if num_keys != 1 {
|
if num_keys != 1 {
|
||||||
@@ -141,17 +133,16 @@ parse_ssh_private_key :: proc(priv_path: string) -> (kp: Ed25519Keypair, ok: boo
|
|||||||
if inner_offset + 8 > len(priv_blob) {
|
if inner_offset + 8 > len(priv_blob) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
check1 :=
|
|
||||||
u32(priv_blob[inner_offset]) << 24 |
|
check1 := endian.get_u32(
|
||||||
u32(priv_blob[inner_offset + 1]) << 16 |
|
transmute([]u8)(priv_blob)[inner_offset:inner_offset + 4],
|
||||||
u32(priv_blob[inner_offset + 2]) << 8 |
|
.Big,
|
||||||
u32(priv_blob[inner_offset + 3])
|
) or_return
|
||||||
inner_offset += 4
|
inner_offset += 4
|
||||||
check2 :=
|
check2 := endian.get_u32(
|
||||||
u32(priv_blob[inner_offset]) << 24 |
|
transmute([]u8)(priv_blob)[inner_offset:inner_offset + 4],
|
||||||
u32(priv_blob[inner_offset + 1]) << 16 |
|
.Big,
|
||||||
u32(priv_blob[inner_offset + 2]) << 8 |
|
) or_return
|
||||||
u32(priv_blob[inner_offset + 3])
|
|
||||||
inner_offset += 4
|
inner_offset += 4
|
||||||
|
|
||||||
if check1 != check2 {
|
if check1 != check2 {
|
||||||
@@ -167,17 +158,14 @@ parse_ssh_private_key :: proc(priv_path: string) -> (kp: Ed25519Keypair, ok: boo
|
|||||||
if !pub_ok || len(pub_wire) != 32 {
|
if !pub_ok || len(pub_wire) != 32 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for i in 0 ..< 32 {
|
mem.copy_non_overlapping(&kp.Public[0], raw_data(pub_wire), 32)
|
||||||
kp.Public[i] = pub_wire[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
priv_wire, priv_ok := read_wire_string(transmute([]u8)priv_blob, &inner_offset)
|
priv_wire, priv_ok := read_wire_string(transmute([]u8)priv_blob, &inner_offset)
|
||||||
if !priv_ok || len(priv_wire) != 64 {
|
if !priv_ok || len(priv_wire) != 64 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for i in 0 ..< 32 {
|
|
||||||
kp.Private[i] = priv_wire[i]
|
mem.copy_non_overlapping(&kp.Private[0], raw_data(priv_wire), 32)
|
||||||
}
|
|
||||||
|
|
||||||
ok = true
|
ok = true
|
||||||
return
|
return
|
||||||
@@ -198,11 +186,7 @@ read_wire_string :: proc(data: []u8, offset: ^int) -> (s: string, ok: bool) {
|
|||||||
if offset^ + 4 > len(data) {
|
if offset^ + 4 > len(data) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
length :=
|
length := endian.get_u32(data[offset^:offset^ + 4], .Big) or_return
|
||||||
u32(data[offset^]) << 24 |
|
|
||||||
u32(data[offset^ + 1]) << 16 |
|
|
||||||
u32(data[offset^ + 2]) << 8 |
|
|
||||||
u32(data[offset^ + 3])
|
|
||||||
offset^ += 4
|
offset^ += 4
|
||||||
|
|
||||||
if offset^ + int(length) > len(data) {
|
if offset^ + int(length) > len(data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user