| 1 |
// Copyright 2021 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package pkcs11 |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"crypto" |
| 20 |
"crypto/ecdsa" |
| 21 |
"crypto/elliptic" |
| 22 |
"crypto/rand" |
| 23 |
"crypto/rsa" |
| 24 |
"crypto/sha1" |
| 25 |
"crypto/sha256" |
| 26 |
"crypto/x509" |
| 27 |
"encoding/pem" |
| 28 |
"flag" |
| 29 |
"fmt" |
| 30 |
"io" |
| 31 |
"os" |
| 32 |
"path/filepath" |
| 33 |
"reflect" |
| 34 |
"sort" |
| 35 |
"strings" |
| 36 |
"testing" |
| 37 |
) |
| 38 |
|
| 39 |
var requireSoftHSMv2 = flag.Bool("require-libsofthsm2", false, |
| 40 |
"When set, tests will fail if libsofthsm2 is not available.") |
| 41 |
|
| 42 |
const ( |
| 43 |
libSoftHSMPath = "/usr/lib/softhsm/libsofthsm2.so" |
| 44 |
syslogPath = "/var/log/syslog" |
| 45 |
|
| 46 |
testAdminPIN = "12345" |
| 47 |
testPIN = "1234" |
| 48 |
testLabel = "label" |
| 49 |
) |
| 50 |
|
| 51 |
func newTestModule(t *testing.T) *Module { |
| 52 |
if _, err := os.Stat(libSoftHSMPath); err != nil { |
| 53 |
if *requireSoftHSMv2 { |
| 54 |
t.Fatalf("libsofthsm2 not installed") |
| 55 |
} |
| 56 |
// TODO(ericchiang): do an actual lookup of registered PKCS #11 modules. |
| 57 |
t.Skipf("libsofthsm2 not installed, skipping testing") |
| 58 |
} |
| 59 |
|
| 60 |
// Open syslog file and seek to end before the tests starts. Anything read |
| 61 |
// after this will have been logged during the test. |
| 62 |
f, err := os.Open(syslogPath) |
| 63 |
if err != nil { |
| 64 |
t.Fatalf("opening syslog file: %v", err) |
| 65 |
} |
| 66 |
if _, err := f.Seek(0, io.SeekEnd); err != nil { |
| 67 |
f.Close() |
| 68 |
t.Fatalf("seeking to end of file: %v", err) |
| 69 |
} |
| 70 |
|
| 71 |
t.Cleanup(func() { |
| 72 |
defer f.Close() |
| 73 |
if !t.Failed() { |
| 74 |
return |
| 75 |
} |
| 76 |
|
| 77 |
data, err := io.ReadAll(f) |
| 78 |
if err != nil { |
| 79 |
t.Errorf("reading syslog file: %v", err) |
| 80 |
} |
| 81 |
lines := strings.Split(string(data), "\n") |
| 82 |
for _, line := range lines { |
| 83 |
// softhsm tags the syslog files using the binary name, not "softhsm" |
| 84 |
// or a related string. Logs were tagged with "pkcs11.test". |
| 85 |
if !strings.Contains(line, "pkcs11") { |
| 86 |
continue |
| 87 |
} |
| 88 |
|
| 89 |
t.Logf("%s", line) |
| 90 |
} |
| 91 |
}) |
| 92 |
|
| 93 |
// See softhsm2.conf(5) for config details |
| 94 |
configPath := filepath.Join(t.TempDir(), "softhsm.conf") |
| 95 |
tokensPath := filepath.Join(t.TempDir(), "tokens") |
| 96 |
if err := os.Mkdir(tokensPath, 0755); err != nil { |
| 97 |
t.Fatalf("create test tokens directory: %v", err) |
| 98 |
} |
| 99 |
|
| 100 |
configData := fmt.Sprintf(` |
| 101 |
directories.tokendir = %s |
| 102 |
`, tokensPath) |
| 103 |
if err := os.WriteFile(configPath, []byte(configData), 0644); err != nil { |
| 104 |
t.Fatalf("write softhsm config: %v", err) |
| 105 |
} |
| 106 |
t.Setenv("SOFTHSM2_CONF", configPath) |
| 107 |
|
| 108 |
m, err := Open(libSoftHSMPath) |
| 109 |
if err != nil { |
| 110 |
t.Fatalf("Open(%s): %v", libSoftHSMPath, err) |
| 111 |
} |
| 112 |
t.Cleanup(func() { |
| 113 |
if err := m.Close(); err != nil { |
| 114 |
t.Errorf("Close module: %v", err) |
| 115 |
} |
| 116 |
}) |
| 117 |
return m |
| 118 |
} |
| 119 |
|
| 120 |
func newTestSlot(t *testing.T) *Slot { |
| 121 |
m := newTestModule(t) |
| 122 |
opts := slotOptions{ |
| 123 |
AdminPIN: testAdminPIN, |
| 124 |
PIN: testPIN, |
| 125 |
Label: testLabel, |
| 126 |
} |
| 127 |
if err := m.createSlot(0, opts); err != nil { |
| 128 |
t.Fatalf("createSlot(0, %v): %v", opts, err) |
| 129 |
} |
| 130 |
|
| 131 |
s, err := m.Slot(0, Options{PIN: testPIN, ReadWrite: true}) |
| 132 |
if err != nil { |
| 133 |
t.Fatalf("Slot(0): %v", err) |
| 134 |
} |
| 135 |
t.Cleanup(func() { |
| 136 |
if err := s.Close(); err != nil { |
| 137 |
t.Errorf("Closing slot: %v", err) |
| 138 |
} |
| 139 |
}) |
| 140 |
return s |
| 141 |
} |
| 142 |
|
| 143 |
func TestNewModule(t *testing.T) { |
| 144 |
newTestModule(t) |
| 145 |
} |
| 146 |
|
| 147 |
func TestSlotInit(t *testing.T) { |
| 148 |
m := newTestModule(t) |
| 149 |
opts := slotOptions{ |
| 150 |
AdminPIN: testAdminPIN, |
| 151 |
PIN: testPIN, |
| 152 |
Label: testLabel, |
| 153 |
} |
| 154 |
if err := m.createSlot(0, opts); err != nil { |
| 155 |
t.Fatalf("createSlot(0, %v): %v", opts, err) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func TestSlotIDs(t *testing.T) { |
| 160 |
m := newTestModule(t) |
| 161 |
got, err := m.SlotIDs() |
| 162 |
if err != nil { |
| 163 |
t.Fatalf("SlotIDs(): %v", err) |
| 164 |
} |
| 165 |
want := []uint32{0} |
| 166 |
sort.Slice(got, func(i, j int) bool { return got[i] < got[j] }) |
| 167 |
sort.Slice(want, func(i, j int) bool { return want[i] < want[j] }) |
| 168 |
if !reflect.DeepEqual(got, want) { |
| 169 |
t.Errorf("SlotIDs() returned unexpected value, got %v, want %v", got, want) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func TestInfo(t *testing.T) { |
| 174 |
m := newTestModule(t) |
| 175 |
info := m.Info() |
| 176 |
|
| 177 |
wantMan := "SoftHSM" |
| 178 |
if info.Manufacturer != wantMan { |
| 179 |
t.Errorf("SlotInfo() unexpected manufacturer, got %s, want %s", info.Manufacturer, wantMan) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func TestSlotInfo(t *testing.T) { |
| 184 |
m := newTestModule(t) |
| 185 |
opts := slotOptions{ |
| 186 |
AdminPIN: testAdminPIN, |
| 187 |
PIN: testPIN, |
| 188 |
Label: testLabel, |
| 189 |
} |
| 190 |
if err := m.createSlot(0, opts); err != nil { |
| 191 |
t.Fatalf("createSlot(0, %v): %v", opts, err) |
| 192 |
} |
| 193 |
|
| 194 |
info, err := m.SlotInfo(0) |
| 195 |
if err != nil { |
| 196 |
t.Fatalf("SlotInfo(0): %v", err) |
| 197 |
} |
| 198 |
wantLabel := testLabel |
| 199 |
if info.Label != wantLabel { |
| 200 |
t.Errorf("SlotInfo() unexpected label, got %s, want %s", info.Label, wantLabel) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
func TestSlot(t *testing.T) { |
| 205 |
tests := []struct { |
| 206 |
name string |
| 207 |
opts Options |
| 208 |
}{ |
| 209 |
{"Default", Options{}}, |
| 210 |
{"RWSession", Options{ReadWrite: true}}, |
| 211 |
{"PIN", Options{PIN: testPIN}}, |
| 212 |
{"AdminPIN", Options{ReadWrite: true, AdminPIN: testAdminPIN}}, |
| 213 |
} |
| 214 |
for _, test := range tests { |
| 215 |
t.Run(test.name, func(t *testing.T) { |
| 216 |
m := newTestModule(t) |
| 217 |
opts := slotOptions{ |
| 218 |
AdminPIN: testAdminPIN, |
| 219 |
PIN: testPIN, |
| 220 |
Label: testLabel, |
| 221 |
} |
| 222 |
if err := m.createSlot(0, opts); err != nil { |
| 223 |
t.Fatalf("createSlot(0, %v): %v", opts, err) |
| 224 |
} |
| 225 |
|
| 226 |
s, err := m.Slot(0, test.opts) |
| 227 |
if err != nil { |
| 228 |
t.Fatalf("Slot(0): %v", err) |
| 229 |
} |
| 230 |
if err := s.Close(); err != nil { |
| 231 |
t.Fatalf("Close(): %v", err) |
| 232 |
} |
| 233 |
}) |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
func TestGenerateECDSA(t *testing.T) { |
| 238 |
tests := []struct { |
| 239 |
name string |
| 240 |
curve elliptic.Curve |
| 241 |
}{ |
| 242 |
{"P256", elliptic.P256()}, |
| 243 |
{"P384", elliptic.P384()}, |
| 244 |
{"P521", elliptic.P521()}, |
| 245 |
} |
| 246 |
|
| 247 |
for _, test := range tests { |
| 248 |
t.Run(test.name, func(t *testing.T) { |
| 249 |
s := newTestSlot(t) |
| 250 |
|
| 251 |
o := keyOptions{ECDSACurve: test.curve} |
| 252 |
if _, err := s.generate(o); err != nil { |
| 253 |
t.Fatalf("generate(%#v) failed: %v", o, err) |
| 254 |
} |
| 255 |
}) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
func TestECDSAPublicKey(t *testing.T) { |
| 260 |
tests := []struct { |
| 261 |
name string |
| 262 |
curve elliptic.Curve |
| 263 |
}{ |
| 264 |
{"P256", elliptic.P256()}, |
| 265 |
{"P384", elliptic.P384()}, |
| 266 |
{"P521", elliptic.P521()}, |
| 267 |
} |
| 268 |
|
| 269 |
for _, test := range tests { |
| 270 |
t.Run(test.name, func(t *testing.T) { |
| 271 |
s := newTestSlot(t) |
| 272 |
|
| 273 |
o := keyOptions{ECDSACurve: test.curve} |
| 274 |
if _, err := s.generate(o); err != nil { |
| 275 |
t.Fatalf("generate(%#v) failed: %v", o, err) |
| 276 |
} |
| 277 |
objs, err := s.Objects(Filter{Class: ClassPublicKey}) |
| 278 |
if err != nil { |
| 279 |
t.Fatalf("Objects(): %v", err) |
| 280 |
} |
| 281 |
if len(objs) != 1 { |
| 282 |
t.Fatalf("Objects() returned an unexpected number of objects, got %d, want 1", len(objs)) |
| 283 |
} |
| 284 |
obj := objs[0] |
| 285 |
pub, err := obj.PublicKey() |
| 286 |
if err != nil { |
| 287 |
t.Fatalf("PublicKey(): %v", err) |
| 288 |
} |
| 289 |
if _, ok := pub.(*ecdsa.PublicKey); !ok { |
| 290 |
t.Errorf("PublicKey() unexpected type, got %T, want *ecdsa.PublicKey", pub) |
| 291 |
} |
| 292 |
}) |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
func TestRSAPublicKey(t *testing.T) { |
| 297 |
tests := []struct { |
| 298 |
name string |
| 299 |
bits int |
| 300 |
}{ |
| 301 |
{"2048", 2048}, |
| 302 |
{"4096", 4096}, |
| 303 |
} |
| 304 |
|
| 305 |
for _, test := range tests { |
| 306 |
t.Run(test.name, func(t *testing.T) { |
| 307 |
s := newTestSlot(t) |
| 308 |
|
| 309 |
o := keyOptions{RSABits: test.bits} |
| 310 |
if _, err := s.generate(o); err != nil { |
| 311 |
t.Fatalf("generate(%#v) failed: %v", o, err) |
| 312 |
} |
| 313 |
objs, err := s.Objects(Filter{Class: ClassPublicKey}) |
| 314 |
if err != nil { |
| 315 |
t.Fatalf("Objects(): %v", err) |
| 316 |
} |
| 317 |
if len(objs) != 1 { |
| 318 |
t.Fatalf("Objects() returned an unexpected number of objects, got %d, want 1", len(objs)) |
| 319 |
} |
| 320 |
obj := objs[0] |
| 321 |
pub, err := obj.PublicKey() |
| 322 |
if err != nil { |
| 323 |
t.Fatalf("PublicKey(): %v", err) |
| 324 |
} |
| 325 |
rsaPub, ok := pub.(*rsa.PublicKey) |
| 326 |
if !ok { |
| 327 |
t.Fatalf("PublicKey() unexpected type, got %T, want *rsa.PublicKey", pub) |
| 328 |
} |
| 329 |
if got := rsaPub.Size() * 8; got != test.bits { |
| 330 |
t.Errorf("Generate returned public key with size %d, want %d", got, test.bits) |
| 331 |
} |
| 332 |
}) |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
func TestECDSAPrivateKey(t *testing.T) { |
| 337 |
tests := []struct { |
| 338 |
name string |
| 339 |
curve elliptic.Curve |
| 340 |
}{ |
| 341 |
{"P256", elliptic.P256()}, |
| 342 |
{"P384", elliptic.P384()}, |
| 343 |
{"P521", elliptic.P521()}, |
| 344 |
} |
| 345 |
|
| 346 |
for _, test := range tests { |
| 347 |
t.Run(test.name, func(t *testing.T) { |
| 348 |
s := newTestSlot(t) |
| 349 |
|
| 350 |
o := keyOptions{ECDSACurve: test.curve} |
| 351 |
priv, err := s.generate(o) |
| 352 |
if err != nil { |
| 353 |
t.Fatalf("generate(%#v) failed: %v", o, err) |
| 354 |
} |
| 355 |
signer, ok := priv.(crypto.Signer) |
| 356 |
if !ok { |
| 357 |
t.Fatalf("generate() key is unexpected type, got %T, want crypto.Signer", priv) |
| 358 |
} |
| 359 |
pub, ok := signer.Public().(*ecdsa.PublicKey) |
| 360 |
if !ok { |
| 361 |
t.Fatalf("Public() key is unexpected type, got %T, want *ecdsa.PublicKey", pub) |
| 362 |
} |
| 363 |
|
| 364 |
h := sha256.New() |
| 365 |
h.Write([]byte("test")) |
| 366 |
digest := h.Sum(nil) |
| 367 |
|
| 368 |
sig, err := signer.Sign(rand.Reader, digest, crypto.SHA256) |
| 369 |
if err != nil { |
| 370 |
t.Fatalf("Sign() failed: %v", err) |
| 371 |
} |
| 372 |
if !ecdsa.VerifyASN1(pub, digest, sig) { |
| 373 |
t.Errorf("Signature failed to verify") |
| 374 |
} |
| 375 |
}) |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
func TestRSAPrivateKey(t *testing.T) { |
| 380 |
tests := []struct { |
| 381 |
name string |
| 382 |
bits int |
| 383 |
}{ |
| 384 |
{"2048", 2048}, |
| 385 |
{"4096", 4096}, |
| 386 |
} |
| 387 |
|
| 388 |
for _, test := range tests { |
| 389 |
t.Run(test.name, func(t *testing.T) { |
| 390 |
s := newTestSlot(t) |
| 391 |
|
| 392 |
o := keyOptions{RSABits: test.bits} |
| 393 |
priv, err := s.generate(o) |
| 394 |
if err != nil { |
| 395 |
t.Fatalf("generate(%#v) failed: %v", o, err) |
| 396 |
} |
| 397 |
signer, ok := priv.(crypto.Signer) |
| 398 |
if !ok { |
| 399 |
t.Fatalf("generate() key is unexpected type, got %T, want crypto.Signer", priv) |
| 400 |
} |
| 401 |
pub, ok := signer.Public().(*rsa.PublicKey) |
| 402 |
if !ok { |
| 403 |
t.Fatalf("Public() key is unexpected type, got %T, want *rsa.PublicKey", pub) |
| 404 |
} |
| 405 |
|
| 406 |
h := sha256.New() |
| 407 |
h.Write([]byte("test")) |
| 408 |
digest := h.Sum(nil) |
| 409 |
|
| 410 |
sig, err := signer.Sign(rand.Reader, digest, crypto.SHA256) |
| 411 |
if err != nil { |
| 412 |
t.Fatalf("Sign() failed: %v", err) |
| 413 |
} |
| 414 |
if err := rsa.VerifyPKCS1v15(pub, crypto.SHA256, digest, sig); err != nil { |
| 415 |
t.Errorf("Signature failed to verify: %v", err) |
| 416 |
} |
| 417 |
}) |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
func TestRSAPrivateKeyPSS(t *testing.T) { |
| 422 |
tests := []struct { |
| 423 |
name string |
| 424 |
bits int |
| 425 |
}{ |
| 426 |
{"2048", 2048}, |
| 427 |
{"4096", 4096}, |
| 428 |
} |
| 429 |
|
| 430 |
for _, test := range tests { |
| 431 |
t.Run(test.name, func(t *testing.T) { |
| 432 |
s := newTestSlot(t) |
| 433 |
|
| 434 |
o := keyOptions{RSABits: test.bits} |
| 435 |
priv, err := s.generate(o) |
| 436 |
if err != nil { |
| 437 |
t.Fatalf("generate(%#v) failed: %v", o, err) |
| 438 |
} |
| 439 |
signer, ok := priv.(crypto.Signer) |
| 440 |
if !ok { |
| 441 |
t.Fatalf("generate() key is unexpected type, got %T, want crypto.Signer", priv) |
| 442 |
} |
| 443 |
pub, ok := signer.Public().(*rsa.PublicKey) |
| 444 |
if !ok { |
| 445 |
t.Fatalf("Public() key is unexpected type, got %T, want *rsa.PublicKey", pub) |
| 446 |
} |
| 447 |
|
| 448 |
h := sha256.New() |
| 449 |
h.Write([]byte("test")) |
| 450 |
digest := h.Sum(nil) |
| 451 |
opts := &rsa.PSSOptions{ |
| 452 |
Hash: crypto.SHA256, |
| 453 |
} |
| 454 |
|
| 455 |
sig, err := signer.Sign(rand.Reader, digest, opts) |
| 456 |
if err != nil { |
| 457 |
t.Fatalf("Sign() failed: %v", err) |
| 458 |
} |
| 459 |
if err := rsa.VerifyPSS(pub, crypto.SHA256, digest, sig, opts); err != nil { |
| 460 |
t.Errorf("Signature failed to verify: %v", err) |
| 461 |
} |
| 462 |
}) |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
func TestObjects(t *testing.T) { |
| 467 |
tests := []struct { |
| 468 |
name string |
| 469 |
opts Filter |
| 470 |
want []Class |
| 471 |
}{ |
| 472 |
{"AllObjects", Filter{}, []Class{ClassPublicKey, ClassPrivateKey}}, |
| 473 |
{"PrivateKey", Filter{Class: ClassPrivateKey}, []Class{ClassPrivateKey}}, |
| 474 |
{"PublicKey", Filter{Class: ClassPublicKey}, []Class{ClassPublicKey}}, |
| 475 |
{"ByLabel", Filter{Label: "privatekey"}, []Class{ClassPrivateKey}}, |
| 476 |
} |
| 477 |
for _, test := range tests { |
| 478 |
t.Run(test.name, func(t *testing.T) { |
| 479 |
s := newTestSlot(t) |
| 480 |
|
| 481 |
o := keyOptions{ |
| 482 |
ECDSACurve: elliptic.P256(), |
| 483 |
LabelPrivate: "privatekey", |
| 484 |
} |
| 485 |
if _, err := s.generate(o); err != nil { |
| 486 |
t.Fatalf("generate(%#v) failed: %v", o, err) |
| 487 |
} |
| 488 |
|
| 489 |
objs, err := s.Objects(test.opts) |
| 490 |
if err != nil { |
| 491 |
t.Fatalf("Slot(0).Objects(): %v", err) |
| 492 |
} |
| 493 |
|
| 494 |
var got []Class |
| 495 |
for _, o := range objs { |
| 496 |
got = append(got, o.Class()) |
| 497 |
} |
| 498 |
sort.Slice(test.want, func(i, j int) bool { return test.want[i] < test.want[j] }) |
| 499 |
sort.Slice(got, func(i, j int) bool { return got[i] < got[j] }) |
| 500 |
|
| 501 |
if !reflect.DeepEqual(test.want, got) { |
| 502 |
t.Fatalf("Objects() classes mismatch, got %v, want %v", got, test.want) |
| 503 |
} |
| 504 |
}) |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
// Generated with: |
| 509 |
// openssl req -subj '/CN=test' -nodes -x509 -newkey rsa:4096 -keyout /dev/null -out /dev/stdout -days 365 |
| 510 |
const testCertData = `-----BEGIN CERTIFICATE----- |
| 511 |
MIIE/zCCAuegAwIBAgIUSxn81BYTB9S1zx4v6EhCOvx5PU8wDQYJKoZIhvcNAQEL |
| 512 |
BQAwDzENMAsGA1UEAwwEdGVzdDAeFw0yMTA5MjkwMDI3NTBaFw0yMjA5MjkwMDI3 |
| 513 |
NTBaMA8xDTALBgNVBAMMBHRlc3QwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIK |
| 514 |
AoICAQC8boHADxkGDGGRlR2GhhkfT7i/+7KrLg/2Px12dIpATtfAOB2gK0sHZpQ3 |
| 515 |
cui8MKm6dtICpY7sV+9ZTNGpeiTRxoJ+/9KhzMNwOgY8bBUR8QdFrLOW7pdxuJqs |
| 516 |
MLJ6IZKyAb02bwHBcBZbsMOVWK8iqMolsdJ6fPYC+aRRExfQg2dEMX7utGbolBLq |
| 517 |
IADgmEVeYH2oRDED+a0MSO8nRsO2ef+L6dB038z+xop5kPwjlyEaF8se/arZhfzN |
| 518 |
Tgv0m5FYeNXIdwDRqb1vhKXIRC6HkHkdjyGpJhjx+S+mtAITO/wjiuXVdiq37qQi |
| 519 |
aIfP7iahmYCmfFleG/czQWs0DPaAXOlOKCdteeUwhPEN9LAXp4LJTukUiidNvtxq |
| 520 |
eb7cRo6rucUSLur3rbaGq/YuSHbHeBLS6VrBQ9QZH1fTCsWqhAhR8zz7qqZZk1M7 |
| 521 |
LBdsOOByxEAKj9IVkXtQDWeL4iH4PrV8fGb+grrqja6IvgPOm7jO2AbCubPR5V02 |
| 522 |
yhIAggZIOx3Mu93qzxcrn1Y5TH4QhqgCa8Mvxe2mQrZlla3lTLFY6SW3/N3iMqzb |
| 523 |
rZx4u/QThCIovrNUr11RhNU4unFrFIHWWrQg52Zh6dxs7y7lwmtV1a4trtM54jsK |
| 524 |
DmBWGovSSRwmrOHyp5xSUWTIe0cF4yhgXzKYZsB8kcKzQoX8TwIDAQABo1MwUTAd |
| 525 |
BgNVHQ4EFgQUZzvuma3BmzFhKWb64Y2PVq/+aK0wHwYDVR0jBBgwFoAUZzvuma3B |
| 526 |
mzFhKWb64Y2PVq/+aK0wDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOC |
| 527 |
AgEAGTtRUGa1Xhx633QWPFgdx3Ylg1paIve882AT1mUN+MyJ88Cx1wvXQwJsf2TI |
| 528 |
6iE4uj2PLQvpt6mrNqT1ItdN4iyfCiXqkzZJ1uXOnneJujk+IuhHbUgP78vYSrZO |
| 529 |
2akl9S3BgwvDLcV6EOXfo5ERU8rTWfYu64tDNQcaxP0pNoyD6um5BsmB2Jxznn4F |
| 530 |
HbrQcBFh4hli1cAbjXeXWgnWuT6Ajz0L98fKaDhx3D7ggMPYd64/XVQBZSw2gCRJ |
| 531 |
9i26kFdbmLz6nDq8RKoiXy8dOgtyCj26QevoDlsq5fIdqDATScKL1/cKBFiwT2h0 |
| 532 |
nbxl1SqoXvP4QRuB7444LEmPrU2TIIhaICoHnCTmr5P2CB4PL8KggVyKHWb3eYR9 |
| 533 |
5/HsXJA21uQqezNhr+mKTtAob4kpWt1MoICul7uIy4fwjeCcCQpOCBlVt11uroN+ |
| 534 |
0OqSY5CDjQfZ+2C1gLdKUZ7nomRuBdxWh+f48dtIh46vkw/dXN5prmU7j8QoAbfr |
| 535 |
40+3biWKDfbCJ0auEucdM3tLGxim1HlKf7ROmrrS8gEBH23Ww3ibKPBnNiQvTK/L |
| 536 |
nBPryTEU4DaFuWh36J5tGuqZFCo9S58dCmajvhAMs2hpw4u6tLCaiaqtUByGnDv9 |
| 537 |
6ymrXrM0Nw+Ri1Lz+EMZ71I5uC4BItv+uZNm3XJz+/CDrMw= |
| 538 |
-----END CERTIFICATE-----` |
| 539 |
|
| 540 |
func mustParseCertificate(s string) *x509.Certificate { |
| 541 |
b, _ := pem.Decode([]byte(s)) |
| 542 |
if b == nil { |
| 543 |
panic("no pem data in certificate") |
| 544 |
} |
| 545 |
cert, err := x509.ParseCertificate(b.Bytes) |
| 546 |
if err != nil { |
| 547 |
panic("parse certificate: " + err.Error()) |
| 548 |
} |
| 549 |
return cert |
| 550 |
} |
| 551 |
|
| 552 |
func TestCreateCertificate(t *testing.T) { |
| 553 |
s := newTestSlot(t) |
| 554 |
|
| 555 |
cert := mustParseCertificate(testCertData) |
| 556 |
|
| 557 |
want := "testcert" |
| 558 |
opt := createOptions{ |
| 559 |
X509Certificate: cert, |
| 560 |
Label: want, |
| 561 |
} |
| 562 |
o, err := s.create(opt) |
| 563 |
if err != nil { |
| 564 |
t.Fatalf("create(%v) %v", opt, err) |
| 565 |
} |
| 566 |
got, err := o.Label() |
| 567 |
if err != nil { |
| 568 |
t.Fatalf("Label(): %v", err) |
| 569 |
} |
| 570 |
if got != want { |
| 571 |
t.Errorf("Label() did not match, got %s, want %s", got, want) |
| 572 |
} |
| 573 |
|
| 574 |
if err := o.setLabel("bar"); err != nil { |
| 575 |
t.Fatalf("setLabel(): %v", err) |
| 576 |
} |
| 577 |
want = "bar" |
| 578 |
got, err = o.Label() |
| 579 |
if err != nil { |
| 580 |
t.Fatalf("Label(): %v", err) |
| 581 |
} |
| 582 |
if got != want { |
| 583 |
t.Errorf("Label() did not match after setting it, got %s, want %s", got, want) |
| 584 |
} |
| 585 |
|
| 586 |
c, err := o.Certificate() |
| 587 |
if err != nil { |
| 588 |
t.Fatalf("Certificate(): %v", err) |
| 589 |
} |
| 590 |
gotCert, err := c.X509() |
| 591 |
if err != nil { |
| 592 |
t.Fatalf("Getting X509() certificate: %v", err) |
| 593 |
} |
| 594 |
if !bytes.Equal(gotCert.Raw, cert.Raw) { |
| 595 |
t.Errorf("Returned certificate did not match loaded certificate") |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
func TestDecryptOAEP(t *testing.T) { |
| 600 |
msg := "Plain text to encrypt" |
| 601 |
b := []byte(msg) |
| 602 |
tests := []struct { |
| 603 |
name string |
| 604 |
bits int |
| 605 |
}{ |
| 606 |
{"2048", 2048}, |
| 607 |
{"4096", 4096}, |
| 608 |
} |
| 609 |
for _, test := range tests { |
| 610 |
t.Run(test.name, func(t *testing.T) { |
| 611 |
s := newTestSlot(t) |
| 612 |
o := keyOptions{RSABits: test.bits} |
| 613 |
priv, err := s.generate(o) |
| 614 |
if err != nil { |
| 615 |
t.Fatalf("generate(%#v) failed: %v", o, err) |
| 616 |
} |
| 617 |
rsaPub := priv.(*rsaPrivateKey).pub |
| 618 |
// SHA1 is the only hash function supported by softhsm |
| 619 |
cipher, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, rsaPub, b, nil) |
| 620 |
if err != nil { |
| 621 |
t.Fatalf("EncryptOAEP Error: %v", err) |
| 622 |
} |
| 623 |
opts := &rsa.OAEPOptions{Hash: crypto.SHA1} |
| 624 |
rsaDecrypter := priv.(crypto.Decrypter) |
| 625 |
decrypted, err := rsaDecrypter.Decrypt(nil, cipher, opts) |
| 626 |
if err != nil { |
| 627 |
t.Fatalf("Decrypt Error: %v", err) |
| 628 |
} |
| 629 |
if string(decrypted) != msg { |
| 630 |
t.Errorf("Decrypt Error: expected %q, got %q", msg, string(decrypted)) |
| 631 |
} |
| 632 |
}) |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
func TestDecryptPKCS(t *testing.T) { |
| 637 |
msg := "Plain text to encrypt" |
| 638 |
b := []byte(msg) |
| 639 |
tests := []struct { |
| 640 |
name string |
| 641 |
bits int |
| 642 |
}{ |
| 643 |
{"2048", 2048}, |
| 644 |
{"4096", 4096}, |
| 645 |
} |
| 646 |
for _, test := range tests { |
| 647 |
t.Run(test.name, func(t *testing.T) { |
| 648 |
s := newTestSlot(t) |
| 649 |
o := keyOptions{RSABits: test.bits} |
| 650 |
priv, err := s.generate(o) |
| 651 |
if err != nil { |
| 652 |
t.Fatalf("generate(%#v) failed: %v", o, err) |
| 653 |
} |
| 654 |
rsaPub := priv.(*rsaPrivateKey).pub |
| 655 |
cipher, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPub, b) |
| 656 |
if err != nil { |
| 657 |
t.Fatalf("EncryptPKCS1v15 Error: %v", err) |
| 658 |
} |
| 659 |
rsaDecrypter := priv.(crypto.Decrypter) |
| 660 |
|
| 661 |
// nil opts for decrypting using PKCS #1 v 1.5 |
| 662 |
decrypted, err := rsaDecrypter.Decrypt(nil, cipher, nil) |
| 663 |
if err != nil { |
| 664 |
t.Fatalf("Decrypt Error: %v", err) |
| 665 |
} |
| 666 |
if string(decrypted) != msg { |
| 667 |
t.Errorf("Decrypt Error: expected %q, got %q", msg, string(decrypted)) |
| 668 |
} |
| 669 |
}) |
| 670 |
} |
| 671 |
} |