• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / iot-manager / 1422858057

22 Aug 2024 08:03AM UTC coverage: 87.172% (-0.4%) from 87.577%
1422858057

Pull #298

gitlab-ci

alfrunes
refac(iotcore): Break on errors instead of falling through

Using long chains of fallthrough error conditions makes it very
difficult to read and error prone to extend. Refactoring to use common
coding patterns instead.

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #298: fix(iot-core): Incosistent serialization format for device private key

42 of 54 new or added lines in 2 files covered. (77.78%)

6 existing lines in 1 file now uncovered.

3255 of 3734 relevant lines covered (87.17%)

11.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.35
/crypto/csr.go
1
// Copyright 2022 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package crypto
16

17
import (
18
        "crypto/ecdsa"
19
        "crypto/elliptic"
20
        "crypto/rand"
21
        "crypto/x509"
22
        "crypto/x509/pkix"
23
        "encoding/pem"
24
        "flag"
25
        "fmt"
26
)
27

28
func NewPrivateKey() (*ecdsa.PrivateKey, error) {
2✔
29
        privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
2✔
30
        if err != nil {
2✔
31
                return nil, err
×
32
        }
×
33
        return privateKey, nil
2✔
34
}
35

36
func PrivateKeyToPem(privateKey *ecdsa.PrivateKey) ([]byte, error) {
1✔
37
        x509Encoded, err := x509.MarshalPKCS8PrivateKey(privateKey)
1✔
38
        if err != nil {
1✔
NEW
39
                return nil, fmt.Errorf("failed to serialize private key: %w", err)
×
NEW
40
        }
×
41
        pemEncoded := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: x509Encoded})
1✔
42
        return pemEncoded, nil
1✔
43
}
44

45
func NewCertificateSigningRequest(commonName string, key *ecdsa.PrivateKey) ([]byte, error) {
1✔
46
        subject := pkix.Name{
1✔
47
                Country:            []string{""},
1✔
48
                Province:           []string{""},
1✔
49
                Locality:           []string{""},
1✔
50
                Organization:       []string{""},
1✔
51
                OrganizationalUnit: []string{""},
1✔
52
                CommonName:         commonName,
1✔
53
        }
1✔
54

1✔
55
        template := x509.CertificateRequest{Subject: subject, DNSNames: flag.Args()}
1✔
56
        csr, err := x509.CreateCertificateRequest(rand.Reader, &template, key)
1✔
57
        if err != nil {
1✔
58
                return nil, err
×
59
        }
×
60
        csrPemBlock := &pem.Block{
1✔
61
                Type:  "CERTIFICATE REQUEST",
1✔
62
                Bytes: csr,
1✔
63
        }
1✔
64

1✔
65
        return pem.EncodeToMemory(csrPemBlock), nil
1✔
66
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc