Skip to content

Instantly share code, notes, and snippets.

@richo
Created December 17, 2012 23:29
Show Gist options
  • Save richo/4323437 to your computer and use it in GitHub Desktop.
Save richo/4323437 to your computer and use it in GitHub Desktop.
;;;; openssl.scm
;;;; Bindings to the OpenSSL SSL/TLS library
(module openssl
(
ssl-connect
ssl-make-client-context
ssl-client-context?
ssl-listen
ssl-close
ssl-port?
ssl-port->tcp-port
ssl-listener?
ssl-listener?
ssl-listener-port
ssl-listener-fileno
ssl-accept-ready?
ssl-accept
ssl-handshake-timeout
ssl-shutdown-timeout
ssl-load-certificate-chain!
ssl-load-private-key!
ssl-set-verify!
ssl-load-verify-root-certificates!
ssl-load-suggested-certificate-authorities!
ssl-peer-verified?
ssl-peer-subject-name ssl-peer-issuer-name
ssl-make-i/o-ports
net-unwrap-tcp-ports
ssl-signature-verify)
(import scheme chicken foreign ports)
(declare
(usual-integrations)
(no-procedure-checks-for-usual-bindings)
(bound-to-procedure
##sys#update-errno
##sys#signal-hook
##sys#string-append
##sys#tcp-port->fileno
##sys#current-thread
##sys#size
##sys#setslot
##sys#check-string
##sys#expand-home-path))
(use srfi-18 tcp)
#>
#include <errno.h>
#ifdef _WIN32
#ifdef _MSC_VER
#include <winsock2.h>
#else
#include <ws2tcpip.h>
#endif
#include <openssl/rand.h>
#else
#define closesocket close
#endif
#ifdef ECOS
#include <sys/sockio.h>
#else
#include <unistd.h>
#endif
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
<#
(foreign-code #<<EOF
ERR_load_crypto_strings();
SSL_load_error_strings();
SSL_library_init();
#ifdef _WIN32
RAND_screen();
#endif
EOF
)
;;
;; ......
;;
;; verify a signature, given a public key file
(define (ssl-signature-verify certfile signature)
((foreign-lambda*
c-string* ((c-string pathname) (c-string signature))
"unsigned char *pad = RSA_PKCS1_PADDING;\n"
"EVP_PKEY *pkey = load_pubkey(NULL, pathname, str2fmt(\"PEM\"), 0, NULL, NULL, \"Public Key\");\n"
"RSA *rsa = EVP_PKEY_get1_RSA(pkey);\n"
"EVP_PKEY_free(pkey);\n"
"int keysize = RSA_size(rsa);\n"
"unsigned char *rsa_out = malloc(keysize);\n"
"int rsa_outlen = RSA_public_decrypt(strlen(signature), signature, rsa_out, rsa, pad);\n"
"C_return(rsa_out);\n")
(certfile signature)))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment