aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ar71xx/patches-2.6.30/802-ag71xx-use-netdev-ops.patch
diff options
context:
space:
mode:
authorHauke Mehrtens <hauke@hauke-m.de>2023-10-11 23:06:24 +0200
committerHauke Mehrtens <hauke@hauke-m.de>2023-10-11 23:06:24 +0200
commitbd4f415efacfc03bbe5b79ae1d39c1451f5f7385 (patch)
treec3f975521b1bb2631edfaa90678011d144d52342 /target/linux/ar71xx/patches-2.6.30/802-ag71xx-use-netdev-ops.patch
parent6637af95aa9085c8367ce8184b0fe6917365c3d3 (diff)
downloadupstream-23.05.0.tar.gz
upstream-23.05.0.tar.bz2
upstream-23.05.0.zip
OpenWrt v23.05.0: adjust config defaultsv23.05.0
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
Diffstat (limited to 'target/linux/ar71xx/patches-2.6.30/802-ag71xx-use-netdev-ops.patch')
0 files changed, 0 insertions, 0 deletions
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
/**
 *
 */
package com.trilead.ssh2.signature;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
import java.security.SignatureException;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECFieldFp;
import java.security.spec.ECParameterSpec;
import java.security.spec.ECPoint;
import java.security.spec.ECPublicKeySpec;
import java.security.spec.EllipticCurve;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Map;
import java.util.TreeMap;

import com.trilead.ssh2.log.Logger;
import com.trilead.ssh2.packets.TypesReader;
import com.trilead.ssh2.packets.TypesWriter;

/**
 * @author Kenny Root
 *
 */
public class ECDSASHA2Verify {
	private static final Logger log = Logger.getLogger(ECDSASHA2Verify.class);

	public static final String ECDSA_SHA2_PREFIX = "ecdsa-sha2-";

	private static final String NISTP256 = "nistp256";
	private static final String NISTP256_OID = "1.2.840.10045.3.1.7";
	private static final String NISTP384 = "nistp384";
	private static final String NISTP384_OID = "1.3.132.0.34";
	private static final String NISTP521 = "nistp521";
	private static final String NISTP521_OID = "1.3.132.0.35";

	private static final Map<String, ECParameterSpec> CURVES = new TreeMap<String, ECParameterSpec>();
	static {
		CURVES.put(NISTP256, EllipticCurves.nistp256);
		CURVES.put(NISTP384, EllipticCurves.nistp384);
		CURVES.put(NISTP521, EllipticCurves.nistp521);
	}

	private static final Map<Integer, String> CURVE_SIZES = new TreeMap<Integer, String>();
	static {
		CURVE_SIZES.put(256, NISTP256);
		CURVE_SIZES.put(384, NISTP384);
		CURVE_SIZES.put(521, NISTP521);
	}

	private static final Map<String, String> CURVE_OIDS = new TreeMap<String, String>();
	static {
		CURVE_OIDS.put(NISTP256_OID, NISTP256);
		CURVE_OIDS.put(NISTP384_OID, NISTP256);
		CURVE_OIDS.put(NISTP521_OID, NISTP256);
	}

	public static int[] getCurveSizes() {
		int[] keys = new int[CURVE_SIZES.size()];
		int i = 0;
		for (Integer n : CURVE_SIZES.keySet().toArray(new Integer[keys.length])) {
			keys[i++] = n;
		}
		return keys;
	}

	public static ECParameterSpec getCurveForSize(int size) {
		final String name = CURVE_SIZES.get(size);
		if (name == null) {
			return null;
		}
		return CURVES.get(name);
	}

	public static ECPublicKey decodeSSHECDSAPublicKey(byte[] key) throws IOException
	{
		TypesReader tr = new TypesReader(key);

		String key_format = tr.readString();

		if (key_format.startsWith(ECDSA_SHA2_PREFIX) == false)
			throw new IllegalArgumentException("This is not an ECDSA public key");

		String curveName = tr.readString();
		byte[] groupBytes = tr.readByteString();

		if (tr.remain() != 0)
			throw new IOException("Padding in ECDSA public key!");

		if (key_format.equals(ECDSA_SHA2_PREFIX + curveName) == false) {
			throw new IOException("Key format is inconsistent with curve name: " + key_format
					+ " != " + curveName);
		}

		ECParameterSpec params = CURVES.get(curveName);
		if (params == null) {
			throw new IOException("Curve is not supported: " + curveName);
		}

		ECPoint group = ECDSASHA2Verify.decodeECPoint(groupBytes, params.getCurve());
		if (group == null) {
			throw new IOException("Invalid ECDSA group");
		}

		KeySpec keySpec = new ECPublicKeySpec(group, params);

		try {
			KeyFactory kf = KeyFactory.getInstance("EC");
			return (ECPublicKey) kf.generatePublic(keySpec);
		} catch (NoSuchAlgorithmException nsae) {
			IOException ioe = new IOException("No EC KeyFactory available");
			ioe.initCause(nsae);
			throw ioe;
		} catch (InvalidKeySpecException ikse) {
			IOException ioe = new IOException("No EC KeyFactory available");
			ioe.initCause(ikse);
			throw ioe;
		}
	}

	public static byte[] encodeSSHECDSAPublicKey(ECPublicKey key) throws IOException {
		TypesWriter tw = new TypesWriter();

		String curveName = getCurveName(key.getParams());

		String keyFormat = ECDSA_SHA2_PREFIX + curveName;

		tw.writeString(keyFormat);

		tw.writeString(curveName);

		byte[] encoded = encodeECPoint(key.getW(), key.getParams().getCurve());
		tw.writeString(encoded, 0, encoded.length);

		return tw.getBytes();
	}

	public static String getCurveName(ECParameterSpec params) throws IOException {
		int fieldSize = getCurveSize(params);
		final String curveName = getCurveName(fieldSize);
		if (curveName == null) {
			throw new IOException("invalid curve size " + fieldSize);
		}
		return curveName;
	}

	public static String getCurveName(int fieldSize) {
		String curveName = CURVE_SIZES.get(fieldSize);
		if (curveName == null) {
			return null;
		}
		return curveName;
	}

	public static int getCurveSize(ECParameterSpec params) {
		return params.getCurve().getField().getFieldSize();
	}

	public static ECParameterSpec getCurveForOID(String oid) {
		String name = CURVE_OIDS.get(oid);
		if (name == null)
			return null;
		return CURVES.get(name);
	}

	public static byte[] decodeSSHECDSASignature(byte[] sig) throws IOException {
		byte[] rsArray = null;

		TypesReader tr = new TypesReader(sig);

		String sig_format = tr.readString();
		if (sig_format.startsWith(ECDSA_SHA2_PREFIX) == false)
			throw new IOException("Peer sent wrong signature format");

		String curveName = sig_format.substring(ECDSA_SHA2_PREFIX.length());
		if (CURVES.containsKey(curveName) == false) {
			throw new IOException("Unsupported curve: " + curveName);
		}

		rsArray = tr.readByteString();

		if (tr.remain() != 0)
			throw new IOException("Padding in ECDSA signature!");

		byte[] rArray;
		byte[] sArray;
		{
			TypesReader rsReader = new TypesReader(rsArray);
			rArray = rsReader.readMPINT().toByteArray();
			sArray = rsReader.readMPINT().toByteArray();
		}

		int first = rArray.length;
		int second = sArray.length;

		/* We can't have the high bit set, so add an extra zero at the beginning if so. */
		if ((rArray[0] & 0x80) != 0) {
			first++;
		}
		if ((sArray[0] & 0x80) != 0) {
			second++;
		}

		/* Calculate total output length */
		ByteArrayOutputStream os = new ByteArrayOutputStream(6 + first + second);

		/* ASN.1 SEQUENCE tag */
		os.write(0x30);

		/* Size of SEQUENCE */
		writeLength(4 + first + second, os);

		/* ASN.1 INTEGER tag */
		os.write(0x02);

		/* "r" INTEGER length */
		writeLength(first, os);

		/* Copy in the "r" INTEGER */
		if (first != rArray.length) {
			os.write(0x00);
		}
		os.write(rArray);

		/* ASN.1 INTEGER tag */
		os.write(0x02);

		/* "s" INTEGER length */
		writeLength(second, os);

		/* Copy in the "s" INTEGER */
		if (second != sArray.length) {
			os.write(0x00);
		}
		os.write(sArray);

		return os.toByteArray();
	}

	private static final void writeLength(int length, OutputStream os) throws IOException {
		if (length <= 0x7F) {
			os.write(length);
			return;
		}

		int numOctets = 0;
		int lenCopy = length;
		while (lenCopy != 0) {
			lenCopy >>>= 8;
			numOctets++;
		}

		os.write(0x80 | numOctets);

		for (int i = (numOctets - 1) * 8; i >= 0; i -= 8) {
			os.write((byte) (length >> i));
		}
	}

