Documentation
¶
Index ¶
- Constants
- Variables
- func ValidateConsignmentIDs(payload Payload) error
- func ValidateConsignments(payload Payload) error
- func ValidateDelegatedUse(payload Payload) error
- func ValidateLimitConsignments(payload Payload) error
- func ValidateLimitDeliveryArea(payload Payload) error
- func ValidatePayload(payload Payload) error
- func ValidateRTU(rtu RTU) error
- func ValidateSellerAddress(payload Payload) error
- func ValidateSellerName(payload Payload) error
- func ValidateTransactionID(payload Payload) error
- func ValidateValidUntil(payload Payload) error
- func Verify(obj PackedRTU, opts ...VerifyOption) (Payload, PublicKey, error)
- type CPK
- type ExternalSigner
- func (e *ExternalSigner) ComputeDigest(data UnsignedPayload) (digest []byte, payload []byte, err error)
- func (e *ExternalSigner) ConstructSigned(payload []byte, signature []byte) (PackedRTU, error)
- func (e *ExternalSigner) ConstructSignedObj(payload []byte, signature []byte) (RTU, error)
- func (e *ExternalSigner) Format() Format
- func (e *ExternalSigner) Parse(payload []byte) (out Payload, err error)
- func (e *ExternalSigner) PublicKey() PublicKey
- func (e *ExternalSigner) Version() Version
- type Format
- type ID
- type PackedCPK
- type PackedRTU
- type Payload
- type PrivateKey
- type PublicKey
- type PublicKeyJWK
- type RTU
- type Ref
- type RefVersion
- type SignOption
- type SignatureAlgorithm
- type Type
- type UnsignedPayload
- type ValidationError
- type Verifier
- type VerifyOption
- type Version
Constants ¶
const ( // Validations for RTU object ValidationFieldPayload = "Payload" ValidationFieldSignature = "Signature" ValidationFieldAlgorithm = "Algorithm" ValidationFieldVersion = "Version" ValidationFieldFormat = "Format" ValidationFieldRTU = "RTU" // Validations for rtu.Payload ValidationFieldTransactionID = "TransactionID" ValidationFieldValidUntil = "ValidUntil" ValidationFieldCPK = "CPK" ValidationFieldDelegatedUse = "DelegatedUse" ValidationFieldSellerName = "SellerName" ValidationFieldSellerAddress = "SellerAddress" ValidationFieldLimitDeliveryArea = "LimitDeliveryArea" ValidationFieldConsignmentIDs = "ConsignmentIDs" ValidationFieldLimitConsignments = "LimitConsignments" )
ValidationFields valid values
const ( // EmptyRef is a reference to an empty Ref EmptyRef = Ref("") )
Variables ¶
var ( ErrValidation = errors.New("validation error") ErrEncoding = errors.New("encoding error") ErrDecoding = errors.New("decoding error") ErrCPKUnsupported = errors.New("unsupported CPK type") // jwt format speicifc errors ErrPackedRTUNotJWT = errors.New("packed RTU not JWT") ErrFieldRequired = errors.New("field is required") ErrSigning = errors.New("signing error") ErrSignatureInvalid = errors.New("signature invalid") ErrSignatureIsNil = errors.New("signature is empty") ErrPayloadIsNil = errors.New("payload is empty") ErrKeyInvalid = errors.New("invalid key") ErrUnknownSignatureAlgorithm = errors.New("unknown signature algorithm") ErrNoSignatureAlgorithm = errors.New("no signature algorithm") ErrUnknownVersion = errors.New("unknown version") ErrNoVersion = errors.New("no version") ErrUnknownFormat = errors.New("unknown format") ErrNoFormat = errors.New("no format") ErrEmptyInput = errors.New("empty input") ErrInvalidIDLength = errors.New("invalid ID length") ErrUnknownRefVersion = errors.New("unknown ref version") )
Functions ¶
func ValidateConsignmentIDs ¶ added in v0.3.0
func ValidateConsignments ¶ added in v0.3.0
func ValidateDelegatedUse ¶ added in v0.3.0
func ValidateLimitConsignments ¶ added in v0.3.0
func ValidateLimitDeliveryArea ¶ added in v0.3.0
func ValidatePayload ¶ added in v0.3.0
ValidatePayload calls every field validation on Payload, and returns the first sign of an error.
func ValidateRTU ¶ added in v0.3.0
ValidateRTU validates the RTUs metadata (in case of ASN, validates the final RTU byte size is within perameters)
func ValidateSellerAddress ¶ added in v0.3.0
func ValidateSellerName ¶ added in v0.3.0
func ValidateTransactionID ¶ added in v0.3.0
func ValidateValidUntil ¶ added in v0.3.0
Types ¶
type CPK ¶
type CPK []byte
CPK - Compressed Public Key is a binary representation of a public key. Together with a SignatureAlgorithm, it should be possible to get a public key from this value.
func NewCPK ¶
func NewCPK(pubKey any, algorithm SignatureAlgorithm) (CPK, error)
NewCPK creates a CPK representation of the given pubKey and SignatureAlgorithm.
type ExternalSigner ¶
type ExternalSigner struct {
// contains filtered or unexported fields
}
func NewExternalSigner ¶
func NewExternalSigner(format Format, version Version, publicKey PublicKey) (*ExternalSigner, error)
func (*ExternalSigner) ComputeDigest ¶
func (e *ExternalSigner) ComputeDigest(data UnsignedPayload) (digest []byte, payload []byte, err error)
func (*ExternalSigner) ConstructSigned ¶
func (e *ExternalSigner) ConstructSigned(payload []byte, signature []byte) (PackedRTU, error)
func (*ExternalSigner) ConstructSignedObj ¶
func (e *ExternalSigner) ConstructSignedObj(payload []byte, signature []byte) (RTU, error)
func (*ExternalSigner) Format ¶ added in v0.3.0
func (e *ExternalSigner) Format() Format
func (*ExternalSigner) Parse ¶ added in v0.3.0
func (e *ExternalSigner) Parse(payload []byte) (out Payload, err error)
func (*ExternalSigner) PublicKey ¶ added in v0.3.0
func (e *ExternalSigner) PublicKey() PublicKey
func (*ExternalSigner) Version ¶
func (e *ExternalSigner) Version() Version
type ID ¶ added in v0.3.1
type ID []byte
type PackedCPK ¶ added in v0.3.0
type PackedCPK string
PackedCPK is a base64url encoded representation of a CPK
type PackedRTU ¶
type PackedRTU string
PackedRTU is the signed base64url encoded IOSS-RTU
func Sign ¶
func Sign(payload UnsignedPayload, key PrivateKey, opts ...SignOption) (PackedRTU, error)
Sign wraps SignRaw, but returns the PackedRTU instead of the RTU object
type Payload ¶
type Payload interface {
Type
ValidUntil() time.Time
TransactionID() *string
DelegatedUse() *bool
SellerName() *string
SellerAddress() *string
LimitDeliveryArea() *string
Consignments() []string
LimitConsignments() *int
}
Payload is an interface allowing reading of the RTU payload
type PrivateKey ¶
type PrivateKey interface {
Public() PublicKey
Raw() crypto.PrivateKey
// Sign allows this private key to sign the given payload, by digesting it with SignatureAlgorithm
// and based on the same algorithm apply the correct signature function, outputting signature and any
// errors that happened while signing.
Sign(rtuType Type, rand io.Reader, payload []byte) ([]byte, error)
}
PrivateKey is a helper structure, that wraps a private key with a SignatureAlgorithm. It also generates its own CPK and exposes a common Sign method, for easier integration with potential other SignatureAlgorithms down the line.
func AddJWKToPrivateKey ¶ added in v0.3.0
func AddJWKToPrivateKey(key PrivateKey) (PrivateKey, error)
AddJWKToPrivateKey builds a PrivateKey, which returns a PublicKeyJWK when Public() method is called
func LoadPrivateKeyPEM ¶
func LoadPrivateKeyPEM(pemBytes []byte) (PrivateKey, error)
LoadPrivateKeyPEM parses a PEM-encoded EC private key. Accepts both PKCS#8 ("PRIVATE KEY") and SEC1 ("EC PRIVATE KEY") formats.
func NewECPrivateKey ¶
func NewECPrivateKey(priv *ecdsa.PrivateKey) (PrivateKey, error)
NewECPrivateKey only accepts P-256 private keys for now (because only AlgorithmEcdsaP256 is added)
type PublicKey ¶
type PublicKey interface {
Verifier
// CPK returns the rtu.CPK of this PublicKey
CPK() CPK
// Algorithm returns the SignatureAlgorithm of this PublicKey
Algorithm() SignatureAlgorithm
// Raw returns the raw publicKey (from go stdlib) of this PublicKey structure
Raw() crypto.PublicKey
// Equal returns if the public key is equal to the other (not same pointer, but same public key internally)
Equal(other PublicKey) bool
}
type PublicKeyJWK ¶ added in v0.3.0
type PublicKeyJWK interface {
PublicKey
// JWK returns the jwk.Key of this PublicKey.
// If a PublicKey implements this interface, JWT RTUs will have the "jwk" header instead of "cpk"
JWK() jwk.Key
}
func AddJWKToPublicKey ¶ added in v0.3.0
func AddJWKToPublicKey(key PublicKey) (PublicKeyJWK, error)
AddJWKToPublicKey wraps PublicKey and creates a PublicKeyJWK. Can be used when building an ExternalSigner, to ensure the JWT encoded RTUs will have a 'jwk' header.
func NewJWKPublicKey ¶ added in v0.3.0
func NewJWKPublicKey(key jwk.Key) (PublicKeyJWK, error)
NewJWKPublicKey creates a PublicKeyJWK from the given jwk.Key. Only jwk.ECDSAPublicKey is currently supported
type RTU ¶
type RTU interface {
Type
Parse() (Payload, PublicKey, error)
Payload() []byte
Signature() []byte
Size() int64
Pack() (PackedRTU, error)
}
func DecodeASN1 ¶ added in v0.3.0
func SignRaw ¶ added in v0.3.0
func SignRaw(payload UnsignedPayload, key PrivateKey, opts ...SignOption) (RTU, error)
SignRaw uses the UnsignedPayload given and signs it with the PrivateKey (it also sets the publicKey to the payload, validates the payload (unless WithoutSignValidation is given as an option) and returns the signed RTU object
type Ref ¶ added in v0.3.0
type Ref string
Ref is a reference string for an RTU. It is used as a reference or identity of an RTU by the IOSS RTU deposit service. When depositing the RTU, a Ref is returned by the deposit service. This code allows other implementations to calculate the Ref before depositing to the deposit service
func NewRefV1 ¶ added in v0.3.0
NewRefV1 creates a version 1 Ref = base32(first_75_bits_sha256(rtu_payload))
func NewRefV2 ¶ added in v0.3.0
NewRefV2 creates a version 2 Ref = "02" + base32(first_160_bits_sha256(rtu_payload))
func (Ref) Bytes ¶ added in v0.3.1
Bytes gets the raw byte array of the ID hash (as many bytes as the Ref holds)
func (Ref) Equals ¶ added in v0.3.1
Equals is used to determine, if our Ref is for the same RTU as the given Ref. Refs used the first X bytes of a SHA256 hash.
By normalizing both byte arrays, to be of same length, we can determine (but not prove) that the Refs were created from the same RTU
(if RefV1 is used, there are some small chances that 2 RTUs have the same Ref)
func (Ref) ParseID ¶ added in v0.3.1
ParseID parses the Ref and tries to get the raw bytes of the hash for the RTU payload (ID). Used by the deposit and verifier services to query the correct RTU based on the given Ref.
func (Ref) Version ¶ added in v0.3.0
func (r Ref) Version() RefVersion
Version returns the version of this Ref. It does not validate the encoding, just tries to determine the version number. For validation, use Ref.Parse
type RefVersion ¶ added in v0.3.1
type RefVersion int
RefVersion is an enum used to determine Ref format and version.
const ( // RefUnknown is only returned by Ref.Version in case the Ref is corrupt RefUnknown RefVersion = iota - 1 RefID RefV1 RefV2 )
type SignOption ¶ added in v0.3.0
type SignOption interface {
// contains filtered or unexported methods
}
func WithoutSignValidation ¶ added in v0.3.0
func WithoutSignValidation() SignOption
type SignatureAlgorithm ¶
type SignatureAlgorithm string
const ( // AlgorithmNone is used when there is no algorithm given in the RTU AlgorithmNone SignatureAlgorithm = "" // AlgorithmEcdsaP256 is the signature algorithm for ECDSA with P-256 curve, signing a SHA256 hash. AlgorithmEcdsaP256 SignatureAlgorithm = "ecdsa-p256" )
func ParseJwa ¶ added in v0.3.0
func ParseJwa(alg jwa.SignatureAlgorithm) (SignatureAlgorithm, error)
ParseJwa takes a jwa.SignatureAlgorithm and parses a SignatureAlgorithm. (example "ES256" -> "ecdsa-p256")
func (SignatureAlgorithm) Digest ¶
func (s SignatureAlgorithm) Digest(payload []byte) []byte
Digest returns the hash version of payload based on the SignatureAlgorithm given. if returned value is nil, it should be treated the same as an ErrSignatureAlgorithmInvalid.
func (SignatureAlgorithm) ToJWA ¶ added in v0.3.0
func (s SignatureAlgorithm) ToJWA() (jwa.SignatureAlgorithm, error)
ToJWA returns the JSON Web Token Algorithm string for the given SignatureAlgorithm
func (SignatureAlgorithm) Validate ¶ added in v0.3.0
func (s SignatureAlgorithm) Validate() error
Validate validates the SignatureAlgorithm, to ensure this library supports it fully
type Type ¶ added in v0.3.0
Type is an interface, that allows PrivateKey.Sign and PublicKey.Verify to know the metadata for their signature.
type UnsignedPayload ¶ added in v0.3.0
type UnsignedPayload interface {
Payload
// Marshal creates a byte array of this payload (to be signed)
Marshal() ([]byte, error)
PublicKey() PublicKey
SetPublicKey(pk PublicKey) (UnsignedPayload, error)
SetValidUntil(t time.Time) UnsignedPayload
SetTransactionID(transactionID string) UnsignedPayload
SetDelegatedUse(d bool) UnsignedPayload
SetSellerName(s string) UnsignedPayload
SetSellerAddress(s string) UnsignedPayload
SetLimitDeliveryArea(s string) UnsignedPayload
SetConsignmentIDs(ids []string) UnsignedPayload
SetLimitConsignments(l int) UnsignedPayload
}
func NewVersion1ASN ¶ added in v0.3.0
func NewVersion1ASN(txID string, validUntil time.Time, delegatedUse bool) UnsignedPayload
NewVersion1ASN is a wrapper of New, that makes an ASN1 encoded Version1 RTU, but takes as arguments the fields, that are required in Version1
func NewVersion1JWT ¶ added in v0.3.0
func NewVersion1JWT(txId string, validUntil time.Time, delegatedUse bool) UnsignedPayload
type ValidationError ¶
ValidationError carries per-field details
func NewValidationError ¶ added in v0.3.0
func NewValidationError(field string, err error) *ValidationError
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Unwrap ¶
func (e *ValidationError) Unwrap() error
type Verifier ¶ added in v0.3.0
type Verifier interface {
// Verify verifies the signature, based on the given pubKey and payload.
// payload must not already be digested, as this function takes care of that.
// Type is needed, to correctly prepare the verification of the signature
// (jwt signature != asn1 signature formats)
Verify(rtuType Type, payload, signature []byte) error
}
type VerifyOption ¶ added in v0.3.0
type VerifyOption interface {
// contains filtered or unexported methods
}
func WithIgnoreValidation ¶ added in v0.3.0
func WithIgnoreValidation() VerifyOption
func WithNoSignatureVerification ¶ added in v0.3.0
func WithNoSignatureVerification() VerifyOption
type Version ¶
type Version int32
Version is an integer (should be uint32, but int32 is used for asn1), that defines the schema for a given RTU. It is used by the RTU to parse its payload to return the common Payload structure.
func (Version) DefaultSignatureAlgorithm ¶
func (v Version) DefaultSignatureAlgorithm() SignatureAlgorithm
DefaultSignatureAlgorithm returns the default signature algorithm for this version if AlgorithmNone is returned, it means either the version does not exist and/or there is no default for that version. Please use other methods, to validate if version exists
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
examples/external-signer
command
|
|
|
examples/signer
command
|
|
|
examples/verify
command
|
|