casdk

package module
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2020 License: MIT Imports: 29 Imported by: 0

README

casdk

  • 只调用ca 相关接口

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidAlgorithmFamily = errors.New("invalid algorithm family")
	ErrInvalidAlgorithm       = errors.New("invalid algorithm for ECDSA")
	ErrInvalidHash            = errors.New("invalid hash algorithm")
	ErrInvalidKeyType         = errors.New("invalid key type is provided")
	ErrEnrollmentIdMissing    = errors.New("enrollment id is empty")
	ErrAffiliationMissing     = errors.New("affiliation is missing")
	ErrTypeMissing            = errors.New("type is missing")
	ErrCertificateEmpty       = errors.New("certificate cannot be nil")
	ErrIdentityNameMissing    = errors.New("identity must have  name")
)

Functions

func B64Decode

func B64Decode(s string) ([]byte, error)

func B64Encode

func B64Encode(buf []byte) string

func CertToPem

func CertToPem(cert *x509.Certificate) []byte

func DERToPrivateKey

func DERToPrivateKey(der []byte) (key interface{}, err error)

func GetCertSerialNumber added in v1.0.4

func GetCertSerialNumber(pemCert []byte) (string, string, error)

func GetPemPrivateKey added in v1.0.4

func GetPemPrivateKey(key interface{}) ([]byte, error)

func GetPemPublicKey added in v1.0.4

func GetPemPublicKey(key interface{}) ([]byte, error)

func IsLowS

func IsLowS(k *ecdsa.PublicKey, s *big.Int) (bool, error)

func IsPathExists

func IsPathExists(filepath string) (bool, error)

func ParsePemCert

func ParsePemCert(cert []byte) (*x509.Certificate, error)

func ParsePemKey

func ParsePemKey(key []byte) (*ecdsa.PrivateKey, error)

func SaveCrl

func SaveCrl(ca *FabricCAClient, request *CARevocationRequest, result *CARevokeResult) error

Save crl

func ToLowS

func ToLowS(k *ecdsa.PublicKey, s *big.Int) (*big.Int, bool, error)

Types

type BasicKeyRequest

type BasicKeyRequest struct {
	Algo string `json:"algo" yaml:"algo"`
	Size int    `json:"size" yaml:"size"`
}

BasicKeyRequest encapsulates size and algorithm for the key to be generated

type CAConfig

type CAConfig struct {
	CryptoConfig      `yaml:"crypto"`
	Url               string `yaml:"url"`
	SkipTLSValidation bool   `yaml:"skiptlsvalidation"`
	MspId             string `yaml:"mspid"`
	FilePath          string `yaml:"filepath"`
	CaName            string `yaml:"caname"`
}

func NewCAConfig

func NewCAConfig(path string) (*CAConfig, error)

type CAGetCertResponse

type CAGetCertResponse struct {
	// RootCertificates is list of pem encoded certificates
	RootCertificates []*x509.Certificate
	// IntermediateCertificates is list of pem encoded intermediate certificates
	IntermediateCertificates []*pem.Block
	// CAName is the name of the CA server that returns this certificates
	CAName string
	// Version is the version of server that returns this certificates
	Version string
}

CAGetCertsResponse holds response from `GetCaCertificateChain`

type CAGetIdentityResponse

type CAGetIdentityResponse struct {
	CaIdentityResponse
	CAName string `json:"caname"`
}

type CAListAllIdentitesResponse

type CAListAllIdentitesResponse struct {
	CAName     string               `json:"caname"`
	Identities []CaIdentityResponse `json:"identities,omitempty"`
}

type CARegistrationRequest

type CARegistrationRequest struct {
	// EnrolmentId is unique name that identifies identity
	EnrolmentId string `json:"id"`
	// Type defines type of this identity (user,client, auditor etc...)
	Type string `json:"type"`
	// Secret is password that will be used for enrollment. If not provided random password will be generated
	Secret string `json:"secret,omitempty" mask:"password" help:"The enrollment secret for the identity being registered"`
	// MaxEnrollments define maximum number of times that identity can enroll. If not provided or is 0 there is no limit
	MaxEnrollments int `json:"max_enrollments,omitempty"`
	// Affiliation associates identity with particular organisation.
	// for example org1.department1 makes this identity part of organisation `org1` and department `department1`
	// Hierarchical structure can be created using .(dot). For example org1.dep1 will create dep1 as part of org1
	Affiliation string `json:"affiliation" help:"The identity's affiliation"`
	// Attrs are attributes associated with this identity
	Attrs []CaRegisterAttribute `json:"attrs,omitempty"`
	// CAName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and
	// this names are used to distinguish between them. If empty default CA instance will be used.
	CAName string `json:"caname,omitempty" skip:"true"`
}

RegistrationRequest holds all data needed for new registration of new user in Certificate Authority

type CARevocationRequest

type CARevocationRequest struct {
	// EnrollmentId of the identity whose certificates should be revoked
	// If this field is omitted, then Serial and AKI must be specified.
	EnrollmentId string `json:"id,omitempty"`
	// Serial number of the certificate to be revoked
	// If this is omitted, then EnrollmentId must be specified
	Serial string `json:"serial,omitempty"`
	// AKI (Authority Key Identifier) of the certificate to be revoked
	AKI string `json:"aki,omitempty"`
	// Reason is the reason for revocation.  See https://godoc.org/golang.org/x/crypto/ocsp for
	// valid values.  The default value is 0 (ocsp.Unspecified).
	Reason string `json:"reason,omitempty"`
	// CAName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and
	// this names are used to distinguish between them. If empty default CA instance will be used.
	CAName string `json:"caname,omitempty"`
	// GenCRL specifies whether to generate a CRL. CRL will be returned only when AKI and Serial are provided.
	GenCRL bool `json:"gencrl,omitempty"`
}

type CARevokeResult

type CARevokeResult struct {
	// RevokedCertificates is list of revoked certificates
	RevokedCertificates []CaRevokeResultCertificate `json:"RevokedCerts"`
	// CRL is the certificate revocation list from the operation.
	CRL string `json:"CRL"`
}

type CSRInfo

type CSRInfo struct {
	CN           string           `json:"CN"`
	Names        []csr.Name       `json:"names,omitempty"`
	Hosts        []string         `json:"hosts,omitempty"`
	KeyRequest   *BasicKeyRequest `json:"key,omitempty"`
	CA           *csr.CAConfig    `json:"ca,omitempty"`
	SerialNumber string           `json:"serial_number,omitempty"`
}

type CaEnrollAttribute

type CaEnrollAttribute struct {
	// Name is the name of the attribute
	Name string `json:"name"`
	// Optional define behaviour when required attribute is not available to user. If `true` then request will continue,
	// but attribute will not be included in ECert. If `false` and attribute is missing, request will fail.
	// If false and attribute is available, request will continue and attribute will be added in ECert
	Optional bool `json:"optional,omitempty"`
}

type CaEnrollmentRequest

type CaEnrollmentRequest struct {
	// EnrollmentId is the unique entity identifies
	EnrollmentId string `json:"name" skip:"true"`
	// Secret is the password for this identity
	Secret string `json:"secret,omitempty" skip:"true" mask:"password"`
	// Profile define which CA profile to be used for signing. When this profile is empty default profile is used.
	// This is the common situation when issuing and ECert.
	// If request is fo generating TLS certificates then profile must be `tls`
	// If operation is related to parent CA server then profile must be `ca`
	// In FabricCA custom profiles can be created. In this situation use custom profile name.
	Profile string `json:"profile,omitempty"`
	//Label is used for hardware secure modules.
	Label string `json:"label,omitempty"`
	// CAName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and
	// this names are used to distinguish between them. If empty default CA instance will be used.
	CAName string `json:"caname,omitempty" skip:"true"`
	// Host is the list of valid host names for this certificate. If empty default hosts will be used
	Hosts []string `json:"hosts"`
	// Attrs are the attributes that must be included in ECert. This is subset of the attributes used in registration.
	Attrs []CaEnrollAttribute `json:"attr_reqs,omitempty"`
	// CSR is Certificate Signing Request info
	CSR *CSRInfo `json:"csr,omitempty" help:"Certificate Signing Request info"`
}

CaEnrollmentRequest holds data needed for getting ECert (enrollment) from CA server

type CaIdentityResponse

type CaIdentityResponse struct {
	ID             string                `json:"id"`
	Type           string                `json:"type"`
	Affiliation    string                `json:"affiliation"`
	Attributes     []CaRegisterAttribute `json:"attrs" mapstructure:"attrs"`
	MaxEnrollments int                   `json:"max_enrollments" mapstructure:"max_enrollments"`
}

type CaRegisterAttribute

type CaRegisterAttribute struct {
	// Name is the name of the attribute.
	Name string `json:"name"`
	// Value is the value of the attribute. Can be empty string
	Value string `json:"value"`
	// ECert define how this attribute will be included in ECert. If this value is true this attribute will be
	// added to ECert automatically on Enrollment if no attributes are requested on Enrollment request.
	ECert bool `json:"ecert,omitempty"`
}

CaRegisterAttribute holds user attribute used for registration for example user may have attr `accountType` with value `premium` this attributes can be accessed in chainCode and build business logic on top of them

type CaRevokeResultCertificate

type CaRevokeResultCertificate struct {
	// Serial is revoked certificate serial number
	Serial string `json:"Serial"`
	// AKI is revoked certificate AKI
	AKI string `json:"AKI"`
}

CaRevokeResultCertificate identify revoked certificate

type CryptoConfig

type CryptoConfig struct {
	Family    string `yaml:"family"`
	Algorithm string `yaml:"algorithm"`
	Hash      string `yaml:"hash"`
}

type CryptoSuite

type CryptoSuite interface {
	// GenerateKey returns PrivateKey.
	GenerateKey() (interface{}, error)
	// CreateCertificateRequest will create CSR request. It takes enrolmentId and Private key
	CreateCertificateRequest(request CaEnrollmentRequest, regAttr, standAttrList map[string]string, key interface{}, hosts []string) ([]byte, error)
	// Sign signs message. It takes message to sign and Private key
	Sign(msg []byte, k interface{}) ([]byte, error)
	// Hash computes Hash value of provided data. Hash function will be different in different crypto implementations.
	Hash(data []byte) []byte
	// new key to byte
	NewKey() (CryptoSuite, error)
	// get privateKey byte
	GetPemPrivateKey() ([]byte, error)
	// get publicKey byte
	GetPemPublicKey() ([]byte, error)
	// verify by public
	Verify(private interface{}, signature, digest []byte) (valid bool, err error)
}

func NewECCryptoSuiteFromConfig

func NewECCryptoSuiteFromConfig(config CryptoConfig) (CryptoSuite, error)

type ECCryptSuite

type ECCryptSuite struct {
	// contains filtered or unexported fields
}

func (*ECCryptSuite) CreateCertificateRequest

func (c *ECCryptSuite) CreateCertificateRequest(request CaEnrollmentRequest, regAttr, standAttrList map[string]string, key interface{}, hosts []string) ([]byte, error)

func (*ECCryptSuite) GenerateKey

func (c *ECCryptSuite) GenerateKey() (interface{}, error)

func (*ECCryptSuite) GetPemPrivateKey

func (c *ECCryptSuite) GetPemPrivateKey() ([]byte, error)

func (*ECCryptSuite) GetPemPublicKey

func (c *ECCryptSuite) GetPemPublicKey() ([]byte, error)

func (*ECCryptSuite) Hash

func (c *ECCryptSuite) Hash(data []byte) []byte

func (*ECCryptSuite) NewKey

func (c *ECCryptSuite) NewKey() (CryptoSuite, error)

func (*ECCryptSuite) Sign

func (c *ECCryptSuite) Sign(msg []byte, k interface{}) ([]byte, error)

func (*ECCryptSuite) UnmarshalECDSASignature added in v1.0.1

func (c *ECCryptSuite) UnmarshalECDSASignature(raw []byte) (*big.Int, *big.Int, error)

func (*ECCryptSuite) Verify added in v1.0.1

func (c *ECCryptSuite) Verify(public interface{}, signature, digest []byte) (valid bool, err error)

私钥 签名 待签名的数据

type FabricCAClient

type FabricCAClient struct {
	// Uri is access point for fabric-ca server. Port number and scheme must be provided.
	// for example http://127.0.0.1:7054
	Url string
	// SkipTLSVerification define how connection must handle invalid TLC certificates.
	// if true, all verifications are skipped. This value is overwritten by Transport property, if provided
	SkipTLSVerification bool
	// Crypto is CryptSuite implementation used to sign request for fabric-ca server
	Crypto CryptoSuite
	// Transport define transport rules for communication with fabric-ca server. If nil, default Go setting will be used
	// It is responsibility of the user to provide proper TLS/certificate setting in TLS communication.
	Transport *http.Transport
	// MspId value will be added to Identity in Enrollment and ReEnrollment invocations.
	// This value is not used anywhere in CA implementation, but is need in every call to Fabric and is added here
	// for convenience, because (in general case) FabricCA is serving one MSP
	// User can overwrite this value at any time.
	MspId string
	//
	FilePath string
	//
	ServerInfo ServerInfo
}

