Files
teleport/lib/darwin/pub_key.go
T
Alan Parra d218fd2625 Implement macOS device enrollment methods (#19079)
Implement native macOS methods required by device enrollment, namely methods to
Create/Get the device key, Sign challenges with it, and to collect device data
(aka serial number).

The implementation is rather similar to lib/auth/touchid, but simpler in a few
aspects:

1. Device keys don't require user interaction (as in they don't cause a touch ID
   prompt); and
2. There exists, at most, a single device key at all times.

I've added a tiny refactor to reuse Apple public key parsing from touchid, plus
some changes so touchid doesn't break in the face of unexpected keys (which the
device key didn't cause, per se, but my experiments getting to it did).

gravitational/teleport.e#514
2022-12-07 18:11:21 +00:00

56 lines
2.1 KiB
Go

// Copyright 2022 Gravitational, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package darwin
import (
"crypto/ecdsa"
"crypto/elliptic"
"fmt"
"math/big"
)
// ECDSAPublicKeyFromRaw reads an ECDSA public key from a raw Apple public key,
// as returned by SecKeyCopyExternalRepresentation.
func ECDSAPublicKeyFromRaw(pubKeyRaw []byte) (*ecdsa.PublicKey, error) {
// Verify key length to avoid a potential panic below.
// 3 is the smallest number that clears it, but in practice 65 is the more
// common length.
// Apple's docs make no guarantees, hence no assumptions are made here.
switch l := len(pubKeyRaw); {
case l < 3:
return nil, fmt.Errorf("public key representation too small (%v bytes)", l)
case l%2 != 1: // 0x4+keyLen+keyLen is always odd, see explanation below.
return nil, fmt.Errorf("public key representation has unexpected length (%v bytes)", l)
case pubKeyRaw[0] != 0x04: // See explanation below.
return nil, fmt.Errorf("public key representation starts with unexpected byte (%#x vs 0x4)", pubKeyRaw[0])
}
// "For an elliptic curve public key, the format follows the ANSI X9.63
// standard using a byte string of 04 || X || Y. (...) All of these
// representations use constant size integers, including leading zeros as
// needed."
// https://developer.apple.com/documentation/security/1643698-seckeycopyexternalrepresentation?language=objc
pubKeyRaw = pubKeyRaw[1:] // skip 0x4
l := len(pubKeyRaw) / 2
x := pubKeyRaw[:l]
y := pubKeyRaw[l:]
return &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: (&big.Int{}).SetBytes(x),
Y: (&big.Int{}).SetBytes(y),
}, nil
}