| 1 |
/* |
| 2 |
(c) Copyright IBM Corporation, 2020 |
| 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 pkcs11uri |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"io/ioutil" |
| 22 |
"os" |
| 23 |
"testing" |
| 24 |
) |
| 25 |
|
| 26 |
var modulePaths = []string{ |
| 27 |
"/usr/lib64/pkcs11/", // Fedora, RHEL, openSUSE |
| 28 |
"/usr/lib/pkcs11/", // Fedora 32 bit, ArchLinux |
| 29 |
"/usr/lib/softhsm/", // Ubuntu, Debian, Alpine |
| 30 |
} |
| 31 |
|
| 32 |
func TestParse1(t *testing.T) { |
| 33 |
uri := New() |
| 34 |
|
| 35 |
original := "pkcs11:id=%02;object=SIGN%20pubkey;token=SSH%20key;manufacturer=piv_II?module-path=/usr/lib64/pkcs11/opensc-pkcs11.so" |
| 36 |
err := uri.Parse(original) |
| 37 |
|
| 38 |
if err != nil { |
| 39 |
t.Fatalf("Could not parse URI: %s", err) |
| 40 |
} |
| 41 |
|
| 42 |
for _, attr := range []string{"id", "object", "token", "manufacturer"} { |
| 43 |
if _, ok := uri.GetPathAttribute(attr, false); !ok { |
| 44 |
t.Fatalf("Path attribute %s is not available", attr) |
| 45 |
} |
| 46 |
} |
| 47 |
for _, attr := range []string{"module-path"} { |
| 48 |
if _, ok := uri.GetQueryAttribute(attr, false); !ok { |
| 49 |
t.Fatalf("Query attribute %s is not available", attr) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
_, err = uri.Format() |
| 54 |
if err != nil { |
| 55 |
t.Fatalf("Could not format the uri: %s", err) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func verifyURI(t *testing.T, uri *Pkcs11URI, expecteduri string) { |
| 60 |
encoded, err := uri.Format() |
| 61 |
if err != nil { |
| 62 |
t.Fatalf("Could not format the uri: %s", err) |
| 63 |
} |
| 64 |
if encoded != expecteduri { |
| 65 |
t.Fatalf("Did not get expected URI '%s' but '%s'", expecteduri, encoded) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func verifyPIN(t *testing.T, uri *Pkcs11URI, expectedpin string) { |
| 70 |
if !uri.HasPIN() { |
| 71 |
t.Fatalf("HasPIN indicated that the URI does not have a PIN") |
| 72 |
} |
| 73 |
pin, err := uri.GetPIN() |
| 74 |
if err != nil { |
| 75 |
t.Fatalf("Could not get PIN: %s", err) |
| 76 |
} |
| 77 |
if pin != expectedpin { |
| 78 |
t.Fatalf("Did not get expected PIN value of '1234' but '%s'", pin) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func TestConstruct1(t *testing.T) { |
| 83 |
uri := New() |
| 84 |
expecteduri := "pkcs11:id=%66%6F%6F" |
| 85 |
|
| 86 |
err := uri.AddPathAttribute("id", "%66oo") |
| 87 |
if err != nil { |
| 88 |
t.Fatalf("Could not add path attribute: %s", err) |
| 89 |
} |
| 90 |
|
| 91 |
verifyURI(t, uri, expecteduri) |
| 92 |
|
| 93 |
expectedpin := "1234" |
| 94 |
expecteduri += fmt.Sprintf("?pin-value=%s", expectedpin) |
| 95 |
|
| 96 |
err = uri.AddQueryAttribute("pin-value", expectedpin) |
| 97 |
if err != nil { |
| 98 |
t.Fatalf("Could not add query attribute: %s", err) |
| 99 |
} |
| 100 |
|
| 101 |
verifyURI(t, uri, expecteduri) |
| 102 |
verifyPIN(t, uri, expectedpin) |
| 103 |
} |
| 104 |
|
| 105 |
func writeTempfile(t *testing.T, value string) *os.File { |
| 106 |
tmpfile, err := ioutil.TempFile("", "mypin") |
| 107 |
if err != nil { |
| 108 |
t.Fatalf("Coult not create temporary file: %s", err) |
| 109 |
} |
| 110 |
if _, err := tmpfile.Write([]byte(value)); err != nil { |
| 111 |
t.Fatalf("Could not write to tempfile: %s", err) |
| 112 |
} |
| 113 |
if err := tmpfile.Close(); err != nil { |
| 114 |
t.Fatalf("Could not close tempfile: %s", err) |
| 115 |
} |
| 116 |
return tmpfile |
| 117 |
} |
| 118 |
|
| 119 |
func TestPinSource(t *testing.T) { |
| 120 |
uri := New() |
| 121 |
expectedpin := "4321" |
| 122 |
|
| 123 |
tmpfile := writeTempfile(t, expectedpin) |
| 124 |
defer os.Remove(tmpfile.Name()) |
| 125 |
|
| 126 |
expecteduri := "pkcs11:id=%66%6F%6F?pin-source=file:" + tmpfile.Name() |
| 127 |
err := uri.AddPathAttribute("id", "foo") |
| 128 |
if err != nil { |
| 129 |
t.Fatalf("Could not add path attribute: %s", err) |
| 130 |
} |
| 131 |
err = uri.AddQueryAttribute("pin-source", "file:"+tmpfile.Name()) |
| 132 |
if err != nil { |
| 133 |
t.Fatalf("Could not add query attribute: %s", err) |
| 134 |
} |
| 135 |
|
| 136 |
verifyURI(t, uri, expecteduri) |
| 137 |
verifyPIN(t, uri, expectedpin) |
| 138 |
|
| 139 |
expecteduri = "pkcs11:id=%66%6F%6F?pin-source=" + tmpfile.Name() |
| 140 |
|
| 141 |
uri.RemoveQueryAttribute("pin-source") |
| 142 |
err = uri.AddQueryAttribute("pin-source", tmpfile.Name()) |
| 143 |
if err != nil { |
| 144 |
t.Fatalf("Could not add query attribute: %s", err) |
| 145 |
} |
| 146 |
|
| 147 |
verifyURI(t, uri, expecteduri) |
| 148 |
verifyPIN(t, uri, expectedpin) |
| 149 |
} |
| 150 |
|
| 151 |
func TestBadInput(t *testing.T) { |
| 152 |
uri := New() |
| 153 |
|
| 154 |
for _, entry := range [][]string{{"slot-id", "foo"}, {"library-version", "foo"}, {"library-version", "1.bar"}, {"type", "fobbar"}} { |
| 155 |
err := uri.AddPathAttribute(entry[0], entry[1]) |
| 156 |
if err != nil { |
| 157 |
t.Fatalf("Could not add path attribute: %s", err) |
| 158 |
} |
| 159 |
|
| 160 |
if err := uri.Validate(); err == nil { |
| 161 |
t.Fatalf("uri validation should have failed due to malformed %s value '%s'", entry[0], entry[1]) |
| 162 |
} |
| 163 |
uri.RemovePathAttribute(entry[0]) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func TestGoodInput(t *testing.T) { |
| 168 |
uri := New() |
| 169 |
|
| 170 |
for _, entry := range [][]string{{"slot-id", "1"}, {"library-version", "7"}, {"library-version", "1.8"}, {"type", "public"}} { |
| 171 |
err := uri.AddPathAttribute(entry[0], entry[1]) |
| 172 |
if err != nil { |
| 173 |
t.Fatalf("Could not add path attribute: %s", err) |
| 174 |
} |
| 175 |
|
| 176 |
if err := uri.Validate(); err != nil { |
| 177 |
t.Fatalf("uri validation should have succeeded for %s value '%s': %s", entry[0], entry[1], err) |
| 178 |
} |
| 179 |
uri.RemovePathAttribute(entry[0]) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func TestURIs(t *testing.T) { |
| 184 |
uri := New() |
| 185 |
uris := []string{ |
| 186 |
"pkcs11:", |
| 187 |
"pkcs11:object=my-pubkey;type=public", |
| 188 |
"pkcs11:object=my-key;type=private?pin-source=file:/etc/token", |
| 189 |
"pkcs11:token=The%20Software%20PKCS%2311%20Softtoken;manufacturer=Snake%20Oil,%20Inc.;model=1.0;object=my-certificate;type=cert;id=%69%95%3E%5C%F4%BD%EC%91;serial=?pin-source=file:/etc/token_pin", |
| 190 |
"pkcs11:object=my-sign-key;type=private?module-name=mypkcs11", |
| 191 |
"pkcs11:object=my-sign-key;type=private?module-path=/mnt/libmypkcs11.so.1", |
| 192 |
"pkcs11:token=Software%20PKCS%2311%20softtoken;manufacturer=Snake%20Oil,%20Inc.?pin-value=the-pin", |
| 193 |
"pkcs11:slot-description=Sun%20Metaslot", |
| 194 |
"pkcs11:library-manufacturer=Snake%20Oil,%20Inc.;library-description=Soft%20Token%20Library;library-version=1.23", |
| 195 |
"pkcs11:token=My%20token%25%20created%20by%20Joe;library-version=3;id=%01%02%03%Ba%dd%Ca%fe%04%05%06", |
| 196 |
"pkcs11:token=A%20name%20with%20a%20substring%20%25%3B;object=my-certificate;type=cert", |
| 197 |
"pkcs11:token=Name%20with%20a%20small%20A%20with%20acute:%20%C3%A1;object=my-certificate;type=cert", |
| 198 |
"pkcs11:token=my-token;object=my-certificate;type=cert;vendor-aaa=value-a?pin-source=file:/etc/token_pin&vendor-bbb=value-b", |
| 199 |
} |
| 200 |
for _, uristring := range uris { |
| 201 |
err := uri.Parse(uristring) |
| 202 |
if err != nil { |
| 203 |
t.Fatalf("Could not parse URI '%s': %s", uristring, err) |
| 204 |
} |
| 205 |
|
| 206 |
encoded, err := uri.Format() |
| 207 |
if err != nil { |
| 208 |
t.Fatalf("Could not format URI '%s': %s", uristring, err) |
| 209 |
} |
| 210 |
// the order of attributes may be different but the string lengths are the same |
| 211 |
if len(encoded) != len(uristring) { |
| 212 |
t.Fatalf("String lengths are different: '%s' vs. '%s'", encoded, uristring) |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
func TestValidateEscapedAttrs(t *testing.T) { |
| 218 |
type data struct { |
| 219 |
uri string |
| 220 |
testp []string // pair of attribute and expected value in path part (unescaped, pct-encoded) |
| 221 |
testq []string // pair of attribute and expected value in query part (unescaped, pct-encoded) |
| 222 |
format bool // whether to format the URI and compare against given uri (equal strings) |
| 223 |
} |
| 224 |
input := []data{ |
| 225 |
{ |
| 226 |
uri: "pkcs11:token=Software%20PKCS%2311%20softtoken;manufacturer=Snake%20Oil,%20Inc.?pin-value=the-pin", |
| 227 |
testp: []string{"token", "Software PKCS#11 softtoken", "Software%20PKCS%2311%20softtoken"}, |
| 228 |
format: false, |
| 229 |
}, { |
| 230 |
uri: "pkcs11:token=My%20token%25%20created%20by%20Joe;library-version=3;id=%01%02%03%Ba%dd%Ca%fe%04%05%06", |
| 231 |
testp: []string{"token", "My token% created by Joe", "My%20token%25%20created%20by%20Joe"}, |
| 232 |
format: false, |
| 233 |
}, { |
| 234 |
// test pk11-query-res-avail and pk11-path-res-avail special characters |
| 235 |
uri: "pkcs11:token=:[]@!$'()*+,=&?attr=:[]@!$'()*+,=/?", |
| 236 |
testp: []string{"token", ":[]@!$'()*+,=&", ":[]@!$'()*+,=&"}, |
| 237 |
testq: []string{"attr", ":[]@!$'()*+,=/?", ":[]@!$'()*+,=/?"}, |
| 238 |
format: true, |
| 239 |
}, { |
| 240 |
// test (some) unnecessarily escaped characters |
| 241 |
uri: "pkcs11:token=%3a%5b%5d%40%21%24%27%28%29%2a%2b%2c%26%3d-%60%20%3c%3e%7b", |
| 242 |
testp: []string{"token", ":[]@!$'()*+,&=-` <>{", ":[]@!$'()*+,&=-%60%20%3C%3E%7B"}, |
| 243 |
format: false, |
| 244 |
}, { |
| 245 |
// test some non-printable characters that have to be escape; |
| 246 |
uri: "pkcs11:token=%00%01%02Hello%FF%FE", |
| 247 |
testp: []string{"token", "\x00\x01\x02Hello\xff\xfe", "%00%01%02Hello%FF%FE"}, |
| 248 |
format: true, |
| 249 |
}, |
| 250 |
} |
| 251 |
|
| 252 |
uri := New() |
| 253 |
for _, data := range input { |
| 254 |
err := uri.Parse(data.uri) |
| 255 |
if err != nil { |
| 256 |
t.Fatalf("Could not parse URI '%s': %s", data.uri, err) |
| 257 |
} |
| 258 |
if len(data.testp[1]) > 0 { |
| 259 |
v, _ := uri.GetPathAttribute(data.testp[0], false) |
| 260 |
if v != data.testp[1] { |
| 261 |
t.Fatalf("Got unexpected unescaped path attribute value '%s'; expected '%s'", v, data.testp[1]) |
| 262 |
} |
| 263 |
} |
| 264 |
if len(data.testp[2]) > 0 { |
| 265 |
v, _ := uri.GetPathAttribute(data.testp[0], true) |
| 266 |
if v != data.testp[2] { |
| 267 |
t.Fatalf("Got unexpected pct-encoded path attribute value '%s'; expected '%s'", v, data.testp[2]) |
| 268 |
} |
| 269 |
} |
| 270 |
if len(data.testq) > 0 { |
| 271 |
if len(data.testq[1]) > 0 { |
| 272 |
v, _ := uri.GetQueryAttribute(data.testq[0], false) |
| 273 |
if v != data.testq[1] { |
| 274 |
t.Fatalf("Got unexpected unescaped query attribute value '%s'; expected '%s'", v, data.testq[1]) |
| 275 |
} |
| 276 |
} |
| 277 |
if len(data.testq[2]) > 0 { |
| 278 |
v, _ := uri.GetQueryAttribute(data.testq[0], true) |
| 279 |
if v != data.testq[2] { |
| 280 |
t.Fatalf("Got unexpected pct-encoded query attribute value '%s'; expected '%s'", v, data.testq[2]) |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
if data.format { |
| 285 |
encoded, err := uri.Format() |
| 286 |
if err != nil { |
| 287 |
t.Fatalf("Could not format URI '%s': %s", data.uri, err) |
| 288 |
} |
| 289 |
if encoded != data.uri { |
| 290 |
t.Fatalf("Formatted URI is different than expected: '%s' vs. '%s'", encoded, data.uri) |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
// This test requires SoftHSM to be installed, will warn otherwise |
| 297 |
func TestGetModule(t *testing.T) { |
| 298 |
uri := New() |
| 299 |
uri.SetModuleDirectories(modulePaths) |
| 300 |
uri.SetAllowAnyModule(true) |
| 301 |
|
| 302 |
uristring := "pkcs11:?module-name=softhsm2" |
| 303 |
err := uri.Parse(uristring) |
| 304 |
if err != nil { |
| 305 |
t.Fatalf("Could not parse pkcs11 URI '%s': %s", uristring, err) |
| 306 |
} |
| 307 |
|
| 308 |
_, err = uri.GetModule() |
| 309 |
if err != nil { |
| 310 |
t.Skipf("Is softhsm2 not installed? GetModule() failed: %s", err) |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
// This test requires SoftHSM to be installed, will warn otherwise |
| 315 |
func TestGetModuleRestricted(t *testing.T) { |
| 316 |
uri := New() |
| 317 |
uri.SetModuleDirectories(modulePaths) |
| 318 |
|
| 319 |
uristring := "pkcs11:?module-name=softhsm2" |
| 320 |
err := uri.Parse(uristring) |
| 321 |
if err != nil { |
| 322 |
t.Fatalf("Could not parse pkcs11 URI '%s': %s", uristring, err) |
| 323 |
} |
| 324 |
|
| 325 |
// we don't want any results |
| 326 |
uri.SetAllowedModulePaths([]string{"/usr"}) |
| 327 |
_, err = uri.GetModule() |
| 328 |
if err == nil { |
| 329 |
t.Errorf("GetModule() must fail due to allowed file paths: %s", err) |
| 330 |
} |
| 331 |
|
| 332 |
// this time we want module paths |
| 333 |
uri.SetAllowedModulePaths(modulePaths) |
| 334 |
_, err = uri.GetModule() |
| 335 |
if err != nil { |
| 336 |
t.Skipf("Is softhsm2 not installed? GetModule() failed: %s", err) |
| 337 |
} |
| 338 |
} |