dnspython: Update to latest upstream.
[samba.git] / lib / dnspython / dns / dnssec.py
1 # Copyright (C) 2003-2007, 2009 Nominum, Inc.
2 #
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose with or without fee is hereby granted,
5 # provided that the above copyright notice and this permission notice
6 # appear in all copies.
7 #
8 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16 """Common DNSSEC-related functions and constants."""
17
18 RSAMD5 = 1
19 DH = 2
20 DSA = 3
21 ECC = 4
22 RSASHA1 = 5
23 DSANSEC3SHA1 = 6
24 RSASHA1NSEC3SHA1 = 7
25 RSASHA256 = 8
26 RSASHA512 = 10
27 INDIRECT = 252
28 PRIVATEDNS = 253
29 PRIVATEOID = 254
30
31 _algorithm_by_text = {
32     'RSAMD5' : RSAMD5,
33     'DH' : DH,
34     'DSA' : DSA,
35     'ECC' : ECC,
36     'RSASHA1' : RSASHA1,
37     'DSANSEC3SHA1' : DSANSEC3SHA1,
38     'RSASHA1NSEC3SHA1' : RSASHA1NSEC3SHA1,
39     'RSASHA256' : RSASHA256,
40     'RSASHA512' : RSASHA512,
41     'INDIRECT' : INDIRECT,
42     'PRIVATEDNS' : PRIVATEDNS,
43     'PRIVATEOID' : PRIVATEOID,
44     }
45
46 # We construct the inverse mapping programmatically to ensure that we
47 # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
48 # would cause the mapping not to be true inverse.
49
50 _algorithm_by_value = dict([(y, x) for x, y in _algorithm_by_text.iteritems()])
51
52 class UnknownAlgorithm(Exception):
53     """Raised if an algorithm is unknown."""
54     pass
55
56 def algorithm_from_text(text):
57     """Convert text into a DNSSEC algorithm value
58     @rtype: int"""
59     
60     value = _algorithm_by_text.get(text.upper())
61     if value is None:
62         value = int(text)
63     return value
64
65 def algorithm_to_text(value):
66     """Convert a DNSSEC algorithm value to text
67     @rtype: string"""
68     
69     text = _algorithm_by_value.get(value)
70     if text is None:
71         text = str(value)
72     return text