use of com.bsnbase.sdk.util.common.UserCertInfo in project PCNGateway-Java-SDK by BSNDA.
the class R1Algorithm method getUserCertInfo.
/**
* Get certificate CSR
*
* @param DN
* @return
*/
@Override
public UserCertInfo getUserCertInfo(String DN) throws Exception {
Security.addProvider(new BouncyCastleProvider());
int algSize = 256;
String sigAlg = "SHA256withECDSA";
KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECDSA");
kpg.initialize(algSize, new SecureRandom());
KeyPair kp = kpg.generateKeyPair();
PrivateKey privateKey = kp.getPrivate();
Signature signature = Signature.getInstance(sigAlg);
signature.initSign(privateKey);
X500Name x500Name = new X500Name(DN);
SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(kp.getPublic().getEncoded());
PKCS10CertificationRequestBuilder builder = new PKCS10CertificationRequestBuilder(x500Name, subjectPublicKeyInfo);
JcaContentSignerBuilder jcaContentSignerBuilder = new JcaContentSignerBuilder(sigAlg);
Provider BC = new BouncyCastleProvider();
jcaContentSignerBuilder.setProvider(BC);
ContentSigner contentSigner = jcaContentSignerBuilder.build(kp.getPrivate());
PKCS10CertificationRequest csr = builder.build(contentSigner);
byte[] der = csr.getEncoded();
String strPEMCSR = "-----BEGIN CERTIFICATE REQUEST-----\n";
strPEMCSR += new String(org.bouncycastle.util.encoders.Base64.encode(der));
strPEMCSR += "\n-----END CERTIFICATE REQUEST-----\n";
UserCertInfo user = new UserCertInfo();
user.setCSRPem(strPEMCSR);
user.setKey(privateKey);
return user;
}
use of com.bsnbase.sdk.util.common.UserCertInfo in project PCNGateway-Java-SDK by BSNDA.
the class UserService method userEnroll.
/**
* User certificate registration in Public Key Upload Mode
* When a user participated in the application in the public key upload mode needs to register a sub-user, after completing the user registration interface, he/she can call this interface to upload a public key certificate application file and obtain a sub-user certificate issued by the city node.
* An exception will be returned when this interface is called in key trust mode.
*/
public static ResKeyEscrowEnroll userEnroll(@NotNull ReqKeyEscrowEnroll kes) throws IOException {
String api = Config.config.getApi() + PathUtil.FABRIC_USER_ENROLL;
UserCertInfo certInfo = StoreUtils.generateCSR(kes.getName(), Config.config.getAppCode());
kes.setCsrPem(certInfo.getCSRPem());
BaseReqModel<ReqKeyEscrowEnroll> req = new BaseReqModel<ReqKeyEscrowEnroll>();
req.setReqHeader(Config.config.getUserCode(), Config.config.getAppCode());
req.setBody(kes);
HttpService<ReqKeyEscrowEnroll, ResKeyEscrowEnroll> httpService = new HttpService<ReqKeyEscrowEnroll, ResKeyEscrowEnroll>();
BaseResModel<ResKeyEscrowEnroll> res = httpService.post(req, api, ResKeyEscrowEnroll.class);
ResKeyEscrowEnroll body = res.getBody();
// Save the private key
Config.config.getKeyStore().storeUserPrivateKey(kes.getName(), Config.config.getAppCode(), certInfo.getKey());
// Save the registered certificate
Config.config.getKeyStore().storeUserCert(kes.getName(), Config.config.getAppCode(), body.getCert());
return body;
}
use of com.bsnbase.sdk.util.common.UserCertInfo in project PCNGateway-Java-SDK by BSNDA.
the class SM2Algorithm method getUserCertInfo.
/**
* Get certificate CSR
*
* @param DN
* @return
*/
@Override
public UserCertInfo getUserCertInfo(String DN) throws Exception {
Sm2Util.createKeyPair();
KeyPair kp = Sm2Util.keyPair;
PrivateKey privateKey = kp.getPrivate();
PKCS10CertificationRequestBuilder builder = new PKCS10CertificationRequestBuilder(new X500Name(DN), SubjectPublicKeyInfo.getInstance(kp.getPublic().getEncoded()));
JcaContentSignerBuilder jcaContentSignerBuilder = new JcaContentSignerBuilder("SM3withSM2");
Provider BC = new BouncyCastleProvider();
jcaContentSignerBuilder.setProvider(BC);
ContentSigner contentSigner = jcaContentSignerBuilder.build(kp.getPrivate());
PKCS10CertificationRequest csr = builder.build(contentSigner);
byte[] der = csr.getEncoded();
String strPEMCSR = "-----BEGIN CERTIFICATE REQUEST-----\n";
strPEMCSR += new String(Base64.encode(der));
strPEMCSR += "\n-----END CERTIFICATE REQUEST-----\n";
UserCertInfo user = new UserCertInfo();
user.setCSRPem(strPEMCSR);
user.setKey(privateKey);
return user;
}
Aggregations