1// Copyright 2022 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14// 15//////////////////////////////////////////////////////////////////////////////// 16 17package signature 18 19import ( 20 "crypto/rsa" 21 "errors" 22 "fmt" 23 24 "google.golang.org/protobuf/proto" 25 "github.com/google/tink/go/core/registry" 26 internal "github.com/google/tink/go/internal/signature" 27 "github.com/google/tink/go/keyset" 28 rsassapkcs1pb "github.com/google/tink/go/proto/rsa_ssa_pkcs1_go_proto" 29 tinkpb "github.com/google/tink/go/proto/tink_go_proto" 30) 31 32const ( 33 rsaSSAPKCS1VerifierKeyVersion = 0 34 rsaSSAPKCS1VerifierTypeURL = "type.googleapis.com/google.crypto.tink.RsaSsaPkcs1PublicKey" 35) 36 37var ( 38 errRSASSAPKCS1NotImplemented = errors.New("rsassapkcs1_verifier_key_manager: not implemented") 39) 40 41type rsaSSAPKCS1VerifierKeyManager struct{} 42 43var _ registry.KeyManager = (*rsaSSAPKCS1VerifierKeyManager)(nil) 44 45func (km *rsaSSAPKCS1VerifierKeyManager) Primitive(serializedKey []byte) (interface{}, error) { 46 if len(serializedKey) == 0 { 47 return nil, fmt.Errorf("rsassapkcs1_verifier_key_manager: invalid serialized public key") 48 } 49 key := &rsassapkcs1pb.RsaSsaPkcs1PublicKey{} 50 if err := proto.Unmarshal(serializedKey, key); err != nil { 51 return nil, err 52 } 53 if err := validateRSAPKCS1PublicKey(key); err != nil { 54 return nil, err 55 } 56 keyData := &rsa.PublicKey{ 57 E: int(bytesToBigInt(key.GetE()).Int64()), 58 N: bytesToBigInt(key.GetN()), 59 } 60 return internal.New_RSA_SSA_PKCS1_Verifier(hashName(key.Params.HashType), keyData) 61} 62 63func validateRSAPKCS1PublicKey(pubKey *rsassapkcs1pb.RsaSsaPkcs1PublicKey) error { 64 if err := keyset.ValidateKeyVersion(pubKey.GetVersion(), rsaSSAPKCS1VerifierKeyVersion); err != nil { 65 return err 66 } 67 return validateRSAPubKeyParams( 68 pubKey.GetParams().GetHashType(), 69 bytesToBigInt(pubKey.GetN()).BitLen(), 70 pubKey.GetE()) 71} 72 73func (km *rsaSSAPKCS1VerifierKeyManager) NewKey(serializedKeyFormat []byte) (proto.Message, error) { 74 return nil, errRSASSAPKCS1NotImplemented 75} 76 77func (km *rsaSSAPKCS1VerifierKeyManager) NewKeyData(serializedKeyFormat []byte) (*tinkpb.KeyData, error) { 78 return nil, errRSASSAPKCS1NotImplemented 79} 80 81func (km *rsaSSAPKCS1VerifierKeyManager) DoesSupport(typeURL string) bool { 82 return typeURL == rsaSSAPKCS1VerifierTypeURL 83} 84 85func (km *rsaSSAPKCS1VerifierKeyManager) TypeURL() string { 86 return rsaSSAPKCS1VerifierTypeURL 87} 88