| 1 |
//go:build containers_image_sequoia |
| 2 |
|
| 3 |
package sequoia |
| 4 |
|
| 5 |
// #cgo CFLAGS: -I. -DGO_SEQUOIA_ENABLE_DLOPEN=1 |
| 6 |
// #include "gosequoia.h" |
| 7 |
// #include <dlfcn.h> |
| 8 |
// #include <limits.h> |
| 9 |
// typedef void (*sequoia_logger_consumer_t) (enum SequoiaLogLevel level, char *message); |
| 10 |
// extern void sequoia_logrus_logger (enum SequoiaLogLevel level, char *message); |
| 11 |
import "C" |
| 12 |
|
| 13 |
import ( |
| 14 |
"errors" |
| 15 |
"fmt" |
| 16 |
"path/filepath" |
| 17 |
"runtime" |
| 18 |
"sync" |
| 19 |
"unsafe" |
| 20 |
|
| 21 |
"github.com/sirupsen/logrus" |
| 22 |
) |
| 23 |
|
| 24 |
// sequoiaLibraryDir is the path to the directory where libpodman_sequoia is installed, |
| 25 |
// if it is not in the platform’s default library path. |
| 26 |
// You can override this at build time with |
| 27 |
// -ldflags '-X go.podman.io/image/v5/signature/sequoia.sequoiaLibraryDir=$your_path' |
| 28 |
var sequoiaLibraryDir = "" |
| 29 |
|
| 30 |
type SigningMechanism struct { |
| 31 |
mechanism *C.SequoiaMechanism |
| 32 |
} |
| 33 |
|
| 34 |
// NewMechanismFromDirectory initializes a mechanism using (user-managed) Sequoia state |
| 35 |
// in dir, which can be "" to indicate the default (using $SEQUOIA_HOME or the default home directory location). |
| 36 |
func NewMechanismFromDirectory( |
| 37 |
dir string, |
| 38 |
) (*SigningMechanism, error) { |
| 39 |
var cerr *C.SequoiaError |
| 40 |
var cDir *C.char |
| 41 |
if dir != "" { |
| 42 |
cDir = C.CString(dir) |
| 43 |
defer C.free(unsafe.Pointer(cDir)) |
| 44 |
} |
| 45 |
cMechanism := C.go_sequoia_mechanism_new_from_directory(cDir, &cerr) |
| 46 |
if cMechanism == nil { |
| 47 |
defer C.go_sequoia_error_free(cerr) |
| 48 |
return nil, errors.New(C.GoString(cerr.message)) |
| 49 |
} |
| 50 |
return &SigningMechanism{ |
| 51 |
mechanism: cMechanism, |
| 52 |
}, nil |
| 53 |
} |
| 54 |
|
| 55 |
func NewEphemeralMechanism() (*SigningMechanism, error) { |
| 56 |
var cerr *C.SequoiaError |
| 57 |
cMechanism := C.go_sequoia_mechanism_new_ephemeral(&cerr) |
| 58 |
if cMechanism == nil { |
| 59 |
defer C.go_sequoia_error_free(cerr) |
| 60 |
return nil, errors.New(C.GoString(cerr.message)) |
| 61 |
} |
| 62 |
return &SigningMechanism{ |
| 63 |
mechanism: cMechanism, |
| 64 |
}, nil |
| 65 |
} |
| 66 |
|
| 67 |
func (m *SigningMechanism) SignWithPassphrase( |
| 68 |
input []byte, |
| 69 |
keyIdentity string, |
| 70 |
passphrase string, |
| 71 |
) ([]byte, error) { |
| 72 |
var cerr *C.SequoiaError |
| 73 |
var cPassphrase *C.char |
| 74 |
if passphrase == "" { |
| 75 |
cPassphrase = nil |
| 76 |
} else { |
| 77 |
cPassphrase = C.CString(passphrase) |
| 78 |
defer C.free(unsafe.Pointer(cPassphrase)) |
| 79 |
} |
| 80 |
cKeyIdentity := C.CString(keyIdentity) |
| 81 |
defer C.free(unsafe.Pointer(cKeyIdentity)) |
| 82 |
sig := C.go_sequoia_sign( |
| 83 |
m.mechanism, |
| 84 |
cKeyIdentity, |
| 85 |
cPassphrase, |
| 86 |
(*C.uchar)(unsafe.Pointer(unsafe.SliceData(input))), |
| 87 |
C.size_t(len(input)), |
| 88 |
&cerr, |
| 89 |
) |
| 90 |
if sig == nil { |
| 91 |
defer C.go_sequoia_error_free(cerr) |
| 92 |
return nil, errors.New(C.GoString(cerr.message)) |
| 93 |
} |
| 94 |
defer C.go_sequoia_signature_free(sig) |
| 95 |
var size C.size_t |
| 96 |
cData := C.go_sequoia_signature_get_data(sig, &size) |
| 97 |
if size > C.size_t(C.INT_MAX) { |
| 98 |
return nil, errors.New("overflow") // Coverage: This should not reasonably happen, and we don’t want to generate gigabytes of input to test this. |
| 99 |
} |
| 100 |
return C.GoBytes(unsafe.Pointer(cData), C.int(size)), nil |
| 101 |
} |
| 102 |
|
| 103 |
func (m *SigningMechanism) Sign( |
| 104 |
input []byte, |
| 105 |
keyIdentity string, |
| 106 |
) ([]byte, error) { |
| 107 |
return m.SignWithPassphrase(input, keyIdentity, "") |
| 108 |
} |
| 109 |
|
| 110 |
func (m *SigningMechanism) Verify( |
| 111 |
unverifiedSignature []byte, |
| 112 |
) (contents []byte, keyIdentity string, err error) { |
| 113 |
var cerr *C.SequoiaError |
| 114 |
result := C.go_sequoia_verify( |
| 115 |
m.mechanism, |
| 116 |
(*C.uchar)(unsafe.Pointer(unsafe.SliceData(unverifiedSignature))), |
| 117 |
C.size_t(len(unverifiedSignature)), |
| 118 |
&cerr, |
| 119 |
) |
| 120 |
if result == nil { |
| 121 |
defer C.go_sequoia_error_free(cerr) |
| 122 |
return nil, "", errors.New(C.GoString(cerr.message)) |
| 123 |
} |
| 124 |
defer C.go_sequoia_verification_result_free(result) |
| 125 |
var size C.size_t |
| 126 |
cContent := C.go_sequoia_verification_result_get_content(result, &size) |
| 127 |
if size > C.size_t(C.INT_MAX) { |
| 128 |
return nil, "", errors.New("overflow") // Coverage: This should not reasonably happen, and we don’t want to generate gigabytes of input to test this. |
| 129 |
} |
| 130 |
contents = C.GoBytes(unsafe.Pointer(cContent), C.int(size)) |
| 131 |
cSigner := C.go_sequoia_verification_result_get_signer(result) |
| 132 |
keyIdentity = C.GoString(cSigner) |
| 133 |
return contents, keyIdentity, nil |
| 134 |
} |
| 135 |
|
| 136 |
func (m *SigningMechanism) ImportKeys(blob []byte) ([]string, error) { |
| 137 |
var cerr *C.SequoiaError |
| 138 |
result := C.go_sequoia_import_keys( |
| 139 |
m.mechanism, |
| 140 |
(*C.uchar)(unsafe.Pointer(unsafe.SliceData(blob))), |
| 141 |
C.size_t(len(blob)), |
| 142 |
&cerr, |
| 143 |
) |
| 144 |
if result == nil { |
| 145 |
defer C.go_sequoia_error_free(cerr) |
| 146 |
return nil, errors.New(C.GoString(cerr.message)) |
| 147 |
} |
| 148 |
defer C.go_sequoia_import_result_free(result) |
| 149 |
|
| 150 |
keyIdentities := []string{} |
| 151 |
count := C.go_sequoia_import_result_get_count(result) |
| 152 |
for i := C.size_t(0); i < count; i++ { |
| 153 |
var cerr *C.SequoiaError |
| 154 |
cKeyIdentity := C.go_sequoia_import_result_get_content(result, i, &cerr) |
| 155 |
if cerr != nil { |
| 156 |
defer C.go_sequoia_error_free(cerr) // Coverage: this can fail only if i is out of range. |
| 157 |
return nil, errors.New(C.GoString(cerr.message)) |
| 158 |
} |
| 159 |
keyIdentities = append(keyIdentities, C.GoString(cKeyIdentity)) |
| 160 |
} |
| 161 |
|
| 162 |
return keyIdentities, nil |
| 163 |
} |
| 164 |
|
| 165 |
func (m *SigningMechanism) Close() error { |
| 166 |
C.go_sequoia_mechanism_free(m.mechanism) |
| 167 |
return nil |
| 168 |
} |
| 169 |
|
| 170 |
//export sequoia_logrus_logger |
| 171 |
func sequoia_logrus_logger(level C.enum_SequoiaLogLevel, message *C.char) { |
| 172 |
var logrusLevel logrus.Level |
| 173 |
switch level { // Coverage: We are not in control of whether / how the Rust code chooses to log things. |
| 174 |
case C.SEQUOIA_LOG_LEVEL_ERROR: |
| 175 |
logrusLevel = logrus.ErrorLevel |
| 176 |
case C.SEQUOIA_LOG_LEVEL_WARN: |
| 177 |
logrusLevel = logrus.WarnLevel |
| 178 |
case C.SEQUOIA_LOG_LEVEL_INFO: |
| 179 |
logrusLevel = logrus.InfoLevel |
| 180 |
case C.SEQUOIA_LOG_LEVEL_DEBUG: |
| 181 |
logrusLevel = logrus.DebugLevel |
| 182 |
case C.SEQUOIA_LOG_LEVEL_TRACE: |
| 183 |
logrusLevel = logrus.TraceLevel |
| 184 |
case C.SEQUOIA_LOG_LEVEL_UNKNOWN: |
| 185 |
fallthrough |
| 186 |
default: |
| 187 |
logrusLevel = logrus.ErrorLevel // Should never happen |
| 188 |
} |
| 189 |
logrus.StandardLogger().Log(logrusLevel, C.GoString(message)) |
| 190 |
} |
| 191 |
|
| 192 |
// initOnce should only be called by Init. |
| 193 |
func initOnce() error { |
| 194 |
var soName string |
| 195 |
switch runtime.GOOS { |
| 196 |
case "linux": |
| 197 |
soName = "libpodman_sequoia.so.0" |
| 198 |
case "darwin": |
| 199 |
soName = "libpodman_sequoia.dylib" |
| 200 |
default: |
| 201 |
return fmt.Errorf("Unhandled OS %q in sequoia initialization", runtime.GOOS) // Coverage: This is ~by definition not reached in tests. |
| 202 |
} |
| 203 |
if sequoiaLibraryDir != "" { |
| 204 |
soName = filepath.Join(sequoiaLibraryDir, soName) |
| 205 |
} |
| 206 |
cSOName := C.CString(soName) |
| 207 |
defer C.free(unsafe.Pointer(cSOName)) |
| 208 |
if C.go_sequoia_ensure_library(cSOName, |
| 209 |
C.RTLD_NOW|C.RTLD_GLOBAL) < 0 { |
| 210 |
return fmt.Errorf("unable to load %q", soName) // Coverage: This is impractical to test in-process, with the static go_sequoia_dlhandle. |
| 211 |
} |
| 212 |
|
| 213 |
var cerr *C.SequoiaError |
| 214 |
if C.go_sequoia_set_logger_consumer(C.sequoia_logger_consumer_t(C.sequoia_logrus_logger), &cerr) != 0 { |
| 215 |
defer C.go_sequoia_error_free(cerr) // Coverage: This is impractical to test in-process, with the static go_sequoia_dlhandle. |
| 216 |
return fmt.Errorf("initializing logging: %s", C.GoString(cerr.message)) |
| 217 |
} |
| 218 |
return nil |
| 219 |
} |
| 220 |
|
| 221 |
// Init ensures the libpodman_sequoia library is available. |
| 222 |
// It is safe to call from arbitrary goroutines. |
| 223 |
var Init = sync.OnceValue(initOnce) |