본문 바로가기
정보기술/일반

Canonical XML (c14n)

by fermi 2007. 10. 5.
- Canonical XML (c14n) 플레인 텍스트 비교와 디지털 서명을 위한 XML:
http://www.ibm.com/developerworks/kr/xml/standards/x-c14nspec.html

- Introducing XML canonical form(Uche Ogbuji): Canonical XML과 예제 (developerWorks, 2004년 12월):
http://www.ibm.com/developerworks/xml/library/x-c14n/


Canonical XML (c14n)
플레인 텍스트 비교와 디지털 서명을 위한 XML
  

난이도 : 중급

필자: W3C

2007 년 6 월 05 일

Canonical XML은 의미를 변경하지 않고 XML 신택스의 변화를 허용한다. 무엇보다도 테스팅과 디지털 서명에 유용하게 사용될 이 표준 메소드에 대해 알아보자.

Canonical XML Version 1.0 [W3C Recommendation]은 canonical form이라고 하는 XML 문서의 물리적 표현법을 만드는 표준 방식으로서, 의미를 변경하지 않고 XML 신택스에서 허용되는 다양한 변화들을 다루고 있다. 예를 들어, 애트리뷰트의 정렬 방식만 다른 두 문서가 있다고 가정하자. 비록 물리적인 표현상의 차이는 있을 수 있겠지만, XML에서 애트리뷰트 순서는 중요하지 않기 때문에, 이 두 문서들은 XML 1.0 표준에 의거하여 동일한 것으로 간주될 것이다. 이는 몇 가지 실질적인 문제들을 만들어 낸다. 예를 들어, 디지털로 암호화된 서명을 통해 문서 훼손을 방지한다고 가정해 보자. 애트리뷰트를 재 조정하면 서명이 깨질 수 있다. 물론 XML 표준에 의거하여 문서는 실제로 변하지는 않는다. 해결책은 서명, 텍스트 비교, 기타 연산을 수행하기 전에 문서들을 표준 폼으로 변환("canonicalization (c14n)")하는 것이다. 이로써 XML에서 변경 사항들이 올바르게 수용될 수 있도록 한다. 현재 Canonical XML 1.1 이 개발 중이고, 여기에서는 1.0 스팩이 가진 여러 중요한 문제들을 다루고 있다.

가끔은, 비교 또는 서명되어야 하는 XML은 하나의 큰 문서의 일부분일 경우가 있다. c14n은 네임스페이스 선언 같은 상세한 부분을 다룰 때 조차도 전체 소스 문서를 끄집어내야 한다. c14n이 문서 서브셋으로 엄격히 제한되어야 한다면 관련 알고리즘 Exclusive XML Canonicalization Version 1.0 [W3C Recommendation]을 사용해야 한다.


The rules of canonical form

The best overview of the c14n process is the following list (which I've edited), provided in the specification:

The document is encoded in UTF-8.
Line breaks are normalized to "#xA" on input, before parsing.
Attribute values are normalized, as if by a validating processor.
Default attributes are added to each element, as if by a validating processor.
CDATA sections are replaced with their literal character content.
Character and parsed entity references are replaced with the literal characters (excepting special characters).
Special characters in attribute values and character content are replaced by character references (as usual for well-formed XML).
The XML declaration and DTD are removed. (Note: I always recommend using an XML declaration in general, but I appreciate the reasoning behind omitting it in canonical XML form.)
Empty elements are converted to start-end tag pairs.
Whitespace outside of the document element and within start and end tags is normalized.
All whitespace in character content is retained (excluding characters removed during line feed normalization).
Attribute value delimiters are set to quotation marks (double quotes).
Superfluous namespace declarations are removed from each element.
Lexicographic order is imposed on the namespace declarations and attributes of each element.
Don't worry if some of these rules seem a bit unclear at this point. I'll provide longer explanations and examples of the more common rules in action. In this article, I don't cover any of the c14n steps that involve DTD validation. I have mentioned the XML Infoset several times, but interestingly enough the W3C chose to define c14n not in terms of the Infoset, but rather in terms of the XPath data model which is a simpler (and some argue cleaner) data model than the Infoset. This is probably a minor detail that will not affect much of your understanding of canonical form, but it's worth keeping in mind if you also have to work with Infoset-based technologies.