/**
 * Copyright (C) Posten Norge AS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package no.difi.sdp.client.domain;

import no.difi.sdp.client.domain.exceptions.SertifikatException;
import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.*;

public class Sertifikat {

    private X509Certificate x509Certificate;

    private Sertifikat(X509Certificate x509Certificate) {
        this.x509Certificate = x509Certificate;
    }

    public X509Certificate getX509Certificate() {
        return x509Certificate;
    }

    public byte[] getEncoded() {
        try {
            return x509Certificate.getEncoded();
        } catch (CertificateEncodingException e) {
            throw new SertifikatException("Kunne ikke hente encoded utgave av sertifikatet", e);
        }
    }

    public static Sertifikat fraBase64X509String(String base64) {
        try {
            X509Certificate x509Certificate = (X509Certificate) CertificateFactory
                    .getInstance("X509")
                    .generateCertificate(new ByteArrayInputStream(Base64.decodeBase64(base64)));
            return new Sertifikat(x509Certificate);
        } catch (CertificateException e) {
            throw new SertifikatException("Kunne ikke lese sertifikat fra base64-streng", e);
        }
    }

    public static Sertifikat fraCertificate(X509Certificate certificate) {
        return new Sertifikat(certificate);
    }

    public static Sertifikat fraKeyStore(KeyStore keyStore, String alias) {
        Certificate certificate;
        try {
            certificate = keyStore.getCertificate(alias);
        } catch (KeyStoreException e) {
            throw new SertifikatException("Klarte ikke lese sertifikat fra keystore", e);
        }

        if (certificate == null) {
            throw new SertifikatException("Kunne ikke finne sertifikat i keystore. Er du sikker på at det er brukt keystore med et sertifikat og at du har oppgitt riktig alias?");
        }

        if (!(certificate instanceof X509Certificate)) {
            throw new SertifikatException("Klienten støtter kun X509-sertifikater. Fikk sertifikat av typen " + certificate.getClass().getSimpleName());
        }

        return new Sertifikat((X509Certificate) certificate);
    }
}

@@ -1515,119 +1515,195 @@
     
-X509C
+return lagS
 ertifi
-cate x509Certificate = (X509Certificate) CertificateFactory%0A                    .getInstance(%22X509%22)
+kat(Base64.decodeBase64(base64));%0A        %7D catch (CertificateException e) %7B%0A            throw new SertifikatException(%22Kunne ikke lese sertifikat fra base64-streng%22, e);
 %0A   
@@ -1711,45 +1711,49 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-    .generateC
+public static S
 ertifi
-cate(new 
+kat fra
 Byte
@@ -1761,53 +1761,45 @@
 rray
-InputStream(Base64.decodeBase64(base64)));%0A  
+(byte%5B%5D certificate) %7B%0A        try %7B%0A
     
@@ -1800,35 +1800,36 @@
 %7B%0A          
+  
 return 
-new 
+lag
 Sertifikat(x
@@ -1827,21 +1827,17 @@
 tifikat(
-x509C
+c
 ertifica
@@ -1955,36 +1955,33 @@
 ifikat fra b
-ase64-streng
+yte array
 %22, e);%0A     
@@ -2954,11 +2954,353 @@
 );%0A    %7D
+%0A%0A    private static Sertifikat lagSertifikat(byte%5B%5D certificate) throws CertificateException %7B%0A        X509Certificate x509Certificate = (X509Certificate) CertificateFactory%0A                .getInstance(%22X509%22)%0A                .generateCertificate(new ByteArrayInputStream(certificate));%0A        return new Sertifikat(x509Certificate);%0A    %7D
 %0A%7D%0A

Create Sertifikat from byte array