identity(without persist & without test)

This commit is contained in:
ghy committed 2021-03-09 15:22:58 +08:00
1 parent 9d370a5353
commit 755ddc2129
21 files changed
+588 -144

No files matched your search

@@ -0,0 +1,235 @@
package minsecurity.identity;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import minsecurity.Common;
import minsecurity.certificate.cert.CertException;
import minsecurity.certificate.cert.Certificate;
import minsecurity.crypto.KeyUtils;
import minsecurity.crypto.PrivateKeyInterface;
import minsecurity.crypto.PublicKeyInterface;
import minsecurity.crypto.SM4;
import minsecurity.crypto.sm2.SM2PrivateKey;
import minsecurity.crypto.sm2.SM2PublicKey;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Base64;
/*
* @Author: hongyu guo
* @Description:
* @Version: 1.0.0
* @Date: 20:25 2021/03/07
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class Identity {
private String Name;
private KeyParam KeyParam;
private PrivateKeyInterface Prikey;
private byte[] PrikeyRawByte;
private PublicKeyInterface Pubkey;
private String Passwd;
private Certificate Cert;
public Identity(String name, KeyParam keyParam,
PrivateKeyInterface prikey, byte[] prikeyRawByte,
PublicKeyInterface pubkey, String passwd, Certificate cert) {
Name = name;
KeyParam = keyParam;
Prikey = prikey;
PrikeyRawByte = prikeyRawByte;
Pubkey = pubkey;
Passwd = passwd;
Cert = cert;
}
public Identity(){}
public byte[] sign(byte[] digest) throws CryptoException {
if(this.Prikey == null)
return null;
switch (this.KeyParam.PublicKeyAlgorithm){
case Common.SM2:
SM2PrivateKey sm2PrivateKey = (SM2PrivateKey) this.Prikey;
switch (this.KeyParam.SignatureAlgorithm){
case Common.SM3withSM2:
return sm2PrivateKey.sign(digest);
default:
return null;
}
default:
return null;
}
}
public boolean verify(byte[] msg, byte[] digest) {
if(this.Pubkey == null)
return false;
switch (this.KeyParam.PublicKeyAlgorithm){
case Common.SM2:
SM2PublicKey sm2PublicKey = (SM2PublicKey) this.Pubkey;
switch (this.KeyParam.SignatureAlgorithm){
case Common.SM3withSM2:
return sm2PublicKey.verify(msg, digest);
default:
return false;
}
default:
return false;
}
}
public byte[] decrypt(byte[] cipher) throws InvalidCipherTextException {
if(this.Prikey == null)
return null;
return this.Prikey.decrypt(cipher);
}
public byte[] encrypt(byte[] content) throws InvalidCipherTextException {
if(this.Pubkey == null)
return null;
return this.Pubkey.encrypt(content);
}
public byte[] dump(String passwd) throws NoSuchPaddingException, InvalidAlgorithmParameterException, CertException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
InnerIdentity innerIdentity = IdentityUtil.parseIdentityToInner(this);
byte[] bytesOfInnerIdentity = mapper.writeValueAsBytes(innerIdentity);
if(passwd != null && passwd.length() != 0) {
byte[] sm4key = KeyUtils.get16bytePasswd(passwd.getBytes());
byte[] cipher = SM4.encrypt_CBC_Padding(sm4key, new byte[16], bytesOfInnerIdentity);
return Base64.getEncoder().encode(cipher);
}
return Base64.getEncoder().encode(bytesOfInnerIdentity);
}
public static Identity load(byte[] data, String passwd)
throws BadPaddingException, NoSuchPaddingException, InvalidAlgorithmParameterException,
NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException,
InvalidKeyException, IOException, CertException {
byte[] bytesOfIdentity = Base64.getDecoder().decode(data);
byte[] plain;
if(passwd != null && passwd.length() != 0){
byte[] sm4Key = KeyUtils.get16bytePasswd(passwd.getBytes());
plain = SM4.decrypt_CBC_Padding(sm4Key, new byte[16], bytesOfIdentity);
} else{
plain = bytesOfIdentity;
}
ObjectMapper mapper = new ObjectMapper();
InnerIdentity innerIdentity = mapper.readValue(plain, InnerIdentity.class);
return IdentityUtil.parseInnerToIdentity(innerIdentity);
}
// TODO: passwd ???
public boolean lock(String passwd, int algo) throws IdentityException {
switch (algo){
case Common.SM4ECB:
if (passwd.length() != 16){
throw new IdentityException("密码应为16位");
}
PrikeyRawByte = Prikey.getBytes();
Prikey = null;
return true;
default:
return false;
}
}
public boolean isLocked(){
return PrikeyRawByte != null && PrikeyRawByte.length > 0;
}
// TODO: passwd ???
public boolean unLock(String passwd, int algo) throws IdentityException {
switch (algo) {
case Common.SM4ECB:
if(passwd == null || passwd.length() == 0)
return false;
PrivateKeyInterface privateKey = KeyUtils.unMarshalPrivateKey(PrikeyRawByte, KeyParam.PublicKeyAlgorithm);
Prikey = privateKey;
PrikeyRawByte = null;
return true;
default:
throw new IdentityException("未知对称加密算法");
}
}
public boolean hasPrivateKey() {
return Prikey != null || (PrikeyRawByte != null && PrikeyRawByte.length > 0);
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public minsecurity.identity.KeyParam getKeyParam() {
return KeyParam;
}
public void setKeyParam(minsecurity.identity.KeyParam keyParam) {
KeyParam = keyParam;
}
public PrivateKeyInterface getPrikey() {
return Prikey;
}
public void setPrikey(PrivateKeyInterface prikey) {
Prikey = prikey;
}
public byte[] getPrikeyRawByte() {
return PrikeyRawByte;
}
public void setPrikeyRawByte(byte[] prikeyRawByte) {
PrikeyRawByte = prikeyRawByte;
}
public PublicKeyInterface getPubkey() {
return Pubkey;
}
public void setPubkey(PublicKeyInterface pubkey) {
Pubkey = pubkey;
}
public String getPasswd() {
return Passwd;
}
public void setPasswd(String passwd) {
Passwd = passwd;
}
public Certificate getCert() {
return Cert;
}
public void setCert(Certificate cert) {
Cert = cert;
}
}
@@ -0,0 +1,14 @@
package minsecurity.identity;
/*
* @Author: hongyu guo
* @Description: Identity自定义异常
* @Version: 1.0.0
* @Date: 14:59 2021/03/09
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class IdentityException extends Exception{
public IdentityException(String msg){
super(msg);
}
}
@@ -0,0 +1,72 @@
package minsecurity.identity;
import minsecurity.Common;
import minsecurity.certificate.cert.CertException;
import minsecurity.certificate.cert.CertUtils;
import minsecurity.certificate.cert.Certificate;
import minsecurity.crypto.KeyUtils;
import minsecurity.crypto.PrivateKeyInterface;
import minsecurity.crypto.PublicKeyInterface;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
/*
* @Author: hongyu guo
* @Description:
* @Version: 1.0.0
* @Date: 10:12 2021/03/09
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class IdentityUtil {
public static InnerIdentity parseIdentityToInner(Identity identity)
throws NoSuchPaddingException, InvalidKeyException, CertException,
IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException,
NoSuchProviderException, InvalidAlgorithmParameterException {
InnerIdentity innerIdentity = new InnerIdentity();
innerIdentity.setName(identity.getName());
innerIdentity.setKeyParam(identity.getKeyParam());
if(identity.getPrikey() != null){
innerIdentity.setPrikey(identity.getPrikey().getBytes());
}
if(identity.getPubkey() != null){
innerIdentity.setPrikey(identity.getPubkey().getBytes());
}
innerIdentity.setPasswd(identity.getPasswd());
innerIdentity.setCert(CertUtils.toPem(identity.getCert(), identity.getPasswd().getBytes(), Common.SM4CBC));
return innerIdentity;
}
public static Identity parseInnerToIdentity(InnerIdentity innerIdentity)
throws BadPaddingException, InvalidKeyException, CertException,
NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException,
InvalidAlgorithmParameterException, IllegalBlockSizeException {
Identity identity = new Identity();
identity.setName(innerIdentity.getName());
identity.setKeyParam(innerIdentity.getKeyParam());
if(innerIdentity.getPubkey() == null || innerIdentity.getPubkey().length == 0){
return null;
}
if(innerIdentity.getPrikey() == null || innerIdentity.getPrikey().length == 0){
return null;
}
PublicKeyInterface pub =
KeyUtils.unMarshalPublicKey(innerIdentity.getPubkey(), innerIdentity.getKeyParam().PublicKeyAlgorithm);
identity.setPubkey(pub);
PrivateKeyInterface priv =
KeyUtils.unMarshalPrivateKey(innerIdentity.getPrikey(), innerIdentity.getKeyParam().PublicKeyAlgorithm);
identity.setPrikey(priv);
identity.setPasswd(innerIdentity.getPasswd());
Certificate certificate =
CertUtils.fromPem(innerIdentity.getCert(), innerIdentity.getPasswd().getBytes(), Common.SM4CBC);
identity.setCert(certificate);
return identity;
}
}
@@ -0,0 +1,96 @@
package minsecurity.identity;
import minsecurity.certificate.cert.Certificate;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import java.util.Arrays;
/*
* @Author: hongyu guo
* @Description:
* @Version: 1.0.0
* @Date: 09:41 2021/03/09
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class InnerIdentity {
private String Name;
private KeyParam KeyParam;
private byte[] Prikey;
private byte[] Pubkey;
private String Passwd;
private String Cert;
public InnerIdentity(String name, minsecurity.identity.KeyParam keyParam, byte[] prikey, byte[] pubkey, String passwd, String cert) {
Name = name;
KeyParam = keyParam;
Prikey = prikey;
Pubkey = pubkey;
Passwd = passwd;
Cert = cert;
}
public InnerIdentity() {
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public minsecurity.identity.KeyParam getKeyParam() {
return KeyParam;
}
public void setKeyParam(minsecurity.identity.KeyParam keyParam) {
KeyParam = keyParam;
}
public byte[] getPrikey() {
return Prikey;
}
public void setPrikey(byte[] prikey) {
Prikey = prikey;
}
public byte[] getPubkey() {
return Pubkey;
}
public void setPubkey(byte[] pubkey) {
Pubkey = pubkey;
}
public String getPasswd() {
return Passwd;
}
public void setPasswd(String passwd) {
Passwd = passwd;
}
public String getCert() {
return Cert;
}
public void setCert(String cert) {
Cert = cert;
}
@Override
public String toString() {
return "InnerIdentity{" +
"Name='" + Name + '\'' +
", KeyParam=" + KeyParam +
", Prikey=" + ByteUtils.toHexString(Prikey) +
", Pubkey=" + ByteUtils.toHexString(Pubkey) +
", Passwd='" + Passwd + '\'' +
", Cert='" + Cert + '\'' +
'}';
}
}
@@ -0,0 +1,21 @@
package minsecurity.identity;
/*
* @Author: hongyu guo
* @Description:
* @Version: 1.0.0
* @Date: 20:31 2021/03/07
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class KeyParam {
public int PublicKeyAlgorithm;
public int SignatureAlgorithm;
public KeyParam(int publicKeyAlgorithm, int signatureAlgorithm) {
this.PublicKeyAlgorithm = publicKeyAlgorithm;
this.SignatureAlgorithm = signatureAlgorithm;
}
public KeyParam() {
}
}