265 lines
9.3 KiB
Ziran
265 lines
9.3 KiB
Ziran
// Legacy daochi-* protocol constants below are released bytes; keep them unchanged.
|
|
// Signed app releases, format "daochi-zib-release-v2". A release names an
|
|
// app, its sequence and version, the runtime and host APIs it needs, the
|
|
// artifacts that deliver it and the packages it depends on, all signed with
|
|
// Ed25519 by the key the publisher registered for the app.
|
|
//
|
|
// MeshParseRelease checks the release's shape and signature; the caller
|
|
// checks its own policy: the app it expects, a runtime and API range it
|
|
// supports, and which dependencies it accepts. The signature check is a
|
|
// callback, so applications keep their own Ed25519 implementation.
|
|
#import "json_scan"
|
|
#import "byte_text_linux"
|
|
#import "vec"
|
|
#import "text"
|
|
#import "mesh_client/crypto"
|
|
|
|
release_c :: #system_library "c";
|
|
ReleaseFree :: (data: *u8) #foreign release_c "free";
|
|
|
|
MESH_RELEASE_ARTIFACTS :: 2
|
|
MESH_RELEASE_DEPENDENCIES :: 16
|
|
MESH_RELEASE_JSON_LIMIT :: 65536
|
|
|
|
MeshReleaseArtifact :: struct {
|
|
variant: [32]u8
|
|
sha256: [65]u8
|
|
size: s64
|
|
}
|
|
|
|
MeshReleaseDependency :: struct {
|
|
app_id: [64]u8
|
|
version: [32]u8
|
|
sha256: [65]u8
|
|
size: s64
|
|
}
|
|
|
|
MeshRelease :: struct {
|
|
app_id: [64]u8
|
|
sequence: s64
|
|
version: [32]u8
|
|
runtime: [32]u8
|
|
host_api: s64
|
|
module_api: s64
|
|
data_schema: s64
|
|
key_id: [64]u8
|
|
// The first artifact is "module", an optional second "standalone".
|
|
artifacts: [MESH_RELEASE_ARTIFACTS]MeshReleaseArtifact
|
|
artifact_count: s32
|
|
// Sorted by app id, each at most once.
|
|
dependencies: [MESH_RELEASE_DEPENDENCIES]MeshReleaseDependency
|
|
dependency_count: s32
|
|
valid: bool
|
|
}
|
|
|
|
// True when signature (64 bytes) signs message under public_key (32 bytes).
|
|
MeshReleaseVerify :: #type (signature: *u8, public_key: *u8, message: *u8, length: usize) -> bool;
|
|
|
|
// A dotted version of three numbers without leading zeros, such as 2.1.28.
|
|
MeshVersionValid :: (version: string) -> bool {
|
|
parts: s32 = 0
|
|
digits: s32 = 0
|
|
first: u8 = 0
|
|
for byte: version {
|
|
if byte == #char "." {
|
|
if digits == 0 {
|
|
return false
|
|
}
|
|
parts += 1
|
|
digits = 0
|
|
} else {
|
|
if byte < #char "0" || byte > #char "9" || digits >= 9 || (digits > 0 && first == #char "0") {
|
|
return false
|
|
}
|
|
if digits == 0 {
|
|
first = byte
|
|
}
|
|
digits += 1
|
|
}
|
|
}
|
|
return parts == 2 && digits > 0
|
|
}
|
|
|
|
// Lowercase letters and digits, then also ".", "_" and "-"; at most 63 bytes.
|
|
MeshIdentifierValid :: (value: string) -> bool {
|
|
if value.count == 0 || value.count > 63 || value == "." || value == ".." {
|
|
return false
|
|
}
|
|
for index: 0..value.count - 1 {
|
|
byte := value[index]
|
|
alphanumeric := (byte >= #char "a" && byte <= #char "z") || (byte >= #char "0" && byte <= #char "9")
|
|
if !alphanumeric && (index == 0 || (byte != #char "." && byte != #char "_" && byte != #char "-")) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// 64 lowercase hex digits.
|
|
MeshHashValid :: (hash: string) -> bool {
|
|
if hash.count != 64 {
|
|
return false
|
|
}
|
|
for byte: hash {
|
|
if !((byte >= #char "0" && byte <= #char "9") || (byte >= #char "a" && byte <= #char "f")) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Byte order, shorter first.
|
|
MeshCompare :: (left: string, right: string) -> s32 {
|
|
at: s64 = 0
|
|
while at < left.count && at < right.count {
|
|
if left[at] < right[at] {
|
|
return -1
|
|
}
|
|
if left[at] > right[at] {
|
|
return 1
|
|
}
|
|
at += 1
|
|
}
|
|
return ifx left.count < right.count then -1 else ifx left.count > right.count then 1 else 0
|
|
}
|
|
|
|
// Whether bytes hash to sha256, 64 lowercase hex digits.
|
|
MeshSha256Matches :: (bytes: string, sha256: string) -> bool {
|
|
if !MeshHashValid(sha256) {
|
|
return false
|
|
}
|
|
actual: [65]u8
|
|
data: *u8 = null
|
|
if bytes.count > 0 {
|
|
descriptor := cast(*TextDescriptor)(*bytes)
|
|
data = descriptor.data
|
|
}
|
|
Sha256Hex(data, bytes.count, *actual[0])
|
|
return TextFromCString(*actual[0]) == sha256
|
|
}
|
|
|
|
ReleaseField :: (json: string, at: s32, key: string, output: []u8) -> string {
|
|
count := ReadStringText(json, FieldAt(json, at, key), output)
|
|
if count <= 0 || count >= output.count {
|
|
return ""
|
|
}
|
|
output[count] = 0
|
|
return TextFromBytes(output[0:count])
|
|
}
|
|
|
|
ReleaseNumber :: (json: string, at: s32, key: string) -> s64 {
|
|
number := ReadNumber(json, FieldAt(json, at, key))
|
|
if !number.valid || number.value < 0.0 || number.value > 2147483647.0 {
|
|
return -1
|
|
}
|
|
integer := cast(s64)number.value
|
|
if cast(float64)integer != number.value {
|
|
return -1
|
|
}
|
|
return integer
|
|
}
|
|
|
|
ReleaseCopy :: (value: string, output: []u8) {
|
|
for index: cast(s64)0..value.count - 1 {
|
|
output[index] = value[index]
|
|
}
|
|
output[value.count] = 0
|
|
}
|
|
|
|
// Parses json and checks its signature under public_hex, the app key's 64
|
|
// hex digits. Each artifact and dependency is at least 8 bytes and at most
|
|
// maximum_size. The result is valid only when everything checks out.
|
|
MeshParseRelease :: (json: string, public_hex: string, maximum_size: s64,
|
|
verify: MeshReleaseVerify) -> MeshRelease {
|
|
release: MeshRelease
|
|
if json.count > MESH_RELEASE_JSON_LIMIT || json.count < 2 || json[0] != #char "{" ||
|
|
Space(json, ValueEnd(json, 0, 0)) != json.count || public_hex.count != 64 || verify == null {
|
|
return release
|
|
}
|
|
app: [64]u8
|
|
version: [32]u8
|
|
runtime: [32]u8
|
|
key: [64]u8
|
|
signature: [129]u8
|
|
app_id := ReleaseField(json, 0, "app_id", app[:])
|
|
version_text := ReleaseField(json, 0, "version", version[:])
|
|
runtime_text := ReleaseField(json, 0, "runtime", runtime[:])
|
|
key_id := ReleaseField(json, 0, "key_id", key[:])
|
|
signed := ReleaseField(json, 0, "signature", signature[:])
|
|
sequence := ReleaseNumber(json, 0, "sequence")
|
|
host := ReleaseNumber(json, 0, "host_api")
|
|
module := ReleaseNumber(json, 0, "module_api")
|
|
schema := ReleaseNumber(json, 0, "data_schema")
|
|
if !MeshIdentifierValid(app_id) || !MeshVersionValid(version_text) || runtime_text.count == 0 ||
|
|
!MeshIdentifierValid(key_id) || sequence <= 0 || host < 0 || module < 0 || schema < 0 ||
|
|
signed.count != 128 {
|
|
return release
|
|
}
|
|
artifacts := FieldAt(json, 0, "artifacts")
|
|
artifact_count := ArrayCount(json, artifacts)
|
|
dependencies := FieldAt(json, 0, "dependencies")
|
|
dependency_count := ArrayCount(json, dependencies)
|
|
if artifact_count < 1 || artifact_count > MESH_RELEASE_ARTIFACTS ||
|
|
dependency_count < 0 || dependency_count > MESH_RELEASE_DEPENDENCIES {
|
|
return release
|
|
}
|
|
message: Vec(u8)
|
|
BuilderPrint(*message, "daochi-zib-release-v2\n%\n%\n%\n%\n%\n%\n%\n%\n%\n",
|
|
app_id, sequence, version_text, runtime_text, host, module, schema, key_id, artifact_count)
|
|
for artifact: cast(s32)0..artifact_count - 1 {
|
|
at := ElementAt(json, artifacts, artifact)
|
|
entry := *release.artifacts[artifact]
|
|
variant := ReleaseField(json, at, "variant", entry.variant[:])
|
|
hash := ReleaseField(json, at, "sha256", entry.sha256[:])
|
|
size := ReleaseNumber(json, at, "size")
|
|
expected := ifx artifact == 0 then "module" else "standalone"
|
|
if variant != expected || !MeshHashValid(hash) || size < 8 || size > maximum_size {
|
|
VecFree(message)
|
|
return MeshRelease.{}
|
|
}
|
|
entry.size = size
|
|
BuilderPrint(*message, "%\n%\n%\n", variant, hash, size)
|
|
}
|
|
BuilderPrint(*message, "%\n", dependency_count)
|
|
for dependency: cast(s32)0..dependency_count - 1 {
|
|
at := ElementAt(json, dependencies, dependency)
|
|
entry := *release.dependencies[dependency]
|
|
id := ReleaseField(json, at, "app_id", entry.app_id[:])
|
|
dependency_version := ReleaseField(json, at, "version", entry.version[:])
|
|
hash := ReleaseField(json, at, "sha256", entry.sha256[:])
|
|
size := ReleaseNumber(json, at, "size")
|
|
previous := ifx dependency > 0 then TextFromCString(*release.dependencies[dependency - 1].app_id[0]) else ""
|
|
if !MeshIdentifierValid(id) || id == app_id || !MeshVersionValid(dependency_version) ||
|
|
!MeshHashValid(hash) || size < 8 || size > maximum_size ||
|
|
(dependency > 0 && MeshCompare(previous, id) >= 0) {
|
|
VecFree(message)
|
|
return MeshRelease.{}
|
|
}
|
|
entry.size = size
|
|
BuilderPrint(*message, "%\n%\n%\n%\n", id, dependency_version, hash, size)
|
|
}
|
|
text := BuilderFinish(message)
|
|
descriptor := cast(*TextDescriptor)(*text)
|
|
public_key: [32]u8
|
|
signature_bytes: [64]u8
|
|
verified := HexToBytes(public_hex, *public_key[0], 32) != 0 &&
|
|
HexToBytes(signed, *signature_bytes[0], 64) != 0 &&
|
|
verify(*signature_bytes[0], *public_key[0], descriptor.data, cast(usize)text.count)
|
|
ReleaseFree(descriptor.data)
|
|
if !verified {
|
|
return MeshRelease.{}
|
|
}
|
|
ReleaseCopy(app_id, release.app_id[:])
|
|
ReleaseCopy(version_text, release.version[:])
|
|
ReleaseCopy(runtime_text, release.runtime[:])
|
|
ReleaseCopy(key_id, release.key_id[:])
|
|
release.sequence = sequence
|
|
release.host_api = host
|
|
release.module_api = module
|
|
release.data_schema = schema
|
|
release.artifact_count = artifact_count
|
|
release.dependency_count = dependency_count
|
|
release.valid = true
|
|
return release
|
|
}
|