Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
c.out
coverage.html
.idea/
cover.out
14 changes: 8 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ branches:
- master
language: go
go:
- 1.11
- "1.11"
- "1.15"
- "1.16"
- tip
install:
- go get golang.org/x/crypto/openpgp
- go get golang.org/x/crypto/cast5
- go get golang.org/x/crypto/ssh/terminal
- go get golang.org/x/tools/cmd/cover
- go get -u golang.org/x/tools/cmd/cover
- go get -u golang.org/x/lint/golint
- make -v
- make deps
- go build -v ./...
script:
- go test -v -cover
- make check
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
deps:
go mod download
go mod verify
go mod tidy

lint:
# format code
gofmt -w=true -s=true -l=true .
# run basic code quality and sanity check
golint ./...
go vet ./...

check: lint
# ran unit tests with coverage report
go test -v -coverprofile=cover.out ./...

test: check
57 changes: 38 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[![GoDoc](https://godoc.org/github.com/jsipprell/keyctl?status.svg)](https://godoc.org/github.com/jsipprell/keyctl)
[![Build Status](https://travis-ci.org/jsipprell/keyctl.svg?branch=master)](https://travis-ci.org/jsipprell/keyctl)

# keyctl

[![GoDoc](https://pkg.go.dev/github.com/jsipprell/keyctl?status.svg)](https://pkg.go.dev/github.com/jsipprell/keyctl)
[![Build Status](https://travis-ci.org/jsipprell/keyctl.svg?branch=master)](https://travis-ci.org/jsipprell/keyctl)
[![Go Report Card](https://goreportcard.com/badge/github.com/jsipprell/keyctl)](https://goreportcard.com/report/github.com/jsipprell/keyctl)

A native Go API for the security key management system (aka "keyrings") found in Linux 2.6+

The keyctl interface is nominally provided by three or so Linux-specific syscalls, however it is almost always wrapped
Expand Down Expand Up @@ -47,24 +48,42 @@ To search for an existing key by name:
package main

import (
"log"
"github.com/jsipprell/keyctl"
"log"

"github.com/jsipprell/keyctl"
)

func main() {
keyring, err := keyctl.SessionKeyring()
if err != nil {
log.Fatal(err)
}
key, err := keyring.Search("some-data")
if err != nil {
log.Fatal(err)
}
data, err := key.Get()
if err != nil {
log.Fatal(err)
}
log.Printf("secure data: %v\n", data)
keyring, err := keyctl.SessionKeyring()
if err != nil {
log.Fatal(err)
}
key, err := keyring.Search("some-data")
if err != nil {
log.Fatal(err)
}

data, err := key.Get()
if err != nil {
log.Fatal(err)
}
log.Printf("secure data: %v\n", data)
}
```

Running tests
===================

Ensure you have [GNU make](https://www.gnu.org/software/make/) installed.

```shell

$ make check

```


Copyright: 2015 Jesse Sipprell. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.

6 changes: 6 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Package keyctl is a Go interface to linux kernel keyrings (keyctl interface) described here https://man7.org/linux/man-pages/man7/keyrings.7.html
package keyctl

// Copyright 2015 Jesse Sipprell. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
module github.com/jsipprell/keyctl

go 1.15

require (
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c h1:taxlMj0D/1sOAuv/CbSD+MMDof2vbyPTqz5FNYKpXt8=
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
20 changes: 10 additions & 10 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ import (
"time"
)

// Represents a single key linked to one or more kernel keyrings.
// Key represents a single key linked to one or more kernel keyrings.
type Key struct {
Name string

id, ring keyId
id, ring keyID
size int
ttl time.Duration
}

func (k *Key) private() {}

// Returns the 32-bit kernel identifier for a specific key
func (k *Key) Id() int32 {
// ID returns the 32-bit kernel identifier for a specific key
func (k *Key) ID() int32 {
return int32(k.id)
}

// To expire a key automatically after some period of time call this method.
// ExpireAfter makes key expire automatically after some period of time call this method.
func (k *Key) ExpireAfter(nsecs uint) error {
k.ttl = time.Duration(nsecs) * time.Second

return keyctl_SetTimeout(k.id, nsecs)
return keyctlSetTimeoutFunc(k.id, nsecs)
}

// Return information about a key.
// Info return information about a key.
func (k *Key) Info() (Info, error) {
return getInfo(k.id)
}
Expand All @@ -46,10 +46,10 @@ func (k *Key) Get() ([]byte, error) {

size := k.size

b = make([]byte, int(size))
b = make([]byte, size)
sizeRead = size + 1
for sizeRead > size {
r1, err := keyctl_Read(k.id, &b[0], size)
r1, err := keyctlReadFunc(k.id, &b[0], size)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -77,5 +77,5 @@ func (k *Key) Set(b []byte) error {
// Unlink a key from the keyring it was loaded from (or added to). If the key
// is not linked to any other keyrings, it is destroyed.
func (k *Key) Unlink() error {
return keyctl_Unlink(k.id, k.ring)
return keyctlUnlinkFunc(k.id, k.ring)
}
6 changes: 3 additions & 3 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestRandomKey256(t *testing.T) {
t.Fatal(err)
}

t.Logf("added %d byte random value key as: %v (%v)\n", len(r256), id.Id(), r256)
t.Logf("added %d byte random value key as: %v (%v)\n", len(r256), id.ID(), r256)
helperCompareBlock(t, "rand256", r256, nil)
}

Expand All @@ -89,12 +89,12 @@ func TestRandomKey700(t *testing.T) {
t.Fatal(err)
}

t.Logf("added %d byte random value key as: %v (%v)\n", len(r700), id.Id(), r700)
t.Logf("added %d byte random value key as: %v (%v)\n", len(r700), id.ID(), r700)
helperCompareBlock(t, "rand700", r700, nil)
time.Sleep(time.Duration(5)*time.Second + time.Duration(250000))

if _, err = ring.Search("rand700"); err == nil {
t.Fatal("'rand700' key did not expire in five seconds")
}
t.Logf("key %v expired after five seconds", id.Id())
t.Logf("key %v expired after five seconds", id.ID())
}
Loading