Documentation
¶
Overview ¶
Package oscore implements OSCORE, Object Security for Constrained RESTful Environments, defined in RFC 8613.
OSCORE protects CoAP requests and responses end to end: the code, most options and the payload of a message are encrypted into a COSE object, and the result is an ordinary CoAP message that proxies can forward. The package works on a Message, the code, options and payload of a CoAP message, and leaves the transport to the application: the Token, Message ID and Type of the original message are copied to the protected one, over UDP, TCP or anything else.
Security contexts ¶
Both endpoints derive a Context from the same Master Secret and Master Salt, with their Sender and Recipient IDs swapped (NewContext). The parameters usually come from a key exchange such as EDHOC (RFC 9528), whose OSCORE parameters map to Config, or are provisioned out of band.
A Sender Sequence Number must never be used twice with the same keys, and a server must not accept a request twice. Two contexts created from the same parameters use the same numbers, so create only one. To keep a context across restarts, set Config.Reserve to store Sender Sequence Numbers ahead of use (RFC 8613, Appendix B.1.1), and either save the replay window with Context.ReplayWindow or set Config.ReplayWindowLost so that the server checks the freshness of the first request with Echo (Appendix B.1.2).
Clients ¶
A client protects a request with Context.ProtectRequest, sends the result with a new Token, and verifies each response with the same Token with Exchange.UnprotectResponse. If a verified response is 4.01 (Unauthorized) with an Echo option, the client protects the request again with that Echo option added (RFC 9175). An error response that is not protected may be forged, and the client must not change its security context because of it.
Servers ¶
A server finds the context of a request from its kid and kid context with ParseRequestID, and verifies it with Context.UnprotectRequest. It protects each response to that request with Exchange.ProtectResponse. When verification fails, ErrorResponse gives the response to send, if any.
Observe and block-wise transfers ¶
A request with an Observe option of 0 is an Observe registration, and its exchange protects and accepts any number of notifications in order (RFC 8613, Section 4.1.3.5). Block options in a message are protected like other options: for an end-to-end block-wise transfer, the application splits the message into blocks and protects each one. Outer block-wise transfers, between hops, happen in the transport after protection and before verification.
Status ¶
The package implements RFC 8613 except Appendix B.2 and the HTTP mapping of Section 11, with every AEAD algorithm of RFC 9053 and HKDF with SHA-256, SHA-384 or SHA-512. Hop-Limit is an Outer option, as in draft-ietf-core-oscore-capable-proxies. It does not implement Group OSCORE.
Example ¶
package main
import (
"fmt"
"log"
"github.com/aquilliam/oscore"
)
var (
masterSecret = []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10}
masterSalt = []byte{0x9e, 0x7c, 0xa9, 0x22, 0x23, 0x78, 0x63, 0x40}
)
// newContexts returns the contexts of a client with Sender ID 0x01 and a
// server with Sender ID 0x02.
func newContexts() (client, server *oscore.Context) {
client, err := oscore.NewContext(&oscore.Config{
MasterSecret: masterSecret,
MasterSalt: masterSalt,
SenderID: []byte{0x01},
RecipientID: []byte{0x02},
})
if err != nil {
log.Fatal(err)
}
server, err = oscore.NewContext(&oscore.Config{
MasterSecret: masterSecret,
MasterSalt: masterSalt,
SenderID: []byte{0x02},
RecipientID: []byte{0x01},
})
if err != nil {
log.Fatal(err)
}
return client, server
}
func main() {
client, server := newContexts()
// The client protects a GET request. The transport sends the result
// with a new Token.
req := &oscore.Message{
Code: oscore.GET,
Options: []oscore.Option{{Number: oscore.URIPath, Value: []byte("temperature")}},
}
protected, exchange, err := client.ProtectRequest(req)
if err != nil {
log.Fatal(err)
}
fmt.Println("on the wire:", protected.Code)
// The server verifies the request and protects its response.
got, serverExchange, err := server.UnprotectRequest(protected)
if err != nil {
log.Fatal(err)
}
fmt.Println("server got:", got.Code, string(got.Options[0].Value))
resp, err := serverExchange.ProtectResponse(&oscore.Message{Code: oscore.Content, Payload: []byte("21.5 C")})
if err != nil {
log.Fatal(err)
}
fmt.Println("on the wire:", resp.Code)
// The client verifies the response with the same Token.
answer, err := exchange.UnprotectResponse(resp)
if err != nil {
log.Fatal(err)
}
fmt.Println("client got:", answer.Code, string(answer.Payload))
}
Output: on the wire: 0.02 server got: 0.01 temperature on the wire: 2.04 client got: 2.05 21.5 C
Index ¶
- Variables
- type Algorithm
- type Code
- type Config
- type Context
- func (c *Context) AEAD() Algorithm
- func (c *Context) IDContext() []byte
- func (c *Context) ProtectRequest(m *Message) (*Message, *Exchange, error)
- func (c *Context) RecipientID() []byte
- func (c *Context) ReplayWindow() (w ReplayWindow, ok bool)
- func (c *Context) SenderID() []byte
- func (c *Context) SenderSequence() uint64
- func (c *Context) UnprotectRequest(m *Message) (*Message, *Exchange, error)
- type Exchange
- type Message
- type Option
- type OptionNumber
- type ReplayWindow
- type RequestID
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotProtected is returned for a message without an OSCORE option. ErrNotProtected = errors.New("oscore: message is not protected with OSCORE") // ErrMalformed is returned for a message whose OSCORE option or decrypted // plaintext cannot be decoded. ErrMalformed = errors.New("oscore: malformed OSCORE message") // ErrUnknownContext is returned when a request is not for the context // it is passed to. A server that finds no context for a request can // send ErrorResponse(ErrUnknownContext). ErrUnknownContext = errors.New("oscore: security context not found") // ErrReplay is returned for a request or response that was received // before. ErrReplay = errors.New("oscore: replay detected") // ErrDecrypt is returned when a message fails to decrypt. ErrDecrypt = errors.New("oscore: decryption failed") // ErrSequenceExhausted is returned when a context has used all its // Sender Sequence Numbers and a new context is needed. The context then // protects no messages and accepts no requests (RFC 8613, Section // 7.2.1), and ErrorResponse is the one for ErrUnknownContext. ErrSequenceExhausted = errors.New("oscore: Sender Sequence Numbers exhausted") // ErrNestedOSCORE is returned when the message to protect already has // an OSCORE option. ErrNestedOSCORE = errors.New("oscore: message already has an OSCORE option") // ErrTooLarge is returned when a message is too large for the AEAD // algorithm: more than 65535 bytes of plaintext for AES-CCM with a // 13-byte nonce. ErrTooLarge = errors.New("oscore: message too large for the AEAD algorithm") // ErrCode is returned when a request to protect does not have a request // code or a response does not have a response code, other than a // signaling code. ErrCode = errors.New("oscore: wrong kind of CoAP code") // ErrObserve is returned for a response with an Observe option to a // request that is not an Observe registration. ErrObserve = errors.New("oscore: Observe option in a response to a request that is not an Observe registration") // ErrExchangeDone is returned when a response is protected for an // exchange that cannot have another one. ErrExchangeDone = errors.New("oscore: exchange already has its response") )
var ErrEchoRequired = errors.New("oscore: request freshness must be verified with Echo")
ErrEchoRequired is returned by Context.UnprotectRequest when the context's replay window was lost and the request has not been shown to be fresh (RFC 8613, Appendix B.1.2). The request must not be processed. ErrorResponse(err) is a protected 4.01 (Unauthorized) response with an Echo option, which asks the client to send the request again with that option.
Functions ¶
This section is empty.
Types ¶
type Algorithm ¶
type Algorithm int
An Algorithm is a COSE AEAD algorithm identifier (RFC 9053).
const ( A128GCM Algorithm = 1 A192GCM Algorithm = 2 A256GCM Algorithm = 3 AESCCM16_64_128 Algorithm = 10 AESCCM16_64_256 Algorithm = 11 AESCCM64_64_128 Algorithm = 12 AESCCM64_64_256 Algorithm = 13 ChaCha20Poly1305 Algorithm = 24 AESCCM16_128_128 Algorithm = 30 AESCCM16_128_256 Algorithm = 31 AESCCM64_128_128 Algorithm = 32 AESCCM64_128_256 Algorithm = 33 )
The AEAD algorithms supported for OSCORE. AESCCM16_64_128 is the default and mandatory to implement (RFC 8613, Section 5).
type Code ¶
type Code uint8
A Code is the code of a CoAP message (RFC 7252, Section 3): a 3-bit class and a 5-bit detail.
const ( GET Code = 0x01 POST Code = 0x02 PUT Code = 0x03 DELETE Code = 0x04 FETCH Code = 0x05 PATCH Code = 0x06 IPATCH Code = 0x07 Created Code = 0x41 Deleted Code = 0x42 Valid Code = 0x43 Changed Code = 0x44 Content Code = 0x45 BadRequest Code = 0x80 BadOption Code = 0x82 Forbidden Code = 0x83 NotFound Code = 0x84 InternalServerError Code = 0xa0 )
CoAP codes used by this package and common in applications.
type Config ¶
type Config struct {
// MasterSecret is required. MasterSalt may be empty.
MasterSecret []byte
MasterSalt []byte
// IDContext is the ID Context, or nil for none. A client sends it as
// 'kid context' in every request; a server with several contexts for the
// same Recipient ID uses it to tell them apart. It can be at most 248
// bytes long, less the length of the longer of SenderID and RecipientID.
IDContext []byte
// SenderID and RecipientID must differ and be at most NonceSize() - 6
// bytes long. Either may be empty.
SenderID []byte
RecipientID []byte
// AEAD is the AEAD algorithm. The default is AESCCM16_64_128.
AEAD Algorithm
// Hash is the hash function used with HKDF: crypto.SHA256 (the
// default), crypto.SHA384 or crypto.SHA512. EDHOC's application hash
// algorithms -16, -43 and -44 are these three.
Hash crypto.Hash
// SenderSequence is the first Sender Sequence Number to use: 0 for a
// new context, or the value last stored by Reserve after a restart. A
// restored context must also set ReplayWindow or ReplayWindowLost, or
// it accepts requests it has received before and may reuse a nonce to
// answer them.
SenderSequence uint64
// Reserve, if not nil, makes Sender Sequence Numbers survive a restart
// (RFC 8613, Appendix B.1.1). Before the context uses a number n for the
// first time without having reserved it, it calls Reserve(n +
// ReserveStep). Reserve must store that value durably before it
// returns; after a restart, pass the stored value as SenderSequence.
// If Reserve fails, so does the operation that needed the number.
// Reserve is called with the context locked and must not call its
// methods or those of its exchanges. The last value it is called with
// is 1<<40, and NewContext returns ErrSequenceExhausted for it.
Reserve func(next uint64) error
// ReserveStep is how many Sender Sequence Numbers each call to Reserve
// covers. The default is 1, which stores the number before every
// message with a Partial IV.
ReserveStep uint64
// ReplayWindowSize is the number of Partial IVs the replay window
// tracks, from 1 to 64. The default is 32.
ReplayWindowSize int
// ReplayWindow restores a replay window saved with Context.ReplayWindow,
// or is nil for a context that has not received requests yet. It is not
// used if ReplayWindowLost is set. A window saved by v0.1.0 must not be
// restored.
ReplayWindow *ReplayWindow
// ReplayWindowLost, for a context restored without its replay window,
// makes the server check the freshness of the first request with the
// Echo option before accepting requests (RFC 8613, Appendix B.1.2).
// Until a client answers, every request, replayed or not, gets a
// challenge with a new Sender Sequence Number, so Reserve is required.
// An accepted request was made after NewContext was called, but is not
// otherwise known to be recent.
ReplayWindowLost bool
}
Config holds the input parameters of a security context (RFC 8613, Section 3.2) and how the context keeps its state across restarts.
Example (Reserve) ¶
A device that keeps its context across reboots stores Sender Sequence Numbers ahead of use, and has the server check the freshness of the first request after a reboot with the Echo option.
package main
import (
"fmt"
"log"
"github.com/aquilliam/oscore"
)
var (
masterSecret = []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10}
masterSalt = []byte{0x9e, 0x7c, 0xa9, 0x22, 0x23, 0x78, 0x63, 0x40}
)
// newContexts returns the contexts of a client with Sender ID 0x01 and a
// server with Sender ID 0x02.
func newContexts() (client, server *oscore.Context) {
client, err := oscore.NewContext(&oscore.Config{
MasterSecret: masterSecret,
MasterSalt: masterSalt,
SenderID: []byte{0x01},
RecipientID: []byte{0x02},
})
if err != nil {
log.Fatal(err)
}
server, err = oscore.NewContext(&oscore.Config{
MasterSecret: masterSecret,
MasterSalt: masterSalt,
SenderID: []byte{0x02},
RecipientID: []byte{0x01},
})
if err != nil {
log.Fatal(err)
}
return client, server
}
func main() {
var stored uint64 // stands in for nonvolatile memory
newServer := func(rebooted bool) *oscore.Context {
ctx, err := oscore.NewContext(&oscore.Config{
MasterSecret: masterSecret,
MasterSalt: masterSalt,
SenderID: []byte{0x02},
RecipientID: []byte{0x01},
SenderSequence: stored,
Reserve: func(next uint64) error {
stored = next
return nil
},
ReserveStep: 100,
ReplayWindowLost: rebooted,
})
if err != nil {
log.Fatal(err)
}
return ctx
}
client, _ := newContexts()
server := newServer(false)
send := func(req *oscore.Message) *oscore.Message {
protected, x, err := client.ProtectRequest(req)
if err != nil {
log.Fatal(err)
}
var resp *oscore.Message
if got, sx, err := server.UnprotectRequest(protected); err != nil {
resp = oscore.ErrorResponse(err)
} else if resp, err = sx.ProtectResponse(&oscore.Message{Code: oscore.Content, Payload: got.Payload}); err != nil {
log.Fatal(err)
}
answer, err := x.UnprotectResponse(resp)
if err != nil {
log.Fatal(err)
}
return answer
}
fmt.Println("before the reboot:", send(&oscore.Message{Code: oscore.GET}).Code)
server = newServer(true)
answer := send(&oscore.Message{Code: oscore.GET})
fmt.Println("first request:", answer.Code)
echo := answer.Options[0]
answer = send(&oscore.Message{Code: oscore.GET, Options: []oscore.Option{echo}})
fmt.Println("with Echo:", answer.Code)
fmt.Println("stored:", stored)
}
Output: before the reboot: 2.05 first request: 4.01 with Echo: 2.05 stored: 100
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
A Context is an OSCORE security context: the Common Context, the Sender Context and the Recipient Context of one endpoint (RFC 8613, Section 3). A Context is safe for concurrent use.
func NewContext ¶
NewContext derives a security context from cfg (RFC 8613, Section 3.2.1).
func (*Context) ProtectRequest ¶
ProtectRequest protects a CoAP request (RFC 8613, Section 8.1). It returns the OSCORE request to send, with the same Token as m, and the exchange to verify the response with.
The options of m are protected as RFC 8613, Section 4.1 says: Uri-Host, Uri-Port, Proxy-Scheme, Hop-Limit and EDHOC are left outside, Proxy-Uri is split, Observe is both inside and outside, and every other option is encrypted. Block options in m are Inner, for block-wise transfer between the endpoints; Outer Block options belong to the transport.
func (*Context) RecipientID ¶
RecipientID returns the Recipient ID.
func (*Context) ReplayWindow ¶
func (c *Context) ReplayWindow() (w ReplayWindow, ok bool)
ReplayWindow returns the state of the replay window, to be stored with the context and passed back in Config.ReplayWindow. It must not be used to restore a context that has received requests since it was saved. ok is false if the window was lost and no client has answered the challenge yet: the context must then be restored with Config.ReplayWindowLost, and NewContext rejects w without it.
func (*Context) SenderSequence ¶
SenderSequence returns the next Sender Sequence Number the context will use.
func (*Context) UnprotectRequest ¶
UnprotectRequest verifies and decrypts an OSCORE request (RFC 8613, Section 8.2). It returns the decrypted request and the exchange to protect the responses with. The context must be the one whose Recipient ID and ID Context match the request; see ParseRequestID.
The decrypted request has the Code, payload and Inner options of the original request, and the Outer Uri-Host, Uri-Port, Proxy-Uri, Proxy-Scheme, Hop-Limit and EDHOC options of m, which are not protected.
If it fails, the request must not be processed; ErrorResponse(err) is the response to send, if any. If several contexts match the request, try the next one on ErrDecrypt or ErrReplay, as a replay is detected before the request is decrypted, and answer with ErrReplay if any returned it.
type Exchange ¶
type Exchange struct {
// contains filtered or unexported fields
}
An Exchange binds the responses to a request to that request (RFC 8613, Section 7.1). A client gets one from ProtectRequest and verifies the responses with UnprotectResponse; a server gets one from UnprotectRequest and protects its responses with ProtectResponse.
An exchange accepts one response, or, for an Observe registration, any number of notifications. It is safe for concurrent use.
func (*Exchange) ProtectResponse ¶
ProtectResponse protects a CoAP response to the request of a server's exchange (RFC 8613, Section 8.3). It returns the OSCORE response to send, with the same Token as the request.
The first response reuses the request's nonce; later notifications to an Observe registration get a Partial IV of their own. A response with an Observe option is protected as a notification, and one without ends the observation. It fails with ErrSequenceExhausted once the context has used all its Sender Sequence Numbers, also for a request accepted before.
func (*Exchange) UnprotectResponse ¶
UnprotectResponse verifies and decrypts an OSCORE response to the request of a client's exchange (RFC 8613, Section 8.4). It returns the decrypted response. If it fails, the response must be ignored. Like a decrypted request, the response has the Outer Uri-Host, Uri-Port, Proxy-Uri, Proxy-Scheme, Hop-Limit and EDHOC options of m, which are not protected.
For an Observe registration, a notification is accepted only if it is newer than every notification accepted before (RFC 8613, Section 7.4.1), so accepted notifications are in order. The Observe option of a decrypted notification is empty. A response without Observe ends the observation.
Example (Observe) ¶
An Observe registration gets any number of notifications.
package main
import (
"errors"
"fmt"
"log"
"github.com/aquilliam/oscore"
)
var (
masterSecret = []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10}
masterSalt = []byte{0x9e, 0x7c, 0xa9, 0x22, 0x23, 0x78, 0x63, 0x40}
)
// newContexts returns the contexts of a client with Sender ID 0x01 and a
// server with Sender ID 0x02.
func newContexts() (client, server *oscore.Context) {
client, err := oscore.NewContext(&oscore.Config{
MasterSecret: masterSecret,
MasterSalt: masterSalt,
SenderID: []byte{0x01},
RecipientID: []byte{0x02},
})
if err != nil {
log.Fatal(err)
}
server, err = oscore.NewContext(&oscore.Config{
MasterSecret: masterSecret,
MasterSalt: masterSalt,
SenderID: []byte{0x02},
RecipientID: []byte{0x01},
})
if err != nil {
log.Fatal(err)
}
return client, server
}
func main() {
client, server := newContexts()
protected, exchange, err := client.ProtectRequest(&oscore.Message{
Code: oscore.GET,
Options: []oscore.Option{
{Number: oscore.Observe},
{Number: oscore.URIPath, Value: []byte("temperature")},
},
})
if err != nil {
log.Fatal(err)
}
_, serverExchange, err := server.UnprotectRequest(protected)
if err != nil {
log.Fatal(err)
}
var notifications []*oscore.Message
for i, v := range []string{"21.5 C", "21.6 C", "21.8 C"} {
n, err := serverExchange.ProtectResponse(&oscore.Message{
Code: oscore.Content,
Options: []oscore.Option{{Number: oscore.Observe, Value: []byte{byte(i)}}},
Payload: []byte(v),
})
if err != nil {
log.Fatal(err)
}
notifications = append(notifications, n)
}
for _, i := range []int{0, 2, 1} { // the second arrives late
n, err := exchange.UnprotectResponse(notifications[i])
if errors.Is(err, oscore.ErrReplay) {
fmt.Println("stale notification dropped")
continue
}
if err != nil {
log.Fatal(err)
}
fmt.Println(string(n.Payload))
}
}
Output: 21.5 C 21.8 C stale notification dropped
type Message ¶
A Message is the part of a CoAP message that OSCORE works on: the code, the options and the payload. The header fields of the transport, such as the Token, Type and Message ID, are not protected and are carried over from the original message by the caller.
Options may be in any order; options with the same number keep their relative order.
func ErrorResponse ¶
ErrorResponse returns the response a server sends when UnprotectRequest or ParseRequestID fails with err (RFC 8613, Section 8.2), or nil if it should send none. The response is an unprotected error with an Outer Max-Age of 0 and a diagnostic payload, except for ErrEchoRequired, whose response is protected. For ErrNotProtected it is 4.01 (Unauthorized), for a server whose resources all require OSCORE.
type OptionNumber ¶
type OptionNumber uint16
An OptionNumber identifies a CoAP option.
const ( IfMatch OptionNumber = 1 URIHost OptionNumber = 3 ETag OptionNumber = 4 IfNoneMatch OptionNumber = 5 Observe OptionNumber = 6 URIPort OptionNumber = 7 LocationPath OptionNumber = 8 OSCORE OptionNumber = 9 URIPath OptionNumber = 11 ContentFormat OptionNumber = 12 MaxAge OptionNumber = 14 URIQuery OptionNumber = 15 HopLimit OptionNumber = 16 Accept OptionNumber = 17 QBlock1 OptionNumber = 19 LocationQuery OptionNumber = 20 EDHOC OptionNumber = 21 Block2 OptionNumber = 23 Block1 OptionNumber = 27 Size2 OptionNumber = 28 QBlock2 OptionNumber = 31 ProxyURI OptionNumber = 35 ProxyScheme OptionNumber = 39 Size1 OptionNumber = 60 Echo OptionNumber = 252 NoResponse OptionNumber = 258 RequestTag OptionNumber = 292 )
CoAP option numbers from the CoAP Option Numbers registry.
type ReplayWindow ¶
A ReplayWindow is the state of a replay window (RFC 8613, Section 7.4): the highest Partial IV received and, in bit i of Received, whether Highest - i was received. A zero ReplayWindow has received nothing.
type RequestID ¶
type RequestID struct {
// KID is the Sender ID of the client: the Recipient ID of the context.
KID []byte
// KIDContext is the 'kid context', the ID Context of the context, or
// nil if the request has none.
KIDContext []byte
}
A RequestID holds the identifiers in an OSCORE request that select the security context to verify it with.
func ParseRequestID ¶
ParseRequestID returns the identifiers in the OSCORE option of a request, for a server to look up the security context to pass the request to with Context.UnprotectRequest. The fields share memory with m. If no context matches, the server can send ErrorResponse(ErrUnknownContext).
Example ¶
A server with several clients finds the context of each request from its kid, and answers with ErrorResponse when a request fails to verify.
package main
import (
"fmt"
"log"
"github.com/aquilliam/oscore"
)
var (
masterSecret = []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10}
masterSalt = []byte{0x9e, 0x7c, 0xa9, 0x22, 0x23, 0x78, 0x63, 0x40}
)
// newContexts returns the contexts of a client with Sender ID 0x01 and a
// server with Sender ID 0x02.
func newContexts() (client, server *oscore.Context) {
client, err := oscore.NewContext(&oscore.Config{
MasterSecret: masterSecret,
MasterSalt: masterSalt,
SenderID: []byte{0x01},
RecipientID: []byte{0x02},
})
if err != nil {
log.Fatal(err)
}
server, err = oscore.NewContext(&oscore.Config{
MasterSecret: masterSecret,
MasterSalt: masterSalt,
SenderID: []byte{0x02},
RecipientID: []byte{0x01},
})
if err != nil {
log.Fatal(err)
}
return client, server
}
func main() {
client, server := newContexts()
contexts := map[string]*oscore.Context{string(server.RecipientID()): server}
handle := func(m *oscore.Message) *oscore.Message {
id, err := oscore.ParseRequestID(m)
if err != nil {
return oscore.ErrorResponse(err)
}
ctx := contexts[string(id.KID)]
if ctx == nil {
return oscore.ErrorResponse(oscore.ErrUnknownContext)
}
req, x, err := ctx.UnprotectRequest(m)
if err != nil {
return oscore.ErrorResponse(err)
}
resp, err := x.ProtectResponse(&oscore.Message{Code: oscore.Changed, Payload: req.Payload})
if err != nil {
return &oscore.Message{Code: oscore.InternalServerError}
}
return resp
}
protected, _, err := client.ProtectRequest(&oscore.Message{Code: oscore.POST, Payload: []byte("on")})
if err != nil {
log.Fatal(err)
}
fmt.Println(handle(protected).Code)
r := handle(protected) // the same request again
fmt.Println(r.Code, string(r.Payload))
}
Output: 2.04 4.01 Replay detected