2020-09-22 11:59:10 +02:00
|
|
|
|
// Copyright 2017 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package cryptobyte
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// A Builder builds byte strings from fixed-length and length-prefixed values.
|
|
|
|
|
// Builders either allocate space as needed, or are ‘fixed’, which means that
|
|
|
|
|
// they write into a given buffer and produce an error if it's exhausted.
|
|
|
|
|
//
|
|
|
|
|
// The zero value is a usable Builder that allocates space as needed.
|
|
|
|
|
//
|
|
|
|
|
// Simple values are marshaled and appended to a Builder using methods on the
|
|
|
|
|
// Builder. Length-prefixed values are marshaled by providing a
|
|
|
|
|
// BuilderContinuation, which is a function that writes the inner contents of
|
|
|
|
|
// the value to a given Builder. See the documentation for BuilderContinuation
|
|
|
|
|
// for details.
|
|
|
|
|
type Builder struct {
|
|
|
|
|
err error
|
|
|
|
|
result []byte
|
|
|
|
|
fixedSize bool
|
|
|
|
|
child *Builder
|
|
|
|
|
offset int
|
|
|
|
|
pendingLenLen int
|
|
|
|
|
pendingIsASN1 bool
|
|
|
|
|
inContinuation *bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewBuilder creates a Builder that appends its output to the given buffer.
|
|
|
|
|
// Like append(), the slice will be reallocated if its capacity is exceeded.
|
|
|
|
|
// Use Bytes to get the final buffer.
|
|
|
|
|
func NewBuilder(buffer []byte) *Builder {
|
|
|
|
|
return &Builder{
|
|
|
|
|
result: buffer,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewFixedBuilder creates a Builder that appends its output into the given
|
|
|
|
|
// buffer. This builder does not reallocate the output buffer. Writes that
|
|
|
|
|
// would exceed the buffer's capacity are treated as an error.
|
|
|
|
|
func NewFixedBuilder(buffer []byte) *Builder {
|
|
|
|
|
return &Builder{
|
|
|
|
|
result: buffer,
|
|
|
|
|
fixedSize: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetError sets the value to be returned as the error from Bytes. Writes
|
|
|
|
|
// performed after calling SetError are ignored.
|
|
|
|
|
func (b *Builder) SetError(err error) {
|
|
|
|
|
b.err = err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Bytes returns the bytes written by the builder or an error if one has
|
|
|
|
|
// occurred during building.
|
|
|
|
|
func (b *Builder) Bytes() ([]byte, error) {
|
|
|
|
|
if b.err != nil {
|
|
|
|
|
return nil, b.err
|
|
|
|
|
}
|
|
|
|
|
return b.result[b.offset:], nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BytesOrPanic returns the bytes written by the builder or panics if an error
|
|
|
|
|
// has occurred during building.
|
|
|
|
|
func (b *Builder) BytesOrPanic() []byte {
|
|
|
|
|
if b.err != nil {
|
|
|
|
|
panic(b.err)
|
|
|
|
|
}
|
|
|
|
|
return b.result[b.offset:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddUint8 appends an 8-bit value to the byte string.
|
|
|
|
|
func (b *Builder) AddUint8(v uint8) {
|
|
|
|
|
b.add(byte(v))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddUint16 appends a big-endian, 16-bit value to the byte string.
|
|
|
|
|
func (b *Builder) AddUint16(v uint16) {
|
|
|
|
|
b.add(byte(v>>8), byte(v))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddUint24 appends a big-endian, 24-bit value to the byte string. The highest
|
|
|
|
|
// byte of the 32-bit input value is silently truncated.
|
|
|
|
|
func (b *Builder) AddUint24(v uint32) {
|
|
|
|
|
b.add(byte(v>>16), byte(v>>8), byte(v))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddUint32 appends a big-endian, 32-bit value to the byte string.
|
|
|
|
|
func (b *Builder) AddUint32(v uint32) {
|
|
|
|
|
b.add(byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
|
|
|
|
|
}
|
|
|
|
|
|
NOISSUE - Switch to Google Zanzibar Access control approach (#1919)
* Return Auth service
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Update Compose to run with SpiceDB and Auth svc
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Update auth gRPC API
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Remove Users' policies
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Move Groups to internal
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Use shared groups in Users
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Remove unused code
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Use pkg Groups in Things
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Remove Things groups
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Make imports consistent
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Update Groups networking
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Remove things groups-specific API
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Move Things Clients to the root
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Move Clients to Users root
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Temporarily remove tracing
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Fix imports
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Add buffer config for gRPC
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Update auth type for Things
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Use Auth for login
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Add temporary solution for refresh token
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Update Tokenizer interface
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Updade tokens issuing
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Fix token issuing
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Update JWT validator and refactor Tokenizer
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Rename access timeout
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Rename login to authenticate
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Update Identify to use SubjectID
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Add Auth to Groups
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Use the Auth service for Groups
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Update auth schema
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Fix Auth for Groups
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Add auth for addons (#14)
Signed-off-by: Arvindh <arvindh91@gmail.com>
Speparate Login and Refresh tokens
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Merge authN and authZ requests for things
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Add connect and disconnect
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Update sharing
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Fix policies addition and removal
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Update relation with roels
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Add gRPC to Things
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Assign and Unassign members to group and Listing of Group members (#15)
* add auth for addons
Signed-off-by: Arvindh <arvindh91@gmail.com>
* add assign and unassign to group
Signed-off-by: Arvindh <arvindh91@gmail.com>
* add group incomplete repo implementation
Signed-off-by: Arvindh <arvindh91@gmail.com>
* groups for users
Signed-off-by: Arvindh <arvindh91@gmail.com>
* groups for users
Signed-off-by: Arvindh <arvindh91@gmail.com>
* groups for users
Signed-off-by: Arvindh <arvindh91@gmail.com>
* groups for users
Signed-off-by: Arvindh <arvindh91@gmail.com>
---------
Signed-off-by: Arvindh <arvindh91@gmail.com>
Move coap mqtt and ws policies to spicedb (#16)
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Remove old policies
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
NOISSUE - Things authorize to return thingID (#18)
This commit modifies the authorize endpoint to the grpc endpoint to return thingID. The authorize endpoint allows adapters to get the publisher of the message.
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Add Groups to users service (#17)
* add assign and unassign to group
Signed-off-by: Arvindh <arvindh91@gmail.com>
* add group incomplete repo implementation
Signed-off-by: Arvindh <arvindh91@gmail.com>
* groups for users
Signed-off-by: Arvindh <arvindh91@gmail.com>
* groups for users
Signed-off-by: Arvindh <arvindh91@gmail.com>
* groups for users
Signed-off-by: Arvindh <arvindh91@gmail.com>
* groups for users
Signed-off-by: Arvindh <arvindh91@gmail.com>
* groups for users stable 1
Signed-off-by: Arvindh <arvindh91@gmail.com>
* groups for users stable 2
Signed-off-by: Arvindh <arvindh91@gmail.com>
* groups for users & things
Signed-off-by: Arvindh <arvindh91@gmail.com>
* Amend signature
Signed-off-by: Arvindh <arvindh91@gmail.com>
* fix merge error
Signed-off-by: Arvindh <arvindh91@gmail.com>
---------
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* NOISSUE - Fix es code (#21)
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* NOISSUE - Fix Bugs (#20)
* fix bugs
Signed-off-by: Arvindh <arvindh91@gmail.com>
* fix bugs
Signed-off-by: Arvindh <arvindh91@gmail.com>
---------
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* NOISSUE - Test e2e (#19)
* fix: connect method
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* fix: e2e
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* fix changes in sdk and e2e
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* feat(docker): remove unnecessary port mapping
Remove the port mapping for MQTT broker in the docker-compose.yml file.
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* Enable group listing
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* feat(responses): update ChannelsPage struct
The ChannelsPage struct in the responses.go file has been updated. The "Channels" field has been renamed to "Groups" to provide more accurate naming. This change ensures consistency and clarity in the codebase.
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* feat(things): add UpdateClientSecret method
Add the UpdateClientSecret method to the things service. This method allows updating the client secret for a specific client identified by the provided token, id, and key parameters.
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
---------
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* Use smaller buffers for gRPC
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* Clean up tests (#22)
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* Add Connect Disconnect endpoints (#23)
* fix bugs
Signed-off-by: Arvindh <arvindh91@gmail.com>
* fix bugs
Signed-off-by: Arvindh <arvindh91@gmail.com>
* fix list of things in a channel and Add connect disconnect endpoint
Signed-off-by: Arvindh <arvindh91@gmail.com>
* fix list of things in a channel and Add connect disconnect endpoint
Signed-off-by: Arvindh <arvindh91@gmail.com>
---------
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* Add: Things share with users (#25)
* fix list of things in a channel and Add connect disconnect endpoint
Signed-off-by: Arvindh <arvindh91@gmail.com>
* add: things share with other users
Signed-off-by: Arvindh <arvindh91@gmail.com>
---------
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* NOISSUE - Rename gRPC Services (#24)
* Rename things and users auth service
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* docs: add authorization docs for gRPC services
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* Rename things and users grpc services
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* Remove mainflux.env package
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
---------
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* Add: Listing of things, channels, groups, users (#26)
* add: listing of channels, users, groups, things
Signed-off-by: Arvindh <arvindh91@gmail.com>
* add: listing of channels, users, groups, things
Signed-off-by: Arvindh <arvindh91@gmail.com>
* add: listing of channels, users, groups, things
Signed-off-by: Arvindh <arvindh91@gmail.com>
* add: listing of channels, users, groups, things
Signed-off-by: Arvindh <arvindh91@gmail.com>
---------
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* NOISSUE - Clean Up Users (#27)
* feat(groups): rename redis package to events
- Renamed the `redis` package to `events` in the `internal/groups` directory.
- Updated the file paths and names accordingly.
- This change reflects the more accurate purpose of the package and improves code organization.
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* feat(auth): Modify identity method
Change request and response of identity method
Add accessToken and refreshToken to Token response
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* clean up users, remove dead code
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* feat(users): add unit tests for user service
This commit adds unit tests for the user service in the `users` package. The tests cover various scenarios and ensure the correct behavior of the service.
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
---------
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* Add: List of user groups & removed repeating code in groups (#29)
* removed repeating code in list groups
Signed-off-by: Arvindh <arvindh91@gmail.com>
* add: list of user group
Signed-off-by: Arvindh <arvindh91@gmail.com>
* fix: otel handler operator name for endpoints
Signed-off-by: Arvindh <arvindh91@gmail.com>
---------
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* NOISSUE - Clean Up Things Service (#28)
* Rework things service
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* add tests
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
---------
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* NOISSUE - Clean Up Auth Service (#30)
* clean up auth service
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
* feat(auth): remove unused import
Remove the unused import of `emptypb` in `auth.pb.go`. This import is not being used in the codebase and can be safely removed.
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
---------
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* NOISSUE - Update API docs (#31)
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* Remove TODO comments and cleanup the code
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
* Update dependenices
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
---------
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Co-authored-by: b1ackd0t <28790446+rodneyosodo@users.noreply.github.com>
Co-authored-by: Arvindh <30824765+arvindh123@users.noreply.github.com>
2023-10-15 22:02:13 +02:00
|
|
|
|
// AddUint48 appends a big-endian, 48-bit value to the byte string.
|
|
|
|
|
func (b *Builder) AddUint48(v uint64) {
|
|
|
|
|
b.add(byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 15:56:35 +02:00
|
|
|
|
// AddUint64 appends a big-endian, 64-bit value to the byte string.
|
|
|
|
|
func (b *Builder) AddUint64(v uint64) {
|
|
|
|
|
b.add(byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-22 11:59:10 +02:00
|
|
|
|
// AddBytes appends a sequence of bytes to the byte string.
|
|
|
|
|
func (b *Builder) AddBytes(v []byte) {
|
|
|
|
|
b.add(v...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BuilderContinuation is a continuation-passing interface for building
|
|
|
|
|
// length-prefixed byte sequences. Builder methods for length-prefixed
|
|
|
|
|
// sequences (AddUint8LengthPrefixed etc) will invoke the BuilderContinuation
|
|
|
|
|
// supplied to them. The child builder passed to the continuation can be used
|
|
|
|
|
// to build the content of the length-prefixed sequence. For example:
|
|
|
|
|
//
|
2022-04-26 18:41:22 +02:00
|
|
|
|
// parent := cryptobyte.NewBuilder()
|
|
|
|
|
// parent.AddUint8LengthPrefixed(func (child *Builder) {
|
|
|
|
|
// child.AddUint8(42)
|
|
|
|
|
// child.AddUint8LengthPrefixed(func (grandchild *Builder) {
|
|
|
|
|
// grandchild.AddUint8(5)
|
|
|
|
|
// })
|
|
|
|
|
// })
|
2020-09-22 11:59:10 +02:00
|
|
|
|
//
|
|
|
|
|
// It is an error to write more bytes to the child than allowed by the reserved
|
|
|
|
|
// length prefix. After the continuation returns, the child must be considered
|
|
|
|
|
// invalid, i.e. users must not store any copies or references of the child
|
|
|
|
|
// that outlive the continuation.
|
|
|
|
|
//
|
|
|
|
|
// If the continuation panics with a value of type BuildError then the inner
|
|
|
|
|
// error will be returned as the error from Bytes. If the child panics
|
|
|
|
|
// otherwise then Bytes will repanic with the same value.
|
|
|
|
|
type BuilderContinuation func(child *Builder)
|
|
|
|
|
|
|
|
|
|
// BuildError wraps an error. If a BuilderContinuation panics with this value,
|
|
|
|
|
// the panic will be recovered and the inner error will be returned from
|
|
|
|
|
// Builder.Bytes.
|
|
|
|
|
type BuildError struct {
|
|
|
|
|
Err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddUint8LengthPrefixed adds a 8-bit length-prefixed byte sequence.
|
|
|
|
|
func (b *Builder) AddUint8LengthPrefixed(f BuilderContinuation) {
|
|
|
|
|
b.addLengthPrefixed(1, false, f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddUint16LengthPrefixed adds a big-endian, 16-bit length-prefixed byte sequence.
|
|
|
|
|
func (b *Builder) AddUint16LengthPrefixed(f BuilderContinuation) {
|
|
|
|
|
b.addLengthPrefixed(2, false, f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddUint24LengthPrefixed adds a big-endian, 24-bit length-prefixed byte sequence.
|
|
|
|
|
func (b *Builder) AddUint24LengthPrefixed(f BuilderContinuation) {
|
|
|
|
|
b.addLengthPrefixed(3, false, f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddUint32LengthPrefixed adds a big-endian, 32-bit length-prefixed byte sequence.
|
|
|
|
|
func (b *Builder) AddUint32LengthPrefixed(f BuilderContinuation) {
|
|
|
|
|
b.addLengthPrefixed(4, false, f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Builder) callContinuation(f BuilderContinuation, arg *Builder) {
|
|
|
|
|
if !*b.inContinuation {
|
|
|
|
|
*b.inContinuation = true
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
*b.inContinuation = false
|
|
|
|
|
|
|
|
|
|
r := recover()
|
|
|
|
|
if r == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if buildError, ok := r.(BuildError); ok {
|
|
|
|
|
b.err = buildError.Err
|
|
|
|
|
} else {
|
|
|
|
|
panic(r)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f(arg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Builder) addLengthPrefixed(lenLen int, isASN1 bool, f BuilderContinuation) {
|
|
|
|
|
// Subsequent writes can be ignored if the builder has encountered an error.
|
|
|
|
|
if b.err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
offset := len(b.result)
|
|
|
|
|
b.add(make([]byte, lenLen)...)
|
|
|
|
|
|
|
|
|
|
if b.inContinuation == nil {
|
|
|
|
|
b.inContinuation = new(bool)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b.child = &Builder{
|
|
|
|
|
result: b.result,
|
|
|
|
|
fixedSize: b.fixedSize,
|
|
|
|
|
offset: offset,
|
|
|
|
|
pendingLenLen: lenLen,
|
|
|
|
|
pendingIsASN1: isASN1,
|
|
|
|
|
inContinuation: b.inContinuation,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b.callContinuation(f, b.child)
|
|
|
|
|
b.flushChild()
|
|
|
|
|
if b.child != nil {
|
|
|
|
|
panic("cryptobyte: internal error")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Builder) flushChild() {
|
|
|
|
|
if b.child == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
b.child.flushChild()
|
|
|
|
|
child := b.child
|
|
|
|
|
b.child = nil
|
|
|
|
|
|
|
|
|
|
if child.err != nil {
|
|
|
|
|
b.err = child.err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
length := len(child.result) - child.pendingLenLen - child.offset
|
|
|
|
|
|
|
|
|
|
if length < 0 {
|
|
|
|
|
panic("cryptobyte: internal error") // result unexpectedly shrunk
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if child.pendingIsASN1 {
|
|
|
|
|
// For ASN.1, we reserved a single byte for the length. If that turned out
|
|
|
|
|
// to be incorrect, we have to move the contents along in order to make
|
|
|
|
|
// space.
|
|
|
|
|
if child.pendingLenLen != 1 {
|
|
|
|
|
panic("cryptobyte: internal error")
|
|
|
|
|
}
|
|
|
|
|
var lenLen, lenByte uint8
|
|
|
|
|
if int64(length) > 0xfffffffe {
|
|
|
|
|
b.err = errors.New("pending ASN.1 child too long")
|
|
|
|
|
return
|
|
|
|
|
} else if length > 0xffffff {
|
|
|
|
|
lenLen = 5
|
|
|
|
|
lenByte = 0x80 | 4
|
|
|
|
|
} else if length > 0xffff {
|
|
|
|
|
lenLen = 4
|
|
|
|
|
lenByte = 0x80 | 3
|
|
|
|
|
} else if length > 0xff {
|
|
|
|
|
lenLen = 3
|
|
|
|
|
lenByte = 0x80 | 2
|
|
|
|
|
} else if length > 0x7f {
|
|
|
|
|
lenLen = 2
|
|
|
|
|
lenByte = 0x80 | 1
|
|
|
|
|
} else {
|
|
|
|
|
lenLen = 1
|
|
|
|
|
lenByte = uint8(length)
|
|
|
|
|
length = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert the initial length byte, make space for successive length bytes,
|
|
|
|
|
// and adjust the offset.
|
|
|
|
|
child.result[child.offset] = lenByte
|
|
|
|
|
extraBytes := int(lenLen - 1)
|
|
|
|
|
if extraBytes != 0 {
|
|
|
|
|
child.add(make([]byte, extraBytes)...)
|
|
|
|
|
childStart := child.offset + child.pendingLenLen
|
|
|
|
|
copy(child.result[childStart+extraBytes:], child.result[childStart:])
|
|
|
|
|
}
|
|
|
|
|
child.offset++
|
|
|
|
|
child.pendingLenLen = extraBytes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l := length
|
|
|
|
|
for i := child.pendingLenLen - 1; i >= 0; i-- {
|
|
|
|
|
child.result[child.offset+i] = uint8(l)
|
|
|
|
|
l >>= 8
|
|
|
|
|
}
|
|
|
|
|
if l != 0 {
|
|
|
|
|
b.err = fmt.Errorf("cryptobyte: pending child length %d exceeds %d-byte length prefix", length, child.pendingLenLen)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if b.fixedSize && &b.result[0] != &child.result[0] {
|
|
|
|
|
panic("cryptobyte: BuilderContinuation reallocated a fixed-size buffer")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b.result = child.result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Builder) add(bytes ...byte) {
|
|
|
|
|
if b.err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if b.child != nil {
|
|
|
|
|
panic("cryptobyte: attempted write while child is pending")
|
|
|
|
|
}
|
|
|
|
|
if len(b.result)+len(bytes) < len(bytes) {
|
|
|
|
|
b.err = errors.New("cryptobyte: length overflow")
|
|
|
|
|
}
|
|
|
|
|
if b.fixedSize && len(b.result)+len(bytes) > cap(b.result) {
|
|
|
|
|
b.err = errors.New("cryptobyte: Builder is exceeding its fixed-size buffer")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
b.result = append(b.result, bytes...)
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
|
// Unwrite rolls back non-negative n bytes written directly to the Builder.
|
|
|
|
|
// An attempt by a child builder passed to a continuation to unwrite bytes
|
|
|
|
|
// from its parent will panic.
|
2020-09-22 11:59:10 +02:00
|
|
|
|
func (b *Builder) Unwrite(n int) {
|
|
|
|
|
if b.err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if b.child != nil {
|
|
|
|
|
panic("cryptobyte: attempted unwrite while child is pending")
|
|
|
|
|
}
|
|
|
|
|
length := len(b.result) - b.pendingLenLen - b.offset
|
|
|
|
|
if length < 0 {
|
|
|
|
|
panic("cryptobyte: internal error")
|
|
|
|
|
}
|
2023-06-14 12:40:37 +02:00
|
|
|
|
if n < 0 {
|
|
|
|
|
panic("cryptobyte: attempted to unwrite negative number of bytes")
|
|
|
|
|
}
|
2020-09-22 11:59:10 +02:00
|
|
|
|
if n > length {
|
|
|
|
|
panic("cryptobyte: attempted to unwrite more than was written")
|
|
|
|
|
}
|
|
|
|
|
b.result = b.result[:len(b.result)-n]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A MarshalingValue marshals itself into a Builder.
|
|
|
|
|
type MarshalingValue interface {
|
|
|
|
|
// Marshal is called by Builder.AddValue. It receives a pointer to a builder
|
|
|
|
|
// to marshal itself into. It may return an error that occurred during
|
|
|
|
|
// marshaling, such as unset or invalid values.
|
|
|
|
|
Marshal(b *Builder) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddValue calls Marshal on v, passing a pointer to the builder to append to.
|
|
|
|
|
// If Marshal returns an error, it is set on the Builder so that subsequent
|
|
|
|
|
// appends don't have an effect.
|
|
|
|
|
func (b *Builder) AddValue(v MarshalingValue) {
|
|
|
|
|
err := v.Marshal(b)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.err = err
|
|
|
|
|
}
|
|
|
|
|
}
|