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