001/*
002 * Copyright 2018 Paul Schaub.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.pgpainless.key.parsing;
017
018import javax.annotation.Nonnull;
019import javax.annotation.Nullable;
020import java.io.ByteArrayInputStream;
021import java.io.IOException;
022import java.io.InputStream;
023import java.nio.charset.Charset;
024
025import org.bouncycastle.openpgp.PGPException;
026import org.bouncycastle.openpgp.PGPPublicKeyRing;
027import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
028import org.bouncycastle.openpgp.PGPSecretKeyRing;
029import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
030import org.bouncycastle.openpgp.PGPUtil;
031import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
032import org.pgpainless.key.collection.PGPKeyRing;
033
034public class KeyRingReader {
035
036    public static final Charset UTF8 = Charset.forName("UTF-8");
037
038    public @Nonnull PGPPublicKeyRing publicKeyRing(@Nonnull InputStream inputStream) throws IOException {
039        return readPublicKeyRing(inputStream);
040    }
041
042    public PGPPublicKeyRing publicKeyRing(@Nonnull byte[] bytes) throws IOException {
043        return publicKeyRing(new ByteArrayInputStream(bytes));
044    }
045
046    public PGPPublicKeyRing publicKeyRing(@Nonnull String asciiArmored) throws IOException {
047        return publicKeyRing(asciiArmored.getBytes(UTF8));
048    }
049
050    public PGPPublicKeyRingCollection publicKeyRingCollection(@Nonnull InputStream inputStream)
051            throws IOException, PGPException {
052        return readPublicKeyRingCollection(inputStream);
053    }
054
055    public PGPPublicKeyRingCollection publicKeyRingCollection(@Nonnull byte[] bytes) throws IOException, PGPException {
056        return publicKeyRingCollection(new ByteArrayInputStream(bytes));
057    }
058
059    public PGPPublicKeyRingCollection publicKeyRingCollection(@Nonnull String asciiArmored) throws IOException, PGPException {
060        return publicKeyRingCollection(asciiArmored.getBytes(UTF8));
061    }
062
063    public PGPSecretKeyRing secretKeyRing(@Nonnull InputStream inputStream) throws IOException, PGPException {
064        return readSecretKeyRing(inputStream);
065    }
066
067    public PGPSecretKeyRing secretKeyRing(@Nonnull byte[] bytes) throws IOException, PGPException {
068        return secretKeyRing(new ByteArrayInputStream(bytes));
069    }
070
071    public PGPSecretKeyRing secretKeyRing(@Nonnull String asciiArmored) throws IOException, PGPException {
072        return secretKeyRing(asciiArmored.getBytes(UTF8));
073    }
074
075    public PGPSecretKeyRingCollection secretKeyRingCollection(@Nonnull InputStream inputStream)
076            throws IOException, PGPException {
077        return readSecretKeyRingCollection(inputStream);
078    }
079
080    public PGPSecretKeyRingCollection secretKeyRingCollection(@Nonnull byte[] bytes) throws IOException, PGPException {
081        return secretKeyRingCollection(new ByteArrayInputStream(bytes));
082    }
083
084    public PGPSecretKeyRingCollection secretKeyRingCollection(@Nonnull String asciiArmored) throws IOException, PGPException {
085        return secretKeyRingCollection(asciiArmored.getBytes(UTF8));
086    }
087
088    public PGPKeyRing keyRing(@Nullable InputStream publicIn, @Nullable InputStream secretIn) throws IOException, PGPException {
089        return readKeyRing(publicIn, secretIn);
090    }
091
092    public PGPKeyRing keyRing(@Nullable byte[] publicBytes, @Nullable byte[] secretBytes) throws IOException, PGPException {
093        return keyRing(
094                publicBytes != null ? new ByteArrayInputStream(publicBytes) : null,
095                secretBytes != null ? new ByteArrayInputStream(secretBytes) : null
096        );
097    }
098
099    public PGPKeyRing keyRing(@Nullable String asciiPublic, @Nullable String asciiSecret) throws IOException, PGPException {
100        return keyRing(
101                asciiPublic != null ? asciiPublic.getBytes(UTF8) : null,
102                asciiSecret != null ? asciiSecret.getBytes(UTF8) : null
103        );
104    }
105
106    /*
107    STATIC METHODS
108     */
109
110    public static PGPPublicKeyRing readPublicKeyRing(@Nonnull InputStream inputStream) throws IOException {
111        return new PGPPublicKeyRing(
112                PGPUtil.getDecoderStream(inputStream),
113                new BcKeyFingerprintCalculator());
114    }
115
116    public static PGPPublicKeyRingCollection readPublicKeyRingCollection(@Nonnull InputStream inputStream)
117            throws IOException, PGPException {
118        return new PGPPublicKeyRingCollection(
119                PGPUtil.getDecoderStream(inputStream),
120                new BcKeyFingerprintCalculator());
121    }
122
123    public static PGPSecretKeyRing readSecretKeyRing(@Nonnull InputStream inputStream) throws IOException, PGPException {
124        return new PGPSecretKeyRing(
125                PGPUtil.getDecoderStream(inputStream),
126                new BcKeyFingerprintCalculator());
127    }
128
129    public static PGPSecretKeyRingCollection readSecretKeyRingCollection(@Nonnull InputStream inputStream)
130            throws IOException, PGPException {
131        return new PGPSecretKeyRingCollection(
132                PGPUtil.getDecoderStream(inputStream),
133                new BcKeyFingerprintCalculator());
134    }
135
136    public static PGPKeyRing readKeyRing(@Nullable InputStream publicIn, @Nullable InputStream secretIn) throws IOException, PGPException {
137
138        if (publicIn == null && secretIn == null) {
139            throw new NullPointerException("publicIn and secretIn cannot be BOTH null.");
140        }
141
142        PGPPublicKeyRing publicKeys = null;
143        if (publicIn != null) {
144            publicKeys = readPublicKeyRing(publicIn);
145        }
146        PGPSecretKeyRing secretKeys = null;
147        if (secretIn != null) {
148            secretKeys = readSecretKeyRing(secretIn);
149        }
150
151        if (secretKeys == null) {
152            return new PGPKeyRing(publicKeys);
153        }
154
155        if (publicKeys == null) {
156            return new PGPKeyRing(secretKeys);
157        }
158
159        return new PGPKeyRing(publicKeys, secretKeys);
160    }
161}