001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.xnet.tests.provider.jsse;
019:
020: import java.security.InvalidKeyException;
021: import java.security.KeyFactory;
022: import java.security.NoSuchAlgorithmException;
023: import java.security.NoSuchProviderException;
024: import java.security.PrivateKey;
025: import java.security.PublicKey;
026: import java.security.SignatureException;
027: import java.security.cert.Certificate;
028: import java.security.cert.CertificateException;
029: import java.security.spec.PKCS8EncodedKeySpec;
030: import java.security.spec.X509EncodedKeySpec;
031:
032: import junit.framework.TestCase;
033:
034: import org.apache.harmony.luni.util.Base64;
035: import org.apache.harmony.xnet.provider.jsse.DigitalSignature;
036:
037: /**
038: * Tests for <code>DigitalSignature</code>class
039: */
040: public class DigitalSignatureTest extends TestCase {
041:
042: public void test_Sign() throws Exception {
043:
044: // Regression for HARMONY-2029
045: // data arrays were taken from HARMONY-2125
046:
047: byte[] b64PublicKeySpec = ("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk7q/qebK6/tSLydRUCAvwcqRlS95aau5"
048: + "xj2fFpLRYYG0avuO0qXn14HNmoC8kztk66Q+4oZS9SkQYwr24x+9ide0s9xfjQ5ohDpY6P+mtD6k"
049: + "0piKwYmLXtqi7BTgpgoDXtYi6VJYzvBxLhe050vi1lUNe2iCl/jsU4IcBCcOjV4CwbTDRhq6PzT7"
050: + "70uWtMhAV28E/jcszlyxHYZ5qK0wp8BoBdcNQf3tihBuQkrsv57z94tbEJxg5JeMOl1aWVtw6LLR"
051: + "K2GQBaDrwvy7R4FA2oOc/JS9PsiT0ieKO1dhPGmqJDaVMlZMFeUY41hTzU3BAmcjYBWQI2oNqHRv"
052: + "ya9tUQIDAQAB").getBytes();
053:
054: byte[] b64PrivateKeySpec = ("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCTur+p5srr+1IvJ1FQIC/BypGV"
055: + "L3lpq7nGPZ8WktFhgbRq+47SpefXgc2agLyTO2TrpD7ihlL1KRBjCvbjH72J17Sz3F+NDmiEOljo"
056: + "/6a0PqTSmIrBiYte2qLsFOCmCgNe1iLpUljO8HEuF7TnS+LWVQ17aIKX+OxTghwEJw6NXgLBtMNG"
057: + "Gro/NPvvS5a0yEBXbwT+NyzOXLEdhnmorTCnwGgF1w1B/e2KEG5CSuy/nvP3i1sQnGDkl4w6XVpZ"
058: + "W3DostErYZAFoOvC/LtHgUDag5z8lL0+yJPSJ4o7V2E8aaokNpUyVkwV5RjjWFPNTcECZyNgFZAj"
059: + "ag2odG/Jr21RAgMBAAECggEAJevfPUbQOilGXHJUTiQk/jL4kfogyX5absfsqYfAla4M2RWAARSz"
060: + "Yb+hPpLjVUv+yPpdZhqi+umymin7XCwOpG6ppS3hnTzgmWi83/qYGVanSqP7olijXRL0lXN6g0S4"
061: + "vsRrK8eGooBYHUPanTD+ppQopNAcDdTJHVqdxHceJi1gum4c3yZmoe510TjlqpOezAO6B0N1dfJf"
062: + "KEIQjMdlWgKRq2WNzPDFO8Luhyg40PoTcoWZRhKWV5xPBmq4ew4epxuZpNnuVIzxs9gpjuK55xFg"
063: + "ukXyyhmjsfJzdRs/9kToJdGU3prxf716fk+7OgzgdfWTXK0/uVBE9JIUPBxuAQKBgQDvT+jtTLYN"
064: + "781CujruJUGIn5HHAsqzs1PuKhXS3Nb/+UPkdz0SK3BqReDWIF7db5bxcDDsbfe+OP90DGhd5O6w"
065: + "QbuWA7PdyUJiz2tZ2gBZwvfKYJ/N45KYrFv+f2YmIqhS8lOCV9OoU1/snn8kHRa/UlFfWc8sXSub"
066: + "frWjPAT+4QKBgQCeB/KHW5nv0vjifx3afxo6ZPfD4QOtDRkG9mduzJFkFE5pFFuaLVMHuXph+RFL"
067: + "/vxcukEZOdBBy0/srXN5PV/1kwrhXcRh5YUq8UHF4ExYctCnSn0NbMPazVHY5HSCVwWdNrMk3jIb"
068: + "vNBnTMXtoi7XpSDHM3nk4mF41KZc8nRscQKBgQCQyMXhm8GhWO3UaxtwLTYi1He78aJ1ag9jTi75"
069: + "7gZdw48h0EowjftrMG/A3MDIM9UcqYXP5RA4E//pVABonjMSjBJTxlWx3yu84ETQjaYcqGqGFENa"
070: + "q05r9AuMQ8OnWtx/ooCHoV86vYaRf6roTHkQs1rr10gSTSQu5VA7O/rBwQKBgElAUdEgSqh52FfU"
071: + "qFfhVpz+tEIdiQCr84/go20ecb48E2RtxVAf9j68YNgNBVF+rielRguVWs1EmpWQiNgH9PT15bM1"
072: + "LZRbOXEAR4abQ4g0IDeLNZAFfHttdKTesIrCH54R/tP2Eq/8w3U+hvhxltjqd9keKUBJFvlVSJAI"
073: + "6qJhAoGBAJU8ET4NHv4/AS3SiTcg65TE4ktrTsVZixNYVORE32bFZCZClWLI2aUattiIk8UqvG74"
074: + "wugnxUGk8tJMcw1J/gh+UlnGrX7HqiWTCzEyepOiDQV3NkOQ+9z9WeQNNUtQyIFZZB4wQHaH21BB"
075: + "a1kmrzyqihpAVWrpBOJCipel8S0X").getBytes();
076:
077: // Create public/private keys and certificate
078: KeyFactory keyFactory = KeyFactory.getInstance("RSA");
079: PrivateKey privateKey = keyFactory
080: .generatePrivate(new PKCS8EncodedKeySpec(Base64
081: .decode(b64PrivateKeySpec)));
082: final PublicKey publicKey = keyFactory
083: .generatePublic(new X509EncodedKeySpec(Base64
084: .decode(b64PublicKeySpec)));
085:
086: @SuppressWarnings("serial")
087: Certificate cert = new Certificate("myType") {
088:
089: public PublicKey getPublicKey() {
090: return publicKey;
091: }
092:
093: public byte[] getEncoded() {
094: return null;
095: }
096:
097: public String toString() {
098: return null;
099: }
100:
101: public void verify(PublicKey key)
102: throws CertificateException,
103: NoSuchAlgorithmException, InvalidKeyException,
104: NoSuchProviderException, SignatureException {
105: }
106:
107: public void verify(PublicKey key, String sigProvider)
108: throws CertificateException,
109: NoSuchAlgorithmException, InvalidKeyException,
110: NoSuchProviderException, SignatureException {
111: }
112: };
113:
114: // Sign first: init DigitalSignature with md5 and sha1
115: // CipherSuite.KeyExchange_RSA_EXPORT == 2
116: DigitalSignature dsig = new DigitalSignature(2);
117:
118: dsig.init(privateKey);
119:
120: byte[] md5 = new byte[] {
121: 0x00, // <=== this is a problem byte (see HARMONY-2125)
122: 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
123: 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
124:
125: byte[] sha1 = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
126: 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
127: 0x0f, 0x10, 0x11, 0x12, 0x13 };
128:
129: dsig.setMD5(md5);
130: dsig.setSHA(sha1);
131:
132: byte[] enc = dsig.sign();
133:
134: // Now let's verify
135: dsig.init(cert);
136: assertTrue(dsig.verifySignature(enc));
137: }
138: }
|