Skip to content

Commit 9acfd73

Browse files
author
Hanno Becker
committed
Adapt mbedtls_pk_write_pubkey_der() to the new PSA public key format
Context: There are two public key writing functions in Mbed TLS. First, mbedtls_pk_write_pubkey(), which exports a public key in the form of a SubjectPublicKey structure containing the raw keying material (for example, EC point coordinates for an EC public key, without reference to the underlying curve). Secondly, mbedtls_pk_write_pubkey_der(), which exports a public key in the form of a SubjectPublicKeyInfo structure, wrapping the SubjectPublicKey structure by additional information identifying the type of public key (and for ECC, e.g., it'd also contain the ECC group identifier). The implementation of mbedtls_pk_write_pubkey_der() calls mbedtls_pk_write_pubkey() first and then adds the corresponding algorithm identifier wrapper. Both of these functions need to be provided for PSA-based opaque PK contexts, based on PSA's public key export function. Previously, PSA used the SubjectPublicKeyInfo structure as its export format, so mbedtls_pk_write_pubkey_der() could be easily implemented, while mbedtls_pk_write_pubkey() would need to trim the output of the PSA export. The previous implementation of mbedtls_pk_write_pubkey() is not quite right because it calls PSA export doesn't do any trimming, hence exporting the large SubjectPublicKeyInfo structure instead of the small SubjectPublicKey. mbedtls_pk_write_pubkey_der(), in turn, immediately returns after calling mbedtls_pk_write_pubkey(), hence also returning the SubjectPublicKeyInfo structure, which is correct. By now, the PSA public key export format has changed to the smaller SubjectPublicKey structure. This means that, now, mbedtls_pk_write_pubkey() can be implemented by just calling the PSA export, and that mbedtls_pk_write_pubkey_der() needs to add the algorithm information around it, just as in the other types of PK contexts. While not correct for the old format, the existing code for mbedtls_pk_write_pubkey() is therefore correct for the new PSA public key format, and needs no change apart from the missing pointer shift in the last commit. The implementation of mbedtls_pk_write_pubkey_der() needs a special code path for PSA-based opaque PK contexts, as the PK context only contains the PSA key handle, and the PSA API needs to be used to extract the underlying EC curve to be able to write the AlgorithmParameter structure that's part of the SubjectPublicKeyInfo structure. That's what this commit does, (hopefully) making both mbedtls_pk_write_pubkey() and mbedtls_pk_write_pubkey_der() export the correctly formatted public key based on the new PSA public key format.
1 parent 69777ca commit 9acfd73

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

include/mbedtls/psa_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "ecp.h"
4242
#include "md.h"
4343
#include "pk.h"
44+
#include "oid.h"
4445

4546
/* Translations for symmetric crypto. */
4647

library/pkwrite.c

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
#if defined(MBEDTLS_USE_PSA_CRYPTO)
5050
#include "psa/crypto.h"
51+
#include "mbedtls/psa_util.h"
5152
#endif
5253
#if defined(MBEDTLS_PLATFORM_C)
5354
#include "mbedtls/platform.h"
@@ -197,16 +198,13 @@ int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, si
197198
int ret;
198199
unsigned char *c;
199200
size_t len = 0, par_len = 0, oid_len;
201+
mbedtls_pk_type_t pk_type;
200202
const char *oid;
201203

202204
c = buf + size;
203205

204206
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
205207

206-
if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_OPAQUE )
207-
{
208-
return( (int) len );
209-
}
210208
if( c - buf < 1 )
211209
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
212210

@@ -221,18 +219,51 @@ int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, si
221219
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
222220
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
223221

224-
if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
225-
&oid, &oid_len ) ) != 0 )
226-
{
227-
return( ret );
228-
}
229-
222+
pk_type = mbedtls_pk_get_type( key );
230223
#if defined(MBEDTLS_ECP_C)
231-
if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
224+
if( pk_type == MBEDTLS_PK_ECKEY )
232225
{
233226
MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
234227
}
235228
#endif
229+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
230+
if( pk_type == MBEDTLS_PK_OPAQUE )
231+
{
232+
psa_status_t status;
233+
psa_key_type_t key_type;
234+
psa_key_handle_t handle;
235+
psa_ecc_curve_t curve;
236+
237+
handle = *((psa_key_handle_t*) key->pk_ctx );
238+
239+
status = psa_get_key_information( handle, &key_type,
240+
NULL /* bitsize not needed */ );
241+
if( status != PSA_SUCCESS )
242+
return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
243+
244+
curve = PSA_KEY_TYPE_GET_CURVE( key_type );
245+
if( curve == 0 )
246+
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
247+
248+
ret = mbedtls_psa_get_ecc_oid_from_id( curve, &oid, &oid_len );
249+
if( ret != 0 )
250+
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
251+
252+
/* Write EC algorithm parameters; that's akin
253+
* to pk_write_ec_param() above. */
254+
MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_oid( &c, buf,
255+
oid, oid_len ) );
256+
257+
/* The rest of the function works as for legacy EC contexts. */
258+
pk_type = MBEDTLS_PK_ECKEY;
259+
}
260+
#endif /* MBEDTLS_USE_PSA_CRYPTO */
261+
262+
if( ( ret = mbedtls_oid_get_oid_by_pk_alg( pk_type, &oid,
263+
&oid_len ) ) != 0 )
264+
{
265+
return( ret );
266+
}
236267

237268
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
238269
par_len ) );

0 commit comments

Comments
 (0)