github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/accesscontrol/crypto/ecdsa/ecdsa.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package ecdsa 18 19 import ( 20 "crypto/ecdsa" 21 "encoding/asn1" 22 "math/big" 23 24 "github.com/hyperledger/fabric/accesscontrol/crypto" 25 ) 26 27 type x509ECDSASignatureVerifierImpl struct { 28 } 29 30 // ECDSASignature represents an ECDSA signature 31 type ECDSASignature struct { 32 R, S *big.Int 33 } 34 35 func (sv *x509ECDSASignatureVerifierImpl) Verify(certificate, signature, message []byte) (bool, error) { 36 // Interpret vk as an x509 certificate 37 cert, err := derToX509Certificate(certificate) 38 if err != nil { 39 return false, err 40 } 41 42 // TODO: verify certificate 43 44 // Interpret signature as an ECDSA signature 45 vk := cert.PublicKey.(*ecdsa.PublicKey) 46 47 return sv.verifyImpl(vk, signature, message) 48 } 49 50 func (sv *x509ECDSASignatureVerifierImpl) verifyImpl(vk *ecdsa.PublicKey, signature, message []byte) (bool, error) { 51 ecdsaSignature := new(ECDSASignature) 52 _, err := asn1.Unmarshal(signature, ecdsaSignature) 53 if err != nil { 54 return false, err 55 } 56 57 h, err := computeHash(message, vk.Params().BitSize) 58 if err != nil { 59 return false, err 60 } 61 62 return ecdsa.Verify(vk, h, ecdsaSignature.R, ecdsaSignature.S), nil 63 } 64 65 func NewX509ECDSASignatureVerifier() crypto.SignatureVerifier { 66 return &x509ECDSASignatureVerifierImpl{} 67 }