Skip to content
Open
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
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
SLH-DSA (smallstep/certinfo#69).
- Show the enrollment time in the `Step Managed Device` extension of a
certificate when it is present (smallstep/certinfo#96).
- Add support for ML-DSA keys (ML-DSA-44, ML-DSA-65, and ML-DSA-87) in the
`step crypto jwk`, `step crypto jws`, and `step crypto jwt` commands, using
the `AKP` key type (smallstep/cli#1742).
- Add support for creating JWK provisioners that use ML-DSA keys
(smallstep/cli#1742).
- Infer the key type and use from `--alg` in `step crypto jwk create` when
`--kty` is not set, for example `--alg ML-DSA-44` (smallstep/cli#1742).
- Allow `step crypto jwk public` and `step crypto jwk thumbprint` to read the
JWK from a file passed as an argument, in addition to STDIN
(smallstep/cli#1742).

### Changed

Expand Down Expand Up @@ -46,6 +56,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Keep the original protected header in `step crypto jose format`, reordering
its fields invalidated the signature or authentication tag of the token
(smallstep/cli#1732).
- Reject JSON JWEs with additional authenticated data in
`step crypto jose format` when converting to the compact serialization, which
cannot carry it. The command used to print a token that could not be decrypted
(smallstep/cli#1742).
- Fix the error of `step crypto keypair` and other key commands when `--curve`
is used with an ML-DSA key type, it named `--size` as the incompatible flag
(smallstep/cli#1742).
- Fix the use of ML-DSA keys in a KMS to sign tokens, which failed with
`unsupported key type *mldsa.PublicKey` (smallstep/cli#1742).

### Security

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Step CLI's command groups illustrate its wide-ranging uses:
- Create, revoke, validate, lint, and bundle X.509 certificates.
- Install (and remove) X.509 certificates into your system's (and browser's) trust store.
- Validate certificate deployment and renewal status for automation
- Create key pairs (RSA, ECDSA, EdDSA) and certificate signing requests (CSRs)
- Create key pairs (RSA, ECDSA, EdDSA, ML-DSA) and certificate signing requests (CSRs)
- [Sign CSRs](https://smallstep.com/docs/step-cli/reference/certificate/sign/)
- Create [RFC5280](https://tools.ietf.org/html/rfc5280) and [CA/Browser Forum](https://cabforum.org/baseline-requirements-documents/)-compliant certificates that work for TLS and HTTPS
- [Create](https://smallstep.com/docs/step-cli/reference/certificate/create/) CA certificates (root and intermediate signing certificates)
Expand Down
25 changes: 19 additions & 6 deletions command/crypto/jose/jose.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"io"
"os"
"regexp"
"strings"
"unicode"

"github.com/pkg/errors"
"github.com/urfave/cli"
Expand Down Expand Up @@ -80,6 +80,12 @@ func formatAction(*cli.Context) error {
// If it fails parse a regular JWS
var srz serializer
if enc, err := joseutil.ParseEncryptedInsecure(token); err == nil {
// The compact serialization cannot carry the additional authenticated
// data, and go-jose would drop it, which makes the token impossible to
// decrypt.
if strings.HasPrefix(token, "{") && len(enc.GetAuthData()) > 0 {
return errors.New("error serializing data: compact serialization does not support additional authenticated data (aad)")
}
srz = enc
} else {
tok, err := joseutil.ParseSignedInsecure(token)
Expand Down Expand Up @@ -117,8 +123,10 @@ func withCompactProtected(compact, token string) string {
return compact
}

// Like go-jose, ignore the flattened members if there are signatures. There
// is only one at this point, the compact serialization fails otherwise.
protected := jws.Protected
if protected == "" && len(jws.Signatures) == 1 {
if len(jws.Signatures) > 0 {
protected = jws.Signatures[0].Protected
}
_, rest, ok := strings.Cut(compact, ".")
Expand All @@ -144,9 +152,14 @@ func withJSONProtected(full, token string) string {
`"protected":"`+protected+`"`, 1)
}

var stripWhitespaceRegex = regexp.MustCompile(`\s`)

// stripWhitespace strip all newlines and whitespace
// stripWhitespace strips all newlines and whitespace. It removes the same
// characters as the parsers, the format of the output and the protected header
// to keep are taken from the result.
func stripWhitespace(data string) string {
return stripWhitespaceRegex.ReplaceAllString(data, "")
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, data)
}
105 changes: 86 additions & 19 deletions command/crypto/jwk/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package jwk

import (
"bytes"
"crypto"
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
Expand Down Expand Up @@ -130,6 +128,12 @@ Create an RSA JWK:
$ step crypto jwk create rsa.pub.json rsa.json --kty RSA
'''

Create a JWK with an ML-DSA-44 key:

'''
$ step crypto jwk create mldsa.pub.json mldsa.json --alg ML-DSA-44
'''

Create a symmetric key (oct key type):

'''
Expand Down Expand Up @@ -176,14 +180,17 @@ If unset, default is EC.
**EC**
: Create an **elliptic curve** keypair

**oct**
: Create a **symmetric key** (octet stream)
**RSA**
: Create an **RSA** keypair

**OKP**
: Create an octet key pair (for **"Ed25519"** curve)

**RSA**
: Create an **RSA** keypair
**AKP**
: Create an **ML-DSA-44**, **ML-DSA-65**, or **ML-DSA-87** keypair

**oct**
: Create a **symmetric key** (octet stream)
`,
},
cli.IntFlag{
Expand All @@ -205,14 +212,15 @@ depends on the key use, key type, and curve (for EC and OKP keys). Defaults are:
| EC | sig | P-256 | ES256 |
| EC | sig | P-384 | ES384 |
| EC | sig | P-521 | ES512 |
| oct | sig | N/A | HS256 |
| RSA | sig | N/A | RS256 |
| OKP | sig | Ed25519 | EdDSA |
| AKP | sig | N/A | ML-DSA-44 |
| oct | sig | N/A | HS256 |
| EC | enc | P-256 | ECDH-ES |
| EC | enc | P-384 | ECDH-ES |
| EC | enc | P-521 | ECDH-ES |
| oct | enc | N/A | A256GCMKW |
| RSA | enc | N/A | RSA-OAP-256 |
| oct | enc | N/A | A256GCMKW |

: If the key **"use"** is **"sig"** (signing) <algorithm> must be one of:

Expand Down Expand Up @@ -255,6 +263,15 @@ depends on the key use, key type, and curve (for EC and OKP keys). Defaults are:
**EdDSA**
: EdDSA signature algorithm

**ML-DSA-44**
: ML-DSA-44 signature algorithm (FIPS 204, RFC 9964)

**ML-DSA-65**
: ML-DSA-65 signature algorithm (FIPS 204, RFC 9964)

**ML-DSA-87**
: ML-DSA-87 signature algorithm (FIPS 204, RFC 9964)

: If the key **"use"** is **"enc"** (encryption) <algorithm> must be one of:

**RSA1_5**
Expand Down Expand Up @@ -436,12 +453,22 @@ func createAction(ctx *cli.Context) (err error) {
size := ctx.Int("size")
pemFile := ctx.String("from-pem")

// Get the kty and use if --alg is passed:
// step crypto jwk create --alg ML-DSA-44 ml.pub ml.priv
if alg != "" && !ctx.IsSet("kty") {
var algUse string
kty, algUse = getKeyTypeAndUseFromAlgorithm(alg, kty)
if !ctx.IsSet("use") {
use = algUse
}
}

switch kty {
case "EC":
case jose.EC:
if ctx.IsSet("size") {
return errs.IncompatibleFlag(ctx, "size", "--kty EC")
}
case "RSA":
case jose.RSA:
if ctx.IsSet("crv") {
return errs.IncompatibleFlag(ctx, "crv", "--kty RSA")
}
Expand All @@ -455,11 +482,18 @@ func createAction(ctx *cli.Context) (err error) {
return errs.MinSizeFlag(ctx, "size", "1")
}
}
case "OKP":
case jose.OKP:
if ctx.IsSet("size") {
return errs.IncompatibleFlag(ctx, "size", "--kty OKP")
}
case "oct":
case jose.AKP:
switch {
case ctx.IsSet("size"):
return errs.IncompatibleFlag(ctx, "size", "--kty AKP")
case ctx.IsSet("crv"):
return errs.IncompatibleFlag(ctx, "crv", "--kty AKP")
}
case jose.OCT:
if ctx.IsSet("crv") {
return errs.IncompatibleFlag(ctx, "crv", "--kty oct")
}
Expand Down Expand Up @@ -498,7 +532,7 @@ func createAction(ctx *cli.Context) (err error) {
}
}
default:
return errs.InvalidFlagValue(ctx, "kty", kty, "EC, RSA, OKP, or oct")
return errs.InvalidFlagValue(ctx, "kty", kty, "EC, RSA, OKP, AKP, or oct")
}

// Generate or read secrets
Expand All @@ -517,13 +551,9 @@ func createAction(ctx *cli.Context) (err error) {
if ctx.IsSet("kid") {
jwk.KeyID = ctx.String("kid")
} else if kty != "oct" {
// A hash of a symmetric key can leak information, so we only thumbprint asymmetric keys.
var hash []byte
hash, err = jwk.Thumbprint(crypto.SHA256)
if err != nil {
return errors.Wrap(err, "error generating JWK thumbprint")
if jwk.KeyID, err = jose.Thumbprint(jwk); err != nil {
return err
}
jwk.KeyID = base64.RawURLEncoding.EncodeToString(hash)
}
jwk.Use = use

Expand Down Expand Up @@ -659,3 +689,40 @@ func aesKeySize(alg string) int {
return 0
}
}

func getKeyTypeAndUseFromAlgorithm(alg, def string) (string, string) {
const (
sig = "sig"
enc = "enc"
)

// Signature algorithms
switch alg {
case jose.ES256, jose.ES384, jose.ES512:
return jose.EC, sig
case jose.RS256, jose.RS384, jose.RS512:
return jose.RSA, sig
case jose.PS256, jose.PS384, jose.PS512:
return jose.RSA, sig
case jose.EdDSA, jose.XEdDSA:
return jose.OKP, sig
case jose.MLDSA44, jose.MLDSA65, jose.MLDSA87:
return jose.AKP, sig
case jose.HS256, jose.HS384, jose.HS512:
return jose.OCT, sig
}

// Encryption algorithms
switch jose.KeyAlgorithm(alg) {
case jose.ECDH_ES, jose.ECDH_ES_A128KW, jose.ECDH_ES_A192KW, jose.ECDH_ES_A256KW:
return jose.EC, enc
case jose.RSA1_5, jose.RSA_OAEP, jose.RSA_OAEP_256:
return jose.RSA, enc
case jose.DIRECT,
jose.A128GCMKW, jose.A192GCMKW, jose.A256GCMKW,
jose.A128KW, jose.A192KW, jose.A256KW:
return jose.OCT, enc
}

return def, sig
}
26 changes: 25 additions & 1 deletion command/crypto/jwk/jwk.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package jwk

import "github.com/urfave/cli"
import (
"github.com/urfave/cli"

"github.com/smallstep/cli-utils/errs"

"github.com/smallstep/cli/utils"
)

// Command returns the jwk subcommand.
func Command() cli.Command {
Expand Down Expand Up @@ -93,3 +99,21 @@ L38TOXsig8h6FeBOos03nFy6iXmwusFcIBBB0ZilahY
},
}
}

func readFromStdInOrFile(ctx *cli.Context) ([]byte, error) {
if err := errs.MinMaxNumberOfArguments(ctx, 0, 1); err != nil {
return nil, err
}

var name string
switch ctx.NArg() {
case 0:
name = "-"
case 1:
name = ctx.Args().First()
default:
return nil, errs.TooManyArguments(ctx)
}

return utils.ReadFile(name)
}
19 changes: 11 additions & 8 deletions command/crypto/jwk/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package jwk
import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/pkg/errors"
"github.com/urfave/cli"
Expand All @@ -18,18 +16,23 @@ func publicCommand() cli.Command {
Name: "public",
Action: cli.ActionFunc(publicAction),
Usage: "extract a public JSON Web Key (JWK) from a private JWK",
UsageText: `**step crypto jwk public**`,
Description: `**step crypto jwk public** command reads a JWK from STDIN, derives the
corresponding public JWK, and prints the derived JWK to STDOUT.
UsageText: `**step crypto jwk public** [<jwk-file>]`,
Description: `**step crypto jwk public** command reads a JWK from <jwk-file> or STDIN,
derives the corresponding public JWK, and prints the derived JWK to STDOUT.

## POSITIONAL ARGUMENTS

<jwk-file>
: Path to a JWK. If it is not set, the JWK is read from STDIN.

For examples, see **step help crypto jwk**.`,
}
}

func publicAction(*cli.Context) error {
b, err := io.ReadAll(os.Stdin)
func publicAction(ctx *cli.Context) error {
b, err := readFromStdInOrFile(ctx)
if err != nil {
return errors.Wrap(err, "error reading from STDIN")
return err
}

jwk := new(jose.JSONWebKey)
Expand Down
Loading