github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/pkcs12/safebags_test.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  package pkcs12
     5  
     6  import (
     7  	"crypto/rand"
     8  	"crypto/rsa"
     9  	"encoding/asn1"
    10  	"testing"
    11  )
    12  
    13  // Assert the default algorithm parameters are in the correct order,
    14  // and default to the correct value.  Defaults are based on OpenSSL.
    15  //  1. IterationCount, defaults to 2,048 long.
    16  //  2. Salt, is 8 bytes long.
    17  func TestDefaultAlgorithmParametersPkcs8ShroudedKeyBag(t *testing.T) {
    18  	privateKey, err := rsa.GenerateKey(rand.Reader, 512)
    19  	if err != nil {
    20  		t.Fatalf("failed to generate a private key: %s", err)
    21  	}
    22  
    23  	password := []byte("sesame")
    24  	bytes, err := encodePkcs8ShroudedKeyBag(privateKey, password)
    25  	if err != nil {
    26  		t.Fatalf("failed to encode PKCS#8 shrouded key bag: %s", err)
    27  	}
    28  
    29  	var pkinfo encryptedPrivateKeyInfo
    30  	rest, err := asn1.Unmarshal(bytes, &pkinfo)
    31  	if err != nil {
    32  		t.Fatalf("failed to unmarshal encryptedPrivateKeyInfo %s", err)
    33  	}
    34  
    35  	if len(rest) != 0 {
    36  		t.Fatalf("unexpected trailing bytes of len=%d, bytes=%x", len(rest), rest)
    37  	}
    38  
    39  	var params pbeParams
    40  	rest, err = asn1.Unmarshal(pkinfo.Algorithm().Parameters.FullBytes, &params)
    41  	if err != nil {
    42  		t.Fatalf("failed to unmarshal encryptedPrivateKeyInfo %s", err)
    43  	}
    44  
    45  	if len(rest) != 0 {
    46  		t.Fatalf("unexpected trailing bytes of len=%d, bytes=%x", len(rest), rest)
    47  	}
    48  
    49  	if params.Iterations != pbeIterationCount {
    50  		t.Errorf("expected iteration count to be %d, but actual=%d", pbeIterationCount, params.Iterations)
    51  	}
    52  	if len(params.Salt) != pbeSaltSizeBytes {
    53  		t.Errorf("expected the number of salt bytes to be %d, but actual=%d", pbeSaltSizeBytes, len(params.Salt))
    54  	}
    55  }
    56  
    57  func TestRoundTripPkcs8ShroudedKeyBag(t *testing.T) {
    58  	privateKey, err := rsa.GenerateKey(rand.Reader, 512)
    59  	if err != nil {
    60  		t.Fatalf("failed to generate a private key: %s", err)
    61  	}
    62  
    63  	password := []byte("sesame")
    64  	bytes, err := encodePkcs8ShroudedKeyBag(privateKey, password)
    65  	if err != nil {
    66  		t.Fatalf("failed to encode PKCS#8 shrouded key bag: %s", err)
    67  	}
    68  
    69  	key, err := decodePkcs8ShroudedKeyBag(bytes, password)
    70  	if err != nil {
    71  		t.Fatalf("failed to decode PKCS#8 shrouded key bag: %s", err)
    72  	}
    73  
    74  	actualPrivateKey := key.(*rsa.PrivateKey)
    75  	if actualPrivateKey.D.Cmp(privateKey.D) != 0 {
    76  		t.Fatalf("failed to round-trip rsa.PrivateKey.D")
    77  	}
    78  }