package Ġkon em . netty . stream Ċ Ċ import Ġio . netty . handler . ssl . Ssl Context Ċ import Ġio . netty . handler . ssl . Ssl Context Builder Ċ import Ġio . netty . handler . ssl . util . In secure Trust ManagerFactory Ċ import Ġio . netty . handler . ssl . util . Self Signed Certificate Ċ import Ġjava . io . File Ċ import Ġjava . io . FileInputStream Ċ import Ġjava . io . FileNotFoundException Ċ import Ġjava . security . KeyStore Ċ import Ġjavax . net . ssl . Key ManagerFactory Ċ import Ġjavax . net . ssl . Trust ManagerFactory Ċ import Ġorg . slf 4 j . LoggerFactory Ċ Ċ object ĠS sl Context Manager Ġ{ ĊĊĠ Ġprivate Ġval Ġlogger Ġ= ĠLoggerFactory . getLogger (" Ssl Context Manager ") ĊĊĠ Ġprivate Ġval Ġuse Ssl Ġ= ĠSystem . getProperty (" kon em . secure . secure ", Ġ" true ") ĊĠ Ġprivate Ġval Ġprotocol Ġ= ĠSystem . getProperty (" kon em . secure . protocol ", Ġ" TLS ") ĊĠ Ġprivate Ġval Ġalgorithm Ġ= ĠSystem . getProperty (" kon em . secure . algorithm ", Ġ" Sun X 5 0 9 ") ĊĠ Ġprivate Ġval Ġkey Store Location Ġ= ĠSystem . getProperty (" kon em . secure . key Store Location ", Ġ"") ĊĠ Ġprivate Ġval Ġkey Store Type Ġ= ĠSystem . getProperty (" kon em . secure . key Store Type ", Ġ" J KS ") ĊĠ Ġprivate Ġval Ġkey Store Password Ġ= ĠSystem . getProperty (" kon em . secure . key Store Password ", Ġ"") ĊĠ Ġprivate Ġval Ġcertificate Password Ġ= ĠSystem . getProperty (" kon em . secure . certificate Password ", Ġ"") ĊĊĠ Ġprivate Ġlateinit Ġvar Ġkey Store : ĠKey Store ĊĠ Ġprivate Ġlateinit Ġvar Ġtrust Factory : ĠTrust ManagerFactory ĊĊĠ Ġprivate Ġlateinit Ġvar Ġserver Context : ĠS sl Context ĊĠ Ġprivate Ġlateinit Ġvar Ġclient Context : ĠS sl Context ĊĊĠ Ġfun Ġget ServerContext (): ĠS sl Context Ġ{ ĊĠĠĠ Ġsynchronized ( this ) Ġ{ ĊĠĠĠĠĠ Ġensure Initialized () ĊĠĠĠĠĠ Ġreturn Ġserver Context ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġfun Ġget ClientContext (): ĠS sl Context Ġ{ ĊĠĠĠ Ġsynchronized ( this ) Ġ{ ĊĠĠĠĠĠ Ġensure Initialized () ĊĠĠĠĠĠ Ġreturn Ġclient Context ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġensure Initialized () Ġ{ ĊĠĠĠ Ġif Ġ(! this :: server Context . isInitialized Ġ|| Ġ! this :: client Context . isInitialized ) Ġ{ ĊĠĠĠĠĠ Ġkey Store Ġ= ĠKey Store . getInstance ( key Store Type ) ĊĠĠĠĠĠ Ġtrust Factory Ġ= ĠTrust ManagerFactory . getInstance ( algorithm ) ĊĠĠĠĠĠ Ġif Ġ( key Store Location Ġ!= Ġ" not Set ") Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġtemp Ġ= ĠFile ( key Store Location ) ĊĠĠĠĠĠĠĠ Ġlogger . info (" key Store Location Ġis Ġset Ġto Ġ${ temp . absolute Path }") ĊĠĠĠĠĠĠĠ Ġval Ġcert Ġ= Ġload Certificate () ĊĠĠĠĠĠĠĠ Ġif Ġ( cert Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġlogger . info (" loading Ġ$ key Store Location ") ĊĠĠĠĠĠĠĠĠĠ Ġkey Store . load ( cert , Ġkey Store Password . toCharArray ()) ĊĠĠĠĠĠĠĠĠĠ Ġval Ġk mf Ġ= ĠKey ManagerFactory . getInstance ( algorithm ) ĊĠĠĠĠĠĠĠĠĠ Ġk mf . init ( key Store , Ġkey Store Password . toCharArray ()) ĊĠĠĠĠĠĠĠĠĠ Ġtrust Factory . init ( key Store ) ĊĠĠĠĠĠĠĠĠĠ Ġserver Context Ġ= ĠS sl Context Builder . for Server ( k mf ). build () ĊĠĠĠĠĠĠĠĠĠ Ġclient Context Ġ= ĠS sl Context Builder . for Client (). trust Manager ( trust Factory ). build () ĊĠĠĠĠĠĠĠĠĠ Ġcert . close () ĊĠĠĠĠĠĠĠĠĠ Ġreturn ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġlogger . info (" Loading Ġself Ġsigned Ġcert ") ĊĠĠĠĠĠ Ġval Ġs sc Ġ= ĠSelf Signed Certificate () ĊĠĠĠĠĠ Ġserver Context Ġ= ĠS sl Context Builder . for Server ( ss c . certificate (), Ġs sc . privateKey ()). build () ĊĠĠĠĠĠ Ġclient Context Ġ= ĠS sl Context Builder . for Client (). trust Manager ( In secure Trust ManagerFactory . INSTANCE ). build () ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġload Certificate (): ĠFileInputStream ? Ġ{ ĊĠĠĠ Ġreturn Ġtry Ġ{ ĊĠĠĠĠĠ ĠFile ( key Store Location ). inputStream () ĊĠĠĠ Ġ} Ġcatch Ġ( e : ĠFileNotFoundException ) Ġ{ ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