|
| 1 | +package signature |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + |
| 6 | + "github.com/notaryproject/notation-core-go/signature" |
| 7 | +) |
| 8 | + |
| 9 | +// one of the following supported key spec names. |
| 10 | +// |
| 11 | +// https://github.com/notaryproject/notaryproject/blob/main/signature-specification.md#algorithm-selection |
| 12 | +const ( |
| 13 | + RSA_2048 = "RSA-2048" |
| 14 | + RSA_3072 = "RSA-3072" |
| 15 | + RSA_4096 = "RSA-4096" |
| 16 | + EC_256 = "EC-256" |
| 17 | + EC_384 = "EC-384" |
| 18 | + EC_521 = "EC-521" |
| 19 | +) |
| 20 | + |
| 21 | +// one of the following supported hash algorithm names. |
| 22 | +// |
| 23 | +// https://github.com/notaryproject/notaryproject/blob/main/signature-specification.md#algorithm-selection |
| 24 | +const ( |
| 25 | + SHA_256 = "SHA-256" |
| 26 | + SHA_384 = "SHA-384" |
| 27 | + SHA_512 = "SHA-512" |
| 28 | +) |
| 29 | + |
| 30 | +// one of the following supported signing algorithm names. |
| 31 | +// |
| 32 | +// https://github.com/notaryproject/notaryproject/blob/main/signature-specification.md#algorithm-selection |
| 33 | +const ( |
| 34 | + ECDSA_SHA_256 = "ECDSA-SHA-256" |
| 35 | + ECDSA_SHA_384 = "ECDSA-SHA-384" |
| 36 | + ECDSA_SHA_512 = "ECDSA-SHA-512" |
| 37 | + RSASSA_PSS_SHA_256 = "RSASSA-PSS-SHA-256" |
| 38 | + RSASSA_PSS_SHA_384 = "RSASSA-PSS-SHA-384" |
| 39 | + RSASSA_PSS_SHA_512 = "RSASSA-PSS-SHA-512" |
| 40 | +) |
| 41 | + |
| 42 | +// InvalidKeySpec is the invalid value of keySpec. |
| 43 | +var InvalidKeySpec = signature.KeySpec{} |
| 44 | + |
| 45 | +// KeySpecName returns the name of a keySpec according to the spec. |
| 46 | +func KeySpecName(k signature.KeySpec) string { |
| 47 | + switch k.Type { |
| 48 | + case signature.KeyTypeEC: |
| 49 | + switch k.Size { |
| 50 | + case 256: |
| 51 | + return EC_256 |
| 52 | + case 384: |
| 53 | + return EC_384 |
| 54 | + case 521: |
| 55 | + return EC_521 |
| 56 | + } |
| 57 | + case signature.KeyTypeRSA: |
| 58 | + switch k.Size { |
| 59 | + case 2048: |
| 60 | + return RSA_2048 |
| 61 | + case 3072: |
| 62 | + return RSA_3072 |
| 63 | + case 4096: |
| 64 | + return RSA_4096 |
| 65 | + } |
| 66 | + } |
| 67 | + return "" |
| 68 | +} |
| 69 | + |
| 70 | +// KeySpecHashName returns the name of hash function according to the spec. |
| 71 | +func KeySpecHashName(k signature.KeySpec) string { |
| 72 | + switch k.Type { |
| 73 | + case signature.KeyTypeEC: |
| 74 | + switch k.Size { |
| 75 | + case 256: |
| 76 | + return SHA_256 |
| 77 | + case 384: |
| 78 | + return SHA_384 |
| 79 | + case 521: |
| 80 | + return SHA_512 |
| 81 | + } |
| 82 | + case signature.KeyTypeRSA: |
| 83 | + switch k.Size { |
| 84 | + case 2048: |
| 85 | + return SHA_256 |
| 86 | + case 3072: |
| 87 | + return SHA_384 |
| 88 | + case 4096: |
| 89 | + return SHA_512 |
| 90 | + } |
| 91 | + } |
| 92 | + return "" |
| 93 | +} |
| 94 | + |
| 95 | +// ParseKeySpecFromName parses keySpec name to a signature.keySpec type. |
| 96 | +func ParseKeySpecFromName(raw string) (keySpec signature.KeySpec, err error) { |
| 97 | + switch raw { |
| 98 | + case RSA_2048: |
| 99 | + keySpec.Size = 2048 |
| 100 | + keySpec.Type = signature.KeyTypeRSA |
| 101 | + case RSA_3072: |
| 102 | + keySpec.Size = 3072 |
| 103 | + keySpec.Type = signature.KeyTypeRSA |
| 104 | + case RSA_4096: |
| 105 | + keySpec.Size = 4096 |
| 106 | + keySpec.Type = signature.KeyTypeRSA |
| 107 | + case EC_256: |
| 108 | + keySpec.Size = 256 |
| 109 | + keySpec.Type = signature.KeyTypeEC |
| 110 | + case EC_384: |
| 111 | + keySpec.Size = 384 |
| 112 | + keySpec.Type = signature.KeyTypeEC |
| 113 | + case EC_521: |
| 114 | + keySpec.Size = 521 |
| 115 | + keySpec.Type = signature.KeyTypeEC |
| 116 | + default: |
| 117 | + keySpec = InvalidKeySpec |
| 118 | + err = errors.New("parse KeySpec error, keySpec not supported") |
| 119 | + } |
| 120 | + return |
| 121 | +} |
| 122 | + |
| 123 | +// SigningAlgorithmName returns the signing algorithm name of an algorithm according to the spec. |
| 124 | +func SigningAlgorithmName(alg signature.Algorithm) string { |
| 125 | + switch alg { |
| 126 | + case signature.AlgorithmES256: |
| 127 | + return ECDSA_SHA_256 |
| 128 | + case signature.AlgorithmES384: |
| 129 | + return ECDSA_SHA_384 |
| 130 | + case signature.AlgorithmES512: |
| 131 | + return ECDSA_SHA_512 |
| 132 | + case signature.AlgorithmPS256: |
| 133 | + return RSASSA_PSS_SHA_256 |
| 134 | + case signature.AlgorithmPS384: |
| 135 | + return RSASSA_PSS_SHA_384 |
| 136 | + case signature.AlgorithmPS512: |
| 137 | + return RSASSA_PSS_SHA_512 |
| 138 | + } |
| 139 | + return "" |
| 140 | +} |
| 141 | + |
| 142 | +// ParseSigningAlgorithFromName parses the signing algorithm name from a given string. |
| 143 | +func ParseSigningAlgorithFromName(raw string) (signature.Algorithm, error) { |
| 144 | + switch raw { |
| 145 | + case ECDSA_SHA_256: |
| 146 | + return signature.AlgorithmES256, nil |
| 147 | + case ECDSA_SHA_384: |
| 148 | + return signature.AlgorithmES384, nil |
| 149 | + case ECDSA_SHA_512: |
| 150 | + return signature.AlgorithmES512, nil |
| 151 | + case RSASSA_PSS_SHA_256: |
| 152 | + return signature.AlgorithmPS256, nil |
| 153 | + case RSASSA_PSS_SHA_384: |
| 154 | + return signature.AlgorithmPS384, nil |
| 155 | + case RSASSA_PSS_SHA_512: |
| 156 | + return signature.AlgorithmPS512, nil |
| 157 | + } |
| 158 | + return 0, errors.New("parse Signing algorithm error, signing algorithm not supported") |
| 159 | +} |
0 commit comments