use of org.xipki.security.exception.P11TokenException in project xipki by xipki.
the class ProxyP11Module method send.
// method send
/**
* The request is constructed as follows.
* <pre>
* 0 - - - 1 - - - 2 - - - 3 - - - 4 - - - 5 - - - 6 - - - 7 - - - 8
* | Version | Transaction ID | Body ... |
* | ... Length | Action | Module ID | Content... |
* | .Content | <-- 10 + Length (offset).
*
* </pre>
* @param action action
* @param content content
* @return result.
* @throws P11TokenException If error occurred.
*/
public byte[] send(short action, ASN1Object content) throws P11TokenException {
byte[] encodedContent;
if (content == null) {
encodedContent = null;
} else {
try {
encodedContent = content.getEncoded();
} catch (IOException ex) {
throw new P11TokenException("could encode the content", ex);
}
}
int bodyLen = 4;
if (encodedContent != null) {
bodyLen += encodedContent.length;
}
byte[] request = new byte[10 + bodyLen];
// version
IoUtil.writeShort(version, request, 0);
// transaction id
byte[] transactionId = randomTransactionId();
System.arraycopy(transactionId, 0, request, 2, 4);
// length
IoUtil.writeInt(bodyLen, request, 6);
// action
IoUtil.writeShort(action, request, 10);
// module ID
IoUtil.writeShort(moduleId, request, 12);
// content
if (encodedContent != null) {
System.arraycopy(encodedContent, 0, request, 14, encodedContent.length);
}
byte[] response;
try {
response = send(request);
} catch (IOException ex) {
final String msg = "could not send the request";
LOG.error(msg + " {}", request);
throw new P11TokenException(msg + ": " + ex.getMessage(), ex);
}
int respLen = response.length;
if (respLen < 12) {
throw new P11TokenException("response too short");
}
// Length
int respBodyLen = IoUtil.parseInt(response, 6);
if (respBodyLen + 10 != respLen) {
throw new P11TokenException("message lengt unmatch");
}
// RC
short rc = IoUtil.parseShort(response, 10);
if (rc != 0) {
throw new P11TokenException("server returned RC " + P11ProxyConstants.getReturnCodeName(rc));
}
// Version
short respVersion = IoUtil.parseShort(response, 0);
if (version != respVersion) {
throw new P11TokenException("version of response and request unmatch");
}
// TransactionID
if (!equals(transactionId, response, 2)) {
throw new P11TokenException("version of response and request unmatch");
}
if (respLen < 14) {
throw new P11TokenException("too short successful response");
}
short respAction = IoUtil.parseShort(response, 12);
if (action != respAction) {
throw new P11TokenException("action of response and request unmatch");
}
int respContentLen = respLen - 14;
if (respContentLen == 0) {
return null;
}
byte[] respContent = new byte[respContentLen];
System.arraycopy(response, 14, respContent, 0, respContentLen);
return respContent;
}
use of org.xipki.security.exception.P11TokenException in project xipki by xipki.
the class ProxyP11Module method refresh.
public void refresh() throws P11TokenException {
byte[] resp = send(P11ProxyConstants.ACTION_GET_SERVER_CAPS, null);
Asn1ServerCaps caps;
try {
caps = Asn1ServerCaps.getInstance(resp);
} catch (BadAsn1ObjectException ex) {
throw new P11TokenException("response is a valid Asn1ServerCaps", ex);
}
if (!caps.getVersions().contains(version)) {
throw new P11TokenException("Server does not support any version supported by the client");
}
this.readOnly = caps.isReadOnly();
resp = send(P11ProxyConstants.ACTION_GET_SLOT_IDS, null);
ASN1Sequence seq;
try {
seq = ASN1Sequence.getInstance(resp);
} catch (IllegalArgumentException ex) {
throw new P11TokenException("response is not ASN1Sequence", ex);
}
final int n = seq.size();
Set<P11Slot> slots = new HashSet<>();
for (int i = 0; i < n; i++) {
Asn1P11SlotIdentifier asn1SlotId;
try {
ASN1Encodable obj = seq.getObjectAt(i);
asn1SlotId = Asn1P11SlotIdentifier.getInstance(obj);
} catch (Exception ex) {
throw new P11TokenException(ex.getMessage(), ex);
}
P11SlotIdentifier slotId = asn1SlotId.getSlotId();
if (!conf.isSlotIncluded(slotId)) {
continue;
}
if (!conf.isSlotIncluded(slotId)) {
LOG.info("skipped slot {}", slotId);
continue;
}
P11Slot slot = new ProxyP11Slot(this, slotId, conf.isReadOnly(), conf.getP11MechanismFilter());
slots.add(slot);
}
setSlots(slots);
}
use of org.xipki.security.exception.P11TokenException in project xipki by xipki.
the class ProxyP11Slot method getCertificate.
private X509Cert getCertificate(P11ObjectIdentifier certId) throws P11TokenException {
P11EntityIdentifier entityId = new P11EntityIdentifier(slotId, certId);
byte[] resp = module.send(P11ProxyConstants.ACTION_GET_CERT, new Asn1P11EntityIdentifier(entityId));
if (resp == null) {
return null;
}
try {
return new X509Cert(X509Util.parseCert(resp), resp);
} catch (CertificateException ex) {
throw new P11TokenException("could not parse certificate:" + ex.getMessage(), ex);
}
}
use of org.xipki.security.exception.P11TokenException in project xipki by xipki.
the class ProxyP11Slot method parseGenerateKeypairResult.
private P11Identity parseGenerateKeypairResult(byte[] resp) throws P11TokenException {
if (resp == null) {
throw new P11TokenException("server returned no result");
}
Asn1P11EntityIdentifier ei;
try {
ei = Asn1P11EntityIdentifier.getInstance(resp);
} catch (BadAsn1ObjectException ex) {
throw new P11TokenException("invalid ASN1 object Asn1P11EntityIdentifier: " + ex.getMessage(), ex);
}
if (!slotId.equals(ei.getSlotId().getSlotId())) {
throw new P11TokenException("");
}
P11EntityIdentifier entityId = ei.getEntityId();
PublicKey publicKey = getPublicKey(entityId.getObjectId());
return new ProxyP11Identity(this, entityId, publicKey, null);
}
use of org.xipki.security.exception.P11TokenException in project xipki by xipki.
the class ProxyP11Slot method parseGenerateSecretKeyResult.
private P11Identity parseGenerateSecretKeyResult(byte[] resp) throws P11TokenException {
if (resp == null) {
throw new P11TokenException("server returned no result");
}
Asn1P11EntityIdentifier ei;
try {
ei = Asn1P11EntityIdentifier.getInstance(resp);
} catch (BadAsn1ObjectException ex) {
throw new P11TokenException("invalid ASN1 object Asn1P11EntityIdentifier: " + ex.getMessage(), ex);
}
if (!slotId.equals(ei.getSlotId().getSlotId())) {
throw new P11TokenException("");
}
P11EntityIdentifier entityId = ei.getEntityId();
return new ProxyP11Identity(this, entityId);
}
Aggregations