All Downloads are FREE. Search and download functionalities are using the official Maven repository.

COSE.SignMessage Maven / Gradle / Ivy

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package COSE;

import java.util.ArrayList;
import java.util.List;
import com.upokecenter.cbor.CBORObject;
import com.upokecenter.cbor.CBORType;

/**
 * The SignMessage class is used to implement the COSE_Sign object.
 * This provides for a signed object with content and one or more signatures attached.
 * The signatures can be either from a single signer or from multiple different signers.
 * In the case where only one signature is required and the signer can be implicitly known, {@link Sign1Message} can be used instead.
 * There is no way to convert a signed message between the two formats.
 * 

* Create a SignMessage object for a new message, when processing an existing message use Message.DecodeFromBytes to create a SignMessage object. *

* Examples can be found at
* Single Signer Example an example of signing and verify a message with a single signature. *
Multiple Signer Example an example of signing and verifying a message which has multiple signatures. * * @author jimsch */ public class SignMessage extends Message { protected List signerList = new ArrayList(); /** * Create a signed message object for which the leading tag and the content will be included. */ public SignMessage() { messageTag = MessageTag.Sign; } /** * Create a signed message object for which the emission of the leading tag and content is controlled by the parameters. * * @param emitTagIn emit leading tag when message is serialized * @param emitContentIn emit the content as part of the message */ public SignMessage(boolean emitTagIn, boolean emitContentIn) { messageTag = MessageTag.Sign; emitTag = emitTagIn; emitContent = emitContentIn; } /** * Internal function used in creating a SignMessage object from a byte string. * * @param obj COSE_Sign encoded object. * @throws CoseException Errors generated by the COSE module */ @Override protected void DecodeFromCBORObject(CBORObject obj) throws CoseException { if (obj.size() != 4) throw new CoseException("Invalid SignMessage structure"); if (obj.get(0).getType() == CBORType.ByteString) { rgbProtected = obj.get(0).GetByteString(); if (obj.get(0).GetByteString().length == 0) { objProtected = CBORObject.NewMap(); } else { objProtected = CBORObject.DecodeFromBytes(rgbProtected); if (objProtected.size() == 0) rgbProtected = new byte[0]; } } else throw new CoseException("Invalid SignMessage structure"); if (obj.get(1).getType() == CBORType.Map) { objUnprotected = obj.get(1); } else throw new CoseException("Invalid SignMessage structure"); if (obj.get(2).getType() == CBORType.ByteString) rgbContent = obj.get(2).GetByteString(); else if (!obj.get(2).isNull()) throw new CoseException("Invalid SignMessage structure"); if (obj.get(3).getType() == CBORType.Array) { for (int i=0; i getSignerList() { return signerList; } /** * Causes a signature to be created for every signer that does not already have one. * * @throws CoseException Errors generated by the COSE module */ public void sign() throws CoseException { if (rgbProtected == null) { if (objProtected.size() == 0) rgbProtected = new byte[0]; else rgbProtected = objProtected.EncodeToBytes(); } for (Signer r : signerList) { r.sign(rgbProtected, rgbContent); } ProcessCounterSignatures(); } /** * Validate the signature on a message for a specific signer. * The signer is required to be one of the Signer objects attached to the message. * The key must be attached to the signer before making this call. * * @param signerToUse which signer to validate with * @return true if the message validates with the signer * @throws CoseException Errors generated by the COSE module */ public boolean validate(Signer signerToUse) throws CoseException { for (Signer r : signerList) { if (r == signerToUse) { return r.validate(rgbProtected, rgbContent); } } throw new CoseException("Signer not found"); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy