Signature mechanism

Updated at:

To ensure the security of API calls, Alibaba Cloud uses a signature to verify the identity of the sender for each API request. You must include a signature in each request, regardless of whether you submit it over HTTP or HTTPS.

Overview

For RPC API requests, you must add a signature to the query string in the following format:

https://Endpoint/?SignatureVersion=1.0&SignatureMethod=HMAC-SHA1&Signature=CT9X0VtwR86fNWSnsc6v8YGOjuE%3D&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf

The parameters are as follows:

  • SignatureMethod: The signature method. Only HMAC-SHA1 is supported.

  • SignatureVersion: The signature version. The current version is 1.0.

  • SignatureNonce: A unique random number used to prevent replay attacks. You must use a different random number for each request. We recommend that you use a universally unique identifier (UUID).

  • Signature: The signature string that is generated using the AccessKey secret to sign the request.

Calculate a signature

The signature algorithm is based on the RFC 2104 specification for Hash-based Message Authentication Code (HMAC). The algorithm uses your AccessKey secret to calculate the HMAC value of an encoded and sorted request string. This value is the signature. Because the content of each API request is different, the signature for each request is unique.

Signature = Base64( HMAC-SHA1( AccessSecret, UTF-8-Encoding-Of(
StringToSign)) )

To calculate a signature, follow these steps:

  1. Create a canonicalized query string from the request parameters.

    1. Sort all request parameters alphabetically by name. The parameters include both common request parameters and operation-specific parameters. The Signature parameter is not included in the sorting.

      For GET requests, these are the parameters in the request URI that follow the question mark (?) and are separated by ampersands (&).

    2. URL-encode the name and value of each sorted request parameter. You must use the UTF-8 character set for encoding.

      The encoding rules are as follows:

      Character

      Encoding method

      A-Z, a-z, 0-9, and the characters -, _, ., and ~

      Are not encoded.

      Other characters

      Are encoded in the format %XY, where XY is the two-digit hexadecimal representation of the character's ASCII code. For example, a double quotation mark (") is encoded as %22.

      Extended UTF-8 characters

      Are encoded in the format %XY%ZA….

      A space character

      Is encoded as %20 instead of a plus sign (+).

      This encoding method differs from the standard application/x-www-form-urlencoded Multipurpose Internet Mail Extensions (MIME) encoding algorithm, such as the implementation of java.net.URLEncoder in the Java standard library. To get the required encoded string, first encode the string using a standard library. Then, replace the plus signs (+) with %20, asterisks (*) with %2A, and %7E with tildes (~). The following percentEncode method implements this algorithm:

      private static final String ENCODING = "UTF-8";
      private static String percentEncode(String value) throws UnsupportedEncodingException 
      {
      return value != null ? URLEncoder.encode(value, ENCODING).replace("+", "%20").replace("*", "%2A").replace("%7E", "~") : null;
      }
  2. Create the string to sign from the canonicalized query string that you created in step 1. The string must be in the following format.

    StringToSign=
          HTTPMethod + "&" +
          percentEncode("/") + "&" +       percentEncode(CanonicalizedQueryString)

    Where:

    • HTTPMethod is the HTTP method used to submit the request, such as GET.

    • percentEncode("/") is the value that is obtained by URL-encoding the forward slash (/) character based on the rules in step 1. The encoded value is %2F.

    • percentEncode(CanonicalizedQueryString) is the URL-encoded canonicalized query string from step 1.

  3. Calculate the signature.

    1. Calculate the HMAC value of the string to sign (StringToSign) as defined in RFC 2104.

      Note

      The key used for the calculation is your AccessKey secret with an ampersand (&) character (ASCII code 38) appended. The SHA1 hash algorithm is used.

    2. Encode the HMAC value in Base64 to obtain the signature string (Signature).

    3. Add the resulting signature string to the request as the Signature parameter.

      Note

      When you add the signature string to the request, you must URL-encode it in the same way as other parameters, as specified in RFC 3986. For more information, see RFC 3986.

Example

This example shows how to sign a request to call the DescribeRegions operation. Assume that the AccessKeyId is testid and the AccessKey secret is testsecret. The request URL before it is signed is as follows:

http://ecs.aliyuncs.com/?Timestamp=2016-02-23T12:46:24Z&Format=XML&AccessKeyId=testid&Action=DescribeRegions&SignatureMethod=HMAC-SHA1&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf&Version=2014-05-26&SignatureVersion=1.0

The signature is calculated using the key testsecret&. The resulting signature is:

OLeaidS1JvxuMvnyHOwuJ+uX5qY=

Add the signature to the request URL as the Signature parameter. The final signed URL is as follows:

http://ecs.aliyuncs.com/?SignatureVersion=1.0&Action=DescribeRegions&Format=XML&SignatureNonce=3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf&Version=2014-05-26&AccessKeyId=testid&Signature=OLeaidS1JvxuMvnyHOwuJ+uX5qY=&SignatureMethod=HMAC-SHA1&Timestamp=2016-02-23T12%3A46%3A24Z