	public static byte[] encodeSSHECDSASignature(byte[] sig, ECParameterSpec params) throws IOException
	{
		TypesWriter tw = new TypesWriter();

		String curveName = getCurveName(params);
		tw.writeString(ECDSA_SHA2_PREFIX + curveName);

		if ((sig[0] != 0x30) || (sig[1] != sig.length - 2) || (sig[2] != 0x02)) {
			throw new IOException("Invalid signature format");
		}

		int rLength = sig[3];
		if ((rLength + 6 > sig.length) || (sig[4 + rLength] != 0x02)) {
			throw new IOException("Invalid signature format");
		}

		int sLength = sig[5 + rLength];
		if (6 + rLength + sLength > sig.length) {
			throw new IOException("Invalid signature format");
		}

		byte[] rArray = new byte[rLength];
		byte[] sArray = new byte[sLength];

		System.arraycopy(sig, 4, rArray, 0, rLength);
		System.arraycopy(sig, 6 + rLength, sArray, 0, sLength);

		BigInteger r = new BigInteger(1, rArray);
		BigInteger s = new BigInteger(1, sArray);

		// Write the <r,s> to its own types writer.
		TypesWriter rsWriter = new TypesWriter();
		rsWriter.writeMPInt(r);
		rsWriter.writeMPInt(s);
		byte[] encoded = rsWriter.getBytes();
		tw.writeString(encoded, 0, encoded.length);

		return tw.getBytes();
	}

	public static byte[] generateSignature(byte[] message, ECPrivateKey pk) throws IOException
	{
		final String algo = getSignatureAlgorithmForParams(pk.getParams());

		try {
			Signature s = Signature.getInstance(algo);
			s.initSign(pk);
			s.update(message);
			return s.sign();
		} catch (NoSuchAlgorithmException e) {
			IOException ex = new IOException();
			ex.initCause(e);
			throw ex;
		} catch (InvalidKeyException e) {
			IOException ex = new IOException();
			ex.initCause(e);
			throw ex;
		} catch (SignatureException e) {
			IOException ex = new IOException();
			ex.initCause(e);
			throw ex;
		}
	}

	public static boolean verifySignature(byte[] message, byte[] ds, ECPublicKey dpk) throws IOException
	{
		final String algo = getSignatureAlgorithmForParams(dpk.getParams());

		try {
			Signature s = Signature.getInstance(algo);
			s.initVerify(dpk);
			s.update(message);
			return s.verify(ds);
		} catch (NoSuchAlgorithmException e) {
			IOException ex = new IOException("No such algorithm");
			ex.initCause(e);
			throw ex;
		} catch (InvalidKeyException e) {
			IOException ex = new IOException("No such algorithm");
			ex.initCause(e);
			throw ex;
		} catch (SignatureException e) {
			IOException ex = new IOException();
			ex.initCause(e);
			throw ex;
		}
	}

	private static String getSignatureAlgorithmForParams(ECParameterSpec params) {
		int size = getCurveSize(params);
		if (size <= 256) {
			return "SHA256withECDSA";
		} else if (size <= 384) {
			return "SHA384withECDSA";
		} else {
			return "SHA512withECDSA";
		}
	}

	public static String getDigestAlgorithmForParams(ECParameterSpec params) {
		int size = getCurveSize(params);
		if (size <= 256) {
			return "SHA-256";
		} else if (size <= 384) {
			return "SHA-384";
		} else {
			return "SHA-512";
		}
	}

	/**
	 * Decode an OctetString to EllipticCurvePoint according to SECG 2.3.4
	 */
	public static ECPoint decodeECPoint(byte[] M, EllipticCurve curve) {
		if (M.length == 0) {
			return null;
		}

		// M has len 2 ceil(log_2(q)/8) + 1 ?
		int elementSize = (curve.getField().getFieldSize() + 7) / 8;
		if (M.length != 2 * elementSize + 1) {
			return null;
		}

		// step 3.2
		if (M[0] != 0x04) {
			return null;
		}

		// Step 3.3
		byte[] xp = new byte[elementSize];
		System.arraycopy(M, 1, xp, 0, elementSize);

		// Step 3.4
		byte[] yp = new byte[elementSize];
		System.arraycopy(M, 1 + elementSize, yp, 0, elementSize);

		ECPoint P = new ECPoint(new BigInteger(1, xp), new BigInteger(1, yp));

		// TODO check point 3.5

		// Step 3.6
		return P;
	}

	/**
	 * Encode EllipticCurvePoint to an OctetString
	 */
	public static byte[] encodeECPoint(ECPoint group, EllipticCurve curve)
	{
		// M has len 2 ceil(log_2(q)/8) + 1 ?
		int elementSize = (curve.getField().getFieldSize() + 7) / 8;
		byte[] M = new byte[2 * elementSize + 1];

		// Uncompressed format
		M[0] = 0x04;

		{
			byte[] affineX = removeLeadingZeroes(group.getAffineX().toByteArray());
			System.arraycopy(affineX, 0, M, 1 + elementSize - affineX.length, affineX.length);
		}

		{
			byte[] affineY = removeLeadingZeroes(group.getAffineY().toByteArray());
			System.arraycopy(affineY, 0, M, 1 + elementSize + elementSize - affineY.length,