github.com/psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/crypto/ssh/testdata_test.go (about)

     1  // Copyright 2014 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  // IMPLEMENTATION NOTE: To avoid a package loop, this file is in three places:
     6  // ssh/, ssh/agent, and ssh/test/. It should be kept in sync across all three
     7  // instances.
     8  
     9  package ssh
    10  
    11  import (
    12  	"crypto/rand"
    13  	"fmt"
    14  
    15  	"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh/testdata"
    16  )
    17  
    18  var (
    19  	testPrivateKeys map[string]interface{}
    20  	testSigners     map[string]Signer
    21  	testPublicKeys  map[string]PublicKey
    22  )
    23  
    24  func init() {
    25  	var err error
    26  
    27  	n := len(testdata.PEMBytes)
    28  	testPrivateKeys = make(map[string]interface{}, n)
    29  	testSigners = make(map[string]Signer, n)
    30  	testPublicKeys = make(map[string]PublicKey, n)
    31  	for t, k := range testdata.PEMBytes {
    32  		testPrivateKeys[t], err = ParseRawPrivateKey(k)
    33  		if err != nil {
    34  			panic(fmt.Sprintf("Unable to parse test key %s: %v", t, err))
    35  		}
    36  		testSigners[t], err = NewSignerFromKey(testPrivateKeys[t])
    37  		if v, ok := testSigners[t].(*rsaSigner); ok {
    38  			switch t {
    39  			case "rsa-sha2-256":
    40  				testSigners[t] = &rsaSigner{v, SigAlgoRSASHA2256}
    41  			case "rsa-sha2-512":
    42  				testSigners[t] = &rsaSigner{v, SigAlgoRSASHA2512}
    43  			}
    44  		}
    45  		if err != nil {
    46  			panic(fmt.Sprintf("Unable to create signer for test key %s: %v", t, err))
    47  		}
    48  		testPublicKeys[t] = testSigners[t].PublicKey()
    49  	}
    50  
    51  	// Create a cert and sign it for use in tests.
    52  	testCert := &Certificate{
    53  		Nonce:           []byte{},                       // To pass reflect.DeepEqual after marshal & parse, this must be non-nil
    54  		ValidPrincipals: []string{"gopher1", "gopher2"}, // increases test coverage
    55  		ValidAfter:      0,                              // unix epoch
    56  		ValidBefore:     CertTimeInfinity,               // The end of currently representable time.
    57  		Reserved:        []byte{},                       // To pass reflect.DeepEqual after marshal & parse, this must be non-nil
    58  		Key:             testPublicKeys["ecdsa"],
    59  		SignatureKey:    testPublicKeys["rsa"],
    60  		Permissions: Permissions{
    61  			CriticalOptions: map[string]string{},
    62  			Extensions:      map[string]string{},
    63  		},
    64  	}
    65  	testCert.SignCert(rand.Reader, testSigners["rsa"])
    66  	testPrivateKeys["cert"] = testPrivateKeys["ecdsa"]
    67  	testSigners["cert"], err = NewCertSigner(testCert, testSigners["ecdsa"])
    68  	if err != nil {
    69  		panic(fmt.Sprintf("Unable to create certificate signer: %v", err))
    70  	}
    71  }