forked from ofdrw/ofdrw
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
263df4b
commit a9a6697
Showing
6 changed files
with
208 additions
and
12 deletions.
There are no files selected for viewing
10 changes: 3 additions & 7 deletions
10
...erify/container/DigitalSignContainer.java → ...y/container/DigitalValidateContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
ofdrw-sign/src/main/java/org/ofdrw/sign/verify/container/SESV1ValidateContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.ofdrw.sign.verify.container; | ||
|
||
import org.bouncycastle.jcajce.provider.asymmetric.x509.CertificateFactory; | ||
import org.bouncycastle.jcajce.provider.digest.SM3; | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
import org.ofdrw.core.signatures.SigType; | ||
|
||
import org.ofdrw.gm.ses.v1.SES_Signature; | ||
import org.ofdrw.gm.ses.v1.TBS_Sign; | ||
import org.ofdrw.sign.verify.SignedDataValidateContainer; | ||
import org.ofdrw.sign.verify.exceptions.InvalidSignedValueException; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.security.MessageDigest; | ||
import java.security.Signature; | ||
import java.security.cert.Certificate; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* 《《GM/T 0031-2014 安全电子签章密码技术规范》 电子印章数据验证 | ||
* <p> | ||
* 注意:仅用于测试,电子签章验证请使用符合国家规范的流程进行! | ||
* | ||
* @author 权观宇 | ||
* @since 2020-04-22 22:56:23 | ||
*/ | ||
public class SESV1ValidateContainer implements SignedDataValidateContainer { | ||
|
||
@Override | ||
public void validate(SigType type, | ||
String signAlgName, | ||
byte[] tbsContent, | ||
byte[] signedValue) | ||
throws InvalidSignedValueException, IOException, GeneralSecurityException { | ||
if (type == SigType.Sign) { | ||
throw new IllegalArgumentException("签名类型(type)必须是 Seal,不支持电子印章验证"); | ||
} | ||
|
||
// 计算原文摘要 | ||
MessageDigest md = new SM3.Digest(); | ||
byte[] actualDataHash = md.digest(tbsContent); | ||
|
||
SES_Signature sesSignature = SES_Signature.getInstance(signedValue); | ||
TBS_Sign toSign = sesSignature.getToSign(); | ||
byte[] expectDataHash = toSign.getDataHash().getOctets(); | ||
|
||
|
||
// 比较原文摘要 | ||
if (!Arrays.equals(actualDataHash, expectDataHash)) { | ||
throw new InvalidSignedValueException("Signature.xml 文件被篡改,电子签章失效。(" | ||
+ toSign.getPropertyInfo().getString() + ")"); | ||
} | ||
|
||
// 预期的电子签章数据,签章值 | ||
byte[] expSigVal = sesSignature.getSignature().getOctets(); | ||
|
||
Signature sg = Signature.getInstance( toSign.getSignatureAlgorithm().getId(), | ||
new BouncyCastleProvider()); | ||
byte[] certDER = toSign.getCert().getOctets(); | ||
// 构造证书对象 | ||
Certificate signCert = new CertificateFactory() | ||
.engineGenerateCertificate(new ByteArrayInputStream(certDER)); | ||
sg.initVerify(signCert); | ||
sg.update(toSign.getEncoded("DER")); | ||
if (!sg.verify(expSigVal)) { | ||
throw new InvalidSignedValueException("电子签章数据签名值不匹配,电子签章数据失效。"); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
ofdrw-sign/src/main/java/org/ofdrw/sign/verify/container/SESV4ValidateContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.ofdrw.sign.verify.container; | ||
|
||
import org.bouncycastle.asn1.ASN1BitString; | ||
import org.bouncycastle.asn1.ASN1OctetString; | ||
import org.bouncycastle.jcajce.provider.asymmetric.x509.CertificateFactory; | ||
import org.bouncycastle.jcajce.provider.digest.SM3; | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
import org.ofdrw.core.signatures.SigType; | ||
import org.ofdrw.gm.ses.v4.SES_Signature; | ||
import org.ofdrw.gm.ses.v4.TBS_Sign; | ||
import org.ofdrw.sign.verify.SignedDataValidateContainer; | ||
import org.ofdrw.sign.verify.exceptions.InvalidSignedValueException; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.security.MessageDigest; | ||
import java.security.Signature; | ||
import java.security.cert.Certificate; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* 《GB/T 38540-2020 信息安全技术 安全电子签章密码技术规范》 电子印章数据验证 | ||
* <p> | ||
* 注意:仅用于测试,电子签章验证请使用符合国家规范的流程进行! | ||
* | ||
* @author 权观宇 | ||
* @since 2020-04-22 22:56:23 | ||
*/ | ||
public class SESV4ValidateContainer implements SignedDataValidateContainer { | ||
|
||
@Override | ||
public void validate(SigType type, | ||
String signAlgName, | ||
byte[] tbsContent, | ||
byte[] signedValue) | ||
throws InvalidSignedValueException, IOException, GeneralSecurityException { | ||
if (type == SigType.Sign) { | ||
throw new IllegalArgumentException("签名类型(type)必须是 Seal,不支持电子印章验证"); | ||
} | ||
|
||
// 计算原文摘要 | ||
MessageDigest md = new SM3.Digest(); | ||
byte[] actualDataHash = md.digest(tbsContent); | ||
|
||
SES_Signature sesSignature = SES_Signature.getInstance(signedValue); | ||
TBS_Sign toSign = sesSignature.getToSign(); | ||
byte[] expectDataHash = toSign.getDataHash().getOctets(); | ||
|
||
// 比较原文摘要 | ||
if (!Arrays.equals(actualDataHash, expectDataHash)) { | ||
throw new InvalidSignedValueException("Signature.xml 文件被篡改,电子签章失效。(" | ||
+ toSign.getPropertyInfo().getString() + ")"); | ||
} | ||
|
||
// 预期的电子签章数据,签章值 | ||
byte[] expSigVal = sesSignature.getSignature().getOctets(); | ||
Signature sg = Signature.getInstance(sesSignature.getSignatureAlgID().getId(), | ||
new BouncyCastleProvider()); | ||
byte[] certDER = sesSignature.getCert().getOctets(); | ||
// 构造证书对象 | ||
Certificate signCert = new CertificateFactory() | ||
.engineGenerateCertificate(new ByteArrayInputStream(certDER)); | ||
sg.initVerify(signCert); | ||
sg.update(toSign.getEncoded("DER")); | ||
if (!sg.verify(expSigVal)) { | ||
throw new InvalidSignedValueException("电子签章数据签名值不匹配,电子签章数据失效。"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
ofdrw-sign/src/test/java/org/ofdrw/sign/verify/container/SESV1ValidateContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.ofdrw.sign.verify.container; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.ofdrw.reader.OFDReader; | ||
import org.ofdrw.sign.verify.OFDValidator; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.security.GeneralSecurityException; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
* @author 权观宇 | ||
* @since 2020-04-22 23:37:16 | ||
*/ | ||
class SESV1ValidateContainerTest { | ||
@Test | ||
void validate() throws IOException, GeneralSecurityException { | ||
Path src = Paths.get("target/SESV1SignDoc.ofd"); | ||
|
||
try (OFDReader reader = new OFDReader(src); | ||
OFDValidator validator = new OFDValidator(reader)) { | ||
validator.setValidator(new SESV1ValidateContainer()); | ||
validator.exeValidate(); | ||
System.out.println(">> 验证通过"); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
ofdrw-sign/src/test/java/org/ofdrw/sign/verify/container/SESV4ValidateContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.ofdrw.sign.verify.container; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.ofdrw.reader.OFDReader; | ||
import org.ofdrw.sign.verify.OFDValidator; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.security.GeneralSecurityException; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
* @author 权观宇 | ||
* @since 2020-04-22 23:10:01 | ||
*/ | ||
class SESV4ValidateContainerTest { | ||
|
||
|
||
@Test | ||
void validate() throws IOException, GeneralSecurityException { | ||
Path src = Paths.get("target/SESV4SignDoc.ofd"); | ||
|
||
try (OFDReader reader = new OFDReader(src); | ||
OFDValidator validator = new OFDValidator(reader)) { | ||
validator.setValidator(new SESV4ValidateContainer()); | ||
validator.exeValidate(); | ||
System.out.println(">> 验证通过"); | ||
} | ||
} | ||
} |