Slide 1

Slide 1 text

Gunosy.go #2 import “crypto” Masakazu Ishibashi @studiomaestro

Slide 2

Slide 2 text

Contents • ߏ੒ • ͬ͟ͱઆ໌ • ͓·͚

Slide 3

Slide 3 text

ߏ੒ crypto aes cipher des dsa ecdsa elliptic hmac md5 rand rc4 rsa sha1 sha256 sha512 subtle tls x509 pkix ѹ౗త αϒύοέʔδ਺

Slide 4

Slide 4 text

ߏ੒ crypto aes cipher des dsa ecdsa elliptic hmac md5 rand rc4 rsa sha1 sha256 sha512 subtle tls x509 pkix ڞ௨伴҉߸

Slide 5

Slide 5 text

ߏ੒ crypto aes cipher des dsa ecdsa elliptic hmac md5 rand rc4 rsa sha1 sha256 sha512 subtle tls x509 pkix ެ։伴҉߸

Slide 6

Slide 6 text

ߏ੒ crypto aes cipher des dsa ecdsa elliptic hmac md5 rand rc4 rsa sha1 sha256 sha512 subtle tls x509 pkix ϋογϡ

Slide 7

Slide 7 text

ߏ੒ crypto aes cipher des dsa ecdsa elliptic hmac md5 rand rc4 rsa sha1 sha256 sha512 subtle tls x509 pkix ཚ਺

Slide 8

Slide 8 text

ߏ੒ crypto aes cipher des dsa ecdsa elliptic hmac md5 rand rc4 rsa sha1 sha256 sha512 subtle tls x509 pkix tlsͱ͔x509ͱ͔

Slide 9

Slide 9 text

ߏ੒ crypto aes cipher des dsa ecdsa elliptic hmac md5 rand rc4 rsa sha1 sha256 sha512 subtle tls x509 pkix ఆ਺ͳͲ

Slide 10

Slide 10 text

༻్ ڞ௨伴҉߸/ެ։伴҉߸/σδλϧॺ໊ ϋογϡ/ཚ਺ੜ੒/X.509/PKI/TLS OpenSSL / JCE ͱ͔ͰͰ͖Δ͜ͱ͕ͦͦ͜͜Ͱ͖Δ

Slide 11

Slide 11 text

ߏ੒ crypto aes cipher des dsa ecdsa elliptic hmac md5 rand rc4 rsa sha1 sha256 sha512 subtle tls x509 pkix ڞ௨伴҉߸

Slide 12

Slide 12 text

ڞ௨伴҉߸ func NewCipher(key []byte) (cipher.Block, error) type KeySizeError func (k KeySizeError) Error() string crypto/aes crypto/des func NewCipher(key []byte) (cipher.Block, error) func NewTripleDESCipher(key []byte) (cipher.Block, error) type KeySizeError func (k KeySizeError) Error() string

Slide 13

Slide 13 text

ڞ௨伴҉߸ crypto/cipher type AEAD func NewGCM(cipher Block) (AEAD, error) type Block type BlockMode func NewCBCDecrypter(b Block, iv []byte) BlockMode func NewCBCEncrypter(b Block, iv []byte) BlockMode type Stream func NewCFBDecrypter(block Block, iv []byte) Stream func NewCFBEncrypter(block Block, iv []byte) Stream func NewCTR(block Block, iv []byte) Stream func NewOFB(b Block, iv []byte) Stream type StreamReader func (r StreamReader) Read(dst []byte) (n int, err error) type StreamWriter func (w StreamWriter) Close() error func (w StreamWriter) Write(src []byte) (n int, err error)

Slide 14

Slide 14 text

ڞ௨伴҉߸ crypto/cipher type AEAD func NewGCM(cipher Block) (AEAD, error) type Block type BlockMode func NewCBCDecrypter(b Block, iv []byte) BlockMode func NewCBCEncrypter(b Block, iv []byte) BlockMode type Stream func NewCFBDecrypter(block Block, iv []byte) Stream func NewCFBEncrypter(block Block, iv []byte) Stream func NewCTR(block Block, iv []byte) Stream func NewOFB(b Block, iv []byte) Stream type StreamReader func (r StreamReader) Read(dst []byte) (n int, err error) type StreamWriter func (w StreamWriter) Close() error func (w StreamWriter) Write(src []byte) (n int, err error) Block Cipher Mode ผʹ࢖͍෼͚Δɻ ʢॲཧ୯Ґ͸Blockʣ http://play.golang.org/p/zjrQdSnz2M αϯϓϧ

Slide 15

