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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 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 assertstest provides helpers for testing code that involves assertions.
package assertstest
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"errors"
"fmt"
"io"
"os/exec"
"strings"
"time"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/randutil"
)
// GenerateKey generates a private/public key pair of the given bits. It panics on error.
func GenerateKey(bits int) (asserts.PrivateKey, *rsa.PrivateKey) {
priv, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
panic(fmt.Errorf("failed to create private key: %v", err))
}
return asserts.RSAPrivateKey(priv), priv
}
// ReadPrivKey reads a PGP private key (either armored or simply base64 encoded). It panics on error.
func ReadPrivKey(pk string) (asserts.PrivateKey, *rsa.PrivateKey) {
rd := bytes.NewReader([]byte(pk))
blk, err := armor.Decode(rd)
var body io.Reader
if err == nil {
body = blk.Body
} else {
rd.Seek(0, 0)
// try unarmored
body = base64.NewDecoder(base64.StdEncoding, rd)
}
pkt, err := packet.Read(body)
if err != nil {
panic(err)
}
pkPkt := pkt.(*packet.PrivateKey)
rsaPrivKey, ok := pkPkt.PrivateKey.(*rsa.PrivateKey)
if !ok {
panic("not a RSA key")
}
return asserts.RSAPrivateKey(rsaPrivKey), rsaPrivKey
}
// A sample developer key.
// See systestkeys for a prebuilt set of trusted keys and assertions.
const (
DevKey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: GnuPG v1
lQcYBFaFwYABEAC0kYiC4rsWFLJHEv/qO93LTMCAYKMLXFU0XN4XvqnkbwFc0QQd
lQcr7PwavYmKdWum+EmGWV/k5vZ0gwfZhBsL2MTWSNvO+5q5AYOqTq01CbSLcoN4
cJI+BU348Vc/AoiIuuHro+gALs59HWsVSAKq7SNyHQfo257TKe8Q+Jjh095eruYJ
2kOvlAgAzjUv7eGDQ53O87wcwgZlCl0XqM/t+SRUxE5i8dQ4nySSekoTsWJo02kf
uMrWo3E5iEt6KKhfQtit2ZO91NYetIplzzZmaUOOkpziFTFW1NcwDKzDsLMh1EQ+
ib+8mSWcou9m35aTkAQXlXlgqe5Pelj5+NUxnnoa1MR478Sv+guT+fbFQrl8PkMD
Jb/3PTKDPBNtjki5ZfIN9id4vidfBY4SCDftnj7yZMf5+1PPZ2XXHUoiUhHbGjST
F/23wr6OWvXe/AXX5BF4wJJTJxSxnYR6nleGMj4sbsbVsxIaqh1lMg5cuQjLr7eI
nxn994geUnQQsEPIVuVjLThJ/0sjXjy8kyxh6eieShZ6NZ0yLyIJRN5pnJ0ckRQF
T9Fs0UuMJZro0hR71t9mAuI45mSmznj78AvTvyuL+0aOj/lQa97NKbCsShYnKqsm
3Yzr03ahUMslwd6jZtRg+0ULYp9vaN7nwmsn6WWJ92CsCzFucdeJfJWKZQARAQAB
AA/9GSda3mzKRhms+hSt/MnJLFxtRpTvsZHztp8nOySO0ykZhf4B9kL/5EEXn3v+
0IBp9jEJQQNrRd5cv79PFSB/igdw6C7vG+bV12bcGhnqrARFl9Vkdh8saCJiCcdI
8ZifP3jVJvfGxlu+3RP/ik/lOz1cnjVoG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xfaWMjH+tuWYPIHzYsT+TfsN0vAEFyhRi8Ncelu1RV4
x2O3wmjxoaX/2FmyuU5WhcVkcpRFgceyf1/86NP9gT5MKbWtJC85YYpxibnvPdGd
+sqtEEqgX3dSsHT+rkBk7kf3ghDwsLtnliFPOeAaIHGZl754EpK+qPUTnYZK022H
2crhYlApO9+06kBeybSO6joMUR007883I9GELYhzmuEjpVGquJQ3+S5QtW1to0w=
=5Myf
-----END PGP PRIVATE KEY BLOCK-----
`
DevKeyID = "EAD4DbLxK_kn0gzNCXOs3kd6DeMU3f-L6BEsSEuJGBqCORR0gXkdDxMbOm11mRFu"
DevKeyPGPFingerprint = "966e70f4b9f257a2772f8f354a423d28aaea4b4f"
)
// GPGImportKey imports the given PGP armored key into the GnuPG setup at homedir. It panics on error.
func GPGImportKey(homedir, armoredKey string) {
path, err := exec.LookPath("gpg1")
if err != nil {
path, err = exec.LookPath("gpg")
}
if err != nil {
panic(err)
}
gpg := exec.Command(path, "--homedir", homedir, "-q", "--batch", "--import", "--armor")
gpg.Stdin = bytes.NewBufferString(armoredKey)
out, err := gpg.CombinedOutput()
if err != nil {
panic(fmt.Errorf("cannot import test key into GPG setup at %q: %v (%q)", homedir, err, out))
}
}
// A SignerDB can sign assertions using its key pairs.
type SignerDB interface {
Sign(assertType *asserts.AssertionType, headers map[string]any, body []byte, keyID string) (asserts.Assertion, error)
}
// NewAccount creates an account assertion for username, it fills in values for
// other missing headers as needed. It panics on error.
func NewAccount(db SignerDB, username string, otherHeaders map[string]any, keyID string) *asserts.Account {
if otherHeaders == nil {
otherHeaders = make(map[string]any)
}
otherHeaders["username"] = username
if otherHeaders["account-id"] == nil {
otherHeaders["account-id"] = randutil.RandomString(32)
}
if otherHeaders["display-name"] == nil {
otherHeaders["display-name"] = strings.ToTitle(username[:1]) + username[1:]
}
if otherHeaders["validation"] == nil {
otherHeaders["validation"] = "unproven"
}
if otherHeaders["timestamp"] == nil {
otherHeaders["timestamp"] = time.Now().Format(time.RFC3339)
}
a, err := db.Sign(asserts.AccountType, otherHeaders, nil, keyID)
if err != nil {
panic(err)
}
return a.(*asserts.Account)
}
// NewAccountKey creates an account-key assertion for the account, it fills in
// values for missing headers as needed. In panics on error.
func NewAccountKey(db SignerDB, acct *asserts.Account, otherHeaders map[string]any, pubKey asserts.PublicKey, keyID string) *asserts.AccountKey {
if otherHeaders == nil {
otherHeaders = make(map[string]any)
}
otherHeaders["account-id"] = acct.AccountID()
otherHeaders["public-key-sha3-384"] = pubKey.ID()
if otherHeaders["name"] == nil {
otherHeaders["name"] = "default"
}
if otherHeaders["since"] == nil {
otherHeaders["since"] = time.Now().Format(time.RFC3339)
}
encodedPubKey, err := asserts.EncodePublicKey(pubKey)
if err != nil {
panic(err)
}
a, err := db.Sign(asserts.AccountKeyType, otherHeaders, encodedPubKey, keyID)
if err != nil {
panic(err)
}
return a.(*asserts.AccountKey)
}
// SigningDB embeds a signing assertion database with a default private key and assigned authority id.
// Sign will use the assigned authority id.
// "" can be passed for keyID to Sign and PublicKey to use the default key.
type SigningDB struct {
AuthorityID string
KeyID string
*asserts.Database
}
// NewSigningDB creates a test signing assertion db with the given defaults. It panics on error.
func NewSigningDB(authorityID string, privKey asserts.PrivateKey) *SigningDB {
db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{})
if err != nil {
panic(err)
}
err = db.ImportKey(privKey)
if err != nil {
panic(err)
}
return &SigningDB{
AuthorityID: authorityID,
KeyID: privKey.PublicKey().ID(),
Database: db,
}
}
func (db *SigningDB) Sign(assertType *asserts.AssertionType, headers map[string]any, body []byte, keyID string) (asserts.Assertion, error) {
if _, ok := headers["authority-id"]; !ok {
// copy before modifying
headers2 := make(map[string]any, len(headers)+1)
for h, v := range headers {
headers2[h] = v
}
headers = headers2
headers["authority-id"] = db.AuthorityID
}
if keyID == "" {
keyID = db.KeyID
}
return db.Database.Sign(assertType, headers, body, keyID)
}
func (db *SigningDB) PublicKey(keyID string) (asserts.PublicKey, error) {
if keyID == "" {
keyID = db.KeyID
}
return db.Database.PublicKey(keyID)
}
// StoreStack realises a store-like set of founding trusted assertions and signing setup.
type StoreStack struct {
// Trusted authority assertions.
TrustedAccount *asserts.Account
TrustedKey *asserts.AccountKey
Trusted []asserts.Assertion
// Generic authority assertions.
GenericAccount *asserts.Account
GenericKey *asserts.AccountKey
GenericModelsKey *asserts.AccountKey
Generic []asserts.Assertion
GenericClassicModel *asserts.Model
// Signing assertion db that signs with the root private key.
RootSigning *SigningDB
// The store-like signing functionality that signs with a store key, setup to also store assertions if desired. It stores a default account-key for the store private key, see also the StoreStack.Key method.
*SigningDB
}
// StoreKeys holds a set of store private keys.
type StoreKeys struct {
Root asserts.PrivateKey
Store asserts.PrivateKey
Generic asserts.PrivateKey
GenericModels asserts.PrivateKey
}
var (
rootPrivKey, _ = GenerateKey(1024)
storePrivKey, _ = GenerateKey(752)
genericPrivKey, _ = GenerateKey(752)
genericModelsPrivKey, _ = GenerateKey(752)
pregenKeys = StoreKeys{
Root: rootPrivKey,
Store: storePrivKey,
Generic: genericPrivKey,
GenericModels: genericModelsPrivKey,
}
)
// NewStoreStack creates a new store assertion stack. It panics on error.
// Optional keys specify private keys to use for the various roles.
func NewStoreStack(authorityID string, keys *StoreKeys) *StoreStack {
if keys == nil {
keys = &pregenKeys
}
rootSigning := NewSigningDB(authorityID, keys.Root)
ts := time.Now().Format(time.RFC3339)
trustedAcct := NewAccount(rootSigning, authorityID, map[string]any{
"account-id": authorityID,
"validation": "verified",
"timestamp": ts,
}, "")
trustedKey := NewAccountKey(rootSigning, trustedAcct, map[string]any{
"name": "root",
"since": ts,
}, keys.Root.PublicKey(), "")
trusted := []asserts.Assertion{trustedAcct, trustedKey}
genericAcct := NewAccount(rootSigning, "generic", map[string]any{
"account-id": "generic",
"validation": "verified",
"timestamp": ts,
}, "")
err := rootSigning.ImportKey(keys.GenericModels)
if err != nil {
panic(err)
}
genericModelsKey := NewAccountKey(rootSigning, genericAcct, map[string]any{
"name": "models",
"since": ts,
}, keys.GenericModels.PublicKey(), "")
generic := []asserts.Assertion{genericAcct, genericModelsKey}
db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{
Backstore: asserts.NewMemoryBackstore(),
Trusted: trusted,
OtherPredefined: generic,
})
if err != nil {
panic(err)
}
err = db.ImportKey(keys.Store)
if err != nil {
panic(err)
}
storeKey := NewAccountKey(rootSigning, trustedAcct, map[string]any{
"name": "store",
}, keys.Store.PublicKey(), "")
err = db.Add(storeKey)
if err != nil {
panic(err)
}
err = db.ImportKey(keys.Generic)
if err != nil {
panic(err)
}
genericKey := NewAccountKey(rootSigning, genericAcct, map[string]any{
"name": "serials",
"since": ts,
}, keys.Generic.PublicKey(), "")
err = db.Add(genericKey)
if err != nil {
panic(err)
}
a, err := rootSigning.Sign(asserts.ModelType, map[string]any{
"authority-id": "generic",
"series": "16",
"brand-id": "generic",
"model": "generic-classic",
"classic": "true",
"timestamp": ts,
}, nil, genericModelsKey.PublicKeyID())
if err != nil {
panic(err)
}
genericClassicMod := a.(*asserts.Model)
return &StoreStack{
TrustedAccount: trustedAcct,
TrustedKey: trustedKey,
Trusted: trusted,
GenericAccount: genericAcct,
GenericKey: genericKey,
GenericModelsKey: genericModelsKey,
Generic: generic,
GenericClassicModel: genericClassicMod,
RootSigning: rootSigning,
SigningDB: &SigningDB{
AuthorityID: authorityID,
KeyID: storeKey.PublicKeyID(),
Database: db,
},
}
}
// StoreAccountKey retrieves one of the account-key assertions for the signing keys of the simulated store signing database.
// "" for keyID means the default one. It panics on error.
func (ss *StoreStack) StoreAccountKey(keyID string) *asserts.AccountKey {
if keyID == "" {
keyID = ss.KeyID
}
key, err := ss.Find(asserts.AccountKeyType, map[string]string{
"account-id": ss.AuthorityID,
"public-key-sha3-384": keyID,
})
if errors.Is(err, &asserts.NotFoundError{}) {
return nil
}
if err != nil {
panic(err)
}
return key.(*asserts.AccountKey)
}
// MockBuiltinBaseDeclaration mocks the builtin base-declaration exposed by asserts.BuiltinBaseDeclaration.
func MockBuiltinBaseDeclaration(headers []byte) (restore func()) {
var prevHeaders []byte
decl := asserts.BuiltinBaseDeclaration()
if decl != nil {
prevHeaders, _ = decl.Signature()
}
err := asserts.InitBuiltinBaseDeclaration(headers)
if err != nil {
panic(err)
}
return func() {
err := asserts.InitBuiltinBaseDeclaration(prevHeaders)
if err != nil {
panic(err)
}
}
}
// FakeAssertionWithBody builds a fake assertion with the given body
// and layered headers. A fake assertion cannot be verified or added
// to a database or properly encoded. It can still be useful for unit
// tests but shouldn't be used in integration tests.
func FakeAssertionWithBody(body []byte, headerLayers ...map[string]any) asserts.Assertion {
headers := map[string]any{
"sign-key-sha3-384": "Jv8_JiHiIzJVcO9M55pPdqSDWUvuhfDIBJUS-3VW7F_idjix7Ffn5qMxB21ZQuij",
}
for _, h := range headerLayers {
for k, v := range h {
headers[k] = v
}
}
_, hasTimestamp := headers["timestamp"]
_, hasSince := headers["since"]
if !(hasTimestamp || hasSince) {
headers["timestamp"] = time.Now().Format(time.RFC3339)
}
a, err := asserts.Assemble(headers, body, nil, []byte("AXNpZw=="))
if err != nil {
panic(fmt.Sprintf("cannot build fake assertion: %v", err))
}
return a
}
// FakeAssertion builds a fake assertion with given layered headers
// and an empty body. A fake assertion cannot be verified or added to
// a database or properly encoded. It can still be useful for unit
// tests but shouldn't be used in integration tests.
func FakeAssertion(headerLayers ...map[string]any) asserts.Assertion {
return FakeAssertionWithBody(nil, headerLayers...)
}
type accuDB interface {
Add(asserts.Assertion) error
}
// AddMany conveniently adds the given assertions to the db.
// It is idempotent but otherwise panics on error.
func AddMany(db accuDB, assertions ...asserts.Assertion) {
for _, a := range assertions {
err := db.Add(a)
if _, ok := err.(*asserts.RevisionError); !ok {
if err != nil {
panic(fmt.Sprintf("cannot add test assertions: %v", err))
}
}
}
}
// SigningAccounts manages a set of brand or user accounts,
// with their keys that can sign models etc.
type SigningAccounts struct {
store *StoreStack
signing map[string]*SigningDB
accts map[string]*asserts.Account
acctKeys map[string]*asserts.AccountKey
}
// NewSigningAccounts creates a new SigningAccounts instance.
func NewSigningAccounts(store *StoreStack) *SigningAccounts {
return &SigningAccounts{
store: store,
signing: make(map[string]*SigningDB),
accts: make(map[string]*asserts.Account),
acctKeys: make(map[string]*asserts.AccountKey),
}
}
func (sa *SigningAccounts) Register(accountID string, brandPrivKey asserts.PrivateKey, extra map[string]any) *SigningDB {
brandSigning := NewSigningDB(accountID, brandPrivKey)
sa.signing[accountID] = brandSigning
acctHeaders := map[string]any{
"account-id": accountID,
}
for k, v := range extra {
acctHeaders[k] = v
}
brandAcct := NewAccount(sa.store, accountID, acctHeaders, "")
sa.accts[accountID] = brandAcct
brandPubKey, err := brandSigning.PublicKey("")
if err != nil {
panic(err)
}
brandAcctKey := NewAccountKey(sa.store, brandAcct, nil, brandPubKey, "")
sa.acctKeys[accountID] = brandAcctKey
return brandSigning
}
func (sa *SigningAccounts) Account(accountID string) *asserts.Account {
if acct := sa.accts[accountID]; acct != nil {
return acct
}
panic(fmt.Sprintf("unknown test account-id: %s", accountID))
}
func (sa *SigningAccounts) AccountKey(accountID string) *asserts.AccountKey {
if acctKey := sa.acctKeys[accountID]; acctKey != nil {
return acctKey
}
panic(fmt.Sprintf("unknown test account-id: %s", accountID))
}
func (sa *SigningAccounts) PublicKey(accountID string) asserts.PublicKey {
pubKey, err := sa.Signing(accountID).PublicKey("")
if err != nil {
panic(err)
}
return pubKey
}
func (sa *SigningAccounts) Signing(accountID string) *SigningDB {
// convenience
if accountID == sa.store.RootSigning.AuthorityID {
return sa.store.RootSigning
}
if signer := sa.signing[accountID]; signer != nil {
return signer
}
panic(fmt.Sprintf("unknown test account-id: %s", accountID))
}
// Model creates a new model for accountID. accountID can also be the account-id of the underlying store stack.
func (sa *SigningAccounts) Model(accountID, model string, extras ...map[string]any) *asserts.Model {
headers := map[string]any{
"series": "16",
"brand-id": accountID,
"model": model,
"timestamp": time.Now().Format(time.RFC3339),
}
for _, extra := range extras {
for k, v := range extra {
headers[k] = v
}
}
signer := sa.Signing(accountID)
modelAs, err := signer.Sign(asserts.ModelType, headers, nil, "")
if err != nil {
panic(err)
}
return modelAs.(*asserts.Model)
}
// AccountsAndKeys returns the account and account-key for each given
// accountID in that order.
func (sa *SigningAccounts) AccountsAndKeys(accountIDs ...string) []asserts.Assertion {
res := make([]asserts.Assertion, 0, 2*len(accountIDs))
for _, accountID := range accountIDs {
res = append(res, sa.Account(accountID))
res = append(res, sa.AccountKey(accountID))
}
return res
}
|