173 lines
12 KiB
Python
173 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
"""Native expired-client TLS rejection, with an unregistered positive control.
|
|
|
|
Fixture key stays on the Cloud VM, signing CA/key stay on the Cloud server.
|
|
No enrollment, device registration, job, clock adjustment or verification bypass.
|
|
"""
|
|
import argparse
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import uuid
|
|
|
|
import cloud_lifecycle_acceptance as lifecycle
|
|
|
|
cloud = lifecycle.cloud
|
|
require = cloud.require
|
|
STATE = lifecycle.STATE
|
|
|
|
|
|
def prepare_vm(payload):
|
|
os.umask(0o077)
|
|
ident = str(uuid.UUID(payload['id']))
|
|
parent = Path('/var/lib/rmm-agent-remote/lifecycle')
|
|
require(not parent.is_symlink() and parent.is_dir() and parent.stat().st_uid == 993 and parent.stat().st_mode & 0o777 == 0o700, 'existing private lifecycle parent required')
|
|
root = parent/('expiry-'+ident);root.mkdir(mode=0o700);os.chown(root,993,984)
|
|
def run(args): return subprocess.run(args,capture_output=True,check=True,timeout=10).stdout
|
|
run(['/usr/bin/openssl','genpkey','-algorithm','EC','-pkeyopt','ec_paramgen_curve:P-256','-out',str(root/'fixture.key')])
|
|
run(['/usr/bin/openssl','req','-new','-sha256','-key',str(root/'fixture.key'),'-out',str(root/'fixture.csr'),'-subj','/CN='+ident])
|
|
for name in ('fixture.key','fixture.csr'):os.chown(root/name,993,984);(root/name).chmod(0o600)
|
|
permanent = Path('/var/lib/rmm-agent-remote')
|
|
witnesses = {}
|
|
for name in ('identity.json','certificates/current.json'):
|
|
p=permanent/name;s=p.stat();witnesses[name]=dict(sha256=hashlib.sha256(p.read_bytes()).hexdigest(),device=s.st_dev,inode=s.st_ino)
|
|
return dict(id=ident,csr=base64.b64encode((root/'fixture.csr').read_bytes()).decode(),
|
|
permanent_witnesses=witnesses,fixture_only=True,enrollment_created=False)
|
|
|
|
|
|
def sign_server(payload):
|
|
import datetime as dt
|
|
os.umask(0o077)
|
|
ident=str(uuid.UUID(payload['id']));root=Path('/opt/rust-discovery/devstate')
|
|
ca=root/'pki/ca.pem';key=root/'pki/ca.key'
|
|
require(all(not p.is_symlink() and p.is_file() for p in (ca,key)) and key.stat().st_mode & 0o077 == 0, 'existing private signing CA required')
|
|
key_stat=key.stat();ca_hash=hashlib.sha256(ca.read_bytes()).hexdigest()
|
|
parent=root/'lifecycle-expiry'
|
|
if not parent.exists():parent.mkdir(mode=0o700)
|
|
require(not parent.is_symlink() and parent.stat().st_uid == 0 and parent.stat().st_mode & 0o777 == 0o700, 'private server fixture parent')
|
|
folder=parent/ident;folder.mkdir(mode=0o700)
|
|
def create(path,raw):
|
|
fd=os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
|
|
with os.fdopen(fd,'wb') as f:f.write(raw);f.flush();os.fsync(f.fileno())
|
|
def run(args,check=True):
|
|
return subprocess.run(args,capture_output=True,check=check,timeout=10,env=dict(PATH='/usr/sbin:/usr/bin:/sbin:/bin',LC_ALL='C'))
|
|
csr=base64.b64decode(payload['csr'],validate=True);require(len(csr)<8192,'bounded public CSR')
|
|
create(folder/'fixture.csr',csr)
|
|
run(['/usr/bin/openssl','req','-in',str(folder/'fixture.csr'),'-verify','-noout'])
|
|
subject=run(['/usr/bin/openssl','req','-in',str(folder/'fixture.csr'),'-noout','-subject','-nameopt','RFC2253']).stdout.strip()
|
|
require(subject==('subject=CN='+ident).encode(),'exact unregistered fixture subject')
|
|
dates=run(['/usr/bin/openssl','x509','-in',str(ca),'-noout','-startdate','-enddate']).stdout.decode().splitlines()
|
|
now=dt.datetime.now(dt.timezone.utc)
|
|
ca_before=dt.datetime.fromtimestamp(ssl.cert_time_to_seconds(dates[0].split('=',1)[1]),dt.timezone.utc)
|
|
ca_after=dt.datetime.fromtimestamp(ssl.cert_time_to_seconds(dates[1].split('=',1)[1]),dt.timezone.utc)
|
|
before=max(ca_before+dt.timedelta(seconds=1),now-dt.timedelta(minutes=10))
|
|
expired=now-dt.timedelta(minutes=1);valid=now+dt.timedelta(minutes=15)
|
|
require(before<expired<now<valid<ca_after,'fixture validity must lie within actual issuer validity')
|
|
create(folder/'index',b'');create(folder/'serial',(uuid.uuid4().hex+'\n').encode())
|
|
config=('[ca]\ndefault_ca=fixture\n[fixture]\ndatabase='+str(folder/'index')+'\nserial='+str(folder/'serial')+
|
|
'\nnew_certs_dir='+str(folder)+'\ncertificate='+str(ca)+'\nprivate_key='+str(key)+
|
|
'\ndefault_md=sha256\npolicy=subject\nunique_subject=no\nx509_extensions=client\n'
|
|
'[subject]\ncommonName=supplied\n[client]\nbasicConstraints=critical,CA:FALSE\n'
|
|
'keyUsage=critical,digitalSignature\nextendedKeyUsage=clientAuth\n')
|
|
create(folder/'ca.cnf',config.encode())
|
|
certificates={};common=[]
|
|
for name,end in (('expired',expired),('valid',valid)):
|
|
cert=folder/(name+'.pem')
|
|
run(['/usr/bin/openssl','ca','-batch','-notext','-config',str(folder/'ca.cnf'),'-startdate',before.strftime('%Y%m%d%H%M%SZ'),
|
|
'-enddate',end.strftime('%Y%m%d%H%M%SZ'),'-in',str(folder/'fixture.csr'),'-out',str(cert)])
|
|
cert.chmod(0o600);certificates[name]=base64.b64encode(cert.read_bytes()).decode()
|
|
common.append(run(['/usr/bin/openssl','x509','-in',str(cert),'-noout','-subject','-issuer','-pubkey','-ext','basicConstraints,keyUsage,extendedKeyUsage']).stdout)
|
|
require(common[0]==common[1],'same issuer/subject/publickey/client constraints required')
|
|
positive=run(['/usr/bin/openssl','verify','-purpose','sslclient','-CAfile',str(ca),str(folder/'valid.pem')])
|
|
negative=run(['/usr/bin/openssl','verify','-purpose','sslclient','-CAfile',str(ca),str(folder/'expired.pem')],check=False)
|
|
require(positive.returncode==0 and negative.returncode!=0 and b'error 10 at 0 depth lookup: certificate has expired' in negative.stderr,'real expired leaf verification required')
|
|
after=key.stat()
|
|
require((key_stat.st_dev,key_stat.st_ino,key_stat.st_size,key_stat.st_mtime_ns)==(after.st_dev,after.st_ino,after.st_size,after.st_mtime_ns)
|
|
and hashlib.sha256(ca.read_bytes()).hexdigest()==ca_hash,'existing CA and key unchanged')
|
|
return dict(id=ident,certificates=certificates,ca=base64.b64encode(ca.read_bytes()).decode(),
|
|
issuer_ca_sha256=ca_hash,common_profile_sha256=hashlib.sha256(common[0]).hexdigest(),
|
|
not_before=before.isoformat(),expired_not_after=expired.isoformat(),valid_not_after=valid.isoformat(),
|
|
expired_verify_code=10,ca_unchanged=True,clock_adjusted=False,enrollment_created=False)
|
|
|
|
|
|
def probe_vm(payload):
|
|
import http.client
|
|
os.umask(0o077)
|
|
ident=str(uuid.UUID(payload['id']));root=Path('/var/lib/rmm-agent-remote/lifecycle')/('expiry-'+ident)
|
|
require(not root.is_symlink() and root.stat().st_uid==993 and root.stat().st_mode & 0o777==0o700,'own private fixture directory')
|
|
for name,data in [('ca.pem',payload['ca'])]+[(name+'.pem',data) for name,data in payload['certificates'].items()]:
|
|
require(name in ('ca.pem','valid.pem','expired.pem'),'fixed public certificate names')
|
|
fd=os.open(root/name,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
|
|
with os.fdopen(fd,'wb') as f:f.write(base64.b64decode(data,validate=True));f.flush();os.fsync(f.fileno());os.fchown(f.fileno(),993,984)
|
|
os.setgroups([]);os.setgid(984);os.setuid(993)
|
|
def request(name):
|
|
context=ssl.create_default_context(cafile=str(root/'ca.pem'))
|
|
require(context.check_hostname and context.verify_mode==ssl.CERT_REQUIRED,'normal CA and hostname verification required')
|
|
context.load_cert_chain(str(root/(name+'.pem')),str(root/'fixture.key'))
|
|
try:
|
|
with socket.create_connection(('88.99.80.202',443),timeout=8) as tcp:
|
|
with context.wrap_socket(tcp,server_hostname='discovery.ease-technologies.de') as peer:
|
|
server_sha=hashlib.sha256(peer.getpeercert(binary_form=True)).hexdigest()
|
|
peer.sendall(b'POST /agent/v1/messages HTTP/1.1\r\nHost: discovery.ease-technologies.de\r\nContent-Type: application/x-protobuf\r\nContent-Length: 0\r\nConnection: close\r\n\r\n')
|
|
reply=http.client.HTTPResponse(peer);reply.begin();reply.read(4096)
|
|
return dict(http_status=reply.status,tls_rejected=False,server_certificate_sha256=server_sha)
|
|
except ssl.SSLError as error:
|
|
return dict(tls_rejected=True,alert_reason=error.reason,http_status=None)
|
|
before=request('valid');expired=request('expired');after=request('valid')
|
|
require(before.get('http_status')==after.get('http_status')==401 and not before['tls_rejected'] and not after['tls_rejected'], 'valid unregistered peer must pass TLS and fail application binding')
|
|
require(expired['tls_rejected'] and expired['http_status'] is None and expired.get('alert_reason') in
|
|
('SSLV3_ALERT_CERTIFICATE_EXPIRED','TLSV1_ALERT_CERTIFICATE_EXPIRED'),'expired client must be rejected by server TLS before HTTP')
|
|
permanent=Path('/var/lib/rmm-agent-remote')
|
|
for name,witness in payload['permanent_witnesses'].items():
|
|
p=permanent/name;s=p.stat();require(dict(sha256=hashlib.sha256(p.read_bytes()).hexdigest(),device=s.st_dev,inode=s.st_ino)==witness,'permanent identity/selector changed')
|
|
return dict(id=ident,verified=True,positive_before=before,expired=expired,positive_after=after,
|
|
permanent_identity_preserved=True,probe_uid=os.geteuid(),clock_adjusted=False,verification_disabled=False)
|
|
|
|
|
|
def server_gate(payload):
|
|
ident=str(uuid.UUID(payload['id']));root=Path('/opt/rust-discovery')
|
|
compose=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml')]
|
|
# Read exactly these two nonsecret runtime keys; never expose full Env.
|
|
profile=subprocess.run(compose+['exec','-T','gateway','printenv','RMM_PROFILE','RMM_DEV_AUTH'],
|
|
capture_output=True,check=True,timeout=15).stdout.decode().splitlines()
|
|
require(profile==['development','true'],'expiry fixture requires actual development gateway with dev authentication')
|
|
command=compose+['exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm']
|
|
query=("SELECT json_build_object('installations',(SELECT count(*) FROM agent_installations WHERE agent_id='"+ident+"'),"
|
|
"'jobs',(SELECT count(*) FROM jobs WHERE agent_id='"+ident+"'),"
|
|
"'outbox',(SELECT count(*) FROM outbox o JOIN jobs j USING(tenant_id,job_id) WHERE j.agent_id='"+ident+"'),"
|
|
"'permits',(SELECT count(*) FROM execution_permits p JOIN jobs j USING(tenant_id,job_id) WHERE j.agent_id='"+ident+"'));")
|
|
result=json.loads(subprocess.run(command,input=query.encode(),capture_output=True,check=True,timeout=15).stdout)
|
|
require(all(v==0 for v in result.values()),'fixture must remain unregistered with zero job authority')
|
|
return result
|
|
|
|
|
|
def main():
|
|
parser=argparse.ArgumentParser(description=__doc__);parser.parse_args()
|
|
os.umask(0o077);ident=str(uuid.uuid4());folder=STATE/('expiry-'+ident);folder.mkdir(mode=0o700)
|
|
m=cloud.contract
|
|
before=cloud.bootstrap.rpc('server.conf',server_gate,dict(id=ident));m.write_private(folder/'gate-before.json',before)
|
|
prepared=cloud.bootstrap.rpc('linux-vm.conf',prepare_vm,dict(id=ident));m.write_private(folder/'prepared.json',prepared)
|
|
signed=cloud.bootstrap.rpc('server.conf',sign_server,prepared);m.write_private(folder/'signed-public-fixtures.json',signed)
|
|
tested=cloud.bootstrap.rpc('linux-vm.conf',probe_vm,dict(signed,permanent_witnesses=prepared['permanent_witnesses']))
|
|
m.write_private(folder/'native-result.json',tested)
|
|
after=cloud.bootstrap.rpc('server.conf',server_gate,dict(id=ident));m.write_private(folder/'gate-after.json',after)
|
|
require(before==after,'fixture acquired authority')
|
|
print(dict(verified=tested['verified'],expired_server_tls_rejected=True,valid_peer_application_status=401,
|
|
unregistered_zero_jobs=True,permanent_identity_preserved=True,probe_uid=tested['probe_uid'],id=ident))
|
|
|
|
|
|
if __name__=='__main__':
|
|
try:main()
|
|
except Exception as error:
|
|
print('CLOUD_TLS_EXPIRY_INCOMPLETE:'+type(error).__name__)
|
|
prefix='remote bootstrap incomplete: '
|
|
if isinstance(error,RuntimeError) and str(error).startswith(prefix):
|
|
try:
|
|
detail=json.loads(str(error)[len(prefix):])
|
|
print('REMOTE_BOOTSTRAP_INCOMPLETE:'+json.dumps(dict(error_class=str(detail['error_class']),
|
|
frames=[dict(function=str(f['function']),line=int(f['line'])) for f in detail['frames']])))
|
|
except (ValueError,TypeError,KeyError):pass
|
|
raise SystemExit(2) from None
|