-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathWolfCryptEccKey.java
More file actions
125 lines (103 loc) · 3.69 KB
/
WolfCryptEccKey.java
File metadata and controls
125 lines (103 loc) · 3.69 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
/* WolfCryptEccKey.java
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
package com.wolfssl;
/**
* Wraps a native ecc_key structure pointer.
*
* This class was previously named 'EccKey' and was renamed to 'WolfCryptEccKey'
* to avoid naming conflicts with the wolfCrypt JNI/JCE package name space.
* Apart from the name change, the class behavior and use remains the same.
*
* @author wolfSSL
*/
public class WolfCryptEccKey {
/* internal ecc_key structure pointer */
private long eccKeyPtr;
/* is this key initialized, or has it been freed? */
private boolean active = false;
/**
* Create new WolfCryptEccKey object, wrapping native ecc_key with pointer
* keyPtr.
*
* @param keyPtr pointer to native ecc_key structure
* @throws com.wolfssl.WolfSSLException if key object creation failed
*/
public WolfCryptEccKey(long keyPtr) throws WolfSSLException {
if (keyPtr == 0) {
throw new WolfSSLException("NULL ecc_key pointer not allowed");
} else {
this.active = true;
this.eccKeyPtr = keyPtr;
}
}
/* ------------------- private/protected methods -------------------- */
long getEccKeyPtr() {
return eccKeyPtr;
}
/* ------------------ native method declarations -------------------- */
private native byte[] EccPublicKeyToDer(long eccKey);
private native byte[] EccPrivateKeyToDer(long eccKey);
private native byte[] EccPrivateKeyToPKCS8(long eccKey);
/* ------------------- session-specific methods --------------------- */
/**
* Return ECC public key in DER format
*
* @return the raw ECC public key as a byte array.
*/
public byte[] getPublicKeyDer() throws IllegalStateException {
if (this.active == false)
throw new IllegalStateException(
"Native ecc_key struct not initialized");
return EccPublicKeyToDer(getEccKeyPtr());
}
/**
* Return ECC private key in DER format
*
* @return the raw ECC private key as a byte array, not PKCS#8 formatted.
*/
public byte[] getPrivateKeyDer() {
if (this.active == false)
throw new IllegalStateException(
"Native ecc_key struct not initialized");
return EccPrivateKeyToDer(getEccKeyPtr());
}
/**
* Return ECC private key DER in PKCS#8 format
*
* @return ECC private key DER in PKCS#8 format.
*/
public byte[] getPrivateKeyPKCS8() {
if (this.active == false)
throw new IllegalStateException(
"Native ecc_key struct not initialized");
return EccPrivateKeyToPKCS8(getEccKeyPtr());
}
@SuppressWarnings({"deprecation", "removal"})
@Override
protected void finalize() throws Throwable
{
if (this.active == true) {
/* set state */
this.active = false;
}
super.finalize();
}
} /* end WolfCryptEccKey */