You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To build a PrivateKey when importing from PEM, you will need to first determine the private key algorithm. Once you know the algorithm, you can use the appropriate C# class to read the encoded data (for example, ``RSAPrivateKey``, ``ECPrivateKey``, or ``Curve25519PrivateKey`` ).
189
189
190
-
The algorithm is specified in the key data itself. However, the .NET Base Class Library does not have a class that can parse ``PrivateKeyInfo`` and build the appropriate object. The only methods that can read this encoding are in classes for the specific algorithms. That is, the RSA class can read PrivateKeyInfo only if the input data is an RSA key, the ECDsa class can read it only if the input data is an ECC key, etc.
190
+
The algorithm is specified in the key data itself. However, the .NET Base Class Library does not have a class that can parse ``PrivateKeyInfo`` and build the appropriate object. The only methods that can read this encoding are in classes for the specific algorithms. That is, the RSA class can read ``PrivateKeyInfo`` only if the input data is an RSA key, the ECDsa class can read it only if the input data is an ECC key, etc.
191
191
192
192
One possible workaround would be to supply the encoded key to the RSA class and if it works, we have an RSA key. If it does not work, give the encoded key to the ECDsa class. However, if the RSA class gets an encoded key that is not RSA, it throws an exception, and using exceptions to determine code flow is not best practice.
When you generate a new key pair in a YubiKey's PIV application, you are given the public key, which is returned as an instance of the ``PublicKey`` class. From this class, you can obtain important information about the key, including ``KeyDefinition`` and ``KeyType``. Depending on the specific class, ``Parameters`` (EC and RSA) and ``PublicPoint`` (EC and Curve25519) are also included.
IPublicKeypublicKey=RSAPublicKey.CreateFromSubjectPublicKeyInfo(spkiBytes); // or ECPublicKey, etc.
168
170
```
169
171
172
+
> [!NOTE]
173
+
> When importing a public key in PEM format, there are a number of possible header and footer combinations, including the following:
174
+
>
175
+
> ```
176
+
> -----BEGIN PUBLIC KEY-----
177
+
> -----END PUBLIC KEY-----
178
+
>
179
+
> -----BEGIN RSA PUBLIC KEY-----
180
+
> -----END RSA PUBLIC KEY-----
181
+
>
182
+
> -----BEGIN EC PUBLIC KEY-----
183
+
> -----END EC PUBLIC KEY-----
184
+
> ```
185
+
186
+
### Determining the algorithm when importing a public key in PEM format
187
+
188
+
If you have a byte array that contains the ``SubjectPublicKeyInfo``, and you want to build a ``PublicKey``, you will need to first determine the public key algorithm. Once you know the algorithm, you can use the appropriate C# class to read the encoded data (for example, ``RSAPublicKey``, ``ECPublicKey``, or ``Curve25519PublicKey`` ).
189
+
190
+
The algorithm is specified in the key data itself. However, the .NET Base Class Library does not have a class that can parse ``SubjectPublicKeyInfo`` and build the appropriate object. The only methods that can read this encoding are in classes for the specific algorithms. That is, the RSA class can read ``SubjectPublicKeyInfo`` only if the input data is an RSA key, the ECDsa class can read it only if the input data is an ECC key, etc.
191
+
192
+
One possible workaround would be to supply the encoded key to the RSA class and if it works, we have an RSA key. If it does not work, give the encoded key to the ECDsa class. However, if the RSA class gets an encoded key that is not RSA, it throws an exception, and using exceptions to determine code flow is not best practice.
193
+
194
+
To determine the algorithm of an imported key, we need to open up the encoding and read the object identifier (OID) of the ``AlgorithmIdentifier``. And to find the OID, we need to decode the DER encoding of ``SubjectPublicKeyInfo``.
195
+
196
+
``SubjectPublicKeyInfo`` is defined as:
197
+
198
+
```csharp
199
+
SubjectPublicKeyInfo ::= SEQUENCE {
200
+
algorithm AlgorithmIdentifier,
201
+
subjectPublicKey BIT STRING }
202
+
203
+
AlgorithmIdentifier ::= SEQUENCE {
204
+
algorithm OBJECT IDENTIFIER,
205
+
parameter ANY DEFINED BY algorithm OPTIONAL }
206
+
```
207
+
208
+
This means that the DER encoding will look something like the following:
209
+
210
+
```csharp
211
+
30len// The len octets might be one, two, or three bytes long.
212
+
30len
213
+
06len
214
+
OIDbytes
215
+
etc.
216
+
```
217
+
218
+
To get to the OID, we need to read the first ``30 len``, then the second ``30 len``, then the ``06 len``.
219
+
170
220
## Error handling
171
221
172
222
Factory methods validate input and may throw exceptions:
0 commit comments