Ejemplo n.º 1
0
// Returns the base64 representation of the message after encryption with
// the given key. The key is a word as used in gosa-si.conf whose md5sum will
// be used as the actual AES key.
// If msg == "", "" will be returned.
func GosaEncrypt(msg string, key string) string {
	if msg == "" {
		return ""
	}
	aes, _ := aes.NewCipher([]byte(util.Md5sum(key)))
	crypter := cipher.NewCBCEncrypter(aes, config.InitializationVector)
	cyphertext := paddedMessage(msg)
	crypter.CryptBlocks(cyphertext, cyphertext)
	return string(util.Base64EncodeString(string(cyphertext)))
}
Ejemplo n.º 2
0
func testHash() {
	f := xml.NewHash("fruit", "banana")
	check(f.Verify(), nil)
	check(f, "<fruit>banana</fruit>")

	f = xml.NewHash("fruit", "banana", "")
	check(f.Verify(), nil)
	check(f, "<fruit><banana></banana></fruit>")

	f = xml.NewHash("fruit", "yellow", "banana")
	check(f.Verify(), nil)
	check(f, "<fruit><yellow>banana</yellow></fruit>")

	f = xml.NewHash("fruit", "yellow", "long", "banana")
	check(f.Verify(), nil)
	check(f, "<fruit><yellow><long>banana</long></yellow></fruit>")

	f = xml.NewHash("fruit", "yellow", "long", "banana")
	yellow := f.First("yellow")
	f.First("yellow").Rename("green")
	check(f.Verify(), nil)
	check(f, "<fruit><green><long>banana</long></green></fruit>")
	check(yellow.Name(), "green")

	x := xml.NewHash("foo")
	check(f.Verify(), nil)
	check(x, "<foo></foo>")

	x.SetText(">Dıes ist ein >>>Test<<<<")
	check(x.String(), "<foo>&gt;Dıes ist ein &gt;&gt;&gt;Test&lt;&lt;&lt;&lt;</foo>")
	check(x.Text(), ">Dıes ist ein >>>Test<<<<")
	foo_haver := xml.NewHash("foo_haver")
	foo_haver.AddClone(x)
	check(foo_haver.Verify(), nil)
	check(x.Verify(), nil)
	check(foo_haver.Get("foo"), []string{">Dıes ist ein >>>Test<<<<"})
	check(foo_haver.Text("foo"), ">Dıes ist ein >>>Test<<<<")
	check(foo_haver.Text(), "")
	check(foo_haver.String(), "<foo_haver><foo>&gt;Dıes ist ein &gt;&gt;&gt;Test&lt;&lt;&lt;&lt;</foo></foo_haver>")

	x.SetText("Dies ist %v %vter Test", "ein", 2)
	check(x, "<foo>Dies ist ein 2ter Test</foo>")

	bar := x.Add("bar")
	check(bar, "<bar></bar>")

	bar.Add("server", "srv1")
	check(x, "<foo>Dies ist ein 2ter Test<bar><server>srv1</server></bar></foo>")

	srv4 := bar.Add("server", "srv2")
	for _, s := range []string{"srv3", "srv4"} {
		srv4 = bar.Add("server", s)
	}
	check(x, "<foo>Dies ist ein 2ter Test<bar><server>srv1</server><server>srv2</server><server>srv3</server><server>srv4</server></bar></foo>")

	srv4.Add("alias", "foxtrott")
	srv4.Add("alias", "alpha")
	check(x, "<foo>Dies ist ein 2ter Test<bar><server>srv1</server><server>srv2</server><server>srv3</server><server>srv4<alias>foxtrott</alias><alias>alpha</alias></server></bar></foo>")

	x_clone := x.Clone()
	x_str := x.String()

	srv4.Add("alias", "bravo")
	check(x, "<foo>Dies ist ein 2ter Test<bar><server>srv1</server><server>srv2</server><server>srv3</server><server>srv4<alias>foxtrott</alias><alias>alpha</alias><alias>bravo</alias></server></bar></foo>")

	srv4.Add("alias", "delta")
	check(x, "<foo>Dies ist ein 2ter Test<bar><server>srv1</server><server>srv2</server><server>srv3</server><server>srv4<alias>foxtrott</alias><alias>alpha</alias><alias>bravo</alias><alias>delta</alias></server></bar></foo>")

	check(hash("a(b(c(d(x)))b(y)z)"), "<a>z<b><c><d>x</d></c></b><b>y</b></a>")

	lst := x.Get("foo") // x is a <foo> but contains no <foo> !!
	check(lst, []string{})

	lst = x.Get()
	check(lst, []string{})

	lst = x.Get("bar")
	check(lst, []string{bar.Text()})

	lst = bar.Get("server")
	check(lst, []string{"srv1", "srv2", "srv3", srv4.Text()})

	txt := srv4.Text("alias")
	check(txt, "foxtrott\u241ealpha\u241ebravo\u241edelta")

	txt = srv4.Text("alias", "doesnotexist")
	check(txt, "foxtrott\u241ealpha\u241ebravo\u241edelta")

	x.Add("max", "X")
	x.Add("moritz", "X")
	txt = x.Text("max", "moritz")
	check(txt, "X\u241eX")

	y, xmlerr := xml.StringToHash("<foo/><bar/>")
	check(y, "<bar></bar>") // last one wins
	check(xmlerr, "StringToHash(): Multiple top-level elements")

	y, xmlerr = xml.StringToHash("Nix gut")
	check(y, "<xml></xml>") // dummy returned if nothing properly parsed
	check(xmlerr, "StringToHash(): Stray text outside of tag: Nix gut")

	y, xmlerr = xml.StringToHash("<?xml version=\"1.0\"?><foo>We<bar>Hallo</bar>lt</foo>")
	check(y, "<foo>Welt<bar>Hallo</bar></foo>")
	check(xmlerr, "StringToHash(): Unsupported XML token: {xml [118 101 114 115 105 111 110 61 34 49 46 48 34]}")

	_, xmlerr = xml.StringToHash("<-foo></-foo>")
	check(xmlerr, "StringToHash(): XML syntax error on line 1: invalid XML name: -foo")

	y, xmlerr = xml.StringToHash("<foo bar=\"cool\"><drink more='yes' alcohol='sure'><sauf/></drink></foo>")
	check(y, "<foo><bar>cool</bar><drink><alcohol>sure</alcohol><more>yes</more><sauf></sauf></drink></foo>")
	check(xmlerr, nil)

	x_str2 := x.String()
	x_clone2 := x.AddClone(x)
	x_str3 := x.String()
	x_clone3 := x.AddClone(x)
	check(x_clone2, x_str2)
	check(x_clone3, x_str3)
	check(x.First("foo"), x_clone2)
	check(x.First("foo").Next(), x_clone3)
	check(x.First("foo").Next().First("foo"), x_clone2)

	check(x_clone, x_str)
	check(x_clone.Verify(), nil)
	check(x.Verify(), nil)
	check(x_clone2.Verify(), nil)
	check(x_clone3.Verify(), nil)

	broken := xml.NewHash("b0rken")
	var panic_err interface{}
	func() {
		defer func() {
			panic_err = recover()
		}()
		broken.AddWithOwnership(broken)
	}()
	check(panic_err, "AddWithOwnership: Sanity check failed!")

	panic_err = nil
	func() {
		defer func() {
			panic_err = recover()
		}()
		broken.AddWithOwnership(nil)
	}()
	check(panic_err, "AddWithOwnership: Sanity check failed!")

	panic_err = nil
	twin := broken.Add("twin")
	func() {
		defer func() {
			panic_err = recover()
		}()
		broken.AddWithOwnership(twin)
	}()
	check(panic_err, "AddWithOwnership: Sanity check failed!")

	panic_err = nil
	func() {
		defer func() {
			panic_err = recover()
		}()
		x.AddWithOwnership(x.First("foo"))
	}()
	check(panic_err, "AddWithOwnership: Sanity check failed!")

	family := xml.NewHash("family")
	mother := xml.NewHash("mother")
	mother.Add("abbr", "mom")
	father := xml.NewHash("father")
	father.Add("abbr", "dad")
	father.Add("abbr", "daddy")
	family.AddWithOwnership(mother)
	check(family.First("mother") == mother, true)
	family.AddWithOwnership(father)
	check(family.First("father") == father, true)
	mommy := xml.NewHash("abbr")
	mommy.SetText("mommy")
	mother.AddWithOwnership(mommy)
	mum := xml.NewHash("abbr")
	mum.SetText("mum")
	mother.AddWithOwnership(mum)
	mummy := xml.NewHash("abbr")
	mummy.SetText("mummy")
	mother.AddWithOwnership(mummy)
	check(mother.First("abbr").Next() == mommy, true)
	check(mommy.Next() == mum, true)
	check(mum.Next() == mummy, true)

	temp_mother := family.RemoveFirst("mother")
	check(temp_mother.Verify(), nil)
	family.AddWithOwnership(temp_mother)
	check(mother.RemoveFirst("not_present"), nil)
	mom := mother.First("abbr")
	mother.AddWithOwnership(mother.RemoveFirst("abbr"))
	check(mother.First("abbr") == mommy, true)
	check(mummy.Next() == mom, true)
	father.RemoveFirst("abbr")
	check(father.Text("abbr"), "daddy")
	check(mother.Get("abbr"), []string{"mommy", "mum", "mummy", "mom"})

	check(family.Verify(), nil)

	ducks := xml.NewHash("ducks")
	dewey := ducks.Add("duck", "dewey")
	donald := ducks.Add("duck", "donald")
	first_removed_duck := ducks.RemoveFirst("duck")
	check(first_removed_duck == dewey, true)
	check(dewey.Verify(), nil)
	daisy := ducks.Add("duck", "daisy")
	darkwing := ducks.Add("duck", "darkwing")
	check(ducks.First("duck") == donald, true)
	check(donald.Next() == daisy, true)
	check(daisy.Next() == darkwing, true)
	check(darkwing.Next() == nil, true)

	check(ducks.Remove(&filterafter{darkwing}), "<ducks></ducks>")
	check(ducks.Remove(&filterafter{donald}).RemoveFirst("duck") == daisy, true)
	check(daisy.Verify(), nil)
	check(ducks.Verify(), nil)
	check(daisy.Next(), nil)
	check(ducks, "<ducks><duck>donald</duck><duck>darkwing</duck></ducks>")
	ducks.AddWithOwnership(daisy)
	check(ducks.Remove(&filterafter{darkwing}).First("duck") == daisy, true)
	check(daisy.Verify(), nil)
	check(daisy.Next(), nil)
	check(ducks.Verify(), nil)
	check(ducks.Remove(&filterafter{donald}).First("duck") == darkwing, true)
	check(darkwing.Verify(), nil)
	check(darkwing.Next(), nil)
	check(ducks.Verify(), nil)
	check(ducks, "<ducks><duck>donald</duck></ducks>")

	for num_kids := 1; num_kids < 5; num_kids++ {
		kids := xml.NewHash("kids")
		for i := 0; i < num_kids; i++ {
			kids.Add("kid", fmt.Sprintf("%d", i+1))
		}
		test_kids := kids.Clone()
		check(test_kids.Verify(), nil)
		test_kid := test_kids.RemoveFirst("kid")
		if !check(test_kid.Verify(), nil) {
			fmt.Printf("FAILED for num_kids=%v\n", num_kids)
		}
		for i := 1; i < num_kids; i++ {
			test_kids = kids.Clone()
			test_kid = test_kids.First("kid")
			for j := 0; j < i; j++ {
				test_kid = test_kid.Next()
			}
			test_kid = test_kids.Remove(&filterafter{test_kid}).First("kid")
			check(test_kids.Verify(), nil)
			if i+1 == num_kids {
				check(test_kid, nil)
			} else {
				if !check(test_kid.Verify(), nil) {
					fmt.Printf("FAILED for num_kids=%v i=%v\n", num_kids, i)
				}
			}
		}
	}

	xyzzy := xml.NewHash("x")
	xyzzy.Add("y", "a")
	xyzzy.Add("y", "b")
	xyzzy.Add("y", "c")
	xyzzy.First("y").Add("A")
	xyzzy.First("y").Add("B")
	xyzzy.First("y").Add("C")
	xyzzy.First("y").Add("D")
	subtags := xyzzy.First("y").Subtags()
	sort.Strings(subtags)
	check(subtags, []string{"A", "B", "C", "D"})

	xmlreader := bytes.NewBufferString("<xml>\n<foo>\nbar</foo>\n</xml>\x00Should be ignored\n")
	x, xmlerr = xml.ReaderToHash(xmlreader)
	check(xmlerr, "StringToHash(): XML syntax error on line 5: illegal character code U+0000")

	xmlreader = bytes.NewBufferString("<xml>\n<foo>\nbar</foo>\n</xml>")
	x, xmlerr = xml.ReaderToHash(xmlreader)
	check(xmlerr, nil)
	check(x, "<xml>\n\n<foo>\nbar</foo></xml>")

	x = xml.NewHash("xml")
	letters := "eacdb"
	for i := range letters {
		child := x.Add(letters[i : i+1])
		for i := range letters {
			child.Add(letters[i : i+1])
		}
	}
	check(x.Verify(), nil)
	check(x.String(), "<xml><a><a></a><b></b><c></c><d></d><e></e></a><b><a></a><b></b><c></c><d></d><e></e></b><c><a></a><b></b><c></c><d></d><e></e></c><d><a></a><b></b><c></c><d></d><e></e></d><e><a></a><b></b><c></c><d></d><e></e></e></xml>")

	x = xml.NewHash("foo")
	check(x.FirstOrAdd("id"), "<id></id>")
	x.FirstOrAdd("id").SetText("1")
	check(x.FirstOrAdd("id"), "<id>1</id>")
	check(x.FirstOrAdd("bar"), "<bar></bar>")
	x.Rename("foobar")
	check(x, "<foobar><bar></bar><id>1</id></foobar>")

	godzilla := xml.NewHash("monster", "godzilla")
	godzilla.Add("home", "japan")
	godzilla.AppendString(" junior")
	monsters := xml.NewHash("monsters")
	monsters.AddWithOwnership(godzilla)
	monsters.Add("monster", "gamera")
	godclone := godzilla.Clone()
	check(godclone.Verify(), nil)
	check(godzilla.Next(), "<monster>gamera</monster>")
	check(godclone.Next(), nil)
	check(godclone, "<monster>godzilla junior<home>japan</home></monster>")
	monsters.SetText("are cool")
	check(monsters, "<monsters>are cool<monster>godzilla junior<home>japan</home></monster><monster>gamera</monster></monsters>")
	monsters.First("monster").SetText("mecha-%v", monsters.Get("monster")[0])
	check(monsters, "<monsters>are cool<monster>mecha-godzilla junior<home>japan</home></monster><monster>gamera</monster></monsters>")
	check(monsters.Verify(), nil)
	check(monsters.RemoveFirst("godzilla"), nil)
	check(monsters.Verify(), nil)
	check(monsters, "<monsters>are cool<monster>mecha-godzilla junior<home>japan</home></monster><monster>gamera</monster></monsters>")
	monsters.FirstOrAdd("creature").SetText("gollum")
	check(monsters.First("creature"), "<creature>gollum</creature>")
	check(godzilla.Next().Next(), nil)
	check(monsters.FirstChild().Element() == godzilla, true)
	check(monsters.FirstChild().Next().Element() == godzilla.Next(), true)
	st := monsters.Subtags()
	sort.Strings(st)
	check(st, []string{"creature", "monster"})
	check(len(st), 2)

	x = xml.NewHash("foo")
	whole := []byte{}
	for n := 0; n <= 12; n++ {
		buf := make([]byte, n)
		for i := range buf {
			buf[i] = byte(i)
		}
		x.AppendString(string(buf))
		whole = append(whole, buf...)
	}
	x.EncodeBase64()
	encoded := string(util.Base64EncodeString(string(whole)))
	check(x.Text(), encoded)
	x.DecodeBase64()
	check([]byte(x.Text()), whole)
	x.SetText()
	for i := range encoded {
		x.AppendString(string([]byte{encoded[i]}))
	}
	x.DecodeBase64()
	check([]byte(x.Text()), whole)

	x.SetText("H")
	x.EncodeBase64()
	check(x.Text(), "SA==")
	x.DecodeBase64()
	x.AppendString("a")
	x.EncodeBase64()
	check(x.Text(), "SGE=")
	x.DecodeBase64()
	x.AppendString("l")
	x.EncodeBase64()
	check(x.Text(), "SGFs")

	x.SetText("H")
	x.AppendString("a")
	x.EncodeBase64()
	check(x.Text(), "SGE=")

	x.SetText("H")
	x.AppendString("a")
	x.AppendString("l")
	x.EncodeBase64()
	check(x.Text(), "SGFs")

	x.SetText("H")
	x.AppendString("a")
	x.AppendString("l")
	x.AppendString("l")
	x.EncodeBase64()
	check(x.Text(), "SGFsbA==")

	x.SetText("H")
	x.AppendString("a")
	x.AppendString("l")
	x.AppendString("l")
	x.AppendString("o")
	x.EncodeBase64()
	check(x.Text(), "SGFsbG8=")

	x.SetText("Ha")
	x.AppendString("ll")
	x.AppendString("o")
	x.EncodeBase64()
	check(x.Text(), "SGFsbG8=")

	x.SetText("S")
	x.AppendString("G")
	x.AppendString("Fs")
	x.AppendString("b")
	x.AppendString("G8")
	x.DecodeBase64()
	check(x.Text(), "Hallo")

	testLDIF()
}
Ejemplo n.º 3
0
func testBase64() {
	check(util.Base64DecodeString("", nil), []byte{})
	check(util.Base64DecodeInPlace([]byte{}), []byte{})
	check(util.Base64DecodeString("=", nil), []byte{})
	check(util.Base64DecodeInPlace([]byte("=")), []byte{})
	check(util.Base64DecodeString("  =  ", nil), []byte{})
	check(util.Base64DecodeInPlace([]byte("  =  ")), []byte{})
	check(util.Base64DecodeString("/+/+", nil), []byte{0xff, 0xef, 0xfe})
	check(util.Base64DecodeInPlace([]byte("/+/+")), []byte{0xff, 0xef, 0xfe})
	check(util.Base64DecodeString("_-_-", nil), []byte{0xff, 0xef, 0xfe})
	check(util.Base64DecodeInPlace([]byte("_-_-")), []byte{0xff, 0xef, 0xfe})
	var devnull int
	check(string(util.Base64DecodeString("SGFsbG8=", nil)), "Hallo")
	check(string(util.Base64DecodeInPlace([]byte("SGFsbG8="))), "Hallo")
	check(string(util.Base64DecodeString("SGFsbA==", nil)), "Hall")
	check(string(util.Base64DecodeInPlace([]byte("SGFsbA=="))), "Hall")
	check(string(util.Base64DecodeString("SGFsbG8", nil)), "Hallo")
	check(string(util.Base64DecodeInPlace([]byte("SGFsbG8"))), "Hallo")
	check(string(util.Base64DecodeString("SGFsbA=", nil)), "Hall")
	check(string(util.Base64DecodeInPlace([]byte("SGFsbA="))), "Hall")
	check(string(util.Base64DecodeString("SGFsbG8===", nil)), "Hallo")
	check(string(util.Base64DecodeInPlace([]byte("SGFsbG8==="))), "Hallo")
	check(string(util.Base64DecodeString("SGFsbA", nil)), "Hall")
	check(string(util.Base64DecodeInPlace([]byte("SGFsbA"))), "Hall")
	check(string(util.Base64DecodeString("SGFsbG8=", &devnull)), "Hallo")
	check(devnull, 0)
	check(string(util.Base64DecodeString("SGFsbA==", &devnull)), "Hall")
	check(devnull, 0)
	check(string(util.Base64DecodeString("SGFsbA=", &devnull)), "Hall")
	check(devnull, 0)
	check(string(util.Base64DecodeString("SGFsbG8===", &devnull)), "Hallo")
	check(devnull, 0)
	check(string(util.Base64DecodeString("AA", nil)), "\000")
	check(string(util.Base64DecodeInPlace([]byte("AA"))), "\000")
	check(string(util.Base64DecodeString("AAA", nil)), "\000\000")
	check(string(util.Base64DecodeInPlace([]byte("AAA"))), "\000\000")
	var zerocarry int
	check(string(util.Base64DecodeString("AA", &zerocarry)), "")
	check(zerocarry != 0, true)
	check(string(util.Base64DecodeString("=", &zerocarry)), "\000")
	check(zerocarry, 0)
	check(string(util.Base64DecodeString("AAA", &zerocarry)), "")
	check(zerocarry != 0, true)
	check(string(util.Base64DecodeString("=", &zerocarry)), "\000\000")
	check(zerocarry, 0)

	testbuf := make([]byte, 1024)
	for i := range testbuf {
		testbuf[i] = byte(i)
	}

	error_list := ""
	for length := 0; length <= 12; length++ {
		for eq := 0; eq <= 4; eq++ {
			for err := 0; err <= 12; err++ {
				b64_1 := base64.StdEncoding.EncodeToString(testbuf[0 : 512-length])

				testslice := b64_1[0:]
				errors := []int{0}
				for e := 0; e < err; e++ {
					errors = append(errors, errors[e]+rand.Intn(len(testslice)-errors[e]))
				}
				errors = append(errors, len(testslice))
				teststr := ""
				for i := 0; i < len(errors)-1; i++ {
					if i != 0 {
						teststr = teststr + "\000\n"
					}
					teststr = teststr + testslice[errors[i]:errors[i+1]]
				}

				for i := 0; i < eq; i++ {
					teststr += "="
				}
				// because we're concatenating we need at least 1 "=" if the
				// first string ends in an incomplete block
				if eq == 0 && (length&3) != 0 {
					teststr += "="
				}

				b64_2 := base64.URLEncoding.EncodeToString(testbuf[512-length:])

				testslice = b64_2[0:]
				errors = []int{0}
				for e := 0; e < err; e++ {
					errors = append(errors, errors[e]+rand.Intn(len(testslice)-errors[e]))
				}
				errors = append(errors, len(testslice))
				for i := 0; i < len(errors)-1; i++ {
					if i != 0 {
						teststr = teststr + " "
					}
					teststr = teststr + testslice[errors[i]:errors[i+1]]
				}

				for i := 0; i < eq; i++ {
					teststr += "="
				}

				for parts := 0; parts < 5; parts++ {
					stops := []int{0}
					for e := 0; e < parts; e++ {
						stops = append(stops, stops[e]+rand.Intn(len(teststr)-stops[e]))
					}
					stops = append(stops, len(teststr))

					decoded := ""
					carry := 0
					for i := 0; i < len(stops)-1; i++ {
						decoded += string(util.Base64DecodeString(teststr[stops[i]:stops[i+1]], &carry))
					}

					if decoded != string(testbuf) {
						error_list += fmt.Sprintf("(util.Base64DecodeString() failed for length=%v eq=%v err=%v parts=%v)\n", length, eq, err, parts)
					}

					buffy := []byte(string(teststr))
					decbuffy := util.Base64DecodeInPlace(buffy)
					if &(decbuffy[0]) != &(buffy[0]) || // verify the in-place property
						string(decbuffy) != string(testbuf) {
						error_list += fmt.Sprintf("(util.Base64DecodeInPlace() failed for length=%v eq=%v err=%v parts=%v)\n", length, eq, err, parts)
					}
				}
			}
		}
	}

	check(error_list, "")

	buffy := make([]byte, 1024)
	st := "Nehmt, esst. Dies ist mein Leib - sprach der Schokohase"
	copy(buffy[32:], st)
	result := util.Base64EncodeInPlace(buffy[:32+len(st)], 32)
	check(&(buffy[0]) == &(result[0]), true)
	check(cap(result), cap(buffy))
	check(string(result), "TmVobXQsIGVzc3QuIERpZXMgaXN0IG1laW4gTGVpYiAtIHNwcmFjaCBkZXIgU2Nob2tvaGFzZQ==")

	st = "Nehmt, esst. Dies ist mein Leib - sprach der Schokohase\n"
	copy(buffy[256:], st)
	result = util.Base64EncodeInPlace(buffy[:256+len(st)], 256)
	check(&(buffy[0]) == &(result[0]), true)
	check(cap(result), cap(buffy))
	check(string(result), "TmVobXQsIGVzc3QuIERpZXMgaXN0IG1laW4gTGVpYiAtIHNwcmFjaCBkZXIgU2Nob2tvaGFzZQo=")

	for n := 0; n <= 12; n++ {
		buffy = make([]byte, n)
		for i := range buffy {
			buffy[i] = byte(i)
		}
		check(string(util.Base64EncodeString(string(buffy))), base64.StdEncoding.EncodeToString(buffy))
	}
}