Slide 15 text

ڞ௨伴҉߸ crypto/rc4 type Cipher func NewCipher(key []byte) (*Cipher, error) func (c *Cipher) Reset() func (c *Cipher) XORKeyStream(dst, src []byte) type KeySizeError func (k KeySizeError) Error() string

Slide 16

Slide 16 text

ڞ௨伴҉߸ crypto/rc4 type Cipher func NewCipher(key []byte) (*Cipher, error) func (c *Cipher) Reset() func (c *Cipher) XORKeyStream(dst, src []byte) type KeySizeError func (k KeySizeError) Error() string ετϦʔϜ҉߸ͷ ΠϯλʔϑΣʔε http://play.golang.org/p/7zs6j2GwK7 αϯϓϧ

Slide 17

Slide 17 text

ߏ੒ crypto aes cipher des dsa ecdsa elliptic hmac md5 rand rc4 rsa sha1 sha256 sha512 subtle tls x509 pkix ެ։伴҉߸

Slide 18

Slide 18 text

ެ։伴҉߸ crypto/rsa func DecryptOAEP(hash hash.Hash, random io.Reader, priv *PrivateKey, ciphertext []byte, label []byte) (msg []byte, err error) func DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (out []byte, err error) func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []byte, key []byte) (err error) func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, label []byte) (out []byte, err error) func EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) (out []byte, err error) func SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte) (s []byte, err error) func SignPSS(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte, opts *PSSOptions) (s []byte, err error) func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte) (err error) func VerifyPSS(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte, opts *PSSOptions) error type CRTValue type PSSOptions type PrecomputedValues type PrivateKey func GenerateKey(random io.Reader, bits int) (priv *PrivateKey, err error) func GenerateMultiPrimeKey(random io.Reader, nprimes int, bits int) (priv *PrivateKey, err error) func (priv *PrivateKey) Precompute() func (priv *PrivateKey) Validate() error type PublicKey

Slide 19

Slide 19 text

ެ։伴҉߸ crypto/rsa func DecryptOAEP(hash hash.Hash, random io.Reader, priv *PrivateKey, ciphertext []byte, label []byte) (msg []byte, err error) func DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (out []byte, err error) func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []byte, key []byte) (err error) func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, label []byte) (out []byte, err error) func EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) (out []byte, err error) func SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte) (s []byte, err error) func SignPSS(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte, opts *PSSOptions) (s []byte, err error) func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte) (err error) func VerifyPSS(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte, opts *PSSOptions) error type CRTValue type PSSOptions type PrecomputedValues type PrivateKey func GenerateKey(random io.Reader, bits int) (priv *PrivateKey, err error) func GenerateMultiPrimeKey(random io.Reader, nprimes int, bits int) (priv *PrivateKey, err error) func (priv *PrivateKey) Precompute() func (priv *PrivateKey) Validate() error type PublicKey ҉߸/ෳ߹ PKCSv1.5, OEAP ॺ໊/ݕূ (PKCSv1.5,PSS) ެ։伴/ൿີ伴

Slide 20

Slide 20 text

ެ։伴҉߸ crypto/dsa func GenerateKey(priv *PrivateKey, rand io.Reader) error func GenerateParameters(params *Parameters, rand io.Reader, sizes ParameterSizes) (err error) func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error) func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool type ParameterSizes type Parameters type PrivateKey type PublicKey crypto/ecdsa func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error) func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool type PrivateKey func GenerateKey(c elliptic.Curve, rand io.Reader) (priv *PrivateKey, err error) type PublicKey

Slide 21

Slide 21 text

ߏ੒ crypto aes cipher des dsa ecdsa elliptic hmac md5 rand rc4 rsa sha1 sha256 sha512 subtle tls x509 pkix ϋογϡ

Slide 22

Slide 22 text

ϋογϡ crypto/md5 func New() hash.Hash func Sum(data []byte) [Size]byte crypto/sha1 func New() hash.Hash func Sum(data []byte) [Size]byte crypto/sha256 func New() hash.Hash func New224() hash.Hash func Sum224(data []byte) (sum224 [Size224]byte) func Sum256(data []byte) [Size]byte func New() hash.Hash func New384() hash.Hash func Sum384(data []byte) (sum384 [Size384]byte) func Sum512(data []byte) [Size]byte crypto/sha512

Slide 23

Slide 23 text

