File: object.go

package info (click to toggle)
golang-github-smallstep-crypto 0.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,800 kB
  • sloc: sh: 66; makefile: 50
file content (60 lines) | stat: -rw-r--r-- 1,457 bytes parent folder | download
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
package nssdb

import (
	"bytes"
	"encoding/hex"
	"fmt"
)

// Object is an entry in nssPublic or nssPrivate plus any related entries in
// the metaData table. The encoding for and meaning of most attributes can be
// found in the PKCS #11 spec.
type Object struct {
	ID                  uint32
	Attributes          map[string][]byte
	ULongAttributes     map[string]uint32
	EncryptedAttributes map[string][]byte
	Metadata            []*Metadata
}

func (obj *Object) Print() {
	fmt.Printf("ID: %d\n", obj.ID)

	for k, data := range obj.EncryptedAttributes {
		fmt.Printf("%s (encrypted): %x\n", k, data)
	}

	for _, meta := range obj.Metadata {
		fmt.Printf("%s: %x\n", meta.ID, meta.Item1)
	}

	for k, u := range obj.ULongAttributes {
		fmt.Printf("%s: %d\n", k, u)
	}

	for k, data := range obj.Attributes {
		fmt.Printf("%s: %x\n", k, data)
	}
}

func (obj Object) ValidateULong(name string, want uint32) error {
	attr, ok := obj.ULongAttributes[name]
	if !ok {
		return fmt.Errorf("object is missing attribute %s", name)
	}
	if attr != want {
		return fmt.Errorf("%s expected %d but got %d", name, attr, want)
	}
	return nil
}

func (obj Object) Validate(name string, want []byte) error {
	attr, ok := obj.Attributes[name]
	if !ok {
		return fmt.Errorf("object is missing attribute %s", name)
	}
	if !bytes.Equal(attr, want) {
		return fmt.Errorf("%s has value %q, expected %q", name, hex.EncodeToString(attr), hex.EncodeToString(want))
	}
	return nil
}