michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.background.fxa; michael@0: michael@0: import java.io.UnsupportedEncodingException; michael@0: import java.math.BigInteger; michael@0: import java.security.NoSuchAlgorithmException; michael@0: michael@0: import org.json.simple.JSONObject; michael@0: import org.mozilla.gecko.background.fxa.FxAccountClient10.CreateDelegate; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: import org.mozilla.gecko.sync.net.SRPConstants; michael@0: michael@0: public class FxAccount10CreateDelegate implements CreateDelegate { michael@0: protected final String email; michael@0: protected final String mainSalt; michael@0: protected final String srpSalt; michael@0: protected final BigInteger v; michael@0: michael@0: public FxAccount10CreateDelegate(String email, byte[] stretchedPWBytes, String mainSalt, String srpSalt) throws NoSuchAlgorithmException, UnsupportedEncodingException { michael@0: this.email = email; michael@0: this.mainSalt = mainSalt; michael@0: this.srpSalt = srpSalt; michael@0: byte[] srpSaltBytes = Utils.hex2Byte(srpSalt, FxAccountUtils.SALT_LENGTH_BYTES); michael@0: this.v = FxAccountUtils.srpVerifierLowercaseV(email.getBytes("UTF-8"), stretchedPWBytes, srpSaltBytes, SRPConstants._2048.g, SRPConstants._2048.N); michael@0: } michael@0: michael@0: @SuppressWarnings("unchecked") michael@0: @Override michael@0: public JSONObject getCreateBody() throws FxAccountClientException { michael@0: final JSONObject body = new JSONObject(); michael@0: try { michael@0: body.put("email", FxAccountUtils.bytes(email)); michael@0: } catch (UnsupportedEncodingException e) { michael@0: throw new FxAccountClientException(e); michael@0: } michael@0: michael@0: final JSONObject stretching = new JSONObject(); michael@0: stretching.put("type", "PBKDF2/scrypt/PBKDF2/v1"); michael@0: stretching.put("PBKDF2_rounds_1", 20000); michael@0: stretching.put("scrypt_N", 65536); michael@0: stretching.put("scrypt_r", 8); michael@0: stretching.put("scrypt_p", 1); michael@0: stretching.put("PBKDF2_rounds_2", 20000); michael@0: stretching.put("salt", mainSalt); michael@0: body.put("passwordStretching", stretching); michael@0: michael@0: final JSONObject srp = new JSONObject(); michael@0: srp.put("type", "SRP-6a/SHA256/2048/v1"); michael@0: srp.put("verifier", FxAccountUtils.hexModN(v, SRPConstants._2048.N)); michael@0: srp.put("salt", srpSalt); michael@0: body.put("srp", srp); michael@0: return body; michael@0: } michael@0: }