-
Notifications
You must be signed in to change notification settings - Fork 24
/
OneKey.java
1143 lines (977 loc) · 44.7 KB
/
OneKey.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package COSE;
import com.upokecenter.cbor.CBORObject;
import com.upokecenter.cbor.CBORType;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.ECPoint;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAMultiPrimePrivateCrtKeySpec;
import java.security.spec.RSAOtherPrimeInfo;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.X509EncodedKeySpec;
import net.i2p.crypto.eddsa.EdDSAPrivateKey;
import net.i2p.crypto.eddsa.EdDSAPublicKey;
import net.i2p.crypto.eddsa.spec.EdDSAGenParameterSpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
/**
*
* @author jimsch
*/
public class OneKey {
protected CBORObject keyMap;
private PrivateKey privateKey;
private PublicKey publicKey;
public OneKey() {
keyMap = CBORObject.NewMap();
}
public OneKey(CBORObject keyData) throws CoseException {
if (keyData.getType() != CBORType.Map) throw new CoseException("Key data is malformed");
keyMap = keyData;
CheckKeyState();
}
/**
* Create a OneKey object from Java Public/Private keys.
* @param pubKey - public key to use - may be null
* @param privKey - private key to use - may be null
* @throws CoseException Internal COSE Exception
*/
public OneKey(PublicKey pubKey, PrivateKey privKey) throws CoseException {
keyMap = CBORObject.NewMap();
if (pubKey != null) {
ArrayList<ASN1.TagValue> spki = ASN1.DecodeSubjectPublicKeyInfo(pubKey.getEncoded());
ArrayList<ASN1.TagValue> alg = spki.get(0).list;
if (Arrays.equals(alg.get(0).value, ASN1.oid_ecPublicKey)) {
byte[] oid = (byte[]) alg.get(1).value;
if (oid == null) throw new CoseException("Invalid SPKI structure");
// EC2 Key
keyMap.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2);
if (Arrays.equals(oid, ASN1.Oid_secp256r1)) keyMap.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256);
else if (Arrays.equals(oid, ASN1.Oid_secp384r1)) keyMap.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P384);
else if (Arrays.equals(oid, ASN1.Oid_secp521r1)) keyMap.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P521);
else throw new CoseException("Unsupported curve");
byte[] keyData = (byte[]) spki.get(1).value;
if (keyData[1] == 2 || keyData[1] == 3) {
keyMap.Add(KeyKeys.EC2_X.AsCBOR(), Arrays.copyOfRange(keyData, 2, keyData.length));
keyMap.Add(KeyKeys.EC2_Y.AsCBOR(), keyData[1] == 2 ? false : true);
}
else if (keyData[1] == 4) {
int keyLength = (keyData.length - 2)/2;
keyMap.Add(KeyKeys.EC2_X.AsCBOR(), Arrays.copyOfRange(keyData, 2, 2+keyLength));
keyMap.Add(KeyKeys.EC2_Y.AsCBOR(), Arrays.copyOfRange(keyData, 2+keyLength, keyData.length));
}
else throw new CoseException("Invalid key data");
}
else if (Arrays.equals(alg.get(0).value, ASN1.Oid_rsaEncryption)) {
ASN1.TagValue compound = ASN1.DecodeCompound(1, spki.get(1).value);
if(compound.list == null || compound.list.size() != 2) {
throw new CoseException("Invalid SPKI structure");
}
ASN1.TagValue n = compound.list.get(0);
ASN1.TagValue e = compound.list.get(1);
if(n.tag != 2 || e.tag != 2) {
throw new CoseException("Invalid SPKI structure");
}
keyMap.Add(KeyKeys.RSA_N.AsCBOR(), n.value);
keyMap.Add(KeyKeys.RSA_E.AsCBOR(), e.value);
} else if (ASN1.isEdXOid(alg.get(0).value)) {
byte[] oid = (byte[]) alg.get(0).value;
if (oid == null)
throw new CoseException("Invalid SPKI structure");
// OKP Key
keyMap.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_OKP);
keyMap.Add(KeyKeys.Algorithm.AsCBOR(), AlgorithmID.EDDSA.AsCBOR());
if (Arrays.equals(oid, ASN1.Oid_X25519))
keyMap.Add(KeyKeys.OKP_Curve.AsCBOR(), KeyKeys.OKP_X25519);
else if (Arrays.equals(oid, ASN1.Oid_X448))
keyMap.Add(KeyKeys.OKP_Curve.AsCBOR(), KeyKeys.OKP_X448);
else if (Arrays.equals(oid, ASN1.Oid_Ed25519))
keyMap.Add(KeyKeys.OKP_Curve.AsCBOR(), KeyKeys.OKP_Ed25519);
else if (Arrays.equals(oid, ASN1.Oid_Ed448))
keyMap.Add(KeyKeys.OKP_Curve.AsCBOR(), KeyKeys.OKP_Ed448);
else
throw new CoseException("Unsupported curve");
byte[] keyData = (byte[]) spki.get(1).value;
if (keyData[0] == 0) {
keyMap.Add(KeyKeys.OKP_X.AsCBOR(), Arrays.copyOfRange(keyData, 1, keyData.length));
} else
throw new CoseException("Invalid key data");
}
else {
throw new CoseException("Unsupported Algorithm");
}
this.publicKey = pubKey;
}
if (privKey != null) {
ArrayList<ASN1.TagValue> pkl = ASN1.DecodePKCS8Structure(privKey.getEncoded());
if (pkl.get(0).tag != 2) throw new CoseException("Invalid PKCS8 structure");
ArrayList<ASN1.TagValue> alg = pkl.get(1).list;
if (Arrays.equals(alg.get(0).value, ASN1.oid_ecPublicKey)) {
byte[] oid = (byte[]) alg.get(1).value;
if (oid == null) throw new CoseException("Invalid PKCS8 structure");
// EC2 Key
if (!keyMap.ContainsKey(KeyKeys.KeyType.AsCBOR())) {
keyMap.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2);
if (Arrays.equals(oid, ASN1.Oid_secp256r1)) keyMap.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256);
else if (Arrays.equals(oid, ASN1.Oid_secp384r1)) keyMap.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P384);
else if (Arrays.equals(oid, ASN1.Oid_secp521r1)) keyMap.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P521);
else throw new CoseException("Unsupported curve");
}
else {
if (!this.get(KeyKeys.KeyType).equals(KeyKeys.KeyType_EC2)) {
throw new CoseException("Public/Private key don't match");
}
}
ArrayList<ASN1.TagValue> pkdl = ASN1.DecodePKCS8EC(pkl);
if (pkdl.get(1).tag != 4) throw new CoseException("Invalid PKCS8 structure");
byte[] keyData = pkdl.get(1).value;
keyMap.Add(KeyKeys.EC2_D.AsCBOR(), keyData);
}
else if (Arrays.equals(alg.get(0).value, ASN1.Oid_rsaEncryption)) {
ArrayList<ASN1.TagValue> pkdl = ASN1.DecodePKCS8RSA(pkl);
if(!keyMap.ContainsKey(KeyKeys.RSA_N.AsCBOR())) {
keyMap.Add(KeyKeys.RSA_N.AsCBOR(), pkdl.get(1).value);
}
if(!keyMap.ContainsKey(KeyKeys.RSA_E.AsCBOR())){
keyMap.Add(KeyKeys.RSA_E.AsCBOR(), pkdl.get(2).value);
}
keyMap.Add(KeyKeys.RSA_D.AsCBOR(), pkdl.get(3).value);
keyMap.Add(KeyKeys.RSA_P.AsCBOR(), pkdl.get(4).value);
keyMap.Add(KeyKeys.RSA_Q.AsCBOR(), pkdl.get(5).value);
keyMap.Add(KeyKeys.RSA_DP.AsCBOR(), pkdl.get(6).value);
keyMap.Add(KeyKeys.RSA_DQ.AsCBOR(), pkdl.get(7).value);
keyMap.Add(KeyKeys.RSA_QI.AsCBOR(), pkdl.get(8).value);
// todo multi prime keys
} else if (ASN1.isEdXOid(alg.get(0).value)) {
byte[] oid = (byte[]) alg.get(0).value;
if (oid == null)
throw new CoseException("Invalid PKCS8 structure");
// OKP Key
if (!keyMap.ContainsKey(KeyKeys.KeyType.AsCBOR())) {
keyMap.Add(KeyKeys.Algorithm.AsCBOR(), AlgorithmID.EDDSA.AsCBOR());
if (Arrays.equals(oid, ASN1.Oid_X25519))
keyMap.Add(KeyKeys.OKP_Curve.AsCBOR(), KeyKeys.OKP_X25519);
else if (Arrays.equals(oid, ASN1.Oid_X448))
keyMap.Add(KeyKeys.OKP_Curve.AsCBOR(), KeyKeys.OKP_X448);
else if (Arrays.equals(oid, ASN1.Oid_Ed25519))
keyMap.Add(KeyKeys.OKP_Curve.AsCBOR(), KeyKeys.OKP_Ed25519);
else if (Arrays.equals(oid, ASN1.Oid_Ed448))
keyMap.Add(KeyKeys.OKP_Curve.AsCBOR(), KeyKeys.OKP_Ed448);
else
throw new CoseException("Unsupported curve");
} else {
if (!this.get(KeyKeys.KeyType).equals(KeyKeys.KeyType_OKP)) {
throw new CoseException("Public/Private key don't match");
}
}
ArrayList<ASN1.TagValue> pkdl = ASN1.DecodePKCS8EC(pkl);
if (pkdl.get(0).tag != 4)
throw new CoseException("Invalid PKCS8 structure");
byte[] keyData = (byte[]) (pkdl.get(0).value);
keyMap.Add(KeyKeys.OKP_D.AsCBOR(), keyData);
}
else {
throw new CoseException("Unsupported Algorithm");
}
this.privateKey = privKey;
}
}
public void add(KeyKeys keyValue, CBORObject value) {
keyMap.Add(keyValue.AsCBOR(), value);
}
public void add(CBORObject keyValue, CBORObject value) {
keyMap.Add(keyValue, value);
}
public CBORObject get(KeyKeys keyValue) {
return keyMap.get(keyValue.AsCBOR());
}
public CBORObject get(CBORObject keyValue) throws CoseException {
if ((keyValue.getType() != CBORType.Integer) && (keyValue.getType() != CBORType.TextString)) throw new CoseException("keyValue type is incorrect");
return keyMap.get(keyValue);
}
/**
* Compares the key's assigned algorithm with the provided value, indicating if the values are the
* same.
*
* @param algorithmId
* the algorithm to compare or {@code null} to check for no assignment.
* @return {@code true} if the current key has the provided algorithm assigned, or {@code false}
* otherwise
*/
public boolean HasAlgorithmID(AlgorithmID algorithmId) {
CBORObject thisObj = get(KeyKeys.Algorithm);
CBORObject thatObj = (algorithmId == null ? null : algorithmId.AsCBOR());
boolean result;
if (thatObj == null) {
result = (thisObj == null);
} else {
result = thatObj.equals(thisObj);
}
return result;
}
/**
* Compares the key's assigned identifier with the provided value, indicating if the values are
* the same.
*
* @param id
* the identifier to compare or {@code null} to check for no assignment.
* @return {@code true} if the current key has the provided identifier assigned, or {@code false}
* otherwise
*/
@Deprecated
public boolean HasKeyID(String id) {
byte[] idB = StandardCharsets.UTF_8.encode(id).array();
return HasKeyID(idB);
}
public boolean HasKeyID(byte[] id)
{
CBORObject thatObj = (id == null) ? null : CBORObject.FromObject(id);
CBORObject thisObj = get(KeyKeys.KeyId);
boolean result;
if (thatObj == null) {
result = (thisObj == null);
} else {
result = thatObj.equals(thisObj);
}
return result;
}
/**
* Compares the key's assigned key type with the provided value, indicating if the values are the
* same.
*
* @param keyTypeObj
* the key type to compare or {@code null} to check for no assignment.
* @return {@code true} if the current key has the provided identifier assigned, or {@code false}
* otherwise
*/
public boolean HasKeyType(CBORObject keyTypeObj) {
CBORObject thatObj = keyTypeObj;
CBORObject thisObj = get(KeyKeys.KeyType);
boolean result;
if (thatObj == null) {
result = (thisObj == null);
} else {
result = thatObj.equals(thisObj);
}
return result;
}
/**
* Compares the key's assigned key operations with the provided value, indicating if the provided
* value was found in the key operation values assigned to the key.
*
* @param that
* the integer operation value to attempt to find in the values provided by the key or
* {@code null} to check for no assignment.
* @return {@code true} if the current key has the provided value assigned, or {@code false}
* otherwise
*/
public boolean HasKeyOp(Integer that) {
CBORObject thisObj = get(KeyKeys.Key_Ops);
boolean result;
if (that == null) {
result = (thisObj == null);
} else {
result = false;
if (thisObj.getType() == CBORType.Integer) {
if (thisObj.AsInt32() == that) {
result = true;
}
} else if (thisObj.getType() == CBORType.Array) {
for (int i = 0; i < thisObj.size(); i++) {
if ((thisObj.get(i).getType() == CBORType.Integer) && (thisObj.get(i).AsInt32() == that)) {
result = true;
break;
}
}
}
}
return result;
}
private void CheckKeyState() throws CoseException {
CBORObject val;
// Must have a key type
val = OneKey.this.get(KeyKeys.KeyType);
if ((val == null) || (val.getType() != CBORType.Integer)) throw new CoseException("Missing or incorrect key type field");
if (val.equals(KeyKeys.KeyType_Octet)) {
val = OneKey.this.get(KeyKeys.Octet_K);
if ((val== null) || (val.getType() != CBORType.ByteString)) throw new CoseException("Malformed key structure");
}
else if (val.equals(KeyKeys.KeyType_EC2)) {
CheckECKey();
}
else if (val.equals(KeyKeys.KeyType_OKP)) {
CheckOkpKey();
}
else if (val.equals(KeyKeys.KeyType_RSA)) {
CheckRsaKey();
}
else throw new CoseException("Unsupported key type");
}
private void CheckECKey() throws CoseException {
// ECParameterSpec params = null; // new ECDomainParameters(curve.getCurve(), curve.getG(), curve.getN(), curve.getH());
boolean needPublic = false;
// ECPrivateKeySpec privKeySpec = null;
CBORObject val;
byte[] oid;
CBORObject cn = this.get(KeyKeys.EC2_Curve);
if (cn == KeyKeys.EC2_P256) {
oid = ASN1.Oid_secp256r1;
}
else if (cn == KeyKeys.EC2_P384) {
oid = ASN1.Oid_secp384r1;
}
else if (cn == KeyKeys.EC2_P521) {
oid = ASN1.Oid_secp521r1;
}
else {
throw new CoseException("Key has an unknown curve");
}
try {
val = this.get(KeyKeys.EC2_D);
if (val != null) {
if (val.getType() != CBORType.ByteString) throw new CoseException("Malformed key structure");
try {
byte[] privateBytes = ASN1.EncodeEcPrivateKey(oid, val.GetByteString(), null);
byte[] pkcs8 = ASN1.EncodePKCS8(ASN1.AlgorithmIdentifier(ASN1.oid_ecPublicKey, oid), privateBytes, null);
KeyFactory fact = KeyFactory.getInstance("EC");
KeySpec keyspec = new PKCS8EncodedKeySpec(pkcs8);
privateKey = fact.generatePrivate(keyspec);
}
catch (NoSuchAlgorithmException e) {
throw new CoseException("Unsupported Algorithm", e);
}
catch (InvalidKeySpecException e) {
throw new CoseException("Invalid Private Key", e);
}
}
val = this.get(KeyKeys.EC2_X);
if (val == null) {
if (privateKey == null) throw new CoseException("Malformed key structure");
else needPublic = true;
}
else if (val.getType() != CBORType.ByteString) throw new CoseException("Malformed key structure");
val = this.get(KeyKeys.EC2_Y);
if (val == null) {
if (privateKey == null) throw new CoseException("Malformed key structure");
else needPublic = true;
}
else if ((val.getType() != CBORType.ByteString) && (val.getType() != CBORType.Boolean)) throw new CoseException("Malformed key structure");
if (privateKey != null && needPublic) {
byte[] pkcs8 = privateKey.getEncoded();
return;
// todo: calculate (and populate) public from private
}
byte[] spki = null;
if (spki == null) {
byte[] rgbKey = null;
byte[] X = this.get(KeyKeys.EC2_X).GetByteString();
if (this.get(KeyKeys.EC2_Y).getType()== CBORType.Boolean) {
rgbKey = new byte[X.length + 1];
System.arraycopy(X, 0, rgbKey, 1, X.length);
rgbKey[0] = (byte) (2 + (this.get(KeyKeys.EC2_Y).AsBoolean() ? 1 : 0));
}
else {
rgbKey = new byte[X.length*2+1];
System.arraycopy(X, 0,rgbKey, 1, X.length);
byte[] Y = this.get(KeyKeys.EC2_Y).GetByteString();
System.arraycopy(Y, 0, rgbKey, 1+X.length, X.length);
rgbKey[0] = 4;
}
spki = ASN1.EncodeSubjectPublicKeyInfo(ASN1.AlgorithmIdentifier(ASN1.oid_ecPublicKey, oid), rgbKey);
}
KeyFactory fact = KeyFactory.getInstance("EC"/*, "BC"*/);
KeySpec keyspec = new X509EncodedKeySpec(spki);
publicKey = fact.generatePublic(keyspec);
}
catch (NoSuchAlgorithmException e) {
throw new CoseException("Alorithm unsupported", e);
}
catch (InvalidKeySpecException e) {
throw new CoseException("Internal error on SPKI", e);
}
/*
catch (NoSuchProviderException e) {
throw new CoseException("BC not found");
}
*/
/*
X9ECParameters curve = GetCurve();
ECDomainParameters params = new ECDomainParameters(curve.getCurve(), curve.getG(), curve.getN(), curve.getH());
boolean needPublic = false;
ECPrivateKeyParameters privKey = null;
ECPublicKeyParameters pubKey = null;
CBORObject val;
val = OneKey.this.get(KeyKeys.EC2_D);
if (val != null) {
if (val.getType() != CBORType.ByteString) throw new CoseException("Malformed key structure");
privKey = new ECPrivateKeyParameters(new BigInteger(1, val.GetByteString()),
params);
}
val = OneKey.this.get(KeyKeys.EC2_X);
if (val == null) {
if (privKey == null) throw new CoseException("Malformed key structure");
else needPublic = true;
}
else if (val.getType() != CBORType.ByteString) throw new CoseException("Malformed key structure");
val = OneKey.this.get(KeyKeys.EC2_Y);
if (val == null) {
if (privKey == null) throw new CoseException("Malformed key structure");
else needPublic = true;
}
else if ((val.getType() != CBORType.ByteString) && (val.getType() != CBORType.Boolean)) throw new CoseException("Malformed key structure");
if (privKey != null && needPublic) {
// todo: calculate (and populate) public from private
pubKey = new ECPublicKeyParameters(params.getG().multiply(privKey.getD()), params);
byte[] rgbX = pubKey.getQ().normalize().getXCoord().getEncoded();
byte[] rgbY = pubKey.getQ().normalize().getYCoord().getEncoded();
add(KeyKeys.EC2_X, CBORObject.FromObject(rgbX));
add(KeyKeys.EC2_Y, CBORObject.FromObject(rgbY));
} else {
// todo: validate public on curve
}
*/
}
public ECGenParameterSpec GetCurve2() throws CoseException {
if (OneKey.this.get(KeyKeys.KeyType) != KeyKeys.KeyType_EC2) throw new CoseException("Not an EC2 key");
CBORObject cnCurve = OneKey.this.get(KeyKeys.EC2_Curve);
if (cnCurve == KeyKeys.EC2_P256) return new ECGenParameterSpec("secp256r1");
if (cnCurve == KeyKeys.EC2_P384) return new ECGenParameterSpec("secp384r1");
if (cnCurve == KeyKeys.EC2_P521) return new ECGenParameterSpec("secp521r1");
throw new CoseException("Unsupported curve " + cnCurve);
}
static public OneKey generateKey(AlgorithmID algorithm) throws CoseException {
return generateKey(algorithm, null);
}
/**
* Generate a random key pair based on the given algorithm.
* Some algorithm can take a parameter. For example, the RSA_PSS family of algorithm
* can take the RSA key size as a parameter.
*
* @param algorithm the algorithm to generate a key pair for
* @param parameters optional parameters to the key pair generator
* @return the generated Key Pair
* @throws CoseException
*/
static public OneKey generateKey(AlgorithmID algorithm, String parameters) throws CoseException {
OneKey returnThis;
switch(algorithm) {
case ECDSA_256:
returnThis = generateECDSAKey("P-256", KeyKeys.EC2_P256);
break;
case ECDSA_384:
returnThis = generateECDSAKey("P-384", KeyKeys.EC2_P384);
break;
case ECDSA_512:
returnThis = generateECDSAKey("P-521", KeyKeys.EC2_P521);
break;
case EDDSA:
returnThis = generateOkpKey("Ed25519", KeyKeys.OKP_Ed25519);
break;
case RSA_PSS_256:
case RSA_PSS_384:
case RSA_PSS_512:
int keySize = 2048;
if(parameters != null) {
try {
keySize = Integer.parseInt(parameters);
} catch (NumberFormatException ignored) {}
}
returnThis = generateRSAKey(keySize);
break;
default:
throw new CoseException("Unknown algorithm");
}
returnThis.add(KeyKeys.Algorithm, algorithm.AsCBOR());
return returnThis;
}
static public OneKey generateKey(CBORObject curve) throws CoseException {
String curveName;
OneKey returnThis;
switch (curve.AsInt32()) {
case 1:
curveName = "P-256";
returnThis = generateECDHKey(curveName, curve);
return returnThis;
case 2:
curveName = "P-384";
returnThis = generateECDHKey(curveName, curve);
return returnThis;
case 3:
curveName = "P-521";
returnThis = generateECDHKey(curveName, curve);
return returnThis;
case 6:
curveName = "Ed25519";
return generateOkpKey(curveName, curve);
case 7:
curveName = "Ed448";
return generateOkpKey(curveName, curve);
case 4:
curveName = "X25519";
return generateOkpKey(curveName, curve);
case 5:
curveName = "X448";
return generateOkpKey(curveName, curve);
default:
throw new CoseException("Unknown curve");
}
}
static private OneKey generateECDHKey(String curveName, CBORObject curve) throws CoseException {
try {
int curveSize;
switch (curveName) {
case "P-256":
curveName = "secp256r1";
curveSize = 256;
break;
case "P-384":
curveName="secp384r1";
curveSize = 384;
break;
case "P-521":
curveName = "secp521r1";
curveSize = 521;
break;
default:
throw new CoseException("Internal Error");
}
ECGenParameterSpec paramSpec = new ECGenParameterSpec(curveName);
KeyPairGenerator gen = KeyPairGenerator.getInstance("EC");
gen.initialize(paramSpec);
KeyPair keyPair = gen.genKeyPair();
ECPoint pubPoint = ((ECPublicKey) keyPair.getPublic()).getW();
byte[] rgbX = ArrayFromBigNum(pubPoint.getAffineX(), curveSize);
byte[] rgbY = ArrayFromBigNum(pubPoint.getAffineY(), curveSize);
byte[] rgbD = ArrayFromBigNum(((ECPrivateKey) keyPair.getPrivate()).getS(), curveSize);
OneKey key = new OneKey();
key.add(KeyKeys.KeyType, KeyKeys.KeyType_EC2);
key.add(KeyKeys.EC2_Curve, curve);
key.add(KeyKeys.EC2_X, CBORObject.FromObject(rgbX));
key.add(KeyKeys.EC2_Y, CBORObject.FromObject(rgbY));
key.add(KeyKeys.EC2_D, CBORObject.FromObject(rgbD));
key.publicKey = keyPair.getPublic();
key.privateKey = keyPair.getPrivate();
return key;
}
catch (NoSuchAlgorithmException e) {
throw new CoseException("No provider for algorithm", e);
}
catch (InvalidAlgorithmParameterException e) {
throw new CoseException("THe curve is not supported", e);
}
}
static private byte[] ArrayFromBigNum(BigInteger n, int curveSize) {
byte[] rgb = new byte[(curveSize+7)/8];
byte[] rgb2 = n.toByteArray();
if (rgb.length == rgb2.length) return rgb2;
if (rgb2.length > rgb.length) {
System.arraycopy(rgb2, rgb2.length-rgb.length, rgb, 0, rgb.length);
}
else {
System.arraycopy(rgb2, 0, rgb, rgb.length-rgb2.length, rgb2.length);
}
return rgb;
}
static private OneKey generateECDSAKey(String curveName, CBORObject curve) throws CoseException {
try {
int curveSize;
switch (curveName) {
case "P-256":
curveName = "secp256r1";
curveSize = 256;
break;
case "P-384":
curveName="secp384r1";
curveSize = 384;
break;
case "P-521":
curveName = "secp521r1";
curveSize = 521;
break;
default:
throw new CoseException("Internal Error");
}
ECGenParameterSpec paramSpec = new ECGenParameterSpec(curveName);
KeyPairGenerator gen = KeyPairGenerator.getInstance("EC");
gen.initialize(paramSpec);
KeyPair keyPair = gen.genKeyPair();
ECPoint pubPoint = ((ECPublicKey) keyPair.getPublic()).getW();
byte[] rgbX = ArrayFromBigNum(pubPoint.getAffineX(), curveSize);
byte[] rgbY = ArrayFromBigNum(pubPoint.getAffineY(), curveSize);
byte[] rgbD = ArrayFromBigNum(((ECPrivateKey) keyPair.getPrivate()).getS(), curveSize);
OneKey key = new OneKey();
key.add(KeyKeys.KeyType, KeyKeys.KeyType_EC2);
key.add(KeyKeys.EC2_Curve, curve);
key.add(KeyKeys.EC2_X, CBORObject.FromObject(rgbX));
key.add(KeyKeys.EC2_Y, CBORObject.FromObject(rgbY));
key.add(KeyKeys.EC2_D, CBORObject.FromObject(rgbD));
key.publicKey = keyPair.getPublic();
key.privateKey = keyPair.getPrivate();
return key;
}
catch (NoSuchAlgorithmException e) {
throw new CoseException("No provider for algorithm", e);
}
catch (InvalidAlgorithmParameterException e) {
throw new CoseException("The curve is not supported", e);
}
}
/**
* Create a OneKey object with only the public fields. Filters out the
* private key fields but leaves all positive number labels and text labels
* along with negative number labels that are public fields.
*
* @return public version of the key
*/
public OneKey PublicKey()
{
OneKey newKey = new OneKey();
CBORObject val = this.get(KeyKeys.KeyType);
if (val.equals(KeyKeys.KeyType_Octet)) {
return null;
}
else if (val.equals(KeyKeys.KeyType_EC2)) {
newKey.add(KeyKeys.EC2_Curve, get(KeyKeys.EC2_Curve));
newKey.add(KeyKeys.EC2_X, get(KeyKeys.EC2_X));
newKey.add(KeyKeys.EC2_Y, get(KeyKeys.EC2_Y));
}
else if (val.equals(KeyKeys.KeyType_OKP)) {
newKey.add(KeyKeys.OKP_Curve, get(KeyKeys.OKP_Curve));
newKey.add(KeyKeys.OKP_X, get(KeyKeys.OKP_X));
}
else if (val.equals(KeyKeys.KeyType_RSA)) {
newKey.add(KeyKeys.RSA_N, get(KeyKeys.RSA_N));
newKey.add(KeyKeys.RSA_E, get(KeyKeys.RSA_E));
}
else {
return null;
}
// Allow them to use the same underlying public key object
newKey.publicKey = publicKey;
for (CBORObject obj : keyMap.getKeys()) {
val = keyMap.get(obj);
if (obj.getType() == CBORType.Integer) {
if (obj.AsInt32() > 0) {
newKey.add(obj, val);
}
}
else if (obj.getType() == CBORType.TextString) {
newKey.add(obj, val);
}
}
return newKey;
}
/**
* Encode to a byte string
*
* @return encoded object as bytes.
*/
public byte[] EncodeToBytes()
{
return keyMap.EncodeToBytes();
}
/**
* Return the key as a CBOR object
*
* @return The key
*/
public CBORObject AsCBOR()
{
return keyMap;
}
/**
* Return a java.security.PublicKey that is the same as the OneKey key
*
* @return the key
* @throws CoseException If there is a conversion error
*/
public PublicKey AsPublicKey() throws CoseException
{
return publicKey;
}
/**
* Return a java.security.PrivateKey that is the same as the OneKey key
*
* @return the key
* @throws CoseException if there is a conversion error
*/
public PrivateKey AsPrivateKey() throws CoseException
{
return privateKey;
}
private Object UserData;
/**
* Return the user data field.
*
* The user data object allows for an application to associate a piece of arbitrary
* data with a key and retrieve it later.
* @return the user data object
*/
public Object getUserData() {
return UserData;
}
/**
* Set the user data field.
*
* The user data field allows for an application to associate a piece of arbitrary
* data with a key and retrieve it later.
* @param newData Data field to be saved.
*/
public void setUserData(Object newData) {
UserData = newData;
}
private void CheckOkpKey() throws CoseException {
boolean needPublic = false;
CBORObject val;
String algName;
byte[] oid;
CBORObject cn = this.get(KeyKeys.OKP_Curve);
if (cn == KeyKeys.OKP_Ed25519) {
oid = ASN1.Oid_Ed25519;
algName = "EdDSA";
}
else if (cn == KeyKeys.OKP_Ed448) {
oid = ASN1.Oid_Ed448;
algName = "EdDSA";
}
else if (cn == KeyKeys.OKP_X25519) {
oid = ASN1.Oid_X25519;
algName = "EdDH";
}
else if (cn == KeyKeys.OKP_X448) {
oid = ASN1.Oid_X448;
algName = "ECDH";
}
else {
throw new CoseException("Key has an unknown curve");
}
try {
val = this.get(KeyKeys.OKP_D);
if (val != null) {
if (val.getType() != CBORType.ByteString) throw new CoseException("Malformed key structure");
try {
byte[] privateKeyBytes = ASN1.EncodeOctetString(val.GetByteString());
byte[] pkcs8 = ASN1.EncodePKCS8(ASN1.AlgorithmIdentifier(oid, null), privateKeyBytes, null);
KeyFactory fact = KeyFactory.getInstance(algName, "EdDSA");
KeySpec keyspec = new PKCS8EncodedKeySpec(pkcs8);
privateKey = fact.generatePrivate(keyspec);
}
catch (NoSuchAlgorithmException e) {
throw new CoseException("Unsupported Algorithm", e);
}
catch (InvalidKeySpecException e) {
throw new CoseException("Invalid Private Key", e);
}
}
val = this.get(KeyKeys.OKP_X);
if (val == null) {
if (privateKey == null) throw new CoseException("Malformed key structure");
else needPublic = true;
}
else if (val.getType() != CBORType.ByteString) throw new CoseException("Malformed key structure");
if (privateKey != null && needPublic) {
byte[] pkcs8 = privateKey.getEncoded();
return;
// todo: calculate (and populate) public from private
}
byte[] spki = null;
if (spki == null) {
byte[] rgbKey = this.get(KeyKeys.OKP_X).GetByteString();
spki = ASN1.EncodeSubjectPublicKeyInfo(ASN1.AlgorithmIdentifier(oid, null), rgbKey);
}
KeyFactory fact = KeyFactory.getInstance("EdDSA", "EdDSA");
KeySpec keyspec = new X509EncodedKeySpec(spki);
publicKey = fact.generatePublic(keyspec);
}
catch (NoSuchAlgorithmException | NoSuchProviderException e) {
throw new CoseException("Algorithm unsupported", e);
}
catch (InvalidKeySpecException e) {
throw new CoseException("Internal error on SPKI", e);
}
}
static private OneKey generateOkpKey(String curveName, CBORObject curve) throws CoseException {
try {
switch (curveName) {
case "Ed25519":
break;
case "Ed448":
case "X22519":
case "X448":
throw new CoseException("Algorithm not supported.");
default:
throw new CoseException("Internal Error");
}
EdDSAGenParameterSpec paramSpec = new EdDSAGenParameterSpec(curveName);
KeyPairGenerator gen = KeyPairGenerator.getInstance("EdDSA", "EdDSA");
gen.initialize(paramSpec);
KeyPair keyPair = gen.genKeyPair();
byte[] rgbX = ((EdDSAPublicKey) keyPair.getPublic()).getAbyte();
byte[] rgbD = ((EdDSAPrivateKey) keyPair.getPrivate()).getSeed();
OneKey key = new OneKey();
key.add(KeyKeys.KeyType, KeyKeys.KeyType_OKP);
key.add(KeyKeys.OKP_Curve, curve);
key.add(KeyKeys.OKP_X, CBORObject.FromObject(rgbX));
key.add(KeyKeys.OKP_D, CBORObject.FromObject(rgbD));
key.publicKey = keyPair.getPublic();
key.privateKey = keyPair.getPrivate();
return key;
}
catch (NoSuchAlgorithmException | NoSuchProviderException e) {
throw new CoseException("No provider for algorithm", e);
}
catch (InvalidAlgorithmParameterException e) {
throw new CoseException("The curve is not supported", e);
}
}
private void CheckRsaKey() throws CoseException {
CBORObject n = this.get(KeyKeys.RSA_N); // modulus, positive int
CBORObject e = this.get(KeyKeys.RSA_E); // public exponent, positive int
CBORObject d = this.get(KeyKeys.RSA_D); // private exponent, positive int
CBORObject p = this.get(KeyKeys.RSA_P); // the prime factor p of n
CBORObject q = this.get(KeyKeys.RSA_Q); // the prime factor q of n
CBORObject dP = this.get(KeyKeys.RSA_DP); // d mod (p - 1)
CBORObject dQ = this.get(KeyKeys.RSA_DQ); // d mod (q - 1)
CBORObject qInv = this.get(KeyKeys.RSA_QI); // CRT coefficient