Skip to content

Commit a8623d6

Browse files
authored
fix: base64 encoding mismatch for GET PKIOperation requests (#250)
EncodeSCEPRequest was using base64.URLEncoding (with - and _) but SCEP servers expect base64.StdEncoding (with + and /). This caused "illegal base64 data" errors when the client sent GET requests to servers that don't support POST. Fixes #249
1 parent 72f493e commit a8623d6

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

server/transport.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ func EncodeSCEPRequest(ctx context.Context, r *http.Request, request interface{}
5050
if len(req.Message) > 0 {
5151
var msg string
5252
if req.Operation == "PKIOperation" {
53-
msg = base64.URLEncoding.EncodeToString(req.Message)
53+
// Use standard base64 encoding (with + and /) as expected by SCEP servers.
54+
// The subsequent params.Encode() call will URL-encode the + and / characters.
55+
msg = base64.StdEncoding.EncodeToString(req.Message)
5456
} else {
5557
msg = string(req.Message)
5658
}

server/transport_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ func TestGetCACertMessage(t *testing.T) {
8585
}
8686
}
8787

88+
func TestEncodeSCEPRequest_PKIOperation_UsesStdBase64(t *testing.T) {
89+
// Data that encodes to "++++////" in standard base64
90+
testData := []byte{0xfb, 0xef, 0xbe, 0xff, 0xff, 0xff}
91+
92+
req, _ := http.NewRequest("GET", "http://example.com/scep", nil)
93+
scepserver.EncodeSCEPRequest(context.Background(), req, scepserver.SCEPRequest{
94+
Operation: "PKIOperation",
95+
Message: testData,
96+
})
97+
98+
// Verify message decodes correctly with StdEncoding (not URLEncoding)
99+
msg := req.URL.Query().Get("message")
100+
if _, err := base64.StdEncoding.DecodeString(msg); err != nil {
101+
t.Fatalf("message should be valid standard base64: %v", err)
102+
}
103+
}
104+
88105
func TestPKIOperation(t *testing.T) {
89106
server, _, teardown := newServer(t)
90107
defer teardown()

0 commit comments

Comments
 (0)