| 1 |
package signingblock |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"crypto" |
| 8 |
"crypto/x509" |
| 9 |
"encoding/binary" |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"sort" |
| 13 |
) |
| 14 |
|
| 15 |
const ( |
| 16 |
attrV3ProofOfRotation = 0x3ba06f8c |
| 17 |
|
| 18 |
lineageVersionFirst = 1 |
| 19 |
) |
| 20 |
|
| 21 |
type schemeV3 struct { |
| 22 |
signers []*schemeV3SignerInfo |
| 23 |
} |
| 24 |
|
| 25 |
type schemeV3SignerInfo struct { |
| 26 |
lineage *V3SigningLineage |
| 27 |
minSdkVersion, maxSdkVersion int32 |
| 28 |
} |
| 29 |
|
| 30 |
func (s *schemeV3) parseSigners(block *bytes.Buffer, contentDigests map[crypto.Hash][]byte, result *VerificationResult) { |
| 31 |
signers, err := getLenghtPrefixedSlice(block) |
| 32 |
if err != nil { |
| 33 |
result.addError("failed to read list of signers: %s", err.Error()) |
| 34 |
return |
| 35 |
} |
| 36 |
|
| 37 |
signerCount := 0 |
| 38 |
for signers.Len() > 0 { |
| 39 |
signerCount++ |
| 40 |
|
| 41 |
signer, err := getLenghtPrefixedSlice(signers) |
| 42 |
if err != nil { |
| 43 |
result.addError("failed to parse/verify signer #%d block: %s", signerCount, err.Error()) |
| 44 |
return |
| 45 |
} |
| 46 |
|
| 47 |
s.verifySigner(signer, contentDigests, result) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func (s *schemeV3) finalizeResult(requestedMinSdkVersion, requestedMaxSdkVersion int32, result *VerificationResult) { |
| 52 |
|
| 53 |
|
| 54 |
if requestedMinSdkVersion < sdkVersionP { |
| 55 |
requestedMinSdkVersion = sdkVersionP |
| 56 |
} |
| 57 |
|
| 58 |
sort.Slice(s.signers, func(i, j int) bool { |
| 59 |
return s.signers[i].minSdkVersion < s.signers[j].minSdkVersion |
| 60 |
}) |
| 61 |
|
| 62 |
var firstMin, lastMax int32 |
| 63 |
lastLineageSize := 0 |
| 64 |
lineages := make([]*V3SigningLineage, 0, len(s.signers)) |
| 65 |
for _, signer := range s.signers { |
| 66 |
currentMin, currentMax := signer.minSdkVersion, signer.maxSdkVersion |
| 67 |
if firstMin == 0 { |
| 68 |
firstMin = currentMin |
| 69 |
} else if currentMin != lastMax+1 { |
| 70 |
result.addError("inconsistent signer sdkversions") |
| 71 |
break |
| 72 |
} |
| 73 |
lastMax = currentMax |
| 74 |
|
| 75 |
if signer.lineage != nil { |
| 76 |
if size := len(signer.lineage.Nodes); size < lastLineageSize { |
| 77 |
result.addError("inconsistent signer lineage sizes") |
| 78 |
break |
| 79 |
} else { |
| 80 |
lastLineageSize = size |
| 81 |
lineages = append(lineages, signer.lineage) |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if firstMin > requestedMinSdkVersion || lastMax < requestedMaxSdkVersion { |
| 87 |
result.addError("missing sdk versions, supports only <%d;%d>", firstMin, lastMax) |
| 88 |
} |
| 89 |
|
| 90 |
var err error |
| 91 |
result.SigningLineage, err = s.consolidateLineages(lineages) |
| 92 |
if err != nil { |
| 93 |
result.addError(err.Error()) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func (s *schemeV3) verifySigner(signerBlock *bytes.Buffer, contentDigests map[crypto.Hash][]byte, result *VerificationResult) { |
| 98 |
signedData, err := getLenghtPrefixedSlice(signerBlock) |
| 99 |
if err != nil { |
| 100 |
result.addError("failed to read signed data: %s", err.Error()) |
| 101 |
return |
| 102 |
} |
| 103 |
signedDataBytes := signedData.Bytes() |
| 104 |
|
| 105 |
|
| 106 |
var parsedMinSdkVersion, parsedMaxSdkVersion int32 |
| 107 |
if err := binary.Read(signerBlock, binary.LittleEndian, &parsedMinSdkVersion); err != nil { |
| 108 |
result.addError("failed to read parsedMinSdkVersion: %s", err.Error()) |
| 109 |
return |
| 110 |
} |
| 111 |
if err := binary.Read(signerBlock, binary.LittleEndian, &parsedMaxSdkVersion); err != nil { |
| 112 |
result.addError("failed to read parsedMaxSdkVersion: %s", err.Error()) |
| 113 |
return |
| 114 |
} |
| 115 |
|
| 116 |
if parsedMinSdkVersion < 1 || (parsedMinSdkVersion > parsedMaxSdkVersion) { |
| 117 |
result.addError("invalid min/max sdk versions: <%d,%d>", parsedMinSdkVersion, parsedMaxSdkVersion) |
| 118 |
} |
| 119 |
|
| 120 |
signerInfo := &schemeV3SignerInfo{ |
| 121 |
minSdkVersion: parsedMinSdkVersion, |
| 122 |
maxSdkVersion: parsedMaxSdkVersion, |
| 123 |
} |
| 124 |
s.signers = append(s.signers, signerInfo) |
| 125 |
|
| 126 |
ctx := signerContext{result: result} |
| 127 |
|
| 128 |
|
| 129 |
signaturesSlice, err := getLenghtPrefixedSlice(signerBlock) |
| 130 |
if err != nil { |
| 131 |
result.addError("failed to read signatures: %s", err.Error()) |
| 132 |
return |
| 133 |
} |
| 134 |
|
| 135 |
if !ctx.parseSignatures(signaturesSlice) { |
| 136 |
return |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
publicKeySlice, err := getLenghtPrefixedSlice(signerBlock) |
| 141 |
if err != nil { |
| 142 |
result.addError("failed to read publicKeySlice: %s", err.Error()) |
| 143 |
return |
| 144 |
} |
| 145 |
|
| 146 |
if !ctx.parsePublicKey(publicKeySlice, signedDataBytes) { |
| 147 |
return |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
digestsSlice, err := getLenghtPrefixedSlice(signedData) |
| 152 |
if err != nil { |
| 153 |
result.addError("failed to read digests from signedData: %s", err.Error()) |
| 154 |
return |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
certificatesSlice, err := getLenghtPrefixedSlice(signedData) |
| 159 |
if err != nil { |
| 160 |
result.addError("failed to read certificates from signedData: %s", err.Error()) |
| 161 |
return |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
var signedMinSdkVersion, signedMaxSdkVersion int32 |
| 166 |
if err := binary.Read(signedData, binary.LittleEndian, &signedMinSdkVersion); err != nil { |
| 167 |
result.addError("failed to read signedMinSdkVersion: %s", err.Error()) |
| 168 |
return |
| 169 |
} |
| 170 |
if err := binary.Read(signedData, binary.LittleEndian, &signedMaxSdkVersion); err != nil { |
| 171 |
result.addError("Failed to read signedMaxSdkVersion: %s", err.Error()) |
| 172 |
return |
| 173 |
} |
| 174 |
|
| 175 |
if parsedMinSdkVersion != signedMinSdkVersion { |
| 176 |
result.addError("mismatch between parsed and signed minSdkVersion: %d != %d", parsedMinSdkVersion, signedMinSdkVersion) |
| 177 |
} |
| 178 |
if parsedMaxSdkVersion != parsedMaxSdkVersion { |
| 179 |
result.addError("mismatch between parsed and signed minSdkVersion: %d != %d", parsedMinSdkVersion, signedMinSdkVersion) |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
mainCert, success := ctx.parseCertificates(certificatesSlice) |
| 184 |
if !success { |
| 185 |
return |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
if !ctx.parseDigests(digestsSlice, contentDigests) { |
| 190 |
return |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
additionalAttributes, err := getLenghtPrefixedSlice(signedData) |
| 195 |
if err != nil { |
| 196 |
result.addError("failed to read additionalAttributes from signedData: %s", err.Error()) |
| 197 |
return |
| 198 |
} |
| 199 |
|
| 200 |
additionalAttributeCount := 0 |
| 201 |
for additionalAttributes.Len() > 0 { |
| 202 |
additionalAttributeCount++ |
| 203 |
|
| 204 |
attribute, err := getLenghtPrefixedSlice(additionalAttributes) |
| 205 |
if err != nil { |
| 206 |
result.addError("failed to read additional attribute %d: %s", additionalAttributeCount, err.Error()) |
| 207 |
return |
| 208 |
} |
| 209 |
|
| 210 |
var id uint32 |
| 211 |
if err := binary.Read(attribute, binary.LittleEndian, &id); err != nil { |
| 212 |
result.addError("failed to read additional attribute %d's id: %s", additionalAttributeCount, err.Error()) |
| 213 |
return |
| 214 |
} |
| 215 |
|
| 216 |
switch id { |
| 217 |
case attrV3ProofOfRotation: |
| 218 |
nodes, err := s.readSigningCertificateLineage(attribute) |
| 219 |
if err != nil { |
| 220 |
result.addError("failed to read signing certificate lineage attribute: %s", err.Error()) |
| 221 |
return |
| 222 |
} |
| 223 |
|
| 224 |
if len(nodes) != 0 { |
| 225 |
signerInfo.lineage = &V3SigningLineage{ |
| 226 |
MinSdkVersion: lineageCalculateMinSdkVersion(nodes), |
| 227 |
Nodes: nodes, |
| 228 |
} |
| 229 |
|
| 230 |
subLineage, err := signerInfo.lineage.getSubLineage(mainCert) |
| 231 |
if err != nil { |
| 232 |
result.addError(err.Error()) |
| 233 |
break |
| 234 |
} |
| 235 |
|
| 236 |
if len(signerInfo.lineage.Nodes) != len(subLineage.Nodes) { |
| 237 |
result.addError("sub lineage cert mismatch") |
| 238 |
} |
| 239 |
} |
| 240 |
default: |
| 241 |
result.addWarning("unknown additional attribute id 0x%x", uint32(id)) |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
func (s *schemeV3) readSigningCertificateLineage(lineageSlice *bytes.Buffer) (V3LineageSigningCertificateNodeList, error) { |
| 247 |
if lineageSlice.Len() == 0 { |
| 248 |
return nil, nil |
| 249 |
} |
| 250 |
|
| 251 |
var version int32 |
| 252 |
if err := binary.Read(lineageSlice, binary.LittleEndian, &version); err != nil { |
| 253 |
return nil, fmt.Errorf("failed to read version: %s", err.Error()) |
| 254 |
} |
| 255 |
|
| 256 |
if version != lineageVersionFirst { |
| 257 |
return nil, fmt.Errorf("unknown lineage version %d", version) |
| 258 |
} |
| 259 |
|
| 260 |
var nodeCount int |
| 261 |
var lastCert *x509.Certificate |
| 262 |
var lastSigAlgorithmId SignatureAlgorithm |
| 263 |
var certHistory []*x509.Certificate |
| 264 |
var result V3LineageSigningCertificateNodeList |
| 265 |
for lineageSlice.Len() != 0 { |
| 266 |
nodeCount++ |
| 267 |
|
| 268 |
nodeBytes, err := getLenghtPrefixedSlice(lineageSlice) |
| 269 |
if err != nil { |
| 270 |
return nil, fmt.Errorf("failed to read nodeBytes for node %d: %s", nodeCount, err.Error()) |
| 271 |
} |
| 272 |
|
| 273 |
signedData, err := getLenghtPrefixedSlice(nodeBytes) |
| 274 |
if err != nil { |
| 275 |
return nil, fmt.Errorf("failed to read signedData for node %d: %s", nodeCount, err.Error()) |
| 276 |
} |
| 277 |
|
| 278 |
var flags int32 |
| 279 |
if err := binary.Read(nodeBytes, binary.LittleEndian, &flags); err != nil { |
| 280 |
return nil, fmt.Errorf("failed to read flags for node %d: %s", nodeCount, err.Error()) |
| 281 |
} |
| 282 |
|
| 283 |
var sigAlgorithmId SignatureAlgorithm |
| 284 |
if err := binary.Read(nodeBytes, binary.LittleEndian, &sigAlgorithmId); err != nil { |
| 285 |
return nil, fmt.Errorf("failed to read sigAlgorithmId for node %d: %s", nodeCount, err.Error()) |
| 286 |
} |
| 287 |
|
| 288 |
signature, err := getLenghtPrefixedSlice(nodeBytes) |
| 289 |
if err != nil { |
| 290 |
return nil, fmt.Errorf("failed to read signature for node %d: %s", nodeCount, err.Error()) |
| 291 |
} |
| 292 |
|
| 293 |
if lastCert != nil { |
| 294 |
if err := verifySignature(lastCert.PublicKey, lastSigAlgorithmId, signedData.Bytes(), signature.Bytes()); err != nil { |
| 295 |
return nil, fmt.Errorf("unable to verify signature of certificate #%d using algo %d: %s", |
| 296 |
nodeCount, lastSigAlgorithmId, err.Error()) |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
encodedCert, err := getLenghtPrefixedSlice(signedData) |
| 301 |
if err != nil { |
| 302 |
return nil, fmt.Errorf("failed to read encodedCert for node %d: %s", nodeCount, err.Error()) |
| 303 |
} |
| 304 |
|
| 305 |
var signedSigAlgorithm SignatureAlgorithm |
| 306 |
if err := binary.Read(signedData, binary.LittleEndian, &signedSigAlgorithm); err != nil { |
| 307 |
return nil, fmt.Errorf("failed to read signedSigAlgorithm for node %d: %s", nodeCount, err.Error()) |
| 308 |
} |
| 309 |
|
| 310 |
if lastCert != nil && lastSigAlgorithmId != signedSigAlgorithm { |
| 311 |
return nil, fmt.Errorf("signing algorithm ID mismatch for certificate #%d", nodeCount) |
| 312 |
} |
| 313 |
|
| 314 |
lastCert, err = x509.ParseCertificate(encodedCert.Bytes()) |
| 315 |
if err != nil { |
| 316 |
return nil, fmt.Errorf("failed to parse cert in node #%d", nodeCount) |
| 317 |
} |
| 318 |
|
| 319 |
for _, histCert := range certHistory { |
| 320 |
if histCert.Equal(lastCert) { |
| 321 |
return nil, fmt.Errorf("encountered duplicate entries at node #%d, signing certificates should be unique.", nodeCount) |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
certHistory = append(certHistory, lastCert) |
| 326 |
lastSigAlgorithmId = sigAlgorithmId |
| 327 |
|
| 328 |
result = append(result, &V3LineageSigningCertificateNode{ |
| 329 |
SigningCert: lastCert, |
| 330 |
ParentSigAlgorithm: signedSigAlgorithm, |
| 331 |
SigAlgorithm: sigAlgorithmId, |
| 332 |
Signature: signature.Bytes(), |
| 333 |
Flags: flags, |
| 334 |
}) |
| 335 |
} |
| 336 |
|
| 337 |
return result, nil |
| 338 |
} |
| 339 |
|
| 340 |
func (s *schemeV3) consolidateLineages(lineages []*V3SigningLineage) (*V3SigningLineage, error) { |
| 341 |
if len(lineages) == 0 { |
| 342 |
return nil, nil |
| 343 |
} |
| 344 |
|
| 345 |
largestIndex := 0 |
| 346 |
maxSize := 0 |
| 347 |
for i := range lineages { |
| 348 |
if s := len(lineages[i].Nodes); s > maxSize { |
| 349 |
maxSize = s |
| 350 |
largestIndex = i |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
largestList := lineages[largestIndex].Nodes |
| 355 |
for i := range lineages { |
| 356 |
if i == largestIndex { |
| 357 |
continue |
| 358 |
} |
| 359 |
|
| 360 |
underTest := lineages[i].Nodes |
| 361 |
if !underTest.Equal(largestList[:len(underTest)]) { |
| 362 |
return nil, errors.New("inconsistent certificate lineage, not all lineages are subsets of each other") |
| 363 |
} |
| 364 |
} |
| 365 |
return lineages[largestIndex], nil |
| 366 |
} |