org.picketlink.pki.internal.DefaultCertificateRequest.java Source code

Java tutorial

Introduction

Here is the source code for org.picketlink.pki.internal.DefaultCertificateRequest.java

Source

/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2012, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.picketlink.pki.internal;

import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder;
import org.picketlink.idm.model.basic.User;
import org.picketlink.pki.cert.CertificateAuthority;
import org.picketlink.pki.cert.CertificateRequest;
import org.picketlink.pki.internal.util.X509Util;

import java.security.KeyPair;

/**
 * @author Pedro Igor
 */
public class DefaultCertificateRequest implements CertificateRequest {

    private static final long serialVersionUID = 1L;

    private final byte[] message;
    private final User user;

    public DefaultCertificateRequest(User user, CertificateAuthority certificateAuthority) {
        try {
            KeyPair userKeyPair = certificateAuthority.getKeyAuthority().getKeyPair(user);
            SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo
                    .getInstance(userKeyPair.getPublic().getEncoded());
            ContentSigner sigGen = X509Util.createSigner(userKeyPair.getPrivate(),
                    certificateAuthority.getConfiguration());
            X500Name userDN = new X500Name(
                    "CN=" + user.getLoginName() + "," + certificateAuthority.getConfiguration().getBaseDN());
            PKCS10CertificationRequest build = new PKCS10CertificationRequestBuilder(userDN, subjectPublicKeyInfo)
                    .build(sigGen);

            this.message = build.getEncoded();
        } catch (Exception e) {
            throw new RuntimeException("Could not create CSR", e);
        }

        this.user = user;
    }

    @Override
    public String getSubjectName() {
        return this.user.getLoginName();
    }

    @Override
    public byte[] getMessage() {
        return this.message;
    }
}