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
|
// Copyright (c) 2020, Control Command Inc. All rights reserved.
// Copyright (c) 2020-2021, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.
package cli
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"github.com/sylabs/singularity/v4/internal/pkg/buildcfg"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/fatih/color"
"github.com/sylabs/sif/v2/pkg/integrity"
"github.com/sylabs/sif/v2/pkg/sif"
sifsignature "github.com/sylabs/singularity/v4/internal/pkg/signature"
"github.com/sylabs/singularity/v4/internal/pkg/sypgp"
"github.com/sylabs/singularity/v4/internal/pkg/util/interactive"
"github.com/sylabs/singularity/v4/pkg/sylog"
)
var (
errEmptyKeyring = errors.New("keyring is empty")
errIndexOutOfRange = errors.New("index out of range")
)
// printEntityAtIndex prints entity e, associated with index i, to w.
func printEntityAtIndex(w io.Writer, i int, e *openpgp.Entity) {
for _, v := range e.Identities {
fmt.Fprintf(w, "%d) U: %s (%s) <%s>\n", i, v.UserId.Name, v.UserId.Comment, v.UserId.Email)
}
fmt.Fprintf(w, " C: %s\n", e.PrimaryKey.CreationTime)
fmt.Fprintf(w, " F: %0X\n", e.PrimaryKey.Fingerprint)
bits, _ := e.PrimaryKey.BitLength()
fmt.Fprintf(w, " L: %d\n", bits)
fmt.Fprint(os.Stdout, " --------\n")
}
// selectEntityInteractive returns an EntitySelector that selects an entity from el, prompting the
// user for a selection if there is more than one entity in el.
func selectEntityInteractive() sypgp.EntitySelector {
return func(el openpgp.EntityList) (*openpgp.Entity, error) {
switch len(el) {
case 0:
return nil, errEmptyKeyring
case 1:
return el[0], nil
default:
for i, e := range el {
printEntityAtIndex(os.Stdout, i, e)
}
n, err := interactive.AskNumberInRange(0, len(el)-1, "Enter # of private key to use : ")
if err != nil {
return nil, err
}
return el[n], nil
}
}
}
// selectEntityAtIndex returns an EntitySelector that selects the entity at index i.
func selectEntityAtIndex(i int) sypgp.EntitySelector {
return func(el openpgp.EntityList) (*openpgp.Entity, error) {
if i >= len(el) {
return nil, errIndexOutOfRange
}
return el[i], nil
}
}
// decryptSelectedEntityInteractive wraps f, attempting to decrypt the private key in the selected
// entity with a passpharse provided interactively by the user.
func decryptSelectedEntityInteractive(f sypgp.EntitySelector) sypgp.EntitySelector {
return func(el openpgp.EntityList) (*openpgp.Entity, error) {
e, err := f(el)
if err != nil {
return nil, err
}
if e.PrivateKey.Encrypted {
if err := decryptPrivateKeyInteractive(e); err != nil {
return nil, err
}
}
return e, nil
}
}
// decryptPrivateKeyInteractive decrypts the private key in e, prompting the user for a passphrase.
func decryptPrivateKeyInteractive(e *openpgp.Entity) error {
passphrase, err := interactive.AskQuestionNoEcho("Enter key passphrase : ")
if err != nil {
return err
}
return e.PrivateKey.Decrypt([]byte(passphrase))
}
// primaryIdentity returns the Identity marked as primary, or the first identity if none are so
// marked.
func primaryIdentity(e *openpgp.Entity) *openpgp.Identity {
var first *openpgp.Identity
for _, id := range e.Identities {
if first == nil {
first = id
}
if id.SelfSignature.IsPrimaryId != nil && *id.SelfSignature.IsPrimaryId {
return id
}
}
return first
}
// isLocal returns true if signing entity e is found in the local keyring, and false otherwise.
func isLocal(e *openpgp.Entity) bool {
kr, err := sypgp.PublicKeyRing()
if err != nil {
return false
}
keys := kr.KeysByIdUsage(e.PrimaryKey.KeyId, packet.KeyFlagSign)
return len(keys) > 0
}
// isGlobal returns true if signing entity e is found in the global keyring, and false otherwise.
func isGlobal(e *openpgp.Entity) bool {
keyring := sypgp.NewHandle(buildcfg.SINGULARITY_CONFDIR, sypgp.GlobalHandleOpt())
kr, err := keyring.LoadPubKeyring()
if err != nil {
return false
}
keys := kr.KeysByIdUsage(e.PrimaryKey.KeyId, packet.KeyFlagSign)
return len(keys) > 0
}
// outputVerify outputs a textual representation of r to stdout.
func outputVerify(_ *sif.FileImage, r integrity.VerifyResult) bool {
e := r.Entity()
// Print signing entity info.
if e != nil {
prefix := color.New(color.FgYellow).Sprint("[REMOTE]")
if isGlobal(e) {
prefix = color.New(color.FgCyan).Sprint("[GLOBAL]")
} else if isLocal(e) {
prefix = color.New(color.FgGreen).Sprint("[LOCAL]")
}
// Print identity, if possible.
if id := primaryIdentity(e); id != nil {
fmt.Printf("%-18v Signing entity: %v\n", prefix, id.Name)
} else {
sylog.Warningf("Primary identity unknown")
}
// Always print fingerprint.
fmt.Printf("%-18v Fingerprint: %X\n", prefix, e.PrimaryKey.Fingerprint)
}
// Print table of signed objects.
if len(r.Verified()) > 0 {
fmt.Printf("Objects verified:\n")
fmt.Printf("%-4s|%-8s|%-8s|%s\n", "ID", "GROUP", "LINK", "TYPE")
fmt.Print("------------------------------------------------\n")
}
for _, od := range r.Verified() {
group := "NONE"
if gid := od.GroupID(); gid != 0 {
group = fmt.Sprintf("%d", gid)
}
link := "NONE"
if l, isGroup := od.LinkedID(); l != 0 {
if isGroup {
link = fmt.Sprintf("%d (G)", l)
} else {
link = fmt.Sprintf("%d", l)
}
}
fmt.Printf("%-4d|%-8s|%-8s|%s\n", od.ID(), group, link, od.DataType())
}
if err := r.Error(); err != nil {
fmt.Printf("\nError encountered during signature verification: %v\n", err)
}
return false
}
type key struct {
Signer keyEntity
}
// keyEntity holds all the key info, used for json output.
type keyEntity struct {
Partition string
Name string
Fingerprint string
KeyLocal bool
KeyCheck bool
DataCheck bool
}
// keyList is a list of one or more keys.
type keyList struct {
Signatures int
SignerKeys []*key
}
// getJSONCallback returns a signature.VerifyCallback that appends to kl.
func getJSONCallback(kl *keyList) sifsignature.VerifyCallback {
return func(f *sif.FileImage, r integrity.VerifyResult) bool {
name, fp := "unknown", ""
var keyLocal, keyCheck bool
// Increment signature count.
kl.Signatures++
// If entity is determined, note a few values.
if e := r.Entity(); e != nil {
if id := primaryIdentity(e); id != nil {
name = id.Name
}
fp = hex.EncodeToString(e.PrimaryKey.Fingerprint[:])
keyLocal = isLocal(e)
keyCheck = true
}
// For each verified object, append an entry to the list.
for _, od := range r.Verified() {
ke := keyEntity{
Partition: od.DataType().String(),
Name: name,
Fingerprint: fp,
KeyLocal: keyLocal,
KeyCheck: keyCheck,
DataCheck: true,
}
kl.SignerKeys = append(kl.SignerKeys, &key{ke})
}
var integrityError *integrity.ObjectIntegrityError
if errors.As(r.Error(), &integrityError) {
od, err := f.GetDescriptor(sif.WithID(integrityError.ID))
if err != nil {
sylog.Errorf("failed to get descriptor: %v", err)
return false
}
ke := keyEntity{
Partition: od.DataType().String(),
Name: name,
Fingerprint: fp,
KeyLocal: keyLocal,
KeyCheck: keyCheck,
DataCheck: false,
}
kl.SignerKeys = append(kl.SignerKeys, &key{ke})
}
return false
}
}
// outputJSON outputs a JSON representation of kl to w.
func outputJSON(w io.Writer, kl keyList) error {
e := json.NewEncoder(w)
e.SetIndent("", " ")
return e.Encode(kl)
}
|