Files
Go_I2p/lib/common/certificate.go

110 lines
2.1 KiB
Go
Raw Normal View History

package common
import (
2016-01-29 07:22:31 -05:00
"github.com/bounce-chat/go-i2p/lib/crypto"
)
const (
2016-01-29 07:22:31 -05:00
CERT_NULL = iota
CERT_HASHCASH
CERT_HIDDEN
CERT_SIGNED
CERT_MULTIPLE
CERT_KEY
)
const (
2016-01-29 07:22:31 -05:00
KEYCERT_SIGN_DSA_SHA1 = iota
KEYCERT_SIGN_P256
KEYCERT_SIGN_P384
KEYCERT_SIGN_P521
KEYCERT_SIGN_RSA2048
KEYCERT_SIGN_RSA3072
KEYCERT_SIGN_RSA4096
KEYCERT_SIGN_ED25519
)
const (
2016-01-29 07:22:31 -05:00
KEYCERT_CRYPTO_ELG = iota
)
// used to append data to existing data structures
type Certificate []byte
2016-01-29 07:22:31 -05:00
// return the type of this certificate
func (c Certificate) Type() byte {
2016-01-29 07:22:31 -05:00
return c[0]
}
// get the length of the data in this certificate
// return -1 if the size of the certificate is invalid
func (c Certificate) Len() int {
if len(c) <= 2 {
// invalid size
return -1
}
2016-02-04 00:54:51 -08:00
return Integer(c[1:3])
}
// get the data for this certificate or null if none exists
func (c Certificate) Data() (d []byte) {
2016-01-29 07:22:31 -05:00
l := c.Len()
if l > 0 && len(c) <= 3+l {
2016-01-29 07:22:31 -05:00
d = c[3 : 3+l]
}
return
}
// a Certificate of type KEY
type KeyCert []byte
func (c KeyCert) Type() byte {
2016-01-29 07:22:31 -05:00
return Certificate(c).Type()
}
func (c KeyCert) Data() []byte {
2016-01-29 07:22:31 -05:00
return Certificate(c).Data()
}
// get the signing public key from this key cert
func (c KeyCert) SigningPublicKey() (k crypto.SigningPublicKey) {
2016-01-29 07:22:31 -05:00
data := c.Data()
2016-02-04 00:54:51 -08:00
ktype := Integer(data[:2])
2016-01-29 07:22:31 -05:00
// set data to be the key data now
data = data[4:]
// determine the key type
if ktype == KEYCERT_SIGN_DSA_SHA1 {
var pk crypto.DSAPublicKey
copy(pk[:], data[:pk.Len()])
k = pk
} else if ktype == KEYCERT_SIGN_P256 {
var pk crypto.ECP256PublicKey
copy(pk[:], data[:pk.Len()])
k = pk
} else if ktype == KEYCERT_SIGN_P384 {
var pk crypto.ECP384PublicKey
copy(pk[:], data[:pk.Len()])
k = pk
} else if ktype == KEYCERT_SIGN_P521 {
var pk crypto.ECP521PublicKey
copy(pk[:], data[:pk.Len()])
k = pk
}
// TODO: rsa/eddsa
return
}
func (c Certificate) signatureSize() int {
sizes := map[int]int{
KEYCERT_SIGN_DSA_SHA1: 40,
KEYCERT_SIGN_P256: 64,
KEYCERT_SIGN_P384: 96,
KEYCERT_SIGN_P521: 132,
KEYCERT_SIGN_RSA2048: 256,
KEYCERT_SIGN_RSA3072: 384,
KEYCERT_SIGN_RSA4096: 512,
KEYCERT_SIGN_ED25519: 64,
}
return sizes[int(c.Type())]
}