mobile/android/base/browserid/MockMyIDTokenFactory.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/browserid/MockMyIDTokenFactory.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.browserid;
     1.9 +
    1.10 +import java.math.BigInteger;
    1.11 +import java.security.NoSuchAlgorithmException;
    1.12 +import java.security.spec.InvalidKeySpecException;
    1.13 +
    1.14 +/**
    1.15 + * Generate certificates and assertions backed by mockmyid.com's private key.
    1.16 + * <p>
    1.17 + * These artifacts are for testing only.
    1.18 + */
    1.19 +public class MockMyIDTokenFactory {
    1.20 +  public static final BigInteger MOCKMYID_x = new BigInteger("385cb3509f086e110c5e24bdd395a84b335a09ae", 16);
    1.21 +  public static final BigInteger MOCKMYID_y = new BigInteger("738ec929b559b604a232a9b55a5295afc368063bb9c20fac4e53a74970a4db7956d48e4c7ed523405f629b4cc83062f13029c4d615bbacb8b97f5e56f0c7ac9bc1d4e23809889fa061425c984061fca1826040c399715ce7ed385c4dd0d402256912451e03452d3c961614eb458f188e3e8d2782916c43dbe2e571251ce38262", 16);
    1.22 +  public static final BigInteger MOCKMYID_p = new BigInteger("ff600483db6abfc5b45eab78594b3533d550d9f1bf2a992a7a8daa6dc34f8045ad4e6e0c429d334eeeaaefd7e23d4810be00e4cc1492cba325ba81ff2d5a5b305a8d17eb3bf4a06a349d392e00d329744a5179380344e82a18c47933438f891e22aeef812d69c8f75e326cb70ea000c3f776dfdbd604638c2ef717fc26d02e17", 16);
    1.23 +  public static final BigInteger MOCKMYID_q = new BigInteger("e21e04f911d1ed7991008ecaab3bf775984309c3", 16);
    1.24 +  public static final BigInteger MOCKMYID_g = new BigInteger("c52a4a0ff3b7e61fdf1867ce84138369a6154f4afa92966e3c827e25cfa6cf508b90e5de419e1337e07a2e9e2a3cd5dea704d175f8ebf6af397d69e110b96afb17c7a03259329e4829b0d03bbc7896b15b4ade53e130858cc34d96269aa89041f409136c7242a38895c9d5bccad4f389af1d7a4bd1398bd072dffa896233397a", 16);
    1.25 +
    1.26 +  // Computed lazily by static <code>getMockMyIDPrivateKey</code>.
    1.27 +  protected static SigningPrivateKey cachedMockMyIDPrivateKey = null;
    1.28 +
    1.29 +  public static SigningPrivateKey getMockMyIDPrivateKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
    1.30 +    if (cachedMockMyIDPrivateKey == null) {
    1.31 +      cachedMockMyIDPrivateKey = DSACryptoImplementation.createPrivateKey(MOCKMYID_x, MOCKMYID_p, MOCKMYID_q, MOCKMYID_g);
    1.32 +    }
    1.33 +    return cachedMockMyIDPrivateKey;
    1.34 +  }
    1.35 +
    1.36 +  /**
    1.37 +   * Sign a public key asserting ownership of username@mockmyid.com with
    1.38 +   * mockmyid.com's private key.
    1.39 +   *
    1.40 +   * @param publicKeyToSign
    1.41 +   *          public key to sign.
    1.42 +   * @param username
    1.43 +   *          sign username@mockmyid.com
    1.44 +   * @param issuedAt
    1.45 +   *          timestamp for certificate, in milliseconds since the epoch.
    1.46 +   * @param expiresAt
    1.47 +   *          expiration timestamp for certificate, in milliseconds since the epoch.
    1.48 +   * @return encoded certificate string.
    1.49 +   * @throws Exception
    1.50 +   */
    1.51 +  public String createMockMyIDCertificate(final VerifyingPublicKey publicKeyToSign, String username,
    1.52 +      final long issuedAt, final long expiresAt)
    1.53 +          throws Exception {
    1.54 +    if (!username.endsWith("@mockmyid.com")) {
    1.55 +      username = username + "@mockmyid.com";
    1.56 +    }
    1.57 +    SigningPrivateKey mockMyIdPrivateKey = getMockMyIDPrivateKey();
    1.58 +    return JSONWebTokenUtils.createCertificate(publicKeyToSign, username, "mockmyid.com", issuedAt, expiresAt, mockMyIdPrivateKey);
    1.59 +  }
    1.60 +
    1.61 +  /**
    1.62 +   * Sign a public key asserting ownership of username@mockmyid.com with
    1.63 +   * mockmyid.com's private key.
    1.64 +   *
    1.65 +   * @param publicKeyToSign
    1.66 +   *          public key to sign.
    1.67 +   * @param username
    1.68 +   *          sign username@mockmyid.com
    1.69 +   * @return encoded certificate string.
    1.70 +   * @throws Exception
    1.71 +   */
    1.72 +  public String createMockMyIDCertificate(final VerifyingPublicKey publicKeyToSign, final String username)
    1.73 +      throws Exception {
    1.74 +    long ciat = System.currentTimeMillis();
    1.75 +    long cexp = ciat + JSONWebTokenUtils.DEFAULT_CERTIFICATE_DURATION_IN_MILLISECONDS;
    1.76 +    return createMockMyIDCertificate(publicKeyToSign, username, ciat, cexp);
    1.77 +  }
    1.78 +
    1.79 +  /**
    1.80 +   * Generate an assertion asserting ownership of username@mockmyid.com to a
    1.81 +   * relying party. The underlying certificate is signed by mockymid.com's
    1.82 +   * private key.
    1.83 +   *
    1.84 +   * @param keyPair
    1.85 +   *          to sign with.
    1.86 +   * @param username
    1.87 +   *          sign username@mockmyid.com.
    1.88 +   * @param certificateIssuedAt
    1.89 +   *          timestamp for certificate, in milliseconds since the epoch.
    1.90 +   * @param certificateExpiresAt
    1.91 +   *          expiration timestamp for certificate, in milliseconds since the epoch.
    1.92 +   * @param assertionIssuedAt
    1.93 +   *          timestamp for assertion, in milliseconds since the epoch; if null,
    1.94 +   *          no timestamp is included.
    1.95 +   * @param assertionExpiresAt
    1.96 +   *          expiration timestamp for assertion, in milliseconds since the epoch.
    1.97 +   * @return encoded assertion string.
    1.98 +   * @throws Exception
    1.99 +   */
   1.100 +  public String createMockMyIDAssertion(BrowserIDKeyPair keyPair, String username, String audience,
   1.101 +      long certificateIssuedAt, long certificateExpiresAt,
   1.102 +      Long assertionIssuedAt, long assertionExpiresAt)
   1.103 +          throws Exception {
   1.104 +    String certificate = createMockMyIDCertificate(keyPair.getPublic(), username,
   1.105 +        certificateIssuedAt, certificateExpiresAt);
   1.106 +    return JSONWebTokenUtils.createAssertion(keyPair.getPrivate(), certificate, audience,
   1.107 +        JSONWebTokenUtils.DEFAULT_ASSERTION_ISSUER, assertionIssuedAt, assertionExpiresAt);
   1.108 +  }
   1.109 +
   1.110 +  /**
   1.111 +   * Generate an assertion asserting ownership of username@mockmyid.com to a
   1.112 +   * relying party. The underlying certificate is signed by mockymid.com's
   1.113 +   * private key.
   1.114 +   *
   1.115 +   * @param keyPair
   1.116 +   *          to sign with.
   1.117 +   * @param username
   1.118 +   *          sign username@mockmyid.com.
   1.119 +   * @return encoded assertion string.
   1.120 +   * @throws Exception
   1.121 +   */
   1.122 +  public String createMockMyIDAssertion(BrowserIDKeyPair keyPair, String username, String audience)
   1.123 +      throws Exception {
   1.124 +    long ciat = System.currentTimeMillis();
   1.125 +    long cexp = ciat + JSONWebTokenUtils.DEFAULT_CERTIFICATE_DURATION_IN_MILLISECONDS;
   1.126 +    long aiat = ciat + 1;
   1.127 +    long aexp = aiat + JSONWebTokenUtils.DEFAULT_ASSERTION_DURATION_IN_MILLISECONDS;
   1.128 +    return createMockMyIDAssertion(keyPair, username, audience,
   1.129 +        ciat, cexp, aiat, aexp);
   1.130 +  }
   1.131 +}

mercurial