fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
7e0ab3b
v2.1.3
1------------------------------------------------------------------------------ 2-- LuaSec 1.2.0 3-- 4-- Copyright (C) 2006-2022 Bruno Silvestre 5-- 6------------------------------------------------------------------------------ 7 8local core =require ( "ssl.core" ) 9local context =require ( "ssl.context" ) 10local x509 =require ( "ssl.x509" ) 11local config =require ( "ssl.config" ) 12 13local unpack =table . unpack or unpack 14 15-- We must prevent the contexts to be collected before the connections, 16-- otherwise the C registry will be cleared. 17local registry =setmetatable ( {} , { __mode ="k" } ) 18 19-- 20-- 21-- 22local function optexec ( func , param , ctx ) 23if param then 24if type ( param ) == "table" then 25return func ( ctx , unpack ( param )) 26else 27return func ( ctx , param ) 28end 29end 30return true 31end 32 33-- 34-- Convert an array of strings to wire-format 35-- 36local function array2wireformat ( array ) 37local str ="" 38for k , v in ipairs ( array ) do 39if type ( v ) ~= "string" then return nil end 40local len =# v 41if len == 0 then 42return nil , "invalid ALPN name (empty string)" 43elseif len > 255 then 44return nil , "invalid ALPN name (length > 255)" 45end 46str =str .. string . char ( len ) .. v 47end 48if str == "" then return nil , "invalid ALPN list (empty)" end 49return str 50end 51 52-- 53-- Convert wire-string format to array 54-- 55local function wireformat2array ( str ) 56local i =1 57local array ={} 58while i < # str do 59local len =str : byte ( i ) 60array [ # array + 1 ] =str : sub ( i + 1 , i + len ) 61i =i + len + 1 62end 63return array 64end 65 66-- 67-- 68-- 69local function newcontext ( cfg ) 70local succ , msg , ctx 71-- Create the context 72ctx , msg =context . create ( cfg . protocol ) 73if not ctx then return nil , msg end 74-- Mode 75succ , msg =context . setmode ( ctx , cfg . mode ) 76if not succ then return nil , msg end 77local certificates =cfg . certificates 78if not certificates then 79certificates ={ 80{ certificate =cfg . certificate , key =cfg . key , password =cfg . password } 81} 82end 83for _ , certificate in ipairs ( certificates ) do 84-- Load the key 85if certificate . key then 86if certificate . password and 87type ( certificate . password ) ~= "function" and 88type ( certificate . password ) ~= "string" 89then 90return nil , "invalid password type" 91end 92succ , msg =context . loadkey ( ctx , certificate . key , certificate . password ) 93if not succ then return nil , msg end 94end 95-- Load the certificate(s) 96if certificate . certificate then 97succ , msg =context . loadcert ( ctx , certificate . certificate ) 98if not succ then return nil , msg end 99if certificate . key and context . checkkey then 100succ =context . checkkey ( ctx ) 101if not succ then return nil , "private key does not match public key" end 102end 103end 104end 105-- Load the CA certificates 106if cfg . cafile or cfg . capath then 107succ , msg =context . locations ( ctx , cfg . cafile , cfg . capath ) 108if not succ then return nil , msg end 109end 110-- Set SSL ciphers 111if cfg . ciphers then 112succ , msg =context . setcipher ( ctx , cfg . ciphers ) 113if not succ then return nil , msg end 114end 115-- Set SSL cipher suites 116if cfg . ciphersuites then 117succ , msg =context . setciphersuites ( ctx , cfg . ciphersuites ) 118if not succ then return nil , msg end 119end 120-- Set the verification options 121succ , msg =optexec ( context . setverify , cfg . verify , ctx ) 122if not succ then return nil , msg end 123-- Set SSL options 124succ , msg =optexec ( context . setoptions , cfg . options , ctx ) 125if not succ then return nil , msg end 126-- Set the depth for certificate verification 127if cfg . depth then 128succ , msg =context . setdepth ( ctx , cfg . depth ) 129if not succ then return nil , msg end 130end 131 132-- NOTE: Setting DH parameters and elliptic curves needs to come after 133-- setoptions(), in case the user has specified the single_{dh,ecdh}_use 134-- options. 135 136-- Set DH parameters 137if cfg . dhparam then 138if type ( cfg . dhparam ) ~= "function" then 139return nil , "invalid DH parameter type" 140end 141context . setdhparam ( ctx , cfg . dhparam ) 142end 143 144-- Set elliptic curves 145if ( not config . algorithms . ec ) and ( cfg . curve or cfg . curveslist ) then 146return false , "elliptic curves not supported" 147end 148if config . capabilities . curves_list and cfg . curveslist then 149succ , msg =context . setcurveslist ( ctx , cfg . curveslist ) 150if not succ then return nil , msg end 151elseif cfg . curve then 152succ , msg =context . setcurve ( ctx , cfg . curve ) 153if not succ then return nil , msg end 154end 155 156-- Set extra verification options 157if cfg . verifyext and ctx . setverifyext then 158succ , msg =optexec ( ctx . setverifyext , cfg . verifyext , ctx ) 159if not succ then return nil , msg end 160end 161 162-- ALPN 163if cfg . mode == "server" and cfg . alpn then 164if type ( cfg . alpn ) == "function" then 165local alpncb =cfg . alpn 166-- This callback function has to return one value only 167succ , msg =context . setalpncb ( ctx , function ( str ) 168local protocols =alpncb ( wireformat2array ( str )) 169if type ( protocols ) == "string" then 170protocols ={ protocols } 171elseif type ( protocols ) ~= "table" then 172return nil 173end 174return ( array2wireformat ( protocols )) -- use "()" to drop error message 175end ) 176if not succ then return nil , msg end 177elseif type ( cfg . alpn ) == "table" then 178local protocols =cfg . alpn 179-- check if array is valid before use it 180succ , msg =array2wireformat ( protocols ) 181if not succ then return nil , msg end 182-- This callback function has to return one value only 183succ , msg =context . setalpncb ( ctx , function () 184return ( array2wireformat ( protocols )) -- use "()" to drop error message 185end ) 186if not succ then return nil , msg end 187else 188return nil , "invalid ALPN parameter" 189end 190elseif cfg . mode == "client" and cfg . alpn then 191local alpn 192if type ( cfg . alpn ) == "string" then 193alpn , msg =array2wireformat ( { cfg . alpn } ) 194elseif type ( cfg . alpn ) == "table" then 195alpn , msg =array2wireformat ( cfg . alpn ) 196else 197return nil , "invalid ALPN parameter" 198end 199if not alpn then return nil , msg end 200succ , msg =context . setalpn ( ctx , alpn ) 201if not succ then return nil , msg end 202end 203 204if config . capabilities . dane and cfg . dane then 205if type ( cfg . dane ) == "table" then 206context . setdane ( ctx , unpack ( cfg . dane )) 207else 208context . setdane ( ctx ) 209end 210end 211 212return ctx 213end 214 215-- 216-- 217-- 218local function wrap ( sock , cfg ) 219local ctx , msg 220if type ( cfg ) == "table" then 221ctx , msg =newcontext ( cfg ) 222if not ctx then return nil , msg end 223else 224ctx =cfg 225end 226local s , msg =core . create ( ctx ) 227if s then 228core . setfd ( s , sock : getfd ()) 229sock : setfd ( core . SOCKET_INVALID ) 230registry [ s ] =ctx 231return s 232end 233return nil , msg 234end 235 236-- 237-- Extract connection information. 238-- 239local function info ( ssl , field ) 240local str , comp , err , protocol 241comp , err =core . compression ( ssl ) 242if err then 243return comp , err 244end 245-- Avoid parser 246if field == "compression" then 247return comp 248end 249local info ={ compression =comp } 250str , info . bits , info . algbits , protocol =core . info ( ssl ) 251if str then 252info . cipher , info . protocol , info . key , 253info . authentication , info . encryption , info . mac = 254string . match ( str , 255"^(%S+)%s+(%S+)%s+Kx=(%S+)%s+Au=(%S+)%s+Enc=(%S+)%s+Mac=(%S+)" ) 256info . export =( string . match ( str , "%sexport%s*$" ) ~= nil ) 257end 258if protocol then 259info . protocol =protocol 260end 261if field then 262return info [ field ] 263end 264-- Empty? 265return ( ( next ( info )) and info ) 266end 267 268-- 269-- Set method for SSL connections. 270-- 271core . setmethod ( "info" , info ) 272 273-------------------------------------------------------------------------------- 274-- Export module 275-- 276 277local _M ={ 278_VERSION ="1.2.0" , 279_COPYRIGHT =core . copyright (), 280config =config , 281loadcertificate =x509 . load , 282newcontext =newcontext , 283wrap =wrap , 284} 285 286return _M