package Ġsocket . tls Ċ Ċ import Ġmqtt . broker . Broker Ċ import Ġsocket . Server Socket Ċ import Ġsocket . Server Socket Loop Ċ import Ġsocket . tcp . Socket Ċ import Ġjava . io . ByteArray InputStream Ċ import Ġjava . io . File Ċ import Ġjava . nio . ByteBuffer Ċ import Ġjava . nio . channels . Selection Key Ċ import Ġjava . security . Key Factory Ċ import Ġjava . security . KeyStore Ċ import Ġjava . security . cert . Certificate Factory Ċ import Ġjava . security . spec . PKCS 8 Encoded KeySpec Ċ import Ġjava . security . spec . X 5 0 9 Encoded KeySpec Ċ import Ġjavax . net . ssl . Key Manager Ċ import Ġjavax . net . ssl . Key ManagerFactory Ċ import Ġjavax . net . ssl . SSL Context ĊĊ Ċ actual Ġclass ĠTLS Server Socket Ġactual Ġconstructor ( ĊĠĠĠ Ġprivate Ġval Ġbroker : ĠBroker , ĊĠĠĠ Ġprivate Ġval Ġselect Callback : Ġ( attachment : ĠAny ?, Ġstate : ĠServer Socket Loop . Socket State ) Ġ-> ĠBoolean Ċ ) Ġ: ĠServer Socket ( broker , Ġselect Callback ) Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġssl Context Ġ= ĠSSL Context . getInstance ( broker . tls Settings !!. version ) ĊĠĠĠ Ġprivate Ġvar Ġsend App Buffer : ĠByteBuffer ĊĠĠĠ Ġprivate Ġvar Ġreceive App Buffer : ĠByteBuffer ĊĊĠĠĠ Ġinit Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġkey Store Ġ= ĠKey Store . getInstance (" PKCS 1 2 ") ĊĠĠĠĠĠĠĠ ĠFile ( broker . tls Settings !!. key Store FilePath ). inputStream (). use Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġkey Store . load ( it , Ġbroker . tls Settings . key Store Password ?. toCharArray ()) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġkey ManagerFactory Ġ= ĠKey ManagerFactory . getInstance ( Key ManagerFactory . getDefault Algorithm ()) ĊĠĠĠĠĠĠĠ Ġkey ManagerFactory . init ( key Store , Ġbroker . tls Settings . key Store Password ?. toCharArray ()) ĊĊĠĠĠĠĠĠĠ Ġssl Context . init ( key ManagerFactory . key Managers , Ġnull , Ġnull ) ĊĊĠĠĠĠĠĠĠ Ġval Ġinit Session Ġ= Ġssl Context . create SSL Engine (). session ĊĠĠĠĠĠĠĠ Ġsend Buffer Ġ= ĠByteBuffer . allocate ( init Session . packet BufferSize ) ĊĠĠĠĠĠĠĠ Ġreceive Buffer Ġ= ĠByteBuffer . allocate ( init Session . packet BufferSize ) ĊĠĠĠĠĠĠĠ Ġsend App Buffer Ġ= ĠByteBuffer . allocate ( init Session . application BufferSize ) ĊĠĠĠĠĠĠĠ Ġreceive App Buffer Ġ= ĠByteBuffer . allocate ( init Session . application BufferSize ) ĊĠĠĠĠĠĠĠ Ġinit Session . invalidate () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġbuild Key Managers ( ĊĠĠĠĠĠĠĠ Ġcertificate Bytes : ĠByteArray , ĊĠĠĠĠĠĠĠ Ġkey Algorithm : ĠString , ĊĠĠĠĠĠĠĠ ĠprivateKey Bytes : ĠByteArray , ĊĠĠĠĠĠĠĠ ĠpublicKey Bytes : ĠByteArray , ĊĠĠĠĠĠĠĠ ĠprivateKey Password : ĠString ? ĊĠĠĠ Ġ): ĠArray < Key Manager > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġkey Store Ġ= ĠKey Store . getInstance ( KeyStore . getDefault Type ()) ĊĠĠĠĠĠĠĠ Ġkey Store . load ( null ) ĊĠĠĠĠĠĠĠ Ġval Ġcertificate Factory Ġ= ĠCertificate Factory . getInstance (" X . 5 0 9 ") ĊĠĠĠĠĠĠĠ Ġval Ġcertificate Ġ= Ġcertificate Factory . generate Certificate ( ByteArray InputStream ( certificate Bytes )) ĊĊĠĠĠĠĠĠĠ Ġval Ġkey Factory Ġ= ĠKey Factory . getInstance ( key Algorithm ) ĊĠĠĠĠĠĠĠ Ġval ĠprivateKey Ġ= Ġkey Factory . generate Private ( PKCS 8 Encoded KeySpec ( privateKey Bytes )) ĊĠĠĠĠĠĠĠ Ġval ĠpublicKey Ġ= Ġkey Factory . generate Public ( X 5 0 9 Encoded KeySpec ( publicKey Bytes )) ĊĠĠĠĠĠĠĠ Ġcertificate . verify ( publicKey ) ĊĊĠĠĠĠĠĠĠ Ġkey Store . set Certificate Entry (" main ", Ġcertificate ) ĊĠĠĠĠĠĠĠ Ġkey Store . set Key Entry (" main ", ĠprivateKey , ĠprivateKey Password ?. toCharArray (), ĠarrayOf ( certificate )) ĊĊĠĠĠĠĠĠĠ Ġval Ġk mf Ġ= ĠKey ManagerFactory . getInstance ( Key ManagerFactory . getDefault Algorithm ()) ĊĠĠĠĠĠĠĠ Ġk mf . init ( key Store , Ġnull ) ĊĠĠĠĠĠĠĠ Ġreturn Ġk mf . key Managers ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġcreate Socket ( socket Key : ĠSelection Key ): ĠSocket Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġengine Ġ= Ġssl Context . create SSL Engine () ĊĠĠĠĠĠĠĠ Ġengine . use Client Mode Ġ= Ġfalse ĊĠĠĠĠĠĠĠ Ġif Ġ( broker . tls Settings ?. require Client Certificate Ġ== Ġtrue ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġengine . need Client Auth Ġ= Ġtrue ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġengine . begin Handshake () ĊĠĠĠĠĠĠĠ Ġreturn ĠTL SS ocket ( socket Key , Ġsend Buffer , Ġreceive Buffer , Ġsend App Buffer , Ġreceive App Buffer , Ġengine ) ĊĠĠĠ Ġ} Ċ } Ċ