2013-04-08 2 views
1

pkcs11 키 저장소에 RSA 키 쌍을 생성 중입니다. 스마트 카드에 저장 중이며 pkcs10 요청을 생성 중입니다. 내가 pkcs11 키 저장소의 개인 키에 액세스 할 수 없기 때문에 키를 이미 스마트 카드에 저장했기 때문에 개인 키없이 스마트 카드에 저장할 수있는 방법은 무엇입니까?pkcs11 키 저장소에 인증서 저장

String wdtokenpath = "path to dll file"; 
    String pkcs11ConfigSettings = "name = SmartCard\n" + "library =" + wdtokenpath; 
    byte[] pkcs11ConfigBytes = pkcs11ConfigSettings.getBytes(); 
    ByteArrayInputStream confStream = new ByteArrayInputStream(pkcs11ConfigBytes); 
    Provider pkcs11Provider = null; 

    Class sunPkcs11Class = Class.forName("sun.security.pkcs11.SunPKCS11"); 
    Constructor pkcs11Constr = sunPkcs11Class.getConstructor(
      java.io.InputStream.class); 
    pkcs11Provider = (Provider) pkcs11Constr.newInstance(confStream); 
    CallbackHandler call = new TextCallbackHandler(); 
    Subject token = new Subject(); 
    AuthProvider aprov = (AuthProvider) pkcs11Provider; 
    aprov.login(token, call); 

    System.out.println("Login successfully"); 
    KeyPairGenerator keyGen1 = KeyPairGenerator.getInstance("RSA", aprov); 
    keyGen1.initialize(2048); 
    KeyPair pair1 = keyGen1.generateKeyPair(); 
    PublicKey publicKey1 = pair1.getPublic(); 

    String sigAlg = "SHA1withRSA"; 
    PKCS10 pkcs10 = new PKCS10(publicKey1); 
    Signature signature = Signature.getInstance("SHA1withRSA", pkcs11Provider); 
    signature.initSign(pair1.getPrivate()); 

답변

0

보유한 스마트 카드의 종류 또는 갖고있는 PKCS # 11 장치의 종류에 따라 다릅니다. 구현이 다를 수 있습니다.

당신이 SunPKCS11를 사용하고, 당신은 이런 식으로 작업을 수행 할 수 있습니다

public boolean uploadCertificate(X509Certificate cert, String label, String id) { 
     CK_ATTRIBUTE[] certificate = new CK_ATTRIBUTE[9]; 

     certificate[0] = new CK_ATTRIBUTE(PKCS11Constants.CKA_CLASS, PKCS11Constants.CKO_CERTIFICATE); 
     certificate[1] = new CK_ATTRIBUTE(PKCS11Constants.CKA_TOKEN, true); 
     certificate[2] = new CK_ATTRIBUTE(PKCS11Constants.CKA_PRIVATE, false); 
     certificate[3] = new CK_ATTRIBUTE(PKCS11Constants.CKA_LABEL, label.toCharArray()); 
     certificate[4] = new CK_ATTRIBUTE(PKCS11Constants.CKA_SUBJECT, cert.getSubjectX500Principal().getEncoded()); 
     certificate[5] = new CK_ATTRIBUTE(PKCS11Constants.CKA_ID, HexUtils.hexStringToByteArray(id)); 
     certificate[6] = new CK_ATTRIBUTE(PKCS11Constants.CKA_ISSUER, cert.getIssuerX500Principal().getEncoded()); 
     certificate[7] = new CK_ATTRIBUTE(PKCS11Constants.CKA_SERIAL_NUMBER, cert.getSerialNumber().toByteArray()); 

     try { 
      certificate[8] = new CK_ATTRIBUTE(PKCS11Constants.CKA_VALUE, cert.getEncoded()); 
      p11.C_CreateObject(hSession, certificate); 
     } catch (Exception e) { 
      logger.log(Level.SEVERE, "Upload Certificate Exception", e); 
      return false; 
     } 

     return true; 
    } 

또는 IAIK PKCS 번호와

11 래퍼 :

// create certificate object template 
     X509PublicKeyCertificate pkcs11X509PublicKeyCertificate = new X509PublicKeyCertificate(); 

     pkcs11X509PublicKeyCertificate.getToken().setBooleanValue(Boolean.TRUE); 
     pkcs11X509PublicKeyCertificate.getPrivate().setBooleanValue(Boolean.FALSE); 
     pkcs11X509PublicKeyCertificate.getLabel().setCharArrayValue("test".toCharArray()); 
     pkcs11X509PublicKeyCertificate.getSubject().setByteArrayValue(cert.getSubjectX500Principal().getEncoded()); 
     pkcs11X509PublicKeyCertificate.getId().setByteArrayValue(objectId); 
     pkcs11X509PublicKeyCertificate.getIssuer().setByteArrayValue(cert.getIssuerX500Principal().getEncoded()); 
     // serial number should be an DER encoded ASN.1 integer 
     /* 
     INTEGER asn1Integer = new INTEGER(userCertificate.getSerialNumber()); 
     ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
     DerCoder.encodeTo(asn1Integer, buffer); 
     pkcs11X509PublicKeyCertificate.getSerialNumber().setByteArrayValue(buffer.toByteArray()); 
     */ 
     // Netscape deviates from the standard here, for use with Netscape rather use 
     pkcs11X509PublicKeyCertificate.getSerialNumber().setByteArrayValue(cert.getSerialNumber().toByteArray()); 
     pkcs11X509PublicKeyCertificate.getValue().setByteArrayValue(cert.getEncoded()); 

     session.createObject(pkcs11X509PublicKeyCertificate); 

인증서 개체의 ID가 동일해야합니다 생성 된 키의 ID