snapkitty
cryptography
python
xml
File size: 5,605 Bytes
dfcd6e3
 
 
 
 
 
f3f1955
 
 
dfcd6e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
---
license: other
license_name: snapkitty-tri-license
license_link: https://huggingface.co/Snapkitty/vault-live/blob/main/LICENSE.tri
tags:
- snapkitty
- cryptography
- python
- xml
---

> Source: [github.com/SNAPKITTYWEST/vault-live](https://github.com/SNAPKITTYWEST/vault-live)

# vault-live

SAML 2.0 SP/IdP with a NAND-gated assertion validation pipeline.

Every inbound SAML assertion passes through a boolean constraint tree before any attribute is trusted. The constraint tree is written in XML β€” the same language as SAML itself. The entropy of the attribute set is bounded at 0.20 nats. The Semantic Hash produced by the gate is the immutable anchor for the WORM audit chain.

**Stack:** Python + `cryptography` library. No lxml, no xmlsec, no external SAML library.  
**Tests:** 31 passing β€” `pytest tests/`

---

## How it works

```
IdP produces signed SAML Response
         β”‚
         β–Ό  Base64 decode β†’ XML parse
         β”‚
         β–Ό  Signature verification (RSA-SHA256, enveloped)
         β”‚  uses IdP signing certificate as verification root
         β”‚
         β–Ό  Assertion validation
         β”‚  Conditions: NotBefore / NotOnOrAfter clock check
         β”‚  SubjectConfirmationData: Recipient + expiry
         β”‚  Audience restriction: must match SP EntityID
         β”‚
         β–Ό  Replay protection
         β”‚  assertion ID stored with expiry; duplicate β†’ ReplayDetectedError
         β”‚
         β–Ό  NAND gate  ← constraints.xml is the program
         β”‚  evaluates attribute set through boolean constraint tree
         β”‚  computes Shannon entropy H(attr values) in nats
         β”‚  enforces H <= 0.20
         β”‚  produces Semantic Hash (SHA-256, deterministic)
         β”‚
         β–Ό  WORM audit chain
            AuditRecord appended: semantic_hash, assertion_id, gate_result, H
            SHA-256 chain: entry_hash = SHA256(prev_hash + record_json)
            tamper any record β†’ chain verification fails
```

---

## The NAND gate

The constraint program lives in `nand/constraints.xml`. It is XML evaluated against the assertion's attribute map.

```
NAND(branch1, branch2) = NOT(branch1 AND branch2)

branch1 (session-check):  Role present AND SessionIndex present
branch2 (privilege-escalation):  Role == "SuperAdmin"

VaultUser:  NAND(True, False) = True   β†’ TRUSTED
SuperAdmin: NAND(True, True)  = False  β†’ BLOCKED
```

The security property: an attacker with maximum privileges satisfies all branches simultaneously, causing NAND to output False. A legitimate user fails at least one "attacker" branch, keeping NAND True.

Entropy constraint: an assertion with multiple distinct values per attribute raises H above 0.20 nats and is rejected regardless of the tree result.

The Semantic Hash binds the assertion identity to the audit record:

```python
semantic_hash = SHA256(
    assertion_id + "|" + issuer + "|" + issue_instant + "|"
    + str(int(tree_result)) + "|" + f"{entropy:.6f}" + "|"
    + json.dumps(sorted_attrs, sort_keys=True, separators=(',',':'))
)
```

---

## Modules

```
vault-live/
β”œβ”€β”€ crypto/
β”‚   β”œβ”€β”€ keys.py         RSA key generation, self-signed X.509, PEM load/save
β”‚   β”œβ”€β”€ xml_dsig.py     XML-DSig: RSA-SHA256 sign/verify, Exclusive C14N
β”‚   └── xml_encrypt.py  XML Encryption: AES-256-GCM payload, RSA-OAEP key wrap
β”‚
β”œβ”€β”€ nand/
β”‚   β”œβ”€β”€ constraints.xml  Constraint DSL (vl: namespace) β€” the program IS XML
β”‚   └── gate.py          NANDGate: tree eval, entropy, semantic hash
β”‚
β”œβ”€β”€ saml/
β”‚   β”œβ”€β”€ metadata.xml     SP metadata (EntityID, ACS, signing cert)
β”‚   β”œβ”€β”€ idp_metadata.xml IdP metadata (EntityID, SSO endpoint, signing cert)
β”‚   β”œβ”€β”€ sp/
β”‚   β”‚   β”œβ”€β”€ authn_request.py    AuthnRequest builder + HTTP-Redirect encoding
β”‚   β”‚   └── assertion_consumer.py  ACS: full validation chain + NAND gate
β”‚   └── idp/
β”‚       └── response_builder.py  Response + signed Assertion builder
β”‚
β”œβ”€β”€ audit/
β”‚   β”œβ”€β”€ worm.py    Append-only SHA-256 chain, verify_chain(), AuditRecord
β”‚   └── replay.py  ReplayStore: seen assertion IDs + expiry, file backend
β”‚
└── tests/
    β”œβ”€β”€ test_saml_flow.py   End-to-end: authn request, full flow, replay, tamper
    β”œβ”€β”€ test_nand_gate.py   Gate logic, entropy, semantic hash, constraint DSL
    └── test_audit.py       WORM chain, tamper detection, replay store
```

---

## Running

```bash
pip install cryptography pytest
pytest tests/ -v
```

---

## Why XML as the constraint language

SAML is XML. The assertions being validated are XML. The SP/IdP metadata is XML. Writing the constraint program in the same language (`vl:` namespace, `nand/constraints.xml`) means the trust policy is readable by the same tooling that reads the protocol β€” XPath queries, XML validators, schema checkers, diff tools. GitHub's language detector counts it as XML, which is accurate: the constraint tree is the program.

---

Built by Ahmad Ali Parr Γ— SnapKitty.

---

## License

Licensed under **SnapKitty Tri-License**. Full text: [LICENSE.tri](LICENSE.tri).

### πŸ’Ό Commercial License

Snapkitty code is free and open under **AGPL-3.0** for open-source use. Building a commercial product or service? A **proprietary commercial license** from Snapkitty Collective LLC lets you ship this code without the AGPL's source-sharing and network-use obligations.

**[β†’ Get a commercial license](mailto:A.parr@belespritdaccord.uk?subject=Commercial%20license:%20vault-live)** Β· A.parr@belespritdaccord.uk