// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.cms;

import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.cms.OtherRecipientInfo;
import org.bouncycastle.asn1.cms.KEMRecipientInfo;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.cms.RecipientIdentifier;
import org.bouncycastle.operator.OperatorException;
import org.bouncycastle.asn1.cms.RecipientInfo;
import org.bouncycastle.operator.GenericKey;
import org.bouncycastle.asn1.cms.IssuerAndSerialNumber;

public abstract class KEMRecipientInfoGenerator implements RecipientInfoGenerator
{
    protected final KEMKeyWrapper wrapper;
    private IssuerAndSerialNumber issuerAndSerial;
    private byte[] subjectKeyIdentifier;
    
    protected KEMRecipientInfoGenerator(final IssuerAndSerialNumber issuerAndSerial, final KEMKeyWrapper wrapper) {
        this.issuerAndSerial = issuerAndSerial;
        this.wrapper = wrapper;
    }
    
    protected KEMRecipientInfoGenerator(final byte[] subjectKeyIdentifier, final KEMKeyWrapper wrapper) {
        this.subjectKeyIdentifier = subjectKeyIdentifier;
        this.wrapper = wrapper;
    }
    
    @Override
    public final RecipientInfo generate(final GenericKey genericKey) throws CMSException {
        byte[] generateWrappedKey;
        try {
            generateWrappedKey = this.wrapper.generateWrappedKey(genericKey);
        }
        catch (final OperatorException ex) {
            throw new CMSException("exception wrapping content key: " + ex.getMessage(), ex);
        }
        RecipientIdentifier recipientIdentifier;
        if (this.issuerAndSerial != null) {
            recipientIdentifier = new RecipientIdentifier(this.issuerAndSerial);
        }
        else {
            recipientIdentifier = new RecipientIdentifier(new DEROctetString(this.subjectKeyIdentifier));
        }
        return new RecipientInfo(new OtherRecipientInfo(CMSObjectIdentifiers.id_ori_kem, new KEMRecipientInfo(recipientIdentifier, this.wrapper.getAlgorithmIdentifier(), new DEROctetString(this.wrapper.getEncapsulation()), this.wrapper.getKdfAlgorithmIdentifier(), new ASN1Integer(this.wrapper.getKekLength()), null, this.wrapper.getWrapAlgorithmIdentifier(), new DEROctetString(generateWrappedKey))));
    }
}
