Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (c *Client) CACerts(ctx context.Context) ([]*x509.Certificate, error) {
return nil, err
}

if err := verifyResponseType(resp, mimeTypePKCS7, encodingTypeBase64); err != nil {
if err := verifyResponseType(resp, []string{mimeTypePKCS7}, encodingTypeBase64); err != nil {
return nil, err
}

Expand Down Expand Up @@ -181,7 +181,7 @@ func (c *Client) CSRAttrs(ctx context.Context) (CSRAttrs, error) {
return CSRAttrs{}, err
}

if err := verifyResponseType(resp, mimeTypeCSRAttrs, encodingTypeBase64); err != nil {
if err := verifyResponseType(resp, []string{mimeTypeCSRAttrs, mimeTypePKCS7}, encodingTypeBase64); err != nil {
return CSRAttrs{}, err
}

Expand Down Expand Up @@ -234,7 +234,7 @@ func (c *Client) enrollCommon(ctx context.Context, r *x509.CertificateRequest, r
return nil, err
}

if err := verifyResponseType(resp, mimeTypePKCS7, encodingTypeBase64); err != nil {
if err := verifyResponseType(resp, []string{mimeTypePKCS7}, encodingTypeBase64); err != nil {
return nil, err
}

Expand Down
14 changes: 8 additions & 6 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,20 @@ func verifyPartType(part *multipart.Part, ct, ce, pos string) error {

// verifyResponseType verifies if the content-type and content-transfer-encoding
// of an HTTP response are as expected. It returns a normal error and is intended
// to be used by client code.
func verifyResponseType(r *http.Response, t, e string) error {
// to be used by client code. Multiple acceptable content types may be provided;
// the response content type must match at least one of them.
func verifyResponseType(r *http.Response, t []string, e string) error {
ctype, _, err := mime.ParseMediaType(r.Header.Get(contentTypeHeader))
if err != nil {
return fmt.Errorf("missing or malformed %s header: %w", contentTypeHeader, err)
}

if !strings.HasPrefix(ctype, t) {
return fmt.Errorf("unexpected %s: %s", contentTypeHeader, ctype)
for _, accepted := range t {
if strings.HasPrefix(ctype, accepted) {
return nil
}
}

return nil
return fmt.Errorf("unexpected %s: %s", contentTypeHeader, ctype)
}

// verifyPartTypeResponse verifies if the content-type and content-transfer-encoding
Expand Down
44 changes: 39 additions & 5 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestVerifyResponseType(t *testing.T) {
var testcases = []struct {
name string
r *http.Response
t string
t []string
e string
err error
}{
Expand All @@ -40,7 +40,7 @@ func TestVerifyResponseType(t *testing.T) {
"Content-Transfer-Encoding": []string{"BasE64"},
},
},
t: "application/pkcs7",
t: []string{"application/pkcs7"},
e: "bASe64",
},
{
Expand All @@ -51,7 +51,7 @@ func TestVerifyResponseType(t *testing.T) {
"Content-Transfer-Encoding": []string{"base64"},
},
},
t: "application/pkcs10",
t: []string{"application/pkcs10"},
e: "base64",
err: errors.New("unexpected Content-Type: application/pkcs7"),
},
Expand All @@ -62,7 +62,7 @@ func TestVerifyResponseType(t *testing.T) {
"Content-Transfer-Encoding": []string{"base64"},
},
},
t: "application/pkcs7",
t: []string{"application/pkcs7"},
e: "base64",
err: errors.New("missing or malformed Content-Type header: mime: no media type"),
},
Expand All @@ -74,10 +74,44 @@ func TestVerifyResponseType(t *testing.T) {
"Content-Transfer-Encoding": []string{"base64"},
},
},
t: "application/pkcs7",
t: []string{"application/pkcs7"},
e: "base64",
err: errors.New("missing or malformed Content-Type header: mime: invalid media parameter"),
},
{
name: "CSRAttrsRFC7030",
r: &http.Response{
Header: http.Header{
"Content-Type": []string{"application/pkcs7-mime"},
"Content-Transfer-Encoding": []string{"base64"},
},
},
t: []string{"application/csrattrs", "application/pkcs7-mime"},
e: "base64",
},
{
name: "CSRAttrsNonStandard",
r: &http.Response{
Header: http.Header{
"Content-Type": []string{"application/csrattrs"},
"Content-Transfer-Encoding": []string{"base64"},
},
},
t: []string{"application/csrattrs", "application/pkcs7-mime"},
e: "base64",
},
{
name: "CSRAttrsWrongType",
r: &http.Response{
Header: http.Header{
"Content-Type": []string{"application/pkcs10"},
"Content-Transfer-Encoding": []string{"base64"},
},
},
t: []string{"application/csrattrs", "application/pkcs7-mime"},
e: "base64",
err: errors.New("unexpected Content-Type: application/pkcs10"),
},
}

for _, tc := range testcases {
Expand Down