-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
239 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/go-ocf/sdk/local" | ||
) | ||
|
||
type testSetupSecureClient struct { | ||
ca []*x509.Certificate | ||
mfgCA []*x509.Certificate | ||
mfgCert tls.Certificate | ||
} | ||
|
||
func (c *testSetupSecureClient) GetManufacturerCertificate() (tls.Certificate, error) { | ||
if c.mfgCert.PrivateKey == nil { | ||
return c.mfgCert, fmt.Errorf("not set") | ||
} | ||
return c.mfgCert, nil | ||
} | ||
|
||
func (c *testSetupSecureClient) GetManufacturerCertificateAuthorities() ([]*x509.Certificate, error) { | ||
if len(c.mfgCA) == 0 { | ||
return nil, fmt.Errorf("not set") | ||
} | ||
return c.mfgCA, nil | ||
} | ||
|
||
func (c *testSetupSecureClient) GetRootCertificateAuthorities() ([]*x509.Certificate, error) { | ||
if len(c.ca) == 0 { | ||
return nil, fmt.Errorf("not set") | ||
} | ||
return c.ca, nil | ||
} | ||
|
||
func NewSDKClient() (*local.Client, error) { | ||
mfgTrustedCABlock, _ := pem.Decode(MfgTrustedCA) | ||
if mfgTrustedCABlock == nil { | ||
return nil, fmt.Errorf("mfgTrustedCABlock is empty") | ||
} | ||
mfgCA, err := x509.ParseCertificates(mfgTrustedCABlock.Bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
identityIntermediateCA, err := ioutil.ReadFile(os.Getenv("TEST_ROOT_CA_CRT")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
identityIntermediateCAKey, err := ioutil.ReadFile(os.Getenv("TEST_ROOT_CA_KEY")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
identityTrustedCA, err := ioutil.ReadFile(os.Getenv("TEST_ROOT_CA_CRT")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
identityIntermediateCABlock, _ := pem.Decode(identityIntermediateCA) | ||
if identityIntermediateCABlock == nil { | ||
return nil, fmt.Errorf("identityIntermediateCABlock is empty") | ||
} | ||
identityIntermediateCAKeyBlock, _ := pem.Decode(identityIntermediateCAKey) | ||
if identityIntermediateCAKeyBlock == nil { | ||
return nil, fmt.Errorf("identityIntermediateCAKeyBlock is empty") | ||
} | ||
|
||
identityTrustedCABlock, _ := pem.Decode(identityTrustedCA) | ||
if identityTrustedCABlock == nil { | ||
return nil, fmt.Errorf("identityTrustedCABlock is empty") | ||
} | ||
identityTrustedCACert, err := x509.ParseCertificates(identityTrustedCABlock.Bytes) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot parse cert: %w", err) | ||
} | ||
mfgCert, err := tls.X509KeyPair(MfgCert, MfgKey) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot X509KeyPair: %w", err) | ||
} | ||
cfg := local.Config{ | ||
DisablePeerTCPSignalMessageCSMs: true, | ||
DeviceOwnershipSDK: &local.DeviceOwnershipSDKConfig{ | ||
ID: CertIdentity, | ||
Cert: identityIntermediateCA, | ||
CertKey: identityIntermediateCAKey, | ||
}, | ||
} | ||
|
||
client, err := local.NewClientFromConfig(&cfg, &testSetupSecureClient{ | ||
mfgCA: mfgCA, | ||
mfgCert: mfgCert, | ||
ca: append(identityTrustedCACert), | ||
}, func(err error) { fmt.Print(err) }, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = client.Initialization(context.Background()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
var ( | ||
CertIdentity = "00000000-0000-0000-0000-000000000001" | ||
|
||
MfgCert = []byte(`-----BEGIN CERTIFICATE----- | ||
MIIB9zCCAZygAwIBAgIRAOwIWPAt19w7DswoszkVIEIwCgYIKoZIzj0EAwIwEzER | ||
MA8GA1UEChMIVGVzdCBPUkcwHhcNMTkwNTAyMjAwNjQ4WhcNMjkwMzEwMjAwNjQ4 | ||
WjBHMREwDwYDVQQKEwhUZXN0IE9SRzEyMDAGA1UEAxMpdXVpZDpiNWEyYTQyZS1i | ||
Mjg1LTQyZjEtYTM2Yi0wMzRjOGZjOGVmZDUwWTATBgcqhkjOPQIBBggqhkjOPQMB | ||
BwNCAAQS4eiM0HNPROaiAknAOW08mpCKDQmpMUkywdcNKoJv1qnEedBhWne7Z0jq | ||
zSYQbyqyIVGujnI3K7C63NRbQOXQo4GcMIGZMA4GA1UdDwEB/wQEAwIDiDAzBgNV | ||
HSUELDAqBggrBgEFBQcDAQYIKwYBBQUHAwIGCCsGAQUFBwMBBgorBgEEAYLefAEG | ||
MAwGA1UdEwEB/wQCMAAwRAYDVR0RBD0wO4IJbG9jYWxob3N0hwQAAAAAhwR/AAAB | ||
hxAAAAAAAAAAAAAAAAAAAAAAhxAAAAAAAAAAAAAAAAAAAAABMAoGCCqGSM49BAMC | ||
A0kAMEYCIQDuhl6zj6gl2YZbBzh7Th0uu5izdISuU/ESG+vHrEp7xwIhANCA7tSt | ||
aBlce+W76mTIhwMFXQfyF3awWIGjOcfTV8pU | ||
-----END CERTIFICATE----- | ||
`) | ||
|
||
MfgKey = []byte(`-----BEGIN EC PRIVATE KEY----- | ||
MHcCAQEEIMPeADszZajrkEy4YvACwcbR0pSdlKG+m8ALJ6lj/ykdoAoGCCqGSM49 | ||
AwEHoUQDQgAEEuHojNBzT0TmogJJwDltPJqQig0JqTFJMsHXDSqCb9apxHnQYVp3 | ||
u2dI6s0mEG8qsiFRro5yNyuwutzUW0Dl0A== | ||
-----END EC PRIVATE KEY----- | ||
`) | ||
|
||
MfgTrustedCA = []byte(`-----BEGIN CERTIFICATE----- | ||
MIIBaTCCAQ+gAwIBAgIQR33gIB75I7Vi/QnMnmiWvzAKBggqhkjOPQQDAjATMREw | ||
DwYDVQQKEwhUZXN0IE9SRzAeFw0xOTA1MDIyMDA1MTVaFw0yOTAzMTAyMDA1MTVa | ||
MBMxETAPBgNVBAoTCFRlc3QgT1JHMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE | ||
xbwMaS8jcuibSYJkCmuVHfeV3xfYVyUq8Iroz7YlXaTayspW3K4hVdwIsy/5U+3U | ||
vM/vdK5wn2+NrWy45vFAJqNFMEMwDgYDVR0PAQH/BAQDAgEGMBMGA1UdJQQMMAoG | ||
CCsGAQUFBwMBMA8GA1UdEwEB/wQFMAMBAf8wCwYDVR0RBAQwAoIAMAoGCCqGSM49 | ||
BAMCA0gAMEUCIBWkxuHKgLSp6OXDJoztPP7/P5VBZiwLbfjTCVRxBvwWAiEAnzNu | ||
6gKPwtKmY0pBxwCo3NNmzNpA6KrEOXE56PkiQYQ= | ||
-----END CERTIFICATE----- | ||
`) | ||
MfgTrustedCAKey = []byte(`-----BEGIN EC PRIVATE KEY----- | ||
MHcCAQEEICzfC16AqtSv3wt+qIbrgM8dTqBhHANJhZS5xCpH6P2roAoGCCqGSM49 | ||
AwEHoUQDQgAExbwMaS8jcuibSYJkCmuVHfeV3xfYVyUq8Iroz7YlXaTayspW3K4h | ||
VdwIsy/5U+3UvM/vdK5wn2+NrWy45vFAJg== | ||
-----END EC PRIVATE KEY----- | ||
`) | ||
) |
Oops, something went wrong.