interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / social / 40auth.om

857 lines · 15811 bytes Raw Blame History
1note social/40auth.om: sessions, CSRF, and sign-in with a Radicle key (challenge + ssh-keygen -Y verify).
2note Cookies: __Host-forge_session=<64 hex token> (the log stores only sha256(token)); __Host-forge_pre=<32 hex>
3note binds the sign-in form to this browser. Both HttpOnly, Secure, SameSite=Lax, Path=/, no Domain (the
4note __Host- prefix), under either base: the browser then refuses the name from a subdomain or over http.
5note ---- Set-Cookie builder (fsck)
6fn soscct t view uchar * to void
7for i ulong len t bound 512
8fscc get t i
9end
10end
11note sosckset: "<name>=<value>; Max-Age=<age>; HttpOnly; Secure; SameSite=Lax; Path=/"
12fn sosckset nm view uchar * v view uchar * age ulong to void
13put fsckl 0 0
14soscct nm
15soscct v
16soscct tsckage
17d = new uchar 20 0
18k = sodec age d
19soscct part d 0 k
20soscct tsckatt
21end
22note ---- cookies
23note sockget: value of cookie nm (prefix "name=") in fck into o; returns its length, 0 when absent or too long
24fn sockget nm view uchar * o edit uchar * to ulong
25n = get fckl 0
26for i ulong n bound 4096
27st = eq i 0
28if gt i 0
29c = get fck sub i 1
30st = or eq c 32 eq c 59
31end
32if and st sockat i nm
33ret sockcp add i len nm o
34end
35end
36ret 0
37end
38fn sockat i ulong nm view uchar * to bool
39if gt add i len nm get fckl 0
40ret false
41end
42for k ulong len nm bound 64
43if ne get fck add i k get nm k
44ret false
45end
46end
47ret true
48end
49fn sockcp p ulong o edit uchar * to ulong
50k = ulong 0
51for it ulong 4096
52if ge add p k get fckl 0
53break
54end
55c = get fck add p k
56if or eq c 59 eq c 32
57break
58end
59if ge k len o
60ret 0
61end
62put o k c
63k = add k 1
64end
65ret k
66end
67note sohexok: s is lowercase hex
68fn sohexok s view uchar * to bool
69for i ulong len s bound 4096
70if lt sohexv get s i 0
71ret false
72end
73end
74ret true
75end
76note ---- sessions
77note sosess: parse the session cookie; sets gscur/gssid, fuser, fcsrf. 1 signed in, 0 not
78fn sosess to long
79put fuserl 0 0
80put fcsrfl 0 0
81put gscur 0 0
82put gssid 0 0
83sonowset
84if eq get gsok 0 0
85ret 0
86end
87t = new uchar 64 0
88if ne sockget tsckses t 64
89ret 0
90end
91if not sohexok t
92ret 0
93end
94hh = new uchar 64 0
95sotokh t hh
96sokclr
97sokt hh
98s = sofind
99if eq s 0
100ret 0
101end
102d = get gssd s
103if or eq d 0 le get gsse s get gsnow 0
104ret 0
105end
106put gscur 0 d
107put gssid 0 s
108souserset d
109socsrfset hh
110ret 1
111end
112note sotokh: hh = hex sha256(token hex)
113fn sotokh t view uchar * hh edit uchar * to void
114h = new uchar 32 0
115x = sha2_digest 256 t h
116sohex h hh
117end
118fn socsrfset hh view uchar * to void
119m = new uchar 69 0
120for i ulong 5
121put m i get tscsrfp i
122end
123for i ulong 64
124put m add i 5 get hh i
125end
126h = new uchar 32 0
127sohmac m h
128x = new uchar 32 0
129sohex part h 0 16 x
130for i ulong 32
131put fcsrf i get x i
132end
133put fcsrfl 0 32
134end
135note souserset: fuser = display name, else the DID's key part (z6Mk...)
136fn souserset d ulong to void
137n = get gsnm d
138k = ulong 0
139if ne n 0
140o = get gsso n
141for i ulong get gssl n bound 64
142put fuser i get gsstr add o i
143end
144put fuserl 0 get gssl n
145ret
146end
147o = get gsso d
148l = sub get gssl d len tsdidp
149if gt l len fuser
150l = len fuser
151end
152for i ulong l
153put fuser i get gsstr add add o len tsdidp i
154end
155put fuserl 0 l
156end
157note socsrfok: signed in and the body field csrf equals the session's CSRF token fcsrf (constant time)
158fn socsrfok to bool
159if eq get gscur 0 0
160ret false
161end
162n = fbget tsfcsrf
163if ne n 32
164ret false
165end
166a = new uchar 32 0
167b = new uchar 32 0
168for i ulong 32
169put a i get fval i
170put b i get fcsrf i
171end
172ret soeqct a b
173end
174note ---- sign-in limiters (in memory; the server is sequential). The hash is keyed with social.key so a
175note client cannot aim its keys at someone else's slot.
176fn soiphash salt ulong to ulong
177h = xor ulong 14695981039346656037 salt
178for i ulong 8
179h = xor h ulong get gskey i
180h = mul h 1099511628211
181end
182for i ulong get fipl 0 bound 64
183h = xor h ulong get fip i
184h = mul h 1099511628211
185end
186ret h
187end
188note soiplim: count one attempt for key (salt, IP) in this minute; false when over lim. A key looks at 8
189note slots from its hash and reuses only slots of an earlier minute, so it never evicts another key's live
190note count; with no room it is refused (fail closed).
191fn soiplim salt ulong lim ulong to bool
192h = soiphash salt
193m = div get gsnow 0 60
194s = and h 4095
195for j ulong 8
196k = and add s j 4095
197if and eq get gsipk k h eq get gsipm k m
198c = add get gsipc k 1
199put gsipc k c
200ret le c lim
201end
202end
203for j ulong 8
204k = and add s j 4095
205if ne get gsipm k m
206put gsipk k h
207put gsipm k m
208put gsipc k 1
209ret le 1 lim
210end
211end
212ret false
213end
214note sodidh: salt for the per-(DID, IP) limiter (FNV-1a of the z... key)
215fn sodidh z view uchar * to ulong
216h = ulong 1469598103934665603
217for i ulong len z bound 64
218h = xor h ulong get z i
219h = mul h 1099511628211
220end
221ret or h 2
222end
223note sovlim: a site-wide backstop of 600 signature checks per minute (bounds ssh-keygen spawns); the
224note per-IP (10/min) and per-(DID, IP) (5/min) limits come first, so one client cannot reach it alone
225fn sovlim to bool
226m = div get gsnow 0 60
227if ne get gsvm 0 m
228put gsvm 0 m
229put gsvc 0 0
230end
231c = add get gsvc 0 1
232put gsvc 0 c
233ret le c 600
234end
235note ---- retired challenges: a nonce is spent by its first signature check, pass or fail. Ring of 16384
236note (nonce key, time); at most 600 checks a minute for 16 minutes is 9600, so a live entry is never
237note overwritten. In memory only: a successful nonce is also in the log (session event).
238fn sonrh nh view uchar * to ulong
239h = xor ulong 14695981039346656037 3
240for i ulong 8
241h = xor h ulong get gskey i
242h = mul h 1099511628211
243end
244for i ulong len nh bound 64
245h = xor h ulong get nh i
246h = mul h 1099511628211
247end
248ret h
249end
250fn sonret nh view uchar * to bool
251h = sonrh nh
252t = get gsnow 0
253for i ulong 16384
254if and eq get gsrnh i h le t add get gsrnt i 960
255ret true
256end
257end
258ret false
259end
260fn sonretire nh view uchar * to void
261i = get gsrni 0
262put gsrnh i sonrh nh
263put gsrnt i get gsnow 0
264put gsrni 0 and add i 1 16383
265end
266note ---- body fields
267note sobval: body field nm into o (at most len o); returns the length or -1 when absent or too long
268fn sobval nm view uchar * o edit uchar * to long
269n = fbget nm
270if or lt n 0 gt n long len o
271ret -1
272end
273for i ulong ulong n
274put o i get fval i
275end
276ret n
277end
278note sotrim: bounds [a, b) of s without leading/trailing spaces, tabs, CR, LF; returns a and sets gspc = b
279fn sotrim s view uchar * n ulong to ulong
280a = ulong 0
281b = n
282for it ulong n bound 8192
283if ge a b
284break
285end
286if not sows get s a
287break
288end
289a = add a 1
290end
291for it ulong n bound 8192
292if le b a
293break
294end
295if not sows get s sub b 1
296break
297end
298b = sub b 1
299end
300put gspc 0 b
301ret a
302end
303fn sows c uchar to bool
304ret or or eq c 32 eq c 9 or eq c 13 eq c 10
305end
306note ---- sign-in
307note sologin: GET shows the challenge (or the signed-in state); POST verifies
308fn sologin to long
309if eq get fmeth 0 3
310ret sologinp
311end
312if gt get fmeth 0 2
313ret 405
314end
315fsetti tsttl
316if ne get gscur 0 0
317ret sopgin
318end
319if eq get gsok 0 0
320ret soerr 503 tsmoff
321end
322pre = new uchar 16 0
323non = new uchar 16 0
324if or not sorand pre not sorand non
325ret soerr 500 tsmrand
326end
327ph = new uchar 32 0
328nh = new uchar 32 0
329sohex pre ph
330sohex non nh
331ts = get gsnow 0
332mac = new uchar 64 0
333somacl nh ts ph mac
334sosckset tsckpre ph 900
335ret sopglog nh ts mac
336end
337note somacl: mac = hex HMAC(key, "login:" nonce ":" ts ":" pre)
338fn somacl nh view uchar 32 ts ulong ph view uchar 32 mac edit uchar 64 to void
339m = new uchar 128 0
340n = ulong 0
341for i ulong len tslogp
342put m n get tslogp i
343n = add n 1
344end
345for i ulong 32
346put m n get nh i
347n = add n 1
348end
349put m n 58
350n = add n 1
351d = new uchar 20 0
352k = sodec ts d
353for i ulong k
354put m n get d i
355n = add n 1
356end
357put m n 58
358n = add n 1
359for i ulong 32
360put m n get ph i
361n = add n 1
362end
363h = new uchar 32 0
364sohmac part m 0 n h
365sohex h mac
366end
367note sochal: the challenge text "interplane-forge-login:<nonce>:<ts>" into o; returns its length
368fn sochal nh view uchar * ts ulong o edit uchar * to ulong
369n = ulong 0
370for i ulong len tschal
371put o n get tschal i
372n = add n 1
373end
374for i ulong 32
375put o n get nh i
376n = add n 1
377end
378put o n 58
379n = add n 1
380d = new uchar 20 0
381k = sodec ts d
382for i ulong k
383put o n get d i
384n = add n 1
385end
386ret n
387end
388note sologinp: POST /login
389fn sologinp to long
390if eq get gsok 0 0
391ret soerr 503 tsmoff
392end
393if not soiplim 1 10
394ret soerr 429 tsmrate
395end
396ph = new uchar 32 0
397nh = new uchar 32 0
398mac = new uchar 64 0
399if ne sockget tsckpre ph 32
400ret soerr 403 tsmform
401end
402if or ne sobval tsfnon nh 32 ne sobval tsfcsrf mac 64
403ret soerr 403 tsmform
404end
405ts = soformts
406if or not sohexok ph not sohexok nh
407ret soerr 403 tsmform
408end
409want = new uchar 64 0
410somacl nh ts ph want
411if not soeqct mac want
412ret soerr 403 tsmform
413end
414if or lt add ts 900 get gsnow 0 gt ts add get gsnow 0 60
415ret soerr 403 tsmexp
416end
417if sononused nh
418ret soerr 403 tsmused
419end
420ret sologin2 nh ts
421end
422note soformts: the ts body field (decimal), 0 when invalid
423fn soformts to ulong
424d = new uchar 16 0
425n = sobval tsfts d
426if or lt n 1 gt n 12
427ret 0
428end
429put gspc 0 0
430put gsbad 0 0
431v = sonum part d 0 ulong n
432if or ne get gsbad 0 0 ne get gspc 0 ulong n
433ret 0
434end
435ret v
436end
437note sononused: the nonce is spent (retired by a check this run, or used by a session in the log)
438fn sononused nh view uchar * to bool
439if sonret nh
440ret true
441end
442sokclr
443sokt tsnonp
444sokt nh
445s = sofind
446if eq s 0
447ret false
448end
449ret ne get gsnu s 0
450end
451note sologin2: DID + signature checks, then the session
452fn sologin2 nh view uchar 32 ts ulong to long
453raw = new uchar 128 0
454n = sobval tsfdid raw
455if lt n 1
456n = sodidln raw
457end
458if lt n 1
459ret soerr 400 tsmdid
460end
461a = sotrim raw ulong n
462b = get gspc 0
463z = new uchar 64 0
464zl = sodidz part raw a sub b a z
465pk = new uchar 32 0
466if eq zl 0
467ret soerr 400 tsmdid
468end
469if not sodidpk part z 0 zl pk
470ret soerr 400 tsmdid
471end
472if not soiplim sodidh part z 0 zl 5
473ret soerr 429 tsmrate
474end
475if not sovlim
476ret soerr 429 tsmrate
477end
478sonretire nh
479v = soverify nh ts pk
480if lt v 0
481ret soerr ulong neg v tsmsig
482end
483if ne v 0
484ret soerr 403 tsmsig
485end
486ret sologin3 nh part z 0 zl
487end
488note sodidln: one-box sign-in: the first word of the sig field (the account id line the sign-in command
489note prints above the signature) into o; returns its length, 0 when there is none
490fn sodidln o edit uchar * to long
491n = fbget tsfsig
492if lt n 1
493ret 0
494end
495m = ulong n
496a = ulong 0
497for it ulong m bound 4096
498if ge a m
499break
500end
501if not sows get fval a
502break
503end
504a = add a 1
505end
506k = ulong 0
507for it ulong m bound 4096
508if ge add a k m
509break
510end
511c = get fval add a k
512if sows c
513break
514end
515if ge k len o
516ret 0
517end
518put o k c
519k = add k 1
520end
521ret long k
522end
523note sodidz: accept "did:key:z..." or "z..." and copy the z... part into z; returns its length or 0
524fn sodidz s view uchar * z edit uchar * to ulong
525p = ulong 0
526if ge len s len tsdidp
527p = len tsdidp
528for i ulong len tsdidp
529if ne get s i get tsdidp i
530p = ulong 0
531end
532end
533end
534l = sub len s p
535if or lt l 40 gt l len z
536ret 0
537end
538for i ulong l
539put z i get s add p i
540end
541ret l
542end
543note soverify: 0 = signature valid; >0 = ssh-keygen refused; <0 = -HTTP status (400 bad input, 500 internal)
544fn soverify nh view uchar 32 ts ulong pk view uchar 32 to long
545sig = new uchar 4096 0
546sl = sosigin sig
547if eq sl 0
548ret -400
549end
550st = new uchar 8 0
551if not sorand st
552ret -500
553end
554for i ulong 8
555put gsstem i get st i
556end
557okw = sovwr nh ts pk part sig 0 sl
558r = long -500
559if okw
560r = sorunk
561end
562sotmprm 97
563sotmprm 115
564sotmprm 109
565ret r
566end
567note sovwr: write the three temp files (a = allowed signers, s = signature, m = challenge)
568fn sovwr nh view uchar 32 ts ulong pk view uchar 32 sg view uchar * to bool
569msg = new uchar 96 0
570ml = sochal nh ts msg
571al = new uchar 128 0
572k = sopkline pk al
573if not sotmpw 97 part al 0 k
574ret false
575end
576if not sotmpw 115 sg
577ret false
578end
579ret sotmpw 109 part msg 0 ml
580end
581note sosigin: the sig field from its first "-" on (a one-box paste has the account id line before the
582note armor; sodidln reads that line), CR removed, trimmed, armored, at most 4000 bytes, newline-terminated
583fn sosigin o edit uchar 4096 to ulong
584n = fbget tsfsig
585if or lt n 1 gt n 4000
586ret 0
587end
588m = ulong n
589s0 = ulong 0
590for it ulong m bound 4096
591if ge s0 m
592break
593end
594if eq get fval s0 45
595break
596end
597s0 = add s0 1
598end
599k = ulong 0
600for j ulong sub m s0 bound 4096
601c = get fval add s0 j
602if ne c 13
603if not sosigc c
604ret 0
605end
606put o k c
607k = add k 1
608end
609end
610a = sotrim part o 0 k k
611b = get gspc 0
612if lt sub b a add len tssigb len tssige
613ret 0
614end
615for i ulong sub b a
616put o i get o add a i
617end
618k = sub b a
619put o k 10
620if not soarmok part o 0 k
621ret 0
622end
623ret add k 1
624end
625fn sosigc c uchar to bool
626if and ge c 65 le c 90
627ret true
628end
629if and ge c 97 le c 122
630ret true
631end
632if and ge c 48 le c 57
633ret true
634end
635ret or or or eq c 43 eq c 47 or eq c 61 eq c 45 or eq c 32 eq c 10
636end
637fn soarmok s view uchar * to bool
638for i ulong len tssigb
639if ne get s i get tssigb i
640ret false
641end
642end
643p = sub len s len tssige
644for i ulong len tssige
645if ne get s add p i get tssige i
646ret false
647end
648end
649ret true
650end
651note sotmpp: gspth = <data>/tmp/<stem hex>.<c>
652fn sotmpp c uchar to void
653sopdd
654sopt tstmpd
655x = new uchar 16 0
656st = new uchar 8 0
657for i ulong 8
658put st i get gsstem i
659end
660sohex st x
661sopt x
662sopc 46
663sopc c
664end
665fn sotmpw c uchar d view uchar * to bool
666sotmpp c
667ret sowfile d
668end
669fn sotmprm c uchar to void
670sotmpp c
671r = unlink gspth 0 get gspthl 0
672end
673note sorunk: ssh-keygen -Y verify -f <a> -I forge-user -n interplane-forge -s <s> < <m>
674fn sorunk to long
675sotmpp 109
676mfd = fopen gspth 0 get gspthl 0 524288 0
677if lt mfd 0
678ret -500
679end
680soavb
681soava tssshkg
682soava tsY
683soava tsverify
684soava tsdf
685sotmpp 97
686soavpth
687soava tsdI
688soava tsprinc
689soava tsdn
690soava tsns
691soava tsds
692sotmpp 115
693soavpth
694r = sorunv mfd 5000
695x = fclose int mfd
696if eq r -2
697ret -500
698end
699if eq r -1
700ret 1
701end
702ret r
703end
704note sologin3: verified; record account (first time) and the session, set the cookie, 303
705fn sologin3 nh view uchar * z view uchar * to long
706sokclr
707sokt tsdidp
708sokt z
709did = sointern
710if eq did 0
711ret soerr 500 tsmfull
712end
713if eq get gsacc did 0
714sokclr
715if not soemit 9 did
716ret soerr 500 tsmwrite
717end
718end
719tk = new uchar 32 0
720if not sorand tk
721ret soerr 500 tsmrand
722end
723th = new uchar 64 0
724sohex tk th
725hh = new uchar 64 0
726sotokh th hh
727sokclr
728sokt hh
729sokc 46
730sokt nh
731if not soemit 11 did
732ret soerr 500 tsmwrite
733end
734sosckset tsckses th 2592000
735put fkind 0 2
736flocb
737if eq get gsnm did 0
738floct tspset2
739end
740ret 303
741end
742note POST /logout
743fn sologout to long
744if ne get fmeth 0 3
745ret 405
746end
747r = sowpre
748if ne r 0
749ret r
750end
751sokclr
752sokid get gssid 0
753if not soemit 12 get gscur 0
754ret soerr 500 tsmwrite
755end
756sosckset tsckses part tsnull 0 0 0
757put fkind 0 2
758flocb
759ret 303
760end
761note sowpre: common write gate: signed in (else 303 to login), CSRF (403), limits (429), store (503)
762fn sowpre to long
763if eq get gscur 0 0
764put fkind 0 2
765flocb
766floct tsplog2
767ret 303
768end
769if not socsrfok
770ret soerr 403 tsmcsrf
771end
772if eq get gsok 0 0
773ret soerr 503 tsmoff
774end
775if not fratelim
776ret soerr 429 tsmrate
777end
778if not sordok get gscur 0
779ret soerr 429 tsmrate
780end
781ret 0
782end
783note ---- display name
784fn sosetname to long
785if ne get fmeth 0 3
786ret 405
787end
788r = sowpre
789if ne r 0
790ret r
791end
792d = get gscur 0
793if eq d get gsown 0
794ret soerr 403 tsmownn
795end
796raw = new uchar 64 0
797n = sobval tsfname raw
798if lt n 0
799ret soerr 400 tsmname
800end
801a = sotrim raw ulong n
802b = get gspc 0
803sokclr
804for i ulong sub b a
805sokc get raw add a i
806end
807if not sonameok
808ret soerr 400 tsmname
809end
810if sorsvd
811ret soerr 400 tsmrsv
812end
813k = sofind
814if ne k 0
815o = get gsnmo k
816if and ne o 0 ne o d
817ret soerr 409 tstaken
818end
819if eq o d
820ret soback tspset2
821end
822end
823if not soemit 10 d
824ret soerr 500 tsmwrite
825end
826ret soback tspset2
827end
828note sorsvd: gskb is a reserved route word ("|name|" occurs in tsrsv)
829fn sorsvd to bool
830n = get gskbl 0
831for p ulong len tsrsv bound 1024
832if sorsvat p n
833ret true
834end
835end
836ret false
837end
838fn sorsvat p ulong n ulong to bool
839if gt add add p n 2 len tsrsv
840ret false
841end
842if or ne get tsrsv p 124 ne get tsrsv add add p n 1 124
843ret false
844end
845for i ulong n bound 64
846if ne get tsrsv add add p 1 i get gskb i
847ret false
848end
849end
850ret true
851end
852fn soback t view uchar * to long
853put fkind 0 2
854flocb
855floct t
856ret 303
857end