-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathopenssl.c
More file actions
5225 lines (4388 loc) · 145 KB
/
openssl.c
File metadata and controls
5225 lines (4388 loc) · 145 KB
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
/* Copyright (C) Simon Josefsson
* Copyright (C) The Written Word, Inc.
* Copyright (C) Sara Golemon <sarag@libssh2.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* Neither the name of the copyright holder nor the names
* of any other contributors may be used to endorse or
* promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifdef LIBSSH2_CRYPTO_C /* Compile this via crypto.c */
#include <stdlib.h>
#include <assert.h>
int _libssh2_hmac_ctx_init(libssh2_hmac_ctx *ctx)
{
#ifdef USE_OPENSSL_3
*ctx = NULL;
return 1;
#elif defined(HAVE_OPAQUE_STRUCTS)
*ctx = HMAC_CTX_new();
return *ctx ? 1 : 0;
#else
HMAC_CTX_init(ctx);
return 1;
#endif
}
#ifdef USE_OPENSSL_3
static int _libssh2_hmac_init(libssh2_hmac_ctx *ctx,
void *key, size_t keylen,
const char *digest_name)
{
EVP_MAC* mac;
OSSL_PARAM params[3];
mac = EVP_MAC_fetch(NULL, OSSL_MAC_NAME_HMAC, NULL);
if(!mac)
return 0;
*ctx = EVP_MAC_CTX_new(mac);
EVP_MAC_free(mac);
if(!*ctx)
return 0;
params[0] = OSSL_PARAM_construct_octet_string(
OSSL_MAC_PARAM_KEY, (void *)key, keylen);
params[1] = OSSL_PARAM_construct_utf8_string(
OSSL_MAC_PARAM_DIGEST, (char *)digest_name, 0);
params[2] = OSSL_PARAM_construct_end();
return EVP_MAC_init(*ctx, NULL, 0, params);
}
#endif
#if LIBSSH2_MD5
int _libssh2_hmac_md5_init(libssh2_hmac_ctx *ctx,
void *key, size_t keylen)
{
#ifdef USE_OPENSSL_3
return _libssh2_hmac_init(ctx, key, keylen, OSSL_DIGEST_NAME_MD5);
#elif defined(HAVE_OPAQUE_STRUCTS)
return HMAC_Init_ex(*ctx, key, (int)keylen, EVP_md5(), NULL);
#else
return HMAC_Init_ex(ctx, key, (int)keylen, EVP_md5(), NULL);
#endif
}
#endif
#if LIBSSH2_HMAC_RIPEMD
int _libssh2_hmac_ripemd160_init(libssh2_hmac_ctx *ctx,
void *key, size_t keylen)
{
#ifdef USE_OPENSSL_3
return _libssh2_hmac_init(ctx, key, keylen, OSSL_DIGEST_NAME_RIPEMD160);
#elif defined(HAVE_OPAQUE_STRUCTS)
return HMAC_Init_ex(*ctx, key, (int)keylen, EVP_ripemd160(), NULL);
#else
return HMAC_Init_ex(ctx, key, (int)keylen, EVP_ripemd160(), NULL);
#endif
}
#endif
int _libssh2_hmac_sha1_init(libssh2_hmac_ctx *ctx,
void *key, size_t keylen)
{
#ifdef USE_OPENSSL_3
return _libssh2_hmac_init(ctx, key, keylen, OSSL_DIGEST_NAME_SHA1);
#elif defined(HAVE_OPAQUE_STRUCTS)
return HMAC_Init_ex(*ctx, key, (int)keylen, EVP_sha1(), NULL);
#else
return HMAC_Init_ex(ctx, key, (int)keylen, EVP_sha1(), NULL);
#endif
}
int _libssh2_hmac_sha256_init(libssh2_hmac_ctx *ctx,
void *key, size_t keylen)
{
#ifdef USE_OPENSSL_3
return _libssh2_hmac_init(ctx, key, keylen, OSSL_DIGEST_NAME_SHA2_256);
#elif defined(HAVE_OPAQUE_STRUCTS)
return HMAC_Init_ex(*ctx, key, (int)keylen, EVP_sha256(), NULL);
#else
return HMAC_Init_ex(ctx, key, (int)keylen, EVP_sha256(), NULL);
#endif
}
int _libssh2_hmac_sha512_init(libssh2_hmac_ctx *ctx,
void *key, size_t keylen)
{
#ifdef USE_OPENSSL_3
return _libssh2_hmac_init(ctx, key, keylen, OSSL_DIGEST_NAME_SHA2_512);
#elif defined(HAVE_OPAQUE_STRUCTS)
return HMAC_Init_ex(*ctx, key, (int)keylen, EVP_sha512(), NULL);
#else
return HMAC_Init_ex(ctx, key, (int)keylen, EVP_sha512(), NULL);
#endif
}
int _libssh2_hmac_update(libssh2_hmac_ctx *ctx,
const void *data, size_t datalen)
{
#ifdef USE_OPENSSL_3
return EVP_MAC_update(*ctx, data, datalen);
#elif defined(HAVE_OPAQUE_STRUCTS)
/* FIXME: upstream bug as of v5.7.0: datalen is int instead of size_t */
#if defined(LIBSSH2_WOLFSSL)
return HMAC_Update(*ctx, data, (int)datalen);
#else /* !LIBSSH2_WOLFSSL */
return HMAC_Update(*ctx, data, datalen);
#endif /* LIBSSH2_WOLFSSL */
#else
return HMAC_Update(ctx, data, datalen);
#endif
}
int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *data)
{
#ifdef USE_OPENSSL_3
return EVP_MAC_final(*ctx, data, NULL, MAX_MACSIZE);
#elif defined(HAVE_OPAQUE_STRUCTS)
return HMAC_Final(*ctx, data, NULL);
#else
return HMAC_Final(ctx, data, NULL);
#endif
}
void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx)
{
#ifdef USE_OPENSSL_3
EVP_MAC_CTX_free(*ctx);
#elif defined(HAVE_OPAQUE_STRUCTS)
HMAC_CTX_free(*ctx);
#else
HMAC_cleanup(ctx);
#endif
}
static int
_libssh2_pub_priv_openssh_keyfilememory(LIBSSH2_SESSION *session,
void **key_ctx,
const char *key_type,
unsigned char **method,
size_t *method_len,
unsigned char **pubkeydata,
size_t *pubkeydata_len,
const char *privatekeydata,
size_t privatekeydata_len,
unsigned const char *passphrase);
static int
_libssh2_sk_pub_openssh_keyfilememory(LIBSSH2_SESSION *session,
void **key_ctx,
const char *key_type,
unsigned char **method,
size_t *method_len,
unsigned char **pubkeydata,
size_t *pubkeydata_len,
int *algorithm,
unsigned char *flags,
const char **application,
const unsigned char **key_handle,
size_t *handle_len,
const char *privatekeydata,
size_t privatekeydata_len,
unsigned const char *passphrase);
#if LIBSSH2_RSA || LIBSSH2_DSA || LIBSSH2_ECDSA
static unsigned char *
write_bn(unsigned char *buf, const BIGNUM *bn, int bn_bytes)
{
unsigned char *p = buf;
/* Left space for bn size which will be written below. */
p += 4;
*p = 0;
BN_bn2bin(bn, p + 1);
if(!(*(p + 1) & 0x80)) {
memmove(p, p + 1, --bn_bytes);
}
_libssh2_htonu32(p - 4, bn_bytes); /* Post write bn size. */
return p + bn_bytes;
}
#endif
static inline void
_libssh2_swap_bytes(unsigned char *buf, unsigned long len)
{
#if !defined(WORDS_BIGENDIAN) || !WORDS_BIGENDIAN
unsigned long i, j;
unsigned char temp;
for(i = 0, j = len - 1; i < j; i++, j--) {
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
#endif
}
int
_libssh2_openssl_random(void *buf, size_t len)
{
if(len > INT_MAX) {
return -1;
}
return RAND_bytes(buf, (int)len) == 1 ? 0 : -1;
}
#if LIBSSH2_RSA
int
_libssh2_rsa_new(libssh2_rsa_ctx ** rsa,
const unsigned char *edata,
unsigned long elen,
const unsigned char *ndata,
unsigned long nlen,
const unsigned char *ddata,
unsigned long dlen,
const unsigned char *pdata,
unsigned long plen,
const unsigned char *qdata,
unsigned long qlen,
const unsigned char *e1data,
unsigned long e1len,
const unsigned char *e2data,
unsigned long e2len,
const unsigned char *coeffdata, unsigned long coefflen)
{
#ifdef USE_OPENSSL_3
int ret = 0;
EVP_PKEY_CTX *ctx;
OSSL_PARAM params[4];
int param_num = 0;
unsigned char *nbuf = NULL;
unsigned char *ebuf = NULL;
unsigned char *dbuf = NULL;
(void)pdata;
(void)plen;
(void)qdata;
(void)qlen;
(void)e1data;
(void)e1len;
(void)e2data;
(void)e2len;
(void)coeffdata;
(void)coefflen;
if(ndata && nlen > 0) {
nbuf = OPENSSL_malloc(nlen);
if(nbuf) {
memcpy(nbuf, ndata, nlen);
_libssh2_swap_bytes(nbuf, nlen);
params[param_num++] =
OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_RSA_N, nbuf, nlen);
}
}
if(edata && elen > 0) {
ebuf = OPENSSL_malloc(elen);
if(ebuf) {
memcpy(ebuf, edata, elen);
_libssh2_swap_bytes(ebuf, elen);
params[param_num++] =
OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_RSA_E, ebuf, elen);
}
}
if(ddata && dlen > 0) {
dbuf = OPENSSL_malloc(dlen);
if(dbuf) {
memcpy(dbuf, ddata, dlen);
_libssh2_swap_bytes(dbuf, dlen);
params[param_num++] =
OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_RSA_D, dbuf, dlen);
}
}
params[param_num] = OSSL_PARAM_construct_end();
*rsa = NULL;
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if(EVP_PKEY_fromdata_init(ctx) > 0) {
ret = EVP_PKEY_fromdata(ctx, rsa, EVP_PKEY_KEYPAIR, params);
}
if(nbuf)
OPENSSL_clear_free(nbuf, nlen);
if(ebuf)
OPENSSL_clear_free(ebuf, elen);
if(dbuf)
OPENSSL_clear_free(dbuf, dlen);
EVP_PKEY_CTX_free(ctx);
return (ret == 1) ? 0 : -1;
#else
BIGNUM * e;
BIGNUM * n;
BIGNUM * d = 0;
BIGNUM * p = 0;
BIGNUM * q = 0;
BIGNUM * dmp1 = 0;
BIGNUM * dmq1 = 0;
BIGNUM * iqmp = 0;
e = BN_new();
BN_bin2bn(edata, (int) elen, e);
n = BN_new();
BN_bin2bn(ndata, (int) nlen, n);
if(ddata) {
d = BN_new();
BN_bin2bn(ddata, (int) dlen, d);
p = BN_new();
BN_bin2bn(pdata, (int) plen, p);
q = BN_new();
BN_bin2bn(qdata, (int) qlen, q);
dmp1 = BN_new();
BN_bin2bn(e1data, (int) e1len, dmp1);
dmq1 = BN_new();
BN_bin2bn(e2data, (int) e2len, dmq1);
iqmp = BN_new();
BN_bin2bn(coeffdata, (int) coefflen, iqmp);
}
*rsa = RSA_new();
#ifdef HAVE_OPAQUE_STRUCTS
RSA_set0_key(*rsa, n, e, d);
#else
(*rsa)->e = e;
(*rsa)->n = n;
(*rsa)->d = d;
#endif
#ifdef HAVE_OPAQUE_STRUCTS
RSA_set0_factors(*rsa, p, q);
#else
(*rsa)->p = p;
(*rsa)->q = q;
#endif
#ifdef HAVE_OPAQUE_STRUCTS
RSA_set0_crt_params(*rsa, dmp1, dmq1, iqmp);
#else
(*rsa)->dmp1 = dmp1;
(*rsa)->dmq1 = dmq1;
(*rsa)->iqmp = iqmp;
#endif
return 0;
#endif /* USE_OPENSSL_3 */
}
int
_libssh2_rsa_sha2_verify(libssh2_rsa_ctx * rsactx,
size_t hash_len,
const unsigned char *sig,
size_t sig_len,
const unsigned char *m, size_t m_len)
{
#ifdef USE_OPENSSL_3
EVP_PKEY_CTX *ctx = NULL;
const EVP_MD *md = NULL;
#endif
int ret;
int nid_type;
unsigned char *hash = malloc(hash_len);
if(!hash)
return -1;
if(hash_len == SHA_DIGEST_LENGTH) {
nid_type = NID_sha1;
ret = _libssh2_sha1(m, m_len, hash);
}
else if(hash_len == SHA256_DIGEST_LENGTH) {
nid_type = NID_sha256;
ret = _libssh2_sha256(m, m_len, hash);
}
else if(hash_len == SHA512_DIGEST_LENGTH) {
nid_type = NID_sha512;
ret = _libssh2_sha512(m, m_len, hash);
}
else {
/* silence:
warning C4701: potentially uninitialized local variable 'nid_type' used */
#if defined(_MSC_VER)
nid_type = 0;
#endif
ret = -1; /* unsupported digest */
}
if(ret) {
free(hash);
return -1; /* failure */
}
#ifdef USE_OPENSSL_3
ctx = EVP_PKEY_CTX_new(rsactx, NULL);
if(nid_type == NID_sha1) {
md = EVP_sha1();
}
else if(nid_type == NID_sha256) {
md = EVP_sha256();
}
else if(nid_type == NID_sha512) {
md = EVP_sha512();
}
if(ctx && md) {
if(EVP_PKEY_verify_init(ctx) > 0 &&
EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) > 0 &&
EVP_PKEY_CTX_set_signature_md(ctx, md) > 0) {
ret = EVP_PKEY_verify(ctx, sig, sig_len, hash, hash_len);
}
}
if(ctx) {
EVP_PKEY_CTX_free(ctx);
}
#else
ret = RSA_verify(nid_type, hash, (unsigned int) hash_len,
(unsigned char *) sig,
(unsigned int) sig_len, rsactx);
#endif
free(hash);
return (ret == 1) ? 0 : -1;
}
#if LIBSSH2_RSA_SHA1
int
_libssh2_rsa_sha1_verify(libssh2_rsa_ctx * rsactx,
const unsigned char *sig,
size_t sig_len,
const unsigned char *m, size_t m_len)
{
return _libssh2_rsa_sha2_verify(rsactx, SHA_DIGEST_LENGTH, sig, sig_len, m,
m_len);
}
#endif
#endif
#if LIBSSH2_DSA
int
_libssh2_dsa_new(libssh2_dsa_ctx ** dsactx,
const unsigned char *p,
unsigned long p_len,
const unsigned char *q,
unsigned long q_len,
const unsigned char *g,
unsigned long g_len,
const unsigned char *y,
unsigned long y_len,
const unsigned char *x, unsigned long x_len)
{
#ifdef USE_OPENSSL_3
int ret = 0;
EVP_PKEY_CTX *ctx = NULL;
OSSL_PARAM params[6];
int param_num = 0;
unsigned char *p_buf = NULL;
unsigned char *q_buf = NULL;
unsigned char *g_buf = NULL;
unsigned char *y_buf = NULL;
unsigned char *x_buf = NULL;
if(p && p_len > 0) {
p_buf = OPENSSL_malloc(p_len);
if(p_buf) {
memcpy(p_buf, p, p_len);
_libssh2_swap_bytes(p_buf, p_len);
params[param_num++] =
OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_FFC_P, p_buf, p_len);
}
}
if(q && q_len > 0) {
q_buf = OPENSSL_malloc(q_len);
if(q_buf) {
memcpy(q_buf, q, q_len);
_libssh2_swap_bytes(q_buf, q_len);
params[param_num++] =
OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_FFC_Q, q_buf, q_len);
}
}
if(g && g_len > 0) {
g_buf = OPENSSL_malloc(g_len);
if(g_buf) {
memcpy(g_buf, g, g_len);
_libssh2_swap_bytes(g_buf, g_len);
params[param_num++] =
OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_FFC_G, g_buf, g_len);
}
}
if(y && y_len > 0) {
y_buf = OPENSSL_malloc(y_len);
if(y_buf) {
memcpy(y_buf, y, y_len);
_libssh2_swap_bytes(y_buf, y_len);
params[param_num++] =
OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_PUB_KEY, y_buf, y_len);
}
}
if(x && x_len > 0) {
x_buf = OPENSSL_malloc(x_len);
if(x_buf) {
memcpy(x_buf, x, x_len);
_libssh2_swap_bytes(x_buf, x_len);
params[param_num++] =
OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_PRIV_KEY,
x_buf, x_len);
}
}
params[param_num] = OSSL_PARAM_construct_end();
*dsactx = NULL;
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DSA, NULL);
if(EVP_PKEY_fromdata_init(ctx) > 0) {
ret = EVP_PKEY_fromdata(ctx, dsactx, EVP_PKEY_KEYPAIR, params);
}
if(p_buf)
OPENSSL_clear_free(p_buf, p_len);
if(q_buf)
OPENSSL_clear_free(q_buf, q_len);
if(g_buf)
OPENSSL_clear_free(g_buf, g_len);
if(x_buf)
OPENSSL_clear_free(x_buf, x_len);
if(y_buf)
OPENSSL_clear_free(y_buf, y_len);
return (ret == 1) ? 0 : -1;
#else
BIGNUM * p_bn;
BIGNUM * q_bn;
BIGNUM * g_bn;
BIGNUM * pub_key;
BIGNUM * priv_key = NULL;
p_bn = BN_new();
BN_bin2bn(p, (int) p_len, p_bn);
q_bn = BN_new();
BN_bin2bn(q, (int) q_len, q_bn);
g_bn = BN_new();
BN_bin2bn(g, (int) g_len, g_bn);
pub_key = BN_new();
BN_bin2bn(y, (int) y_len, pub_key);
if(x_len) {
priv_key = BN_new();
BN_bin2bn(x, (int) x_len, priv_key);
}
*dsactx = DSA_new();
#ifdef HAVE_OPAQUE_STRUCTS
DSA_set0_pqg(*dsactx, p_bn, q_bn, g_bn);
#else
(*dsactx)->p = p_bn;
(*dsactx)->g = g_bn;
(*dsactx)->q = q_bn;
#endif
#ifdef HAVE_OPAQUE_STRUCTS
DSA_set0_key(*dsactx, pub_key, priv_key);
#else
(*dsactx)->pub_key = pub_key;
(*dsactx)->priv_key = priv_key;
#endif
return 0;
#endif /* USE_OPENSSL_3 */
}
int
_libssh2_dsa_sha1_verify(libssh2_dsa_ctx * dsactx,
const unsigned char *sig,
const unsigned char *m, size_t m_len)
{
#ifdef USE_OPENSSL_3
EVP_PKEY_CTX *ctx = NULL;
unsigned char *der = NULL;
int der_len = 0;
#endif
unsigned char hash[SHA_DIGEST_LENGTH];
DSA_SIG * dsasig;
BIGNUM * r;
BIGNUM * s;
int ret = -1;
r = BN_new();
BN_bin2bn(sig, 20, r);
s = BN_new();
BN_bin2bn(sig + 20, 20, s);
dsasig = DSA_SIG_new();
#ifdef HAVE_OPAQUE_STRUCTS
DSA_SIG_set0(dsasig, r, s);
#else
dsasig->r = r;
dsasig->s = s;
#endif
#ifdef USE_OPENSSL_3
ctx = EVP_PKEY_CTX_new(dsactx, NULL);
der_len = i2d_DSA_SIG(dsasig, &der);
if(ctx && !_libssh2_sha1(m, m_len, hash)) {
/* _libssh2_sha1() succeeded */
if(EVP_PKEY_verify_init(ctx) > 0) {
ret = EVP_PKEY_verify(ctx, der, der_len, hash, SHA_DIGEST_LENGTH);
}
}
if(ctx) {
EVP_PKEY_CTX_free(ctx);
}
if(der) {
OPENSSL_clear_free(der, der_len);
}
#else
if(!_libssh2_sha1(m, m_len, hash))
/* _libssh2_sha1() succeeded */
ret = DSA_do_verify(hash, SHA_DIGEST_LENGTH, dsasig, dsactx);
#endif
DSA_SIG_free(dsasig);
return (ret == 1) ? 0 : -1;
}
#endif /* LIBSSH_DSA */
#if LIBSSH2_ECDSA
/* _libssh2_ecdsa_get_curve_type
*
* returns key curve type that maps to libssh2_curve_type
*
*/
libssh2_curve_type
_libssh2_ecdsa_get_curve_type(libssh2_ecdsa_ctx *ec_ctx)
{
#ifdef USE_OPENSSL_3
int bits = 0;
EVP_PKEY_get_int_param(ec_ctx, OSSL_PKEY_PARAM_BITS, &bits);
if(bits == 256) {
return LIBSSH2_EC_CURVE_NISTP256;
}
else if(bits == 384) {
return LIBSSH2_EC_CURVE_NISTP384;
}
else if(bits == 521) {
return LIBSSH2_EC_CURVE_NISTP521;
}
return LIBSSH2_EC_CURVE_NISTP256;
#else
const EC_GROUP *group = EC_KEY_get0_group(ec_ctx);
return EC_GROUP_get_curve_name(group);
#endif
}
/* _libssh2_ecdsa_curve_type_from_name
*
* returns 0 for success, key curve type that maps to libssh2_curve_type
*
*/
int
_libssh2_ecdsa_curve_type_from_name(const char *name,
libssh2_curve_type *out_type)
{
libssh2_curve_type type;
if(!name || strlen(name) != 19)
return -1;
if(strcmp(name, "ecdsa-sha2-nistp256") == 0)
type = LIBSSH2_EC_CURVE_NISTP256;
else if(strcmp(name, "ecdsa-sha2-nistp384") == 0)
type = LIBSSH2_EC_CURVE_NISTP384;
else if(strcmp(name, "ecdsa-sha2-nistp521") == 0)
type = LIBSSH2_EC_CURVE_NISTP521;
else {
return -1;
}
if(out_type) {
*out_type = type;
}
return 0;
}
/* _libssh2_ecdsa_curve_name_with_octal_new
*
* Creates a new public key given an octal string, length and type
*
*/
int
_libssh2_ecdsa_curve_name_with_octal_new(libssh2_ecdsa_ctx ** ec_ctx,
const unsigned char *k,
size_t k_len, libssh2_curve_type curve)
{
int ret = 0;
#ifdef USE_OPENSSL_3
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
const char *n = EC_curve_nid2nist(curve);
char *group_name = NULL;
unsigned char *data = NULL;
if(!ctx)
return -1;
if(n) {
group_name = OPENSSL_zalloc(strlen(n) + 1);
}
if(k_len > 0) {
data = OPENSSL_malloc(k_len);
}
if(group_name && data) {
OSSL_PARAM params[3] = { 0 };
memcpy(group_name, n, strlen(n));
memcpy(data, k, k_len);
params[0] =
OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
group_name, 0);
params[1] =
OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
data, k_len);
params[2] = OSSL_PARAM_construct_end();
if(EVP_PKEY_fromdata_init(ctx) > 0)
ret = EVP_PKEY_fromdata(ctx, ec_ctx, EVP_PKEY_PUBLIC_KEY,
params);
else
ret = -1;
}
else
ret = -1;
if(group_name)
OPENSSL_clear_free(group_name, strlen(n));
if(data)
OPENSSL_clear_free(data, k_len);
EVP_PKEY_CTX_free(ctx);
#else
EC_KEY *ec_key = EC_KEY_new_by_curve_name(curve);
if(ec_key) {
const EC_GROUP *ec_group = NULL;
EC_POINT *point = NULL;
ec_group = EC_KEY_get0_group(ec_key);
point = EC_POINT_new(ec_group);
if(point) {
ret = EC_POINT_oct2point(ec_group, point, k, k_len, NULL);
if(ret == 1)
ret = EC_KEY_set_public_key(ec_key, point);
EC_POINT_free(point);
}
else
ret = -1;
if(ret == 1 && ec_ctx)
*ec_ctx = ec_key;
else {
EC_KEY_free(ec_key);
ret = -1;
}
}
else
ret = -1;
#endif
return (ret == 1) ? 0 : -1;
}
#ifdef USE_OPENSSL_3
#define LIBSSH2_ECDSA_VERIFY(digest_type) \
do { \
unsigned char hash[SHA##digest_type##_DIGEST_LENGTH]; \
if(libssh2_sha##digest_type(m, m_len, hash) == 0) { \
ret = EVP_PKEY_verify_init(ctx); \
if(ret > 0) { \
ret = EVP_PKEY_verify(ctx, der, der_len, hash, \
SHA##digest_type##_DIGEST_LENGTH); \
} \
} \
} while(0)
#else
#define LIBSSH2_ECDSA_VERIFY(digest_type) \
do { \
unsigned char hash[SHA##digest_type##_DIGEST_LENGTH]; \
if(libssh2_sha##digest_type(m, m_len, hash) == 0) { \
ret = ECDSA_do_verify(hash, \
SHA##digest_type##_DIGEST_LENGTH, \
ecdsa_sig, ec_key); \
} \
} while(0)
#endif
int
_libssh2_ecdsa_verify(libssh2_ecdsa_ctx * ecdsa_ctx,
const unsigned char *r, size_t r_len,
const unsigned char *s, size_t s_len,
const unsigned char *m, size_t m_len)
{
int ret = 0;
libssh2_curve_type type = _libssh2_ecdsa_get_curve_type(ecdsa_ctx);
#ifdef USE_OPENSSL_3
EVP_PKEY_CTX *ctx = NULL;
unsigned char *der = NULL;
int der_len = 0;
#else
EC_KEY *ec_key = (EC_KEY*)ecdsa_ctx;
#endif
#ifdef HAVE_OPAQUE_STRUCTS
ECDSA_SIG *ecdsa_sig = ECDSA_SIG_new();
BIGNUM *pr = BN_new();
BIGNUM *ps = BN_new();
BN_bin2bn(r, (int) r_len, pr);
BN_bin2bn(s, (int) s_len, ps);
ECDSA_SIG_set0(ecdsa_sig, pr, ps);
#else
ECDSA_SIG ecdsa_sig_;
ECDSA_SIG *ecdsa_sig = &ecdsa_sig_;
ecdsa_sig_.r = BN_new();
BN_bin2bn(r, (int) r_len, ecdsa_sig_.r);
ecdsa_sig_.s = BN_new();
BN_bin2bn(s, (int) s_len, ecdsa_sig_.s);
#endif
#ifdef USE_OPENSSL_3
ctx = EVP_PKEY_CTX_new(ecdsa_ctx, NULL);
if(!ctx) {
ret = -1;
goto cleanup;
}
der_len = i2d_ECDSA_SIG(ecdsa_sig, &der);
if(der_len <= 0) {
ret = -1;
goto cleanup;
}
#endif
if(type == LIBSSH2_EC_CURVE_NISTP256) {
LIBSSH2_ECDSA_VERIFY(256);
}
else if(type == LIBSSH2_EC_CURVE_NISTP384) {
LIBSSH2_ECDSA_VERIFY(384);
}
else if(type == LIBSSH2_EC_CURVE_NISTP521) {
LIBSSH2_ECDSA_VERIFY(512);
}
#ifdef USE_OPENSSL_3
cleanup:
if(ctx)
EVP_PKEY_CTX_free(ctx);
if(der)
OPENSSL_free(der);
#endif
#ifdef HAVE_OPAQUE_STRUCTS
if(ecdsa_sig)
ECDSA_SIG_free(ecdsa_sig);
#else
if(ecdsa_sig_.s)
BN_clear_free(ecdsa_sig_.s);
if(ecdsa_sig_.r)
BN_clear_free(ecdsa_sig_.r);
#endif
return (ret == 1) ? 0 : -1;
}
#endif /* LIBSSH2_ECDSA */
int
_libssh2_cipher_init(_libssh2_cipher_ctx * h,
_libssh2_cipher_type(algo),
unsigned char *iv, unsigned char *secret, int encrypt)
{
#ifdef HAVE_OPAQUE_STRUCTS
#if LIBSSH2_AES_GCM
const int is_aesgcm = (algo == EVP_aes_128_gcm) ||
(algo == EVP_aes_256_gcm);
#endif /* LIBSSH2_AES_GCM */
int rc;
*h = EVP_CIPHER_CTX_new();
rc = !EVP_CipherInit(*h, algo(), secret, iv, encrypt);