-
Notifications
You must be signed in to change notification settings - Fork 23
/
client.go
173 lines (149 loc) · 5.07 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package rsa_signingservice
import (
"bytes"
"context"
"crypto"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/credentials/identity/hostpath"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/signing"
"github.com/open-component-model/ocm/pkg/signing/handlers/rsa"
)
const (
AcceptHeader = "Accept"
// MediaTypePEM defines the media type for PEM formatted data.
MediaTypePEM = "application/x-pem-file"
)
type SigningServerSigner struct {
ServerURL *url.URL
}
func NewSigningClient(serverURL string) (*SigningServerSigner, error) {
u, err := url.Parse(serverURL)
if err != nil {
return nil, errors.Wrapf(err, "invalid signing server URL (%q)", serverURL)
}
signer := SigningServerSigner{
ServerURL: u,
}
return &signer, nil
}
func (signer *SigningServerSigner) Sign(cctx credentials.Context, signatureAlgo string, hashAlgo crypto.Hash, digest string, issuer string, key interface{}) (*signing.Signature, error) {
decodedHash, err := hex.DecodeString(digest)
if err != nil {
return nil, fmt.Errorf("unable to hex decode hash: %w", err)
}
u := *signer.ServerURL
if !strings.HasSuffix(u.Path, "/") {
u.Path += "/"
}
u.Path += "sign/" + strings.ToLower(signatureAlgo)
q := u.Query()
q.Set("hashAlgorithm", hashAlgo.String())
u.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(
context.Background(),
http.MethodPost,
u.String(),
bytes.NewBuffer(decodedHash),
)
if err != nil {
return nil, fmt.Errorf("unable to build http request: %w", err)
}
req.Header.Add(AcceptHeader, MediaTypePEM)
// TODO: split up signing server url into protocol, host, and port for matching?
consumerId := credentials.ConsumerIdentity{
credentials.ID_TYPE: CONSUMER_TYPE,
ID_HOSTNAME: signer.ServerURL.Hostname(),
ID_PORT: signer.ServerURL.Port(),
ID_SCHEME: signer.ServerURL.Scheme,
ID_PATHPREFIX: signer.ServerURL.Path,
}
credSource, err := cctx.GetCredentialsForConsumer(consumerId, hostpath.Matcher)
if err != nil && !errors.IsErrUnknown(err) {
return nil, fmt.Errorf("unable to get credential source: %w", err)
}
var caCertPool *x509.CertPool
var clientCerts []tls.Certificate
if credSource != nil {
cred, err := credSource.Credentials(cctx)
if err != nil {
return nil, fmt.Errorf("unable to get credentials from credential source: %w", err)
}
if !cred.ExistsProperty(CLIENT_CERT) {
return nil, fmt.Errorf("credential for consumer %+v is missing property %q", consumerId, CLIENT_CERT)
}
if !cred.ExistsProperty(PRIVATE_KEY) {
return nil, fmt.Errorf("credential for consumer %+v is missing property %q", consumerId, PRIVATE_KEY)
}
rawClientCert := []byte(cred.GetProperty(CLIENT_CERT))
rawPrivateKey := []byte(cred.GetProperty(PRIVATE_KEY))
clientCert, err := tls.X509KeyPair(rawClientCert, rawPrivateKey)
if err != nil {
return nil, fmt.Errorf("unable to build client certificate: %w", err)
}
clientCerts = append(clientCerts, clientCert)
if cred.ExistsProperty(CA_CERTS) {
caCertPool = x509.NewCertPool()
rawCaCerts := []byte(cred.GetProperty(CA_CERTS))
if ok := caCertPool.AppendCertsFromPEM(rawCaCerts); !ok {
return nil, fmt.Errorf("unable to append ca certificates to cert pool")
}
}
}
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS13,
RootCAs: caCertPool,
Certificates: clientCerts,
},
},
}
res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("unable to send http request: %w", err)
}
defer res.Body.Close()
responseBodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("unable to read response body: %w", err)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request returned with status code %d: %s", res.StatusCode, string(responseBodyBytes))
}
signaturePemBlocks, err := rsa.GetSignaturePEMBlocks(responseBodyBytes)
if err != nil {
return nil, fmt.Errorf("unable to get signature pem block from response: %w", err)
}
if len(signaturePemBlocks) != 1 {
return nil, fmt.Errorf("expected 1 signature pem block, found %d", len(signaturePemBlocks))
}
signatureBlock := signaturePemBlocks[0]
signature := signatureBlock.Bytes
if len(signature) == 0 {
return nil, errors.New("invalid response: signature block doesn't contain signature")
}
algorithm := signatureBlock.Headers[rsa.SignaturePEMBlockAlgorithmHeader]
if algorithm == "" {
return nil, fmt.Errorf("invalid response: %s header is empty: %s", rsa.SignaturePEMBlockAlgorithmHeader, string(responseBodyBytes))
}
encodedSignature := pem.EncodeToMemory(signatureBlock)
return &signing.Signature{
Value: string(encodedSignature),
MediaType: MediaTypePEM,
Algorithm: algorithm,
Issuer: issuer,
}, nil
}