-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add credential resource to allow configure CA during onboard bridge t…
…he device
- Loading branch information
Showing
17 changed files
with
312 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/**************************************************************************** | ||
* | ||
* Copyright (c) 2024 plgd.dev s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"), | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
* | ||
****************************************************************************/ | ||
|
||
package credential | ||
|
||
import "github.com/plgd-dev/device/v2/schema/credential" | ||
|
||
type Config = credential.CredentialResponse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/**************************************************************************** | ||
* | ||
* Copyright (c) 2024 plgd.dev s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"), | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
* | ||
****************************************************************************/ | ||
|
||
package credential | ||
|
||
import ( | ||
"crypto/x509" | ||
|
||
"github.com/plgd-dev/device/v2/bridge/net" | ||
"github.com/plgd-dev/device/v2/bridge/resources" | ||
"github.com/plgd-dev/device/v2/pkg/codec/cbor" | ||
"github.com/plgd-dev/device/v2/schema/credential" | ||
"github.com/plgd-dev/device/v2/schema/interfaces" | ||
"github.com/plgd-dev/go-coap/v3/message/codes" | ||
"github.com/plgd-dev/go-coap/v3/message/pool" | ||
coapSync "github.com/plgd-dev/go-coap/v3/pkg/sync" | ||
"github.com/plgd-dev/kit/v2/security" | ||
) | ||
|
||
type Manager struct { | ||
credentials *coapSync.Map[int, credential.Credential] | ||
save func() | ||
} | ||
|
||
func New(save func()) *Manager { | ||
if save == nil { | ||
save = func() {} | ||
} | ||
return &Manager{ | ||
save: save, | ||
credentials: coapSync.NewMap[int, credential.Credential](), | ||
} | ||
} | ||
|
||
func (m *Manager) GetCAPool() []*x509.Certificate { | ||
certs := make([]*x509.Certificate, 0, m.credentials.Length()) | ||
m.credentials.Range(func(key int, value credential.Credential) bool { | ||
if value.Type != credential.CredentialType_ASYMMETRIC_SIGNING_WITH_CERTIFICATE { | ||
return true | ||
} | ||
if value.Usage != credential.CredentialUsage_TRUST_CA && value.Usage != credential.CredentialUsage_MFG_TRUST_CA { | ||
return true | ||
} | ||
cas, err := security.ParseX509FromPEM(value.PublicData.Data()) | ||
if err != nil { | ||
return true | ||
} | ||
certs = append(certs, cas...) | ||
return true | ||
}) | ||
return certs | ||
} | ||
|
||
func (m *Manager) getNextID() int { | ||
var id int | ||
m.credentials.Range(func(key int, value credential.Credential) bool { | ||
if key > id { | ||
id = key | ||
} | ||
return true | ||
}) | ||
return id + 1 | ||
} | ||
|
||
func (m *Manager) AddOrReplaceCredential(c credential.Credential) { | ||
if c.Type == credential.CredentialType_EMPTY { | ||
return | ||
} | ||
for { | ||
_, loaded := m.credentials.LoadOrStore(c.ID, c) | ||
if !loaded { | ||
return | ||
} | ||
c.ID = m.getNextID() | ||
} | ||
} | ||
|
||
func (m *Manager) AddOrReplaceCredentials(ca ...credential.Credential) { | ||
for _, c := range ca { | ||
m.AddOrReplaceCredential(c) | ||
} | ||
} | ||
|
||
func (m *Manager) RemoveRootCAs(ids ...int) { | ||
for _, id := range ids { | ||
m.credentials.Delete(id) | ||
} | ||
} | ||
|
||
func (m *Manager) ClearRootCAs() { | ||
_ = m.credentials.LoadAndDeleteAll() | ||
} | ||
|
||
func (m *Manager) getRep() credential.CredentialResponse { | ||
cas := m.credentials.CopyData() | ||
creds := credential.CredentialResponse{ | ||
Interfaces: []string{interfaces.OC_IF_BASELINE, interfaces.OC_IF_RW}, | ||
ResourceTypes: []string{credential.ResourceType}, | ||
Credentials: make([]credential.Credential, 0, len(cas)), | ||
} | ||
for _, cred := range cas { | ||
creds.Credentials = append(creds.Credentials, cred) | ||
} | ||
return creds | ||
} | ||
|
||
func (m *Manager) Get(request *net.Request) (*pool.Message, error) { | ||
creds := m.getRep() | ||
return resources.CreateResponseContent(request.Context(), creds, codes.Content) | ||
} | ||
|
||
func (m *Manager) Post(request *net.Request) (*pool.Message, error) { | ||
var cfg credential.CredentialUpdateRequest | ||
err := cbor.ReadFrom(request.Body(), &cfg) | ||
if err != nil { | ||
return resources.CreateResponseBadRequest(request.Context(), err) | ||
} | ||
m.AddOrReplaceCredentials(cfg.Credentials...) | ||
m.save() | ||
creds := m.getRep() | ||
return resources.CreateResponseContent(request.Context(), creds, codes.Changed) | ||
} | ||
|
||
func (m *Manager) ExportConfig() Config { | ||
return m.getRep() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.