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

org.jscep.message.PkiMessage Maven / Gradle / Ivy

There is a newer version: 1.3
Show newest version
/*
 * Copyright (c) 2010 ThruPoint Ltd
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.jscep.message;

import java.util.Collection;
import java.util.HashSet;

import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERPrintableString;
import org.bouncycastle.asn1.DERSet;
import org.bouncycastle.asn1.cms.Attribute;
import org.jscep.asn1.ScepObjectIdentifiers;
import org.jscep.transaction.MessageType;
import org.jscep.transaction.Nonce;
import org.jscep.transaction.TransactionId;

/**
 * This class represents an abstract SCEP PkiMessage, which may be either a
 * request or response.
 *
 * @param  the MessageData for this message.
 */
public abstract class PkiMessage {
	private final TransactionId transId;
	private final MessageType messageType;
	private final Nonce senderNonce;
	private final T messageData;
	
	public PkiMessage(TransactionId transId, MessageType messageType, Nonce senderNonce, T messageData) {
		this.transId = transId;
		this.messageType = messageType;
		this.senderNonce = senderNonce;
		this.messageData = messageData;
	}
	
	public TransactionId getTransactionId() {
		return transId;
	}
	
	public MessageType getMessageType() {
		return messageType;
	}
	
	public Nonce getSenderNonce() {
		return senderNonce;
	}
	
	public T getMessageData() {
		return messageData;
	}
	
	public Collection getAttributes() {
		final Collection attrs = new HashSet();

		final Attribute transIdAttr = new Attribute(ScepObjectIdentifiers.transId, toSet(transId));
		final Attribute messageTypeAttr = new Attribute(ScepObjectIdentifiers.messageType, toSet(messageType));
		final Attribute senderNonceAttr = new Attribute(ScepObjectIdentifiers.senderNonce, toSet(senderNonce));
		attrs.add(transIdAttr);
		attrs.add(messageTypeAttr);
		attrs.add(senderNonceAttr);
		
		return attrs;
	}
	
	ASN1Set toSet(Nonce nonce) {
		return new DERSet(new DEROctetString(nonce.getBytes()));
	}
	
	private ASN1Set toSet(TransactionId transId) {
		return new DERSet(new DERPrintableString(transId.getBytes()));
	}
	
	private ASN1Set toSet(MessageType messageType) {
		return new DERSet(new DERPrintableString(messageType.toString()));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy