-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathoqs_encode_key2any.c
2585 lines (2355 loc) · 116 KB
/
oqs_encode_key2any.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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0 AND MIT
/*
* OQS OpenSSL 3 provider
*
* Code strongly inspired by OpenSSL endecoder.
*
* ToDo: Adding hybrid alg support
*/
#include <openssl/asn1.h>
#include <openssl/asn1t.h>
#include <openssl/core.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/params.h>
#include <openssl/pem.h>
#include <openssl/pkcs12.h> /* PKCS8_encrypt() */
#include <openssl/proverr.h>
#include <openssl/types.h>
#include <openssl/x509.h>
#include <string.h>
#include "oqs_endecoder_local.h"
#include "oqs_prov.h"
#ifdef NDEBUG
#define OQS_ENC_PRINTF(a)
#define OQS_ENC_PRINTF2(a, b)
#define OQS_ENC_PRINTF3(a, b, c)
#else
#define OQS_ENC_PRINTF(a) \
if (getenv("OQSENC")) \
printf(a)
#define OQS_ENC_PRINTF2(a, b) \
if (getenv("OQSENC")) \
printf(a, b)
#define OQS_ENC_PRINTF3(a, b, c) \
if (getenv("OQSENC")) \
printf(a, b, c)
#endif // NDEBUG
struct key2any_ctx_st {
PROV_OQS_CTX *provctx;
/* Set to 0 if parameters should not be saved (dsa only) */
int save_parameters;
/* Set to 1 if intending to encrypt/decrypt, otherwise 0 */
int cipher_intent;
EVP_CIPHER *cipher;
OSSL_PASSPHRASE_CALLBACK *pwcb;
void *pwcbarg;
};
typedef int check_key_type_fn(const void *key, int nid);
typedef int key_to_paramstring_fn(const void *key, int nid, int save,
void **str, int *strtype);
typedef int key_to_der_fn(BIO *out, const void *key, int key_nid,
const char *pemname, key_to_paramstring_fn *p2s,
i2d_of_void *k2d, struct key2any_ctx_st *ctx);
typedef int write_bio_of_void_fn(BIO *bp, const void *x);
/* Free the blob allocated during key_to_paramstring_fn */
static void free_asn1_data(int type, void *data) {
switch (type) {
case V_ASN1_OBJECT:
ASN1_OBJECT_free(data);
break;
case V_ASN1_SEQUENCE:
ASN1_STRING_free(data);
break;
}
}
static PKCS8_PRIV_KEY_INFO *key_to_p8info(const void *key, int key_nid,
void *params, int params_type,
i2d_of_void *k2d) {
/* der, derlen store the key DER output and its length */
unsigned char *der = NULL;
int derlen;
/* The final PKCS#8 info */
PKCS8_PRIV_KEY_INFO *p8info = NULL;
OQS_ENC_PRINTF("OQS ENC provider: key_to_p8info called\n");
if ((p8info = PKCS8_PRIV_KEY_INFO_new()) == NULL ||
(derlen = k2d(key, &der)) <= 0 ||
!PKCS8_pkey_set0(p8info, OBJ_nid2obj(key_nid), 0,
// doesn't work with oqs-openssl:
// params_type, params,
// does work/interop:
V_ASN1_UNDEF, NULL, der, derlen)) {
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
PKCS8_PRIV_KEY_INFO_free(p8info);
OPENSSL_free(der);
p8info = NULL;
}
return p8info;
}
static X509_SIG *p8info_to_encp8(PKCS8_PRIV_KEY_INFO *p8info,
struct key2any_ctx_st *ctx) {
X509_SIG *p8 = NULL;
char kstr[PEM_BUFSIZE];
size_t klen = 0;
OSSL_LIB_CTX *libctx = PROV_OQS_LIBCTX_OF(ctx->provctx);
OQS_ENC_PRINTF("OQS ENC provider: p8info_to_encp8 called\n");
if (ctx->cipher == NULL || ctx->pwcb == NULL)
return NULL;
if (!ctx->pwcb(kstr, PEM_BUFSIZE, &klen, NULL, ctx->pwcbarg)) {
ERR_raise(ERR_LIB_USER, PROV_R_UNABLE_TO_GET_PASSPHRASE);
return NULL;
}
/* First argument == -1 means "standard" */
p8 = PKCS8_encrypt_ex(-1, ctx->cipher, kstr, klen, NULL, 0, 0, p8info,
libctx, NULL);
OPENSSL_cleanse(kstr, klen);
return p8;
}
static X509_SIG *key_to_encp8(const void *key, int key_nid, void *params,
int params_type, i2d_of_void *k2d,
struct key2any_ctx_st *ctx) {
PKCS8_PRIV_KEY_INFO *p8info =
key_to_p8info(key, key_nid, params, params_type, k2d);
X509_SIG *p8 = NULL;
OQS_ENC_PRINTF("OQS ENC provider: key_to_encp8 called\n");
if (p8info == NULL) {
free_asn1_data(params_type, params);
} else {
p8 = p8info_to_encp8(p8info, ctx);
PKCS8_PRIV_KEY_INFO_free(p8info);
}
return p8;
}
static X509_PUBKEY *oqsx_key_to_pubkey(const void *key, int key_nid,
void *params, int params_type,
i2d_of_void k2d) {
/* der, derlen store the key DER output and its length */
unsigned char *der = NULL;
int derlen;
/* The final X509_PUBKEY */
X509_PUBKEY *xpk = NULL;
OQS_ENC_PRINTF2("OQS ENC provider: oqsx_key_to_pubkey called for NID %d\n",
key_nid);
if ((xpk = X509_PUBKEY_new()) == NULL || (derlen = k2d(key, &der)) <= 0 ||
!X509_PUBKEY_set0_param(
xpk, OBJ_nid2obj(key_nid), V_ASN1_UNDEF,
NULL, // as per logic in oqs_meth.c in oqs-openssl
der, derlen)) {
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
X509_PUBKEY_free(xpk);
OPENSSL_free(der);
xpk = NULL;
}
return xpk;
}
/*
* key_to_epki_* produce encoded output with the private key data in a
* EncryptedPrivateKeyInfo structure (defined by PKCS#8). They require
* that there's an intent to encrypt, anything else is an error.
*
* key_to_pki_* primarly produce encoded output with the private key data
* in a PrivateKeyInfo structure (also defined by PKCS#8). However, if
* there is an intent to encrypt the data, the corresponding key_to_epki_*
* function is used instead.
*
* key_to_spki_* produce encoded output with the public key data in an
* X.509 SubjectPublicKeyInfo.
*
* Key parameters don't have any defined envelopment of this kind, but are
* included in some manner in the output from the functions described above,
* either in the AlgorithmIdentifier's parameter field, or as part of the
* key data itself.
*/
static int key_to_epki_der_priv_bio(BIO *out, const void *key, int key_nid,
ossl_unused const char *pemname,
key_to_paramstring_fn *p2s,
i2d_of_void *k2d,
struct key2any_ctx_st *ctx) {
int ret = 0;
void *str = NULL;
int strtype = V_ASN1_UNDEF;
X509_SIG *p8;
OQS_ENC_PRINTF("OQS ENC provider: key_to_epki_der_priv_bio called\n");
if (!ctx->cipher_intent)
return 0;
if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype))
return 0;
p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx);
if (p8 != NULL)
ret = i2d_PKCS8_bio(out, p8);
X509_SIG_free(p8);
return ret;
}
static int key_to_epki_pem_priv_bio(BIO *out, const void *key, int key_nid,
ossl_unused const char *pemname,
key_to_paramstring_fn *p2s,
i2d_of_void *k2d,
struct key2any_ctx_st *ctx) {
int ret = 0;
void *str = NULL;
int strtype = V_ASN1_UNDEF;
X509_SIG *p8;
OQS_ENC_PRINTF("OQS ENC provider: key_to_epki_pem_priv_bio called\n");
if (!ctx->cipher_intent)
return 0;
if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype))
return 0;
p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx);
if (p8 != NULL)
ret = PEM_write_bio_PKCS8(out, p8);
X509_SIG_free(p8);
return ret;
}
static int key_to_pki_der_priv_bio(BIO *out, const void *key, int key_nid,
ossl_unused const char *pemname,
key_to_paramstring_fn *p2s, i2d_of_void *k2d,
struct key2any_ctx_st *ctx) {
int ret = 0;
void *str = NULL;
int strtype = V_ASN1_UNDEF;
PKCS8_PRIV_KEY_INFO *p8info;
OQS_ENC_PRINTF("OQS ENC provider: key_to_pki_der_priv_bio called\n");
if (ctx->cipher_intent)
return key_to_epki_der_priv_bio(out, key, key_nid, pemname, p2s, k2d,
ctx);
if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype))
return 0;
p8info = key_to_p8info(key, key_nid, str, strtype, k2d);
if (p8info != NULL)
ret = i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8info);
else
free_asn1_data(strtype, str);
PKCS8_PRIV_KEY_INFO_free(p8info);
return ret;
}
static int key_to_pki_pem_priv_bio(BIO *out, const void *key, int key_nid,
ossl_unused const char *pemname,
key_to_paramstring_fn *p2s, i2d_of_void *k2d,
struct key2any_ctx_st *ctx) {
int ret = 0, cmp_len = 0;
void *str = NULL;
int strtype = V_ASN1_UNDEF;
PKCS8_PRIV_KEY_INFO *p8info;
OQS_ENC_PRINTF("OQS ENC provider: key_to_pki_pem_priv_bio called\n");
if (ctx->cipher_intent)
return key_to_epki_pem_priv_bio(out, key, key_nid, pemname, p2s, k2d,
ctx);
if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype))
return 0;
p8info = key_to_p8info(key, key_nid, str, strtype, k2d);
if (p8info != NULL)
ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info);
else
free_asn1_data(strtype, str);
PKCS8_PRIV_KEY_INFO_free(p8info);
return ret;
}
static int key_to_spki_der_pub_bio(BIO *out, const void *key, int key_nid,
ossl_unused const char *pemname,
key_to_paramstring_fn *p2s, i2d_of_void *k2d,
struct key2any_ctx_st *ctx) {
int ret = 0;
OQSX_KEY *okey = (OQSX_KEY *)key;
X509_PUBKEY *xpk = NULL;
void *str = NULL;
int strtype = V_ASN1_UNDEF;
OQS_ENC_PRINTF("OQS ENC provider: key_to_spki_der_pub_bio called\n");
if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype))
return 0;
xpk = oqsx_key_to_pubkey(key, key_nid, str, strtype, k2d);
if (xpk != NULL)
ret = i2d_X509_PUBKEY_bio(out, xpk);
X509_PUBKEY_free(xpk);
return ret;
}
static int key_to_spki_pem_pub_bio(BIO *out, const void *key, int key_nid,
ossl_unused const char *pemname,
key_to_paramstring_fn *p2s, i2d_of_void *k2d,
struct key2any_ctx_st *ctx) {
int ret = 0;
X509_PUBKEY *xpk = NULL;
void *str = NULL;
int strtype = V_ASN1_UNDEF;
OQS_ENC_PRINTF("OQS ENC provider: key_to_spki_pem_pub_bio called\n");
if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype))
return 0;
xpk = oqsx_key_to_pubkey(key, key_nid, str, strtype, k2d);
if (xpk != NULL)
ret = PEM_write_bio_X509_PUBKEY(out, xpk);
else
free_asn1_data(strtype, str);
/* Also frees |str| */
X509_PUBKEY_free(xpk);
return ret;
}
/*
* key_to_type_specific_* produce encoded output with type specific key data,
* no envelopment; the same kind of output as the type specific i2d_ and
* PEM_write_ functions, which is often a simple SEQUENCE of INTEGER.
*
* OpenSSL tries to discourage production of new keys in this form, because
* of the ambiguity when trying to recognise them, but can't deny that PKCS#1
* et al still are live standards.
*
* Note that these functions completely ignore p2s, and rather rely entirely
* on k2d to do the complete work.
*/
/*
static int key_to_type_specific_der_bio(BIO *out, const void *key,
int key_nid,
ossl_unused const char *pemname,
key_to_paramstring_fn *p2s,
i2d_of_void *k2d,
struct key2any_ctx_st *ctx)
{
unsigned char *der = NULL;
int derlen;
int ret;
OQS_ENC_PRINTF("OQS ENC provider: key_to_type_specific_der_bio called\n");
if ((derlen = k2d(key, &der)) <= 0) {
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
return 0;
}
ret = BIO_write(out, der, derlen);
OPENSSL_free(der);
return ret > 0;
}
#define key_to_type_specific_der_priv_bio key_to_type_specific_der_bio
#define key_to_type_specific_der_pub_bio key_to_type_specific_der_bio
#define key_to_type_specific_der_param_bio key_to_type_specific_der_bio
static int key_to_type_specific_pem_bio_cb(BIO *out, const void *key,
int key_nid, const char *pemname,
key_to_paramstring_fn *p2s,
i2d_of_void *k2d,
struct key2any_ctx_st *ctx)
{
OQS_ENC_PRINTF("OQS ENC provider: key_to_type_specific_pem_bio_cb called
\n");
return PEM_ASN1_write_bio(k2d, pemname, out, key, ctx->cipher,
NULL, 0, ctx->pwcb, ctx->pwcbarg) > 0;
}
static int key_to_type_specific_pem_priv_bio(BIO *out, const void *key,
int key_nid, const char *pemname,
key_to_paramstring_fn *p2s,
i2d_of_void *k2d,
struct key2any_ctx_st *ctx)
{
OQS_ENC_PRINTF("OQS ENC provider: key_to_type_specific_pem_priv_bio
called\n");
return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname,
p2s, k2d, ctx, ctx->pwcb,
ctx->pwcbarg);
}
static int key_to_type_specific_pem_pub_bio(BIO *out, const void *key,
int key_nid, const char *pemname,
key_to_paramstring_fn *p2s,
i2d_of_void *k2d,
struct key2any_ctx_st *ctx)
{
OQS_ENC_PRINTF("OQS ENC provider: key_to_type_specific_pem_pub_bio
called\n");
return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname,
p2s, k2d, ctx, NULL, NULL);
}
#ifndef OPENSSL_NO_KEYPARAMS
static int key_to_type_specific_pem_param_bio(BIO *out, const void *key,
int key_nid, const char *pemname,
key_to_paramstring_fn *p2s,
i2d_of_void *k2d,
struct key2any_ctx_st *ctx)
{
OQS_ENC_PRINTF("OQS ENC provider: key_to_type_specific_pem_param_bio
called\n");
return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname,
p2s, k2d, ctx, NULL, NULL);
}
#endif
*/
/* ---------------------------------------------------------------------- */
static int prepare_oqsx_params(const void *oqsxkey, int nid, int save,
void **pstr, int *pstrtype) {
ASN1_OBJECT *params = NULL;
OQSX_KEY *k = (OQSX_KEY *)oqsxkey;
OQS_ENC_PRINTF3("OQS ENC provider: prepare_oqsx_params called with nid %d "
"(tlsname: %s)\n",
nid, k->tls_name);
if (k->tls_name && OBJ_sn2nid(k->tls_name) != nid) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_KEY);
return 0;
}
if (nid != NID_undef) {
params = OBJ_nid2obj(nid);
if (params == NULL)
return 0;
} else {
ERR_raise(ERR_LIB_USER, OQSPROV_R_MISSING_OID);
return 0;
}
if (OBJ_length(params) == 0) {
/* unexpected error */
ERR_raise(ERR_LIB_USER, OQSPROV_R_MISSING_OID);
ASN1_OBJECT_free(params);
return 0;
}
*pstr = params;
*pstrtype = V_ASN1_OBJECT;
return 1;
}
static int oqsx_spki_pub_to_der(const void *vxkey, unsigned char **pder) {
const OQSX_KEY *oqsxkey = vxkey;
unsigned char *keyblob, *buf;
int keybloblen, nid, buflen = 0;
ASN1_OCTET_STRING oct;
STACK_OF(ASN1_TYPE) *sk = NULL;
int ret = 0;
OQS_ENC_PRINTF("OQS ENC provider: oqsx_spki_pub_to_der called\n");
if (oqsxkey == NULL || oqsxkey->pubkey == NULL) {
ERR_raise(ERR_LIB_USER, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
if (oqsxkey->keytype != KEY_TYPE_CMP_SIG) {
keyblob = OPENSSL_memdup(oqsxkey->pubkey, oqsxkey->pubkeylen);
if (keyblob == NULL) {
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
return 0;
}
*pder = keyblob;
return oqsxkey->pubkeylen;
} else {
if ((sk = sk_ASN1_TYPE_new_null()) == NULL)
return -1;
ASN1_TYPE **aType =
OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_TYPE *));
ASN1_BIT_STRING **aString =
OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_BIT_STRING *));
unsigned char **temp =
OPENSSL_malloc(oqsxkey->numkeys * sizeof(unsigned char *));
size_t *templen = OPENSSL_malloc(oqsxkey->numkeys * sizeof(size_t));
int i;
for (i = 0; i < oqsxkey->numkeys; i++) {
aType[i] = ASN1_TYPE_new();
aString[i] = ASN1_BIT_STRING_new();
temp[i] = NULL;
buflen = oqsxkey->pubkeylen_cmp[i];
buf = OPENSSL_secure_malloc(buflen);
memcpy(buf, oqsxkey->comp_pubkey[i], buflen);
oct.data = buf;
oct.length = buflen;
oct.flags = 8;
templen[i] = i2d_ASN1_BIT_STRING(&oct, &temp[i]);
ASN1_STRING_set(aString[i], temp[i], templen[i]);
ASN1_TYPE_set1(aType[i], V_ASN1_SEQUENCE, aString[i]);
if (!sk_ASN1_TYPE_push(sk, aType[i])) {
for (int j = 0; j <= i; j++) {
OPENSSL_cleanse(aString[j]->data, aString[j]->length);
ASN1_BIT_STRING_free(aString[j]);
OPENSSL_cleanse(aType[j]->value.sequence->data,
aType[j]->value.sequence->length);
OPENSSL_clear_free(temp[j], templen[j]);
}
sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
OPENSSL_secure_clear_free(buf, buflen);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
return -1;
}
OPENSSL_secure_clear_free(buf, buflen);
}
keybloblen = i2d_ASN1_SEQUENCE_ANY(sk, pder);
for (i = 0; i < oqsxkey->numkeys; i++) {
OPENSSL_cleanse(aString[i]->data, aString[i]->length);
ASN1_BIT_STRING_free(aString[i]);
OPENSSL_cleanse(aType[i]->value.sequence->data,
aType[i]->value.sequence->length);
OPENSSL_clear_free(temp[i], templen[i]);
}
sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
return keybloblen;
}
}
static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder) {
OQSX_KEY *oqsxkey = (OQSX_KEY *)vxkey;
unsigned char *buf = NULL;
uint32_t buflen = 0, privkeylen = 0;
ASN1_OCTET_STRING oct;
int keybloblen, nid;
STACK_OF(ASN1_TYPE) *sk = NULL;
char *name;
OQS_ENC_PRINTF("OQS ENC provider: oqsx_pki_priv_to_der called\n");
// Encoding private _and_ public key concatenated ... seems unlogical and
// unnecessary, but is what oqs-openssl does, so we repeat it for
// interop... also from a security perspective not really smart to copy key
// material (side channel attacks, anyone?), but so be it for now (TBC).
if (oqsxkey == NULL || oqsxkey->privkey == NULL
#ifndef NOPUBKEY_IN_PRIVKEY
|| oqsxkey->pubkey == NULL
#endif
) {
ERR_raise(ERR_LIB_USER, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
// only concatenate private classic key (if any) and OQS private and public
// key NOT saving public classic key component (if any)
if (oqsxkey->keytype != KEY_TYPE_CMP_SIG) {
privkeylen = oqsxkey->privkeylen;
if (oqsxkey->numkeys > 1) { // hybrid
uint32_t actualprivkeylen = 0;
size_t fixed_pq_privkeylen =
oqsxkey->oqsx_provider_ctx.oqsx_qs_ctx.kem->length_secret_key;
size_t space_for_classical_privkey =
privkeylen - SIZE_OF_UINT32 - fixed_pq_privkeylen;
DECODE_UINT32(actualprivkeylen, oqsxkey->privkey);
if ((actualprivkeylen > oqsxkey->evp_info->length_private_key) ||
(actualprivkeylen > space_for_classical_privkey)) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
return 0;
}
privkeylen -=
(oqsxkey->evp_info->length_private_key - actualprivkeylen);
}
#ifdef NOPUBKEY_IN_PRIVKEY
buflen = privkeylen;
buf = OPENSSL_secure_malloc(buflen);
if (buf == NULL) {
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
return -1;
}
OQS_ENC_PRINTF2("OQS ENC provider: saving privkey of length %zu\n",
buflen);
memcpy(buf, oqsxkey->privkey, privkeylen);
#else
buflen = privkeylen + oqsx_key_get_oqs_public_key_len(oqsxkey);
buf = OPENSSL_secure_malloc(buflen);
if (buf == NULL) {
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
return -1;
}
OQS_ENC_PRINTF2("OQS ENC provider: saving priv+pubkey of length %d\n",
buflen);
memcpy(buf, oqsxkey->privkey, privkeylen);
if (oqsxkey->reverse_share) {
memcpy(buf + privkeylen, oqsxkey->comp_pubkey[0],
oqsx_key_get_oqs_public_key_len(oqsxkey));
} else {
memcpy(buf + privkeylen, oqsxkey->comp_pubkey[oqsxkey->numkeys - 1],
oqsx_key_get_oqs_public_key_len(oqsxkey));
}
#endif
oct.data = buf;
oct.length = buflen;
// more logical:
// oct.data = oqsxkey->privkey;
// oct.length = oqsxkey->privkeylen;
oct.flags = 0;
keybloblen = i2d_ASN1_OCTET_STRING(&oct, pder);
if (keybloblen < 0) {
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
keybloblen = 0; // signal error
}
OPENSSL_secure_clear_free(buf, buflen);
} else {
ASN1_TYPE **aType =
OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_TYPE *));
ASN1_OCTET_STRING **aString =
OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_OCTET_STRING *));
unsigned char **temp =
OPENSSL_malloc(oqsxkey->numkeys * sizeof(unsigned char *));
unsigned char *ed_internal;
size_t *templen = OPENSSL_malloc(oqsxkey->numkeys * sizeof(size_t)),
ed_internallen;
PKCS8_PRIV_KEY_INFO *p8inf_internal = NULL;
sk = sk_ASN1_TYPE_new_null();
int i;
if (!sk || !templen || !aType || !aString || !temp) {
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
if (sk) {
sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
}
return -1;
}
for (i = 0; i < oqsxkey->numkeys; i++) {
aType[i] = ASN1_TYPE_new();
aString[i] = ASN1_OCTET_STRING_new();
p8inf_internal = PKCS8_PRIV_KEY_INFO_new();
temp[i] = NULL;
int nid, version;
void *pval;
if ((name = get_cmpname(OBJ_sn2nid(oqsxkey->tls_name), i)) ==
NULL) {
for (int j = 0; j <= i; j++) {
OPENSSL_cleanse(aString[j]->data, aString[j]->length);
ASN1_OCTET_STRING_free(aString[j]);
OPENSSL_cleanse(aType[j]->value.sequence->data,
aType[j]->value.sequence->length);
if (j < i)
OPENSSL_clear_free(temp[j], templen[j]);
}
if (sk_ASN1_TYPE_num(sk) != -1)
sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
else
ASN1_TYPE_free(aType[i]);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
PKCS8_PRIV_KEY_INFO_free(p8inf_internal);
return -1;
}
if (get_oqsname_fromtls(name) == 0) {
nid =
oqsxkey->oqsx_provider_ctx.oqsx_evp_ctx->evp_info->keytype;
if (nid == EVP_PKEY_RSA) { // get the RSA real key size
unsigned char *enc_len = (unsigned char *)OPENSSL_strndup(
oqsxkey->comp_privkey[i], 4);
OPENSSL_cleanse(enc_len, 2);
DECODE_UINT32(buflen, enc_len);
buflen += 4;
OPENSSL_free(enc_len);
if (buflen > oqsxkey->privkeylen_cmp[i]) {
for (int j = 0; j <= i; j++) {
OPENSSL_cleanse(aString[j]->data,
aString[j]->length);
ASN1_OCTET_STRING_free(aString[j]);
OPENSSL_cleanse(aType[j]->value.sequence->data,
aType[j]->value.sequence->length);
if (j < i)
OPENSSL_clear_free(temp[j], templen[j]);
}
if (sk_ASN1_TYPE_num(sk) != -1)
sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
else
ASN1_TYPE_free(aType[i]);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
PKCS8_PRIV_KEY_INFO_free(p8inf_internal);
OPENSSL_free(name);
return -1;
}
} else
buflen = oqsxkey->privkeylen_cmp[i];
} else {
nid = OBJ_sn2nid(name);
buflen = oqsxkey->privkeylen_cmp[i] + oqsxkey->pubkeylen_cmp[i];
}
buf = OPENSSL_secure_malloc(buflen);
if (buf == NULL) {
for (int j = 0; j <= i; j++) {
OPENSSL_cleanse(aString[j]->data, aString[j]->length);
ASN1_OCTET_STRING_free(aString[j]);
OPENSSL_cleanse(aType[j]->value.sequence->data,
aType[j]->value.sequence->length);
if (j < i)
OPENSSL_clear_free(temp[j], templen[j]);
}
if (sk_ASN1_TYPE_num(sk) != -1)
sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
else
ASN1_TYPE_free(aType[i]);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
PKCS8_PRIV_KEY_INFO_free(p8inf_internal);
OPENSSL_free(name);
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
return -1;
}
if (get_oqsname_fromtls(name) !=
0) { // include pubkey in privkey for PQC
memcpy(buf, oqsxkey->comp_privkey[i],
oqsxkey->privkeylen_cmp[i]);
memcpy(buf + oqsxkey->privkeylen_cmp[i],
oqsxkey->comp_pubkey[i], oqsxkey->pubkeylen_cmp[i]);
} else {
memcpy(buf, oqsxkey->comp_privkey[i],
buflen); // buflen for classical (RSA)
// might be different from
// oqsxkey->privkeylen_cmp
}
if (nid == EVP_PKEY_EC) { // add the curve OID with the ECPubkey OID
version = V_ASN1_OBJECT;
pval = OBJ_nid2obj(
oqsxkey->oqsx_provider_ctx.oqsx_evp_ctx->evp_info->nid);
} else {
version = V_ASN1_UNDEF;
pval = NULL;
}
if (nid == EVP_PKEY_ED25519 || nid == EVP_PKEY_ED448) {
oct.data = buf;
oct.length = buflen;
oct.flags = 0;
ed_internal = NULL;
ed_internallen = i2d_ASN1_OCTET_STRING(&oct, &ed_internal);
if (ed_internallen < 0) {
for (int j = 0; j <= i; j++) {
OPENSSL_cleanse(aString[j]->data, aString[j]->length);
ASN1_OCTET_STRING_free(aString[j]);
OPENSSL_cleanse(aType[j]->value.sequence->data,
aType[j]->value.sequence->length);
OPENSSL_clear_free(temp[j], templen[j]);
}
sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
OPENSSL_free(name);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
OPENSSL_cleanse(buf,
buflen); // buf is part of p8inf_internal so
// we cant free now, we cleanse it
// to remove pkey from memory
PKCS8_PRIV_KEY_INFO_free(
p8inf_internal); // this also free buf
return -1;
}
if (!PKCS8_pkey_set0(p8inf_internal, OBJ_nid2obj(nid), 0,
version, pval, ed_internal,
ed_internallen)) {
for (int j = 0; j <= i; j++) {
OPENSSL_cleanse(aString[j]->data, aString[j]->length);
ASN1_OCTET_STRING_free(aString[j]);
OPENSSL_cleanse(aType[j]->value.sequence->data,
aType[j]->value.sequence->length);
OPENSSL_clear_free(temp[j], templen[j]);
}
sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
OPENSSL_free(name);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
OPENSSL_secure_clear_free(buf, buflen);
OPENSSL_cleanse(ed_internal, ed_internallen);
PKCS8_PRIV_KEY_INFO_free(
p8inf_internal); // this also free ed_internal
return -1;
}
} else {
if (!PKCS8_pkey_set0(p8inf_internal, OBJ_nid2obj(nid), 0,
version, pval, buf, buflen)) {
for (int j = 0; j <= i; j++) {
OPENSSL_cleanse(aString[j]->data, aString[j]->length);
ASN1_OCTET_STRING_free(aString[j]);
OPENSSL_cleanse(aType[j]->value.sequence->data,
aType[j]->value.sequence->length);
OPENSSL_clear_free(temp[j], templen[j]);
}
sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
OPENSSL_free(name);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
OPENSSL_cleanse(buf,
buflen); // buf is part of p8inf_internal so
// we cant free now, we cleanse it
// to remove pkey from memory
PKCS8_PRIV_KEY_INFO_free(
p8inf_internal); // this also free buf
return -1;
}
}
templen[i] =
i2d_PKCS8_PRIV_KEY_INFO(p8inf_internal,
&temp[i]); // create the privkey info
// for each individual key
ASN1_STRING_set(aString[i], temp[i],
templen[i]); // add privkey info as ASN1_STRING
ASN1_TYPE_set1(aType[i], V_ASN1_SEQUENCE,
aString[i]); // add the ASN1_STRING into a ANS1_TYPE
// so it can be added into the stack
if (!sk_ASN1_TYPE_push(sk, aType[i])) {
for (int j = 0; j <= i; j++) {
OPENSSL_cleanse(aString[j]->data, aString[j]->length);
ASN1_OCTET_STRING_free(aString[j]);
OPENSSL_cleanse(aType[j]->value.sequence->data,
aType[j]->value.sequence->length);
OPENSSL_clear_free(temp[j], templen[j]);
}
sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
OPENSSL_free(name);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
OPENSSL_cleanse(buf,
buflen); // buf is part of p8inf_internal so we
// cant free now, we cleanse it to
// remove pkey from memory
if (nid == EVP_PKEY_ED25519 || nid == EVP_PKEY_ED448) {
OPENSSL_cleanse(ed_internal, ed_internallen);
OPENSSL_secure_free(
buf); // in this case the ed_internal is
// freed from the pkcs8_free instead
// of buf, so we need to free buf here
}
PKCS8_PRIV_KEY_INFO_free(
p8inf_internal); // this also free buf or ed_internal
return -1;
}
OPENSSL_free(name);
OPENSSL_cleanse(buf, buflen);
if (nid == EVP_PKEY_ED25519 || nid == EVP_PKEY_ED448) {
OPENSSL_cleanse(ed_internal, ed_internallen);
OPENSSL_secure_free(buf); // in this case the ed_internal is
// freed from the pkcs8_free instead
// of buf, so we need to free buf here
}
PKCS8_PRIV_KEY_INFO_free(p8inf_internal);
}
keybloblen = i2d_ASN1_SEQUENCE_ANY(sk, pder);
for (i = 0; i < oqsxkey->numkeys; i++) {
OPENSSL_cleanse(aString[i]->data, aString[i]->length);
ASN1_OCTET_STRING_free(aString[i]);
OPENSSL_cleanse(aType[i]->value.sequence->data,
aType[i]->value.sequence->length);
OPENSSL_clear_free(temp[i], templen[i]);
}
sk_ASN1_TYPE_pop_free(sk, &ASN1_TYPE_free);
OPENSSL_free(aType);
OPENSSL_free(aString);
OPENSSL_free(temp);
OPENSSL_free(templen);
}
return keybloblen;
}
#define oqsx_epki_priv_to_der oqsx_pki_priv_to_der
/*
* OQSX only has PKCS#8 / SubjectPublicKeyInfo
* representation, so we don't define
* oqsx_type_specific_[priv,pub,params]_to_der.
*/
#define oqsx_check_key_type NULL
// OQS provider uses NIDs generated at load time as EVP_type identifiers
// so initially this must be 0 and set to a real value by OBJ_sn2nid later
///// OQS_TEMPLATE_FRAGMENT_ENCODER_DEFINES_START
#define frodo640aes_evp_type 0
#define frodo640aes_input_type "frodo640aes"
#define frodo640aes_pem_type "frodo640aes"
#define p256_frodo640aes_evp_type 0
#define p256_frodo640aes_input_type "p256_frodo640aes"
#define p256_frodo640aes_pem_type "p256_frodo640aes"
#define x25519_frodo640aes_evp_type 0
#define x25519_frodo640aes_input_type "x25519_frodo640aes"
#define x25519_frodo640aes_pem_type "x25519_frodo640aes"
#define frodo640shake_evp_type 0
#define frodo640shake_input_type "frodo640shake"
#define frodo640shake_pem_type "frodo640shake"
#define p256_frodo640shake_evp_type 0
#define p256_frodo640shake_input_type "p256_frodo640shake"
#define p256_frodo640shake_pem_type "p256_frodo640shake"
#define x25519_frodo640shake_evp_type 0
#define x25519_frodo640shake_input_type "x25519_frodo640shake"
#define x25519_frodo640shake_pem_type "x25519_frodo640shake"
#define frodo976aes_evp_type 0
#define frodo976aes_input_type "frodo976aes"
#define frodo976aes_pem_type "frodo976aes"
#define p384_frodo976aes_evp_type 0
#define p384_frodo976aes_input_type "p384_frodo976aes"
#define p384_frodo976aes_pem_type "p384_frodo976aes"
#define x448_frodo976aes_evp_type 0
#define x448_frodo976aes_input_type "x448_frodo976aes"
#define x448_frodo976aes_pem_type "x448_frodo976aes"
#define frodo976shake_evp_type 0
#define frodo976shake_input_type "frodo976shake"