ϋογϡ crypto/md5 func New() hash.Hash func Sum(data []byte) [Size]byte crypto/sha1 func New() hash.Hash func Sum(data []byte) [Size]byte crypto/sha256 func New() hash.Hash func New224() hash.Hash func Sum224(data []byte) (sum224 [Size224]byte) func Sum256(data []byte) [Size]byte func New() hash.Hash func New384() hash.Hash func Sum384(data []byte) (sum384 [Size384]byte) func Sum512(data []byte) [Size]byte crypto/sha512 func hash(c string) string { h := sha256.New() fmt.Fprint(h, c) return hex.EncodeToString(h.Sum(nil)) } ͜Μͳײ͡ͷ Α͘࢖͍ͦ͏

Slide 24

Slide 24 text

HMAC crypto/hmac func Equal(mac1, mac2 []byte) bool func New(h func() hash.Hash, key []byte) hash.Hash

Slide 25

Slide 25 text

ߏ੒ crypto aes cipher des dsa ecdsa elliptic hmac md5 rand rc4 rsa sha1 sha256 sha512 subtle tls x509 pkix ཚ਺

Slide 26

Slide 26 text

ཚ਺ੜ੒ crypto/rand func Int(rand io.Reader, max *big.Int) (n *big.Int, err error) func Prime(rand io.Reader, bits int) (p *big.Int, err error) func Read(b []byte) (n int, err error)

Slide 27

Slide 27 text

ཚ਺ੜ੒ crypto/rand func Int(rand io.Reader, max *big.Int) (n *big.Int, err error) func Prime(rand io.Reader, bits int) (p *big.Int, err error) func Read(b []byte) (n int, err error) math/rand func ExpFloat64() float64 func Float32() float32 func Float64() float64 func Int() int func Int31() int32 func Int31n(n int32) int32 func Int63() int64 func Int63n(n int64) int64 func Intn(n int) int func NormFloat64() float64 func Perm(n int) []int func Seed(seed int64) func Uint32() uint32 go্ͷ࣮૷ /dev/urandom

Slide 28

Slide 28 text

ߏ੒ crypto aes cipher des dsa ecdsa elliptic hmac md5 rand rc4 rsa sha1 sha256 sha512 subtle tls x509 pkix tlsͱ͔x509ͱ͔

Slide 29

Slide 29 text

X.509 crypto/x509 type CertPool func NewCertPool() *CertPool func (s *CertPool) AddCert(cert *Certificate) func (s *CertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool) func (s *CertPool) Subjects() (res [][]byte) type Certificate func ParseCertificate(asn1Data []byte) (*Certificate, error) func (c *Certificate) CheckCRLSignature(crl *pkix.CertificateList) (err error) func (c *Certificate) CheckSignature(algo SignatureAlgorithm, signed, signature []byte) (err error) func (c *Certificate) CheckSignatureFrom(parent *Certificate) (err error) func (c *Certificate) CreateCRL(rand io.Reader, priv interface{}, revokedCerts []pkix.RevokedCertificate, now, expiry time.Time) (crlBytes []byte, err error) func (c *Certificate) Equal(other *Certificate) bool func (c *Certificate) Verify(opts VerifyOptions) (chains [][]*Certificate, err error) func (c *Certificate) VerifyHostname(h string) error type ExtKeyUsage type KeyUsage type PEMCipher type PublicKeyAlgorithm type SignatureAlgorithm

Slide 30

Slide 30 text

X.509 crypto/x509 type CertPool func NewCertPool() *CertPool func (s *CertPool) AddCert(cert *Certificate) func (s *CertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool) func (s *CertPool) Subjects() (res [][]byte) type Certificate func ParseCertificate(asn1Data []byte) (*Certificate, error) func (c *Certificate) CheckCRLSignature(crl *pkix.CertificateList) (err error) func (c *Certificate) CheckSignature(algo SignatureAlgorithm, signed, signature []byte) (err error) func (c *Certificate) CheckSignatureFrom(parent *Certificate) (err error) func (c *Certificate) CreateCRL(rand io.Reader, priv interface{}, revokedCerts []pkix.RevokedCertificate, now, expiry time.Time) (crlBytes []byte, err error) func (c *Certificate) Equal(other *Certificate) bool func (c *Certificate) Verify(opts VerifyOptions) (chains [][]*Certificate, err error) func (c *Certificate) VerifyHostname(h string) error type ExtKeyUsage type KeyUsage type PEMCipher type PublicKeyAlgorithm type SignatureAlgorithm ূ໌ॻ͓Αͼ ϓʔϧ ʢෳ਺ͷূ໌ॻʣ ূ໌ॻͷ߲໨

Slide 31

Slide 31 text

