|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 2022 Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.helidon.common.pki; |
| 18 | + |
| 19 | +import java.security.spec.KeySpec; |
| 20 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 21 | + |
| 22 | +final class Pkcs1Util { |
| 23 | + // this is a constant for RSA (and we only support RSA) - HexFormat.of().parseHex("020100300D06092A864886F70D0101010500") |
| 24 | + // as HexFormat is only available since Java 17, we have to use explicit bytes here |
| 25 | + private static final byte[] RSA_ALG = new byte[] { |
| 26 | + 0x02, |
| 27 | + 0x01, |
| 28 | + 0x00, |
| 29 | + 0x30, |
| 30 | + 0x0D, |
| 31 | + 0x06, |
| 32 | + 0x09, |
| 33 | + 0x2A, |
| 34 | + (byte) 0x86, |
| 35 | + 0x48, |
| 36 | + (byte) 0x86, |
| 37 | + (byte) 0xF7, |
| 38 | + 0x0D, |
| 39 | + 0x01, |
| 40 | + 0x01, |
| 41 | + 0x01, |
| 42 | + 0x05, |
| 43 | + 0x00 |
| 44 | + }; |
| 45 | + |
| 46 | + private Pkcs1Util() { |
| 47 | + } |
| 48 | + |
| 49 | + static KeySpec pkcs1RsaKeySpec(byte[] bytes) { |
| 50 | + return new PKCS8EncodedKeySpec(pkcs1ToPkcs8(bytes)); |
| 51 | + } |
| 52 | + |
| 53 | + // Code provided by Weijun Wang |
| 54 | + private static byte[] pkcs1ToPkcs8(byte[] pkcs1Bytes) { |
| 55 | + |
| 56 | + // PKCS #8 key will look like |
| 57 | + // 30 len1 |
| 58 | + // 02 01 00 30 0D 06 09 2A 86 48 86 F7 0D 01 01 01 05 00 |
| 59 | + // 04 len2 |
| 60 | + // p1 |
| 61 | + |
| 62 | + byte[] len2 = encodeLen(pkcs1Bytes.length); |
| 63 | + int p8len = pkcs1Bytes.length + len2.length + 1 + RSA_ALG.length; |
| 64 | + byte[] len1 = encodeLen(p8len); |
| 65 | + byte[] pkcs8bytes = new byte[1 + len1.length + p8len]; |
| 66 | + |
| 67 | + pkcs8bytes[0] = 0x30; |
| 68 | + System.arraycopy(len1, 0, pkcs8bytes, 1, len1.length); |
| 69 | + System.arraycopy(RSA_ALG, 0, pkcs8bytes, 1 + len1.length, RSA_ALG.length); |
| 70 | + pkcs8bytes[1 + len1.length + RSA_ALG.length] = 0x04; |
| 71 | + System.arraycopy(len2, 0, pkcs8bytes, 1 + len1.length + RSA_ALG.length + 1, len2.length); |
| 72 | + System.arraycopy(pkcs1Bytes, 0, pkcs8bytes, 1 + len1.length + RSA_ALG.length + 1 + len2.length, pkcs1Bytes.length); |
| 73 | + |
| 74 | + return pkcs8bytes; |
| 75 | + } |
| 76 | + |
| 77 | + private static byte[] encodeLen(int len) { |
| 78 | + if (len < 128) { |
| 79 | + return new byte[] {(byte) len}; |
| 80 | + } else if (len < (1 << 8)) { |
| 81 | + return new byte[] {(byte) 0x081, (byte) len}; |
| 82 | + } else if (len < (1 << 16)) { |
| 83 | + return new byte[] {(byte) 0x082, (byte) (len >> 8), (byte) len}; |
| 84 | + } else { |
| 85 | + throw new PkiException("PKCS#1 key of unexpected size: " + len); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments