Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust asps #17

Merged
merged 11 commits into from
Oct 23, 2024
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
bin
bin
# in Rust projects, <project>/src/bin/ holds source code
!**/src/bin

# in Rust projects, this directory contains only derived files.
target

# in Rust projects, lock files
Cargo.lock
3 changes: 3 additions & 0 deletions attestation_asps/python_asps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.spec
build/
dist/
20 changes: 20 additions & 0 deletions attestation_asps/python_asps/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Expects that the Python package, pyinstaller, has been installed,
# and is in the search path.
# To install:
# python3 -m pip install pyinstaller

SRCDIR := ./
BINDIR := dist/
DEST_BINDIR := ../bin/

SOURCE := p_hashfile_id.py
AUX_FILES := copland.py
EXE_NAME := $(patsubst %.py,%, $(SOURCE))

all: $(SOURCE) $(AUX_FILES)
pyinstaller $(SOURCE) --onefile
cp $(BINDIR)/$(EXE_NAME) $(DEST_BINDIR)

clean:
rm -rf $(BINDIR)
rm -rf build/
71 changes: 71 additions & 0 deletions attestation_asps/python_asps/TEMPLATE.py.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
## TEMPLATE.txt
## General structure for ASP's written in rust

import sys
import json
import hashlib
import binascii
import base64

import copland

## function where the work of the ASP is performed.
## May raise exception(s) which will be handled in main.

def body():

# For every ASP, an ASPRunRequest appears as the single command-line argument
numargs = len(sys.argv)
if (numargs == 1):
raise Exception("no ASPRunRequest provided to p_hashfile_id")
json_req = sys.argv[1]
request = json.loads(json_req, object_hook=copland.ASPRunRequest.from_json)

# Code for specific for this ASP.
# This example computes the HASH of the file named in an argument for the ASP.
# May raise an exception, which will be captured in main.

asp_args = request.ASP_ARGS
filename = asp_args['filepath']

with open(filename,"rb") as f:
bytes = f.read()

hash_string = hashlib.sha256(bytes).hexdigest()
# evidence as bytes
hash_bytes = hash_string.encode()

# End of code specific for this ASP.

# Common code to bundle computed value.
# Step 1:
# The return value for an ASP, must be
# encoded in BASE64, and converted to ascii for JSON transmission
hash_b64 = base64.b64encode(hash_bytes).decode('ascii')

# Step 2:
# wrap the value as Evidence
evidence = copland.RAWEV([hash_b64])

# Step 3:
# Construct the ASPRunResponse with this evidence.
response = copland.successfulASPRunResponse(evidence)
response_json = json.dumps(response, default=lambda o: o.__dict__)
return response_json

# Main simply invokes the body() function above,
# and checks for exceptions.
# If it detects an exception, this ASP will return
# an ASPRunResponse with SUCCESS = false, o/w uses
# ASPRunResponse returned from body()

if __name__ == "__main__":
try:
response_json = body()
except BaseException as e:
response = copland.failureASPRunResponse(str(e))
response_json = json.dumps(response, default=lambda o: o.__dict__)
finally:
# The ASP output (ASPRunRequest) is written to stdout.
# The caller will capture stdout to receive the response from this ASP.
print(response_json)
110 changes: 110 additions & 0 deletions attestation_asps/python_asps/copland.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
class ASP_PARAMS:
def __init__(self, asp_id, asp_args, plc, targ_id):
self.ASP_ID = asp_id
self.ASP_ARGS = asp_args
self.ASP_PLC = asp
self.ASP_TARG_ID = asp_targ_id

class Attestation_Session:
def __init__(self, session_plc, plc_mapping, pubkey_mapping):
self.Session_Plc = session_plc
self.Plc_Mapping = plc_mapping
self.PubKey_Mapping = pubkey_mapping

class ProtocolRunRequest:
def __init__(self, type, action, req_plc, term, rawev, attestation_session):
self.TYPE = type
self.ACTION = action
self.REQ_PLC = req_plc
self.TERM = term
self.RAWEV = rawev
self.ATTESTATION_SESSION = attestation_session

class ProtocolRunResponse:
def __init__(self, type, action, success, payload):
self.TYPE = type
self.ACTION = action
self.SUCCESS = success,
self.PAYLOAD = payload

class ProtocolAppraiseRequest:
def __init__(self, type, action, attestation_session, term, req_plc, evidence, rawev):
self.TYPE = type,
self.ACTION = action,
self.ATTESTATION_SESSION = attestation_session,
self.TERM = term,
self.REQ_PLC = req_plc,
self.EVIDENCE = evidence,
self.RAWEV: rawev

class ProtocolAppraiseResponse:
def __init__(self, type, action, success, payload):
self.TYPE = type
self.ACTION = action
self.SUCCESS = success
self.PAYLOAD = payload

class RAWEV:
def __init__(self, rawev):
self.RawEv = rawev

@staticmethod
def from_json(dct):
keys = dct.keys()
if len(keys) == 1 and 'RawEv' in keys:
return RAWEV(dct['RawEv'])
else:
return dct

class ASPRunRequest:
def __init__(self, type, action, asp_id, asp_args, asp_plc, asp_targ_id, rawev):
self.TYPE = type
self.ACTION = action
self.ASP_ID = asp_id
self.ASP_ARGS = asp_args
self.ASP_PLC = asp_plc
self.ASP_TARG_ID = asp_targ_id
self.RAWEV = rawev

@staticmethod
def from_json(dct):
keys = dct.keys()
if 'TYPE' in keys and 'ACTION' in keys and 'ASP_ID' in keys and 'ASP_ARGS' in keys and 'ASP_PLC' in keys and 'ASP_TARG_ID' in keys and 'RAWEV' in keys:
return ASPRunRequest(dct['TYPE'], dct['ACTION'], dct['ASP_ID'], dct['ASP_ARGS'], dct['ASP_PLC'], dct['ASP_TARG_ID'], dct['RAWEV'])
elif len(keys) == 1 and 'RawEv' in keys:
return RAWEV.from_json(dct)
else:
return dct

class ASPRunResponse:
def __init__(self, type, action, success, payload):
self.TYPE = type
self.ACTION = action
self.SUCCESS = success
self.PAYLOAD = payload

@staticmethod
def from_json(dct):
keys = dct.keys()
if 'TYPE' in keys and 'ACTION' in keys and 'SUCCESS' in keys and 'PAYLOAD' in keys:
return ASPRunResponse(dct['TYPE'], dct['ACTION'], dct['SUCCESS'], dct['PAYLOAD'])
elif len(keys) == 1 and 'RawEv' in keys:
return RAWEV.from_json(dct)
else:
return dct


def failureASPRunResponse (error_msg):
empty_evidence = RAWEV([])
response = ASPRunResponse("RESPONSE",
"ASP_RUN",
False,
empty_evidence)
return response

def successfulASPRunResponse (evidence):
response = ASPRunResponse("RESPONSE",
"ASP_RUN",
True,
evidence)
return response
51 changes: 51 additions & 0 deletions attestation_asps/python_asps/p_hashfile_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
## TEMPLATE.txt
## General structure for ASP's written in rust

import sys
import json
import hashlib
import binascii
import base64

import copland

## ASP to compute the hash of the filenmae provided in the 'filepath' argument.

def body():

# For every ASP, an ASPRunRequest appears as the single command-line argument
numargs = len(sys.argv)
if (numargs == 1):
raise Exception("no ASPRunRequest provided to p_hashfile_id")
json_req = sys.argv[1]
request = json.loads(json_req, object_hook=copland.ASPRunRequest.from_json)


asp_args = request.ASP_ARGS
filename = asp_args['filepath']

with open(filename,"rb") as f:
bytes = f.read()

hash_string = hashlib.sha256(bytes).hexdigest()
# evidence as bytes
hash_bytes = hash_string.encode()
hash_b64 = base64.b64encode(hash_bytes).decode('ascii')

evidence = copland.RAWEV([hash_b64])

response = copland.successfulASPRunResponse(evidence)
response_json = json.dumps(response, default=lambda o: o.__dict__)
return response_json


if __name__ == "__main__":
try:
response_json = body()
except BaseException as e:
response = copland.failureASPRunResponse(str(e))
response_json = json.dumps(response, default=lambda o: o.__dict__)
finally:
# The ASP output (ASPRunRequest) is written to stdout.
# The caller will capture stdout to receive the response from this ASP.
print(response_json)
24 changes: 24 additions & 0 deletions attestation_asps/rust_asps/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "rust_asps"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "r_hashfile_id"
path = "src/bin/r_hashfile_id.rs"
test = false
bench = false

[[bin]]
name = "r_uptime_id"
path = "src/bin/r_uptime_id.rs"
test = false
bench = false

[dependencies]
rust_am_lib = { git = "https://github.com/ku-sldg/rust-am-lib.git", veersion = "0.1.1"}
serde_json = "1.0.125"
anyhow = "1.0.86"
base64 = "0.13"
sha2 = "0.10.8"
sysinfo = "0.31.3"
15 changes: 15 additions & 0 deletions attestation_asps/rust_asps/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SRCDIR := src/bin/
BINDIR := target/debug/
DEST_BINDIR := ../bin/

SOURCES := $(wildcard $(SRCDIR)*.rs)
BASE_NAMES := $(patsubst src/bin/%,%, $(patsubst %.rs,%, $(SOURCES)))

all:
for asp in $(BASE_NAMES); do \
cargo build --bin $$asp; \
cp $(BINDIR)/$$asp $(DEST_BINDIR); \
done

clean:
cargo clean
Loading