X.509 crypto/x509 type CertificateInvalidError func (e CertificateInvalidError) Error() string type ConstraintViolationError func (ConstraintViolationError) Error() string type HostnameError func (h HostnameError) Error() string type InvalidReason type SystemRootsError func (e SystemRootsError) Error() string type UnhandledCriticalExtension func (h UnhandledCriticalExtension) Error() string type UnknownAuthorityError func (e UnknownAuthorityError) Error() string type VerifyOptions

Slide 32

Slide 32 text

X.509 crypto/x509 func CreateCertificate(rand io.Reader, template, parent *Certificate, pub interface{}, priv interface{}) (cert []byte, err error) func DecryptPEMBlock(b *pem.Block, password []byte) ([]byte, error) func EncryptPEMBlock(rand io.Reader, blockType string, data, password []byte, alg PEMCipher) (*pem.Block, error) func IsEncryptedPEMBlock(b *pem.Block) bool func MarshalECPrivateKey(key *ecdsa.PrivateKey) ([]byte, error) func MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte func MarshalPKIXPublicKey(pub interface{}) ([]byte, error) func ParseCRL(crlBytes []byte) (certList *pkix.CertificateList, err error) func ParseCertificates(asn1Data []byte) ([]*Certificate, error) func ParseDERCRL(derBytes []byte) (certList *pkix.CertificateList, err error) func ParseECPrivateKey(der []byte) (key *ecdsa.PrivateKey, err error) func ParsePKCS1PrivateKey(der []byte) (key *rsa.PrivateKey, err error) func ParsePKCS8PrivateKey(der []byte) (key interface{}, err error) func ParsePKIXPublicKey(derBytes []byte) (pub interface{}, err error) ূ໌ॻ࡞੒ ಡΈॻ͖

Slide 33

Slide 33 text

PKIX crypto/pkix type AlgorithmIdentifier type AttributeTypeAndValue type CertificateList func (certList *CertificateList) HasExpired(now time.Time) bool type Extension type Name func (n *Name) FillFromRDNSequence(rdns *RDNSequence) func (n Name) ToRDNSequence() (ret RDNSequence) type RDNSequence type RelativeDistinguishedNameSET type RevokedCertificate type TBSCertificateList

Slide 34

Slide 34 text

TLS crypto/tls func Listen(network, laddr string, config *Config) (net.Listener, error) func NewListener(inner net.Listener, config *Config) net.Listener type Certificate func LoadX509KeyPair(certFile, keyFile string) (cert Certificate, err error) func X509KeyPair(certPEMBlock, keyPEMBlock []byte) (cert Certificate, err error) type ClientAuthType type Config func (c *Config) BuildNameToCertificate()

Slide 35

Slide 35 text

TLS crypto/tls type Conn func Client(conn net.Conn, config *Config) *Conn func Dial(network, addr string, config *Config) (*Conn, error) func Server(conn net.Conn, config *Config) *Conn func (c *Conn) Close() error func (c *Conn) ConnectionState() ConnectionState func (c *Conn) Handshake() error func (c *Conn) LocalAddr() net.Addr func (c *Conn) OCSPResponse() []byte func (c *Conn) Read(b []byte) (n int, err error) func (c *Conn) RemoteAddr() net.Addr func (c *Conn) SetDeadline(t time.Time) error func (c *Conn) SetReadDeadline(t time.Time) error func (c *Conn) SetWriteDeadline(t time.Time) error func (c *Conn) VerifyHostname(host string) error func (c *Conn) Write(b []byte) (int, error) type ConnectionStatet

Slide 36

Slide 36 text

exerciseʢ͓·͚ʣ golangͰ ΦϨΦϨূ໌ॻΛ࡞ͬͯɺ tlsαʔόΛཱͯΑ͏ʂ

Slide 37

Slide 37 text

exercise • λεΫ • ূ໌ॻΛͭ͘Δ • crypto/rand,crypto/rsa,crypto/x509(/pkix) • tlsαʔόΛཱͯΔ • crypto/rand,crypto/x509,crypto/tlsɹ

Slide 38

Slide 38 text

