1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-2022 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package asserts
import (
"bytes"
"crypto"
"encoding/hex"
"errors"
"fmt"
"strings"
"time"
// expected for digests
_ "golang.org/x/crypto/sha3"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap/naming"
"github.com/snapcore/snapd/strutil"
)
// SnapDeclaration holds a snap-declaration assertion, declaring a
// snap binding its identifying snap-id to a name, asserting its
// publisher and its other properties.
type SnapDeclaration struct {
assertionBase
refreshControl []string
plugRules map[string]*PlugRule
slotRules map[string]*SlotRule
autoAliases []string
aliases map[string]string
revisionAuthorities []*RevisionAuthority
timestamp time.Time
}
// Series returns the series for which the snap is being declared.
func (snapdcl *SnapDeclaration) Series() string {
return snapdcl.HeaderString("series")
}
// SnapID returns the snap id of the declared snap.
func (snapdcl *SnapDeclaration) SnapID() string {
return snapdcl.HeaderString("snap-id")
}
// SnapName returns the declared snap name.
func (snapdcl *SnapDeclaration) SnapName() string {
return snapdcl.HeaderString("snap-name")
}
// PublisherID returns the identifier of the publisher of the declared snap.
func (snapdcl *SnapDeclaration) PublisherID() string {
return snapdcl.HeaderString("publisher-id")
}
// Timestamp returns the time when the snap-declaration was issued.
func (snapdcl *SnapDeclaration) Timestamp() time.Time {
return snapdcl.timestamp
}
// RefreshControl returns the ids of snaps whose updates are controlled by this declaration.
func (snapdcl *SnapDeclaration) RefreshControl() []string {
return snapdcl.refreshControl
}
// PlugRule returns the plug-side rule about the given interface if one was included in the plugs stanza of the declaration, otherwise it returns nil.
func (snapdcl *SnapDeclaration) PlugRule(interfaceName string) *PlugRule {
return snapdcl.plugRules[interfaceName]
}
// SlotRule returns the slot-side rule about the given interface if one was included in the slots stanza of the declaration, otherwise it returns nil.
func (snapdcl *SnapDeclaration) SlotRule(interfaceName string) *SlotRule {
return snapdcl.slotRules[interfaceName]
}
// AutoAliases returns the optional auto-aliases granted to this snap.
// XXX: deprecated, will go away
func (snapdcl *SnapDeclaration) AutoAliases() []string {
return snapdcl.autoAliases
}
// Aliases returns the optional explicit aliases granted to this snap.
func (snapdcl *SnapDeclaration) Aliases() map[string]string {
return snapdcl.aliases
}
// RevisionAuthority return any revision authority entries matching the given
// provenance.
func (snapdcl *SnapDeclaration) RevisionAuthority(provenance string) []*RevisionAuthority {
res := make([]*RevisionAuthority, 0, 1)
for _, ra := range snapdcl.revisionAuthorities {
if strutil.ListContains(ra.Provenance, provenance) {
res = append(res, ra)
}
}
if len(res) == 0 {
return nil
}
return res
}
// Implement further consistency checks.
func (snapdcl *SnapDeclaration) checkConsistency(db RODatabase, acck *AccountKey) error {
if !db.IsTrustedAccount(snapdcl.AuthorityID()) {
return fmt.Errorf("snap-declaration assertion for %q (id %q) is not signed by a directly trusted authority: %s", snapdcl.SnapName(), snapdcl.SnapID(), snapdcl.AuthorityID())
}
_, err := db.Find(AccountType, map[string]string{
"account-id": snapdcl.PublisherID(),
})
if errors.Is(err, &NotFoundError{}) {
return fmt.Errorf("snap-declaration assertion for %q (id %q) does not have a matching account assertion for the publisher %q", snapdcl.SnapName(), snapdcl.SnapID(), snapdcl.PublisherID())
}
if err != nil {
return err
}
return nil
}
// expected interface is implemented
var _ consistencyChecker = (*SnapDeclaration)(nil)
// Prerequisites returns references to this snap-declaration's prerequisite assertions.
func (snapdcl *SnapDeclaration) Prerequisites() []*Ref {
return []*Ref{
{Type: AccountType, PrimaryKey: []string{snapdcl.PublisherID()}},
}
}
func compilePlugRules(plugs map[string]any, compiled func(iface string, plugRule *PlugRule)) error {
for iface, rule := range plugs {
plugRule, err := compilePlugRule(iface, rule)
if err != nil {
return err
}
compiled(iface, plugRule)
}
return nil
}
func compileSlotRules(slots map[string]any, compiled func(iface string, slotRule *SlotRule)) error {
for iface, rule := range slots {
slotRule, err := compileSlotRule(iface, rule)
if err != nil {
return err
}
compiled(iface, slotRule)
}
return nil
}
func snapDeclarationFormatAnalyze(headers map[string]any, body []byte) (formatnum int, err error) {
_, plugsOk := headers["plugs"]
_, slotsOk := headers["slots"]
if !(plugsOk || slotsOk) {
return 0, nil
}
formatnum = 1
setFormatNum := func(num int) {
if num > formatnum {
formatnum = num
}
}
plugs, err := checkMap(headers, "plugs")
if err != nil {
return 0, err
}
err = compilePlugRules(plugs, func(_ string, rule *PlugRule) {
if rule.feature(dollarAttrConstraintsFeature) {
setFormatNum(2)
}
if rule.feature(deviceScopeConstraintsFeature) {
setFormatNum(3)
}
if rule.feature(nameConstraintsFeature) {
setFormatNum(4)
}
if rule.feature(altAttrMatcherFeature) {
setFormatNum(5)
}
if rule.feature(publisherIDConstraintsFeature) {
setFormatNum(6)
}
})
if err != nil {
return 0, err
}
slots, err := checkMap(headers, "slots")
if err != nil {
return 0, err
}
err = compileSlotRules(slots, func(_ string, rule *SlotRule) {
if rule.feature(dollarAttrConstraintsFeature) {
setFormatNum(2)
}
if rule.feature(deviceScopeConstraintsFeature) {
setFormatNum(3)
}
if rule.feature(nameConstraintsFeature) {
setFormatNum(4)
}
if rule.feature(altAttrMatcherFeature) {
setFormatNum(5)
}
if rule.feature(publisherIDConstraintsFeature) {
setFormatNum(6)
}
})
if err != nil {
return 0, err
}
return formatnum, nil
}
func checkAliases(headers map[string]any) (map[string]string, error) {
value, ok := headers["aliases"]
if !ok {
return nil, nil
}
aliasList, ok := value.([]any)
if !ok {
return nil, fmt.Errorf(`"aliases" header must be a list of alias maps`)
}
if len(aliasList) == 0 {
return nil, nil
}
aliasMap := make(map[string]string, len(aliasList))
for i, item := range aliasList {
aliasItem, ok := item.(map[string]any)
if !ok {
return nil, fmt.Errorf(`"aliases" header must be a list of alias maps`)
}
what := fmt.Sprintf(`in "aliases" item %d`, i+1)
name, err := checkStringMatchesWhat(aliasItem, "name", what, naming.ValidAlias)
if err != nil {
return nil, err
}
what = fmt.Sprintf(`for alias %q`, name)
target, err := checkStringMatchesWhat(aliasItem, "target", what, naming.ValidApp)
if err != nil {
return nil, err
}
if _, ok := aliasMap[name]; ok {
return nil, fmt.Errorf(`duplicated definition in "aliases" for alias %q`, name)
}
aliasMap[name] = target
}
return aliasMap, nil
}
func assembleSnapDeclaration(assert assertionBase) (Assertion, error) {
_, err := checkExistsString(assert.headers, "snap-name")
if err != nil {
return nil, err
}
_, err = checkNotEmptyString(assert.headers, "publisher-id")
if err != nil {
return nil, err
}
timestamp, err := checkRFC3339Date(assert.headers, "timestamp")
if err != nil {
return nil, err
}
var refControl []string
var plugRules map[string]*PlugRule
var slotRules map[string]*SlotRule
refControl, err = checkStringList(assert.headers, "refresh-control")
if err != nil {
return nil, err
}
plugs, err := checkMap(assert.headers, "plugs")
if err != nil {
return nil, err
}
if plugs != nil {
plugRules = make(map[string]*PlugRule, len(plugs))
err := compilePlugRules(plugs, func(iface string, rule *PlugRule) {
plugRules[iface] = rule
})
if err != nil {
return nil, err
}
}
slots, err := checkMap(assert.headers, "slots")
if err != nil {
return nil, err
}
if slots != nil {
slotRules = make(map[string]*SlotRule, len(slots))
err := compileSlotRules(slots, func(iface string, rule *SlotRule) {
slotRules[iface] = rule
})
if err != nil {
return nil, err
}
}
// XXX: depracated, will go away later
autoAliases, err := checkStringListMatches(assert.headers, "auto-aliases", naming.ValidAlias)
if err != nil {
return nil, err
}
aliases, err := checkAliases(assert.headers)
if err != nil {
return nil, err
}
var ras []*RevisionAuthority
ra, ok := assert.headers["revision-authority"]
if ok {
ramaps, ok := ra.([]any)
if !ok {
return nil, fmt.Errorf("revision-authority stanza must be a list of maps")
}
if len(ramaps) == 0 {
// there is no syntax producing this scenario but be robust
return nil, fmt.Errorf("revision-authority stanza cannot be empty")
}
ras = make([]*RevisionAuthority, 0, len(ramaps))
for _, ramap := range ramaps {
m, ok := ramap.(map[string]any)
if !ok {
return nil, fmt.Errorf("revision-authority stanza must be a list of maps")
}
accountID, err := checkStringMatchesWhat(m, "account-id", "in revision authority", validAccountID)
if err != nil {
return nil, err
}
prov, err := checkStringListInMap(m, "provenance", "provenance in revision authority", naming.ValidProvenance)
if err != nil {
return nil, err
}
if len(prov) == 0 {
return nil, fmt.Errorf("provenance in revision authority cannot be empty")
}
minRevision := 1
maxRevision := 0
if _, ok := m["min-revision"]; ok {
var err error
minRevision, err = checkSnapRevisionWhat(m, "min-revision", "in revision authority")
if err != nil {
return nil, err
}
}
if _, ok := m["max-revision"]; ok {
var err error
maxRevision, err = checkSnapRevisionWhat(m, "max-revision", "in revision authority")
if err != nil {
return nil, err
}
}
if maxRevision != 0 && maxRevision < minRevision {
return nil, fmt.Errorf("optional max-revision cannot be less than min-revision in revision-authority")
}
devscope, err := compileDeviceScopeConstraint(m, "revision-authority")
if err != nil {
return nil, err
}
ras = append(ras, &RevisionAuthority{
AccountID: accountID,
Provenance: prov,
MinRevision: minRevision,
MaxRevision: maxRevision,
DeviceScope: devscope,
})
}
}
return &SnapDeclaration{
assertionBase: assert,
refreshControl: refControl,
plugRules: plugRules,
slotRules: slotRules,
autoAliases: autoAliases,
aliases: aliases,
revisionAuthorities: ras,
timestamp: timestamp,
}, nil
}
// RevisionAuthority holds information about an account that can sign revisions
// for a given snap.
type RevisionAuthority struct {
AccountID string
Provenance []string
MinRevision int
MaxRevision int
DeviceScope *DeviceScopeConstraint
}
func (ra *RevisionAuthority) checkProvenanceAndRevision(a interface {
Assertion
Provenance() string
}, what string, revno int, model *Model, store *Store) error {
if !strutil.ListContains(ra.Provenance, a.Provenance()) {
return fmt.Errorf("provenance mismatch")
}
if a.AuthorityID() != ra.AccountID {
return fmt.Errorf("authority-id mismatch")
}
if revno < ra.MinRevision {
return fmt.Errorf("%s revision %d is less than min-revision %d", what, revno, ra.MinRevision)
}
if ra.MaxRevision != 0 && revno > ra.MaxRevision {
return fmt.Errorf("%s revision %d is greater than max-revision %d", what, revno, ra.MaxRevision)
}
if ra.DeviceScope != nil && model != nil {
opts := DeviceScopeConstraintCheckOptions{UseFriendlyStores: true}
if err := ra.DeviceScope.Check(model, store, &opts); err != nil {
return err
}
}
return nil
}
// Check tests whether rev matches the revision authority constraints.
// Optional model and store must be provided to cross-check device-specific
// constraints.
func (ra *RevisionAuthority) Check(rev *SnapRevision, model *Model, store *Store) error {
return ra.checkProvenanceAndRevision(rev, "snap", rev.SnapRevision(), model, store)
}
// CheckResourceRevision tests whether resrev matches the revision authority
// constraints. Optional model and store must be provided to cross-check
// device-specific constraints.
func (ra *RevisionAuthority) CheckResourceRevision(resrev *SnapResourceRevision, model *Model, store *Store) error {
return ra.checkProvenanceAndRevision(resrev, "resource", resrev.ResourceRevision(), model, store)
}
var validSnapIntegrityTypes = []string{"dm-verity"}
var validVersionsForIntegrityType = map[string][]int{
// version 1 corresponds to dm-verity format 1
"dm-verity": {1},
}
var validHashAlgorithmsForIntegrityType = map[string][]string{
// kernel supported algorithms:
// https://gitlab.com/cryptsetup/cryptsetup/-/blob/main/lib/crypto_backend/crypto_kernel.c?ref_type=heads#L35
// Go crypto's supported algorithms:
// https://cs.opensource.google/go/go/+/refs/tags/go1.23.4:src/crypto/crypto.go;l=68
"dm-verity": {
"sha256",
},
}
func contains[V int | string](l []V, i V) bool {
for _, v := range l {
if v == i {
return true
}
}
return false
}
func toHash(s string) crypto.Hash {
switch s {
case "sha256":
return crypto.SHA256
default:
return 0
}
}
// SnapIntegrityData holds information about integrity data of a specific type included in a snap's revision.
//
// A single snap revision can have multiple variants of integrity data which are represented as an array in the
// snap revision assertion.
type SnapIntegrityData struct {
Type string
Version uint
HashAlg string
DataBlockSize uint
HashBlockSize uint
Digest string
Salt string
}
// SnapFileSHA3_384 computes the SHA3-384 digest of the given snap file.
// It also returns its size.
func SnapFileSHA3_384(snapPath string) (digest string, size uint64, err error) {
sha3_384Dgst, size, err := osutil.FileDigest(snapPath, crypto.SHA3_384)
if err != nil {
return "", 0, fmt.Errorf("cannot compute snap %q digest: %v", snapPath, err)
}
sha3_384, err := EncodeDigest(crypto.SHA3_384, sha3_384Dgst)
if err != nil {
return "", 0, fmt.Errorf("cannot encode snap %q digest: %v", snapPath, err)
}
return sha3_384, size, nil
}
// SnapBuild holds a snap-build assertion, asserting the properties of a snap
// at the time it was built by the developer.
type SnapBuild struct {
assertionBase
size uint64
timestamp time.Time
}
// SnapSHA3_384 returns the SHA3-384 digest of the snap.
func (snapbld *SnapBuild) SnapSHA3_384() string {
return snapbld.HeaderString("snap-sha3-384")
}
// SnapID returns the snap id of the snap.
func (snapbld *SnapBuild) SnapID() string {
return snapbld.HeaderString("snap-id")
}
// SnapSize returns the size of the snap.
func (snapbld *SnapBuild) SnapSize() uint64 {
return snapbld.size
}
// Grade returns the grade of the snap: devel|stable
func (snapbld *SnapBuild) Grade() string {
return snapbld.HeaderString("grade")
}
// Timestamp returns the time when the snap-build assertion was created.
func (snapbld *SnapBuild) Timestamp() time.Time {
return snapbld.timestamp
}
func assembleSnapBuild(assert assertionBase) (Assertion, error) {
_, err := checkDigest(assert.headers, "snap-sha3-384", crypto.SHA3_384)
if err != nil {
return nil, err
}
_, err = checkNotEmptyString(assert.headers, "snap-id")
if err != nil {
return nil, err
}
_, err = checkNotEmptyString(assert.headers, "grade")
if err != nil {
return nil, err
}
size, err := checkUint(assert.headers, "snap-size", 64)
if err != nil {
return nil, err
}
timestamp, err := checkRFC3339Date(assert.headers, "timestamp")
if err != nil {
return nil, err
}
// ignore extra headers and non-empty body for future compatibility
return &SnapBuild{
assertionBase: assert,
size: size,
timestamp: timestamp,
}, nil
}
// SnapRevision holds a snap-revision assertion, which is a statement by the
// store acknowledging the receipt of a build of a snap and labeling it with a
// snap revision.
type SnapRevision struct {
assertionBase
snapSize uint64
snapRevision int
timestamp time.Time
snapIntegrityData []SnapIntegrityData
}
// SnapSHA3_384 returns the SHA3-384 digest of the snap.
func (snaprev *SnapRevision) SnapSHA3_384() string {
return snaprev.HeaderString("snap-sha3-384")
}
// Provenance returns the optional provenance of the snap (defaults to
// global-upload (naming.DefaultProvenance)).
func (snaprev *SnapRevision) Provenance() string {
return snaprev.HeaderString("provenance")
}
// SnapID returns the snap id of the snap.
func (snaprev *SnapRevision) SnapID() string {
return snaprev.HeaderString("snap-id")
}
// SnapSize returns the size in bytes of the snap submitted to the store.
func (snaprev *SnapRevision) SnapSize() uint64 {
return snaprev.snapSize
}
// SnapRevision returns the revision assigned to this build of the snap.
func (snaprev *SnapRevision) SnapRevision() int {
return snaprev.snapRevision
}
// DeveloperID returns the id of the developer that submitted this build of the
// snap.
func (snaprev *SnapRevision) DeveloperID() string {
return snaprev.HeaderString("developer-id")
}
// Timestamp returns the time when the snap-revision was issued.
func (snaprev *SnapRevision) Timestamp() time.Time {
return snaprev.timestamp
}
// SnapIntegrityData returns the snap integrity data associated with the snap revision if any.
func (snaprev *SnapRevision) SnapIntegrityData() []SnapIntegrityData {
return snaprev.snapIntegrityData
}
// Implement further consistency checks.
func (snaprev *SnapRevision) checkConsistency(db RODatabase, acck *AccountKey) error {
otherProvenance := snaprev.Provenance() != naming.DefaultProvenance
if !otherProvenance && !db.IsTrustedAccount(snaprev.AuthorityID()) {
// delegating global-upload revisions is not allowed
return fmt.Errorf("snap-revision assertion for snap id %q is not signed by a store: %s", snaprev.SnapID(), snaprev.AuthorityID())
}
_, err := db.Find(AccountType, map[string]string{
"account-id": snaprev.DeveloperID(),
})
if errors.Is(err, &NotFoundError{}) {
return fmt.Errorf("snap-revision assertion for snap id %q does not have a matching account assertion for the developer %q", snaprev.SnapID(), snaprev.DeveloperID())
}
if err != nil {
return err
}
a, err := db.Find(SnapDeclarationType, map[string]string{
// XXX: mediate getting current series through some context object? this gets the job done for now
"series": release.Series,
"snap-id": snaprev.SnapID(),
})
if errors.Is(err, &NotFoundError{}) {
return fmt.Errorf("snap-revision assertion for snap id %q does not have a matching snap-declaration assertion", snaprev.SnapID())
}
if err != nil {
return err
}
if otherProvenance {
decl := a.(*SnapDeclaration)
ras := decl.RevisionAuthority(snaprev.Provenance())
matchingRevAuthority := false
for _, ra := range ras {
// model==store==nil, we do not perform device-specific
// checks at this level, those are performed at
// higher-level guarding installing actual snaps
if err := ra.Check(snaprev, nil, nil); err == nil {
matchingRevAuthority = true
break
}
}
if !matchingRevAuthority {
return fmt.Errorf("snap-revision assertion with provenance %q for snap id %q is not signed by an authorized authority: %s", snaprev.Provenance(), snaprev.SnapID(), snaprev.AuthorityID())
}
}
return nil
}
// expected interface is implemented
var _ consistencyChecker = (*SnapRevision)(nil)
// Prerequisites returns references to this snap-revision's prerequisite assertions.
func (snaprev *SnapRevision) Prerequisites() []*Ref {
return []*Ref{
// XXX: mediate getting current series through some context object? this gets the job done for now
{Type: SnapDeclarationType, PrimaryKey: []string{release.Series, snaprev.SnapID()}},
{Type: AccountType, PrimaryKey: []string{snaprev.DeveloperID()}},
}
}
func checkSnapRevisionWhat(headers map[string]any, name, what string) (snapRevision int, err error) {
snapRevision, err = checkIntWhat(headers, name, what)
if err != nil {
return 0, err
}
if snapRevision < 1 {
return 0, fmt.Errorf(`%q %s must be >=1: %d`, name, what, snapRevision)
}
return snapRevision, nil
}
func checkOptionalSnapRevisionWhat(headers map[string]any, name, what string) (snapRevision int, err error) {
if _, ok := headers[name]; !ok {
return 0, nil
}
return checkSnapRevisionWhat(headers, name, what)
}
func checkSnapIntegrity(headers map[string]any) ([]SnapIntegrityData, error) {
value, ok := headers["integrity"]
if !ok {
// integrity stanzas are optional
return nil, nil
}
integrityList, ok := value.([]any)
if !ok {
return nil, fmt.Errorf(`"integrity" header must contain a list of integrity data`)
}
if len(integrityList) == 0 {
return nil, nil
}
var snapIntegrityDataList []SnapIntegrityData
for i, il := range integrityList {
id, ok := il.(map[string]any)
if !ok {
return nil, fmt.Errorf(`"integrity" header must contain a list of integrity data`)
}
what := fmt.Sprintf("of integrity data [%d]", i)
typ, err := checkExistsStringWhat(id, "type", what)
if err != nil {
return nil, err
}
if !contains(validSnapIntegrityTypes, typ) {
return nil, fmt.Errorf("\"type\" of integrity data [%d] must be one of (%s)", i, strings.Join(validSnapIntegrityTypes, "|"))
}
what = fmt.Sprintf("of integrity data [%d] of type %q", i, typ)
version, err := checkUintWhat(id, "version", 64, what)
if err != nil {
return nil, err
}
if !contains(validVersionsForIntegrityType[typ], int(version)) {
return nil, fmt.Errorf(`version of integrity data [%d] of type %q must be one of %v`, i, typ, validVersionsForIntegrityType[typ])
}
alg, err := checkExistsStringWhat(id, "hash-algorithm", what)
if err != nil {
return nil, err
}
if !contains(validHashAlgorithmsForIntegrityType[typ], alg) {
return nil, fmt.Errorf(`hash algorithm of integrity data [%d] of type %q must be one of %v`, i, typ, validHashAlgorithmsForIntegrityType[typ])
}
what = fmt.Sprintf("of integrity data [%d] of type %q (%s)", i, typ, alg)
dataBlockSize, err := checkUintWhat(id, "data-block-size", 64, what)
if err != nil {
return nil, err
}
hashBlockSize, err := checkUintWhat(id, "hash-block-size", 64, what)
if err != nil {
return nil, err
}
h := toHash(alg)
encDigest, err := checkDigestDecWhat(id, "digest", h, hex.DecodeString, what)
if err != nil {
return nil, err
}
encSalt, err := checkDigestDecWhat(id, "salt", h, hex.DecodeString, what)
if err != nil {
return nil, err
}
snapIntegrityData := SnapIntegrityData{
Type: typ,
Version: uint(version),
HashAlg: alg,
DataBlockSize: uint(dataBlockSize),
HashBlockSize: uint(hashBlockSize),
Digest: encDigest,
Salt: encSalt,
}
snapIntegrityDataList = append(snapIntegrityDataList, snapIntegrityData)
}
return snapIntegrityDataList, nil
}
func assembleSnapRevision(assert assertionBase) (Assertion, error) {
_, err := checkDigest(assert.headers, "snap-sha3-384", crypto.SHA3_384)
if err != nil {
return nil, err
}
_, err = checkStringMatches(assert.headers, "provenance", naming.ValidProvenance)
if err != nil {
return nil, err
}
_, err = checkNotEmptyString(assert.headers, "snap-id")
if err != nil {
return nil, err
}
snapSize, err := checkUint(assert.headers, "snap-size", 64)
if err != nil {
return nil, err
}
snapRevision, err := checkSnapRevisionWhat(assert.headers, "snap-revision", "header")
if err != nil {
return nil, err
}
_, err = checkNotEmptyString(assert.headers, "developer-id")
if err != nil {
return nil, err
}
timestamp, err := checkRFC3339Date(assert.headers, "timestamp")
if err != nil {
return nil, err
}
snapIntegrityData, err := checkSnapIntegrity(assert.headers)
if err != nil {
return nil, err
}
return &SnapRevision{
assertionBase: assert,
snapSize: snapSize,
snapRevision: snapRevision,
timestamp: timestamp,
snapIntegrityData: snapIntegrityData,
}, nil
}
// Validation holds a validation assertion, describing that a combination of
// (snap-id, approved-snap-id, approved-revision) has been validated for
// the series, meaning updating to that revision of approved-snap-id
// has been approved by the owner of the gating snap with snap-id.
type Validation struct {
assertionBase
revoked bool
timestamp time.Time
approvedSnapRevision int
}
// Series returns the series for which the validation holds.
func (validation *Validation) Series() string {
return validation.HeaderString("series")
}
// SnapID returns the ID of the gating snap.
func (validation *Validation) SnapID() string {
return validation.HeaderString("snap-id")
}
// ApprovedSnapID returns the ID of the gated snap.
func (validation *Validation) ApprovedSnapID() string {
return validation.HeaderString("approved-snap-id")
}
// ApprovedSnapRevision returns the approved revision of the gated snap.
func (validation *Validation) ApprovedSnapRevision() int {
return validation.approvedSnapRevision
}
// Revoked returns true if the validation has been revoked.
func (validation *Validation) Revoked() bool {
return validation.revoked
}
// Timestamp returns the time when the validation was issued.
func (validation *Validation) Timestamp() time.Time {
return validation.timestamp
}
// Implement further consistency checks.
func (validation *Validation) checkConsistency(db RODatabase, acck *AccountKey) error {
_, err := db.Find(SnapDeclarationType, map[string]string{
"series": validation.Series(),
"snap-id": validation.ApprovedSnapID(),
})
if errors.Is(err, &NotFoundError{}) {
return fmt.Errorf("validation assertion by snap-id %q does not have a matching snap-declaration assertion for approved-snap-id %q", validation.SnapID(), validation.ApprovedSnapID())
}
if err != nil {
return err
}
a, err := db.Find(SnapDeclarationType, map[string]string{
"series": validation.Series(),
"snap-id": validation.SnapID(),
})
if errors.Is(err, &NotFoundError{}) {
return fmt.Errorf("validation assertion by snap-id %q does not have a matching snap-declaration assertion", validation.SnapID())
}
if err != nil {
return err
}
gatingDecl := a.(*SnapDeclaration)
if gatingDecl.PublisherID() != validation.AuthorityID() {
return fmt.Errorf("validation assertion by snap %q (id %q) not signed by its publisher", gatingDecl.SnapName(), validation.SnapID())
}
return nil
}
// expected interface is implemented
var _ consistencyChecker = (*Validation)(nil)
// Prerequisites returns references to this validation's prerequisite assertions.
func (validation *Validation) Prerequisites() []*Ref {
return []*Ref{
{Type: SnapDeclarationType, PrimaryKey: []string{validation.Series(), validation.SnapID()}},
{Type: SnapDeclarationType, PrimaryKey: []string{validation.Series(), validation.ApprovedSnapID()}},
}
}
func assembleValidation(assert assertionBase) (Assertion, error) {
approvedSnapRevision, err := checkSnapRevisionWhat(assert.headers, "approved-snap-revision", "header")
if err != nil {
return nil, err
}
revoked, err := checkOptionalBool(assert.headers, "revoked")
if err != nil {
return nil, err
}
timestamp, err := checkRFC3339Date(assert.headers, "timestamp")
if err != nil {
return nil, err
}
return &Validation{
assertionBase: assert,
revoked: revoked,
timestamp: timestamp,
approvedSnapRevision: approvedSnapRevision,
}, nil
}
// BaseDeclaration holds a base-declaration assertion, declaring the
// policies (to start with interface ones) applying to all snaps of
// a series.
type BaseDeclaration struct {
assertionBase
plugRules map[string]*PlugRule
slotRules map[string]*SlotRule
timestamp time.Time
}
// Series returns the series whose snaps are governed by the declaration.
func (basedcl *BaseDeclaration) Series() string {
return basedcl.HeaderString("series")
}
// Timestamp returns the time when the base-declaration was issued.
func (basedcl *BaseDeclaration) Timestamp() time.Time {
return basedcl.timestamp
}
// PlugRule returns the plug-side rule about the given interface if one was included in the plugs stanza of the declaration, otherwise it returns nil.
func (basedcl *BaseDeclaration) PlugRule(interfaceName string) *PlugRule {
return basedcl.plugRules[interfaceName]
}
// SlotRule returns the slot-side rule about the given interface if one was included in the slots stanza of the declaration, otherwise it returns nil.
func (basedcl *BaseDeclaration) SlotRule(interfaceName string) *SlotRule {
return basedcl.slotRules[interfaceName]
}
// Implement further consistency checks.
func (basedcl *BaseDeclaration) checkConsistency(db RODatabase, acck *AccountKey) error {
// XXX: not signed or stored yet in a db, but being ready for that
if !db.IsTrustedAccount(basedcl.AuthorityID()) {
return fmt.Errorf("base-declaration assertion for series %s is not signed by a directly trusted authority: %s", basedcl.Series(), basedcl.AuthorityID())
}
return nil
}
// expected interface is implemented
var _ consistencyChecker = (*BaseDeclaration)(nil)
func assembleBaseDeclaration(assert assertionBase) (Assertion, error) {
var plugRules map[string]*PlugRule
plugs, err := checkMap(assert.headers, "plugs")
if err != nil {
return nil, err
}
if plugs != nil {
plugRules = make(map[string]*PlugRule, len(plugs))
err := compilePlugRules(plugs, func(iface string, rule *PlugRule) {
plugRules[iface] = rule
})
if err != nil {
return nil, err
}
}
var slotRules map[string]*SlotRule
slots, err := checkMap(assert.headers, "slots")
if err != nil {
return nil, err
}
if slots != nil {
slotRules = make(map[string]*SlotRule, len(slots))
err := compileSlotRules(slots, func(iface string, rule *SlotRule) {
slotRules[iface] = rule
})
if err != nil {
return nil, err
}
}
timestamp, err := checkRFC3339Date(assert.headers, "timestamp")
if err != nil {
return nil, err
}
return &BaseDeclaration{
assertionBase: assert,
plugRules: plugRules,
slotRules: slotRules,
timestamp: timestamp,
}, nil
}
var builtinBaseDeclaration *BaseDeclaration
// BuiltinBaseDeclaration exposes the initialized builtin base-declaration assertion. This is used by overlord/assertstate, other code should use assertstate.BaseDeclaration.
func BuiltinBaseDeclaration() *BaseDeclaration {
return builtinBaseDeclaration
}
var (
builtinBaseDeclarationCheckOrder = []string{"type", "authority-id", "series"}
builtinBaseDeclarationExpectedHeaders = map[string]any{
"type": "base-declaration",
"authority-id": "canonical",
"series": release.Series,
}
)
// InitBuiltinBaseDeclaration initializes the builtin base-declaration based on headers (or resets it if headers is nil).
func InitBuiltinBaseDeclaration(headers []byte) error {
if headers == nil {
builtinBaseDeclaration = nil
return nil
}
trimmed := bytes.TrimSpace(headers)
h, err := parseHeaders(trimmed)
if err != nil {
return err
}
for _, name := range builtinBaseDeclarationCheckOrder {
expected := builtinBaseDeclarationExpectedHeaders[name]
if h[name] != expected {
return fmt.Errorf("the builtin base-declaration %q header is not set to expected value %q", name, expected)
}
}
revision, err := checkRevision(h)
if err != nil {
return fmt.Errorf("cannot assemble the builtin-base declaration: %v", err)
}
h["timestamp"] = time.Now().UTC().Format(time.RFC3339)
a, err := assembleBaseDeclaration(assertionBase{
headers: h,
body: nil,
revision: revision,
content: trimmed,
signature: []byte("$builtin"),
})
if err != nil {
return fmt.Errorf("cannot assemble the builtin base-declaration: %v", err)
}
builtinBaseDeclaration = a.(*BaseDeclaration)
return nil
}
type dateRange struct {
Since time.Time
Until time.Time
}
// SnapDeveloper holds a snap-developer assertion, defining the developers who
// can collaborate on a snap while it's owned by a specific publisher.
//
// The primary key (snap-id, publisher-id) allows a snap to have many
// snap-developer assertions, e.g. to allow a future publisher's collaborations
// to be defined before the snap is transferred. However only the
// snap-developer for the current publisher (the snap-declaration publisher-id)
// is relevant to a device.
type SnapDeveloper struct {
assertionBase
developerRanges map[string][]*dateRange
}
// SnapID returns the snap id of the snap.
func (snapdev *SnapDeveloper) SnapID() string {
return snapdev.HeaderString("snap-id")
}
// PublisherID returns the publisher's account id.
func (snapdev *SnapDeveloper) PublisherID() string {
return snapdev.HeaderString("publisher-id")
}
func (snapdev *SnapDeveloper) checkConsistency(db RODatabase, acck *AccountKey) error {
// Check authority is the publisher or trusted.
authorityID := snapdev.AuthorityID()
publisherID := snapdev.PublisherID()
if !db.IsTrustedAccount(authorityID) && (publisherID != authorityID) {
return fmt.Errorf("snap-developer must be signed by the publisher or a trusted authority but got authority %q and publisher %q", authorityID, publisherID)
}
// Check snap-declaration for the snap-id exists for the series.
// Note: the current publisher is irrelevant here because this assertion
// may be for a future publisher.
_, err := db.Find(SnapDeclarationType, map[string]string{
// XXX: mediate getting current series through some context object? this gets the job done for now
"series": release.Series,
"snap-id": snapdev.SnapID(),
})
if err != nil {
if errors.Is(err, &NotFoundError{}) {
return fmt.Errorf("snap-developer assertion for snap id %q does not have a matching snap-declaration assertion", snapdev.SnapID())
}
return err
}
// check there's an account for the publisher-id
_, err = db.Find(AccountType, map[string]string{"account-id": publisherID})
if err != nil {
if errors.Is(err, &NotFoundError{}) {
return fmt.Errorf("snap-developer assertion for snap-id %q does not have a matching account assertion for the publisher %q", snapdev.SnapID(), publisherID)
}
return err
}
// check there's an account for each developer
for developerID := range snapdev.developerRanges {
if developerID == publisherID {
continue
}
_, err = db.Find(AccountType, map[string]string{"account-id": developerID})
if err != nil {
if errors.Is(err, &NotFoundError{}) {
return fmt.Errorf("snap-developer assertion for snap-id %q does not have a matching account assertion for the developer %q", snapdev.SnapID(), developerID)
}
return err
}
}
return nil
}
// expected interface is implemented
var _ consistencyChecker = (*SnapDeveloper)(nil)
// Prerequisites returns references to this snap-developer's prerequisite assertions.
func (snapdev *SnapDeveloper) Prerequisites() []*Ref {
// Capacity for the snap-declaration, the publisher and all developers.
refs := make([]*Ref, 0, 2+len(snapdev.developerRanges))
// snap-declaration
// XXX: mediate getting current series through some context object? this gets the job done for now
refs = append(refs, &Ref{SnapDeclarationType, []string{release.Series, snapdev.SnapID()}})
// the publisher and developers
publisherID := snapdev.PublisherID()
refs = append(refs, &Ref{AccountType, []string{publisherID}})
for developerID := range snapdev.developerRanges {
if developerID != publisherID {
refs = append(refs, &Ref{AccountType, []string{developerID}})
}
}
return refs
}
func assembleSnapDeveloper(assert assertionBase) (Assertion, error) {
developerRanges, err := checkDevelopers(assert.headers)
if err != nil {
return nil, err
}
return &SnapDeveloper{
assertionBase: assert,
developerRanges: developerRanges,
}, nil
}
func checkDevelopers(headers map[string]any) (map[string][]*dateRange, error) {
value, ok := headers["developers"]
if !ok {
return nil, nil
}
developers, ok := value.([]any)
if !ok {
return nil, fmt.Errorf(`"developers" must be a list of developer maps`)
}
if len(developers) == 0 {
return nil, nil
}
// Used to check for a developer with revoking and non-revoking items.
// No entry means developer not yet seen, false means seen but not revoked,
// true means seen and revoked.
revocationStatus := map[string]bool{}
developerRanges := make(map[string][]*dateRange)
for i, item := range developers {
developer, ok := item.(map[string]any)
if !ok {
return nil, fmt.Errorf(`"developers" must be a list of developer maps`)
}
what := fmt.Sprintf(`in "developers" item %d`, i+1)
accountID, err := checkStringMatchesWhat(developer, "developer-id", what, validAccountID)
if err != nil {
return nil, err
}
what = fmt.Sprintf(`in "developers" item %d for developer %q`, i+1, accountID)
since, err := checkRFC3339DateWhat(developer, "since", what)
if err != nil {
return nil, err
}
until, err := checkRFC3339DateWithDefaultWhat(developer, "until", what, time.Time{})
if err != nil {
return nil, err
}
if !until.IsZero() && since.After(until) {
return nil, fmt.Errorf(`"since" %s must be less than or equal to "until"`, what)
}
// Track/check for revocation conflicts.
revoked := since.Equal(until)
previouslyRevoked, ok := revocationStatus[accountID]
if !ok {
revocationStatus[accountID] = revoked
} else if previouslyRevoked || revoked {
return nil, fmt.Errorf(`revocation for developer %q must be standalone but found other "developers" items`, accountID)
}
developerRanges[accountID] = append(developerRanges[accountID], &dateRange{since, until})
}
return developerRanges, nil
}
|