Skip to content

Commit 6b602bc

Browse files
authored
fix(cryptosctl): accept any serial form in ca revoke (#311)
* fix(cryptosctl): accept any serial form in ca revoke RevokeCertificate looked the serial up exactly as typed, while the node stores serials as lower-case hex with no leading zeros. A serial copied from openssl x509 -serial, which prints upper case, was refused with a message claiming the certificate was not issued by this node. Normalise the serial with the same function GetIssuedCertificate uses, so case, leading zeros, a 0x prefix and colons no longer matter. A serial that is not hex is InvalidArgument, and an unknown one says it was not found and shows the normalised value that was looked up. Signed-off-by: Bugs5382 <12115015+Bugs5382@users.noreply.github.com> * docs(revocation): list the serial forms ca revoke accepts and its errors Signed-off-by: Bugs5382 <12115015+Bugs5382@users.noreply.github.com> --------- Signed-off-by: Bugs5382 <12115015+Bugs5382@users.noreply.github.com> Co-authored-by: Bugs5382 <12115015+Bugs5382@users.noreply.github.com>
1 parent 7ce4811 commit 6b602bc

4 files changed

Lines changed: 84 additions & 8 deletions

File tree

‎cmd/cryptosctl/revocation.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func newRevokeCmd(opts *globalOpts) *cobra.Command {
6363
return writeRevocations(cmd.OutOrStdout(), []*cryptosv1.Revocation{resp.GetRevocation()}, opts.output)
6464
},
6565
}
66-
cmd.Flags().StringVar(&serial, "serial", "", "hex serial of the certificate to revoke (required)")
66+
cmd.Flags().StringVar(&serial, "serial", "", "hex serial of the certificate to revoke; case, leading zeros, a 0x prefix and colons are ignored (required)")
6767
cmd.Flags().IntVar(&reason, "reason", 0, "RFC 5280 CRL reason code (0 = unspecified)")
6868
return cmd
6969
}

‎docs/issued-certificates.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,15 @@ cryptosctl --endpoint pki-issuing.example:443 ca revocations
6969
7070
`--reason` is an RFC 5280 CRL reason code. The node records the revocation
7171
and rebuilds its CRL. OCSP answers from the same store.
72+
73+
`--serial` accepts the same spellings as `get-issued`: upper or lower case,
74+
leading zeros, a `0x` prefix and colon-separated bytes, so a serial copied
75+
from `openssl x509 -serial` works as it is. The node normalises it to the
76+
stored form (lower case, no leading zeros) before the lookup.
77+
78+
Errors:
79+
80+
- **`NotFound`:** no certificate this node issued has that serial. The
81+
message shows the normalised serial the node looked up, for example
82+
`serial "4f1a09c2" not found among the certificates this node issued`.
83+
- **`InvalidArgument`:** the serial is not hex.

