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

     1  // Copyright 2015 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  
     5  package pkcs12
     6  
     7  import (
     8  	"crypto/ecdsa"
     9  	"crypto/elliptic"
    10  	"crypto/rand"
    11  	"crypto/rsa"
    12  	"crypto/tls"
    13  	"crypto/x509"
    14  	"crypto/x509/pkix"
    15  	"encoding/base64"
    16  	"encoding/pem"
    17  	"fmt"
    18  	"math/big"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  func TestPfx(t *testing.T) {
    24  	for commonName, base64P12 := range testdata {
    25  		p12, _ := base64.StdEncoding.DecodeString(base64P12)
    26  
    27  		priv, cert, err := Decode(p12, "")
    28  		if err != nil {
    29  			t.Fatal(err)
    30  		}
    31  
    32  		if err := priv.(*rsa.PrivateKey).Validate(); err != nil {
    33  			t.Errorf("error while validating private key: %v", err)
    34  		}
    35  
    36  		if cert.Subject.CommonName != commonName {
    37  			t.Errorf("expected common name to be %q, but found %q", commonName, cert.Subject.CommonName)
    38  		}
    39  	}
    40  }
    41  
    42  func TestPfxRoundTriRsa(t *testing.T) {
    43  	privateKey, err := rsa.GenerateKey(rand.Reader, 512)
    44  	if err != nil {
    45  		t.Fatal(err.Error())
    46  	}
    47  
    48  	key := testPfxRoundTrip(t, privateKey)
    49  
    50  	actualPrivateKey, ok := key.(*rsa.PrivateKey)
    51  	if !ok {
    52  		t.Fatalf("failed to decode private key")
    53  	}
    54  
    55  	if privateKey.D.Cmp(actualPrivateKey.D) != 0 {
    56  		t.Errorf("priv.D")
    57  	}
    58  }
    59  
    60  func TestPfxRoundTriEcdsa(t *testing.T) {
    61  	privateKey, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
    62  	if err != nil {
    63  		t.Fatal(err.Error())
    64  	}
    65  
    66  	key := testPfxRoundTrip(t, privateKey)
    67  
    68  	actualPrivateKey, ok := key.(*ecdsa.PrivateKey)
    69  	if !ok {
    70  		t.Fatalf("failed to decode private key")
    71  	}
    72  
    73  	if privateKey.D.Cmp(actualPrivateKey.D) != 0 {
    74  		t.Errorf("priv.D")
    75  	}
    76  }
    77  
    78  func testPfxRoundTrip(t *testing.T, privateKey interface{}) interface{} {
    79  	certificateBytes, err := newCertificate("hostname", privateKey)
    80  	if err != nil {
    81  		t.Fatal(err.Error())
    82  	}
    83  
    84  	bytes, err := Encode(certificateBytes, privateKey, "sesame")
    85  	if err != nil {
    86  		t.Fatal(err.Error())
    87  	}
    88  
    89  	key, _, err := Decode(bytes, "sesame")
    90  	if err != nil {
    91  		t.Fatalf(err.Error())
    92  	}
    93  
    94  	return key
    95  }
    96  
    97  func TestPEM(t *testing.T) {
    98  	for commonName, base64P12 := range testdata {
    99  		p12, _ := base64.StdEncoding.DecodeString(base64P12)
   100  
   101  		blocks, err := ToPEM(p12, "")
   102  		if err != nil {
   103  			t.Fatalf("error while converting to PEM: %s", err)
   104  		}
   105  
   106  		var pemData []byte
   107  		for _, b := range blocks {
   108  			pemData = append(pemData, pem.EncodeToMemory(b)...)
   109  		}
   110  
   111  		cert, err := tls.X509KeyPair(pemData, pemData)
   112  		if err != nil {
   113  			t.Errorf("err while converting to key pair: %v", err)
   114  		}
   115  		config := tls.Config{
   116  			Certificates: []tls.Certificate{cert},
   117  		}
   118  		config.BuildNameToCertificate()
   119  
   120  		if _, exists := config.NameToCertificate[commonName]; !exists {
   121  			t.Errorf("did not find our cert in PEM?: %v", config.NameToCertificate)
   122  		}
   123  	}
   124  }
   125  
   126  func ExampleToPEM() {
   127  	p12, _ := base64.StdEncoding.DecodeString(`MIIJzgIBAzCCCZQGCS ... CA+gwggPk==`)
   128  
   129  	blocks, err := ToPEM(p12, "password")
   130  	if err != nil {
   131  		panic(err)
   132  	}
   133  
   134  	var pemData []byte
   135  	for _, b := range blocks {
   136  		pemData = append(pemData, pem.EncodeToMemory(b)...)
   137  	}
   138  
   139  	// then use PEM data for tls to construct tls certificate:
   140  	cert, err := tls.X509KeyPair(pemData, pemData)
   141  	if err != nil {
   142  		panic(err)
   143  	}
   144  
   145  	config := &tls.Config{
   146  		Certificates: []tls.Certificate{cert},
   147  	}
   148  
   149  	_ = config
   150  }
   151  
   152  func newCertificate(hostname string, privateKey interface{}) ([]byte, error) {
   153  	t, _ := time.Parse("2006-01-02", "2016-01-01")
   154  	notBefore := t
   155  	notAfter := notBefore.Add(365 * 24 * time.Hour)
   156  
   157  	serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
   158  	if err != nil {
   159  		err := fmt.Errorf("Failed to Generate Serial Number: %v", err)
   160  		return nil, err
   161  	}
   162  
   163  	template := x509.Certificate{
   164  		SerialNumber: serialNumber,
   165  		Issuer: pkix.Name{
   166  			CommonName: hostname,
   167  		},
   168  		Subject: pkix.Name{
   169  			CommonName: hostname,
   170  		},
   171  
   172  		NotBefore: notBefore,
   173  		NotAfter:  notAfter,
   174  
   175  		KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
   176  		ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
   177  		BasicConstraintsValid: true,
   178  	}
   179  
   180  	var publicKey interface{}
   181  	switch key := privateKey.(type) {
   182  	case *rsa.PrivateKey:
   183  		publicKey = key.Public()
   184  	case *ecdsa.PrivateKey:
   185  		publicKey = key.Public()
   186  	default:
   187  		panic(fmt.Sprintf("unsupported private key type: %T", privateKey))
   188  	}
   189  
   190  	derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey, privateKey)
   191  	if err != nil {
   192  		return nil, fmt.Errorf("Failed to Generate derBytes: " + err.Error())
   193  	}
   194  
   195  	return derBytes, nil
   196  }
   197  
   198  var testdata = map[string]string{
   199  	// 'null' password test case
   200  	"Windows Azure Tools": `MIIKDAIBAzCCCcwGCSqGSIb3DQEHAaCCCb0Eggm5MIIJtTCCBe4GCSqGSIb3DQEHAaCCBd8E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rpxbT0aMjdFderlndKRIyG23mSp0HaUwNzAfMAcGBSsOAwIaBBRlviCbIyRrhIysg2dc/KbLFTc2vQQUg4rfwHMM4IKYRD/fsd1x6dda+wQ=`,
   201  	// empty string password test case
   202  	"testing@example.com": `MIIJzgIBAzCCCZQGCSqGSIb3DQEHAaCCCYUEggmBMIIJfTCCA/cGCSqGSIb3DQEHBqCCA+gwggPk
   203  AgEAMIID3QYJKoZIhvcNAQcBMBwGCiqGSIb3DQEMAQYwDgQIIszfRGqcmPcCAggAgIIDsOZ9Eg1L
   204  s5Wx8JhYoV3HAL4aRnkAWvTYB5NISZOgSgIQTssmt/3A7134dibTmaT/93LikkL3cTKLnQzJ4wDf
   205  YZ1bprpVJvUqz+HFT79m27bP9zYXFrvxWBJbxjYKTSjQMgz+h8LAEpXXGajCmxMJ1oCOtdXkhhzc
   206  LdZN6SAYgtmtyFnCdMEDskSggGuLb3fw84QEJ/Sj6FAULXunW/CPaS7Ce0TMsKmNU/jfFWj3yXXw
   207  ro0kwjKiVLpVFlnBlHo2OoVU7hmkm59YpGhLgS7nxLD3n7nBroQ0ID1+8R01NnV9XLGoGzxMm1te
   208  6UyTCkr5mj+kEQ8EP1Ys7g/TC411uhVWySMt/rcpkx7Vz1r9kYEAzJpONAfr6cuEVkPKrxpq4Fh0
   209  2fzlKBky0i/hrfIEUmngh+ERHUb/Mtv/fkv1j5w9suESbhsMLLiCXAlsP1UWMX+3bNizi3WVMEts
   210  FM2k9byn+p8IUD/A8ULlE4kEaWeoc+2idkCNQkLGuIdGUXUFVm58se0auUkVRoRJx8x4CkMesT8j
   211  b1H831W66YRWoEwwDQp2kK1lA2vQXxdVHWlFevMNxJeromLzj3ayiaFrfByeUXhR2S+Hpm+c0yNR
   212  4UVU9WED2kacsZcpRm9nlEa5sr28mri5JdBrNa/K02OOhvKCxr5ZGmbOVzUQKla2z4w+Ku9k8POm
   213  dfDNU/fGx1b5hcFWtghXe3msWVsSJrQihnN6q1ughzNiYZlJUGcHdZDRtiWwCFI0bR8h/Dmg9uO9
   214  4rawQQrjIRT7B8yF3UbkZyAqs8Ppb1TsMeNPHh1rxEfGVQknh/48ouJYsmtbnzugTUt3mJCXXiL+
   215  XcPMV6bBVAUu4aaVKSmg9+yJtY4/VKv10iw88ktv29fViIdBe3t6l/oPuvQgbQ8dqf4T8w0l/uKZ
   216  9lS1Na9jfT1vCoS7F5TRi+tmyj1vL5kr/amEIW6xKEP6oeAMvCMtbPAzVEj38zdJ1R22FfuIBxkh
   217  f0Zl7pdVbmzRxl/SBx9iIBJSqAvcXItiT0FIj8HxQ+0iZKqMQMiBuNWJf5pYOLWGrIyntCWwHuaQ
   218  wrx0sTGuEL9YXLEAsBDrsvzLkx/56E4INGZFrH8G7HBdW6iGqb22IMI4GHltYSyBRKbB0gadYTyv
   219  abPEoqww8o7/85aPSzOTJ/53ozD438Q+d0u9SyDuOb60SzCD/zPuCEd78YgtXJwBYTuUNRT27FaM
   220  3LGMX8Hz+6yPNRnmnA2XKPn7dx/IlaqAjIs8MIIFfgYJKoZIhvcNAQcBoIIFbwSCBWswggVnMIIF
   221  YwYLKoZIhvcNAQwKAQKgggTuMIIE6jAcBgoqhkiG9w0BDAEDMA4ECJr0cClYqOlcAgIIAASCBMhe
   222  OQSiP2s0/46ONXcNeVAkz2ksW3u/+qorhSiskGZ0b3dFa1hhgBU2Q7JVIkc4Hf7OXaT1eVQ8oqND
   223  uhqsNz83/kqYo70+LS8Hocj49jFgWAKrf/yQkdyP1daHa2yzlEw4mkpqOfnIORQHvYCa8nEApspZ
   224  wVu8y6WVuLHKU67mel7db2xwstQp7PRuSAYqGjTfAylElog8ASdaqqYbYIrCXucF8iF9oVgmb/Qo
   225  xrXshJ9aSLO4MuXlTPELmWgj07AXKSb90FKNihE+y0bWb9LPVFY1Sly3AX9PfrtkSXIZwqW3phpv
   226  MxGxQl/R6mr1z+hlTfY9Wdpb5vlKXPKA0L0Rt8d2pOesylFi6esJoS01QgP1kJILjbrV731kvDc0
   227  Jsd+Oxv4BMwA7ClG8w1EAOInc/GrV1MWFGw/HeEqj3CZ/l/0jv9bwkbVeVCiIhoL6P6lVx9pXq4t
   228  KZ0uKg/tk5TVJmG2vLcMLvezD0Yk3G2ZOMrywtmskrwoF7oAUpO9e87szoH6fEvUZlkDkPVW1NV4
   229  cZk3DBSQiuA3VOOg8qbo/tx/EE3H59P0axZWno2GSB0wFPWd1aj+b//tJEJHaaNR6qPRj4IWj9ru
   230  Qbc8eRAcVWleHg8uAehSvUXlFpyMQREyrnpvMGddpiTC8N4UMrrBRhV7+UbCOWhxPCbItnInBqgl
   231  1JpSZIP7iUtsIMdu3fEC2cdbXMTRul+4rdzUR7F9OaezV3jjvcAbDvgbK1CpyC+MJ1Mxm/iTgk9V
   232  iUArydhlR8OniN84GyGYoYCW9O/KUwb6ASmeFOu/msx8x6kAsSQHIkKqMKv0TUR3kZnkxUvdpBGP
   233  KTl4YCTvNGX4dYALBqrAETRDhua2KVBD/kEttDHwBNVbN2xi81+Mc7ml461aADfk0c66R/m2sjHB
   234  2tN9+wG12OIWFQjL6wF/UfJMYamxx2zOOExiId29Opt57uYiNVLOO4ourPewHPeH0u8Gz35aero7
   235  lkt7cZAe1Q0038JUuE/QGlnK4lESK9UkSIQAjSaAlTsrcfwtQxB2EjoOoLhwH5mvxUEmcNGNnXUc
   236  9xj3M5BD3zBz3Ft7G3YMMDwB1+zC2l+0UG0MGVjMVaeoy32VVNvxgX7jk22OXG1iaOB+PY9kdk+O
   237  X+52BGSf/rD6X0EnqY7XuRPkMGgjtpZeAYxRQnFtCZgDY4wYheuxqSSpdF49yNczSPLkgB3CeCfS
   238  +9NTKN7aC6hBbmW/8yYh6OvSiCEwY0lFS/T+7iaVxr1loE4zI1y/FFp4Pe1qfLlLttVlkygga2UU
   239  SCunTQ8UB/M5IXWKkhMOO11dP4niWwb39Y7pCWpau7mwbXOKfRPX96cgHnQJK5uG+BesDD1oYnX0
   240  6frN7FOnTSHKruRIwuI8KnOQ/I+owmyz71wiv5LMQt+yM47UrEjB/EZa5X8dpEwOZvkdqL7utcyo
   241  l0XH5kWMXdW856LL/FYftAqJIDAmtX1TXF/rbP6mPyN/IlDC0gjP84Uzd/a2UyTIWr+wk49Ek3vQ
   242  /uDamq6QrwAxVmNh5Tset5Vhpc1e1kb7mRMZIzxSP8JcTuYd45oFKi98I8YjvueHVZce1g7OudQP
   243  SbFQoJvdT46iBg1TTatlltpOiH2mFaxWVS0xYjAjBgkqhkiG9w0BCRUxFgQUdA9eVqvETX4an/c8
   244  p8SsTugkit8wOwYJKoZIhvcNAQkUMS4eLABGAHIAaQBlAG4AZABsAHkAIABuAGEAbQBlACAAZgBv
   245  AHIAIABjAGUAcgB0MDEwITAJBgUrDgMCGgUABBRFsNz3Zd1O1GI8GTuFwCWuDOjEEwQIuBEfIcAy
   246  HQ8CAggA`,
   247  }