Skip to content

Commit

Permalink
work on streams
Browse files Browse the repository at this point in the history
  • Loading branch information
fjames86 committed Jan 6, 2019
1 parent 5038fae commit 6c5d3c8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
3 changes: 1 addition & 2 deletions ffi.lisp
Expand Up @@ -1046,8 +1046,7 @@ bend is buffer end index and extra-start is starting index of first extra byte."
'cbuffer)))))))

(values bend extra-start nil))
((or (= sts +incomplete-message+)
#+nil(= sts +invalid-token+))
((or (= sts +incomplete-message+) #+nil(= sts +invalid-token+))
(values nil nil t))
(t (win-error sts)))))))

Expand Down
1 change: 1 addition & 0 deletions package.lisp
Expand Up @@ -15,6 +15,7 @@
;; class functions
#:free-schannel-context
#:make-client-context
#:client-context-hostname
#:make-server-context
#:encrypt-message
#:decrypt-message
Expand Down
51 changes: 47 additions & 4 deletions streams.lisp
@@ -1,6 +1,7 @@

(defpackage #:schannel-streams
(:use #:cl))
(:use #:cl)
(:export #:make-client-stream))

(in-package #:schannel-streams)

Expand Down Expand Up @@ -106,8 +107,50 @@ offets to point to end of plaintext and remaining undecrypted bytes from next me

(defclass client-stream (schannel-stream)
())


(defmethod print-object ((cs client-stream) stream)
(print-unreadable-object (cs stream :type t)
(format stream ":HOSTNAME ~S"
(let ((cxt (stream-cxt cs)))
(when cxt (schannel:client-context-hostname cxt))))))

(defun init-client-stream (cxt base-stream)
;; start by generating the first token
(let ((tok (schannel:initialize-client-context cxt)))
(write-sequence tok base-stream)
(force-output base-stream))

(do ((offset 0)
(buf (make-array (* 16 1024) :element-type '(unsigned-byte 8)))
(done nil))
(done)
(format t ";; offset=~A~%" offset)
(let ((n (read-sequence buf base-stream :start offset)))
(format t ";; new offset=~A~%" n)
(setf offset n))
(multiple-value-bind (token extra-bytes incomplete-p)
(schannel:initialize-client-context cxt buf 0 offset)
(cond
(incomplete-p
;; recv token incomplete - need more bytes
nil)
(t
;; token complete and was processed
(when (arrayp token)
;; generated output token, send it
(write-sequence token base-stream)
(force-output base-stream))

(cond
(extra-bytes
;; received extra bytes, memmove and update offsets
(dotimes (i extra-bytes)
(setf (aref buf i) (aref buf (+ (- offset extra-bytes) i))))
(setf offset extra-bytes))
(t
(setf offset 0)))
(when (eq token t)
;; token=t implies context complete
(setf done t)))))))

(defun make-client-stream (base-stream hostname &key ignore-certificates-p)
(let ((cxt (schannel:make-client-context
Expand All @@ -119,7 +162,7 @@ offets to point to end of plaintext and remaining undecrypted bytes from next me


;; setup context
;; TODO
(init-client-stream cxt base-stream)

;; return instance
(make-instance 'client-stream :stream base-stream :cxt cxt))))
Expand Down

0 comments on commit 6c5d3c8

Please sign in to comment.