|
| 1 | +// Copyright 2024 Yubico AB |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). |
| 4 | +// You may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Security.Cryptography; |
| 17 | + |
| 18 | +namespace Yubico.YubiKey.Cryptography; |
| 19 | + |
| 20 | +/// <summary> |
| 21 | +/// Represents the parameters for an Elliptic Curve (EC) public key. |
| 22 | +/// </summary> |
| 23 | +/// <remarks> |
| 24 | +/// This class encapsulates the parameters specific to EC public keys, |
| 25 | +/// ensuring that the key only contains necessary public key components. |
| 26 | +/// </remarks> |
| 27 | +public class ECPublicKey : PublicKey |
| 28 | +{ |
| 29 | + private readonly byte[] _publicPointBytes; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Gets the key definition associated with this RSA private key. |
| 33 | + /// </summary> |
| 34 | + /// <value> |
| 35 | + /// A <see cref="KeyDefinition"/> object that describes the key's properties, including its type and length. |
| 36 | + /// </value> |
| 37 | + public KeyDefinition KeyDefinition { get; } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets the Elliptic Curve parameters associated with this instance. |
| 41 | + /// </summary> |
| 42 | + /// <value> |
| 43 | + /// An <see cref="ECParameters"/> structure containing the curve parameters, key, and other |
| 44 | + /// cryptographic elements needed for EC operations. |
| 45 | + /// </value> |
| 46 | + public ECParameters Parameters { get; } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Gets the bytes representing the public key coordinates. |
| 50 | + /// </summary> |
| 51 | + /// <returns>A <see cref="ReadOnlyMemory{T}"/> containing the public key bytes with the format 0x04 || X || Y.</returns> |
| 52 | + public ReadOnlyMemory<byte> PublicPoint => _publicPointBytes; |
| 53 | + |
| 54 | + /// <inheritdoc /> |
| 55 | + public override KeyType KeyType => KeyDefinition.KeyType; |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Initializes a new instance of the <see cref="ECPublicKey"/> class. |
| 59 | + /// It is a wrapper for the <see cref="ECParameters"/> class. |
| 60 | + /// </summary> |
| 61 | + /// <remarks> |
| 62 | + /// This constructor is used to create an instance from a <see cref="ECParameters"/> object. |
| 63 | + /// It will deep copy the parameters from the <see cref="ECParameters"/> object. |
| 64 | + /// </remarks> |
| 65 | + /// <param name="parameters"></param> |
| 66 | + /// <exception cref="ArgumentException">Thrown when the parameters contain private key data (D value).</exception> |
| 67 | + protected ECPublicKey(ECParameters parameters) |
| 68 | + { |
| 69 | + if (parameters.D != null) |
| 70 | + { |
| 71 | + throw new ArgumentException( |
| 72 | + "Parameters must not contain private key data (D value)", nameof(parameters)); |
| 73 | + } |
| 74 | + |
| 75 | + Parameters = parameters.DeepCopy(); |
| 76 | + KeyDefinition = KeyDefinitions.GetByOid(Parameters.Curve.Oid); |
| 77 | + |
| 78 | + // Format identifier (uncompressed point): 0x04 |
| 79 | + _publicPointBytes = [0x4, .. Parameters.Q.X, .. Parameters.Q.Y]; |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Initializes a new instance of the <see cref="ECPublicKey"/> class. |
| 84 | + /// </summary> |
| 85 | + /// <param name="ecdsa"></param> |
| 86 | + protected ECPublicKey(ECDsa ecdsa) |
| 87 | + { |
| 88 | + if (ecdsa == null) |
| 89 | + { |
| 90 | + throw new ArgumentNullException(nameof(ecdsa)); |
| 91 | + } |
| 92 | + |
| 93 | + Parameters = ecdsa.ExportParameters(false); |
| 94 | + KeyDefinition = KeyDefinitions.GetByOid(Parameters.Curve.Oid); |
| 95 | + |
| 96 | + // Format identifier (uncompressed point): 0x04 |
| 97 | + _publicPointBytes = [0x4, .. Parameters.Q.X, .. Parameters.Q.Y]; |
| 98 | + } |
| 99 | + |
| 100 | + /// <inheritdoc /> |
| 101 | + public override byte[] ExportSubjectPublicKeyInfo() => AsnPublicKeyEncoder.EncodeToSubjectPublicKeyInfo(Parameters); |
| 102 | + |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Creates an instance of <see cref="ECPublicKey"/> from the given <paramref name="parameters"/>. |
| 106 | + /// </summary> |
| 107 | + /// <param name="parameters">The parameters to create the key from.</param> |
| 108 | + /// <returns>An instance of <see cref="ECPublicKey"/>.</returns> |
| 109 | + public static ECPublicKey CreateFromParameters(ECParameters parameters) => new(parameters); |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Creates an instance of <see cref="ECPublicKey"/> from the given |
| 113 | + /// <paramref name="publicPoint"/> and <paramref name="keyType"/>. |
| 114 | + /// </summary> |
| 115 | + /// <param name="publicPoint">The raw public key data, formatted as an compressed point.</param> |
| 116 | + /// <param name="keyType">The type of key this is.</param> |
| 117 | + /// <returns>An instance of <see cref="ECPublicKey"/>.</returns> |
| 118 | + /// <exception cref="ArgumentException"> |
| 119 | + /// Thrown if the key type is not a valid EC key. |
| 120 | + /// </exception> |
| 121 | + public static IPublicKey CreateFromValue(ReadOnlyMemory<byte> publicPoint, KeyType keyType) |
| 122 | + { |
| 123 | + var keyDefinition = KeyDefinitions.GetByKeyType(keyType); |
| 124 | + if (keyDefinition.AlgorithmOid is not Oids.ECDSA) |
| 125 | + { |
| 126 | + throw new ArgumentException("Only P-256, P-384 and P-521 are supported.", nameof(keyType)); |
| 127 | + } |
| 128 | + |
| 129 | + int coordinateLength = keyDefinition.LengthInBytes; |
| 130 | + var curve = ECCurve.CreateFromValue(keyDefinition.CurveOid); |
| 131 | + var ecParameters = new ECParameters |
| 132 | + { |
| 133 | + Curve = curve, |
| 134 | + Q = new ECPoint |
| 135 | + { |
| 136 | + X = publicPoint.Slice(1, coordinateLength).ToArray(), |
| 137 | + Y = publicPoint.Slice(coordinateLength + 1, coordinateLength).ToArray() |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + return CreateFromParameters(ecParameters); |
| 142 | + } |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Creates an instance of <see cref="IPublicKey"/> from a DER-encoded public key. |
| 146 | + /// </summary> |
| 147 | + /// <param name="encodedKey">The DER-encoded public key.</param> |
| 148 | + /// <returns>An instance of <see cref="IPublicKey"/>.</returns> |
| 149 | + /// <exception cref="CryptographicException"> |
| 150 | + /// Thrown if the public key is invalid. |
| 151 | + /// </exception> |
| 152 | + public static IPublicKey CreateFromPkcs8(ReadOnlyMemory<byte> encodedKey) => |
| 153 | + AsnPublicKeyDecoder.CreatePublicKey(encodedKey); |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Gets the bytes representing the public key coordinates. |
| 157 | + /// </summary> |
| 158 | + /// <returns>A <see cref="ReadOnlyMemory{T}"/> containing the public key bytes with the format 0x04 || X || Y.</returns> |
| 159 | + [Obsolete("Use PublicPoint instead")] |
| 160 | + public ReadOnlyMemory<byte> GetBytes() => _publicPointBytes; |
| 161 | +} |
0 commit comments