Exemplo n.º 1
0
//
// Unlink wraps keyctl_unlink(3).
//
func Unlink(key KeySerial, keyRing KeySerial) error {

	_, err := C.keyctl_unlink(C.key_serial_t(key), C.key_serial_t(keyRing))

	if err != nil {
		return err.(syscall.Errno)
	}
	return nil
}
Exemplo n.º 2
0
//
// SetPerm() will call keyctl_setperm(3) to set permissions on a key.
// mask is a bitwise `or` value of KeyPerm values, e.g.
// KEY_USR_VIEW | KEY_USR_READ
//
// See: http://man7.org/linux/man-pages/man3/keyctl_setperm.3.html
//
func SetPerm(key KeySerial, mask KeyPerm) error {
	_, err := C.keyctl_setperm(C.key_serial_t(key), C.key_perm_t(mask))

	if err != nil {
		return err.(syscall.Errno)
	}
	return nil
}
Exemplo n.º 3
0
//
// Revoke() will call keyctl_revoke(3) to revoke a key.
//
// See: http://man7.org/linux/man-pages/man3/keyctl_revoke.3.html
//
func Revoke(key KeySerial) error {
	_, err := C.keyctl_revoke(C.key_serial_t(key))

	if err != nil {
		return err.(syscall.Errno)
	}
	return nil
}
Exemplo n.º 4
0
//
// SetTimeout() will call keyctl_set_timeout(3) to set a `seconds`
// timeout on a key.
//
// See: http://man7.org/linux/man-pages/man3/keyctl_set_timeout.3.html
//
func SetTimeout(key KeySerial, seconds uint) error {
	_, err := C.keyctl_set_timeout(C.key_serial_t(key), C.uint(seconds))

	if err != nil {
		return err.(syscall.Errno)
	}
	return nil
}
Exemplo n.º 5
0
//
// Clear() will call keyctl_clear(3) to clear a keyring.
//
func Clear(keyring KeySerial) error {
	_, err := C.keyctl_clear(C.key_serial_t(keyring))

	if err != nil {
		return err.(syscall.Errno)
	}
	return nil
}
Exemplo n.º 6
0
//
// Chown wraps keyctl_chown(3) to change ownership of the key.
//
// See: http://man7.org/linux/man-pages/man3/keyctl_chown.3.html
//
func Chown(key KeySerial, uid uint, gid uint) error {

	_, err := C.keyctl_chown(C.key_serial_t(key), C.uid_t(uid), C.gid_t(gid))

	if err != nil {
		return err.(syscall.Errno)
	}
	return nil
}
Exemplo n.º 7
0
//
// DescribeKey() wraps keyctl_describe_alloc() to describe a key
//
func DescribeKey(key KeySerial) (*KeyDesc, error) {
	var ptr *C.char = nil
	bytes, err := C.keyctl_describe_alloc(C.key_serial_t(int(key)), (&ptr))

	if err == nil && bytes > 0 && ptr != nil {
		descString := C.GoString(ptr)
		C.free(unsafe.Pointer(ptr))
		return parseKeyDesc(key, descString)
	}

	return nil, err.(syscall.Errno)
}
Exemplo n.º 8
0
//
// RequestKey() wraps request_key(2).
//
// It returns the serial number of the key found with type = `keyType`
// and description = `desc` in the keyring `keyring`.
//
func RequestKey(keyType KeyType, desc string, keyring KeySerial) (KeySerial, error) {
	cKeyType := C.CString(string(keyType))
	cDesc := C.CString(desc)
	result, err := C.request_key(
		cKeyType,
		cDesc,
		nil,
		C.key_serial_t(int(keyring)))
	C.free(unsafe.Pointer(cKeyType))
	C.free(unsafe.Pointer(cDesc))
	if err != nil {
		return 0, err.(syscall.Errno)
	} else {
		return KeySerial(int(result)), nil
	}
}
Exemplo n.º 9
0
//
// ReadKeyBytes() reads a key with the given serial # using keyctl_read_alloc(3), and returns the bytes read.
//
func ReadKeyBytes(key KeySerial) ([]byte, error) {

	var ptr unsafe.Pointer = nil
	bytes, err := C.keyctl_read_alloc(C.key_serial_t(int(key)), (*unsafe.Pointer)(&ptr))

	if err != nil {
		return nil, err.(syscall.Errno)
	}
	if bytes > 0 && ptr != nil {
		result := C.GoBytes(ptr, bytes)
		C.free(ptr)
		return result, nil
	}
	return nil, nil

}
Exemplo n.º 10
0
//
// AddKeyBytes wraps add_key(2).
//
// It returns the serial number of the added key.
//
func AddKeyBytes(keyType KeyType, desc string, data []byte, keyring KeySerial) (KeySerial, error) {
	cKeyType := C.CString(string(keyType))
	cDesc := C.CString(desc)
	payloadLen := C.size_t(len(data))

	var secret unsafe.Pointer

	if data != nil {
		secret = unsafe.Pointer(&data[0])
	}

	result, err := C.add_key(cKeyType, cDesc, secret, payloadLen, C.key_serial_t(int(keyring)))

	C.free(unsafe.Pointer(cKeyType))
	C.free(unsafe.Pointer(cDesc))

	if err != nil {
		return 0, err.(syscall.Errno)
	} else {
		return KeySerial(int(result)), nil
	}
}