| 1 | package signingblock |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto" |
| 6 | "crypto/x509" |
| 7 | "encoding/binary" |
| 8 | "fmt" |
| 9 | "math" |
| 10 | ) |
| 11 | |
| 12 | type SignatureAlgorithm int32 |
| 13 | |
| 14 | const ( |
| 15 | SigRsaPssWithSha256 SignatureAlgorithm = 0x0101 |
| 16 | SigRsaPssWithSha512 = 0x0102 |
| 17 | SigRsaPkcs1V15WithSha256 = 0x0103 |
| 18 | SigRsaPkcs1V15WithSha512 = 0x0104 |
| 19 | SigEcdsaWithSha256 = 0x0201 |
| 20 | SigEcdsaWithSha512 = 0x0202 |
| 21 | SigDsaWithSha256 = 0x0301 |
| 22 | SigVerityRsaPkcs1V15WithSha256 = 0x0421 |
| 23 | SigVerityEcdsaWithSha256 = 0x0423 |
| 24 | SigVerityDsaWithSha256 = 0x425 |
| 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | veritySHA256 crypto.Hash = iota + 65535 |
| 29 | ) |
| 30 | |
| 31 | func (algo SignatureAlgorithm) String() string { |
| 32 | switch algo { |
| 33 | case SigRsaPssWithSha256: |
| 34 | return "SigRsaPssWithSha256" |
| 35 | case SigRsaPssWithSha512: |
| 36 | return "SigRsaPssWithSha512" |
| 37 | case SigRsaPkcs1V15WithSha256: |
| 38 | return "SigRsaPkcs1V15WithSha256" |
| 39 | case SigRsaPkcs1V15WithSha512: |
| 40 | return "SigRsaPkcs1V15WithSha512" |
| 41 | case SigEcdsaWithSha256: |
| 42 | return "SigEcdsaWithSha256" |
| 43 | case SigEcdsaWithSha512: |
| 44 | return "SigEcdsaWithSha512" |
| 45 | case SigDsaWithSha256: |
| 46 | return "SigDsaWithSha256" |
| 47 | case SigVerityRsaPkcs1V15WithSha256: |
| 48 | return "SigVerityRsaPkcs1V15WithSha256" |
| 49 | case SigVerityEcdsaWithSha256: |
| 50 | return "SigVerityEcdsaWithSha256" |
| 51 | case SigVerityDsaWithSha256: |
| 52 | return "SigVerityDsaWithSha256" |
| 53 | } |
| 54 | return fmt.Sprintf("0x%04x", uint32(algo)) |
| 55 | } |
| 56 | |
| 57 | func (algo SignatureAlgorithm) isSupported() bool { |
| 58 | switch algo { |
| 59 | case SigRsaPssWithSha256, SigRsaPssWithSha512, |
| 60 | SigRsaPkcs1V15WithSha256, SigRsaPkcs1V15WithSha512, |
| 61 | SigEcdsaWithSha256, SigEcdsaWithSha512, |
| 62 | SigDsaWithSha256, |
| 63 | SigVerityRsaPkcs1V15WithSha256, SigVerityEcdsaWithSha256, SigVerityDsaWithSha256: |
| 64 | return true |
| 65 | default: |
| 66 | return false |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func (algo SignatureAlgorithm) getDigestType() crypto.Hash { |
| 71 | switch algo { |
| 72 | case SigRsaPssWithSha256, SigRsaPkcs1V15WithSha256, SigEcdsaWithSha256, SigDsaWithSha256: |
| 73 | return crypto.SHA256 |
| 74 | case SigVerityRsaPkcs1V15WithSha256, SigVerityEcdsaWithSha256, SigVerityDsaWithSha256: |
| 75 | return veritySHA256 |
| 76 | case SigRsaPssWithSha512, SigRsaPkcs1V15WithSha512, SigEcdsaWithSha512: |
| 77 | return crypto.SHA512 |
| 78 | default: |
| 79 | panic(fmt.Sprintf("Unknown signature algorithm 0x%x", algo)) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func (algo SignatureAlgorithm) getMinSdkVersion() int { |
| 84 | switch algo { |
| 85 | case SigRsaPssWithSha512, SigRsaPkcs1V15WithSha256, SigRsaPkcs1V15WithSha512, |
| 86 | SigEcdsaWithSha256, SigEcdsaWithSha512, SigDsaWithSha256: |
| 87 | return sdkVersionN |
| 88 | case SigVerityRsaPkcs1V15WithSha256, SigVerityEcdsaWithSha256, SigVerityDsaWithSha256: |
| 89 | return sdkVersionP |
| 90 | default: |
| 91 | return math.MaxInt32 |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | type signerContext struct { |
| 96 | result *VerificationResult |
| 97 | |
| 98 | bestAlgo SignatureAlgorithm |
| 99 | bestAlgoSignature []byte |
| 100 | signaturesAlgos []SignatureAlgorithm |
| 101 | |
| 102 | publicKeyBytes []byte |
| 103 | } |
| 104 | |
| 105 | func (s *signerContext) parseSignatures(signaturesSlice *bytes.Buffer) (success bool) { |
| 106 | s.bestAlgo = -1 |
| 107 | signatureCount := 0 |
| 108 | for signaturesSlice.Len() > 0 { |
| 109 | signatureCount++ |
| 110 | |
| 111 | signature, err := getLenghtPrefixedSlice(signaturesSlice) |
| 112 | if err != nil { |
| 113 | s.result.addError("failed to parse signature record #%d: %s", signatureCount, err.Error()) |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | if signature.Len() < 8 { |
| 118 | s.result.addError("signature record %d is too short", signatureCount) |
| 119 | return |
| 120 | } |
| 121 | |
| 122 | var algo SignatureAlgorithm |
| 123 | if err := binary.Read(signature, binary.LittleEndian, &algo); err != nil { |
| 124 | s.result.addError("failed to parse signature record #%d: %s", signatureCount, err.Error()) |
| 125 | return |
| 126 | } |
| 127 | |
| 128 | s.signaturesAlgos = append(s.signaturesAlgos, algo) |
| 129 | if !algo.isSupported() { |
| 130 | s.result.addWarning("signature %d is using unsupported algorithm %s", signatureCount, algo.String()) |
| 131 | continue |
| 132 | } |
| 133 | |
| 134 | if s.bestAlgo == -1 || s.compareAlgos(algo, s.bestAlgo) > 0 { |
| 135 | s.bestAlgo = algo |
| 136 | sigBytes, err := getLenghtPrefixedSlice(signature) |
| 137 | if err != nil { |
| 138 | s.result.addError("failed to read signature bytes from signature record #%d: %s", signatureCount, err.Error()) |
| 139 | return |
| 140 | } |
| 141 | s.bestAlgoSignature = sigBytes.Bytes() |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if s.bestAlgo == -1 { |
| 146 | if signatureCount == 0 { |
| 147 | s.result.addError("no signatures found") |
| 148 | } else { |
| 149 | s.result.addError("no supported signatures found") |
| 150 | } |
| 151 | return |
| 152 | } |
| 153 | |
| 154 | return true |
| 155 | } |
| 156 | |
| 157 | func (s *signerContext) parsePublicKey(publicKeySlice *bytes.Buffer, signedDataBytes []byte) (success bool) { |
| 158 | s.publicKeyBytes = publicKeySlice.Bytes() |
| 159 | |
| 160 | publicKey, err := x509.ParsePKIXPublicKey(s.publicKeyBytes) |
| 161 | if err != nil { |
| 162 | s.result.addError("failed to parse public key: %s", err.Error()) |
| 163 | return |
| 164 | } |
| 165 | |
| 166 | err = verifySignature(publicKey, s.bestAlgo, signedDataBytes, s.bestAlgoSignature) |
| 167 | if err != nil { |
| 168 | s.result.addError("failed to verify signature of type 0x%x: %s", uint32(s.bestAlgo), err.Error()) |
| 169 | return |
| 170 | } |
| 171 | |
| 172 | return true |
| 173 | } |
| 174 | |
| 175 | func (s *signerContext) parseCertificates(certificatesSlice *bytes.Buffer) (mainCert *x509.Certificate, success bool) { |
| 176 | certAdder := s.result.getCertAdder() |
| 177 | certificateCount := 0 |
| 178 | for certificatesSlice.Len() > 0 { |
| 179 | certificateCount++ |
| 180 | encodedCert, err := getLenghtPrefixedSlice(certificatesSlice) |
| 181 | if err != nil { |
| 182 | s.result.addError("failed to read certificate #%d: %s", certificateCount, err.Error()) |
| 183 | return |
| 184 | } |
| 185 | |
| 186 | cert, err := x509.ParseCertificate(encodedCert.Bytes()) |
| 187 | if err != nil { |
| 188 | s.result.addError("Failed to parse certificate #%d: %s", certificateCount, err.Error()) |
| 189 | return |
| 190 | } |
| 191 | certAdder.append(cert) |
| 192 | } |
| 193 | |
| 194 | if len(certAdder.Certs) == 0 { |
| 195 | s.result.addError("No certificates listed.") |
| 196 | return |
| 197 | } |
| 198 | |
| 199 | mainCert = certAdder.Certs[0] |
| 200 | if !bytes.Equal(mainCert.RawSubjectPublicKeyInfo, s.publicKeyBytes) { |
| 201 | s.result.addError("Public key mismatch between certificate and signature record") |
| 202 | return |
| 203 | } |
| 204 | |
| 205 | return mainCert, true |
| 206 | } |
| 207 | |
| 208 | func (s *signerContext) parseDigests(digestsSlice *bytes.Buffer, contentDigests map[crypto.Hash][]byte) (success bool) { |
| 209 | var contentDigest []byte |
| 210 | var digestSigAlgorithms []SignatureAlgorithm |
| 211 | digestCount := 0 |
| 212 | for digestsSlice.Len() > 0 { |
| 213 | digestCount++ |
| 214 | |
| 215 | digest, err := getLenghtPrefixedSlice(digestsSlice) |
| 216 | if err != nil { |
| 217 | s.result.addError("failed to parse digest #%d: %s", digestCount, err.Error()) |
| 218 | return |
| 219 | } else if digest.Len() < 8 { |
| 220 | s.result.addError("failed to parse digest #%d: record too short", digestCount) |
| 221 | return |
| 222 | } |
| 223 | |
| 224 | var sigAlgorithm SignatureAlgorithm |
| 225 | binary.Read(digest, binary.LittleEndian, &sigAlgorithm) |
| 226 | digestSigAlgorithms = append(digestSigAlgorithms, sigAlgorithm) |
| 227 | if sigAlgorithm == s.bestAlgo { |
| 228 | cd, err := getLenghtPrefixedSlice(digest) |
| 229 | if err != nil { |
| 230 | s.result.addError("failed to read content digest for digest #%d: %s", digestCount, err.Error()) |
| 231 | return |
| 232 | } |
| 233 | contentDigest = cd.Bytes() |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | algosEqual := len(digestSigAlgorithms) == len(s.signaturesAlgos) |
| 238 | for i := 0; algosEqual && i < len(digestSigAlgorithms); i++ { |
| 239 | algosEqual = digestSigAlgorithms[i] == s.signaturesAlgos[i] |
| 240 | } |
| 241 | |
| 242 | if !algosEqual { |
| 243 | s.result.addError("signature algorithms don't match between digests and signatures records") |
| 244 | return |
| 245 | } |
| 246 | |
| 247 | digestAlgorithm := s.bestAlgo.getDigestType() |
| 248 | previousSignerDigest := contentDigests[digestAlgorithm] |
| 249 | contentDigests[digestAlgorithm] = contentDigest |
| 250 | if previousSignerDigest != nil && !bytes.Equal(previousSignerDigest, contentDigest) { |
| 251 | s.result.addError("0x%x contents digest does not match the digest specified by a preceding signer", digestAlgorithm) |
| 252 | return |
| 253 | } |
| 254 | |
| 255 | return true |
| 256 | } |
| 257 | |
| 258 | func (s *signerContext) compareAlgos(a, b SignatureAlgorithm) int { |
| 259 | digest1 := a.getDigestType() |
| 260 | digest2 := b.getDigestType() |
| 261 | |
| 262 | switch digest1 { |
| 263 | case crypto.SHA256: |
| 264 | switch digest2 { |
| 265 | case crypto.SHA256: |
| 266 | return 0 |
| 267 | case crypto.SHA512, veritySHA256: |
| 268 | return -1 |
| 269 | default: |
| 270 | panic(fmt.Sprintf("Unknown digest2: %d", digest2)) |
| 271 | } |
| 272 | case crypto.SHA512: |
| 273 | switch digest2 { |
| 274 | case crypto.SHA256, veritySHA256: |
| 275 | return 1 |
| 276 | case crypto.SHA512: |
| 277 | return 0 |
| 278 | default: |
| 279 | panic(fmt.Sprintf("Unknown digest2: %d", digest2)) |
| 280 | } |
| 281 | case veritySHA256: |
| 282 | switch digest2 { |
| 283 | case crypto.SHA256: |
| 284 | return 1 |
| 285 | case veritySHA256: |
| 286 | return 0 |
| 287 | case crypto.SHA512: |
| 288 | return -1 |
| 289 | default: |
| 290 | panic(fmt.Sprintf("Unknown digest2: %d", digest2)) |
| 291 | } |
| 292 | default: |
| 293 | panic(fmt.Sprintf("Unknown digest1: %d", digest1)) |
| 294 | } |
| 295 | } |