| 1 |
package signingblock |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto" |
| 6 |
"crypto/dsa" |
| 7 |
"crypto/ecdsa" |
| 8 |
"crypto/rsa" |
| 9 |
"crypto/sha256" |
| 10 |
"crypto/sha512" |
| 11 |
"crypto/x509/pkix" |
| 12 |
"encoding/asn1" |
| 13 |
"encoding/binary" |
| 14 |
"errors" |
| 15 |
"fmt" |
| 16 |
"math/big" |
| 17 |
"strings" |
| 18 |
) |
| 19 |
|
| 20 |
func getLenghtPrefixedSlice(r *bytes.Buffer) (*bytes.Buffer, error) { |
| 21 |
if r.Len() < 4 { |
| 22 |
return nil, fmt.Errorf("Remaining buffer too short to contain length of length-prefixed field. Remaining: %d", r.Len()) |
| 23 |
} |
| 24 |
|
| 25 |
var length int32 |
| 26 |
if err := binary.Read(r, binary.LittleEndian, &length); err != nil { |
| 27 |
return nil, err |
| 28 |
} |
| 29 |
|
| 30 |
if length < 0 { |
| 31 |
return nil, errors.New("negative length") |
| 32 |
} else if int(length) > r.Len() { |
| 33 |
return nil, fmt.Errorf("Length-prefixed field longer than remaining buffer. "+ |
| 34 |
"Field length: %d, remaining: %d", length, r.Len()) |
| 35 |
} |
| 36 |
return bytes.NewBuffer(r.Next(int(length))), nil |
| 37 |
} |
| 38 |
|
| 39 |
func verifySignature(publicKey interface{}, algo SignatureAlgorithm, signedDataBytes, signature []byte) error { |
| 40 |
switch algo { |
| 41 |
case SigRsaPssWithSha256: |
| 42 |
hashed := sha256.Sum256(signedDataBytes) |
| 43 |
return rsa.VerifyPSS(publicKey.(*rsa.PublicKey), crypto.SHA256, hashed[:], signature, &rsa.PSSOptions{ |
| 44 |
SaltLength: 256 / 8, |
| 45 |
}) |
| 46 |
case SigRsaPssWithSha512: |
| 47 |
hashed := sha512.Sum512(signedDataBytes) |
| 48 |
return rsa.VerifyPSS(publicKey.(*rsa.PublicKey), crypto.SHA512, hashed[:], signature, &rsa.PSSOptions{ |
| 49 |
SaltLength: 512 / 8, |
| 50 |
}) |
| 51 |
case SigRsaPkcs1V15WithSha256, SigVerityRsaPkcs1V15WithSha256: |
| 52 |
hashed := sha256.Sum256(signedDataBytes) |
| 53 |
return rsa.VerifyPKCS1v15(publicKey.(*rsa.PublicKey), crypto.SHA256, hashed[:], signature) |
| 54 |
case SigRsaPkcs1V15WithSha512: |
| 55 |
hashed := sha512.Sum512(signedDataBytes) |
| 56 |
return rsa.VerifyPKCS1v15(publicKey.(*rsa.PublicKey), crypto.SHA512, hashed[:], signature) |
| 57 |
case SigEcdsaWithSha256, SigEcdsaWithSha512, SigDsaWithSha256, SigVerityEcdsaWithSha256, SigVerityDsaWithSha256: |
| 58 |
var params []*big.Int |
| 59 |
if _, err := asn1.Unmarshal(signature, ¶ms); err != nil { |
| 60 |
return fmt.Errorf("failed to unmarshal ECDSA signature: %s", err.Error()) |
| 61 |
} |
| 62 |
|
| 63 |
var hashed []byte |
| 64 |
switch algo { |
| 65 |
case SigEcdsaWithSha256, SigDsaWithSha256, SigVerityEcdsaWithSha256, SigVerityDsaWithSha256: |
| 66 |
h := sha256.Sum256(signedDataBytes) |
| 67 |
hashed = h[:] |
| 68 |
case SigEcdsaWithSha512: |
| 69 |
h := sha512.Sum512(signedDataBytes) |
| 70 |
hashed = h[:] |
| 71 |
default: |
| 72 |
panic("Unandled") |
| 73 |
} |
| 74 |
|
| 75 |
if algo == SigDsaWithSha256 || algo == SigVerityDsaWithSha256 { |
| 76 |
k := publicKey.(*dsa.PublicKey) |
| 77 |
hashed = hashed[:k.Q.BitLen()/8] |
| 78 |
if !dsa.Verify(k, hashed, params[0], params[1]) { |
| 79 |
return errors.New("DSA verification failed.") |
| 80 |
} |
| 81 |
} else { |
| 82 |
if !ecdsa.Verify(publicKey.(*ecdsa.PublicKey), hashed, params[0], params[1]) { |
| 83 |
return errors.New("ECDSA verification failed.") |
| 84 |
} |
| 85 |
} |
| 86 |
default: |
| 87 |
return errors.New("unhandled signature type") |
| 88 |
} |
| 89 |
|
| 90 |
return nil |
| 91 |
} |
| 92 |
|
| 93 |
func PkixNameToString(n *pkix.Name) string { |
| 94 |
var buf bytes.Buffer |
| 95 |
|
| 96 |
if len(n.Country) != 0 { |
| 97 |
fmt.Fprintf(&buf, "C=%s, ", strings.Join(n.Country, ";")) |
| 98 |
} |
| 99 |
if len(n.Province) != 0 { |
| 100 |
fmt.Fprintf(&buf, "ST=%s, ", strings.Join(n.Province, ";")) |
| 101 |
} |
| 102 |
if len(n.Locality) != 0 { |
| 103 |
fmt.Fprintf(&buf, "L=%s, ", strings.Join(n.Locality, ";")) |
| 104 |
} |
| 105 |
if len(n.Organization) != 0 { |
| 106 |
fmt.Fprintf(&buf, "O=%s, ", strings.Join(n.Organization, ";")) |
| 107 |
} |
| 108 |
if len(n.OrganizationalUnit) != 0 { |
| 109 |
fmt.Fprintf(&buf, "OU=%s, ", strings.Join(n.OrganizationalUnit, ";")) |
| 110 |
} |
| 111 |
if len(n.CommonName) != 0 { |
| 112 |
fmt.Fprintf(&buf, "CN=%s, ", n.CommonName) |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
if buf.Len() != 0 { |
| 117 |
buf.Truncate(buf.Len() - 2) |
| 118 |
} |
| 119 |
|
| 120 |
return buf.String() |
| 121 |
} |