-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSm2Certificate.java
More file actions
112 lines (98 loc) · 2.47 KB
/
Sm2Certificate.java
File metadata and controls
112 lines (98 loc) · 2.47 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
/*
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package org.gmssl;
public class Sm2Certificate {
private byte[] cert = null;
public Sm2Certificate() {
this.cert = null;
}
public byte[] getBytes() {
if (this.cert == null) {
throw new GmSSLException("");
}
return this.cert;
}
public void importPem(String file) {
if ((this.cert = GmSSLJNI.cert_from_pem(file)) == null) {
throw new GmSSLException("");
}
}
public void exportPem(String file) {
if (this.cert == null) {
throw new GmSSLException("");
}
if (GmSSLJNI.cert_to_pem(this.cert, file) != 1) {
throw new GmSSLException("");
}
}
public byte[] getSerialNumber() {
if (this.cert == null) {
throw new GmSSLException("");
}
byte[] serial;
if ((serial = GmSSLJNI.cert_get_serial_number(this.cert)) == null) {
throw new GmSSLException("");
}
return serial;
}
public String[] getIssuer() {
if (this.cert == null) {
throw new GmSSLException("");
}
String[] issuer;
if ((issuer = GmSSLJNI.cert_get_issuer(this.cert)) == null) {
throw new GmSSLException("");
}
return issuer;
}
public String[] getSubject() {
if (this.cert == null) {
throw new GmSSLException("");
}
String[] subject;
if ((subject = GmSSLJNI.cert_get_subject(this.cert)) == null) {
throw new GmSSLException("");
}
return subject;
}
public java.util.Date getNotBefore() {
if (this.cert == null) {
throw new GmSSLException("");
}
return new java.util.Date(GmSSLJNI.cert_get_not_before(this.cert));
}
public java.util.Date getNotAfter() {
if (this.cert == null) {
throw new GmSSLException("");
}
return new java.util.Date(GmSSLJNI.cert_get_not_after(this.cert));
}
public Sm2Key getSubjectPublicKey() {
if (this.cert == null) {
throw new GmSSLException("");
}
long pub_key;
if ((pub_key = GmSSLJNI.cert_get_subject_public_key(this.cert)) == 0) {
throw new GmSSLException("");
}
boolean has_private_key = false;
return new Sm2Key(pub_key, has_private_key);
}
public boolean verifyByCaCertificate(Sm2Certificate caCert, String sm2Id) {
if (this.cert == null) {
throw new GmSSLException("");
}
int ret = GmSSLJNI.cert_verify_by_ca_cert(this.cert, caCert.getBytes(), sm2Id);
if (ret == 1) {
return true;
} else {
return false;
}
}
}