1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | /**************************************************************************** |
20 | * Copyright (c) 1998-2009 AOL LLC. |
21 | * |
22 | * Licensed under the Apache License, Version 2.0 (the "License"); |
23 | * you may not use this file except in compliance with the License. |
24 | * You may obtain a copy of the License at |
25 | * |
26 | * http://www.apache.org/licenses/LICENSE-2.0 |
27 | * |
28 | * Unless required by applicable law or agreed to in writing, software |
29 | * distributed under the License is distributed on an "AS IS" BASIS, |
30 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
31 | * See the License for the specific language governing permissions and |
32 | * limitations under the License. |
33 | * |
34 | **************************************************************************** |
35 | * |
36 | * @author: zhang |
37 | * @version: $Revision$ |
38 | * @created: Apr 24, 2009 |
39 | * |
40 | * Description: A KeySpec for PKCS#1 encoded RSA private key |
41 | * |
42 | ****************************************************************************/ |
43 | |
44 | package net.oauth.signature.pem; |
45 | |
46 | import java.io.IOException; |
47 | import java.math.BigInteger; |
48 | import java.security.spec.RSAPublicKeySpec; |
49 | |
50 | /** |
51 | * PKCS#1 encoded public key spec. In oauth package as they made all classes |
52 | * package visible. |
53 | * |
54 | * |
55 | * @author Adrian Cole |
56 | */ |
57 | public class PKCS1EncodedPublicKeySpec { |
58 | |
59 | private RSAPublicKeySpec keySpec; |
60 | |
61 | /** |
62 | * Create a PKCS#1 keyspec from DER encoded buffer |
63 | * |
64 | * @param keyBytes |
65 | * DER encoded octet stream |
66 | * @throws IOException |
67 | */ |
68 | public PKCS1EncodedPublicKeySpec(byte[] keyBytes) throws IOException { |
69 | decode(keyBytes); |
70 | } |
71 | |
72 | /** |
73 | * Get the key spec that JCE understands. |
74 | * |
75 | * @return CRT keyspec defined by JCE |
76 | */ |
77 | public RSAPublicKeySpec getKeySpec() { |
78 | return keySpec; |
79 | } |
80 | |
81 | /** |
82 | * Decode PKCS#1 encoded private key into RSAPublicKeySpec. |
83 | * |
84 | * <p/> |
85 | * The ASN.1 syntax for the private key with CRT is |
86 | * |
87 | * <pre> |
88 | * -- |
89 | * -- Representation of RSA private key with information for the CRT algorithm. |
90 | * -- |
91 | * RSAPrivateKey ::= SEQUENCE { |
92 | * version Version, |
93 | * modulus INTEGER, -- n |
94 | * publicExponent INTEGER, -- e |
95 | * } |
96 | * </pre> |
97 | * |
98 | * @param keyBytes |
99 | * PKCS#1 encoded key |
100 | * @throws IOException |
101 | */ |
102 | |
103 | private void decode(byte[] keyBytes) throws IOException { |
104 | |
105 | DerParser parser = new DerParser(keyBytes); |
106 | |
107 | Asn1Object sequence = parser.read(); |
108 | if (sequence.getType() != DerParser.SEQUENCE) |
109 | throw new IOException("Invalid DER: not a sequence"); //$NON-NLS-1$ |
110 | |
111 | // Parse inside the sequence |
112 | parser = sequence.getParser(); |
113 | |
114 | BigInteger modulus = parser.read().getInteger(); |
115 | BigInteger publicExp = parser.read().getInteger(); |
116 | |
117 | keySpec = new RSAPublicKeySpec(modulus, publicExp); |
118 | } |
119 | } |