| 1 |
;;; Copyright (C) 2001, 2003 Eric Marsden |
|---|
| 2 |
;;; Copyright (C) 2005 David Lichteblau |
|---|
| 3 |
;;; "the conditions and ENSURE-SSL-FUNCALL are by Jochen Schmidt." |
|---|
| 4 |
;;; |
|---|
| 5 |
;;; See LICENSE for details. |
|---|
| 6 |
|
|---|
| 7 |
#| |
|---|
| 8 |
(load "example.lisp") |
|---|
| 9 |
(ssl-test::test-https-client "www.google.com") |
|---|
| 10 |
(ssl-test::test-https-server) |
|---|
| 11 |
|# |
|---|
| 12 |
|
|---|
| 13 |
(defpackage :ssl-test |
|---|
| 14 |
(:use :cl)) |
|---|
| 15 |
(in-package :ssl-test) |
|---|
| 16 |
|
|---|
| 17 |
(eval-when (:compile-toplevel :load-toplevel :execute) |
|---|
| 18 |
(asdf:operate 'asdf:load-op :trivial-sockets)) |
|---|
| 19 |
|
|---|
| 20 |
(defun read-line-crlf (stream &optional eof-error-p) |
|---|
| 21 |
(let ((s (make-string-output-stream))) |
|---|
| 22 |
(loop |
|---|
| 23 |
for empty = t then nil |
|---|
| 24 |
for c = (read-char stream eof-error-p nil) |
|---|
| 25 |
while (and c (not (eql c #\return))) |
|---|
| 26 |
do |
|---|
| 27 |
(unless (eql c #\newline) |
|---|
| 28 |
(write-char c s)) |
|---|
| 29 |
finally |
|---|
| 30 |
(return |
|---|
| 31 |
(if empty nil (get-output-stream-string s)))))) |
|---|
| 32 |
|
|---|
| 33 |
(defun test-nntps-client (&optional (host "snews.gmane.org") (port 563)) |
|---|
| 34 |
(let* ((fd (trivial-sockets:open-stream host port |
|---|
| 35 |
:element-type '(unsigned-byte 8))) |
|---|
| 36 |
(nntps (cl+ssl:make-ssl-client-stream fd :external-format :iso-8859-1))) |
|---|
| 37 |
(format t "NNTPS> ~A~%" (read-line-crlf nntps)) |
|---|
| 38 |
(write-line "HELP" nntps) |
|---|
| 39 |
(force-output nntps) |
|---|
| 40 |
(loop :for line = (read-line-crlf nntps nil) |
|---|
| 41 |
:until (string-equal "." line) |
|---|
| 42 |
:do (format t "NNTPS> ~A~%" line)))) |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
;; open an HTTPS connection to a secure web server and make a |
|---|
| 46 |
;; HEAD request |
|---|
| 47 |
(defun test-https-client (host &optional (port 443)) |
|---|
| 48 |
(let* ((deadline (+ (get-internal-real-time) |
|---|
| 49 |
(* 3 internal-time-units-per-second))) |
|---|
| 50 |
(socket (ccl:make-socket :address-family :internet |
|---|
| 51 |
:connect :active |
|---|
| 52 |
:type :stream |
|---|
| 53 |
:remote-host host |
|---|
| 54 |
:remote-port port |
|---|
| 55 |
;; :local-host (resolve-hostname local-host) |
|---|
| 56 |
;; :local-port local-port |
|---|
| 57 |
:deadline deadline)) |
|---|
| 58 |
(https |
|---|
| 59 |
(progn |
|---|
| 60 |
(cl+ssl:make-ssl-client-stream |
|---|
| 61 |
socket |
|---|
| 62 |
:unwrap-stream-p t |
|---|
| 63 |
:external-format :iso-8859-1)))) |
|---|
| 64 |
(unwind-protect |
|---|
| 65 |
(progn |
|---|
| 66 |
(format https "GET / HTTP/1.0~%Host: ~a~%~%" host) |
|---|
| 67 |
(force-output https) |
|---|
| 68 |
(loop :for line = (read-line-crlf https nil) |
|---|
| 69 |
:while line :do |
|---|
| 70 |
(format t "HTTPS> ~a~%" line))) |
|---|
| 71 |
(close socket) |
|---|
| 72 |
(close https)))) |
|---|
| 73 |
|
|---|
| 74 |
;; start a simple HTTPS server. See the mod_ssl documentation at |
|---|
| 75 |
;; <URL:http://www.modssl.org/> for information on generating the |
|---|
| 76 |
;; server certificate and key |
|---|
| 77 |
;; |
|---|
| 78 |
;; You can stress-test the server with |
|---|
| 79 |
;; |
|---|
| 80 |
;; siege -c 10 -u https://host:8080/foobar |
|---|
| 81 |
;; |
|---|
| 82 |
(defun test-https-server |
|---|
| 83 |
(&key (port 8080) |
|---|
| 84 |
(cert "/home/david/newcert.pem") |
|---|
| 85 |
(key "/home/david/newkey.pem")) |
|---|
| 86 |
(format t "~&SSL server listening on port ~d~%" port) |
|---|
| 87 |
(trivial-sockets:with-server (server (:port port)) |
|---|
| 88 |
(loop |
|---|
| 89 |
(let* ((socket (trivial-sockets:accept-connection |
|---|
| 90 |
server |
|---|
| 91 |
:element-type '(unsigned-byte 8))) |
|---|
| 92 |
(client (cl+ssl:make-ssl-server-stream |
|---|
| 93 |
(cl+ssl:stream-fd socket) |
|---|
| 94 |
:external-format :iso-8859-1 |
|---|
| 95 |
:certificate cert |
|---|
| 96 |
:key key))) |
|---|
| 97 |
(unwind-protect |
|---|
| 98 |
(progn |
|---|
| 99 |
(loop :for line = (read-line-crlf client nil) |
|---|
| 100 |
:while (> (length line) 1) :do |
|---|
| 101 |
(format t "HTTPS> ~a~%" line)) |
|---|
| 102 |
(format client "HTTP/1.0 200 OK~%") |
|---|
| 103 |
(format client "Server: SSL-CMUCL/1.1~%") |
|---|
| 104 |
(format client "Content-Type: text/plain~%") |
|---|
| 105 |
(terpri client) |
|---|
| 106 |
(format client "G'day at ~A!~%" |
|---|
| 107 |
(multiple-value-list (get-decoded-time))) |
|---|
| 108 |
(format client "CL+SSL running in ~A ~A~%" |
|---|
| 109 |
(lisp-implementation-type) |
|---|
| 110 |
(lisp-implementation-version))) |
|---|
| 111 |
(close socket) |
|---|
| 112 |
(close client)))))) |
|---|