static int handleChap(isess_t *sess, pdu_t *pp) { pdu_t spp; login_req_t *lp; isc_opt_t *op = sess->op; char *ap, *ip, *cp, *digest; // MD5 is 128bits, SHA1 160bits debug_called(3); bzero(&spp, sizeof(pdu_t)); lp = (login_req_t *)&spp.ipdu.bhs; lp->cmd = ISCSI_LOGIN_CMD | 0x40; // login request + Inmediate memcpy(lp->isid, sess->isid, 6); lp->tsih = sess->tsih; // MUST be zero the first time! lp->CID = htons(1); lp->CSG = SN_PHASE; // Security Negotiation lp->NSG = LON_PHASE; lp->T = 1; if(((ap = getkeyval("CHAP_A=", pp)) == NULL) || ((ip = getkeyval("CHAP_I=", pp)) == NULL) || ((cp = getkeyval("CHAP_C=", pp)) == NULL)) return -1; if((digest = chapDigest(ap, (char)strtol(ip, (char **)NULL, 0), cp, op->chapSecret)) == NULL) return -1; addText(&spp, "CHAP_N=%s", op->chapIName? op->chapIName: op->initiatorName); addText(&spp, "CHAP_R=%s", digest); free(digest); if(op->tgtChapSecret != NULL) { op->tgtChapID = (random() >> 24) % 255; // should be random enough ... addText(&spp, "CHAP_I=%d", op->tgtChapID); cp = genChapChallenge(cp, op->tgtChallengeLen? op->tgtChallengeLen: 8); addText(&spp, "CHAP_C=%s", cp); op->tgtChapDigest = chapDigest(ap, op->tgtChapID, cp, op->tgtChapSecret); }
main(int cc, char **vv) { char *p, *ap, *ip, *cp, *chapSecret, *digest; int len; #if 0 ap = "5"; chapSecret = "0xa5aff013dd839b1edd31ee73a1df0b1b"; // chapSecret = "abcdefghijklmnop"; len = str2bin(chapSecret, &cp); puke(chapSecret, cp, len); ip = "238"; cp = "0xbd456029"; if((digest = chapDigest(ap, ip, cp, chapSecret)) != NULL) { len = str2bin(digest, &cp); puke(digest, cp, len); } #else printf("%d] %s\n", 24, genChallenge("0X", 24)); #endif }