| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package signaturetest |
| 15 |
|
| 16 |
import ( |
| 17 |
"crypto/elliptic" |
| 18 |
"crypto/x509" |
| 19 |
"fmt" |
| 20 |
|
| 21 |
"github.com/notaryproject/notation-core-go/signature" |
| 22 |
"github.com/notaryproject/notation-core-go/testhelper" |
| 23 |
) |
| 24 |
|
| 25 |
// GetTestLocalSigner returns the local signer with given keyType and size for testing |
| 26 |
func GetTestLocalSigner(keyType signature.KeyType, size int) (signature.Signer, error) { |
| 27 |
switch keyType { |
| 28 |
case signature.KeyTypeEC: |
| 29 |
switch size { |
| 30 |
case 256: |
| 31 |
leafCertTuple := testhelper.GetECCertTuple(elliptic.P256()) |
| 32 |
certs := []*x509.Certificate{leafCertTuple.Cert, testhelper.GetECRootCertificate().Cert} |
| 33 |
return signature.NewLocalSigner(certs, leafCertTuple.PrivateKey) |
| 34 |
case 384: |
| 35 |
leafCertTuple := testhelper.GetECCertTuple(elliptic.P384()) |
| 36 |
certs := []*x509.Certificate{leafCertTuple.Cert, testhelper.GetECRootCertificate().Cert} |
| 37 |
return signature.NewLocalSigner(certs, leafCertTuple.PrivateKey) |
| 38 |
case 521: |
| 39 |
leafCertTuple := testhelper.GetECCertTuple(elliptic.P521()) |
| 40 |
certs := []*x509.Certificate{leafCertTuple.Cert, testhelper.GetECRootCertificate().Cert} |
| 41 |
return signature.NewLocalSigner(certs, leafCertTuple.PrivateKey) |
| 42 |
default: |
| 43 |
return nil, fmt.Errorf("key size not supported") |
| 44 |
} |
| 45 |
case signature.KeyTypeRSA: |
| 46 |
switch size { |
| 47 |
case 2048: |
| 48 |
leafCertTuple := testhelper.GetRSACertTuple(2048) |
| 49 |
certs := []*x509.Certificate{leafCertTuple.Cert, testhelper.GetRSARootCertificate().Cert} |
| 50 |
return signature.NewLocalSigner(certs, leafCertTuple.PrivateKey) |
| 51 |
case 3072: |
| 52 |
leafCertTuple := testhelper.GetRSACertTuple(3072) |
| 53 |
certs := []*x509.Certificate{leafCertTuple.Cert, testhelper.GetRSARootCertificate().Cert} |
| 54 |
return signature.NewLocalSigner(certs, leafCertTuple.PrivateKey) |
| 55 |
case 4096: |
| 56 |
leafCertTuple := testhelper.GetRSACertTuple(4096) |
| 57 |
certs := []*x509.Certificate{leafCertTuple.Cert, testhelper.GetRSARootCertificate().Cert} |
| 58 |
return signature.NewLocalSigner(certs, leafCertTuple.PrivateKey) |
| 59 |
default: |
| 60 |
return nil, fmt.Errorf("key size not supported") |
| 61 |
} |
| 62 |
default: |
| 63 |
return nil, fmt.Errorf("keyType not supported") |
| 64 |
} |
| 65 |
} |