author | shade |
Fri, 22 Feb 2019 17:54:13 +0100 | |
changeset 53899 | bae1944fc279 |
parent 52575 | 1092ba73cb2d |
permissions | -rw-r--r-- |
50053 | 1 |
/* |
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.security.ec; |
|
27 |
||
52575
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
28 |
import java.io.*; |
50053 | 29 |
import java.security.interfaces.XECPrivateKey; |
30 |
import java.util.Optional; |
|
52575
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
31 |
import java.security.*; |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
32 |
import java.security.spec.*; |
50053 | 33 |
|
34 |
import sun.security.pkcs.PKCS8Key; |
|
35 |
import sun.security.x509.AlgorithmId; |
|
52575
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
36 |
import sun.security.util.*; |
50053 | 37 |
|
38 |
public final class XDHPrivateKeyImpl extends PKCS8Key implements XECPrivateKey { |
|
39 |
||
40 |
private static final long serialVersionUID = 1L; |
|
41 |
||
52575
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
42 |
private final AlgorithmParameterSpec paramSpec; |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
43 |
private byte[] k; |
50053 | 44 |
|
45 |
XDHPrivateKeyImpl(XECParameters params, byte[] k) |
|
46 |
throws InvalidKeyException { |
|
47 |
||
48 |
this.paramSpec = new NamedParameterSpec(params.getName()); |
|
52575
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
49 |
this.k = k.clone(); |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
50 |
|
50053 | 51 |
this.algid = new AlgorithmId(params.getOid()); |
52575
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
52 |
encodeKey(); |
50053 | 53 |
|
54 |
checkLength(params); |
|
55 |
} |
|
56 |
||
57 |
XDHPrivateKeyImpl(byte[] encoded) throws InvalidKeyException { |
|
58 |
||
59 |
decode(encoded); |
|
60 |
XECParameters params = XECParameters.get( |
|
61 |
InvalidKeyException::new, algid); |
|
62 |
paramSpec = new NamedParameterSpec(params.getName()); |
|
52575
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
63 |
decodeKey(); |
50053 | 64 |
|
65 |
checkLength(params); |
|
66 |
} |
|
67 |
||
52575
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
68 |
private void decodeKey() throws InvalidKeyException { |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
69 |
try { |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
70 |
DerInputStream derStream = new DerInputStream(key); |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
71 |
k = derStream.getOctetString(); |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
72 |
} catch (IOException ex) { |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
73 |
throw new InvalidKeyException(ex); |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
74 |
} |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
75 |
} |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
76 |
|
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
77 |
private void encodeKey() { |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
78 |
DerOutputStream derKey = new DerOutputStream(); |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
79 |
try { |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
80 |
derKey.putOctetString(k); |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
81 |
this.key = derKey.toByteArray(); |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
82 |
} catch (IOException ex) { |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
83 |
throw new ProviderException(ex); |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
84 |
} |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
85 |
} |
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
86 |
|
50053 | 87 |
void checkLength(XECParameters params) throws InvalidKeyException { |
88 |
||
52575
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
89 |
if (params.getBytes() != this.k.length) { |
50053 | 90 |
throw new InvalidKeyException( |
91 |
"key length must be " + params.getBytes()); |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
public byte[] getK() { |
|
52575
1092ba73cb2d
8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents:
50053
diff
changeset
|
96 |
return k.clone(); |
50053 | 97 |
} |
98 |
||
99 |
@Override |
|
100 |
public String getAlgorithm() { |
|
101 |
return "XDH"; |
|
102 |
} |
|
103 |
||
104 |
@Override |
|
105 |
public AlgorithmParameterSpec getParams() { |
|
106 |
return paramSpec; |
|
107 |
} |
|
108 |
||
109 |
@Override |
|
110 |
public Optional<byte[]> getScalar() { |
|
111 |
return Optional.of(getK()); |
|
112 |
} |
|
113 |
} |
|
114 |