‎internal/grpc/server.go‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -733,9 +733,10 @@ func (s *Server) SubmitRenewedCertificate(ctx context.Context, req *cryptosv1.Su
733733
// a certificate this node issued (identified by its hex serial) as revoked and
734734
// refreshes the published CRL. The maintenance servers leave Revoker nil, so the
735735
// RPC returns Unimplemented there. On a running node the caller is authorized
736-
// against the bootstrap admin trust before any state changes. A serial this node
737-
// never issued surfaces as NotFound (revocation.ErrNotIssued); the revoke is
738-
// idempotent, so re-revoking a serial returns the original record.
736+
// against the bootstrap admin trust before any state changes. The serial is
737+
// normalised to the stored form the way GetIssuedCertificate does it. A serial
738+
// this node never issued surfaces as NotFound (revocation.ErrNotIssued); the
739+
// revoke is idempotent, so re-revoking a serial returns the original record.
739740
func (s *Server) RevokeCertificate(ctx context.Context, req *cryptosv1.RevokeCertificateRequest) (*cryptosv1.RevokeCertificateResponse, error) {
740741
if s.cfg.Revoker == nil {
741742
return nil, status.Error(codes.Unimplemented, "revocation is not available in maintenance mode")
@@ -746,10 +747,14 @@ func (s *Server) RevokeCertificate(ctx context.Context, req *cryptosv1.RevokeCer
746747
if req == nil || req.GetSerialHex() == "" {
747748
return nil, status.Error(codes.InvalidArgument, "RevokeCertificate: serial_hex is required")
748749
}
749-
rev, err := s.cfg.Revoker.Revoke(ctx, req.GetSerialHex(), int(req.GetReasonCode()))
750+
serial, ok := canonicalSerialHex(req.GetSerialHex())
751+
if !ok {
752+
return nil, status.Errorf(codes.InvalidArgument, "RevokeCertificate: serial_hex %q is not a hex serial", req.GetSerialHex())
753+
}
754+
rev, err := s.cfg.Revoker.Revoke(ctx, serial, int(req.GetReasonCode()))
750755
if err != nil {
751756
if errors.Is(err, revocation.ErrNotIssued) {
752-
return nil, status.Errorf(codes.NotFound, "RevokeCertificate: serial %q was not issued by this node", req.GetSerialHex())
757+
return nil, status.Errorf(codes.NotFound, "RevokeCertificate: serial %q not found among the certificates this node issued", serial)
753758
}
754759
return nil, err
755760
}

‎internal/grpc/server_test.go‎

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"math/big"
3333
"net"
3434
"slices"
35+
"strings"
3536
"sync"
3637
"testing"
3738
"time"
@@ -1221,8 +1222,66 @@ func TestRevokeCertificate_KnownSerialOK(t *testing.T) {
12211222
if resp.GetRevocation().GetSerialHex() != "0a" || resp.GetRevocation().GetReasonCode() != 4 {
12221223
t.Fatalf("revocation = %v", resp.GetRevocation())
12231224
}
1224-
if rev.gotSerial != "0a" || rev.gotReason != 4 {
1225-
t.Fatalf("revoker got serial=%q reason=%d", rev.gotSerial, rev.gotReason)
1225+
if rev.gotSerial != "a" || rev.gotReason != 4 {
1226+
t.Fatalf("revoker got serial=%q reason=%d, want the stored form a", rev.gotSerial, rev.gotReason)
1227+
}
1228+
}
1229+
1230+
func newRevokeServer(t *testing.T, rev *fakeRevoker) *Server {
1231+
t.Helper()
1232+
srv, err := New(ServerConfig{
1233+
TLSConfig: newFixtures(t).serverConf,
1234+
Auditor: &mockAuditor{},
1235+
Revoker: rev,
1236+
})
1237+
if err != nil {
1238+
t.Fatalf("New: %v", err)
1239+
}
1240+
return srv
1241+
}
1242+
1243+
// A serial copied from openssl (upper case, often colon-separated), from a
1244+
// 0x-prefixed tool, or zero-padded to whole bytes names the same certificate
1245+
// as the node's stored lower-case form.
1246+
func TestRevokeCertificate_NormalisesTheSerial(t *testing.T) {
1247+
for _, in := range []string{"a1b", "A1B", "0a1b", "000A1B", "0x0a1b", "0X0A1B", "0a:1b", "0A:1B", " 0A:1B "} {
1248+
rev := &fakeRevoker{revocation: &cryptosv1.Revocation{SerialHex: "a1b"}}
1249+
if _, err := newRevokeServer(t, rev).RevokeCertificate(context.Background(), &cryptosv1.RevokeCertificateRequest{SerialHex: in, ReasonCode: 1}); err != nil {
1250+
t.Fatalf("RevokeCertificate(%q): %v", in, err)
1251+
}
1252+
if rev.gotSerial != "a1b" {
1253+
t.Errorf("serial %q reached the revoker as %q, want a1b", in, rev.gotSerial)
1254+
}
1255+
}
1256+
}
1257+
1258+
// An unknown serial says it was not found and names the normalised serial
1259+
// that was looked up, so the operator can see what was compared.
1260+
func TestRevokeCertificate_UnknownSerialNamesTheNormalisedValue(t *testing.T) {
1261+
rev := &fakeRevoker{notIssued: "a1b"}
1262+
_, err := newRevokeServer(t, rev).RevokeCertificate(context.Background(), &cryptosv1.RevokeCertificateRequest{SerialHex: "0A:1B", ReasonCode: 1})
1263+
if status.Code(err) != codes.NotFound {
1264+
t.Fatalf("code = %v, want NotFound", status.Code(err))
1265+
}
1266+
msg := status.Convert(err).Message()
1267+
if !strings.Contains(msg, "not found") || !strings.Contains(msg, `"a1b"`) {
1268+
t.Errorf("message = %q, want it to say not found and show a1b", msg)
1269+
}
1270+
if strings.Contains(msg, "not issued by this node") {
1271+
t.Errorf("message = %q still claims the serial was not issued by this node", msg)
1272+
}
1273+
}
1274+
1275+
func TestRevokeCertificate_RejectsABadSerial(t *testing.T) {
1276+
for _, in := range []string{"xyz", "0x", "-1", ":"} {
1277+
rev := &fakeRevoker{}
1278+
_, err := newRevokeServer(t, rev).RevokeCertificate(context.Background(), &cryptosv1.RevokeCertificateRequest{SerialHex: in, ReasonCode: 1})
1279+
if status.Code(err) != codes.InvalidArgument {
1280+
t.Errorf("serial %q: code = %v, want InvalidArgument", in, status.Code(err))
1281+
}
1282+
if rev.gotSerial != "" {
1283+
t.Errorf("serial %q: the revoker was consulted", in)
1284+
}
12261285
}
12271286
}
12281287

0 commit comments

Comments
 (0)