forked from Linaro/freertos-pkcs11-psa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiot_pkcs11_psa_object_management.c
918 lines (862 loc) · 32.3 KB
/
iot_pkcs11_psa_object_management.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/*
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright (c) 2019-2020 Arm Limited. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
/*
* This file is derivative of amazon-freertos\vendors\st\boards\stm32l475_discovery\
* ports\pkcs11\iot_pkcs11_pal.c(amazon-freertos commit 74875b1d2)
*/
/**
* @file iot_pkcs11_psa_object_management.c
* @brief Functions of managing the objects stored in TF-M.
*/
#include <string.h>
#include "iot_pkcs11_psa_object_management.h"
#include "iot_pkcs11_psa_input_format.h"
extern int convert_pem_to_der( const unsigned char * pucInput, size_t xLen,
unsigned char * pucOutput, size_t * pxOlen );
/*
* This is the context of the PKCS#11 PSA object. It is placed in a section.
* named "tasks_share". If MPU is enabled, tasks that call PKCS#11 APIs should
* be configured to have access to this region.
*/
P11KeyConfig_t P11KeyConfig __attribute__(( section( "tasks_share" ) ));
/**
* @brief Helper function to convert MBedtls ECP group to PSA ECC group ID
*
* @param[in] grpid MBedtls ECP group ID.
*
* @return PSA ECC group ID.
*/
static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
{
switch( grpid )
{
case MBEDTLS_ECP_DP_SECP192R1:
return( PSA_ECC_CURVE_SECP192R1 );
case MBEDTLS_ECP_DP_SECP224R1:
return( PSA_ECC_CURVE_SECP224R1 );
case MBEDTLS_ECP_DP_SECP256R1:
return( PSA_ECC_CURVE_SECP256R1 );
case MBEDTLS_ECP_DP_SECP384R1:
return( PSA_ECC_CURVE_SECP384R1 );
case MBEDTLS_ECP_DP_SECP521R1:
return( PSA_ECC_CURVE_SECP521R1 );
case MBEDTLS_ECP_DP_BP256R1:
return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
case MBEDTLS_ECP_DP_BP384R1:
return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
case MBEDTLS_ECP_DP_BP512R1:
return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
case MBEDTLS_ECP_DP_CURVE25519:
return( PSA_ECC_CURVE_CURVE25519 );
case MBEDTLS_ECP_DP_SECP192K1:
return( PSA_ECC_CURVE_SECP192K1 );
case MBEDTLS_ECP_DP_SECP224K1:
return( PSA_ECC_CURVE_SECP224K1 );
case MBEDTLS_ECP_DP_SECP256K1:
return( PSA_ECC_CURVE_SECP256K1 );
case MBEDTLS_ECP_DP_CURVE448:
return( PSA_ECC_CURVE_CURVE448 );
default:
return( 0 );
}
}
/**
* @brief Writes a file to local storage.
*
* Port-specific file write for crytographic information.
*
* @param[in] pxClass Class of the object to be saved.
* @param[in] pxLabel Label of the object to be saved.
* @param[in] pucData Data buffer to be written to file
* @param[in] ulDataSize Size (in bytes) of data to be saved.
* @param[in] pvContext Context of the key.
*
* @return The file handle of the object that was stored.
*/
CK_OBJECT_HANDLE PKCS11PSASaveObject( CK_ATTRIBUTE_PTR pxClass,
CK_ATTRIBUTE_PTR pxLabel,
uint8_t * pucData,
uint32_t ulDataSize,
mbedtls_pk_context *pvContext )
{
CK_OBJECT_HANDLE xHandle = eInvalidHandle;
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
CK_ULONG uxKeyType;
psa_key_handle_t uxKeyHandle;
psa_algorithm_t uxAlgorithm;
CK_RV xBytesWritten = 0;
CK_RV xReturn;
psa_status_t uxStatus = PSA_SUCCESS;
psa_ecc_curve_t curve_id;
const mbedtls_ecp_keypair *ec;
unsigned char cPrivateKeyRaw[EC_PRIVATE_KEY_MAX_LENGTH];
size_t xPrivateKeyRawSize = 0;
unsigned char *pcPublicKeyUncompressedData = NULL;
size_t xPublicKeySizeUncompressed = 0;
uint8_t * pucKeyData = NULL;
uint32_t ulKeyDataSize = 0;
mbedtls_pk_type_t uxPrivateKeyTypePKCS11 = 0;
if( ulDataSize <= pkcs11OBJECT_MAX_SIZE )
{
/*
* write client certificate.
*/
if( ( *( ( uint32_t * )pxClass->pValue ) == CKO_CERTIFICATE ) &&
( strcmp( pxLabel->pValue,
pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS ) == 0 ) )
{
uxStatus = psa_ps_set( PSA_DEVICE_CERTIFICATE_UID,
ulDataSize,
pucData,
( psa_storage_create_flags_t ) PSA_STORAGE_FLAG_NONE );
if( uxStatus == PSA_SUCCESS )
{
xHandle = eAwsDeviceCertificate;
/* Import the object into P11KeyConfig context. */
PKCS11PSAContextImportObject( pxLabel->pValue,
strlen( ( const char * ) pxLabel->pValue ),
pkcs11OBJECT_HANDLE_NA );
}
}
/*
* write Jitp certificate.
*/
else if( ( *( ( uint32_t * )pxClass->pValue ) == CKO_CERTIFICATE ) &&
( strcmp( pxLabel->pValue,
pkcs11configLABEL_JITP_CERTIFICATE ) == 0 ) )
{
uxStatus = psa_ps_set( PSA_JITP_CERTIFICATE_UID,
ulDataSize,
pucData,
( psa_storage_create_flags_t ) PSA_STORAGE_FLAG_NONE );
if( uxStatus == PSA_SUCCESS )
{
xHandle = eAwsJitpCertificate;
/* Import the object into P11KeyConfig context. */
PKCS11PSAContextImportObject( pxLabel->pValue,
strlen( ( const char * ) pxLabel->pValue ),
pkcs11OBJECT_HANDLE_NA );
}
}
/*
* write Root certificate.
*/
else if( ( *( ( uint32_t * )pxClass->pValue ) == CKO_CERTIFICATE ) &&
( strcmp( pxLabel->pValue,
pkcs11configLABEL_ROOT_CERTIFICATE ) == 0 ) )
{
uxStatus = psa_ps_set( PSA_ROOT_CERTIFICATE_UID,
ulDataSize,
pucData,
( psa_storage_create_flags_t ) PSA_STORAGE_FLAG_NONE );
if( uxStatus == PSA_SUCCESS )
{
xHandle = eAwsRootCertificate;
/* Import the object into P11KeyConfig context. */
PKCS11PSAContextImportObject( pxLabel->pValue,
strlen( ( const char * ) pxLabel->pValue ),
pkcs11OBJECT_HANDLE_NA );
}
}
/*
* Store device private key by crypto service of PSA.
*/
else if( ( *( ( uint32_t * )pxClass->pValue ) == CKO_PRIVATE_KEY ) &&
( strcmp( pxLabel->pValue,
pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS ) == 0 ) )
{
/*
* Device private key is only used to make a signature,
* only support RSA alg, only support MBEDTLS_RSA_PKCS_V15
* padding mode, only support SHA256 md alg.
*/
uxPrivateKeyTypePKCS11 = mbedtls_pk_get_type( pvContext );
switch ( uxPrivateKeyTypePKCS11 )
{
case MBEDTLS_PK_RSA:
uxKeyType = PSA_KEY_TYPE_RSA_KEY_PAIR;
pucKeyData = pucData;
ulKeyDataSize = ulDataSize;
switch ( ( ( mbedtls_rsa_context * ) ( pvContext->pk_ctx ) )->padding )
{
case MBEDTLS_RSA_PKCS_V15:
uxAlgorithm = PSA_ALG_RSA_PKCS1V15_SIGN( PSA_ALG_SHA_256 );
break;
case MBEDTLS_RSA_PKCS_V21:
uxAlgorithm = PSA_ALG_RSA_PSS( PSA_ALG_SHA_256 );
break;
default:
uxAlgorithm = 0;
uxStatus = PSA_ERROR_INVALID_ARGUMENT;
break;
}
break;
case MBEDTLS_PK_ECKEY:
case MBEDTLS_PK_ECDSA:
ec = (mbedtls_ecp_keypair *) (pvContext->pk_ctx );
curve_id = mbedtls_ecp_curve_info_from_grp_id( ec->grp.id )->grp_id;
uxKeyType = PSA_KEY_TYPE_ECC_KEY_PAIR(mbedtls_ecc_group_to_psa(curve_id));
uxAlgorithm = PSA_ALG_ECDSA( PSA_ALG_SHA_256 );
xPrivateKeyRawSize = ( ec->grp.nbits + 7 ) / 8;
if( 0 != mbedtls_mpi_write_binary( &ec->d,
cPrivateKeyRaw,
xPrivateKeyRawSize ) )
{
uxStatus = PSA_ERROR_GENERIC_ERROR;
}
pucKeyData = cPrivateKeyRaw;
ulKeyDataSize = xPrivateKeyRawSize;
break;
default:
uxAlgorithm = 0;
uxKeyType = 0;
uxStatus = PSA_ERROR_INVALID_ARGUMENT;
break;
}
if ( uxStatus == PSA_SUCCESS )
{
/* Device private key is saved as persistent key. */
#ifndef pkcs11configTFM_VERSION_1_0
psa_set_key_id( &key_attributes, PSA_DEVICE_PRIVATE_KEY_ID );
#endif
psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_SIGN_HASH );
psa_set_key_algorithm( &key_attributes, uxAlgorithm );
psa_set_key_type( &key_attributes, uxKeyType );
uxStatus = psa_import_key( &key_attributes,
( const uint8_t * )pucKeyData,
ulKeyDataSize,
&uxKeyHandle );
}
if ( uxStatus == PSA_SUCCESS )
{
xHandle = eAwsDevicePrivateKey;
/* Import the object into P11KeyConfig context. */
PKCS11PSAContextImportObject( pxLabel->pValue,
strlen( ( const char * ) pxLabel->pValue ),
uxKeyHandle );
}
}
else if( ( *( ( uint32_t * )pxClass->pValue ) == CKO_PUBLIC_KEY ) &&
( strcmp( pxLabel->pValue,
pkcs11configLABEL_CODE_VERIFICATION_KEY ) == 0 ) )
{
/*
* Code verify key is only used to make a verify,
* only SHA256 md alg.
*/
switch ( mbedtls_pk_get_type( pvContext ) )
{
case MBEDTLS_PK_RSA:
/**
* The RSA private key should contain the public key. So it should not go here.
*/
uxStatus = PSA_ERROR_INVALID_ARGUMENT;
break;
case MBEDTLS_PK_ECKEY:
case MBEDTLS_PK_ECDSA:
ec = (mbedtls_ecp_keypair *) (pvContext->pk_ctx );
curve_id = mbedtls_ecp_curve_info_from_grp_id( ec->grp.id )->grp_id;
uxKeyType = PSA_KEY_TYPE_ECC_PUBLIC_KEY(mbedtls_ecc_group_to_psa(curve_id));
uxAlgorithm = PSA_ALG_ECDSA( PSA_ALG_SHA_256 );
if( 0 !=get_public_key_ECPoint( pucData,
ulDataSize,
&pcPublicKeyUncompressedData,
&xPublicKeySizeUncompressed ) )
{
uxStatus = PSA_ERROR_GENERIC_ERROR;
}
pucKeyData = pcPublicKeyUncompressedData;
ulKeyDataSize = xPublicKeySizeUncompressed;
break;
default:
uxAlgorithm = 0;
uxStatus = PSA_ERROR_INVALID_ARGUMENT;
break;
}
if ( uxStatus == PSA_SUCCESS )
{
psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_VERIFY_HASH );
psa_set_key_algorithm( &key_attributes, uxAlgorithm );
psa_set_key_type( &key_attributes, uxKeyType );
uxStatus = psa_import_key( &key_attributes,
( const uint8_t * )pucKeyData,
ulKeyDataSize,
&uxKeyHandle );
}
if ( uxStatus == PSA_SUCCESS )
{
xHandle = eAwsCodeVerifyingKey;
/* Import the object into P11KeyConfig context. */
PKCS11PSAContextImportObject( pxLabel->pValue,
strlen( ( const char * ) pxLabel->pValue ),
uxKeyHandle );
}
}
else if( ( *( ( uint32_t * )pxClass->pValue ) == CKO_PUBLIC_KEY ) &&
( strcmp( pxLabel->pValue,
pkcs11configLABEL_DEVICE_PUBLIC_KEY_FOR_TLS ) == 0 ) )
{
/*
* Code verify key is only used to make a verify,
* only SHA256 md alg.
*/
switch ( mbedtls_pk_get_type( pvContext ) )
{
case MBEDTLS_PK_RSA:
uxStatus = PSA_ERROR_INVALID_ARGUMENT;
break;
case MBEDTLS_PK_ECKEY:
case MBEDTLS_PK_ECDSA:
ec = (mbedtls_ecp_keypair *) (pvContext->pk_ctx );
curve_id = mbedtls_ecp_curve_info_from_grp_id( ec->grp.id )->grp_id;
uxKeyType = PSA_KEY_TYPE_ECC_PUBLIC_KEY(mbedtls_ecc_group_to_psa(curve_id));
uxAlgorithm = PSA_ALG_ECDSA( PSA_ALG_SHA_256 );
if( 0 !=get_public_key_ECPoint( pucData,
ulDataSize,
&pcPublicKeyUncompressedData,
&xPublicKeySizeUncompressed ) )
{
uxStatus = PSA_ERROR_GENERIC_ERROR;
}
pucKeyData = pcPublicKeyUncompressedData;
ulKeyDataSize = xPublicKeySizeUncompressed;
break;
default:
uxStatus = PSA_ERROR_INVALID_ARGUMENT;
uxAlgorithm = 0;
break;
}
if( uxStatus == PSA_SUCCESS )
{
/* Device public key is saved as persistent key. */
#ifndef pkcs11configTFM_VERSION_1_0
psa_set_key_id( &key_attributes, PSA_DEVICE_PUBLIC_KEY_ID );
#endif
psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_VERIFY_HASH );
psa_set_key_algorithm( &key_attributes, uxAlgorithm );
psa_set_key_type( &key_attributes, uxKeyType );
uxStatus = psa_import_key( &key_attributes,
( const uint8_t * )pucKeyData,
ulKeyDataSize,
&uxKeyHandle );
}
if ( uxStatus == PSA_SUCCESS )
{
xHandle = eAwsDevicePublicKey;
/* Import the object into P11KeyConfig context. */
PKCS11PSAContextImportObject( pxLabel->pValue,
strlen( ( const char * ) pxLabel->pValue ),
uxKeyHandle );
}
}
}
return xHandle;
}
/**
* @brief Helper function to get value of a certificate from PSA secure storage
*
* @param[in] uid The uid value.
* @param[out] pucData Pointer to buffer for file data.
* @param[out] pulDataSize Size (in bytes) of data located in file.
* @param[out] pIsPrivate Boolean indicating if value is private (CK_TRUE)
* or exportable (CK_FALSE)
*
* @return CKR_OK if operation was successful. CKR_KEY_HANDLE_INVALID if
* no such object handle was found, CKR_DEVICE_MEMORY if memory for
* buffer could not be allocated, CKR_FUNCTION_FAILED for device driver
* error.
*/
static CK_RV PSAGetCertificateValue( psa_storage_uid_t uid,
uint8_t * pucData,
size_t * pulDataSize,
CK_BBOOL * pIsPrivate )
{
CK_RV ulReturn = CKR_OBJECT_HANDLE_INVALID;
size_t ulDataSize = *pulDataSize;
psa_status_t uxStatus;
uint8_t * pucBuffer;
size_t ulReadDataLen = 0;
struct psa_storage_info_t info = {0};
/* Get the size of the data associated with the certificate UID firstly. */
uxStatus = psa_ps_get_info( uid, &info );
if ( uxStatus == PSA_SUCCESS )
{
/* Allocate buffer for object value */
pucBuffer = pvPortMalloc( info.size );
if ( pucBuffer != NULL )
{
/* Get the object value */
uxStatus = psa_ps_get( uid, 0, info.size, pucBuffer, &ulReadDataLen );
if ( uxStatus == PSA_SUCCESS )
{
/* Convert the certificate from PEM to DER format */
if ( convert_pem_to_der( pucBuffer, ulReadDataLen, pucData, &ulDataSize ) != 0 )
{
/* Not PEM format, use as it is. */
ulDataSize = ulReadDataLen;
memcpy( pucData, pucBuffer, ulDataSize );
}
*pulDataSize = ulDataSize;
*pIsPrivate = CK_FALSE;
ulReturn = CKR_OK;
}
else
{
ulReturn = CKR_FUNCTION_FAILED;
}
vPortFree( pucBuffer );
}
else
{
ulReturn = CKR_DEVICE_MEMORY;
}
}
return ulReturn;
}
/**
* @brief Gets the value of an object in storage, by handle.
*
* Port-specific file access for cryptographic information.
*
* This call dynamically allocates the buffer which object value
* data is copied into. PKCS11PSAGetObjectValueCleanup()
* should be called after each use to free the dynamically allocated
* buffer.
*
* @sa PKCS11PSAGetObjectValueCleanup
*
* @param[in] pcFileName The name of the file to be read.
* @param[out] pucData Pointer to buffer for file data.
* @param[out] pulDataSize Size (in bytes) of data located in file.
* @param[out] pIsPrivate Boolean indicating if value is private (CK_TRUE)
* or exportable (CK_FALSE)
*
* @return CKR_OK if operation was successful. CKR_KEY_HANDLE_INVALID if
* no such object handle was found, CKR_DEVICE_MEMORY if memory for
* buffer could not be allocated, CKR_FUNCTION_FAILED for device driver
* error.
*/
CK_RV PKCS11PSAGetObjectValue( CK_OBJECT_HANDLE xHandle,
uint8_t * pucData,
size_t * pulDataSize,
CK_BBOOL * pIsPrivate )
{
CK_RV ulReturn = CKR_OBJECT_HANDLE_INVALID;
psa_status_t uxStatus;
psa_key_type_t key_type;
size_t key_bits;
size_t buffer_size;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
/*
* Read client certificate.
*/
if( xHandle == eAwsDeviceCertificate )
{
/*
* return reference and size only if certificates are present in the device.
*/
if( P11KeyConfig.xDeviceCertificateMark == pdTRUE )
{
ulReturn = PSAGetCertificateValue(PSA_DEVICE_CERTIFICATE_UID, pucData, pulDataSize, pIsPrivate);
}
}
/*
* Read Jitp certificate.
*/
if( xHandle == eAwsJitpCertificate )
{
/*
* return reference and size only if certificates are present in the device.
*/
if( P11KeyConfig.xJitpCertificateMark == pdTRUE )
{
ulReturn = PSAGetCertificateValue(PSA_JITP_CERTIFICATE_UID, pucData, pulDataSize, pIsPrivate);
}
}
/*
* Read Root certificate.
*/
if( xHandle == eAwsRootCertificate )
{
/*
* return reference and size only if certificates are present in the device.
*/
if( P11KeyConfig.xRootCertificateMark == pdTRUE )
{
ulReturn = PSAGetCertificateValue(PSA_ROOT_CERTIFICATE_UID, pucData, pulDataSize, pIsPrivate);
}
}
/*
* Read client key.
*/
else if( xHandle == eAwsDevicePrivateKey )
{
/*
* return reference and size only if key is present in the device and is not private
*/
if( P11KeyConfig.xDevicePrivateKeyMark == pdTRUE )
{
uxStatus = psa_get_key_attributes( P11KeyConfig.uxDevicePrivateKey, &attributes );
if( uxStatus == PSA_SUCCESS )
{
key_type = psa_get_key_type( &attributes );
key_bits = psa_get_key_bits( &attributes );
buffer_size = PSA_KEY_EXPORT_MAX_SIZE( key_type, key_bits );
uxStatus = psa_export_key( P11KeyConfig.uxDevicePrivateKey, pucData, buffer_size, pulDataSize );
if ( uxStatus == PSA_ERROR_NOT_PERMITTED )
{
*pIsPrivate = CK_TRUE;
ulReturn = CKR_OK;
}
else if ( uxStatus == PSA_SUCCESS )
{
*pIsPrivate = CK_FALSE;
ulReturn = CKR_OK;
}
else
{
ulReturn = CKR_FUNCTION_FAILED;
}
}
else
{
ulReturn = CKR_FUNCTION_FAILED;
}
}
}
else if( xHandle == eAwsDevicePublicKey )
{
/*
* return reference and size only if key is present in the device and is not private
*/
if( P11KeyConfig.xDevicePublicKeyMark == pdTRUE )
{
uxStatus = psa_get_key_attributes( P11KeyConfig.uxDevicePublicKey, &attributes );
if( uxStatus == PSA_SUCCESS )
{
key_type = psa_get_key_type( &attributes );
key_bits = psa_get_key_bits( &attributes );
buffer_size = PSA_KEY_EXPORT_MAX_SIZE( key_type, key_bits );
uxStatus = psa_export_key( P11KeyConfig.uxDevicePublicKey, pucData, buffer_size, pulDataSize );
if ( uxStatus == PSA_ERROR_NOT_PERMITTED )
{
*pIsPrivate = CK_TRUE;
ulReturn = CKR_OK;
}
else if ( uxStatus == PSA_SUCCESS )
{
*pIsPrivate = CK_FALSE;
ulReturn = CKR_OK;
}
else
{
ulReturn = CKR_FUNCTION_FAILED;
}
}
else
{
ulReturn = CKR_FUNCTION_FAILED;
}
}
}
else if( xHandle == eAwsCodeVerifyingKey )
{
/*
* return reference and size only if key is present in the device and is not private
*/
if( P11KeyConfig.xCodeVerifyKeyMark == pdTRUE )
{
uxStatus = psa_get_key_attributes( P11KeyConfig.uxCodeVerifyKey, &attributes );
if( uxStatus == PSA_SUCCESS )
{
key_type = psa_get_key_type( &attributes );
key_bits = psa_get_key_bits( &attributes );
buffer_size = PSA_KEY_EXPORT_MAX_SIZE( key_type, key_bits );
uxStatus = psa_export_key( P11KeyConfig.uxCodeVerifyKey, pucData, buffer_size, pulDataSize );
if ( uxStatus == PSA_ERROR_NOT_PERMITTED )
{
*pIsPrivate = CK_TRUE;
ulReturn = CKR_OK;
}
else if ( uxStatus == PSA_SUCCESS )
{
*pIsPrivate = CK_FALSE;
ulReturn = CKR_OK;
}
else
{
ulReturn = CKR_FUNCTION_FAILED;
}
}
else
{
ulReturn = CKR_FUNCTION_FAILED;
}
}
}
return ulReturn;
}
/**
* @brief Cleanup after PKCS11PSAGetObjectValue().
*
* @param[in] pucData The buffer to free.
* (*ppucData from PKCS11PSAGetObjectValue())
* @param[in] ulDataSize The length of the buffer to free.
* (*pulDataSize from PKCS11PSAGetObjectValue())
*/
void PKCS11PSAGetObjectValueCleanup( uint8_t * pucData,
uint32_t ulDataSize )
{
/* Unused parameters. */
( void ) pucData;
( void ) ulDataSize;
/* Since no buffer was allocated on heap, there is no cleanup
* to be done. */
}
/**
* @brief Import an object into P11KeyConfig.
*
* @param[in] pLableValue The lable of the object.
*
* @param[in] ulLableLen The length of the object label.
*
* @param[in] uxKeyHandle The key handle.
*
*/
void PKCS11PSAContextImportObject( CK_VOID_PTR pLableValue,
CK_LONG ulLableLen,
psa_key_handle_t uxKeyHandle )
{
if( strcmp( pLableValue,
pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS ) == 0 )
{
P11KeyConfig.uxDevicePrivateKey = uxKeyHandle;
/*change key present mark'*/
P11KeyConfig.xDevicePrivateKeyMark = pdTRUE;
}
else if( strcmp( pLableValue,
pkcs11configLABEL_DEVICE_PUBLIC_KEY_FOR_TLS ) == 0 )
{
P11KeyConfig.uxDevicePublicKey = uxKeyHandle;
/*change key present mark'*/
P11KeyConfig.xDevicePublicKeyMark = pdTRUE;
}
else if( strcmp( pLableValue,
pkcs11configLABEL_CODE_VERIFICATION_KEY ) == 0 )
{
P11KeyConfig.uxCodeVerifyKey = uxKeyHandle;
/*change key present mark'*/
P11KeyConfig.xCodeVerifyKeyMark = pdTRUE;
}
else if( strcmp( pLableValue,
pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS ) == 0 )
{
/*change certificate present mark'*/
P11KeyConfig.xDeviceCertificateMark = pdTRUE;
}
else if( strcmp( pLableValue,
pkcs11configLABEL_JITP_CERTIFICATE ) == 0 )
{
/*change certificate present mark'*/
P11KeyConfig.xJitpCertificateMark = pdTRUE;
}
else if( strcmp( pLableValue,
pkcs11configLABEL_ROOT_CERTIFICATE ) == 0 )
{
/*change certificate present mark'*/
P11KeyConfig.xRootCertificateMark = pdTRUE;
}
else
{
/* Do nothing. */
}
}
/**
* @brief Close or remove an object from PSA and update P11KeyConfig accordingly.
*
* @param[in] pLableValue The lable of the object.
*
* @param[in] ulLableLen The length of the object label.
*
* @param[in] permanent Destroy the key or just close the key.
*
*/
CK_RV PKCS11PSARemoveObject(uint8_t * pcLable, size_t xLabelLength, bool permanent)
{
CK_RV xResult = CKR_OK;
psa_status_t uxStatus;
if( memcmp( pcLable, pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS, xLabelLength ) == 0 )
{
if( P11KeyConfig.xDevicePrivateKeyMark == pdTRUE )
{
if( permanent )
{
uxStatus = psa_destroy_key( P11KeyConfig.uxDevicePrivateKey );
}
else
{
uxStatus = psa_close_key( P11KeyConfig.uxDevicePrivateKey );
}
if( uxStatus != PSA_SUCCESS )
{
xResult = CKR_FUNCTION_FAILED;
}
else
{
P11KeyConfig.uxDevicePrivateKey = 0;
P11KeyConfig.xDevicePrivateKeyMark = pdFALSE;
}
}
}
else if( memcmp( pcLable, pkcs11configLABEL_DEVICE_PUBLIC_KEY_FOR_TLS, xLabelLength ) == 0 )
{
if( P11KeyConfig.xDevicePublicKeyMark == pdTRUE )
{
if( permanent )
{
uxStatus = psa_destroy_key( P11KeyConfig.uxDevicePublicKey );
}
else
{
uxStatus = psa_close_key( P11KeyConfig.uxDevicePublicKey );
}
if( uxStatus != PSA_SUCCESS )
{
xResult = CKR_FUNCTION_FAILED;
}
else
{
P11KeyConfig.uxDevicePublicKey = 0;
P11KeyConfig.xDevicePublicKeyMark = pdFALSE;
}
}
}
else if( memcmp( pcLable, pkcs11configLABEL_CODE_VERIFICATION_KEY, xLabelLength ) == 0 )
{
if( P11KeyConfig.xCodeVerifyKeyMark == pdTRUE )
{
if( permanent )
{
uxStatus = psa_destroy_key( P11KeyConfig.uxCodeVerifyKey );
}
else
{
uxStatus = psa_close_key( P11KeyConfig.uxCodeVerifyKey );
}
if( uxStatus != PSA_SUCCESS )
{
xResult = CKR_FUNCTION_FAILED;
}
else
{
P11KeyConfig.uxCodeVerifyKey = 0;
P11KeyConfig.xCodeVerifyKeyMark = pdFALSE;
}
}
}
else if( memcmp( pcLable, pkcs11configLABEL_ROOT_CERTIFICATE, xLabelLength ) == 0 )
{
if( P11KeyConfig.xRootCertificateMark == pdTRUE )
{
uxStatus = psa_ps_remove( PSA_ROOT_CERTIFICATE_UID );
if( uxStatus != PSA_SUCCESS )
{
xResult = CKR_FUNCTION_FAILED;
}
else
{
P11KeyConfig.xRootCertificateMark = pdFALSE;
}
}
}
else if( memcmp( pcLable, pkcs11configLABEL_JITP_CERTIFICATE, xLabelLength ) == 0 )
{
if( P11KeyConfig.xJitpCertificateMark == pdTRUE )
{
uxStatus = psa_ps_remove( PSA_JITP_CERTIFICATE_UID );
if( uxStatus != PSA_SUCCESS )
{
xResult = CKR_FUNCTION_FAILED;
}
else
{
P11KeyConfig.xJitpCertificateMark = pdFALSE;
}
}
}
else if( memcmp( pcLable, pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS, xLabelLength ) == 0 )
{
if( P11KeyConfig.xDeviceCertificateMark == pdTRUE )
{
uxStatus = psa_ps_remove( PSA_DEVICE_CERTIFICATE_UID );
if( uxStatus != PSA_SUCCESS )
{
xResult = CKR_FUNCTION_FAILED;
}
else
{
P11KeyConfig.xDeviceCertificateMark = pdFALSE;
}
}
}
else
{
xResult = CKR_ATTRIBUTE_VALUE_INVALID;
}
return xResult;
}
/**
* @brief Get the PSA key handle by the object label.
*
* @param[in] pLableValue The lable of the object.
*
* @param[in] ulLableLen The length of the object label.
*
* @param[in] uxKeyHandle The returned key handle.
*
*/
CK_RV PKCS11PSAGetKeyHandle( uint8_t * pcLable, size_t xLabelLength, psa_key_handle_t * uxKeyHandle )
{
CK_RV xResult = CKR_OK;
if(( memcmp( pcLable, pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS, xLabelLength ) == 0 ) &&
( P11KeyConfig.xDevicePrivateKeyMark == pdTRUE ))
{
*uxKeyHandle = P11KeyConfig.uxDevicePrivateKey;
}
else if(( memcmp( pcLable, pkcs11configLABEL_DEVICE_PUBLIC_KEY_FOR_TLS, xLabelLength ) == 0 ) &&
( P11KeyConfig.xDevicePublicKeyMark == pdTRUE ))
{
*uxKeyHandle = P11KeyConfig.uxDevicePublicKey;
}
else if(( memcmp( pcLable, pkcs11configLABEL_CODE_VERIFICATION_KEY, xLabelLength ) == 0 ) &&
( P11KeyConfig.xCodeVerifyKeyMark == pdTRUE ))
{
*uxKeyHandle = P11KeyConfig.uxCodeVerifyKey;
}
else
{
xResult = CKR_ARGUMENTS_BAD;
*uxKeyHandle = 0;
}
return xResult;
}