Ejemplo n.º 1
0
func TestThumbprint(t *testing.T) {
	expected := []byte{55, 54, 203, 177, 120, 124, 184, 48, 156, 119, 238,
		140, 55, 5, 197, 225, 111, 251, 158, 133, 151, 21, 144, 31, 30, 76, 89,
		177, 17, 130, 245, 123,
	}
	n, err := buffer.FromBase64([]byte("0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw"))
	if !assert.NoError(t, err, "decode N succeeds") {
		return
	}
	e, err := buffer.FromBase64([]byte("AQAB"))
	if !assert.NoError(t, err, "decode E succeeds") {
		return
	}
	key := RsaPublicKey{
		EssentialHeader: &EssentialHeader{KeyType: "RSA"},
		N:               n,
		E:               e,
	}

	tp, err := key.Thumbprint(crypto.SHA256)
	if !assert.NoError(t, err, "Thumbprint should succeed") {
		return
	}

	if !assert.Equal(t, expected, tp, "Thumbprint should match") {
		return
	}
}
Ejemplo n.º 2
0
// parseCompact parses a JWS value serialized via compact serialization.
func parseCompact(buf []byte) (*Message, error) {
	parts := bytes.Split(buf, []byte{'.'})
	if len(parts) != 3 {
		return nil, ErrInvalidCompactPartsCount
	}

	enc := base64.RawURLEncoding

	hdrbuf, err := buffer.FromBase64(parts[0])
	if err != nil {
		return nil, err
	}

	debug.Printf("hdrbuf = %s", hdrbuf.Bytes())
	hdr := &EncodedHeader{Header: NewHeader()}
	if err := json.Unmarshal(hdrbuf.Bytes(), hdr.Header); err != nil {
		return nil, err
	}
	hdr.Source = hdrbuf

	payload, err := buffer.FromBase64(parts[1])
	if err != nil {
		return nil, err
	}

	signature := make([]byte, enc.DecodedLen(len(parts[2])))
	if _, err := enc.Decode(signature, parts[2]); err != nil {
		return nil, err
	}
	signature = bytes.TrimRight(signature, "\x00")

	s := NewSignature()
	s.Signature = signature
	s.ProtectedHeader = hdr
	m := &Message{
		Payload:    buffer.Buffer(payload),
		Signatures: []Signature{*s},
	}
	return m, nil
}
Ejemplo n.º 3
0
func TestAppendix_A3(t *testing.T) {
	var jwksrc = []byte(`{"keys":
       [
         {"kty":"oct",
          "alg":"A128KW",
          "k":"GawgguFyGrWKav7AX4VKUg"},

         {"kty":"oct",
          "k":"AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow",
          "kid":"HMAC key used in JWS spec Appendix A.1 example"}
       ]
     }`)
	set, err := Parse(jwksrc)
	if !assert.NoError(t, err, "Parse should succeed") {
		return
	}

	{
		key, ok := set.Keys[0].(*SymmetricKey)
		if !assert.True(t, ok, "set.Keys[0] should be a SymmetricKey") {
			return
		}

		bkey, err := buffer.FromBase64([]byte("GawgguFyGrWKav7AX4VKUg"))
		if !assert.NoError(t, err, "created key to compare") {
			return
		}

		ckey, err := key.Materialize()
		if !assert.NoError(t, err, "materialized key") {
			return
		}

		if !assert.Equal(t, jwa.OctetSeq, key.KeyType, "key type matches") ||
			!assert.Equal(t, jwa.A128KW.String(), key.Algorithm, "key algorithm matches") ||
			!assert.Equal(t, bkey.Bytes(), ckey, "key content matches") {
			return
		}
	}
}