|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | +import os.path |
6 | 7 |
|
7 | 8 | from cryptography.hazmat.backends import default_backend |
8 | 9 | from cryptography.hazmat.primitives import serialization |
@@ -102,37 +103,60 @@ def generate(): |
102 | 103 | def _get_public(self): |
103 | 104 | return self.key.public_key() |
104 | 105 |
|
105 | | - def _build_minimal_ecdsa_privkey(self, der): |
| 106 | + def _build_minimal_ecdsa_privkey(self, der, format): |
106 | 107 | ''' |
107 | 108 | Builds a new DER that only includes the EC private key, removing the |
108 | 109 | public key that is added as an "optional" BITSTRING. |
109 | 110 | ''' |
110 | | - offset_PUB = 68 |
| 111 | + |
| 112 | + if format == serialization.PrivateFormat.OpenSSH: |
| 113 | + print(os.path.basename(__file__) + |
| 114 | + ': Warning: --minimal is supported only for PKCS8 ' |
| 115 | + 'or TraditionalOpenSSL formats') |
| 116 | + return bytearray(der) |
| 117 | + |
111 | 118 | EXCEPTION_TEXT = "Error parsing ecdsa key. Please submit an issue!" |
112 | | - if der[offset_PUB] != 0xa1: |
113 | | - raise ECDSAUsageError(EXCEPTION_TEXT) |
114 | | - len_PUB = der[offset_PUB + 1] |
115 | | - b = bytearray(der[:-offset_PUB]) |
116 | | - offset_SEQ = 29 |
117 | | - if b[offset_SEQ] != 0x30: |
118 | | - raise ECDSAUsageError(EXCEPTION_TEXT) |
119 | | - b[offset_SEQ + 1] -= len_PUB |
120 | | - offset_OCT_STR = 27 |
121 | | - if b[offset_OCT_STR] != 0x04: |
122 | | - raise ECDSAUsageError(EXCEPTION_TEXT) |
123 | | - b[offset_OCT_STR + 1] -= len_PUB |
124 | | - if b[0] != 0x30 or b[1] != 0x81: |
125 | | - raise ECDSAUsageError(EXCEPTION_TEXT) |
126 | | - b[2] -= len_PUB |
| 119 | + if format == serialization.PrivateFormat.PKCS8: |
| 120 | + offset_PUB = 68 # where the context specific TLV starts (tag 0xA1) |
| 121 | + if der[offset_PUB] != 0xa1: |
| 122 | + raise ECDSAUsageError(EXCEPTION_TEXT) |
| 123 | + len_PUB = der[offset_PUB + 1] + 2 # + 2 for 0xA1 0x44 bytes |
| 124 | + b = bytearray(der[:offset_PUB]) # remove the TLV with the PUB key |
| 125 | + offset_SEQ = 29 |
| 126 | + if b[offset_SEQ] != 0x30: |
| 127 | + raise ECDSAUsageError(EXCEPTION_TEXT) |
| 128 | + b[offset_SEQ + 1] -= len_PUB |
| 129 | + offset_OCT_STR = 27 |
| 130 | + if b[offset_OCT_STR] != 0x04: |
| 131 | + raise ECDSAUsageError(EXCEPTION_TEXT) |
| 132 | + b[offset_OCT_STR + 1] -= len_PUB |
| 133 | + if b[0] != 0x30 or b[1] != 0x81: |
| 134 | + raise ECDSAUsageError(EXCEPTION_TEXT) |
| 135 | + # as b[1] has bit7 set, the length is on b[2] |
| 136 | + b[2] -= len_PUB |
| 137 | + if b[2] < 0x80: |
| 138 | + del(b[1]) |
| 139 | + |
| 140 | + elif format == serialization.PrivateFormat.TraditionalOpenSSL: |
| 141 | + offset_PUB = 51 |
| 142 | + if der[offset_PUB] != 0xA1: |
| 143 | + raise ECDSAUsageError(EXCEPTION_TEXT) |
| 144 | + len_PUB = der[offset_PUB + 1] + 2 |
| 145 | + b = bytearray(der[0:offset_PUB]) |
| 146 | + b[1] -= len_PUB |
| 147 | + |
127 | 148 | return b |
128 | 149 |
|
129 | | - def get_private_bytes(self, minimal): |
| 150 | + def get_private_bytes(self, minimal, format): |
| 151 | + formats = {'pkcs8': serialization.PrivateFormat.PKCS8, |
| 152 | + 'openssl': serialization.PrivateFormat.TraditionalOpenSSL |
| 153 | + } |
130 | 154 | priv = self.key.private_bytes( |
131 | 155 | encoding=serialization.Encoding.DER, |
132 | | - format=serialization.PrivateFormat.PKCS8, |
| 156 | + format=formats[format], |
133 | 157 | encryption_algorithm=serialization.NoEncryption()) |
134 | 158 | if minimal: |
135 | | - priv = self._build_minimal_ecdsa_privkey(priv) |
| 159 | + priv = self._build_minimal_ecdsa_privkey(priv, formats[format]) |
136 | 160 | return priv |
137 | 161 |
|
138 | 162 | def export_private(self, path, passwd=None): |
|
0 commit comments