/
pkinit_crypto_openssl.c
5710 lines (5018 loc) · 172 KB
/
pkinit_crypto_openssl.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
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* COPYRIGHT (C) 2006,2007
* THE REGENTS OF THE UNIVERSITY OF MICHIGAN
* ALL RIGHTS RESERVED
*
* Permission is granted to use, copy, create derivative works
* and redistribute this software and such derivative works
* for any purpose, so long as the name of The University of
* Michigan is not used in any advertising or publicity
* pertaining to the use of distribution of this software
* without specific, written prior authorization. If the
* above copyright notice or any other identification of the
* University of Michigan is included in any copy of any
* portion of this software, then the disclaimer below must
* also be included.
*
* THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
* FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
* PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
* MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
* WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
* REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
* FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
* CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
* OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
* IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGES.
*/
#include "k5-int.h"
#include "k5-buf.h"
#include "k5-err.h"
#include "k5-hex.h"
#include "pkinit.h"
#include <dirent.h>
#include <openssl/bn.h>
#include <openssl/dh.h>
#include <openssl/x509.h>
#include <openssl/pkcs7.h>
#include <openssl/pkcs12.h>
#include <openssl/obj_mac.h>
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/asn1.h>
#include <openssl/pem.h>
#include <openssl/asn1t.h>
#include <openssl/cms.h>
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/core_names.h>
#include <openssl/kdf.h>
#include <openssl/decoder.h>
#include <openssl/params.h>
#endif
#define DN_BUF_LEN 256
#define MAX_CREDS_ALLOWED 20
struct _pkinit_cred_info {
char *name;
X509 *cert;
EVP_PKEY *key;
#ifndef WITHOUT_PKCS11
CK_BYTE_PTR cert_id;
int cert_id_len;
#endif
};
typedef struct _pkinit_cred_info *pkinit_cred_info;
struct _pkinit_identity_crypto_context {
pkinit_cred_info creds[MAX_CREDS_ALLOWED+1];
X509 *my_cert; /* selected user or KDC cert */
char *identity; /* identity name for user cert */
EVP_PKEY *my_key; /* selected cert key if in filesystem */
STACK_OF(X509) *trustedCAs; /* available trusted ca certs */
STACK_OF(X509) *intermediateCAs; /* available intermediate ca certs */
STACK_OF(X509_CRL) *revoked; /* available crls */
int pkcs11_method;
krb5_prompter_fct prompter;
void *prompter_data;
#ifndef WITHOUT_PKCS11
char *p11_module_name;
CK_SLOT_ID slotid;
char *token_label;
char *cert_label;
/* These are crypto-specific. */
struct plugin_file_handle *p11_module;
CK_SESSION_HANDLE session;
CK_FUNCTION_LIST_PTR p11;
uint8_t *cert_id;
size_t cert_id_len;
#endif
krb5_boolean defer_id_prompt;
pkinit_deferred_id *deferred_ids;
};
struct _pkinit_plg_crypto_context {
EVP_PKEY *dh_1024;
EVP_PKEY *dh_2048;
EVP_PKEY *dh_4096;
EVP_PKEY *ec_p256;
EVP_PKEY *ec_p384;
EVP_PKEY *ec_p521;
ASN1_OBJECT *id_pkinit_authData;
ASN1_OBJECT *id_pkinit_DHKeyData;
ASN1_OBJECT *id_pkinit_rkeyData;
ASN1_OBJECT *id_pkinit_san;
ASN1_OBJECT *id_ms_san_upn;
ASN1_OBJECT *id_pkinit_KPClientAuth;
ASN1_OBJECT *id_pkinit_KPKdc;
ASN1_OBJECT *id_ms_kp_sc_logon;
ASN1_OBJECT *id_kp_serverAuth;
};
struct _pkinit_req_crypto_context {
X509 *received_cert;
EVP_PKEY *client_pkey;
};
static krb5_error_code pkinit_init_pkinit_oids(pkinit_plg_crypto_context );
static void pkinit_fini_pkinit_oids(pkinit_plg_crypto_context );
static krb5_error_code pkinit_init_dh_params(krb5_context,
pkinit_plg_crypto_context);
static void pkinit_fini_dh_params(pkinit_plg_crypto_context );
static krb5_error_code pkinit_init_certs(pkinit_identity_crypto_context ctx);
static void pkinit_fini_certs(pkinit_identity_crypto_context ctx);
static krb5_error_code pkinit_init_pkcs11(pkinit_identity_crypto_context ctx);
static void pkinit_fini_pkcs11(pkinit_identity_crypto_context ctx);
static krb5_error_code pkinit_sign_data
(krb5_context context, pkinit_identity_crypto_context cryptoctx,
unsigned char *data, unsigned int data_len,
unsigned char **sig, unsigned int *sig_len);
static krb5_error_code create_signature
(unsigned char **, unsigned int *, unsigned char *, unsigned int,
EVP_PKEY *pkey);
#ifdef DEBUG_DH
static void print_dh(DH *, char *);
static void print_pubkey(BIGNUM *, char *);
#endif
static int openssl_callback (int, X509_STORE_CTX *);
static int openssl_callback_ignore_crls (int, X509_STORE_CTX *);
static ASN1_OBJECT * pkinit_pkcs7type2oid
(pkinit_plg_crypto_context plg_cryptoctx, int pkcs7_type);
static krb5_error_code pkinit_create_sequence_of_principal_identifiers
(krb5_context context, pkinit_plg_crypto_context plg_cryptoctx,
pkinit_req_crypto_context req_cryptoctx,
pkinit_identity_crypto_context id_cryptoctx,
int type, krb5_pa_data ***e_data_out);
#ifndef WITHOUT_PKCS11
static krb5_error_code
pkinit_find_private_key(krb5_context context,
pkinit_identity_crypto_context id_cryptoctx,
CK_ATTRIBUTE_TYPE usage,
CK_OBJECT_HANDLE *objp);
static krb5_error_code pkinit_login
(krb5_context context, pkinit_identity_crypto_context id_cryptoctx,
CK_TOKEN_INFO *tip, const char *password);
static krb5_error_code pkinit_open_session
(krb5_context context, pkinit_identity_crypto_context id_cryptoctx);
#ifdef SILLYDECRYPT
CK_RV pkinit_C_Decrypt
(pkinit_identity_crypto_context id_cryptoctx,
CK_BYTE_PTR pEncryptedData, CK_ULONG ulEncryptedDataLen,
CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen);
#endif
static krb5_error_code pkinit_sign_data_pkcs11
(krb5_context context, pkinit_identity_crypto_context id_cryptoctx,
unsigned char *data, unsigned int data_len,
unsigned char **sig, unsigned int *sig_len);
static krb5_error_code p11err(krb5_context context, CK_RV rv, const char *op);
#endif /* WITHOUT_PKCS11 */
static krb5_error_code pkinit_sign_data_fs
(krb5_context context, pkinit_identity_crypto_context id_cryptoctx,
unsigned char *data, unsigned int data_len,
unsigned char **sig, unsigned int *sig_len);
static krb5_error_code
create_krb5_invalidCertificates(krb5_context context,
pkinit_plg_crypto_context plg_cryptoctx,
pkinit_req_crypto_context req_cryptoctx,
pkinit_identity_crypto_context id_cryptoctx,
krb5_external_principal_identifier *** ids);
static krb5_error_code
create_identifiers_from_stack(STACK_OF(X509) *sk,
krb5_external_principal_identifier *** ids);
#if OPENSSL_VERSION_NUMBER < 0x10100000L
/* 1.1 standardizes constructor and destructor names, renaming
* EVP_MD_CTX_{create,destroy} and deprecating ASN1_STRING_data. */
#define EVP_MD_CTX_new EVP_MD_CTX_create
#define EVP_MD_CTX_free EVP_MD_CTX_destroy
#define ASN1_STRING_get0_data ASN1_STRING_data
/*
* 1.1 adds DHX support, which uses the RFC 3279 DomainParameters encoding we
* need for PKINIT. For 1.0 we must use the original DH type when creating
* EVP_PKEY objects.
*/
#define EVP_PKEY_DHX EVP_PKEY_DH
/* 1.1 makes many handle types opaque and adds accessors. Add compatibility
* versions of the new accessors we use for pre-1.1. */
#define OBJ_get0_data(o) ((o)->data)
#define OBJ_length(o) ((o)->length)
#define DH_set0_key compat_dh_set0_key
static int
compat_dh_set0_key(DH *dh, BIGNUM *pub, BIGNUM *priv)
{
if (pub != NULL) {
BN_clear_free(dh->pub_key);
dh->pub_key = pub;
}
if (priv != NULL) {
BN_clear_free(dh->priv_key);
dh->priv_key = priv;
}
return 1;
}
#define DH_get0_key compat_dh_get0_key
static void compat_dh_get0_key(const DH *dh, const BIGNUM **pub,
const BIGNUM **priv)
{
if (pub != NULL)
*pub = dh->pub_key;
if (priv != NULL)
*priv = dh->priv_key;
}
#define EVP_PKEY_get0_DH compat_get0_DH
static DH *
compat_get0_DH(const EVP_PKEY *pkey)
{
if (pkey->type != EVP_PKEY_DH)
return NULL;
return pkey->pkey.dh;
}
#define EVP_PKEY_get0_EC_KEY compat_get0_EC
static EC_KEY *
compat_get0_EC(const EVP_PKEY *pkey)
{
if (pkey->type != EVP_PKEY_EC)
return NULL;
return pkey->pkey.ec;
}
#define ECDSA_SIG_set0 compat_ECDSA_SIG_set0
static int
compat_ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
{
sig->r = r;
sig->s = s;
return 1;
}
/* Return true if the cert c includes a key usage which doesn't include u.
* Define using direct member access for pre-1.1. */
#define ku_reject(c, u) \
(((c)->ex_flags & EXFLAG_KUSAGE) && !((c)->ex_kusage & (u)))
#else /* OPENSSL_VERSION_NUMBER >= 0x10100000L */
/* Return true if the cert x includes a key usage which doesn't include u. */
#define ku_reject(c, u) (!(X509_get_key_usage(c) & (u)))
#endif
#if OPENSSL_VERSION_NUMBER < 0x30000000L
/* OpenSSL 3.0 changes several preferred function names. */
#define EVP_PKEY_parameters_eq EVP_PKEY_cmp_parameters
#define EVP_PKEY_get_size EVP_PKEY_size
#define EVP_PKEY_get_bits EVP_PKEY_bits
#define EVP_PKEY_get_base_id EVP_PKEY_base_id
/*
* Convert *dh to an EVP_PKEY object, taking ownership of *dh and setting it to
* NULL. On error, return NULL and do not take ownership of or change *dh.
* OpenSSL 3.0 deprecates the low-level DH interfaces, so this helper will only
* be used with prior versions.
*/
static EVP_PKEY *
dh_to_pkey(DH **dh)
{
EVP_PKEY *pkey;
pkey = EVP_PKEY_new();
if (pkey == NULL)
return NULL;
if (!EVP_PKEY_assign(pkey, EVP_PKEY_DHX, *dh)) {
EVP_PKEY_free(pkey);
return NULL;
}
*dh = NULL;
return pkey;
}
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
/* Encode a bignum as an ASN.1 integer in DER. */
static int
encode_bn_der(const BIGNUM *bn, uint8_t **der_out, int *len_out)
{
ASN1_INTEGER *intval;
int len;
uint8_t *der = NULL, *outptr;
intval = BN_to_ASN1_INTEGER(bn, NULL);
if (intval == NULL)
return 0;
len = i2d_ASN1_INTEGER(intval, NULL);
if (len > 0 && (outptr = der = malloc(len)) != NULL)
(void)i2d_ASN1_INTEGER(intval, &outptr);
ASN1_INTEGER_free(intval);
if (der == NULL)
return 0;
*der_out = der;
*len_out = len;
return 1;
}
/* Decode an ASN.1 integer, returning a bignum. */
static BIGNUM *
decode_bn_der(const uint8_t *der, size_t len)
{
ASN1_INTEGER *intval;
BIGNUM *bn;
intval = d2i_ASN1_INTEGER(NULL, &der, len);
if (intval == NULL)
return NULL;
bn = ASN1_INTEGER_to_BN(intval, NULL);
ASN1_INTEGER_free(intval);
return bn;
}
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
static EVP_PKEY *
decode_params(const krb5_data *params_der, const char *type)
{
EVP_PKEY *pkey = NULL;
const uint8_t *inptr = (uint8_t *)params_der->data;
size_t len = params_der->length;
OSSL_DECODER_CTX *dctx;
int ok;
dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", "type-specific", type,
EVP_PKEY_KEY_PARAMETERS, NULL, NULL);
if (dctx == NULL)
return NULL;
ok = OSSL_DECODER_from_data(dctx, &inptr, &len);
OSSL_DECODER_CTX_free(dctx);
return ok ? pkey : NULL;
}
static EVP_PKEY *
decode_dh_params(const krb5_data *params_der)
{
return decode_params(params_der, "DHX");
}
#else
static EVP_PKEY *
decode_dh_params(const krb5_data *params_der)
{
const uint8_t *p = (uint8_t *)params_der->data;
DH *dh;
EVP_PKEY *pkey;
dh = d2i_DHxparams(NULL, &p, params_der->length);
pkey = dh_to_pkey(&dh);
DH_free(dh);
return pkey;
}
#endif
static krb5_error_code
encode_spki(EVP_PKEY *pkey, krb5_data *spki_out)
{
krb5_error_code ret = ENOMEM;
int len;
uint8_t *outptr;
len = i2d_PUBKEY(pkey, NULL);
ret = alloc_data(spki_out, len);
if (ret)
goto cleanup;
outptr = (uint8_t *)spki_out->data;
(void)i2d_PUBKEY(pkey, &outptr);
cleanup:
return ret;
}
static EVP_PKEY *
decode_spki(const krb5_data *spki)
{
const uint8_t *inptr = (uint8_t *)spki->data;
return d2i_PUBKEY(NULL, &inptr, spki->length);
}
#else /* OPENSSL_VERSION_NUMBER < 0x10100000L */
/*
* OpenSSL 1.0 has no DHX support, so we need a custom decoder for RFC 3279
* DomainParameters, and we need to use X509_PUBKEY values to marshal
* SubjectPublicKeyInfo.
*/
typedef struct {
ASN1_BIT_STRING *seed;
BIGNUM *counter;
} int_dhvparams;
typedef struct {
BIGNUM *p;
BIGNUM *q;
BIGNUM *g;
BIGNUM *j;
int_dhvparams *vparams;
} int_dhxparams;
ASN1_SEQUENCE(int_dhvparams) = {
ASN1_SIMPLE(int_dhvparams, seed, ASN1_BIT_STRING),
ASN1_SIMPLE(int_dhvparams, counter, BIGNUM)
} ASN1_SEQUENCE_END(int_dhvparams);
ASN1_SEQUENCE(int_dhxparams) = {
ASN1_SIMPLE(int_dhxparams, p, BIGNUM),
ASN1_SIMPLE(int_dhxparams, g, BIGNUM),
ASN1_SIMPLE(int_dhxparams, q, BIGNUM),
ASN1_OPT(int_dhxparams, j, BIGNUM),
ASN1_OPT(int_dhxparams, vparams, int_dhvparams)
} ASN1_SEQUENCE_END(int_dhxparams);
static EVP_PKEY *
decode_dh_params(const krb5_data *params_der)
{
int_dhxparams *params;
DH *dh;
EVP_PKEY *pkey;
const uint8_t *p;
dh = DH_new();
if (dh == NULL)
return NULL;
p = (uint8_t *)params_der->data;
params = (int_dhxparams *)ASN1_item_d2i(NULL, &p, params_der->length,
ASN1_ITEM_rptr(int_dhxparams));
if (params == NULL) {
DH_free(dh);
return NULL;
}
/* Steal p, q, and g from dhparams for dh. Ignore j and vparams. */
dh->p = params->p;
dh->q = params->q;
dh->g = params->g;
params->p = params->q = params->g = NULL;
ASN1_item_free((ASN1_VALUE *)params, ASN1_ITEM_rptr(int_dhxparams));
pkey = dh_to_pkey(&dh);
DH_free(dh);
return pkey;
}
static krb5_error_code
encode_spki(EVP_PKEY *pkey, krb5_data *spki_out)
{
krb5_error_code ret = ENOMEM;
const DH *dh;
uint8_t *param_der = NULL, *pubkey_der = NULL, *outptr;
int param_der_len, pubkey_der_len, len;
X509_PUBKEY pubkey;
int_dhxparams dhxparams;
X509_ALGOR algor;
ASN1_OBJECT algorithm;
ASN1_TYPE parameter;
ASN1_STRING param_str, pubkey_str;
if (EVP_PKEY_base_id(pkey) != EVP_PKEY_DH) {
/* Only DH keys require special encoding. */
len = i2d_PUBKEY(pkey, NULL);
ret = alloc_data(spki_out, len);
if (ret)
goto cleanup;
outptr = (uint8_t *)spki_out->data;
(void)i2d_PUBKEY(pkey, &outptr);
return 0;
}
dh = EVP_PKEY_get0_DH(pkey);
if (dh == NULL)
goto cleanup;
dhxparams.p = dh->p;
dhxparams.q = dh->q;
dhxparams.g = dh->g;
dhxparams.j = NULL;
dhxparams.vparams = NULL;
param_der_len = ASN1_item_i2d((ASN1_VALUE *)&dhxparams, ¶m_der,
ASN1_ITEM_rptr(int_dhxparams));
if (param_der_len < 0)
goto cleanup;
param_str.length = param_der_len;
param_str.type = V_ASN1_SEQUENCE;
param_str.data = param_der;
param_str.flags = 0;
parameter.type = V_ASN1_SEQUENCE;
parameter.value.sequence = ¶m_str;
memset(&algorithm, 0, sizeof(algorithm));
algorithm.data = (uint8_t *)dh_oid.data;
algorithm.length = dh_oid.length;
algor.algorithm = &algorithm;
algor.parameter = ¶meter;
if (!encode_bn_der(dh->pub_key, &pubkey_der, &pubkey_der_len))
goto cleanup;
pubkey_str.length = pubkey_der_len;
pubkey_str.type = V_ASN1_BIT_STRING;
pubkey_str.data = pubkey_der;
pubkey_str.flags = ASN1_STRING_FLAG_BITS_LEFT;
pubkey.algor = &algor;
pubkey.public_key = &pubkey_str;
len = i2d_X509_PUBKEY(&pubkey, NULL);
if (len < 0)
goto cleanup;
ret = alloc_data(spki_out, len);
if (ret)
goto cleanup;
outptr = (uint8_t *)spki_out->data;
i2d_X509_PUBKEY(&pubkey, &outptr);
cleanup:
OPENSSL_free(param_der);
free(pubkey_der);
return ret;
}
static EVP_PKEY *
decode_spki(const krb5_data *spki)
{
X509_PUBKEY *pubkey = NULL;
const uint8_t *inptr;
DH *dh;
EVP_PKEY *pkey = NULL, *pkey_ret = NULL;
const ASN1_STRING *params;
const ASN1_BIT_STRING *public_key;
krb5_data d;
inptr = (uint8_t *)spki->data;
pubkey = d2i_X509_PUBKEY(NULL, &inptr, spki->length);
if (pubkey == NULL)
goto cleanup;
if (OBJ_cmp(pubkey->algor->algorithm, OBJ_nid2obj(NID_dhKeyAgreement))) {
/* This is not a DH key, so we don't need special decoding. */
X509_PUBKEY_free(pubkey);
inptr = (uint8_t *)spki->data;
return d2i_PUBKEY(NULL, &inptr, spki->length);
}
if (pubkey->algor->parameter->type != V_ASN1_SEQUENCE)
goto cleanup;
params = pubkey->algor->parameter->value.sequence;
d = make_data(params->data, params->length);
pkey = decode_dh_params(&d);
if (pkey == NULL)
goto cleanup;
dh = EVP_PKEY_get0_DH(pkey);
if (dh == NULL)
goto cleanup;
public_key = pubkey->public_key;
dh->pub_key = decode_bn_der(public_key->data, public_key->length);
if (dh->pub_key == NULL)
goto cleanup;
pkey_ret = pkey;
pkey = NULL;
cleanup:
X509_PUBKEY_free(pubkey);
EVP_PKEY_free(pkey);
return pkey_ret;
}
#endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
static EVP_PKEY *
decode_ec_params(const krb5_data *params_der)
{
return decode_params(params_der, "EC");
}
#else /* OPENSSL_VERSION_NUMBER < 0x30000000L */
static EVP_PKEY *
decode_ec_params(const krb5_data *params_der)
{
const uint8_t *p = (uint8_t *)params_der->data;
EC_KEY *eckey;
EVP_PKEY *pkey;
eckey = d2i_ECParameters(NULL, &p, params_der->length);
if (eckey == NULL)
return NULL;
pkey = EVP_PKEY_new();
if (pkey != NULL) {
if (!EVP_PKEY_set1_EC_KEY(pkey, eckey)) {
EVP_PKEY_free(pkey);
pkey = NULL;
}
}
EC_KEY_free(eckey);
return pkey;
}
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
/* Attempt to specify padded Diffie-Hellman result derivation. Don't error out
* if this fails since we also detect short results and adjust them. */
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
static void
set_padded_derivation(EVP_PKEY_CTX *ctx)
{
EVP_PKEY_CTX_set_dh_pad(ctx, 1);
}
#elif OPENSSL_VERSION_NUMBER >= 0x10100000L
static void
set_padded_derivation(EVP_PKEY_CTX *ctx)
{
/* We would use EVP_PKEY_CTX_set_dh_pad() but it doesn't work with DHX. */
EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_DH_PAD, 1, NULL);
}
#else
static void
set_padded_derivation(EVP_PKEY_CTX *ctx)
{
/* There's no support for padded derivation in 1.0. */
}
#endif
static int
dh_result(EVP_PKEY *pkey, EVP_PKEY *peer,
uint8_t **result_out, unsigned int *len_out)
{
EVP_PKEY_CTX *derive_ctx = NULL;
int ok = 0;
uint8_t *buf = NULL;
size_t len, result_size;
krb5_boolean ecc = (EVP_PKEY_id(pkey) == EVP_PKEY_EC);
*result_out = NULL;
*len_out = 0;
derive_ctx = EVP_PKEY_CTX_new(pkey, NULL);
if (derive_ctx == NULL)
goto cleanup;
if (EVP_PKEY_derive_init(derive_ctx) <= 0)
goto cleanup;
if (!ecc)
set_padded_derivation(derive_ctx);
if (EVP_PKEY_derive_set_peer(derive_ctx, peer) <= 0)
goto cleanup;
if (ecc) {
if (EVP_PKEY_derive(derive_ctx, NULL, &result_size) <= 0)
goto cleanup;
} else {
/*
* For finite-field Diffie-Hellman we must ensure that the result
* matches the key size (normally through padded derivation, but that
* isn't supported by OpenSSL 1.0 so we must check).
*/
result_size = EVP_PKEY_get_size(pkey);
}
buf = malloc(result_size);
if (buf == NULL)
goto cleanup;
len = result_size;
if (EVP_PKEY_derive(derive_ctx, buf, &len) <= 0)
goto cleanup;
/* If we couldn't specify padded derivation for finite-field DH we may need
* to fix up the result by right-shifting it within the buffer. */
if (len < result_size) {
memmove(buf + (result_size - len), buf, len);
memset(buf, 0, result_size - len);
}
ok = 1;
*result_out = buf;
*len_out = result_size;
buf = NULL;
cleanup:
EVP_PKEY_CTX_free(derive_ctx);
free(buf);
return ok;
}
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
static int
dh_pubkey_der(EVP_PKEY *pkey, uint8_t **pubkey_out, unsigned int *len_out)
{
BIGNUM *pubkey_bn = NULL;
int len, ok = 0;
uint8_t *buf, *outptr;
if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
len = i2d_PublicKey(pkey, NULL);
if (len > 0 && (outptr = buf = malloc(len)) != NULL) {
(void)i2d_PublicKey(pkey, &outptr);
ok = 1;
}
} else {
if (!EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PUB_KEY, &pubkey_bn))
return 0;
ok = encode_bn_der(pubkey_bn, &buf, &len);
BN_free(pubkey_bn);
}
if (ok) {
*pubkey_out = buf;
*len_out = len;
}
return ok;
}
#else
static int
dh_pubkey_der(EVP_PKEY *pkey, uint8_t **pubkey_out, unsigned int *len_out)
{
const DH *dh;
EC_KEY *eckey; /* can be const when OpenSSL 1.0 dropped */
const BIGNUM *pubkey_bn;
uint8_t *buf, *outptr;
int len;
dh = EVP_PKEY_get0_DH(pkey);
if (dh != NULL) {
DH_get0_key(dh, &pubkey_bn, NULL);
if (!encode_bn_der(pubkey_bn, &buf, &len))
return 0;
*pubkey_out = buf;
*len_out = len;
return 1;
}
eckey = EVP_PKEY_get0_EC_KEY(pkey);
if (eckey != NULL) {
len = i2o_ECPublicKey(eckey, NULL);
if (len > 0 && (outptr = buf = malloc(len)) != NULL) {
(void)i2o_ECPublicKey(eckey, &outptr);
*pubkey_out = buf;
*len_out = len;
return 1;
}
}
return 0;
}
#endif
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
/* OpenSSL 1.1 and later will copy the q parameter when generating keys. */
static int
copy_q_openssl10(EVP_PKEY *src, EVP_PKEY *dest)
{
return 1;
}
#else
/* OpenSSL 1.0 won't copy the q parameter, so we have to do it. */
static int
copy_q_openssl10(EVP_PKEY *src, EVP_PKEY *dest)
{
DH *dhsrc = EVP_PKEY_get0_DH(src), *dhdest = EVP_PKEY_get0_DH(dest);
if (dhsrc == NULL || dhsrc->q == NULL || dhdest == NULL)
return 0;
if (dhdest->q != NULL)
return 1;
dhdest->q = BN_dup(dhsrc->q);
return dhdest->q != NULL;
}
#endif
static EVP_PKEY *
generate_dh_pkey(EVP_PKEY *params)
{
EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY *pkey = NULL;
ctx = EVP_PKEY_CTX_new(params, NULL);
if (ctx == NULL)
goto cleanup;
if (EVP_PKEY_keygen_init(ctx) <= 0)
goto cleanup;
if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
goto cleanup;
if (EVP_PKEY_get_base_id(pkey) == EVP_PKEY_DH &&
!copy_q_openssl10(params, pkey)) {
EVP_PKEY_free(pkey);
pkey = NULL;
}
cleanup:
EVP_PKEY_CTX_free(ctx);
return pkey;
}
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
static EVP_PKEY *
compose_dh_pkey(EVP_PKEY *params, const uint8_t *pubkey_der, size_t der_len)
{
EVP_PKEY *pkey = NULL, *pkey_ret = NULL;
BIGNUM *pubkey_bn = NULL;
uint8_t *pubkey_bin = NULL;
int binlen;
pkey = EVP_PKEY_dup(params);
if (pkey == NULL)
goto cleanup;
if (EVP_PKEY_id(params) == EVP_PKEY_EC) {
if (d2i_PublicKey(EVP_PKEY_id(params), &pkey, &pubkey_der,
der_len) == NULL)
goto cleanup;
} else {
pubkey_bn = decode_bn_der(pubkey_der, der_len);
if (pubkey_bn == NULL)
goto cleanup;
binlen = EVP_PKEY_get_size(pkey);
pubkey_bin = malloc(binlen);
if (pubkey_bin == NULL)
goto cleanup;
if (BN_bn2binpad(pubkey_bn, pubkey_bin, binlen) != binlen)
goto cleanup;
if (EVP_PKEY_set1_encoded_public_key(pkey, pubkey_bin, binlen) != 1)
goto cleanup;
}
pkey_ret = pkey;
pkey = NULL;
cleanup:
EVP_PKEY_free(pkey);
BN_free(pubkey_bn);
free(pubkey_bin);
return pkey_ret;
}
#else /* OPENSSL_VERSION_NUMBER < 0x30000000L */
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
static DH *
dup_dh_params(DH *src)
{
return DHparams_dup(src);
}
#else
/* DHparams_dup() won't copy q in OpenSSL 1.0. */
static DH *
dup_dh_params(DH *src)
{
DH *dh;
dh = DH_new();
if (dh == NULL)
return NULL;
dh->p = BN_dup(src->p);
dh->q = BN_dup(src->q);
dh->g = BN_dup(src->g);
if (dh->p == NULL || dh->q == NULL || dh->g == NULL) {
DH_free(dh);
return NULL;
}
return dh;
}
#endif
static EVP_PKEY *
compose_dh_pkey(EVP_PKEY *params, const uint8_t *pubkey_der, size_t der_len)
{
DH *dhparams, *dh = NULL;
EVP_PKEY *pkey = NULL, *pkey_ret = NULL;
BIGNUM *pubkey_bn = NULL;
EC_KEY *params_eckey, *eckey = NULL;
const EC_GROUP *group;
if (EVP_PKEY_id(params) == EVP_PKEY_EC) {
/* We would like to use EVP_PKEY_copy_parameters() and d2i_PublicKey(),
* but the latter is broken in OpenSSL 1.1.0-1.1.1a for EC keys. */
params_eckey = EVP_PKEY_get0_EC_KEY(params);
if (params_eckey == NULL)
goto cleanup;
group = EC_KEY_get0_group(params_eckey);
eckey = EC_KEY_new();
if (eckey == NULL)
goto cleanup;
if (!EC_KEY_set_group(eckey, group))
goto cleanup;
if (o2i_ECPublicKey(&eckey, &pubkey_der, der_len) == NULL)
goto cleanup;
pkey = EVP_PKEY_new();
if (pkey == NULL)
return NULL;
if (!EVP_PKEY_assign(pkey, EVP_PKEY_EC, eckey)) {
EVP_PKEY_free(pkey);
return NULL;
}
eckey = NULL;
} else {
pubkey_bn = decode_bn_der(pubkey_der, der_len);
if (pubkey_bn == NULL)
goto cleanup;
dhparams = EVP_PKEY_get0_DH(params);
if (dhparams == NULL)
goto cleanup;
dh = dup_dh_params(dhparams);
if (dh == NULL)
goto cleanup;
if (!DH_set0_key(dh, pubkey_bn, NULL))
goto cleanup;
pubkey_bn = NULL;
pkey = dh_to_pkey(&dh);
}
pkey_ret = pkey;
pkey = NULL;
cleanup:
BN_free(pubkey_bn);
DH_free(dh);
EC_KEY_free(eckey);
EVP_PKEY_free(pkey);
return pkey_ret;
}
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
#ifndef WITHOUT_PKC11
static struct pkcs11_errstrings {
CK_RV code;
char *text;
} pkcs11_errstrings[] = {
{ 0x0, "ok" },
{ 0x1, "cancel" },
{ 0x2, "host memory" },
{ 0x3, "slot id invalid" },
{ 0x5, "general error" },
{ 0x6, "function failed" },
{ 0x7, "arguments bad" },
{ 0x8, "no event" },
{ 0x9, "need to create threads" },
{ 0xa, "cant lock" },
{ 0x10, "attribute read only" },
{ 0x11, "attribute sensitive" },
{ 0x12, "attribute type invalid" },
{ 0x13, "attribute value invalid" },
{ 0x20, "data invalid" },
{ 0x21, "data len range" },
{ 0x30, "device error" },
{ 0x31, "device memory" },
{ 0x32, "device removed" },