This commit is contained in:
Matt Drollette
2014-12-11 13:11:38 -06:00
parent 0f66b45fce
commit f4cb92b4fa

View File

@@ -53,12 +53,10 @@ type Su3File struct {
} }
func NewSu3File() *Su3File { func NewSu3File() *Su3File {
s := Su3File{ return &Su3File{
Version: []byte(strconv.FormatInt(time.Now().Unix(), 10)), Version: []byte(strconv.FormatInt(time.Now().Unix(), 10)),
SignatureType: SIGTYPE_RSA_SHA512, SignatureType: SIGTYPE_RSA_SHA512,
} }
return &s
} }
func (s *Su3File) Sign(privkey *rsa.PrivateKey) error { func (s *Su3File) Sign(privkey *rsa.PrivateKey) error {
@@ -73,7 +71,7 @@ func (s *Su3File) Sign(privkey *rsa.PrivateKey) error {
case SIGTYPE_ECDSA_SHA512, SIGTYPE_RSA_SHA512: case SIGTYPE_ECDSA_SHA512, SIGTYPE_RSA_SHA512:
hashType = crypto.SHA512 hashType = crypto.SHA512
default: default:
return fmt.Errorf("Unknown signature type") return fmt.Errorf("Unknown signature type.")
} }
h := hashType.New() h := hashType.New()
@@ -91,9 +89,9 @@ func (s *Su3File) Sign(privkey *rsa.PrivateKey) error {
} }
func (s *Su3File) BodyBytes() []byte { func (s *Su3File) BodyBytes() []byte {
buf := new(bytes.Buffer)
var ( var (
buf = new(bytes.Buffer)
skip [1]byte skip [1]byte
bigSkip [12]byte bigSkip [12]byte
@@ -103,6 +101,7 @@ func (s *Su3File) BodyBytes() []byte {
contentLength = uint64(len(s.Content)) contentLength = uint64(len(s.Content))
) )
// determine sig length based on type
switch s.SignatureType { switch s.SignatureType {
case SIGTYPE_DSA: case SIGTYPE_DSA:
signatureLength = uint16(40) signatureLength = uint16(40)
@@ -145,10 +144,8 @@ func (s *Su3File) BodyBytes() []byte {
} }
func (s *Su3File) MarshalBinary() ([]byte, error) { func (s *Su3File) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer) buf := bytes.NewBuffer(s.BodyBytes())
// write the body
buf.Write(s.BodyBytes())
// append the signature // append the signature
binary.Write(buf, binary.BigEndian, s.Signature) binary.Write(buf, binary.BigEndian, s.Signature)
@@ -216,7 +213,7 @@ func (s *Su3File) VerifySignature(cert *x509.Certificate) error {
case SIGTYPE_RSA_SHA512: case SIGTYPE_RSA_SHA512:
sigAlg = x509.SHA512WithRSA sigAlg = x509.SHA512WithRSA
default: default:
return fmt.Errorf("Unsupported signature type.") return fmt.Errorf("Unknown signature type.")
} }
return checkSignature(cert, sigAlg, s.BodyBytes(), s.Signature) return checkSignature(cert, sigAlg, s.BodyBytes(), s.Signature)