| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package p11token |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "crypto" |
| 22 | "crypto/x509" |
| 23 | "encoding/base64" |
| 24 | "errors" |
| 25 | "fmt" |
| 26 | "io" |
| 27 | "strings" |
| 28 | |
| 29 | "github.com/miekg/pkcs11" |
| 30 | |
| 31 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 32 | "github.com/sassoftware/relic/v7/token" |
| 33 | ) |
| 34 | |
| 35 | var classNames = map[uint]string{ |
| 36 | pkcs11.CKO_DATA: "data", |
| 37 | pkcs11.CKO_CERTIFICATE: "certificate", |
| 38 | pkcs11.CKO_PUBLIC_KEY: "public_key", |
| 39 | pkcs11.CKO_PRIVATE_KEY: "private_key", |
| 40 | pkcs11.CKO_SECRET_KEY: "secret_key", |
| 41 | pkcs11.CKO_HW_FEATURE: "hw_feature", |
| 42 | pkcs11.CKO_DOMAIN_PARAMETERS: "domain_parameters", |
| 43 | pkcs11.CKO_MECHANISM: "mechanism", |
| 44 | pkcs11.CKO_OTP_KEY: "otp_key", |
| 45 | } |
| 46 | |
| 47 | var keyTypes = map[uint]string{ |
| 48 | pkcs11.CKK_RSA: "rsa", |
| 49 | pkcs11.CKK_DSA: "dsa", |
| 50 | pkcs11.CKK_EC: "ec", |
| 51 | } |
| 52 | |
| 53 | func (tok *Token) ListKeys(opts token.ListOptions) (err error) { |
| 54 | filterKeyId, err := parseKeyID(opts.ID) |
| 55 | if err != nil { |
| 56 | return errors.New("invalid filter id") |
| 57 | } |
| 58 | tok.mutex.Lock() |
| 59 | defer tok.mutex.Unlock() |
| 60 | if err := tok.ctx.FindObjectsInit(tok.sh, nil); err != nil { |
| 61 | return err |
| 62 | } |
| 63 | defer func() { |
| 64 | err2 := tok.ctx.FindObjectsFinal(tok.sh) |
| 65 | if err2 != nil && err == nil { |
| 66 | err = err2 |
| 67 | } |
| 68 | }() |
| 69 | for { |
| 70 | objects, _, err := tok.ctx.FindObjects(tok.sh, 1) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } else if len(objects) == 0 { |
| 74 | break |
| 75 | } |
| 76 | for _, handle := range objects { |
| 77 | objId := tok.getAttribute(handle, pkcs11.CKA_ID) |
| 78 | label := tok.getAttribute(handle, pkcs11.CKA_LABEL) |
| 79 | if opts.Label != "" && string(label) != opts.Label { |
| 80 | continue |
| 81 | } |
| 82 | if len(filterKeyId) != 0 && !bytes.Equal(filterKeyId, objId) { |
| 83 | continue |
| 84 | } |
| 85 | fmt.Fprintf(opts.Output, "handle 0x%08x:\n", handle) |
| 86 | rawClass := tok.getAttribute(handle, pkcs11.CKA_CLASS) |
| 87 | class, err := getUlong(rawClass) |
| 88 | if name := classNames[class]; name != "" && err == nil { |
| 89 | fmt.Fprintf(opts.Output, " class: %s\n", name) |
| 90 | } else { |
| 91 | fmt.Fprintf(opts.Output, " class: 0x%x\n", rawClass) |
| 92 | } |
| 93 | if len(objId) > 0 { |
| 94 | fmt.Fprintf(opts.Output, " id: %s\n", formatKeyID(objId)) |
| 95 | } |
| 96 | if len(label) > 0 { |
| 97 | fmt.Fprintf(opts.Output, " label: %s\n", label) |
| 98 | } |
| 99 | switch class { |
| 100 | case pkcs11.CKO_PUBLIC_KEY: |
| 101 | tok.printKey(opts, handle) |
| 102 | case pkcs11.CKO_PRIVATE_KEY: |
| 103 | tok.printKey(opts, handle) |
| 104 | case pkcs11.CKO_CERTIFICATE: |
| 105 | tok.printCertificate(opts, handle) |
| 106 | case pkcs11.CKO_DATA: |
| 107 | value := tok.getAttribute(handle, pkcs11.CKA_VALUE) |
| 108 | fmt.Fprintf(opts.Output, " size: %d\n", len(value)) |
| 109 | if opts.Values { |
| 110 | fmt.Fprintln(opts.Output, " value: !!binary |") |
| 111 | dumpData(opts.Output, value) |
| 112 | } |
| 113 | } |
| 114 | fmt.Fprintln(opts.Output) |
| 115 | } |
| 116 | } |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | func (tok *Token) printKey(opts token.ListOptions, handle pkcs11.ObjectHandle) { |
| 121 | rawKeyType := tok.getAttribute(handle, pkcs11.CKA_KEY_TYPE) |
| 122 | keyType, err := getUlong(rawKeyType) |
| 123 | if name := keyTypes[keyType]; name != "" && err == nil { |
| 124 | fmt.Fprintf(opts.Output, " type: %s\n", name) |
| 125 | } else { |
| 126 | fmt.Fprintf(opts.Output, " type: 0x%x\n", keyType) |
| 127 | } |
| 128 | switch keyType { |
| 129 | case pkcs11.CKK_RSA: |
| 130 | if n := tok.getAttribute(handle, pkcs11.CKA_MODULUS); len(n) != 0 { |
| 131 | fmt.Fprintf(opts.Output, " bits: %d\n", len(n)*8) |
| 132 | if opts.Values { |
| 133 | fmt.Fprintf(opts.Output, " n: 0x%x\n", bytesToBig(n)) |
| 134 | } |
| 135 | } |
| 136 | if e := tok.getAttribute(handle, pkcs11.CKA_PUBLIC_EXPONENT); len(e) != 0 && opts.Values { |
| 137 | fmt.Fprintf(opts.Output, " e: %s\n", bytesToBig(e)) |
| 138 | } |
| 139 | case pkcs11.CKK_EC: |
| 140 | ecparams := tok.getAttribute(handle, pkcs11.CKA_EC_PARAMS) |
| 141 | if len(ecparams) == 0 { |
| 142 | return |
| 143 | } |
| 144 | curve, err := x509tools.CurveByDer(ecparams) |
| 145 | if err != nil { |
| 146 | fmt.Fprintf(opts.Output, " curve: %x\n", ecparams) |
| 147 | return |
| 148 | } |
| 149 | fmt.Fprintf(opts.Output, " bits: %d\n", curve.Bits) |
| 150 | ecpoint := tok.getAttribute(handle, pkcs11.CKA_EC_POINT) |
| 151 | if len(ecpoint) > 0 && opts.Values { |
| 152 | x, y := x509tools.DerToPoint(curve.Curve, ecpoint) |
| 153 | if x != nil { |
| 154 | fmt.Fprintf(opts.Output, " x: 0x%x\n", x) |
| 155 | fmt.Fprintf(opts.Output, " y: 0x%x\n", y) |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func (tok *Token) printCertificate(opts token.ListOptions, handle pkcs11.ObjectHandle) { |
| 162 | blob := tok.getAttribute(handle, pkcs11.CKA_VALUE) |
| 163 | if len(blob) == 0 { |
| 164 | fmt.Fprintln(opts.Output, "certificate is missing") |
| 165 | return |
| 166 | } |
| 167 | cert, err := x509.ParseCertificate(blob) |
| 168 | if err != nil { |
| 169 | fmt.Fprintln(opts.Output, "certificate is invalid:", err) |
| 170 | } |
| 171 | d := crypto.SHA1.New() |
| 172 | d.Write(blob) |
| 173 | fmt.Fprintf(opts.Output, " subject: %s\n issuer: %s\n sha1: %x\n", x509tools.FormatSubject(cert), x509tools.FormatIssuer(cert), d.Sum(nil)) |
| 174 | if opts.Values { |
| 175 | fmt.Fprintln(opts.Output, " value: |\n -----BEGIN CERTIFICATE-----") |
| 176 | dumpData(opts.Output, blob) |
| 177 | fmt.Fprintln(opts.Output, " -----END CERTIFICATE-----") |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func formatKeyID(keyID []byte) string { |
| 182 | chunks := make([]string, len(keyID)) |
| 183 | for i, j := range keyID { |
| 184 | chunks[i] = fmt.Sprintf("%02x", j) |
| 185 | } |
| 186 | return strings.Join(chunks, ":") |
| 187 | } |
| 188 | |
| 189 | func dumpData(w io.Writer, d []byte) { |
| 190 | encoded := base64.StdEncoding.EncodeToString(d) |
| 191 | for len(encoded) > 0 { |
| 192 | n := 64 |
| 193 | if n > len(encoded) { |
| 194 | n = len(encoded) |
| 195 | } |
| 196 | fmt.Fprintln(w, " ", encoded[:n]) |
| 197 | encoded = encoded[n:] |
| 198 | } |
| 199 | } |