use of iso.std.iso_iec._24727.tech.schema.CryptoMarkerType in project open-ecard by ecsec.
the class CryptoMarkerBuilder method build.
public CryptoMarkerType build() {
CryptoMarkerType marker = new CryptoMarkerType();
marker.setProtocol(PROTOCOL);
if (algInfo != null) {
try {
JAXBElement<AlgorithmInfoType> e;
e = new JAXBElement<>(new QName(ISONS, "AlgorithmInfo"), AlgorithmInfoType.class, algInfo);
Document d = m.marshal(e);
marker.getAny().add(d.getDocumentElement());
} catch (MarshallingTypeException ex) {
LOG.error("Failed to marshal AlgorithmInfo element.", ex);
}
}
if (keyInfo != null) {
try {
JAXBElement<CryptoKeyInfoType> e;
e = new JAXBElement<>(new QName(ISONS, "KeyInfo"), CryptoKeyInfoType.class, keyInfo);
Document d = m.marshal(e);
marker.getAny().add(d.getDocumentElement());
} catch (MarshallingTypeException ex) {
LOG.error("Failed to marshal KeyInfo element.", ex);
}
}
if (sigGenInfo != null) {
try {
JAXBElement<String> e;
e = new JAXBElement(new QName(ISONS, "SignatureGenerationInfo"), String.class, sigGenInfo);
Document d = m.marshal(e);
marker.getAny().add(d.getDocumentElement());
} catch (MarshallingTypeException ex) {
LOG.error("Failed to marshal SignatureGenerationInfo element.", ex);
}
}
if (legacySignGenInfo != null) {
try {
JAXBElement<LegacySignatureGenerationType> e;
e = new JAXBElement(new QName(ISONS, "LegacySignatureGenerationInfo"), LegacySignatureGenerationType.class, legacySignGenInfo);
Document d = m.marshal(e);
marker.getAny().add(d.getDocumentElement());
} catch (MarshallingTypeException ex) {
LOG.error("Failed to marshal LegacySignatureGenerationInfo element.", ex);
}
}
if (hashGenInfo != null) {
try {
JAXBElement<HashGenerationInfoType> e;
e = new JAXBElement(new QName(ISONS, "HashGenerationInfo"), HashGenerationInfoType.class, hashGenInfo);
Document d = m.marshal(e);
marker.getAny().add(d.getDocumentElement());
} catch (MarshallingTypeException ex) {
LOG.error("Failed to marshal HashGenerationInfo element.", ex);
}
}
for (CertificateRefType certRef : getCertRefs()) {
try {
JAXBElement<CertificateRefType> e;
e = new JAXBElement(new QName(ISONS, "CertificateRef"), CertificateRefType.class, certRef);
Document d = m.marshal(e);
marker.getAny().add(d.getDocumentElement());
} catch (MarshallingTypeException ex) {
LOG.error("Failed to marshal CertificateRef element.", ex);
}
}
if (legacyKeyname != null) {
try {
JAXBElement<String> e;
e = new JAXBElement(new QName(ISONS, "LegacyKeyName"), String.class, legacyKeyname);
Document d = m.marshal(e);
marker.getAny().add(d.getDocumentElement());
} catch (MarshallingTypeException ex) {
LOG.error("Failed to marshal LegacyKeyName element.", ex);
}
}
return marker;
}
use of iso.std.iso_iec._24727.tech.schema.CryptoMarkerType in project open-ecard by ecsec.
the class MiddlewareSAL method sign.
@Override
public SignResponse sign(Sign request) {
SignResponse response = WSHelper.makeResponse(SignResponse.class, WSHelper.makeResultOK());
try {
ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(states, connectionHandle, false);
byte[] application = cardStateEntry.getImplicitlySelectedApplicationIdentifier();
byte[] slotHandle = connectionHandle.getSlotHandle();
String didName = SALUtils.getDIDName(request);
byte[] message = request.getMessage();
Assert.assertIncorrectParameter(message, "The parameter Message is empty.");
DIDStructureType didStructure = cardStateEntry.getDIDStructure(didName, application);
Assert.assertNamedEntityNotFound(didStructure, "The given DIDName cannot be found.");
CryptoMarkerType marker = new CryptoMarkerType(didStructure.getDIDMarker());
String keyLabel = marker.getLegacyKeyName();
MwSession session = managedSessions.get(slotHandle);
for (MwPrivateKey key : session.getPrivateKeys()) {
String nextLabel = "";
try {
nextLabel = key.getKeyLabel();
} catch (CryptokiException ex) {
LOG.warn("Error reading key label.", ex);
}
LOG.debug("Try to match keys '{}' == '{}'", keyLabel, nextLabel);
if (keyLabel.equals(nextLabel)) {
long sigAlg = getPKCS11Alg(marker.getAlgorithmInfo());
byte[] sig = key.sign(sigAlg, message);
response.setSignature(sig);
// set PIN to unauthenticated
setPinNotAuth(cardStateEntry);
return response;
}
}
// TODO: use other exception
String msg = String.format("The given DIDName %s references an unknown key.", didName);
throw new IncorrectParameterException(msg);
} catch (ECardException e) {
LOG.debug(e.getMessage(), e);
response.setResult(e.getResult());
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throwThreadKillException(e);
response.setResult(WSHelper.makeResult(e));
}
return response;
}
use of iso.std.iso_iec._24727.tech.schema.CryptoMarkerType in project open-ecard by ecsec.
the class CIFCreator method createCryptoDID.
private DIDInfoType createCryptoDID(List<MwCertificate> mwCerts, SignatureAlgorithms sigalg) throws WSMarshallerException, CryptokiException {
LOG.debug("Creating Crypto DID object.");
DIDInfoType di = new DIDInfoType();
String keyLabel = mwCerts.get(0).getLabel();
// create differential identity
DifferentialIdentityType did = new DifferentialIdentityType();
di.setDifferentialIdentity(did);
String didName = keyLabel + "_" + mwCerts.get(0).getLabel() + "_" + sigalg.getJcaAlg();
LOG.debug("DIDName: {}", didName);
did.setDIDName(didName);
did.setDIDProtocol("urn:oid:1.3.162.15480.3.0.25");
did.setDIDScope(DIDScopeType.LOCAL);
// create crypto marker
CryptoMarkerBuilder markerBuilder = new CryptoMarkerBuilder();
// add AlgorithmInfo
AlgorithmInfoType algInfo = new AlgorithmInfoType();
algInfo.setAlgorithm(sigalg.getJcaAlg());
AlgorithmIdentifierType algIdentifier = new AlgorithmIdentifierType();
algIdentifier.setAlgorithm(sigalg.getAlgId());
algInfo.setAlgorithmIdentifier(algIdentifier);
algInfo.getSupportedOperations().add("Compute-signature");
markerBuilder.setAlgInfo(algInfo);
markerBuilder.setLegacyKeyname(keyLabel);
// add certificates
for (MwCertificate nextCert : mwCerts) {
try {
CertificateRefType certRef = new CertificateRefType();
certRef.setDataSetName(nextCert.getLabel());
markerBuilder.getCertRefs().add(certRef);
} catch (CryptokiException ex) {
LOG.warn("Certificate chain is not complete.");
break;
}
}
// wrap crypto marker and add to parent
CryptoMarkerType marker = markerBuilder.build();
DIDMarkerType markerWrapper = new DIDMarkerType();
markerWrapper.setCryptoMarker(marker);
did.setDIDMarker(markerWrapper);
// create acl
AccessControlListType acl = new AccessControlListType();
di.setDIDACL(acl);
List<AccessRuleType> rules = acl.getAccessRule();
rules.add(createRuleTrue(AuthorizationServiceActionName.ACL_LIST));
rules.add(createRuleTrue(DifferentialIdentityServiceActionName.DID_GET));
// create sign rule with PIN reference
AccessRuleType signRule = createRuleTrue(CryptographicServiceActionName.SIGN);
signRule.setSecurityCondition(createDidCond(PIN_NAME));
rules.add(signRule);
return di;
}
use of iso.std.iso_iec._24727.tech.schema.CryptoMarkerType in project open-ecard by ecsec.
the class CryptoMarkerTypeTest method testCryptoMarkerType.
/**
* Simple test for CryptoMarkerType.
* After creating the CryptoMarker of the PrK.CH.AUT_signPKCS1_V1_5 DID in the the
* ESIGN application of the EGK we check if the get-methods return the expected values.
*
* @throws Exception
* when something in this test went unexpectedly wrong
*/
@Test
public void testCryptoMarkerType() throws Exception {
WSMarshaller marshaller = WSMarshallerFactory.createInstance();
// setup the iso cryptoMarker type
iso.std.iso_iec._24727.tech.schema.CryptoMarkerType cryptoMarker = new iso.std.iso_iec._24727.tech.schema.CryptoMarkerType();
cryptoMarker.setProtocol("urn:oid:1.3.162.15480.3.0.25");
// algorithm info
AlgorithmInfoType algType = new AlgorithmInfoType();
algType.setAlgorithm("signPKCS1_V1_5");
AlgorithmIdentifierType aIdType = new AlgorithmIdentifierType();
aIdType.setAlgorithm("http://ws.openecard.org/alg/rsa");
algType.setAlgorithmIdentifier(aIdType);
algType.getSupportedOperations().add("Compute-signature");
algType.setCardAlgRef(new byte[] { (byte) 0x02 });
QName elemName = new QName("urn:iso:std:iso-iec:24727:tech:schema", "AlgorithmInfo");
JAXBElement<AlgorithmInfoType> algInfo = new JAXBElement<>(elemName, AlgorithmInfoType.class, algType);
Element algInfoElem = marshaller.marshal(algInfo).getDocumentElement();
cryptoMarker.getAny().add(algInfoElem);
// key info
elemName = new QName("urn:iso:std:iso-iec:24727:tech:schema", "KeyInfo");
CryptoKeyInfoType cryptoKey = new CryptoKeyInfoType();
KeyRefType keyref = new KeyRefType();
keyref.setKeyRef(new byte[] { (byte) 0x02 });
cryptoKey.setKeyRef(keyref);
JAXBElement<CryptoKeyInfoType> keyInfoElem = new JAXBElement<>(elemName, CryptoKeyInfoType.class, cryptoKey);
Element keyrefElem = marshaller.marshal(keyInfoElem).getDocumentElement();
cryptoMarker.getAny().add(keyrefElem);
// signature generation info
elemName = new QName("urn:iso:std:iso-iec:24727:tech:schema", "SignatureGenerationInfo");
JAXBElement<String> sigGenInfoElem = new JAXBElement<>(elemName, String.class, "MSE_KEY_DS PSO_CDS");
Element sigGenElem = marshaller.marshal(sigGenInfoElem).getDocumentElement();
cryptoMarker.getAny().add(sigGenElem);
// certificate references if available
elemName = new QName("urn:iso:std:iso-iec:24727:tech:schema", "CertificateRef");
CertificateRefType certRef = new CertificateRefType();
certRef.setDataSetName("EF.C.CH.AUT");
JAXBElement<CertificateRefType> certRefType = new JAXBElement<>(elemName, CertificateRefType.class, certRef);
Element certRefElement = marshaller.marshal(certRefType).getDocumentElement();
cryptoMarker.getAny().add(certRefElement);
// perform the tests
CryptoMarkerType cryptoMarkerNew = new CryptoMarkerType(cryptoMarker);
assertTrue(cryptoMarkerNew.getAlgorithmInfo().getSupportedOperations().size() > 0);
assertEquals(cryptoMarkerNew.getSignatureGenerationInfo(), new String[] { "MSE_KEY_DS", "PSO_CDS" });
assertEquals(cryptoMarkerNew.getCryptoKeyInfo().getKeyRef().getKeyRef(), new byte[] { 0x02 });
assertEquals(cryptoMarkerNew.getAlgorithmInfo().getAlgorithmIdentifier().getAlgorithm(), "http://ws.openecard.org/alg/rsa");
assertNull(cryptoMarkerNew.getLegacyKeyName());
assertNotNull(cryptoMarkerNew.getHashGenerationInfo());
assertEquals(cryptoMarkerNew.getHashGenerationInfo(), HashGenerationInfoType.NOT_ON_CARD);
assertEquals(cryptoMarkerNew.getCertificateRefs().get(0).getDataSetName(), "EF.C.CH.AUT");
// assertEquals(cryptoMarker.getStateInfo(), "");
assertEquals(cryptoMarker.getProtocol(), ECardConstants.Protocol.GENERIC_CRYPTO);
}
use of iso.std.iso_iec._24727.tech.schema.CryptoMarkerType in project open-ecard by ecsec.
the class DecipherStep method perform.
@Override
public DecipherResponse perform(Decipher request, Map<String, Object> internalData) {
DecipherResponse response = WSHelper.makeResponse(DecipherResponse.class, WSHelper.makeResultOK());
try {
ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
String didName = SALUtils.getDIDName(request);
byte[] applicationID = connectionHandle.getCardApplication();
CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(internalData, connectionHandle);
Assert.securityConditionDID(cardStateEntry, applicationID, didName, CryptographicServiceActionName.DECIPHER);
DIDStructureType didStructure = SALUtils.getDIDStructure(request, didName, cardStateEntry, connectionHandle);
CryptoMarkerType cryptoMarker = new CryptoMarkerType(didStructure.getDIDMarker());
byte[] keyReference = cryptoMarker.getCryptoKeyInfo().getKeyRef().getKeyRef();
byte[] algorithmIdentifier = cryptoMarker.getAlgorithmInfo().getCardAlgRef();
byte[] slotHandle = connectionHandle.getSlotHandle();
// See eGK specification, part 1, version 2.2.0, section 15.9.6.
if (didStructure.getDIDScope().equals(DIDScopeType.LOCAL)) {
keyReference[0] = (byte) (0x80 | keyReference[0]);
}
TLV tagKeyReference = new TLV();
tagKeyReference.setTagNumWithClass(0x84);
tagKeyReference.setValue(keyReference);
TLV tagAlgorithmIdentifier = new TLV();
tagAlgorithmIdentifier.setTagNumWithClass(0x80);
tagAlgorithmIdentifier.setValue(algorithmIdentifier);
byte[] mseData = ByteUtils.concatenate(tagKeyReference.toBER(), tagAlgorithmIdentifier.toBER());
CardCommandAPDU apdu = new ManageSecurityEnvironment((byte) 0x41, ManageSecurityEnvironment.CT, mseData);
apdu.transmit(dispatcher, slotHandle);
byte[] ciphertext = request.getCipherText();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BigInteger bitKeySize = cryptoMarker.getCryptoKeyInfo().getKeySize();
int blocksize = bitKeySize.divide(new BigInteger("8")).intValue();
// check if the ciphertext length is divisible by the blocksize without rest
if ((ciphertext.length % blocksize) != 0) {
return WSHelper.makeResponse(DecipherResponse.class, WSHelper.makeResultError(ECardConstants.Minor.App.INCORRECT_PARM, "The length of the ciphertext should be a multiple of the blocksize."));
}
// decrypt the ciphertext block for block
for (int offset = 0; offset < ciphertext.length; offset += blocksize) {
byte[] ciphertextblock = ByteUtils.copy(ciphertext, offset, blocksize);
apdu = new PSODecipher(ByteUtils.concatenate(PADDING_INDICATOR_BYTE, ciphertextblock), (byte) blocksize);
CardResponseAPDU responseAPDU = apdu.transmit(dispatcher, slotHandle);
baos.write(responseAPDU.getData());
}
response.setPlainText(baos.toByteArray());
} catch (ECardException e) {
response.setResult(e.getResult());
} catch (Exception e) {
logger.error(e.getMessage(), e);
response.setResult(WSHelper.makeResult(e));
}
return response;
}
Aggregations