Plan 9(9legacy)ã¯ãå®å®çã®ããããå½ã¦ã¦ãTLS_RSA_WITH_AES_128_CBC_SHA256ã¾ã§ãã対å¿ãã¦ãªããããããå¤ããªã£ã¦ãã¦ãã¾ãã9frontã¯ãã£ã¨å¼·ãæå·ã¹ã¤ã¼ãã«å¯¾å¿ãã¦ããã®ã§ãå¿ è¦ãªãã®ã移æ¤ãããã¨æãã¾ãããã¨ã¯ããTLSã«ã¤ãã¦è©³ãããªãã®ã§ãä½ãã©ãé¢é£ãã¦ããã®ããä¸å¿ã«æ¢åå®è£ ãèªãã ã¡ã¢ã§ãã
TLSã®æ¦è¦
TLSã¯ã以ä¸ã®è¦ç´ (æå·ã¹ã¤ã¼ã)ã§æ§æããã¦ããã
- éµäº¤æ(æå·åã§å©ç¨ããå ±ééµã®äº¤ææ¹æ³)
- èªè¨¼(ãªããã¾ãé²æ¢)
- ããã§è¨¼ææ¸ã«å«ã¾ããå ¬ééµã®ç¨®é¡ã決ã¾ã
- æå·å
- ã¡ãã»ã¼ã¸èªè¨¼(æ¹ããé²æ¢ãããã·ã¥)
RFC 5246 - TLS Protocol Version 1.2ã§ã¯ãããããä¸ã¤ã®æååã«é£çµãã¦ãä¾ãã°TLS_RSA_WITH_AES_128_CBC_SHA256ã®ãããªIDã¨ãã¦è¡¨ç¾ãã¦ããããã®IDã¯TLS 1.2ã®å ´åã
// TLS 1.2 TLS_[éµäº¤æ]_[èªè¨¼]_WITH_[å ±ééµæå·]_[ã¡ãã»ã¼ã¸èªè¨¼]
ã®ããã«ãããããã®ä½ç½®ã«å¯¾å¿ããã¢ã«ã´ãªãºã ãå½ã¦ã¯ããããã®ã¨ãéµäº¤æã¨èªè¨¼ãã©ã¡ããRSAã®å ´åã¯TLS_RSA_WITH_ã¨ã¾ã¨ããããããã ã£ãããã®è¨äºã§ã¯ããã¾ã§è§¦ããªãããTLS 1.3ã®å ´åã¯å¤§å¹ ã«ç°¡ç´ åããã¦
// TLS 1.3 TLS_[AEAD]_[HASH]
ã¨ãªã£ããAEADã¯éã«ããã¨å ±ééµæå·ã¨ã¡ãã»ã¼ã¸èªè¨¼ãåæã«è¡ãã¢ã«ã´ãªãºã ã§ãAES_128_GCMãªã©ãããããã®ããTLS 1.3ã§ã¯ãå ·ä½çã«ã¯TLS_AES_128_GCM_SHA256ã®ãããªIDã¨ãªãã
TLSã¯ã¾ããè¤æ°ã®ãããã³ã«å±¤ãæã£ã¦ããã
- TLSãã³ãã·ã§ã¤ã¯ãããã³ã«
- ãã³ãã·ã§ã¤ã¯ãããã³ã«
- æå·ä»æ§å¤æ´ãããã³ã«
- ã¢ã©ã¼ããããã³ã«
- ã¢ããªã±ã¼ã·ã§ã³ãã¼ã¿ãããã³ã«
- TLSã¬ã³ã¼ããããã³ã«
TLSä¸è¬çãªè©±ã¯ãã®è¨äºãåãããããã£ãã
- Webåå¿è ã®çºã®SSL/TLSã¾ã¨ã
- ç解ãã¦ãã¤ããã® SSL/TLS ã§ãããã£ã¨ç解ãããé¢ç½ãã£ã話
Plan 9ã§ã®å®é
Plan 9ã§ã¯ãè¤æ°ã®ã³ã³ãã¼ãã³ããé¢ãã£ã¦TLSãå®ç¾ãã¦ãããå ·ä½çã«ã¯ä»¥ä¸ã®3ã¤ã
- ã«ã¼ãã«(devtlsãã©ã¤ã)
- libsec
- factotum
ã¾ãã¯ç°¡åã«TLSæ¥ç¶ã®æµãã追ãã
äºåæºå
ãµã¼ãã¯ããµã¼ã証ææ¸ããã¡ã¤ã«ã·ã¹ãã (ä¾ãã°/sys/lib/tls/cert.pem)ã«ä¿åãã¦ããã対å¿ããç§å¯éµããã¹ããªã¼ãã¼(æ®éã¯bootes)ã®factotumã«ãã¼ããã¦ãããfactotumã®ãã¼ã¿ã¯ãã¹ãã®åèµ·åã«ãã£ã¦æ®çºããã®ã§ãä¸è¬çã«ã¯ãåèµ·åæã«secstoreããèªã¿è¾¼ãããã«æ§æããã
% auth/secstore -G factotum % cat /mnt/factotum/ctl key proto=rsa service=tls !p=... !q=...
éµäº¤æã¨ã·ã¼ã¯ã¬ããçæ
TLSæ¥ç¶ãéå§ããã¨ããã¯ã©ã¤ã¢ã³ãã§ããµã¼ãã§ããlibsecã®é¢æ°ã使ããèªèº«ããµã¼ãã¨ãªãå ´åã¯tlsServerã使ããã¯ã©ã¤ã¢ã³ãã¨ãªãå ´åã¯tlsClientã使ãã
#include <mp.h> #include <libsec.h> /* fdã¯TLSéä¿¡ç¸æã¨ã®ã³ãã¯ã·ã§ã³ */ int tlsClient(int fd, TLSconn *conn); int tlsServer(int fd, TLSconn *conn);
ä¸è¨ã©ã¡ãã®é¢æ°ãã渡ããfdã®å 容ãTLSã§å ãã æ°ãããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ãè¿ããé¢æ°ããè¿ããããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã¯ãæ»ã£ãæç¹ã§æå·éä¿¡ãéå§ããç¶æ ãªã®ã§ãå¼ã³åºããå´ã¯ãã®ã¾ã¾TLSã«ä¹ãããããã³ã«*1ãåãã°è¯ãã
tlsClientã¨tlsServerããã£ã¦ãããã¨ãããå°ã追ã£ã¦ããã¨ããµã¼ãã§ãã¯ã©ã¤ã¢ã³ãã§ãã渡ãããfdãä»ããTLSãã³ãã·ã§ã¤ã¯ã§æå·ã¹ã¤ã¼ãã®æ±ºå®ã¨ãããã«å¾ã£ãéµäº¤æãè¡ããã¾ãããµã¼ããªããäºåæºåãã§ç¨æãã¦ãããç§å¯éµãfactotumããåãåºãã¦ã·ã¼ã¯ã¬ããã®çæã«å©ç¨ãããããã§ãæå·ã¹ã¤ã¼ãã®ãã¡éµäº¤æã¨èªè¨¼ã¯å®äºãã¦ãã¦ãæå·ã¨ã¡ãã»ã¼ã¸èªè¨¼ã®ã¢ã«ã´ãªãºã ã決ã¾ã£ã¦ãã¦ãæå·åã§ä½¿ãã·ã¼ã¯ã¬ãããç¨æã§ãããã¨ã«ãªãã
次ã«ãtlsClientã¾ãã¯tlsServeré¢æ°ã¯ã«ã¼ãã«ã«å¦çãå¼ã渡ãã
ã«ã¼ãã«ã®å½¹å²
Plan 9ã§ã¯ãTLSã¬ã³ã¼ããããã³ã«ã¯ã«ã¼ãã«ã§å®è£ ãã¦ãããä¸è¨ã§æå·ã«å¿ è¦ãªå¤ã¯å ¨ã¦æ±ºã¾ã£ãã®ã§ãtlsClientã¾ãã¯tlsServerã¯ã«ã¼ãã«ãæä¾ãããã¡ã¤ã«ã«å¿ è¦ãªå¤ãæ¸ãè¾¼ããå®éã¯Cã§æ¸ããã¦ããããæ¬ä¼¼çã«ã¯ä»¥ä¸ã®ãããªå¦çãè¡ãã
n=`{cat '#a'/tls/clone} echo fd $fd >'#a'/tls/$n/ctl echo version $protoVersion >'#a'/tls/$n/ctl echo secret aes_128_cbc sha256 $isclient $secret >'#a'/tls/$n/ctl # '#a'/tls/encalgsã¨'#a'/tls/hashalgsãèªãã¨å©ç¨å¯è½ãªæå·é¢æ°ãããã·ã¥é¢æ°ãããã cat '#a'/tls/$n/hand # TLSãã³ãã·ã§ã¤ã¯ãããã³ã«ããå ´åã¯ãã®ãã¡ã¤ã«ãèªã¿æ¸ããã cat '#a'/tls/$n/data # TLSã¬ã³ã¼ããããã³ã«ããå ´åã¯ãã®ãã¡ã¤ã«ãèªã¿æ¸ããã
ã«ã¼ãã«ã«å®è£ ãããdevtlsãã©ã¤ãã¯ãæåã¯æå·åããªãããctlãã¡ã¤ã«ã«ã·ã¼ã¯ã¬ãããæ¸ãè¾¼ã¾ããå¾ã¯å ¨ã¦ã®ééãããã¼ã¿ãæå·åã¾ãã¯è¤åããã
å¼·ãæå·ã«å¯¾å¿ããã«ã¯
æåã«æ¸ããããã«ã9legacyã§ãµãã¼ãããã¦ããæå·ã¹ã¤ã¼ãã¯ãã¾ã ç¦æ¢ããã¦ã¯ããªããã®ã®æ¨å¥¨ãããªããªã£ã¦ããããªã®ã§ãã®ã¾ã¾ã§ã¯ã¾ããã®ã ããã©ããæ¹åããã¨è¯ãã®ããæå·ã«é¢ãããã®ã®ãããã°ã¯å°é£ãªã®ã§ãã¾ãã¯ç°¡åãªã¨ãããã対å¿ããã¨è¯ãã®ã ãããæåã¯TLS_RSA_WITH_AES_128_GCM_SHA256ã§AES_128_GCMã確èªãã¦ã次ã«TLS_DHE_RSA_WITH_AES_128_GCM_SHA256ã§DHEã«å¯¾å¿ããæ¹éã妥å½ã«æãããæå¤ã¨TLS_DHE_RSA_WITH_AES_256_GCM_SHA256ã®çµã¿åããã¯åå¨ããªãã£ãã
éµäº¤æ
DHEã¾ãã¯ECDHEã¯TLS 1.3ã§ãèªãããã¦ããã®ã§ããã®è¾ºããªãè¯ãã ããã¨æããéµäº¤æã¯libsecã®éµäº¤æå¦çã«è¿½å ããã°è¯ããããã°ããã ããªãã ããã©ãDHEã¨AES_128_CBCã®çµã¿åããã¯ãªãããã ã£ãã®ã§ãå ã«AES_128_GCM対å¿ãå¿ è¦ã ã¨æãã
èªè¨¼(証ææ¸)
ããã¯ãfactotumã«æ°ããprotoã追å ããå¿ è¦ããã£ã¦ãããããã¨ä»®ã«9legacyã¸ããããéã£ã¦ããã¼ã¸ããããã©ããããããªãããªã®ã§ãããã¨ãã¦ãæå¾ã«ããã
AEAD
AEADã¯èªè¨¼ä»ãæå·ã¨å¼ã°ãããã®ã§ãAES_128_GCMããã®ä¸ã¤ã
TLS 1.2ãããã³ã« Appendixã«ããã¨ã以åã¾ã§ã®TLSã§ã¯ãã¹ããªã¼ã æå·ã¨ãããã¯æå·ãèæ ®ããã¦ããã¨ããã«ãæ°ããAEADæå·ã追å ããããããã
TLS 1.2ã®ã¬ã³ã¼ã層ã§ã¯ã
struct ProtocolVersion { uint8 major; uint8 minor; }; enum { ChangeCipherSpec = 20, Alert = 21, Handshake = 22, ApplicationData = 23, } ContentType; /* æåã®å¹³æ */ struct TLSPlaintext { ContentType type; ProtocolVersion version; uint16 length; uchar opaque[]; }; /* æå·ããã¹ãã¯3ã¤ã«åå² */ struct GenericStreamCipher { /* ãã¾ãéè¦ã§ã¯ãªãã®ã§çç¥ */ }; struct GenericBlockCipher { uchar iv[]; /* ãã¼ã¿ã®é·ãã¯ã¢ã«ã´ãªãºã ã«ãã£ã¦æ±ºã¾ã */ uchar content[]; /* ãã¼ã¿ã®é·ãã¯TLSCiphertext.length */ uchar mac[]; /* ãã¼ã¿ã®é·ãã¯ã¢ã«ã´ãªãºã ã«ãã£ã¦æ±ºã¾ã */ uint8 padding[]; uint8 padding_length; }; struct GenericAEADCipher { uchar nonce[]; /* ãã¼ã¿ã®é·ãã¯ã¢ã«ã´ãªãºã ã«ãã£ã¦æ±ºã¾ã */ uchar content[]; /* ãã¼ã¿ã®é·ãã¯TLSCiphertext.length */ }; struct TLSCiphertext { ContentType type; ProtocolVersion version; uint16 length; union { GenericStreamCipher stream; GenericBlockCipher block; GenericAEADCipher aead; }; };
ã®ããã«åå²ãã¦ãã¦ãããã¾ã§ã®ãããã¯æå·ã§ã¯ãæå·åã¨ã¡ãã»ã¼ã¸èªè¨¼ãåãã¦è¨ç®ãã¦ããããåãã¦è¨ç®ãããã¨ã«ããåé¡ãããããã*2ãAEADã§ã¯æå·åã¨åæã«èªè¨¼ã¿ã°ã¨å¼ã°ããå¤ãçæããããã®èªè¨¼ã¿ã°ããè¤åæã«ã使ããã®ãããã
ãããã¯æå·ã®å ´å
AEADã¨æ¯ã¹ãããã«ãæåã«ãããã¯æå·ãã¿ãããããã¯æå·ã§æå·åããå ´åãã«ã¼ãã«ã¯ä¾ãã°ä»¥ä¸ã®ããã«ãã¼ã¿ãæå·åãããããã§ã¯ãæå·åã¯aes128_cbcã§ãã¡ãã»ã¼ã¸èªè¨¼ã¯sha256ã使ãã¨ä»®å®ããã
TLSCiphertext b; /* TLS 1.2 */ b.type = ApplicationData; b.version.major = 3; b.version.minor = 3; b.length = len(body); b.block.iv = (æåã¯ç©º); b.block.content = body; b.block.mac = (æåã¯ç©º); /* é¢åãªã®ã§ããã£ã³ã°ã¯çç¥ */ /* æªæå·å(ivã¨macã¯ç©º)ã®ç¶æ ã§ããã·ã¥å¤ãè¨ç®ãã */ b.block.mac = hmac_sha256(64bitã·ã¼ã±ã³ã¹çªå· + b); b.length += len(b.block.mac); /* æå·å */ b.block.iv = (ä¹±æ°çæ); /* é·ãã¯æå·é¢æ°ã«ä¾åãã; ä¾ãã°AESã¯16ãã¤ã */ b.length += len(b.block.iv); b.length = aes128_cbc(&b.block, b.length);
ããã§ãæçµçã«b.block
ã¯æå·åããã¦ãb.length
ã¯æå·åãããb.block
ã®é·ããæã¤ãããã§éè¦ãªã®ã¯å¹³æãSHA256ããããã·ã¥å¤ãå¹³æã®æ«å°¾ã«å ãã¦ããããæå·åãã¦ããã¨ããã§ãAEADã®å ´åã¯ãããç°ãªãã
AEAD(èªè¨¼ä»ãæå·)ã®å ´å
ããã§ã¯AES_128_GCMã使ãã¨ä»®å®ãã¦å ·ä½çãªåããã¿ãã
ã¾ãåæã¨ãã¦ãAES128-GCMã§ã¯
- å¹³æ
- åæåãã¯ãã«(IV)
- 追å ãã¼ã¿(aad)
- èªè¨¼ã«ã¯å©ç¨ããããæå·åã¯ããªããã¼ã¿
ãä¸ããã¨ã
- æå·æ
- èªè¨¼ã¿ã°
ãè¿ããPlan 9(9front)ã®å ´åã¯ä»¥ä¸ã®é¢æ°ãããã¿ã¤ããæã¤ã
#include <mp.h> #include <libsec.h> void setupAESGCMstate(AESGCMstate *s, uchar *key, int keylen, uchar *iv, int ivlen); void aesgcm_setiv(AESGCMstate *s, uchar *iv, int ivlen); void aesgcm_encrypt(uchar *p, ulong n, uchar *aad, ulong naad, uchar tag[16], AESGCMstate *s); int aesgcm_decrypt(uchar *p, ulong n, uchar *aad, ulong naad, uchar tag[16], AESGCMstate *s);
ããå°ãå ·ä½çãªã³ã¼ãã§ã¿ãã¨ã
TLSCiphertext b; /* TLS 1.2 */ b.type = ApplicationData; b.version.major = 3; b.version.minor = 3; b.length = len(body); aad = 64bitã·ã¼ã±ã³ã¹çªå· + b; /* [seq:8][type:1][major:1][minor:1][len:2]ã§13byte */ iv[4:12] ^= aad[0:8]; /* IVã®ä¸ä½4ãã¤ãã¯ãã®ã¾ã¾æ®ãã¦ãå¾ã8ãã¤ããNonceã§åãã */ aesgcm_setiv(state, iv, len(iv)); aesgcm_encrypt(body, len(body), aad, len(aad), &tag, state); b.nonce = iv[4:12]; /* æå·åã«ä½¿ã£ãivã®æ«å°¾8ãã¤ããã¡ãã»ã¼ã¸ã«å«ãã */ b.content = body + tag; /* æå·åããbodyã®å¾ãã«èªè¨¼ã¿ã°ãå ãã */ b.length = len(b.nonce) + len(body) + len(tag);
ããããããã¯ãã¨ã«è¨ç®ãããAEADã«é¢ããè¨äºã¯ãã®è¾ºããé¢ç½ãã
ã¡ãã»ã¼ã¸èªè¨¼ã®æå³
ã¨ããã§ãä¸è¨ã§ã¿ãããã«AEADæå·ã§ã¯ã¡ãã»ã¼ã¸èªè¨¼ãè¨ç®ããªããªã£ã¦ããã®ããããããªã®ã§Plan 9(9front)ã®devtlsã§ã¯ãaes_128_gcm_aeadã§æå·åããå ´åã®ããã·ã¥é¢æ°ã¯clearã使ãããã«ãªã£ã¦ãããã ãã©ãTLSã®æå·ã¹ã¤ã¼ãã«ã¯ä¾ç¶ã¨ãã¦TLS_RSA_WITH_AES_128_GCM_SHA256ã®ããã«SHA256ã¨ããååãæ®ã£ã¦ããããããã¯ã©ãã§ä½¿ã£ã¦ããã®ãã
RFC 5288 - AES Galois Counter Mode (GCM) Cipher Suites for TLSã«ããã¨ãTLS 1.2ã®å ´åã¯PRF(Pseudo Random Function)ã§ã ã使ãã¨æ¸ããã¦ããããªã®ã§éµäº¤æãçµãã£ã¦ãã¾ãã°ããã以é使ããããã¨ã¯ãªããããã
TLS 1.3ã®å ´åã¯ãHMACãã¼ã¹éµå°åºé¢æ°(hkdf)ã«ä½¿ããããã詳ããã¯èª¿ã¹ã¦ããªãã