exercise ca := &x509.Certificate{ Subject: pkix.Name{ Country: []string{"JP"}, Organization: []string{"Gunosy.com"}, OrganizationalUnit: []string{"Golang TEAM"}, CommonName: "fake.gunosy.com", }, NotBefore: time.Now(), NotAfter: time.Now().AddDate(10,0,0), SerialNumber: big.NewInt(1), SubjectKeyId: []byte{1,2,3,4,5}, BasicConstraintsValid: true, IsCA: true, KeyUsage: x509.KeyUsageDigitalSignature|x509.KeyUsageCertSign, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, } ! priv, _ := rsa.GenerateKey(rand.Reader, 1024) pub := &priv.PublicKey ca_bytes, _ := x509.CreateCertificate(rand.Reader, ca, ca, pub, priv) ioutil.WriteFile("ca.pem", ca_bytes, 0400) ! priv_bytes := x509.MarshalPKCS1PrivateKey(priv) ioutil.WriteFile("ca.key", priv_bytes, 0400) ͪ͜ΒΛվม͠·ͨ͠ https://github.com/go-av/tls-example

Slide 39

Slide 39 text

exercise ca := &x509.Certificate{ Subject: pkix.Name{ Country: []string{"JP"}, Organization: []string{"gunosy.com"}, OrganizationalUnit: []string{"Golang TEAM"}, CommonName: "fake.gunosy.com", }, NotBefore: time.Now(), NotAfter: time.Now().AddDate(10,0,0), SerialNumber: big.NewInt(1), SubjectKeyId: []byte{1,2,3,4,5}, BasicConstraintsValid: true, IsCA: true, KeyUsage: x509.KeyUsageDigitalSignature|x509.KeyUsageCertSign, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, } ! priv, _ := rsa.GenerateKey(rand.Reader, 1024) pub := &priv.PublicKey ca_bytes, _ := x509.CreateCertificate(rand.Reader, ca, ca, pub, priv) ioutil.WriteFile("ca.pem", ca_bytes, 0400) ! priv_bytes := x509.MarshalPKCS1PrivateKey(priv) ioutil.WriteFile("ca.key", priv_bytes, 0400) ূ໌ॻͭ͘Δʂ ൿີ伴/ެ։伴ͱ ূ໌ॻͷ࡞੒ ൿີ伴ͷ ϚʔγϟϦϯά

Slide 40

Slide 40 text

exercise ca_bytes, _ := ioutil.ReadFile("ca.pem") ca, _ := x509.ParseCertificate(ca_bytes) priv_bytes, _ := ioutil.ReadFile("ca.key") priv, _ := x509.ParsePKCS1PrivateKey(priv_bytes) pool := x509.NewCertPool() pool.AddCert(ca) cert := tls.Certificate{ Certificate: [][]byte{ ca_bytes }, PrivateKey: priv, } ! config := tls.Config{ ClientAuth: tls.NoClientCert, Certificates: []tls.Certificate{cert}, ClientCAs: pool, } config.Rand = rand.Reader service := "0.0.0.0:1443" listener, _ := tls.Listen("tcp", service, &config) log.Print("server: listening") ͪ͜ΒΛվม͠·ͨ͠ https://github.com/go-av/tls-example

Slide 41

Slide 41 text

exercise ca_bytes, _ := ioutil.ReadFile("ca.pem") ca, _ := x509.ParseCertificate(ca_bytes) priv_bytes, _ := ioutil.ReadFile("ca.key") priv, _ := x509.ParsePKCS1PrivateKey(priv_bytes) pool := x509.NewCertPool() pool.AddCert(ca) cert := tls.Certificate{ Certificate: [][]byte{ ca_bytes }, PrivateKey: priv, } ! config := tls.Config{ ClientAuth: tls.NoClientCert, Certificates: []tls.Certificate{cert}, ClientCAs: pool, } config.Rand = rand.Reader service := "0.0.0.0:1443" listener, _ := tls.Listen("tcp", service, &config) log.Print("server: listening") PEMܗࣜͷূ໌ॻͱ 伴ΛಡΈࠐΉ TLSͷઃఆ Listenʂʂ

Slide 42

Slide 42 text

Today you learned • cryptoٴͼαϒύοέʔδ͕͋Δ͜ͱ • ͍͍ͩͨͷͰ͖Δ͜ͱ • go͚ͩͰͷΦϨΦϨূ໌ॻͷ࡞Γํ

Slide 43

Slide 43 text

ײ૝ https://godoc.org/code.google.com/p/go.crypto ͳΜ͔ແ͍ͳͱࢥΘ͍͠΋ͷ͍͍ͩͨ͜͜ʹ͋ΔͬΆ͍ ΞϧΰϦζϜͱ͔গͳ͍Πϝʔδ ʢtls࣮૷ͷͨΊͷ࠷খߏ੒ʁʣ OpenSSL / JCE ͱ͔ͰͰ͖Δ͜ͱ͕ͦͦ͜͜Ͱ͖Δ

Slide 44

Slide 44 text

Questions?

Slide 45

Slide 45 text

thank you