func NewCAClient

func NewCAClient(path string, transport *http.Transport) (*FabricCAClient, error)

func NewCaClientFromConfig

func NewCaClientFromConfig(config CAConfig, transport *http.Transport) (*FabricCAClient, error)

func (*FabricCAClient) Enroll

func (f *FabricCAClient) Enroll(request CaEnrollmentRequest) (*Identity, error)

给已注册的用户申请一个ca证书 Enroll execute enrollment request for registered user in FabricCA server. On success new Identity with ECert and generated csr are returned.

func (*FabricCAClient) EnrollByKey

func (f *FabricCAClient) EnrollByKey(request CaEnrollmentRequest, pemPrivateKey []byte, regAttr map[string]string, standardAttrList map[string]string) (*Identity, error)

根据私钥申请证书

func (*FabricCAClient) Gencrl added in v1.0.2

func (f *FabricCAClient) Gencrl(identity *Identity) (out *pkix.CertificateList, err error)

获取证书吊销列表

func (*FabricCAClient) GetCaInfo

func (f *FabricCAClient) GetCaInfo() (*CAGetCertResponse, error)

GetCaCertificateChain gets root and intermediate certificates used by FabricCA server. This certificates must be presented to Fabric entities (peers, orderers) as MSP so they can verify that request are from valid entities. caName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and this names are used to distinguish between them. If empty default CA instance will be used.

func (*FabricCAClient) GetCertSerialAki added in v1.0.2

func (f *FabricCAClient) GetCertSerialAki(pemCert []byte) (string, string, error)

*

  • 获取证书的 serial、aki

func (*FabricCAClient) GetIdentities

func (f *FabricCAClient) GetIdentities(identity *Identity, caName string) (*CAListAllIdentitesResponse, error)

ListAllIdentities get list of all identities from FabricCa server

func (*FabricCAClient) GetIdentity

func (f *FabricCAClient) GetIdentity(identity *Identity, id string, caName string) (*CAGetIdentityResponse, error)

func (*FabricCAClient) NewKey

func (f *FabricCAClient) NewKey() (privateKey, publicKey []byte, err error)

生成一对公私钥

func (*FabricCAClient) Register

func (f *FabricCAClient) Register(identity *Identity, req *CARegistrationRequest) (string, error)

Register registers new user in fabric-ca server. In registration request attributes, affiliation and max enrolments must be set. It is responsibility of the SDK user to ensure passwords are with big entropy. Identity parameter is certificate for user that makes registration and this user MUST have the role for registering new users.

func (*FabricCAClient) Revoke

func (f *FabricCAClient) Revoke(identity *Identity, request *CARevocationRequest) (*CARevokeResult, error)

Revoke revokes ECert in fabric-ca server. Note that this request will revoke certificate ONLY in FabricCa server. Peers (for now) do not know about this certificate revocation. It is responsibility of the SDK user to update peers and set this certificate in every peer revocation list.

type Identity

type Identity struct {
	Certificate *x509.Certificate
	PrivateKey  interface{}
	MspId       string
}
var ID *Identity

func InitAdminIdentity

func InitAdminIdentity(cert, privateKey []byte) (*Identity, error)

func (*Identity) GetPemCert

func (i *Identity) GetPemCert() []byte

func (*Identity) GetPemPrivateKey

func (i *Identity) GetPemPrivateKey() ([]byte, error)

func (*Identity) GetPemPublicKey

func (i *Identity) GetPemPublicKey() (b []byte, err error)

func (*Identity) GetStoreData

func (i *Identity) GetStoreData() (cert, privateKey, publicKey []byte, err error)

func (*Identity) SaveCert

func (i *Identity) SaveCert(ca *FabricCAClient, enreq *CaEnrollmentRequest, cainfo *CAGetCertResponse) error

func (*Identity) SaveTLScert

func (i *Identity) SaveTLScert(ca *FabricCAClient, cainfo *CAGetCertResponse) error

type ServerInfo

type ServerInfo struct {
	CAName string
	CACert *x509.Certificate
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL